Class: BitWizard::Boards::Motor
- Inherits:
-
BitWizard::Board
- Object
- BitWizard::Board
- BitWizard::Boards::Motor
- Defined in:
- lib/bitwizard/motor.rb
Instance Attribute Summary
Attributes inherited from BitWizard::Board
#address, #bus, #features, #logger, #type, #version
Instance Method Summary collapse
-
#[](port) ⇒ Number
Read a PWM value from a port.
-
#[]=(port, value) ⇒ Object
Set a PWM value from a port.
-
#initialize(options = {}) ⇒ Motor
constructor
Create an instance of a Motor board.
-
#motor_start(port, value) ⇒ Object
Start spinning a motor on a specific port.
-
#motor_stop(port) ⇒ Object
Stop spinning a motor on a specific port.
-
#pwm_disable(*port) ⇒ Object
Disables Pulse Width Modulation on the specified port.
-
#pwm_enable(*port) ⇒ Object
Enables Pulse Width Modulation on the specified port.
-
#pwm_enabled?(port) ⇒ Boolean
Checks if a port has PWM enabled.
-
#pwm_ports ⇒ Array
Returns the ports that have PWM enabled.
-
#stepper_delay ⇒ Number
Read the step delay of the stepper motor.
-
#stepper_delay=(delay) ⇒ Object
Set the step delay of the stepper motor.
-
#stepper_position ⇒ Number
Read the current position of the stepper motor.
-
#stepper_position=(position) ⇒ Object
Set the current position of the stepper motor, without actually moving it.
-
#stepper_target ⇒ Number
Read the target position of the stepper motor.
-
#stepper_target=(position) ⇒ Object
Set the target position of the stepper motor.
Methods inherited from BitWizard::Board
detect, #read, #valid?, #write
Constructor Details
#initialize(options = {}) ⇒ Motor
Create an instance of a Motor board
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/bitwizard/motor.rb', line 9 def initialize(={}) = { bus: :spi }.merge() = .merge({ type: "#{[:bus]}_motor".to_sym, }) super() end |
Instance Method Details
#[](port) ⇒ Number
Read a PWM value from a 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
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
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
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
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
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
121 122 123 |
# File 'lib/bitwizard/motor.rb', line 121 def pwm_enabled?(port) pwm_ports.include? port end |
#pwm_ports ⇒ Array
Returns the ports that have 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_delay ⇒ Number
Read the step delay of the stepper motor
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
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_position ⇒ Number
Read the current position of the stepper motor
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
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_target ⇒ Number
Read the target position of the stepper motor
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
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 |