class BitWizard::Boards::Motor

Public Class Methods

new(options={}) click to toggle source

Create an instance of a Motor board

@param [optional, Hash] options A Hash of options.
Calls superclass method BitWizard::Board::new
# File lib/bitwizard/motor.rb, line 9
def initialize(options={})
        options = {
                bus: :spi
        }.merge(options)
        options = options.merge({
                type: "#{options[:bus]}_motor".to_sym,
        })

        super(options)
end

Public Instance Methods

[](port) click to toggle source

Read a PWM value from a port

@param [Symbol] port The port to read from (1..4)
@return [Number] The PWM value on the port
# File lib/bitwizard/motor.rb, line 129
def [](port)
        raise ArgumentError.new "Port must be an integer between 1 and 4" unless port.is_a? Fixnum and (1..4).include? port

        read(0x50+(port-1), 1)[0]
end
[]=(port, value) click to toggle source

Set a PWM value from a port

@param [Number] port The port to set (1..4)
@param [Number] value The PWM value to set on the port (0..255)
# File lib/bitwizard/motor.rb, line 139
def []=(port, value)
        raise ArgumentError.new "Port must be an integer between 1 and 4" unless port.is_a? Fixnum and (1..4).include? port

        write(0x50+(port-1), value)
end
motor_start(port, value) click to toggle source

Start spinning a motor on a specific port

@param [Symbol] port The port to spin (:A or :B)
@param [Number] value The direction and speed of the motor (-255..255)
# File lib/bitwizard/motor.rb, line 24
def motor_start(port, value)
        raise ArgumentError.new("Port must be :A or :B") unless port == :A or port == :B
        raise ArgumentError.new("Value must be an integer beween -255 and 255") unless value.is_a? Fixnum and (-255..255).include? value

        basereg = 0x20
        basereg = 0x30 if port == :B

        if value < 0 then
                write(basereg+1, -value)
        elsif value > 0 then
                write(basereg+1, value)
        else
                write(basereg+2, 1)
        end
end
motor_stop(port) click to toggle source

Stop spinning a motor on a specific port

This is the same as running start_motor with the value 0

@param [Symbol] port The port to stop (:A or :B)
# File lib/bitwizard/motor.rb, line 45
def motor_stop(port)
        raise ArgumentError.new "Port must be :A or :B" unless port == :A or port == :B

        motor_start(port, 0)
end
pwm_disable(*port) click to toggle source

Disables Pulse Width Modulation on the specified port

@param [Number|Array] port The port/ports to disable PWM on
# File lib/bitwizard/motor.rb, line 106
def pwm_disable(*port)
        false # Board doesn't support enabling/disabling PWM
end
pwm_enable(*port) click to toggle source

Enables Pulse Width Modulation on the specified port

@param [Number|Array] port The port/ports to enable PWM on
# File lib/bitwizard/motor.rb, line 99
def pwm_enable(*port)
        false # Board doesn't support enabling/disabling PWM
end
pwm_enabled?(port) click to toggle source

Checks if a port has PWM enabled

@param [Number] port The port to check
@return [Boolean] Is the port enabled for PWM control
# File lib/bitwizard/motor.rb, line 121
def pwm_enabled?(port)
        pwm_ports.include? port
end
pwm_ports() click to toggle source

Returns the ports that have PWM enabled

@return [Array] An array containing the port numbers with PWM enabled
# File lib/bitwizard/motor.rb, line 113
def pwm_ports
        [1, 2, 3, 4] # Board doesn't support enabling/disabling PWM
end
stepper_delay() click to toggle source

Read the step delay of the stepper motor

@return [Number] The stepdelay in tenths of a millisecond
# File lib/bitwizard/motor.rb, line 84
def stepper_delay
        read(0x43, 1)[0]
end
stepper_delay=(delay) click to toggle source

Set the step delay of the stepper motor

@param [Number] delay The new stepdelay, in tenths of a millisecond (maximum 255 - 25ms between steps)
# File lib/bitwizard/motor.rb, line 90
def stepper_delay=(delay)
        raise ArgumentError.new "Delay must be an integer between 0 and 255" unless delay.is_a? Fixnum and (0..255).include? delay

        write(0x43, delay)
end
stepper_position() click to toggle source

Read the current position of the stepper motor

@return [Number] The current stepper position
# File lib/bitwizard/motor.rb, line 54
def stepper_position
        read(0x40, 4).pack("C*").unpack("l>")[0]
end
stepper_position=(position) click to toggle source

Set the current position of the stepper motor, without actually moving it

@param [Number] position The new position of the stepper motor
# File lib/bitwizard/motor.rb, line 60
def stepper_position=(position)
        raise ArgumentError.new "Position must be an integer" unless position.is_a? Fixnum

        write(0x40, [position].pack("l>"))
end
stepper_target() click to toggle source

Read the target position of the stepper motor

@return [Number] The target position of the stepper
# File lib/bitwizard/motor.rb, line 69
def stepper_target
        read(0x41, 4).pack("C*").unpack("l>")[0]
end
stepper_target=(position) click to toggle source

Set the target position of the stepper motor

@param [Number] position The target position for the stepper motor
# File lib/bitwizard/motor.rb, line 75
def stepper_target=(position)
        raise ArgumentError.new "Position must be an integer" unless position.is_a? Fixnum

        write(0x41, [position].pack("l>"))
end