Class: BitWizard::Boards::Motor

Inherits:
BitWizard::Board show all
Defined in:
lib/bitwizard/motor.rb

Instance Attribute Summary

Attributes inherited from BitWizard::Board

#address, #bus, #features, #logger, #type, #version

Instance Method Summary collapse

Methods inherited from BitWizard::Board

detect, #read, #valid?, #write

Constructor Details

#initialize(options = {}) ⇒ Motor

Create an instance of a Motor board

Parameters:

  • options (optional, Hash) (defaults to: {})

    A Hash of options.



9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#[](port) ⇒ Number

Read a PWM value from a port

Parameters:

  • port (Symbol)

    The port to read from (1..4)

Returns:

  • (Number)

    The PWM value on the port



129
130
131
132
133
# 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) ⇒ Object

Set a PWM value from a port

Parameters:

  • port (Number)

    The port to set (1..4)

  • value (Number)

    The PWM value to set on the port (0..255)



139
140
141
142
143
# 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) ⇒ Object

Start spinning a motor on a specific port

Parameters:

  • port (Symbol)

    The port to spin (:A or :B)

  • value (Number)

    The direction and speed of the motor (-255..255)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

Stop spinning a motor on a specific port

This is the same as running start_motor with the value 0

Parameters:

  • port (Symbol)

    The port to stop (:A or :B)



45
46
47
48
49
# 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) ⇒ Object

Disables Pulse Width Modulation on the specified port

Parameters:

  • port (Number|Array)

    The port/ports to disable PWM on



106
107
108
# File 'lib/bitwizard/motor.rb', line 106

def pwm_disable(*port)
	false # Board doesn't support enabling/disabling PWM
end

#pwm_enable(*port) ⇒ Object

Enables Pulse Width Modulation on the specified port

Parameters:

  • port (Number|Array)

    The port/ports to enable PWM on



99
100
101
# File 'lib/bitwizard/motor.rb', line 99

def pwm_enable(*port)
	false # Board doesn't support enabling/disabling PWM
end

#pwm_enabled?(port) ⇒ Boolean

Checks if a port has PWM enabled

Parameters:

  • port (Number)

    The port to check

Returns:

  • (Boolean)

    Is the port enabled for PWM control



121
122
123
# File 'lib/bitwizard/motor.rb', line 121

def pwm_enabled?(port)
	pwm_ports.include? port
end

#pwm_portsArray

Returns the ports that have PWM enabled

Returns:

  • (Array)

    An array containing the port numbers with PWM enabled



113
114
115
# File 'lib/bitwizard/motor.rb', line 113

def pwm_ports
	[1, 2, 3, 4] # Board doesn't support enabling/disabling PWM
end

#stepper_delayNumber

Read the step delay of the stepper motor

Returns:

  • (Number)

    The stepdelay in tenths of a millisecond



84
85
86
# File 'lib/bitwizard/motor.rb', line 84

def stepper_delay
	read(0x43, 1)[0]
end

#stepper_delay=(delay) ⇒ Object

Set the step delay of the stepper motor

Parameters:

  • delay (Number)

    The new stepdelay, in tenths of a millisecond (maximum 255 - 25ms between steps)



90
91
92
93
94
# 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_positionNumber

Read the current position of the stepper motor

Returns:

  • (Number)

    The current stepper position



54
55
56
# File 'lib/bitwizard/motor.rb', line 54

def stepper_position
	read(0x40, 4).pack("C*").unpack("l>")[0]
end

#stepper_position=(position) ⇒ Object

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

Parameters:

  • position (Number)

    The new position of the stepper motor



60
61
62
63
64
# 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_targetNumber

Read the target position of the stepper motor

Returns:

  • (Number)

    The target position of the stepper



69
70
71
# File 'lib/bitwizard/motor.rb', line 69

def stepper_target
	read(0x41, 4).pack("C*").unpack("l>")[0]
end

#stepper_target=(position) ⇒ Object

Set the target position of the stepper motor

Parameters:

  • position (Number)

    The target position for the stepper motor



75
76
77
78
79
# 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