Class: Artoo::Drivers::Roomba
- Inherits:
-
Driver
- Object
- Driver
- Artoo::Drivers::Roomba
- Defined in:
- lib/artoo/drivers/roomba.rb
Overview
The Roomba driver behaviors
Defined Under Namespace
Modules: Direction, Mode, Note, Song, Speed
Constant Summary collapse
- COMMANDS =
[:start, :safe_mode, :full_mode, :forward, :stop, :fast_forward, :backwards, :nudge_left, :nudge_right, :turn_left, :turn_right, :turn_around, :drive, :play, :song, :beep].freeze
Instance Method Summary collapse
-
#backwards(seconds) ⇒ Object
Move backward.
-
#beep ⇒ Object
Beeps with a G note.
-
#drive(v, r, s = 0) ⇒ Object
Drive.
-
#fast_forward(seconds) ⇒ Object
Move forward with fast speed.
-
#forward(seconds, velocity = Speed::SLOW) ⇒ Object
Move forward.
-
#full_mode ⇒ Object
Sends full mode.
-
#nudge_left ⇒ Object
Nudge left.
-
#nudge_right ⇒ Object
Nudge right.
-
#play(song_number = 0) ⇒ Object
Play song.
-
#play_song(notes, song_number = 0) ⇒ Object
Play song.
-
#safe_mode ⇒ Object
Sends safe mode.
-
#song(notes, song_number = 0) ⇒ Object
Save song.
-
#split_bytes(num) ⇒ Object
Split bytes (hex).
-
#start ⇒ Object
Sends start mode.
-
#stop ⇒ Object
Stop movement.
-
#turn_around ⇒ Object
Turn around.
-
#turn_left(seconds = 1) ⇒ Object
Turn left.
-
#turn_right(seconds = 1) ⇒ Object
Turn right.
Instance Method Details
#backwards(seconds) ⇒ Object
Move backward
95 96 97 98 |
# File 'lib/artoo/drivers/roomba.rb', line 95 def backwards(seconds) drive(Speed::NEG, Direction::STRAIGHT, seconds) stop if seconds > 0 end |
#beep ⇒ Object
Beeps with a G note
174 175 176 |
# File 'lib/artoo/drivers/roomba.rb', line 174 def beep play_song([Note::G, Note::WHOLE]) end |
#drive(v, r, s = 0) ⇒ Object
Drive
135 136 137 138 139 140 |
# File 'lib/artoo/drivers/roomba.rb', line 135 def drive(v, r, s = 0) vH,vL = split_bytes(v) rH,rL = split_bytes(r) send_bytes([137, vH, vL, rH, rL]) sleep(s) if s > 0 end |
#fast_forward(seconds) ⇒ Object
Move forward with fast speed
88 89 90 91 |
# File 'lib/artoo/drivers/roomba.rb', line 88 def fast_forward(seconds) drive(Speed::MAX, Direction::STRAIGHT, seconds) stop if seconds > 0 end |
#forward(seconds, velocity = Speed::SLOW) ⇒ Object
Move forward
76 77 78 79 |
# File 'lib/artoo/drivers/roomba.rb', line 76 def forward(seconds, velocity = Speed::SLOW) drive(velocity, Direction::STRAIGHT, seconds) stop if seconds > 0 end |
#full_mode ⇒ Object
Sends full mode
66 67 68 69 70 |
# File 'lib/artoo/drivers/roomba.rb', line 66 def full_mode start send_bytes(Mode::FULL) sleep 0.1 end |
#nudge_left ⇒ Object
Nudge left
101 102 103 |
# File 'lib/artoo/drivers/roomba.rb', line 101 def nudge_left turn_left(0.25) end |
#nudge_right ⇒ Object
Nudge right
120 121 122 |
# File 'lib/artoo/drivers/roomba.rb', line 120 def nudge_right turn_right(0.25) end |
#play(song_number = 0) ⇒ Object
Play song
150 151 152 |
# File 'lib/artoo/drivers/roomba.rb', line 150 def play(song_number = 0) send_bytes([Song::PLAY, song_number]) end |
#play_song(notes, song_number = 0) ⇒ Object
Play song
168 169 170 171 |
# File 'lib/artoo/drivers/roomba.rb', line 168 def play_song(notes, song_number = 0) song(notes, song_number) play(song_number) end |
#safe_mode ⇒ Object
Sends safe mode
59 60 61 62 63 |
# File 'lib/artoo/drivers/roomba.rb', line 59 def safe_mode start send_bytes(Mode::SAFE) sleep 0.1 end |
#song(notes, song_number = 0) ⇒ Object
Save song
158 159 160 161 162 |
# File 'lib/artoo/drivers/roomba.rb', line 158 def song(notes, song_number = 0) note_group = notes.flatten.compact l = note_group.length / 2 send_bytes([Song::SONG, song_number, l] + note_group) end |
#split_bytes(num) ⇒ Object
Split bytes (hex)
144 145 146 |
# File 'lib/artoo/drivers/roomba.rb', line 144 def split_bytes(num) [num >> 8, num & 255] end |
#start ⇒ Object
Sends start mode
53 54 55 56 |
# File 'lib/artoo/drivers/roomba.rb', line 53 def start send_bytes(Mode::START) sleep 0.2 end |
#stop ⇒ Object
Stop movement
82 83 84 |
# File 'lib/artoo/drivers/roomba.rb', line 82 def stop drive(Speed::ZERO, Direction::STRAIGHT) end |
#turn_around ⇒ Object
Turn around
125 126 127 |
# File 'lib/artoo/drivers/roomba.rb', line 125 def turn_around turn_left(1.6) end |
#turn_left(seconds = 1) ⇒ Object
Turn left
107 108 109 110 |
# File 'lib/artoo/drivers/roomba.rb', line 107 def turn_left(seconds = 1) drive(Speed::SLOW, Direction::COUNTERCLOCKWISE, seconds) stop if seconds > 0 end |