class Smalruby::Hardware::SmalrubotV3

Smalrubot v3 class

Constants

WAIT_TIME

wait time for next action

Attributes

green_led[R]

Green LED that is connected D2

left_motor[R]

Left Motor that is connected D6, D7, D8

left_sensor[R]

Left sensor that is connected A0

red_led[R]

Red LED that is connected D13

right_motor[R]

Right Motor that is connected D9, D10, D11

right_sensor[R]

Right sensor that is connected A1

Public Class Methods

lock() { || ... } click to toggle source

lock for motor driver

# File lib/smalruby/hardware/smalrubot_v3.rb, line 14
def lock(&block)
  @mutex.synchronize do
    yield
  end
end
new(_) click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 47
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_sensor = Sensor.new(pin: 'A0')
  @right_sensor = Sensor.new(pin: 'A1')

  @current_motor_direction = :stop
end
unlock() { || ... } click to toggle source

unlock for motor driver

# File lib/smalruby/hardware/smalrubot_v3.rb, line 21
def unlock(&block)
  @mutex.unlock
  yield
ensure
  @mutex.lock
end

Public Instance Methods

left_dc_motor_power_ratio() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 62
def left_dc_motor_power_ratio
  @left_motor.speed
end
left_dc_motor_power_ratio=(val) click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 66
def left_dc_motor_power_ratio=(val)
  @left_motor.speed = val
end
left_sensor_value() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 117
def left_sensor_value
  @left_sensor.value.to_i
end
right_dc_motor_power_ratio() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 70
def right_dc_motor_power_ratio
  @right_motor.speed
end
right_dc_motor_power_ratio=(val) click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 74
def right_dc_motor_power_ratio=(val)
  @right_motor.speed = val
end
right_sensor_value() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 121
def right_sensor_value
  @right_sensor.value.to_i
end
stop(sec: WAIT_TIME) click to toggle source

stop

@param [Float] time of stopping

# File lib/smalruby/hardware/smalrubot_v3.rb, line 106
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_off_green_led() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 130
def turn_off_green_led
  @green_led.turn_off
end
Also aliased as: turn_off_left_led
turn_off_left_led()
Alias for: turn_off_green_led
turn_off_red_led() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 140
def turn_off_red_led
  @red_led.turn_off
end
Also aliased as: turn_off_right_led
turn_off_right_led()
Alias for: turn_off_red_led
turn_on_green_led() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 125
def turn_on_green_led
  @green_led.turn_on
end
Also aliased as: turn_on_left_led
turn_on_left_led()
Alias for: turn_on_green_led
turn_on_red_led() click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 135
def turn_on_red_led
  @red_led.turn_on
end
Also aliased as: turn_on_right_led
turn_on_right_led()
Alias for: turn_on_red_led

Private Instance Methods

run(direction, sec: nil) click to toggle source
# File lib/smalruby/hardware/smalrubot_v3.rb, line 147
def run(direction, sec: nil)
  self.class.lock do
    if @current_motor_direction != :stop
      self.class.unlock do
        stop(sec: WAIT_TIME)
      end
    end
    case direction
    when :forward
      @left_motor.forward
      @right_motor.forward
    when :backward
      @left_motor.backward
      @right_motor.backward
    when :turn_left
      @left_motor.backward
      @right_motor.forward
    when :turn_right
      @left_motor.forward
      @right_motor.backward
    end
    @current_motor_direction = direction
    if (sec = sec.to_f) > 0
      sleep(sec)
      if direction != :stop
        self.class.unlock do
          stop
        end
      end
    end
  end
end