Class: Phidgets::MotorControl

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/phidgets-ffi/motor_control.rb

Overview

This class represents a PhidgetMotorControl.

Defined Under Namespace

Classes: MotorControlAnalogSensors, MotorControlDigitalInputs, MotorControlEncoders, MotorControlMotors

Constant Summary collapse

Klass =
Phidgets::FFI::CPhidgetMotorControl

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#attached?, #attached_to_server?, attributes, #close, #detached?, #detached_to_server?, device_class, #device_class, device_id, #id, #initialize, #label, #label=, #name, #on_attach, #on_detach, #on_error, #on_server_connect, #on_server_disconnect, #on_sleep, #on_wake, #serial_number, #server_address, server_address, #server_id, server_id, server_status, #type, #version, #wait_for_attachment

Instance Attribute Details

#attributesObject (readonly)

The attributes of a PhidgetMotorControl



28
29
30
# File 'lib/phidgets-ffi/motor_control.rb', line 28

def attributes
  @attributes
end

#encodersMotorControlEncoders (readonly)

Collection of encoders



15
16
17
# File 'lib/phidgets-ffi/motor_control.rb', line 15

def encoders
  @encoders
end

#inputsMotorControlDigitalInputs (readonly)

Collection of digital inputs



19
20
21
# File 'lib/phidgets-ffi/motor_control.rb', line 19

def inputs
  @inputs
end

#motorsMotorControlMotors (readonly)

Collection of motors

Returns:



11
12
13
# File 'lib/phidgets-ffi/motor_control.rb', line 11

def motors
  @motors
end

#sensorsMotorControlAnalogSensors (readonly)

Collection of analog sensors



23
24
25
# File 'lib/phidgets-ffi/motor_control.rb', line 23

def sensors
  @sensors
end

Instance Method Details

#on_back_emf_update(obj = nil, &block) ⇒ Boolean

Sets a Back EMF update handler callback function. This is called at a steady rate of 16ms, when BackEMF sensing is enabled.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_back_emf_update do |device, motor, voltage|
  puts "Motor #{motor.index}'s back EMF is #{voltage}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



126
127
128
129
130
131
132
# File 'lib/phidgets-ffi/motor_control.rb', line 126

def on_back_emf_update(obj=nil, &block)
  @on_back_emf_update_obj = obj
  @on_back_emf_update = Proc.new { |device, obj_ptr, motor, voltage|
    yield self, @motors[motor], voltage, object_for(obj_ptr)
  }
  Klass.set_OnBackEMFUpdate_Handler(@handle, @on_back_emf_update, pointer_for(obj))
end

#on_current_change(obj = nil, &block) ⇒ Boolean

Sets a current change handler callback function. This is called when the current consumed by a motor changes.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_current_change do |device, motor, current|
  puts "Motor #{motor.index}'s current changed to #{current}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



68
69
70
71
72
73
74
75
76
# File 'lib/phidgets-ffi/motor_control.rb', line 68

def on_current_change(obj=nil, &block)
	
  @on_current_change_obj = obj
  @on_current_change = Proc.new { |device, obj_ptr, motor, current|
 yield self, @motors[motor], current, object_for(obj_ptr)

	}
  Klass.set_OnCurrentChange_Handler(@handle, @on_current_change, pointer_for(obj))
end

#on_current_update(obj = nil, &block) ⇒ Boolean

Sets a current update handler callback function. This is called when the current consumed by a motor changes.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_current_update do |device, motor, current|
  puts "Motor #{motor.index}'s current is #{current}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



88
89
90
91
92
93
94
95
96
# File 'lib/phidgets-ffi/motor_control.rb', line 88

def on_current_update(obj=nil, &block)
	
  @on_current_update_obj = obj
  @on_current_update = Proc.new { |device, obj_ptr, motor, current|
 yield self, @motors[motor], current, object_for(obj_ptr)

	}
  Klass.set_OnCurrentChange_Handler(@handle, @on_current_update, pointer_for(obj))
end

#on_input_change(obj = nil, &block) ⇒ Boolean

Sets an input change handler callback function. This is called when a digital input on the PhidgetMotorControl board has changed.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_input_change do |device, input, state, obj|
  puts "Digital Input  #{input.index}, changed to #{state}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



108
109
110
111
112
113
114
# File 'lib/phidgets-ffi/motor_control.rb', line 108

