Class: Smalruby::Hardware::SmalrubotV3
- Inherits:
-
Object
- Object
- Smalruby::Hardware::SmalrubotV3
- Defined in:
- lib/smalruby/hardware/smalrubot_v3.rb
Overview
Smalrubot v3 class
Constant Summary collapse
- WAIT_TIME =
wait time for next action
0.01
Instance Attribute Summary collapse
-
#green_led ⇒ Object
readonly
Green LED that is connected D2.
-
#left_motor ⇒ Object
readonly
Left Motor that is connected D6, D7, D8.
-
#left_touch_sensor ⇒ Object
readonly
Left touch sensor that is connected D4.
-
#light_sensor ⇒ Object
readonly
Light sensor that is connected A0.
-
#red_led ⇒ Object
readonly
Red LED that is connected D13.
-
#right_motor ⇒ Object
readonly
Right Motor that is connected D9, D10, D11.
-
#right_touch_sensor ⇒ Object
readonly
Right touch sensor that is connected D3.
Class Method Summary collapse
-
.lock(&block) ⇒ Object
lock for motor driver.
-
.unlock(&block) ⇒ Object
unlock for motor driver.
Instance Method Summary collapse
-
#backward(sec: nil) ⇒ Object
backward.
-
#forward(sec: nil) ⇒ Object
forward.
-
#initialize(_) ⇒ SmalrubotV3
constructor
A new instance of SmalrubotV3.
-
#stop(sec: WAIT_TIME) ⇒ Object
stop.
-
#turn_left(sec: nil) ⇒ Object
turn left.
-
#turn_right(sec: nil) ⇒ Object
turn right.
Constructor Details
#initialize(_) ⇒ SmalrubotV3
Returns a new instance of SmalrubotV3.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 51 def initialize(_) @red_led = Led.new(pin: 'D13') @green_led = Led.new(pin: 'D2') @left_motor = MotorDriver.new(pin: 'D6') @left_motor.speed = 100 @right_motor = MotorDriver.new(pin: 'D9') @right_motor.speed = 100 @left_touch_sensor = Button.new(pin: 'D4') @right_touch_sensor = Button.new(pin: 'D3') @light_sensor = Sensor.new(pin: 'A0') @current_motor_direction = :stop end |
Instance Attribute Details
#green_led ⇒ Object (readonly)
Green LED that is connected D2
34 35 36 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 34 def green_led @green_led end |
#left_motor ⇒ Object (readonly)
Left Motor that is connected D6, D7, D8
37 38 39 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 37 def left_motor @left_motor end |
#left_touch_sensor ⇒ Object (readonly)
Left touch sensor that is connected D4
43 44 45 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 43 def left_touch_sensor @left_touch_sensor end |
#light_sensor ⇒ Object (readonly)
Light sensor that is connected A0
49 50 51 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 49 def light_sensor @light_sensor end |
#red_led ⇒ Object (readonly)
Red LED that is connected D13
31 32 33 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 31 def red_led @red_led end |
#right_motor ⇒ Object (readonly)
Right Motor that is connected D9, D10, D11
40 41 42 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 40 def right_motor @right_motor end |
#right_touch_sensor ⇒ Object (readonly)
Right touch sensor that is connected D3
46 47 48 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 46 def right_touch_sensor @right_touch_sensor end |
Class Method Details
.lock(&block) ⇒ Object
lock for motor driver
15 16 17 18 19 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 15 def lock(&block) @mutex.synchronize do yield end end |
.unlock(&block) ⇒ Object
unlock for motor driver
22 23 24 25 26 27 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 22 def unlock(&block) @mutex.unlock yield ensure @mutex.lock end |
Instance Method Details
#backward(sec: nil) ⇒ Object
backward
|
|
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 73
|
#forward(sec: nil) ⇒ Object
forward
|
|
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 68
|
#stop(sec: WAIT_TIME) ⇒ Object
stop
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 96 def stop(sec: WAIT_TIME) self.class.lock do @left_motor.stop @right_motor.stop @current_motor_direction = :stop if (sec = sec.to_f) > 0 sleep(sec) end end end |
#turn_left(sec: nil) ⇒ Object
turn left
|
|
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 78
|
#turn_right(sec: nil) ⇒ Object
turn right
87 88 89 90 91 |
# File 'lib/smalruby/hardware/smalrubot_v3.rb', line 87 %i(forward backward turn_left turn_right).each do |direction| define_method(direction) do |sec: nil| run(direction, sec: sec) end end |