-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.rb
144 lines (99 loc) · 3.2 KB
/
sensor.rb
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/ruby
require 'rubygems'
require 'phidgets-ffi'
# Set to true if you want some extra output
debug = false
puts "Library Version: #{Phidgets::FFI.library_version}"
ifkit = Phidgets::InterfaceKit.new
adv = Phidgets::AdvancedServo.new
# Servo Functions
adv.on_attach do |device, obj|
puts "Servos Initilaized"
# Set the servo to engaged
device.advanced_servos[0].engaged = true
#allow time for engaged to be set before event ends
sleep 1
end
adv.on_detach do |device, obj|
puts "#{device.attributes.inspect} detached"
end
adv.on_error do |device, obj, code, description|
puts "Error #{code}: #{description}"
end
# Additional Output if you like to see
if debug
# Output the velocity change
adv.on_velocity_change do |device, servo, velocity, obj|
puts "Servo #{servo.index}'s velocity has changed to #{velocity}"
end
# Output the position change of the servo
adv.on_position_change do |device, servo, position, obj|
puts "Servo #{servo.index}'s position has changed to #{position}"
end
# Output the current of the servo
adv.on_current_change do |device, servo, current, obj|
puts "Servo #{servo.index}'s current has changed to #{current}"
end
end
# Interface kit functions
ifkit.on_attach do |device, obj|
if debug
puts "Device attributes: #{device.attributes} attached"
puts "Class: #{device.device_class}"
puts "Id: #{device.id}"
puts "Serial number: #{device.serial_number}"
puts "Version: #{device.version}"
puts "# Digital inputs: #{device.inputs.size}"
puts "# Digital outputs: #{device.outputs.size}"
puts "# Analog inputs: #{device.sensors.size}"
end
sleep 1
if(device.sensors.size > 0)
device.ratiometric = false
device.sensors[0].data_rate = 64
device.sensors[0].sensitivity = 15
if debug
puts "Sensivity: #{device.sensors[0].sensitivity}"
puts "Data rate: #{device.sensors[0].data_rate}"
puts "Data rate max: #{device.sensors[0].data_rate_max}"
puts "Data rate min: #{device.sensors[0].data_rate_min}"
puts "Sensor value[0]: #{device.sensors[0].to_i}"
puts "Raw sensor value[0]: #{device.sensors[0].raw_value}"
end
device.outputs[0].state = true
sleep 1 #allow time for digital output 0's state to be set
puts "Is digital output 0's state on? ... #{device.outputs[0].on?}"
end
end
if debug
ifkit.on_detach do |device, obj|
puts "#{device.attributes.inspect} detached"
end
ifkit.on_error do |device, obj, code, description|
puts "Error #{code}: #{description}"
end
ifkit.on_input_change do |device, input, state, obj|
puts "Input #{input.index}'s state has changed to #{state}"
end
ifkit.on_output_change do |device, output, state, obj|
puts "Output #{output.index}'s state has changed to #{state}"
end
end
ifkit.on_sensor_change do |device, input, value, obj|
position = value / 10
max = adv.advanced_servos[0].position_max
puts "Sensor #{input.index}'s value has changed to #{value}, Position: #{position}"
if position < max
adv.advanced_servos[0].position = position
end
end
sleep 10
# Closing the connection
puts 'DONE'
gets.chomp
# Closing the connection
puts 'DONE'
ifkit.close
adv.advanced_servos[0].engaged = false
sleep 1
adv.close