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
data/examples/led.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
|
6
|
+
led = Phidgets::LED.new
|
7
|
+
|
8
|
+
puts "Wait for PhidgetLED to attached..."
|
9
|
+
|
10
|
+
#The following method runs when the PhidgetLED is attached to the system
|
11
|
+
led.on_attach do |device, obj|
|
12
|
+
|
13
|
+
puts "Device attributes: #{device.attributes} attached"
|
14
|
+
puts "Class: #{device.device_class}"
|
15
|
+
puts "Id: #{device.id}"
|
16
|
+
puts "Serial number: #{device.serial_number}"
|
17
|
+
puts "Version: #{device.version}"
|
18
|
+
puts "# LEDs: #{device.leds.size}"
|
19
|
+
|
20
|
+
device.leds.each do |i|
|
21
|
+
device.leds[i].brightness = 50
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
led.on_detach do |device, obj|
|
27
|
+
puts "#{device.attributes.inspect} detached"
|
28
|
+
end
|
29
|
+
|
30
|
+
led.on_error do |device, obj, code, description|
|
31
|
+
puts "Error #{code}: #{description}"
|
32
|
+
end
|
33
|
+
|
34
|
+
sleep 5
|
35
|
+
|
36
|
+
led.close
|
data/examples/manager.rb
CHANGED
@@ -2,16 +2,22 @@ require 'rubygems'
|
|
2
2
|
require 'phidgets-ffi'
|
3
3
|
|
4
4
|
Phidgets::Manager.new do |manager|
|
5
|
-
puts manager.devices.inspect
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
manager.devices.size.times do |i|
|
7
|
+
puts "Attached: #{Phidgets::Common.name(manager.devices[i])}, serial number: #{Phidgets::Common.serial_number(manager.devices[i])}"
|
8
|
+
end
|
9
|
+
puts ''
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
manager.on_attach do |device_ptr, obj|
|
12
|
+
puts "Attaching #{Phidgets::Common.name(device_ptr)}, serial number: #{Phidgets::Common.serial_number(device_ptr)}"
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
manager.on_detach do |device_ptr, obj|
|
17
|
+
puts "Detaching #{Phidgets::Common.name(device_ptr)}, serial number: #{Phidgets::Common.serial_number(device_ptr)}"
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "You have 20 seconds to plug/unplug USB devices and watch the callbacks execute"
|
22
|
+
sleep 20
|
17
23
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
|
6
|
+
mc = Phidgets::MotorControl.new
|
7
|
+
|
8
|
+
puts "Wait for PhidgetMotorControl to attached..."
|
9
|
+
|
10
|
+
mc.on_attach do |device, obj|
|
11
|
+
puts 'attach event'
|
12
|
+
puts "Device attributes: #{device.attributes} attached"
|
13
|
+
puts "Class: #{device.device_class}"
|
14
|
+
puts "Id: #{device.id}"
|
15
|
+
puts "Serial number: #{device.serial_number}"
|
16
|
+
puts "Version: #{device.version}"
|
17
|
+
puts "# Motors: #{device.motors.size}"
|
18
|
+
puts "# Inputs: #{device.inputs.size}"
|
19
|
+
puts "# Encoders: #{device.encoders.size}"
|
20
|
+
puts "# Sensors: #{device.sensors.size}"
|
21
|
+
end
|
22
|
+
|
23
|
+
mc.on_detach do |device, obj|
|
24
|
+
puts "#{device.attributes.inspect} detached"
|
25
|
+
end
|
26
|
+
|
27
|
+
mc.on_error do |device, obj, code, description|
|
28
|
+
#puts "Error #{code}: #{description}"
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
mc.on_input_change do |device, input, state|
|
33
|
+
puts "Digital Input #{input.index}'s state changed to #{state}"
|
34
|
+
end
|
35
|
+
|
36
|
+
=begin
|
37
|
+
mc.on_sensor_update do |device, sensor, value|
|
38
|
+
puts "Analog Sensor #{sensor.index}'s value is #{value}"
|
39
|
+
end
|
40
|
+
=end
|
41
|
+
|
42
|
+
mc.on_velocity_change do |device, motor, velocity|
|
43
|
+
puts "Motor #{motor.index}'s velocity changed to #{velocity}"
|
44
|
+
end
|
45
|
+
|
46
|
+
mc.on_current_change do |device, motor, current|
|
47
|
+
puts "Motor #{motor.index}'s current changed to #{current}"
|
48
|
+
end
|
49
|
+
|
50
|
+
=begin
|
51
|
+
mc.on_current_update do |device, motor, current|
|
52
|
+
puts "Motor #{motor.index}'s current is #{current}"
|
53
|
+
end
|
54
|
+
=end
|
55
|
+
|
56
|
+
=begin
|
57
|
+
mc.on_back_emf_update do |device, motor, voltage|
|
58
|
+
puts "Motor #{motor.index}'s back EMF is #{voltage}"
|
59
|
+
end
|
60
|
+
=end
|
61
|
+
|
62
|
+
mc.on_position_change do |device, encoder, time, position|
|
63
|
+
puts "Encoder #{encoder.index}'s position changed to #{position} in #{time}"
|
64
|
+
end
|
65
|
+
|
66
|
+
=begin
|
67
|
+
mc.on_position_update do |device, encoder, position|
|
68
|
+
puts "Encoder #{encoder.index}'s position is #{position}"
|
69
|
+
end
|
70
|
+
=end
|
71
|
+
|
72
|
+
sleep 5
|
73
|
+
|
74
|
+
if(mc.attached?)
|
75
|
+
|
76
|
+
if(mc.id.to_s == "motor_control_1motor")
|
77
|
+
|
78
|
+
mc.ratiometric = true
|
79
|
+
|
80
|
+
puts "Digital input: #{mc.inputs.inspect}"
|
81
|
+
|
82
|
+
puts "Encoder: #{mc.encoders.inspect}"
|
83
|
+
|
84
|
+
puts "Analog sensor: #{mc.sensors[0].inspect}"
|
85
|
+
puts "Analog sensor: #{mc.sensors[1].inspect}"
|
86
|
+
|
87
|
+
puts "Motor current: #{mc.motors[0].current}"
|
88
|
+
|
89
|
+
mc.motors[0].acceleration = (mc.motors[0].acceleration_max)/2
|
90
|
+
mc.motors[0].back_emf_sensing = true
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
puts "Motor acceleration min: #{mc.motors[0].acceleration_min}"
|
95
|
+
puts "Motor acceleration max: #{mc.motors[0].acceleration_max}"
|
96
|
+
|
97
|
+
sleep 1
|
98
|
+
100.times do |i|
|
99
|
+
mc.motors[0].velocity = i
|
100
|
+
sleep 0.1
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
mc.motors[0].velocity = 0
|
105
|
+
|
106
|
+
sleep 2
|
107
|
+
end
|
108
|
+
|
@@ -25,6 +25,7 @@ ServerDisconnectedHandler = Proc.new { |dict, obj_ptr|
|
|
25
25
|
ptr = FFI::MemoryPointer.new(:pointer)
|
26
26
|
CPhidgetDictionary.create(ptr)
|
27
27
|
dictionary = ptr.get_pointer(0)
|
28
|
+
|
28
29
|
CPhidget_set_OnServerConnect_Handler(dictionary, ServerConnectedHandler, nil)
|
29
30
|
CPhidget_set_OnServerDisconnect_Handler(dictionary, ServerDisconnectedHandler, nil)
|
30
31
|
CPhidget_set_OnError_Handler(dictionary, ErrorHandler, nil)
|
@@ -32,7 +33,7 @@ CPhidget_set_OnError_Handler(dictionary, ErrorHandler, nil)
|
|
32
33
|
server_status = FFI::MemoryPointer.new(:int)
|
33
34
|
address = FFI::MemoryPointer.new(:string)
|
34
35
|
port = FFI::MemoryPointer.new(:int)
|
35
|
-
result = CPhidgetDictionary_openRemoteIP(dictionary, "
|
36
|
+
result = CPhidgetDictionary_openRemoteIP(dictionary, "localhost", 5001, nil);
|
36
37
|
while server_status.get_int(0) != 1
|
37
38
|
CPhidgetDictionary_getServerStatus(dictionary, server_status)
|
38
39
|
end
|
@@ -42,18 +43,27 @@ CPhidgetDictionary_getServerAddress(dictionary, address, port)
|
|
42
43
|
printf "Address: %s -- Port: %i \n", address.get_pointer(0).read_string, port.get_int(0)
|
43
44
|
|
44
45
|
# Set some keys
|
46
|
+
|
45
47
|
sleep 1
|
46
48
|
CPhidgetDictionary_addKey(dictionary, "TEST1", "1234", 1)
|
47
49
|
CPhidgetDictionary_addKey(dictionary, "TEST2", "5678", 1);
|
48
50
|
CPhidgetDictionary_addKey(dictionary, "TEST3", "9012", 0);
|
49
51
|
CPhidgetDictionary_addKey(dictionary, "TEST4", "3456", 1);
|
52
|
+
|
50
53
|
sleep 2
|
54
|
+
|
51
55
|
puts "Delete the 4th key...."
|
56
|
+
|
52
57
|
sleep 1
|
58
|
+
|
53
59
|
CPhidgetDictionary_removeKey(dictionary, "TEST4")
|
60
|
+
|
54
61
|
sleep 2
|
62
|
+
|
55
63
|
puts "Press any key to end"
|
56
64
|
$stdin.getc
|
65
|
+
|
57
66
|
CPhidgetDictionary_close(dictionary);
|
58
67
|
CPhidgetDictionary_delete(dictionary);
|
68
|
+
|
59
69
|
sleep 10
|
@@ -24,21 +24,38 @@ else
|
|
24
24
|
ifkit = ifkit.get_pointer(0)
|
25
25
|
end
|
26
26
|
|
27
|
-
# Create an attachment and detachment handler
|
28
27
|
AttachHandler = Proc.new { |device, ptr|
|
29
28
|
print "Device attached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
30
29
|
}
|
31
30
|
CPhidget_set_OnAttach_Handler(ifkit, AttachHandler, nil)
|
31
|
+
|
32
32
|
DetachHandler = Proc.new { |device, ptr|
|
33
33
|
print "Device detached: #{device_name_for(device)} (#{serial_number_for(device)})\n"
|
34
34
|
}
|
35
35
|
CPhidget_set_OnDetach_Handler(ifkit, DetachHandler, nil)
|
36
36
|
|
37
|
+
InputChangeHandler = Proc.new { |device, obj, ind, state|
|
38
|
+
print "Digital input #{ind} changed to #{(state == 0) ? false : true}\n"
|
39
|
+
}
|
40
|
+
CPhidgetInterfaceKit_set_OnInputChange_Handler(ifkit, InputChangeHandler, nil)
|
41
|
+
|
42
|
+
OutputChangeHandler = Proc.new { |device, obj, ind, state|
|
43
|
+
print "Digital output #{ind} changed to #{(state == 0) ? false : true}\n"
|
44
|
+
}
|
45
|
+
CPhidgetInterfaceKit_set_OnOutputChange_Handler(ifkit, OutputChangeHandler, nil)
|
46
|
+
|
47
|
+
SensorChangeHandler = Proc.new { |device, obj, ind, val|
|
48
|
+
print "Sensor #{ind} changed to #{val}\n"
|
49
|
+
}
|
50
|
+
CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifkit, SensorChangeHandler, nil)
|
51
|
+
|
37
52
|
CPhidget_open(ifkit, -1)
|
38
53
|
CPhidget_waitForAttachment(ifkit, 10000)
|
39
54
|
|
55
|
+
puts Phidgets::Common.serial_number(ifkit)
|
56
|
+
|
40
57
|
print "You have 20 seconds to attach/detach the interface kit. It should activate the callback handlers\n"
|
41
|
-
sleep
|
58
|
+
sleep 10
|
42
59
|
|
43
60
|
CPhidget_close(ifkit)
|
44
61
|
CPhidget_delete(ifkit)
|
File without changes
|
File without changes
|
@@ -8,15 +8,18 @@ manager = ptr.get_pointer(0)
|
|
8
8
|
CPhidgetManager.open(manager)
|
9
9
|
|
10
10
|
sleep(1)
|
11
|
-
devices_ptr, count = FFI::MemoryPointer.new(:pointer,
|
11
|
+
devices_ptr, count = FFI::MemoryPointer.new(:pointer, 70), FFI::MemoryPointer.new(:int)
|
12
12
|
CPhidgetManager.getAttachedDevices(manager, devices_ptr, count)
|
13
|
+
|
13
14
|
devices = devices_ptr.get_array_of_pointer(0, count.get_int(0))
|
14
15
|
|
15
16
|
ptr1 = FFI::MemoryPointer.new(:string)
|
16
17
|
ptr2 = FFI::MemoryPointer.new(:int)
|
17
|
-
devices.each_with_index do |device, i|
|
18
|
-
device = device.get_pointer(0)
|
19
18
|
|
19
|
+
count.get_int(0).times do |i|
|
20
|
+
device = devices[0].get_pointer(i*4) #On Mac OS X, it is i*4
|
21
|
+
# device = devices[0].get_pointer(i*4) #On Linux, it is i*8
|
22
|
+
|
20
23
|
Common.getDeviceName(device, ptr1)
|
21
24
|
name = ptr1.get_pointer(0).read_string
|
22
25
|
|
data/examples/rfid.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
|
6
|
+
rfid = Phidgets::RFID.new
|
7
|
+
|
8
|
+
puts "Wait for PhidgetRFID to attached..."
|
9
|
+
|
10
|
+
#The following method runs when the PhidgetRFID is attached to the system
|
11
|
+
rfid.on_attach do |device, obj|
|
12
|
+
|
13
|
+
puts "Device attributes: #{device.attributes} attached"
|
14
|
+
puts "Class: #{device.device_class}"
|
15
|
+
puts "Id: #{device.id}"
|
16
|
+
puts "Serial number: #{device.serial_number}"
|
17
|
+
puts "# Digital Outputs: #{device.outputs.size}"
|
18
|
+
|
19
|
+
rfid.outputs[0].state = true
|
20
|
+
puts rfid.outputs.inspect
|
21
|
+
|
22
|
+
rfid.antenna = true
|
23
|
+
rfid.led = true
|
24
|
+
sleep 1
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
rfid.on_detach do |device, obj|
|
29
|
+
puts "#{device.attributes.inspect} detached"
|
30
|
+
end
|
31
|
+
|
32
|
+
rfid.on_error do |device, obj, code, description|
|
33
|
+
puts "Error #{code}: #{description}"
|
34
|
+
end
|
35
|
+
|
36
|
+
rfid.on_output_change do |device, output, state, obj|
|
37
|
+
puts "Output #{output.index}'s state has changed to #{state}"
|
38
|
+
end
|
39
|
+
|
40
|
+
rfid.on_tag do |device, tag, obj|
|
41
|
+
puts "Tag #{tag} detected"
|
42
|
+
end
|
43
|
+
|
44
|
+
rfid.on_tag_lost do |device, tag, obj|
|
45
|
+
puts "Tag #{tag} removed"
|
46
|
+
end
|
47
|
+
|
48
|
+
puts "Please insert a tag to read read ..."
|
49
|
+
|
50
|
+
sleep 3
|
51
|
+
|
52
|
+
if(rfid.attached?)
|
53
|
+
sleep 1
|
54
|
+
|
55
|
+
begin
|
56
|
+
puts "Tag present: #{rfid.tag_present}"
|
57
|
+
puts "Last tag: #{rfid.last_tag}"
|
58
|
+
rescue Phidgets::Error::UnknownVal => e
|
59
|
+
puts "Exception caught: #{e.message}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
rfid.close
|
data/examples/servo.rb
CHANGED
@@ -2,35 +2,50 @@ require 'rubygems'
|
|
2
2
|
require 'phidgets-ffi'
|
3
3
|
|
4
4
|
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
5
|
+
|
6
|
+
servo = Phidgets::Servo.new
|
7
|
+
|
8
|
+
puts "Wait for PhidgetServo to attached..."
|
9
|
+
|
10
|
+
#The following method runs when the PhidgetServo is attached to the system
|
11
|
+
servo.on_attach do |device, obj|
|
12
|
+
|
13
|
+
puts "Device attributes: #{device.attributes} attached"
|
14
|
+
puts "Class: #{device.device_class}"
|
15
|
+
puts "Id: #{device.id}"
|
16
|
+
puts "Serial number: #{device.serial_number}"
|
17
|
+
puts "Version: #{device.version}"
|
18
|
+
puts "# Servos: #{device.servos.size}"
|
19
|
+
|
20
|
+
device.servos[0].engaged = true
|
21
|
+
device.servos[0].type = Phidgets::FFI::ServoType[:default]
|
22
|
+
puts "Setting servo parameters: #{device.servos[0].set_servo_parameters(600, 2000, 120)}"
|
23
|
+
sleep 1
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
servo.on_detach do |device, obj|
|
28
|
+
puts "#{device.attributes.inspect} detached"
|
29
|
+
end
|
30
|
+
|
31
|
+
servo.on_error do |device, obj, code, description|
|
32
|
+
puts "Error #{code}: #{description}"
|
33
|
+
end
|
34
|
+
|
35
|
+
servo.on_position_change do |device, motor, position|
|
36
|
+
puts "Moving servo #{motor.index} to #{position}"
|
37
|
+
end
|
38
|
+
|
39
|
+
sleep 5
|
40
|
+
|
41
|
+
if servo.attached?
|
42
|
+
max = servo.servos[0].position_max
|
43
|
+
3.times do
|
44
|
+
servo.servos[0].position = rand(max)
|
45
|
+
sleep 1
|
46
|
+
end
|
35
47
|
end
|
36
48
|
|
49
|
+
servo.servos[0].engaged = false
|
50
|
+
sleep 1
|
51
|
+
servo.close
|
data/examples/spatial.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'phidgets-ffi'
|
3
|
+
|
4
|
+
puts "Library Version: #{Phidgets::FFI.library_version}"
|
5
|
+
|
6
|
+
spatial = Phidgets::Spatial.new
|
7
|
+
|
8
|
+
puts "Wait for PhidgetSpatial to attached..."
|
9
|
+
|
10
|
+
#The following method runs when the PhidgetSpatial is attached to the system
|
11
|
+
spatial.on_attach do |device, obj|
|
12
|
+
|
13
|
+
puts "Device attributes: #{device.attributes} attached"
|
14
|
+
puts "Class: #{device.device_class}"
|
15
|
+
puts "Id: #{device.id}"
|
16
|
+
puts "Serial number: #{device.serial_number}"
|
17
|
+
puts "Version: #{device.version}"
|
18
|
+
puts "# Accelerometer Axes: #{device.accelerometer_axes.size}"
|
19
|
+
puts "# Compass Axes: #{device.compass_axes.size}"
|
20
|
+
puts "# Gyro Axes: #{device.gyro_axes.size}"
|
21
|
+
|
22
|
+
device.accelerometer_axes.size.times do |i|
|
23
|
+
puts "Acceleration Axis[#{i}]: #{device.accelerometer_axes[i].acceleration}"
|
24
|
+
puts "Acceleration Axis[#{i}] Min: #{device.accelerometer_axes[i].acceleration_min}"
|
25
|
+
puts "Acceleration Axis[#{i}] Max: #{device.accelerometer_axes[i].acceleration_max}"
|
26
|
+
end
|
27
|
+
|
28
|
+
device.compass_axes.size.times do |i|
|
29
|
+
#Even when there is a compass chip, sometimes there won't be valid data in the event.
|
30
|
+
begin
|
31
|
+
puts "Compass Axis[#{i}]: #{device.compass_axes[i].magnetic_field}"
|
32
|
+
rescue Phidgets::Error::UnknownVal => e
|
33
|
+
puts "Exception caught: #{e.message}"
|
34
|
+
end
|
35
|
+
puts "Compass Axis[#{i}] Min: #{device.compass_axes[i].magnetic_field_min}"
|
36
|
+
puts "Compass Axis[#{i}] Max: #{device.compass_axes[i].magnetic_field_max}"
|
37
|
+
end
|
38
|
+
|
39
|
+
device.gyro_axes.size.times do |i|
|
40
|
+
puts "Gyro Axis[#{i}]: #{device.gyro_axes[i].angular_rate}"
|
41
|
+
puts "Gyro Axis[#{i}] Min: #{device.gyro_axes[i].angular_rate_min}"
|
42
|
+
puts "Gyro Axis[#{i}] Max: #{device.gyro_axes[i].angular_rate_max}"
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
spatial.on_detach do |device, obj|
|
48
|
+
puts "#{device.attributes.inspect} detached"
|
49
|
+
end
|
50
|
+
|
51
|
+
spatial.on_error do |device, obj, code, description|
|
52
|
+
puts "Error #{code}: #{description}"
|
53
|
+
end
|
54
|
+
|
55
|
+
spatial.on_spatial_data do |device, acceleration, magnetic_field, angular_rate|
|
56
|
+
if acceleration.size > 0
|
57
|
+
puts "Acceleration: #{acceleration[0]}, #{acceleration[1]}, #{acceleration[2]}"
|
58
|
+
end
|
59
|
+
|
60
|
+
if magnetic_field.size > 0
|
61
|
+
puts "Magnetic field: #{magnetic_field[0]}, #{magnetic_field[1]}, #{magnetic_field[2]}"
|
62
|
+
end
|
63
|
+
|
64
|
+
if angular_rate.size > 0
|
65
|
+
puts "Angular rate: #{angular_rate[0]}, #{angular_rate[1]}, #{angular_rate[2]}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
sleep 2
|
70
|
+
|
71
|
+
if spatial.attached?
|
72
|
+
sleep 1
|
73
|
+
end
|
74
|
+
|
75
|
+
sleep 10
|