Class: Mi100
- Inherits:
-
Object
- Object
- Mi100
- Defined in:
- lib/mi100/morsecoder.rb,
lib/mi100.rb,
lib/mi100/version.rb
Overview
morsecoder.rb Copyright © 2014 Masami Yamakawa
This software is released under the MIT License. opensource.org/lisenses/mit-license.php
Defined Under Namespace
Classes: Morsecoder
Constant Summary collapse
- DEFAULT_RETRIES =
5- READ_TIMEOUT =
5000- WRITE_TIMEOUT =
5000- SHORT_READ_TIMEOUT =
1- CMD_PING =
"H"- CMD_GET_POWER_LEVEL =
"H"- CMD_STOP =
"S"- CMD_MOVE_FORWARD =
"F"- CMD_MOVE_BACKWARD =
"B"- CMD_SPIN_RIGHT =
"R"- CMD_SPIN_LEFT =
"L"- CMD_BLINK_LED =
"D"- CMD_TONE =
"T"- CMD_GET_LIGHT =
"P"- CMD_TURN_RIGHT =
"U"- CMD_TURN_LEFT =
"A"- CMD_SET_SPEED =
"W"- CMD_FREE_RAM =
"M"- CMD_TURN_LED_RGB =
"V"- CMD_DRIVE =
"Z"- DEFAULT_SPEED =
1023- DEFAULT_MOVE_DURATION =
300- DEFAULT_SPIN_DURATION =
140- DEFAULT_DRIVE_DURATION =
50- DEFAULT_BLINK_DURATION =
600- DEFAULT_TONE_DURATION =
300- DEFAULT_MOVE_DIRECTION =
"FORWARD"- DEFAULT_SPIN_DIRECTION =
"RIGHT"- FREQUENCY =
{ DO: 262, DOS: 277, RE: 294, RES: 311, MI: 330, FA: 349, FAS: 370, SO: 392, SOS: 415, LA: 440, LAS: 466, SI: 494, HDO: 523, HDOS: 554, HRE: 587, HRES: 622, HMI: 659, HFA: 698, HFAS: 740, HSO: 784, HSOS: 831, HLA: 880, HSI: 988, }
- VERSION =
"0.4.2"
Instance Method Summary collapse
- #bad ⇒ Object
- #blink(r = nil, g = nil, b = nil, duration = DEFAULT_BLINK_DURATION) ⇒ Object
- #close ⇒ Object
- #drive(right_speed, left_speed, duration = @drive_duration) ⇒ Object
- #drive!(right_speed, left_speed, duration = @drive_duration) ⇒ Object
- #drive_duration(duration = DEFAULT_DRIVE_DURATION) ⇒ Object
- #drive_left(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
- #drive_left!(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
- #drive_right(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
- #drive_right!(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
- #free_ram ⇒ Object
- #good ⇒ Object
-
#initialize(dev) ⇒ Mi100
constructor
A new instance of Mi100.
- #led_off ⇒ Object
- #led_on ⇒ Object
- #light ⇒ Object
- #morse_frequency ⇒ Object
- #morse_frequency=(frequency) ⇒ Object
- #morse_unit ⇒ Object
- #morse_unit=(millisec) ⇒ Object
- #move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION) ⇒ Object
- #move_backward(duration) ⇒ Object
- #move_backward!(duration) ⇒ Object
- #move_forward(duration) ⇒ Object
- #move_forward!(duration) ⇒ Object
- #moveb(duration = DEFAULT_MOVE_DURATION) ⇒ Object
- #movef(duration = DEFAULT_MOVE_DURATION) ⇒ Object
- #ping ⇒ Object
- #power ⇒ Object
- #sound(pitch = "?", duration = DEFAULT_TONE_DURATION) ⇒ Object
- #speed(pwm_value = DEFAULT_SPEED) ⇒ Object
- #spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION) ⇒ Object
- #spin_left(duration) ⇒ Object
- #spin_left!(duration) ⇒ Object
- #spin_right(duration) ⇒ Object
- #spin_right!(duration) ⇒ Object
- #spinl(duration = DEFAULT_SPIN_DURATION) ⇒ Object
- #spinr(duration = DEFAULT_SPIN_DURATION) ⇒ Object
- #stop ⇒ Object
- #talk(str) ⇒ Object
- #tone(frequency = nil, duration = DEFAULT_TONE_DURATION) ⇒ Object
- #turn_led(r = 0, g = 0, b = 0) ⇒ Object
- #turn_left(duration) ⇒ Object
- #turn_left!(duration) ⇒ Object
- #turn_right(duration) ⇒ Object
- #turn_right!(duration) ⇒ Object
Constructor Details
#initialize(dev) ⇒ Mi100
Returns a new instance of Mi100.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mi100.rb', line 72 def initialize(dev) retries_left = DEFAULT_RETRIES begin initialize_serialport dev rescue Errno::ENOENT puts "Retry Bluetooth connection: #{retries_left.to_s}" retries_left -= 1 retry unless retries_left < 0 puts "Bluetooth connection failed." raise end initialize_robo end |
Instance Method Details
#bad ⇒ Object
273 274 275 276 277 278 |
# File 'lib/mi100.rb', line 273 def bad duration = 400.0 freq = 100 tone freq, duration sleep duration / 1000.0 end |
#blink(r = nil, g = nil, b = nil, duration = DEFAULT_BLINK_DURATION) ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/mi100.rb', line 218 def blink(r = nil, g = nil, b = nil, duration = DEFAULT_BLINK_DURATION) r ||= rand(100)+1 g ||= rand(100)+1 b ||= rand(100)+1 r = 100 if r > 100 g = 100 if g > 100 b = 100 if b > 100 send_command_get_response "#{CMD_BLINK_LED},#{r.to_s},#{g.to_s},#{b.to_s},#{duration.to_s}" end |
#close ⇒ Object
87 88 89 90 |
# File 'lib/mi100.rb', line 87 def close sleep 2 @sp.close end |
#drive(right_speed, left_speed, duration = @drive_duration) ⇒ Object
157 158 159 |
# File 'lib/mi100.rb', line 157 def drive(right_speed, left_speed, duration = @drive_duration) send_command_get_response "#{CMD_DRIVE},#{right_speed.to_s},#{left_speed.to_s},#{duration.to_s}" end |
#drive!(right_speed, left_speed, duration = @drive_duration) ⇒ Object
193 194 195 |
# File 'lib/mi100.rb', line 193 def drive!(right_speed, left_speed, duration = @drive_duration) sendln "#{CMD_DRIVE},#{right_speed.to_s},#{left_speed.to_s},#{duration.to_s}" end |
#drive_duration(duration = DEFAULT_DRIVE_DURATION) ⇒ Object
210 211 212 |
# File 'lib/mi100.rb', line 210 def drive_duration(duration = DEFAULT_DRIVE_DURATION) @drive_duration = duration end |
#drive_left(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
165 166 167 |
# File 'lib/mi100.rb', line 165 def drive_left(turn_speed, speed = @speed, duration = @drive_duration) send_command_get_response "#{CMD_DRIVE},#{(speed + turn_speed).to_s},#{speed.to_s},#{duration.to_s}" end |
#drive_left!(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
201 202 203 |
# File 'lib/mi100.rb', line 201 def drive_left!(turn_speed, speed = @speed, duration = @drive_duration) sendln "#{CMD_DRIVE},#{(speed + turn_speed).to_s},#{speed.to_s},#{duration.to_s}" end |
#drive_right(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
161 162 163 |
# File 'lib/mi100.rb', line 161 def drive_right(turn_speed, speed = @speed, duration = @drive_duration) send_command_get_response "#{CMD_DRIVE},#{speed.to_s},#{(speed + turn_speed).to_s},#{duration.to_s}" end |
#drive_right!(turn_speed, speed = @speed, duration = @drive_duration) ⇒ Object
197 198 199 |
# File 'lib/mi100.rb', line 197 def drive_right!(turn_speed, speed = @speed, duration = @drive_duration) sendln "#{CMD_DRIVE},#{speed.to_s},#{(speed + turn_speed).to_s},#{duration.to_s}" end |
#free_ram ⇒ Object
214 215 216 |
# File 'lib/mi100.rb', line 214 def free_ram send_command_get_response "#{CMD_FREE_RAM}" end |
#good ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/mi100.rb', line 264 def good freqs = [440,880,1760] duration = 100.0 freqs.each do |freq| tone freq, duration sleep duration / 1000.0 end end |
#led_off ⇒ Object
236 237 238 |
# File 'lib/mi100.rb', line 236 def led_off turn_led(0,0,0) end |
#led_on ⇒ Object
232 233 234 |
# File 'lib/mi100.rb', line 232 def led_on turn_led(100,100,100) end |
#light ⇒ Object
102 103 104 105 |
# File 'lib/mi100.rb', line 102 def light response = send_command_get_response CMD_GET_LIGHT response[1].to_i end |
#morse_frequency ⇒ Object
285 286 287 |
# File 'lib/mi100.rb', line 285 def morse_frequency Morsecoder.default_frequency end |
#morse_frequency=(frequency) ⇒ Object
289 290 291 |
# File 'lib/mi100.rb', line 289 def morse_frequency=(frequency) Morsecoder.default_frequency = frequency end |
#morse_unit ⇒ Object
293 294 295 |
# File 'lib/mi100.rb', line 293 def morse_unit Morsecoder.default_unit end |
#morse_unit=(millisec) ⇒ Object
297 298 299 |
# File 'lib/mi100.rb', line 297 def morse_unit=(millisec) Morsecoder.default_unit = millisec end |
#move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION) ⇒ Object
107 108 109 110 |
# File 'lib/mi100.rb', line 107 def move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION) cmd = direction.upcase == "BACKWARD" ? CMD_MOVE_BACKWARD : CMD_MOVE_FORWARD send_command_get_response "#{cmd},#{duration.to_s}" end |
#move_backward(duration) ⇒ Object
121 122 123 |
# File 'lib/mi100.rb', line 121 def move_backward(duration) send_command_get_response "#{CMD_MOVE_BACKWARD},#{duration.to_s}" end |
#move_backward!(duration) ⇒ Object
173 174 175 |
# File 'lib/mi100.rb', line 173 def move_backward!(duration) sendln "#{CMD_MOVE_BACKWARD},#{duration.to_s}" end |
#move_forward(duration) ⇒ Object
117 118 119 |
# File 'lib/mi100.rb', line 117 def move_forward(duration) send_command_get_response "#{CMD_MOVE_FORWARD},#{duration.to_s}" end |
#move_forward!(duration) ⇒ Object
169 170 171 |
# File 'lib/mi100.rb', line 169 def move_forward!(duration) sendln "#{CMD_MOVE_FORWARD},#{duration.to_s}" end |
#moveb(duration = DEFAULT_MOVE_DURATION) ⇒ Object
145 146 147 |
# File 'lib/mi100.rb', line 145 def moveb(duration = DEFAULT_MOVE_DURATION) move_backward duration end |
#movef(duration = DEFAULT_MOVE_DURATION) ⇒ Object
141 142 143 |
# File 'lib/mi100.rb', line 141 def movef(duration = DEFAULT_MOVE_DURATION) move_forward duration end |
#ping ⇒ Object
92 93 94 |
# File 'lib/mi100.rb', line 92 def ping send_command_get_response CMD_PING end |
#power ⇒ Object
96 97 98 99 100 |
# File 'lib/mi100.rb', line 96 def power response = send_command_get_response CMD_GET_POWER_LEVEL voltage = response[1].to_i / 1000.0 voltage.round 2 end |
#sound(pitch = "?", duration = DEFAULT_TONE_DURATION) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/mi100.rb', line 251 def sound(pitch = "?", duration = DEFAULT_TONE_DURATION) if pitch.instance_of?(String) pitch = FREQUENCY.keys[rand(FREQUENCY.size)].to_s if pitch == "?" frequency = FREQUENCY[pitch.upcase.to_sym] else frequency = pitch end tone frequency, duration if frequency sleep duration.to_f / 1000.0 end |
#speed(pwm_value = DEFAULT_SPEED) ⇒ Object
205 206 207 208 |
# File 'lib/mi100.rb', line 205 def speed(pwm_value = DEFAULT_SPEED) @speed = pwm_value send_command_get_response "#{CMD_SET_SPEED},#{pwm_value.to_s}" end |
#spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION) ⇒ Object
112 113 114 115 |
# File 'lib/mi100.rb', line 112 def spin(direction = DEFAULT_SPIN_DIRECTION, duration = DEFAULT_SPIN_DURATION) cmd = direction.upcase == "LEFT" ? CMD_SPIN_LEFT : CMD_SPIN_RIGHT send_command_get_response "#{cmd},#{duration.to_s}" end |
#spin_left(duration) ⇒ Object
129 130 131 |
# File 'lib/mi100.rb', line 129 def spin_left(duration) send_command_get_response "#{CMD_SPIN_LEFT},#{duration.to_s}" end |
#spin_left!(duration) ⇒ Object
181 182 183 |
# File 'lib/mi100.rb', line 181 def spin_left!(duration) sendln "#{CMD_SPIN_LEFT},#{duration.to_s}" end |
#spin_right(duration) ⇒ Object
125 126 127 |
# File 'lib/mi100.rb', line 125 def spin_right(duration) send_command_get_response "#{CMD_SPIN_RIGHT},#{duration.to_s}" end |
#spin_right!(duration) ⇒ Object
177 178 179 |
# File 'lib/mi100.rb', line 177 def spin_right!(duration) sendln "#{CMD_SPIN_RIGHT},#{duration.to_s}" end |
#spinl(duration = DEFAULT_SPIN_DURATION) ⇒ Object
153 154 155 |
# File 'lib/mi100.rb', line 153 def spinl(duration = DEFAULT_SPIN_DURATION) spin_left duration end |
#spinr(duration = DEFAULT_SPIN_DURATION) ⇒ Object
149 150 151 |
# File 'lib/mi100.rb', line 149 def spinr(duration = DEFAULT_SPIN_DURATION) spin_right duration end |
#stop ⇒ Object
240 241 242 |
# File 'lib/mi100.rb', line 240 def stop send_command_get_response CMD_STOP end |
#talk(str) ⇒ Object
280 281 282 283 |
# File 'lib/mi100.rb', line 280 def talk(str) morsecoder = Morsecoder.new str morsecoder.each {|frequency, duration| sound(frequency,duration)} end |
#tone(frequency = nil, duration = DEFAULT_TONE_DURATION) ⇒ Object
244 245 246 247 248 249 |
# File 'lib/mi100.rb', line 244 def tone(frequency = nil, duration = DEFAULT_TONE_DURATION) frequency ||= rand(4186 - 28) + 28 frequency = 4186 if frequency > 4186 frequency = 28 if frequency < 28 send_command_get_response "#{CMD_TONE},#{frequency.to_s},#{duration.to_s}" end |
#turn_led(r = 0, g = 0, b = 0) ⇒ Object
228 229 230 |
# File 'lib/mi100.rb', line 228 def turn_led(r = 0, g = 0, b = 0) send_command_get_response "#{CMD_TURN_LED_RGB},#{r.to_s},#{g.to_s},#{b.to_s}" end |
#turn_left(duration) ⇒ Object
137 138 139 |
# File 'lib/mi100.rb', line 137 def turn_left(duration) send_command_get_response "#{CMD_TURN_LEFT},#{duration.to_s}" end |
#turn_left!(duration) ⇒ Object
189 190 191 |
# File 'lib/mi100.rb', line 189 def turn_left!(duration) sendln "#{CMD_TURN_LEFT},#{duration.to_s}" end |
#turn_right(duration) ⇒ Object
133 134 135 |
# File 'lib/mi100.rb', line 133 def turn_right(duration) send_command_get_response "#{CMD_TURN_RIGHT},#{duration.to_s}" end |
#turn_right!(duration) ⇒ Object
185 186 187 |
# File 'lib/mi100.rb', line 185 def turn_right!(duration) sendln "#{CMD_TURN_RIGHT},#{duration.to_s}" end |