def on_input_change(obj=nil, &block)
  @on_input_change_obj = obj
  @on_input_change = Proc.new { |device, obj_ptr, index, state|
    yield self, @inputs[index], (state == 0 ? false : true), object_for(obj_ptr)
  }
  Klass.set_OnInputChange_Handler(@handle, @on_input_change, pointer_for(obj))
end

#on_position_change(obj = nil, &block) ⇒ Boolean

Sets a position change handler callback function. This event provides data about how many ticks have occured, and how much time has passed since the last position change event, but does not contain an absolute position. This be be obtained from Phidgets::MotorControl::MotorControlEncoders#position

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_position_change do |device, encoder, time, position|
  puts "Encoder #{encoder.index}'s position changed to #{position} in #{time}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



144
145
146
147
148
149
150
151
152
# File 'lib/phidgets-ffi/motor_control.rb', line 144

def on_position_change(obj=nil, &block)
	
  @on_position_change_obj = obj
  @on_position_change = Proc.new { |device, obj_ptr, encoder, time, position|
 yield self, @encoders[encoder], time, position, object_for(obj_ptr)

	}
  Klass.set_OnEncoderPositionChange_Handler(@handle, @on_position_change, pointer_for(obj))
end

#on_position_update(obj = nil, &block) ⇒ Boolean

Sets a position update handler callback function. This event provides data about how many ticks have occured since the last update event. It is called at a steady rate of 8ms.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_position_update do |device, encoder, position|
  puts "Encoder #{encoder.index}'s position is #{position}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



164
165
166
167
168
169
170
171
172
# File 'lib/phidgets-ffi/motor_control.rb', line 164

def on_position_update(obj=nil, &block)
	
  @on_position_update_obj = obj
  @on_position_update = Proc.new { |device, obj_ptr, encoder, position|
 yield self, @encoders[encoder], position, object_for(obj_ptr)

	}
  Klass.set_OnEncoderPositionUpdate_Handler(@handle, @on_position_update, pointer_for(obj))
end

#on_sensor_update(obj = nil, &block) ⇒ Boolean

Sets a sensor update handler callback function. This is called at a steady rate of 8ms.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_sensor_update do |device, sensor, value|
  puts "Analog Sensor #{sensor.index}'s value is #{value}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



184
185
186
187
188
189
190
# File 'lib/phidgets-ffi/motor_control.rb', line 184

def on_sensor_update(obj=nil, &block)
  @on_sensor_update_obj = obj
  @on_sensor_update = Proc.new { |device, obj_ptr, index, value|
    yield self, @sensors[index], value, object_for(obj_ptr)
  }
  Klass.set_OnSensorUpdate_Handler(@handle, @on_sensor_update, pointer_for(obj))
end

#on_velocity_change(obj = nil, &block) ⇒ Boolean

Sets a velocity change handler callback function. This is called when the velocity of a motor changes.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

mc.on_velocity_change do |device, motor, velocity|
  puts "Motor #{motor.index}'s velocity changed to #{velocity}"
end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



48
49
50
51
52
53
54
55
56
# File 'lib/phidgets-ffi/motor_control.rb', line 48

def on_velocity_change(obj=nil, &block)
	
  @on_velocity_change_obj = obj
  @on_velocity_change = Proc.new { |device, obj_ptr, motor, velocity|
 yield self, @motors[motor], velocity, object_for(obj_ptr)

	}
  Klass.set_OnVelocityChange_Handler(@handle, @on_velocity_change, pointer_for(obj))
end

#ratiometricBoolean Also known as: ratiometric?

Returns the ratiometric state or raises an error

Returns:

  • (Boolean)

    returns the ratiometric state or raises an error



195
196
197
198
199
# File 'lib/phidgets-ffi/motor_control.rb', line 195

def ratiometric
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getRatiometric(@handle, ptr)
  (ptr.get_int(0) == 0) ? false : true
end

#ratiometric=(new_state) ⇒ Boolean

Sets the ratiometric state of the board.

Parameters:

  • new_state (Boolean)

    new ratiometric state

Returns:

  • (Boolean)

    returns the ratiometric state or raises an error



205
206
207
208
209
# File 'lib/phidgets-ffi/motor_control.rb', line 205

def ratiometric=(new_state)
  tmp = new_state ? 1 : 0
  Klass.setRatiometric(@handle, tmp)
  new_state
end