Intent: the marble is placed at one end, it is moved by the spinner 180 degrees into the narrow path. The marble moves through the narrow path by the speed applied
print(“Hello World!”)
print(“Starting dual_spin script.”)
import math, time
import board
import pwmio
class DRV8833():
def init(self, AIN1=board.GP18, AIN2=board.GP19, BIN2=board.GP20, BIN1=board.GP21, pwm_rate=20000):
# Create a pair of PWMOut objects for each motor channel.
self.ain1 = pwmio.PWMOut(AIN1, duty_cycle=0, frequency=pwm_rate)
self.ain2 = pwmio.PWMOut(AIN2, duty_cycle=0, frequency=pwm_rate)
self.bin1 = pwmio.PWMOut(BIN1, duty_cycle=0, frequency=pwm_rate) self.bin2 = pwmio.PWMOut(BIN2, duty_cycle=0, frequency=pwm_rate) def write(self, channel, rate): """Set the speed and direction on a single motor channel. :param channel: 0 for motor A, 1 for motor B :param rate: modulation value between -1.0 and 1.0, full reverse to full forward.""" # convert the rate into a 16-bit fixed point integer pwm = min(max(int(2**16 * abs(rate)), 0), 65535) if channel == 0: if rate < 0: self.ain1.duty_cycle = pwm self.ain2.duty_cycle = 0 else: self.ain1.duty_cycle = 0 self.ain2.duty_cycle = pwm else: if rate < 0: self.bin1.duty_cycle = pwm self.bin2.duty_cycle = 0 else: self.bin1.duty_cycle = 0 self.bin2.duty_cycle = pwm
——————————————————————————–
Create an object to represent a dual motor driver.
print(“Creating driver object.”)
driver = DRV8833()
print(“Starting main script.”)
while True:
# initial pause
time.sleep(2.0)
print("Testing motor A.") driver.write(0, 0.75) time.sleep(2.0) driver.write(0, 0.0) time.sleep(2.0) driver.write(0, 0.75) time.sleep(2.0) driver.write(0, 0.0) time.sleep(2.0)
Leave a Reply
You must be logged in to post a comment.