Drivers

Artoo has a extensible system for connecting to hardware devices.

Motor


Provides motor manipulation tools for artoo like on/off, speed modulation using PWM, use of bidirectional motors using forward/backward pins.

How To Connect

For Arduino:

require 'artoo'
connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
device :motor, :driver => :motor, :speed_pin => 3 # Use a PWM pin

For Digispark:

require 'artoo'
connection :digispark, :adaptor => :littlewire, :vendor => 0x1781, :product => 0x0c9f
device :motor, :driver => :motor, :speed_pin => 3 # Use a PWM pin

How To Use

Example of a dc motor with speed changes using PWM.

require 'artoo'

#Circuit's breadboard layout here: http://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/breadboard-layout

#connection :arduino, :adaptor => :firmata, :port => '/dev/tty*'
connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
device :board, :driver => :device_info
device :motor, :driver => :motor, :speed_pin => 3 # Use a PWM pin

work do
  puts "Firmware name: #{board.firmware_name}"
  puts "Firmata version: #{board.version}"
  puts "Stopping motor..."
  motor.min # same as 'motor.stop' or 'motor.speed(0)'
  sleep 3
  puts "Setting to maximum speed..."
  motor.max # same as 'motor.start'
  sleep 3

  speed = 0
  step = 50

  every 3.seconds do
    motor.speed(speed)
    puts "Current speed: #{motor.current_speed}"
    speed += step
    if [0, 250].include?(speed)
      step = -step
    end
  end
end

Example of a dc motor with direction and speed change.

require 'artoo'

#Circuit's breadboard layout for the L293D: http://www.electrojoystick.com/tutorial/?p=759
#For the L239DNE: http://bit.ly/14QdjD5

#connection :arduino, :adaptor => :firmata, :port => '/dev/tty*'
connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
device :board, :driver => :device_info
device :motor, :driver => :motor,
               :forward_pin  => 4, # Digital or PWM pin
               :backward_pin => 2, # Digital or PWM pin
               :speed_pin    => 9  # PWM pin only

work do
  puts "Firmware name: #{board.firmware_name}"
  puts "Firmata version: #{board.version}"
  puts "Stopping motor..."
  motor.stop
  sleep 2

  loop do
    motor.forward # if no speed set, spins at max speed
    puts "Going forward, Speed: #{motor.current_speed}"
    sleep 3
    motor.forward 180
    puts "Going forward, Speed: #{motor.current_speed}"
    sleep 3
    puts "Stopping..."
    motor.stop
    sleep 2
    motor.backward(150) # spins at speed 150
    puts "Going backward, Speed: #{motor.current_speed}"
    sleep 3
    motor.backward(255)
    puts "Going backward, Speed: #{motor.current_speed}"
    sleep 3
  end
end

Commands

stop

Stops the motor.

Params

no params

Returns

true or nil

start

Starts the motor.

Params

no params

Returns

true or nil

on?

Returns true if the motor is on, otherwise returns false.

Params

no params

Returns

true or false

off?

Returns true if the motor is off, otherwise returns false.

Params

no params

Returns

true or false

toggle

Sets the state of the motor to the oposite of the current state, if motor is on then sets it to off.

Params

no params

Returns

true or nil

speed(speed_value)

Sets the speed of the motor to the value provided in the speed param, speed value must be an integer between 0 and 255.

Params
  • speed_value - params, the speed value
Returns

true or nil

min

Stops the motor, sets the speed to 0.

Params

no params

Returns

true or nil

max

Starts the motor at max speed, sets the speed to 255.

Params

no params

Returns

true or nil

forward(speed)

Starts the motor in forward direction at the provided speed.

Params
  • speed - params, the speed value
Returns

true or nil

backward(speed)

Starts the motor in backward direction at the provided speed.

Params
  • speed - params, the speed value
Returns

true or nil

forward?

Returns true if the motor is direction is set to forward, otherwise false.

Params

no params

Returns

true or false

backward?

Returns true if the motor is direction is set to backward, otherwise false.

Params

no params

Returns

true or false

current_speed

Returns the current speed of the motor as an integer between 0 and 255.

Params

no params

Returns

speed

Circuit

Compatibility

Important Note: You need to install pi-blaster in the raspberry-pi, this project enables PWM in the GPIO pins .