class Mi100
morsecoder.rb Copyright © 2014 Masami Yamakawa
This software is released under the MIT License. opensource.org/lisenses/mit-license.php
Constants
- CMD_BLINK_LED
- CMD_DRIVE
- CMD_FREE_RAM
- CMD_GET_LIGHT
- CMD_GET_POWER_LEVEL
- CMD_MOVE_BACKWARD
- CMD_MOVE_FORWARD
- CMD_PING
- CMD_SET_SPEED
- CMD_SPIN_LEFT
- CMD_SPIN_RIGHT
- CMD_STOP
- CMD_TONE
- CMD_TURN_LED_RGB
- CMD_TURN_LEFT
- CMD_TURN_RIGHT
- DEFAULT_BLINK_DURATION
- DEFAULT_DRIVE_DURATION
- DEFAULT_MOVE_DIRECTION
- DEFAULT_MOVE_DURATION
- DEFAULT_RETRIES
- DEFAULT_SPEED
- DEFAULT_SPIN_DIRECTION
- DEFAULT_SPIN_DURATION
- DEFAULT_TONE_DURATION
- FREQUENCY
- READ_TIMEOUT
- SHORT_READ_TIMEOUT
- VERSION
- WRITE_TIMEOUT
Public Class Methods
new(dev)
click to toggle source
# 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
Public Instance Methods
bad()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/mi100.rb, line 87 def close sleep 2 @sp.close end
drive(right_speed, left_speed, duration = @drive_duration)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/mi100.rb, line 214 def free_ram send_command_get_response "#{CMD_FREE_RAM}" end
good()
click to toggle source
# 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()
click to toggle source
# File lib/mi100.rb, line 236 def led_off turn_led(0,0,0) end
led_on()
click to toggle source
# File lib/mi100.rb, line 232 def led_on turn_led(100,100,100) end
light()
click to toggle source
# File lib/mi100.rb, line 102 def light response = send_command_get_response CMD_GET_LIGHT response[1].to_i end
morse_frequency()
click to toggle source
# File lib/mi100.rb, line 285 def morse_frequency Morsecoder.default_frequency end
morse_frequency=(frequency)
click to toggle source
# File lib/mi100.rb, line 289 def morse_frequency=(frequency) Morsecoder.default_frequency = frequency end
morse_unit()
click to toggle source
# File lib/mi100.rb, line 293 def morse_unit Morsecoder.default_unit end
morse_unit=(millisec)
click to toggle source
# File lib/mi100.rb, line 297 def morse_unit=(millisec) Morsecoder.default_unit = millisec end
move(direction = DEFAULT_MOVE_DIRECTION, duration = DEFAULT_MOVE_DURATION)
click to toggle source
# 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)
click to toggle source
# File lib/mi100.rb, line 121 def move_backward(duration) send_command_get_response "#{CMD_MOVE_BACKWARD},#{duration.to_s}" end
move_backward!(duration)
click to toggle source
# File lib/mi100.rb, line 173 def move_backward!(duration) sendln "#{CMD_MOVE_BACKWARD},#{duration.to_s}" end
move_forward(duration)
click to toggle source
# File lib/mi100.rb, line 117 def move_forward(duration) send_command_get_response "#{CMD_MOVE_FORWARD},#{duration.to_s}" end
move_forward!(duration)
click to toggle source
# File lib/mi100.rb, line 169 def move_forward!(duration) sendln "#{CMD_MOVE_FORWARD},#{duration.to_s}" end
moveb(duration = DEFAULT_MOVE_DURATION)
click to toggle source
# File lib/mi100.rb, line 145 def moveb(duration = DEFAULT_MOVE_DURATION) move_backward duration end
movef(duration = DEFAULT_MOVE_DURATION)
click to toggle source
# File lib/mi100.rb, line 141 def movef(duration = DEFAULT_MOVE_DURATION) move_forward duration end
ping()
click to toggle source
# File lib/mi100.rb, line 92 def ping send_command_get_response CMD_PING end
power()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/mi100.rb, line 129 def spin_left(duration) send_command_get_response "#{CMD_SPIN_LEFT},#{duration.to_s}" end
spin_left!(duration)
click to toggle source
# File lib/mi100.rb, line 181 def spin_left!(duration) sendln "#{CMD_SPIN_LEFT},#{duration.to_s}" end
spin_right(duration)
click to toggle source
# File lib/mi100.rb, line 125 def spin_right(duration) send_command_get_response "#{CMD_SPIN_RIGHT},#{duration.to_s}" end
spin_right!(duration)
click to toggle source
# File lib/mi100.rb, line 177 def spin_right!(duration) sendln "#{CMD_SPIN_RIGHT},#{duration.to_s}" end
spinl(duration = DEFAULT_SPIN_DURATION)
click to toggle source
# File lib/mi100.rb, line 153 def spinl(duration = DEFAULT_SPIN_DURATION) spin_left duration end
spinr(duration = DEFAULT_SPIN_DURATION)
click to toggle source
# File lib/mi100.rb, line 149 def spinr(duration = DEFAULT_SPIN_DURATION) spin_right duration end
stop()
click to toggle source
# File lib/mi100.rb, line 240 def stop send_command_get_response CMD_STOP end
talk(str)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/mi100.rb, line 137 def turn_left(duration) send_command_get_response "#{CMD_TURN_LEFT},#{duration.to_s}" end
turn_left!(duration)
click to toggle source
# File lib/mi100.rb, line 189 def turn_left!(duration) sendln "#{CMD_TURN_LEFT},#{duration.to_s}" end
turn_right(duration)
click to toggle source
# File lib/mi100.rb, line 133 def turn_right(duration) send_command_get_response "#{CMD_TURN_RIGHT},#{duration.to_s}" end
turn_right!(duration)
click to toggle source
# File lib/mi100.rb, line 185 def turn_right!(duration) sendln "#{CMD_TURN_RIGHT},#{duration.to_s}" end
Private Instance Methods
empty_receive_buffer()
click to toggle source
# File lib/mi100.rb, line 334 def empty_receive_buffer begin char = @sp.getbyte end while char && char.length > 0 end
initialize_robo()
click to toggle source
# File lib/mi100.rb, line 309 def initialize_robo self.speed DEFAULT_SPEED self.drive_duration DEFAULT_DRIVE_DURATION self.turn_led 0,0,0 end
initialize_serialport(dev)
click to toggle source
Private methods
# File lib/mi100.rb, line 304 def initialize_serialport(dev) require 'rubyserial' @sp = Serial.new dev, 9600 end
receiveln()
click to toggle source
# File lib/mi100.rb, line 340 def receiveln @sp.gets end
send_command_get_response(cmd = CMD_PING)
click to toggle source
# File lib/mi100.rb, line 315 def send_command_get_response(cmd = CMD_PING) empty_receive_buffer sendln cmd received_line = receiveln begin timeout(READ_TIMEOUT/1000) do received_line = receiveln until received_line[0] == cmd[0] end rescue return nil end received_line.chomp.split(",") end
sendln(str)
click to toggle source
# File lib/mi100.rb, line 330 def sendln str @sp.write str + "\n" end