Motion & Autonomy — Days 1 and 2 of 4
'F' → Fullscreen
The bench circuit comes down today. The driver needs pins the LED array has been using, and the robot is a different object. Keep the OLED — it comes with you.
Tasks 1 to 3 are the first session; tasks 4 to 6 are the second. Create a new MicroPython program named 2-12-motors.
AO1/AO2 outputs.Two connections cause almost every failure here. STBY must be HIGH or the driver ignores you completely and silently. And the battery's minus must share a ground with the board, or the driver has no common reference and behaves erratically.
from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
SPEED = 40000
a_in1.value(1)a_in2.value(0)a_pwm.duty_u16(SPEED)time.sleep(2)a_pwm.duty_u16(0)from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
SPEED = 40000
a_in1.value(1)a_in2.value(0)a_pwm.duty_u16(SPEED)time.sleep(2)
a_in1.value(0)a_in2.value(1)time.sleep(2)
a_pwm.duty_u16(0)motor(in1, in2, pwm, speed) where a positive speed goes forward, a negative speed goes backward, and zero stops.Note what -speed is doing: the duty cycle can never be negative, so the function has to flip the sign back after it has used it to pick the direction.
from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed): if speed > 0: in1.value(1) in2.value(0) pwm.duty_u16(speed) elif speed < 0: in1.value(0) in2.value(1) pwm.duty_u16(-speed) else: in1.value(0) in2.value(0) pwm.duty_u16(0)
motor(a_in1, a_in2, a_pwm, SPEED)time.sleep(2)motor(a_in1, a_in2, a_pwm, -SPEED)time.sleep(2)motor(a_in1, a_in2, a_pwm, 0)BO1/BO2 and its three control pins.Both wheels turning forward will very likely not drive straight. That is not a bug, and it is the first thing the Final Project asks you to deal with.
from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)b_in2 = Pin(35, Pin.OUT)b_pwm = PWM(Pin(34))b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed): if speed > 0: in1.value(1) in2.value(0) pwm.duty_u16(speed) elif speed < 0: in1.value(0) in2.value(1) pwm.duty_u16(-speed) else: in1.value(0) in2.value(0) pwm.duty_u16(0)
motor(a_in1, a_in2, a_pwm, SPEED)motor(b_in1, b_in2, b_pwm, SPEED)time.sleep(2)motor(a_in1, a_in2, a_pwm, 0)motor(b_in1, b_in2, b_pwm, 0)drive(left, right) that takes a speed for each wheel.stop() as well — you will use it constantly.from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)b_in2 = Pin(35, Pin.OUT)b_pwm = PWM(Pin(34))b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed): if speed > 0: in1.value(1) in2.value(0) pwm.duty_u16(speed) elif speed < 0: in1.value(0) in2.value(1) pwm.duty_u16(-speed) else: in1.value(0) in2.value(0) pwm.duty_u16(0)
def drive(left, right): motor(b_in1, b_in2, b_pwm, left) motor(a_in1, a_in2, a_pwm, right)
def stop(): drive(0, 0)
drive(SPEED, SPEED)time.sleep(1)stop()from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
b_in1 = Pin(21, Pin.OUT)b_in2 = Pin(35, Pin.OUT)b_pwm = PWM(Pin(34))b_pwm.freq(1000)
SPEED = 40000
def motor(in1, in2, pwm, speed): if speed > 0: in1.value(1) in2.value(0) pwm.duty_u16(speed) elif speed < 0: in1.value(0) in2.value(1) pwm.duty_u16(-speed) else: in1.value(0) in2.value(0) pwm.duty_u16(0)
def drive(left, right): motor(b_in1, b_in2, b_pwm, left) motor(a_in1, a_in2, a_pwm, right)
def stop(): drive(0, 0)
drive(SPEED, SPEED)time.sleep(1)
drive(-SPEED, -SPEED)time.sleep(1)
drive(-SPEED, SPEED)time.sleep(0.6)
drive(SPEED, -SPEED)time.sleep(0.6)
stop()SPEED at which your wheels still turn reliably from a standing start.motor() with a negative speed runs backward, and with zero stops deaddrive(-SPEED, SPEED) spins the robot rather than driving itAnswer the following questions before submitting your work.
Submit the required files to the appropriate dropbox.