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.
With the USB cable unplugged and the battery pack switched off, push the driver into an empty breadboard so it straddles the center channel, like the button did in 2.7. Its pin names are printed beside each pin; PWMA and VM both go in column 11. Each connection then goes in the same column as its pin, at row a above the channel or row j below it:
| Part | Where it goes |
|---|---|
Wire from the 3V3 pin | Top + rail |
Wire from a GND pin | Top − rail |
| Red jumper | Top + rail to bottom + rail, column 29 |
| Black jumper | Top − rail to bottom − rail, column 30 |
Wire from GPIO 33 | a11 (PWMA) |
Wire from GPIO 32 | a12 (AIN2) |
Wire from GPIO 31 | a13 (AIN1) |
| Jumper | a14 (STBY) to the top + rail |
| Jumper | j12 (VCC) to the bottom + rail |
| Jumper | j18 (GND) to the bottom − rail |
| Battery pack's red lead | j11 (VM), and nowhere else |
| Battery pack's black lead | Bottom − rail |
| Right motor's two leads | j14 and j15 (A01, A02), either way round |
Either lead of the motor can go in either hole: swapping them only swaps which way counts as forward. Check every row of the table, then switch the battery pack on and plug in the USB cable.
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.
Switch the battery pack off and unplug the USB cable, then add four connections:
| Part | Where it goes |
|---|---|
Wire from GPIO 21 | a15 (BIN1) |
Wire from GPIO 35 | a16 (BIN2) |
Wire from GPIO 34 | a17 (PWMB) |
| Left motor's two leads | j16 and j17 (B02, B01) |
The two motors face opposite ways on the robot, so the left wheel may spin backward when you ask for forward. If it does, swap its two leads in j16 and j17.
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.