phidgets-ffi 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +1 -1
- data/README.rdoc +92 -42
- data/examples/accelerometer.rb +39 -0
- data/examples/advanced_servo.rb +94 -0
- data/examples/analog.rb +43 -0
- data/examples/bridge.rb +57 -0
- data/examples/dictionary.rb +46 -31
- data/examples/encoder.rb +59 -0
- data/examples/frequency_counter.rb +63 -0
- data/examples/gps.rb +91 -0
- data/examples/interface_kit_with_block.rb +68 -0
- data/examples/interface_kit_without_block.rb +60 -0
- data/examples/ir.rb +157 -0
- data/examples/led.rb +36 -0
- data/examples/manager.rb +16 -10
- data/examples/motor_control.rb +108 -0
- data/examples/{ffi → raw-ffi}/dictionary.rb +11 -1
- data/examples/{ffi → raw-ffi}/interface_kit.rb +19 -2
- data/examples/{ffi → raw-ffi}/library_version.rb +0 -0
- data/examples/{ffi → raw-ffi}/log.rb +0 -0
- data/examples/{ffi → raw-ffi}/manager.rb +6 -3
- data/examples/rfid.rb +63 -0
- data/examples/servo.rb +45 -30
- data/examples/spatial.rb +75 -0
- data/examples/stepper.rb +87 -0
- data/examples/temperature_sensor.rb +49 -0
- data/examples/text_lcd.rb +101 -0
- data/lib/phidgets-ffi.rb +34 -3
- data/lib/phidgets-ffi/accelerometer.rb +122 -0
- data/lib/phidgets-ffi/advanced_servo.rb +304 -0
- data/lib/phidgets-ffi/analog.rb +111 -0
- data/lib/phidgets-ffi/bridge.rb +167 -0
- data/lib/phidgets-ffi/common.rb +506 -103
- data/lib/phidgets-ffi/dictionary.rb +136 -23
- data/lib/phidgets-ffi/encoder.rb +196 -0
- data/lib/phidgets-ffi/error.rb +8 -3
- data/lib/phidgets-ffi/ffi/accelerometer.rb +30 -0
- data/lib/phidgets-ffi/ffi/advanced_servo.rb +73 -0
- data/lib/phidgets-ffi/ffi/analog.rb +29 -0
- data/lib/phidgets-ffi/ffi/bridge.rb +44 -0
- data/lib/phidgets-ffi/ffi/common.rb +51 -34
- data/lib/phidgets-ffi/ffi/constants.rb +3 -1
- data/lib/phidgets-ffi/ffi/dictionary.rb +25 -20
- data/lib/phidgets-ffi/ffi/encoder.rb +32 -0
- data/lib/phidgets-ffi/ffi/frequency_counter.rb +38 -0
- data/lib/phidgets-ffi/ffi/gps.rb +32 -0
- data/lib/phidgets-ffi/ffi/interface_kit.rb +26 -23
- data/lib/phidgets-ffi/ffi/ir.rb +50 -0
- data/lib/phidgets-ffi/ffi/led.rb +40 -0
- data/lib/phidgets-ffi/ffi/log.rb +7 -6
- data/lib/phidgets-ffi/ffi/manager.rb +35 -20
- data/lib/phidgets-ffi/ffi/motor_control.rb +66 -0
- data/lib/phidgets-ffi/ffi/rfid.rb +36 -0
- data/lib/phidgets-ffi/ffi/servo.rb +16 -15
- data/lib/phidgets-ffi/ffi/spatial.rb +40 -0
- data/lib/phidgets-ffi/ffi/stepper.rb +56 -0
- data/lib/phidgets-ffi/ffi/temperature_sensor.rb +42 -0
- data/lib/phidgets-ffi/ffi/text_lcd.rb +55 -0
- data/lib/phidgets-ffi/frequency_counter.rb +148 -0
- data/lib/phidgets-ffi/gps.rb +181 -0
- data/lib/phidgets-ffi/interface_kit.rb +205 -92
- data/lib/phidgets-ffi/ir.rb +290 -0
- data/lib/phidgets-ffi/led.rb +112 -0
- data/lib/phidgets-ffi/log.rb +14 -2
- data/lib/phidgets-ffi/manager.rb +143 -26
- data/lib/phidgets-ffi/motor_control.rb +497 -0
- data/lib/phidgets-ffi/phidgets-ffi.rb +15 -2
- data/lib/phidgets-ffi/rfid.rb +220 -0
- data/lib/phidgets-ffi/servo.rb +103 -61
- data/lib/phidgets-ffi/spatial.rb +306 -0
- data/lib/phidgets-ffi/stepper.rb +370 -0
- data/lib/phidgets-ffi/temperature_sensor.rb +157 -0
- data/lib/phidgets-ffi/text_lcd.rb +298 -0
- data/lib/phidgets-ffi/version.rb +1 -1
- data/phidgets-ffi.gemspec +2 -2
- metadata +89 -76
- data/examples/ffi/servo.rb +0 -67
- data/examples/interface_kit.rb +0 -20
@@ -0,0 +1,306 @@
|
|
1
|
+
module Phidgets
|
2
|
+
|
3
|
+
# This class represents a PhidgetSpatial.
|
4
|
+
class Spatial
|
5
|
+
|
6
|
+
Klass = Phidgets::FFI::CPhidgetSpatial
|
7
|
+
include Phidgets::Common
|
8
|
+
|
9
|
+
# Collection of accelerometer axes
|
10
|
+
# @return [SpatialAccelerometerAxes]
|
11
|
+
attr_reader :accelerometer_axes
|
12
|
+
|
13
|
+
# Collection of compass axes
|
14
|
+
# @return [SpatialCompassAxes]
|
15
|
+
attr_reader :compass_axes
|
16
|
+
|
17
|
+
# Collection of gyro axes
|
18
|
+
# @return [SpatialGyroAxes]
|
19
|
+
attr_reader :gyro_axes
|
20
|
+
|
21
|
+
attr_reader :attributes
|
22
|
+
|
23
|
+
# The attributes of a PhidgetSpatial
|
24
|
+
def attributes
|
25
|
+
super.merge({
|
26
|
+
:accelerometer_axes => accelerometer_axes.size,
|
27
|
+
:compass_axes => compass_axes.size,
|
28
|
+
:gyro_axes => gyro_axes.size
|
29
|
+
})
|
30
|
+
end
|
31
|
+
|
32
|
+
# Sets a spatial data handler callback function. This is called at a fixed rate as determined by the data rate property. Contains data for acceleration/gyro/compass depending on what the board supports.
|
33
|
+
#
|
34
|
+
# @param [String] obj Object to pass to the callback function. This is optional.
|
35
|
+
# @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
|
36
|
+
# @example
|
37
|
+
# spatial.on_spatial_data do |device, acceleration, magnetic_field, angular_rate, obj|
|
38
|
+
# puts "Acceleration: #{acceleration[0]},#{acceleration[1]}, #{acceleration[2]} | Magnetic field: #{magnetic_field[0]},#{magnetic_field[1]}, #{magnetic_field[2]} | Angular rate: #{angular_rate[0]},#{angular_rate[1]}, #{angular_rate[2]}" # end
|
39
|
+
# 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.
|
40
|
+
# @return [Boolean] returns true or raises an error
|
41
|
+
def on_spatial_data(obj=nil, &block)
|
42
|
+
@on_spatial_data_obj = obj
|
43
|
+
@on_spatial_data = Proc.new { |device, obj_ptr, data, data_count|
|
44
|
+
|
45
|
+
acceleration = []
|
46
|
+
if accelerometer_axes.size > 0
|
47
|
+
acceleration = [accelerometer_axes[0].acceleration, accelerometer_axes[1].acceleration, accelerometer_axes[2].acceleration]
|
48
|
+
end
|
49
|
+
|
50
|
+
magnetic_field= []
|
51
|
+
if compass_axes.size > 0
|
52
|
+
#Even when there is a compass chip, sometimes there won't be valid data in the event.
|
53
|
+
begin
|
54
|
+
magnetic_field = [compass_axes[0].magnetic_field, compass_axes[1].magnetic_field, compass_axes[2].magnetic_field]
|
55
|
+
rescue Phidgets::Error::UnknownVal => e
|
56
|
+
magnetic_field = ['Unknown', 'Unknown', 'Unknown']
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
angular_rate = []
|
61
|
+
if gyro_axes.size > 0
|
62
|
+
angular_rate = [gyro_axes[0].angular_rate, gyro_axes[1].angular_rate, gyro_axes[2].angular_rate]
|
63
|
+
end
|
64
|
+
|
65
|
+
yield self, acceleration, magnetic_field, angular_rate, object_for(obj_ptr)
|
66
|
+
}
|
67
|
+
Klass.set_OnSpatialData_Handler(@handle, @on_spatial_data, pointer_for(obj))
|
68
|
+
end
|
69
|
+
|
70
|
+
# Zeroes the gyro. This takes 1-2 seconds to complete and should only be called when the board is stationary.
|
71
|
+
# @return [Boolean] returns true if successful, or raises an error.
|
72
|
+
def zero_gyro
|
73
|
+
Klass.zeroGyro(@handle)
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
77
|
+
# Resets correction parameters for the magnetometer triad. This returns magnetometer output to raw magnetic field strength.
|
78
|
+
# @return [Boolean] returns true if successful, or raises an error.
|
79
|
+
def reset_compass_correction_parameters
|
80
|
+
Klass.resetCompassCorrectionParameters(@handle)
|
81
|
+
true
|
82
|
+
end
|
83
|
+
|
84
|
+
# Sets correction paramaters for the magnetometer triad. This is for filtering out hard and soft iron offsets, and scaling the output to match the local field strength. These parameters can be obtained from the compass calibration program provided by Phidgets Inc.
|
85
|
+
# @param [Integer] new_mag_field local magnetic field strength
|
86
|
+
# @param [Integer] new_offset0 axis 0 offset correction
|
87
|
+
# @param [Integer] new_offset1 axis 1 offset correction
|
88
|
+
# @param [Integer] new_offset2 axis 2 offset correction
|
89
|
+
# @param [Integer] new_gain0 axis 0 gain correction.
|
90
|
+
# @param [Integer] new_gain1 axis 1 gain correction.
|
91
|
+
# @param [Integer] new_gain2 axis 2 gain correction.
|
92
|
+
# @param [Integer] new_t0 non-orthogonality correction factor 0
|
93
|
+
# @param [Integer] new_t1 non-orthogonality correction factor 1
|
94
|
+
# @param [Integer] new_t2 non-orthogonality correction factor 2
|
95
|
+
# @param [Integer] new_t3 non-orthogonality correction factor 3
|
96
|
+
# @param [Integer] new_t4 non-orthogonality correction factor 4
|
97
|
+
# @param [Integer] new_t5 non-orthogonality correction factor 5
|
98
|
+
# @return [Boolean] returns true if successful, or raises an error.
|
99
|
+
def set_compass_correction_parameters(new_mag_field, new_offset0, new_offset1, new_offset2, new_gain0, new_gain1, new_gain2, new_t0, new_t1, new_t2, new_t3, new_t4, new_t5)
|
100
|
+
Klass.setCompassCorrectionParameters(@handle, new_mag_field, new_offset0, new_offset1, new_offset2, new_gain0, new_gain1, new_gain2, new_t0, new_t1, new_t2, new_t3, new_t4, new_t5)
|
101
|
+
true
|
102
|
+
end
|
103
|
+
|
104
|
+
# @return [Integer] returns data rate in ms, or raises an error.
|
105
|
+
def data_rate
|
106
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
107
|
+
Klass.getDataRate(@handle, @index, ptr)
|
108
|
+
ptr.get_int(0)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Sets the data rate in ms, or raises an error.
|
112
|
+
# @param [Integer] new_data_rate data rate
|
113
|
+
# @return [Integer] returns the data rate in ms, or raises an error.
|
114
|
+
def data_rate=(new_data_rate)
|
115
|
+
Klass.setDataRate(@handle, @index, new_data_rate.to_i)
|
116
|
+
new_data_rate.to_i
|
117
|
+
end
|
118
|
+
|
119
|
+
# @return [Integer] returns minimum data rate in ms, or raises an error.
|
120
|
+
def data_rate_min
|
121
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
122
|
+
Klass.getDataRateMin(@handle, @index, ptr)
|
123
|
+
ptr.get_int(0)
|
124
|
+
end
|
125
|
+
|
126
|
+
# @return [Integer] returns maximum data rate in ms, or raises an error.
|
127
|
+
def data_rate_max
|
128
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
129
|
+
Klass.getDataRateMax(@handle, @index, ptr)
|
130
|
+
ptr.get_int(0)
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def load_device_attributes
|
136
|
+
load_acceleromter_axes
|
137
|
+
load_compass_axes
|
138
|
+
load_gyro_axes
|
139
|
+
end
|
140
|
+
|
141
|
+
def load_acceleromter_axes
|
142
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
143
|
+
Klass.getAccelerationAxisCount(@handle, ptr)
|
144
|
+
@accelerometer_axes = []
|
145
|
+
ptr.get_int(0).times do |i|
|
146
|
+
@accelerometer_axes << SpatialAccelerometerAxes.new(@handle, i)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def load_compass_axes
|
151
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
152
|
+
Klass.getCompassAxisCount(@handle, ptr)
|
153
|
+
@compass_axes = []
|
154
|
+
ptr.get_int(0).times do |i|
|
155
|
+
@compass_axes << SpatialCompassAxes.new(@handle, i)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def load_gyro_axes
|
160
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
161
|
+
Klass.getGyroAxisCount(@handle, ptr)
|
162
|
+
@gyro_axes = []
|
163
|
+
ptr.get_int(0).times do |i|
|
164
|
+
@gyro_axes << SpatialGyroAxes.new(@handle, i)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# This class represents an axis of acceleration for a PhidgetSpatial. All the properties of an accelerometer axis are stored and modified in this class.
|
169
|
+
class SpatialAccelerometerAxes
|
170
|
+
Klass = Phidgets::FFI::CPhidgetSpatial
|
171
|
+
|
172
|
+
private
|
173
|
+
def initialize(handle, index)
|
174
|
+
@handle, @index = handle, index.to_i
|
175
|
+
end
|
176
|
+
|
177
|
+
public
|
178
|
+
|
179
|
+
# Displays data for the accelerometer axis
|
180
|
+
def inspect
|
181
|
+
"#<#{self.class} @acceleration=#{acceleration}, @acceleration_max=#{acceleration_max}, @acceleration_min=#{acceleration_min}>"
|
182
|
+
end
|
183
|
+
|
184
|
+
# @return [Integer] returns index of the accelerometer axis, or raises an error.
|
185
|
+
def index
|
186
|
+
@index
|
187
|
+
end
|
188
|
+
|
189
|
+
# @return [Float] returns the current acceleration in gs, or raises an error.
|
190
|
+
def acceleration
|
191
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
192
|
+
Klass.getAcceleration(@handle, @index, ptr)
|
193
|
+
ptr.get_double(0)
|
194
|
+
end
|
195
|
+
|
196
|
+
# @return [Float] returns the maximum acceleration that the sensor can measure, or raises an error.
|
197
|
+
def acceleration_max
|
198
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
199
|
+
Klass.getAccelerationMax(@handle, @index, ptr)
|
200
|
+
ptr.get_double(0)
|
201
|
+
end
|
202
|
+
|
203
|
+
# @return [Float] returns the minimum acceleration that the sensor can measure, or raises an error.
|
204
|
+
def acceleration_min
|
205
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
206
|
+
Klass.getAccelerationMin(@handle, @index, ptr)
|
207
|
+
ptr.get_double(0)
|
208
|
+
end
|
209
|
+
|
210
|
+
end #SpatialAccelerometerAxes
|
211
|
+
|
212
|
+
# This class represents an axis of compass for a PhidgetSpatial. All the properties of an compass axis are stored and modified in this class.
|
213
|
+
class SpatialCompassAxes
|
214
|
+
Klass = Phidgets::FFI::CPhidgetSpatial
|
215
|
+
|
216
|
+
private
|
217
|
+
def initialize(handle, index)
|
218
|
+
@handle, @index = handle, index.to_i
|
219
|
+
end
|
220
|
+
|
221
|
+
public
|
222
|
+
|
223
|
+
# Displays data for the compass axis
|
224
|
+
def inspect
|
225
|
+
"#<#{self.class} @magnetic_field=#{magnetic_field}, @magnetic_field_max=#{magnetic_field_max}, @magnetic_field_min=#{magnetic_field_min}>"
|
226
|
+
end
|
227
|
+
|
228
|
+
# @return [Integer] returns index of the compass axis, or raises an error.
|
229
|
+
def index
|
230
|
+
@index
|
231
|
+
end
|
232
|
+
|
233
|
+
# @return [Float] returns the magnetic field strength of the axis, in Gauss, or raises an error.
|
234
|
+
def magnetic_field
|
235
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
236
|
+
Klass.getMagneticField(@handle, @index, ptr)
|
237
|
+
ptr.get_double(0)
|
238
|
+
end
|
239
|
+
|
240
|
+
# @return [Float] returns the maximum magnetic field strength measurable by the compass axis, or raises an error.
|
241
|
+
def magnetic_field_max
|
242
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
243
|
+
Klass.getMagneticFieldMax(@handle, @index, ptr)
|
244
|
+
ptr.get_double(0)
|
245
|
+
end
|
246
|
+
|
247
|
+
# @return [Float] returns the minimum magnetic field strength measurable by the compass axis, or raises an error.
|
248
|
+
def magnetic_field_min
|
249
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
250
|
+
Klass.getMagneticFieldMin(@handle, @index, ptr)
|
251
|
+
ptr.get_double(0)
|
252
|
+
end
|
253
|
+
|
254
|
+
end #SpatialCompassAxes
|
255
|
+
|
256
|
+
# This class represents an axis of gyroscope for a PhidgetSpatial. All the properties of an gyroscope axis are stored and modified in this class.
|
257
|
+
class SpatialGyroAxes
|
258
|
+
Klass = Phidgets::FFI::CPhidgetSpatial
|
259
|
+
|
260
|
+
private
|
261
|
+
def initialize(handle, index)
|
262
|
+
@handle, @index = handle, index.to_i
|
263
|
+
end
|
264
|
+
|
265
|
+
public
|
266
|
+
|
267
|
+
# Displays data for the gyroscope axis
|
268
|
+
def inspect
|
269
|
+
"#<#{self.class} @angular_rate=#{angular_rate}, @angular_rate_max=#{angular_rate_max}, @angular_rate_min=#{angular_rate_min}"
|
270
|
+
end
|
271
|
+
|
272
|
+
# @return [Integer] returns index of the gyroscope axis, or raises an error.
|
273
|
+
def index
|
274
|
+
@index
|
275
|
+
end
|
276
|
+
|
277
|
+
# @return [Float] returns the angular rate of rotation for the gyro axis, in degress per second, or raises an error.
|
278
|
+
def angular_rate
|
279
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
280
|
+
Klass.getAngularRate(@handle, @index, ptr)
|
281
|
+
ptr.get_double(0)
|
282
|
+
end
|
283
|
+
|
284
|
+
# @return [Float] returns the maximum supported angular rate for the gyro axis, or raises an error.
|
285
|
+
def angular_rate_max
|
286
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
287
|
+
Klass.getAngularRateMax(@handle, @index, ptr)
|
288
|
+
ptr.get_double(0)
|
289
|
+
end
|
290
|
+
|
291
|
+
# @return [Float] returns the minimum supported angular rate for the gyro axis, or raises an error.
|
292
|
+
def angular_rate_min
|
293
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
294
|
+
Klass.getAngularRateMin(@handle, @index, ptr)
|
295
|
+
ptr.get_double(0)
|
296
|
+
end
|
297
|
+
|
298
|
+
end #SpatialGyroAxes
|
299
|
+
|
300
|
+
def remove_specific_event_handlers
|
301
|
+
Klass.set_OnSpatialData_Handler(@handle, nil, nil)
|
302
|
+
end
|
303
|
+
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
@@ -0,0 +1,370 @@
|
|
1
|
+
module Phidgets
|
2
|
+
|
3
|
+
# This class represents a PhidgetStepper.
|
4
|
+
class Stepper
|
5
|
+
|
6
|
+
Klass = Phidgets::FFI::CPhidgetStepper
|
7
|
+
include Phidgets::Common
|
8
|
+
|
9
|
+
# Collection of digital inputs
|
10
|
+
# @return [StepperDigitalInputs]
|
11
|
+
attr_reader :inputs
|
12
|
+
|
13
|
+
# Collection of stepper motors
|
14
|
+
# @return [StepperSteppers]
|
15
|
+
attr_reader :steppers
|
16
|
+
|
17
|
+
attr_reader :attributes
|
18
|
+
|
19
|
+
# The attributes of a PhidgetStepper
|
20
|
+
def attributes
|
21
|
+
super.merge({
|
22
|
+
:inputs => inputs.size,
|
23
|
+
:steppers => steppers.size,
|
24
|
+
})
|
25
|
+
end
|
26
|
+
|
27
|
+
# Sets an input change handler callback function. This is called when a digital input on the PhidgetStepper board has changed.
|
28
|
+
#
|
29
|
+
# @param [String] obj Object to pass to the callback function. This is optional.
|
30
|
+
# @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
|
31
|
+
# @example
|
32
|
+
# st.on_input_change do |device, input, state, obj|
|
33
|
+
# print "Digital Input #{input.index}, changed to #{state}\n"
|
34
|
+
# end
|
35
|
+
# 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.
|
36
|
+
# @return [Boolean] returns true or raises an error
|
37
|
+
def on_input_change(obj=nil, &block)
|
38
|
+
@on_input_change_obj = obj
|
39
|
+
@on_input_change = Proc.new { |device, obj_ptr, index, state|
|
40
|
+
yield self, @inputs[index], (state == 0 ? false : true), object_for(obj_ptr)
|
41
|
+
}
|
42
|
+
Klass.set_OnInputChange_Handler(@handle, @on_input_change, pointer_for(obj))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Sets a velocity change handler callback function. This is called when a stepper velocity changes.
|
46
|
+
#
|
47
|
+
# @param [String] obj Object to pass to the callback function. This is optional.
|
48
|
+
# @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
|
49
|
+
# @example
|
50
|
+
# st.on_velocity_change do |device, stepper, velocity, obj|
|
51
|
+
# puts "Stepper #{stepper.index}'s velocity has changed to #{velocity}"
|
52
|
+
# end
|
53
|
+
# 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.
|
54
|
+
# @return [Boolean] returns true or raises an error
|
55
|
+
def on_velocity_change(obj=nil, &block)
|
56
|
+
@on_velocity_change_obj = obj
|
57
|
+
@on_velocity_change = Proc.new { |device, obj_ptr, index, velocity|
|
58
|
+
yield self, @steppers[index], velocity, object_for(obj_ptr)
|
59
|
+
}
|
60
|
+
Klass.set_OnVelocityChange_Handler(@handle, @on_velocity_change, pointer_for(obj))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Sets a position change handler callback function. This is called when the stepper position changes.
|
64
|
+
#
|
65
|
+
# @param [String] obj Object to pass to the callback function. This is optional.
|
66
|
+
# @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
|
67
|
+
# @example
|
68
|
+
# st.on_position_change do |device, stepper, position, obj|
|
69
|
+
# puts "Stepper #{stepper.index}'s position has changed to #{position}"
|
70
|
+
# end
|
71
|
+
# 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.
|
72
|
+
# @return [Boolean] returns true or raises an error
|
73
|
+
def on_position_change(obj=nil, &block)
|
74
|
+
@on_position_change_obj = obj
|
75
|
+
@on_position_change = Proc.new { |device, obj_ptr, index, position|
|
76
|
+
yield self, @steppers[index], position, object_for(obj_ptr)
|
77
|
+
}
|
78
|
+
Klass.set_OnPositionChange_Handler(@handle, @on_position_change, pointer_for(obj))
|
79
|
+
end
|
80
|
+
|
81
|
+
# Sets a current change handler callback function. This is called when current consumption of a stepper changes. Not all PhidgetSteppers support current sense.
|
82
|
+
#
|
83
|
+
# @param [String] obj Object to pass to the callback function. This is optional.
|
84
|
+
# @param [Proc] Block When the callback is executed, the device and object are yielded to this block.
|
85
|
+
# @example
|
86
|
+
# st.on_current_change do |device, stepper, current, obj|
|
87
|
+
# puts "Stepper #{stepper.index}'s current has changed to #{current}"
|
88
|
+
# end
|
89
|
+
# 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.
|
90
|
+
# @return [Boolean] returns true or raises an error
|
91
|
+
def on_current_change(obj=nil, &block)
|
92
|
+
@on_current_change_obj = obj
|
93
|
+
@on_current_change = Proc.new { |device, obj_ptr, index, current|
|
94
|
+
yield self, @steppers[index], current, object_for(obj_ptr)
|
95
|
+
}
|
96
|
+
Klass.set_OnCurrentChange_Handler(@handle, @on_current_change, pointer_for(obj))
|
97
|
+
end
|
98
|
+
|
99
|
+
# This class represents a digital input for a PhidgetStepper. All the properties of an digital input are stored and modified in this class.
|
100
|
+
class StepperDigitalInputs
|
101
|
+
Klass = Phidgets::FFI::CPhidgetStepper
|
102
|
+
|
103
|
+
private
|
104
|
+
def initialize(handle, index)
|
105
|
+
@handle, @index = handle, index.to_i
|
106
|
+
end
|
107
|
+
|
108
|
+
public
|
109
|
+
|
110
|
+
# Displays data for the digital input
|
111
|
+
def inspect
|
112
|
+
"#<#{self.class} @index=#{index}, @state=#{state}>"
|
113
|
+
end
|
114
|
+
|
115
|
+
# @return [Integer] returns index of the digital input, or raises an error.
|
116
|
+
def index
|
117
|
+
@index
|
118
|
+
end
|
119
|
+
|
120
|
+
# @return [Boolean] returns state of the digital input, or raises an error.
|
121
|
+
def state
|
122
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
123
|
+
Klass.getInputState(@handle, @index, ptr)
|
124
|
+
(ptr.get_int(0) == 0) ? false : true
|
125
|
+
end
|
126
|
+
|
127
|
+
# @return [Boolean] returns true if the state is true.
|
128
|
+
def on
|
129
|
+
state == true
|
130
|
+
end
|
131
|
+
alias_method :on?, :on
|
132
|
+
|
133
|
+
# @return [Boolean] returns true if the state is off.
|
134
|
+
def off
|
135
|
+
!on
|
136
|
+
end
|
137
|
+
alias_method :off?, :off
|
138
|
+
end #StepperDigitalInputs
|
139
|
+
|
140
|
+
# This class represents a stepper motor of a PhidgetStepper. All the properties of a stepper motor are stored and modified in this class.
|
141
|
+
class StepperSteppers
|
142
|
+
Klass = Phidgets::FFI::CPhidgetStepper
|
143
|
+
|
144
|
+
private
|
145
|
+
def initialize(handle, index)
|
146
|
+
@handle, @index = handle, index.to_i
|
147
|
+
end
|
148
|
+
|
149
|
+
public
|
150
|
+
|
151
|
+
# Displays data for the stepper
|
152
|
+
def inspect
|
153
|
+
"#<#{self.class} @index=#{index}, @acceleration_min=#{acceleration_min}, @acceleration_max=#{acceleration_max}, @current_min=#{current_min}, @current_max=#{current_max}, @engaged=#{engaged}, @position_min=#{position_min}, @position_max=#{position_max}, @velocity_min=#{velocity_min}, @velocity_max=#{velocity_max}>"
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
# @return [Integer] returns index of the stepper, or raises an error.
|
158
|
+
def index
|
159
|
+
@index
|
160
|
+
end
|
161
|
+
|
162
|
+
# @return [Float] returns the acceleration of a stepper, in (micro)steps per second squared, or raises an error.
|
163
|
+
def acceleration
|
164
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
165
|
+
Klass.getAcceleration(@handle, @index, ptr)
|
166
|
+
ptr.get_double(0)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Sets the acceleration of a stepper, in (micro)steps per second squared, or raises an error.
|
170
|
+
# @param [Integer] new_acceleration new acceleration
|
171
|
+
# @return [Float] returns acceleration of a servo, in degrees/second, or raises an error.
|
172
|
+
def acceleration=(new_acceleration)
|
173
|
+
Klass.setAcceleration(@handle, @index, new_acceleration.to_f)
|
174
|
+
new_acceleration
|
175
|
+
end
|
176
|
+
|
177
|
+
# @return [Float] returns the largest acceleration value that the stepper will accept, or raises an error.
|
178
|
+
def acceleration_max
|
179
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
180
|
+
Klass.getAccelerationMax(@handle, @index, ptr)
|
181
|
+
ptr.get_double(0)
|
182
|
+
end
|
183
|
+
|
184
|
+
# @return [Float] returns the smallest acceleration value that the stepper will accept, or raises an error.
|
185
|
+
def acceleration_min
|
186
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
187
|
+
Klass.getAccelerationMin(@handle, @index, ptr)
|
188
|
+
ptr.get_double(0)
|
189
|
+
end
|
190
|
+
|
191
|
+
# @return [Float] returns the stepper's current usage, in Amps, or raises an error.
|
192
|
+
def current
|
193
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
194
|
+
Klass.getCurrent(@handle, @index, ptr)
|
195
|
+
ptr.get_double(0)
|
196
|
+
end
|
197
|
+
|
198
|
+
# @return [Float] returns the stepper's current usage limit, in Amps, or raises an error.
|
199
|
+
def current_limit
|
200
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
201
|
+
Klass.getCurrentLimit(@handle, @index, ptr)
|
202
|
+
ptr.get_double(0)
|
203
|
+
end
|
204
|
+
|
205
|
+
# Sets the stepper's current usage limit, in Amps, or raises an error.
|
206
|
+
# @param [Integer] new_current_limit new current limit
|
207
|
+
# @return [Float] returns current limit, in Amps, or raises an error.
|
208
|
+
def current_limit=(new_current_limit)
|
209
|
+
Klass.setCurrentLimit(@handle, @index, new_current_limit.to_f)
|
210
|
+
new_current_limit
|
211
|
+
end
|
212
|
+
|
213
|
+
# @return [Float] returns the largest current value that the stepper will accept, or raises an error.
|
214
|
+
def current_max
|
215
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
216
|
+
Klass.getCurrentMax(@handle, @index, ptr)
|
217
|
+
ptr.get_double(0)
|
218
|
+
end
|
219
|
+
|
220
|
+
# @return [Float] returns the smallest acceleration value that the stepper will accept, or raises an error.
|
221
|
+
def current_min
|
222
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
223
|
+
Klass.getCurrentMin(@handle, @index, ptr)
|
224
|
+
ptr.get_double(0)
|
225
|
+
end
|
226
|
+
|
227
|
+
# @return [Integer] returns the stepper's current position, in (micro)steps, or raises an error.
|
228
|
+
def current_position
|
229
|
+
ptr = ::FFI::MemoryPointer.new(:long_long)
|
230
|
+
Klass.getCurrentPosition(@handle, @index, ptr)
|
231
|
+
ptr.get_long_long(0)
|
232
|
+
end
|
233
|
+
|
234
|
+
# Sets the current position of a stepper, in (micro)steps, or raises an error.
|
235
|
+
# @param [Integer] new_current_position new current position
|
236
|
+
# @return [Integer] returns the current position of a stepper, in (micro)steps, or raises an error.
|
237
|
+
def current_position=(new_current_position)
|
238
|
+
Klass.setCurrentPosition(@handle, @index, new_current_position.to_i)
|
239
|
+
new_current_position
|
240
|
+
end
|
241
|
+
|
242
|
+
# Returns the engaged state of the stepper.
|
243
|
+
#
|
244
|
+
# @return [Boolean] returns the engaged state or raises an error
|
245
|
+
def engaged
|
246
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
247
|
+
Klass.getEngaged(@handle, @index, ptr)
|
248
|
+
(ptr.get_int(0) == 0) ? false : true
|
249
|
+
end
|
250
|
+
|
251
|
+
# Sets the engaged state of the Phidget.
|
252
|
+
# @param [Boolean] new_state new engaged state
|
253
|
+
# @return [Boolean] returns the engaged state or raises an error
|
254
|
+
def engaged=(new_state)
|
255
|
+
tmp = new_state ? 1 : 0
|
256
|
+
Klass.setEngaged(@handle, @index, tmp)
|
257
|
+
new_state
|
258
|
+
end
|
259
|
+
|
260
|
+
# @return [Integer] returns the largest position value that the stepper will accept, or raises an error.
|
261
|
+
def position_max
|
262
|
+
ptr = ::FFI::MemoryPointer.new(:long_long)
|
263
|
+
Klass.getPositionMax(@handle, @index, ptr)
|
264
|
+
ptr.get_long_long(0)
|
265
|
+
end
|
266
|
+
|
267
|
+
# @return [Integer] returns the smallest acceleration value that the stepper will accept, or raises an error.
|
268
|
+
def position_min
|
269
|
+
ptr = ::FFI::MemoryPointer.new(:long_long)
|
270
|
+
Klass.getPositionMin(@handle, @index, ptr)
|
271
|
+
ptr.get_long_long(0)
|
272
|
+
end
|
273
|
+
|
274
|
+
# @return [Boolean] returns the stopped state of a stepper, or raises an error.
|
275
|
+
def stopped
|
276
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
277
|
+
Klass.getStopped(@handle, @index, ptr)
|
278
|
+
ptr.get_int(0)
|
279
|
+
end
|
280
|
+
|
281
|
+
# @return [Integer] returns the stepper's target position, in (micro)steps, in degrees/second, or raises an error.
|
282
|
+
def target_position
|
283
|
+
ptr = ::FFI::MemoryPointer.new(:long_long)
|
284
|
+
Klass.getTargetPosition(@handle, @index, ptr)
|
285
|
+
ptr.get_long_long(0)
|
286
|
+
end
|
287
|
+
|
288
|
+
# Sets the target position of a stepper, in (micro)steps, or raises an error.
|
289
|
+
# @param [Integer] new_target_position new target position
|
290
|
+
# @return [Integer] returns the target position of a stepper, in (micro)steps, or raises an error.
|
291
|
+
def target_position=(new_target_position)
|
292
|
+
Klass.setTargetPosition(@handle, @index, new_target_position.to_i)
|
293
|
+
new_target_position
|
294
|
+
end
|
295
|
+
|
296
|
+
# @return [Float] returns the stepper's current velocity, in (micro)steps per second, or raises an error.
|
297
|
+
def velocity
|
298
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
299
|
+
Klass.getVelocity(@handle, @index, ptr)
|
300
|
+
ptr.get_double(0)
|
301
|
+
end
|
302
|
+
|
303
|
+
# @return [Float] returns the stepper's velocity limit, in (micro)steps per second, or raises an error.
|
304
|
+
def velocity_limit
|
305
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
306
|
+
Klass.getVelocityLimit(@handle, @index, ptr)
|
307
|
+
ptr.get_double(0)
|
308
|
+
end
|
309
|
+
|
310
|
+
# Sets the velocity limit of a stepper, in (micro)steps per second, or raises an error.
|
311
|
+
# @param [Integer] new_velocity_limit new velocity limit
|
312
|
+
# @return [Float] returns the velocity limit of a stepper, in (micro)steps per second, or raises an error.
|
313
|
+
def velocity_limit=(new_velocity_limit)
|
314
|
+
Klass.setVelocityLimit(@handle, @index, new_velocity_limit.to_f)
|
315
|
+
new_velocity_limit
|
316
|
+
end
|
317
|
+
|
318
|
+
# @return [Float] returns the largest velocity value that the stepper will accept, or raises an error.
|
319
|
+
def velocity_max
|
320
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
321
|
+
Klass.getVelocityMax(@handle, @index, ptr)
|
322
|
+
ptr.get_double(0)
|
323
|
+
end
|
324
|
+
|
325
|
+
# @return [Float] returns the smallest velocity value that the stepper will accept, or raises an error.
|
326
|
+
def velocity_min
|
327
|
+
ptr = ::FFI::MemoryPointer.new(:double)
|
328
|
+
Klass.getVelocityMin(@handle, @index, ptr)
|
329
|
+
ptr.get_double(0)
|
330
|
+
end
|
331
|
+
|
332
|
+
end #StepperSteppers
|
333
|
+
|
334
|
+
|
335
|
+
private
|
336
|
+
|
337
|
+
def load_device_attributes
|
338
|
+
load_inputs
|
339
|
+
load_stepper_steppers
|
340
|
+
end
|
341
|
+
|
342
|
+
def load_inputs
|
343
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
344
|
+
Klass.getInputCount(@handle, ptr)
|
345
|
+
|
346
|
+
@inputs = []
|
347
|
+
ptr.get_int(0).times do |i|
|
348
|
+
@inputs << StepperDigitalInputs.new(@handle, i)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
def load_stepper_steppers
|
353
|
+
ptr = ::FFI::MemoryPointer.new(:int)
|
354
|
+
Klass.getMotorCount(@handle, ptr)
|
355
|
+
|
356
|
+
@steppers = []
|
357
|
+
ptr.get_int(0).times do |i|
|
358
|
+
@steppers << StepperSteppers.new(@handle, i)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
def remove_specific_event_handlers
|
363
|
+
Klass.set_OnInputChange_Handler(@handle, nil, nil)
|
364
|
+
Klass.set_OnVelocityChange_Handler(@handle, nil, nil)
|
365
|
+
Klass.set_OnPositionChange_Handler(@handle, nil, nil)
|
366
|
+
Klass.set_OnCurrentChange_Handler(@handle, nil, nil)
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
end
|