Class: BirdbrainRequest
- Inherits:
-
Object
- Object
- BirdbrainRequest
- Defined in:
- lib/birdbrain/birdbrain_request.rb
Direct Known Subclasses
BirdbrainFinchInput, BirdbrainFinchOutput, BirdbrainHummingbirdInput, BirdbrainHummingbirdOutput, BirdbrainMicrobitInput, BirdbrainMicrobitOutput
Constant Summary collapse
- BIRDBRAIN_TEST =
false
Class Method Summary collapse
- .bounds(input, input_min, input_max, pass_through_input = nil) ⇒ Object
- .calculate_angle(intensity) ⇒ Object
- .calculate_intensity(intensity) ⇒ Object
- .calculate_left_or_right(direction) ⇒ Object
- .calculate_speed(speed) ⇒ Object
- .connected?(device) ⇒ Boolean
- .disconnect(device) ⇒ Object
- .not_connected?(device) ⇒ Boolean
- .request_status(status) ⇒ Object
- .response(*args) ⇒ Object
- .response_body(*args) ⇒ Object
- .uri(*args) ⇒ Object
- .xyz_response(device, sensor, type_method = 'to_f') ⇒ Object
Class Method Details
.bounds(input, input_min, input_max, pass_through_input = nil) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/birdbrain/birdbrain_request.rb', line 115 def self.bounds(input, input_min, input_max, pass_through_input = nil) return input if !pass_through_input.nil? && (input == pass_through_input) return input_min if input < input_min return input_max if input > input_max input end |
.calculate_angle(intensity) ⇒ Object
93 94 95 |
# File 'lib/birdbrain/birdbrain_request.rb', line 93 def self.calculate_angle(intensity) intensity * 255 / 180 end |
.calculate_intensity(intensity) ⇒ Object
97 98 99 |
# File 'lib/birdbrain/birdbrain_request.rb', line 97 def self.calculate_intensity(intensity) intensity * 255 / 100 end |
.calculate_left_or_right(direction) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/birdbrain/birdbrain_request.rb', line 108 def self.calculate_left_or_right(direction) return 'Left' if direction == BirdbrainDevice::LEFT return 'Right' if direction == BirdbrainDevice::RIGHT false end |
.calculate_speed(speed) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/birdbrain/birdbrain_request.rb', line 101 def self.calculate_speed(speed) return 255 if speed.between?(-10, 10) # QUESTION: why this calculation instead of normal mapping to 0..255 (and 255 means stop) ((speed * 23 / 100) + 122) end |
.connected?(device) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/birdbrain/birdbrain_request.rb', line 43 def self.connected?(device) response = response_body('hummingbird', 'in', 'orientation', 'Shake', device) !response.nil? end |
.disconnect(device) ⇒ Object
53 54 55 |
# File 'lib/birdbrain/birdbrain_request.rb', line 53 def self.disconnect(device) request_status(response_body('hummingbird', 'out', 'stopall', device)) end |
.not_connected?(device) ⇒ Boolean
49 50 51 |
# File 'lib/birdbrain/birdbrain_request.rb', line 49 def self.not_connected?(device) !connected?(device) end |
.request_status(status) ⇒ Object
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 |
# File 'lib/birdbrain/birdbrain_request.rb', line 57 def self.request_status(status) puts "Test: request status is #{status.inspect}" if BIRDBRAIN_TEST return nil if status.nil? return true if status == 'true' return true if status == 'led set' return true if status == 'triled set' return true if status == 'servo set' return true if status == 'buzzer set' return true if status == 'symbol set' return true if status == 'print set' return true if status == 'all stopped' return true if status == 'finch moved' return true if status == 'finch turned' return true if status == 'finch wheels started' return true if status == 'finch wheels stopped' return true if status == 'finch encoders reset' return false if status == 'false' return false if status == 'Not Connected' return false if status == 'Invalid orientation' nil end |
.response(*args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/birdbrain/birdbrain_request.rb', line 18 def self.response(*args) return false if (valid_args = args.flatten).include?(false) response = Net::HTTP.get_response(URI.parse(uri(valid_args))) sleep(0.01) # HACK: prevent http requests from overloading the bluebird connector response rescue Errno::ECONNREFUSED nil end |
.response_body(*args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/birdbrain/birdbrain_request.rb', line 30 def self.response_body(*args) response = response(args) return false if response == false return nil if response.nil? return nil if response.body.downcase == 'not connected' puts "Test: response: #{response.body.inspect}" if BIRDBRAIN_TEST response.body end |
.uri(*args) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/birdbrain/birdbrain_request.rb', line 9 def self.uri(*args) uri = 'http://127.0.0.1:30061' args.flatten.each { |s| uri += "/#{s}" } puts "Test: uri is #{uri}" if BIRDBRAIN_TEST uri end |
.xyz_response(device, sensor, type_method = 'to_f') ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/birdbrain/birdbrain_request.rb', line 84 def self.xyz_response(device, sensor, type_method = 'to_f') return nil if (x = response_body('hummingbird', 'in', sensor, 'X', device)).nil? y = response_body('hummingbird', 'in', sensor, 'Y', device) z = response_body('hummingbird', 'in', sensor, 'Z', device) [x.send(type_method), y.send(type_method), z.send(type_method)] end |