5% OFF Over \$19, Code: Elephant5; 10% OFF Over \$59, Code: Elephant10

# Microbit Python Tutorials 14: Robot advance

Posted by SuFiona on

Learning goals:

This lesson learns to use Python programming to make the robot advance.

Code

from microbit import *

import ustruct

import math

# Registers/etc:

MODE1 = 0x00

MODE2 = 0x01

PRESCALE = 0xFE

LED0_ON_L = 0x06

LED0_ON_H = 0x07

LED0_OFF_L = 0x08

LED0_OFF_H = 0x09

ALL_LED_ON_L = 0xFA

ALL_LED_ON_H = 0xFB

ALL_LED_OFF_L = 0xFC

ALL_LED_OFF_H = 0xFD

# Bits:

RESTART = 0x80

SLEEP = 0x10

ALLCALL = 0x01

INVRT = 0x10

OUTDRV = 0x04

RESET = 0x00

class PCA9685():

"""PCA9685 PWM LED/servo controller."""

"""Initialize the PCA9685."""

self.set_all_pwm(0, 0)

sleep(5)  # wait for oscillator

mode1 = ustruct.unpack('<H', mode1)[0]

mode1 = mode1 & ~SLEEP  # wake up (reset sleep)

sleep(5)  # wait for oscillator

def set_pwm_freq(self, freq_hz):

"""Set the PWM frequency to the provided value in hertz."""

prescaleval = 25000000.0    # 25MHz

prescaleval /= 4096.0       # 12-bit

prescaleval /= float(freq_hz)

prescaleval -= 1.0

# print('Setting PWM frequency to {0} Hz'.format(freq_hz))

# print('Estimated pre-scale: {0}'.format(prescaleval))

prescale = int(math.floor(prescaleval + 0.5))

# print('Final pre-scale: {0}'.format(prescale))

oldmode = ustruct.unpack('<H', oldmode)[0]

newmode = (oldmode & 0x7F) | 0x10    # sleep

i2c.write(self.address, bytearray([MODE1, newmode]))  # go to sleep

sleep(5)

def set_pwm(self, channel, on, off):

"""Sets a single PWM channel."""

if on is None or off is None:

return ustruct.unpack('<HH', data)

def set_all_pwm(self, on, off):

"""Sets all PWM channels."""

def duty(self, index, value=None, invert=False):

if value is None:

pwm = self.set_pwm(index)

if pwm == (0, 4096):

value = 0

elif pwm == (4096, 0):

value = 4095

value = pwm[1]

if invert:

value = 4095 - value

return value

if not 0 <= value <= 4095:

raise ValueError("Out of range")

if invert:

value = 4095 - value

if value == 0:

self.set_pwm(index, 0, 4096)

elif value == 4095:

self.set_pwm(index, 4096, 0)

else:

self.set_pwm(index, 0, value)

# Initialise the PCA9685 using the default address (0x41).

pwm = PCA9685()

# Configure min and max servo pulse lengths

servo_min = 150  # Min pulse length out of 4096  0?

servo_max = 600  # Max pulse length out of 4096: 180?

# Set frequency to 60hz, good for servos.

pwm.set_pwm_freq(60)

display.show(Image.HAPPY)

pwm.set_pwm(12, 0, 4095)

pwm.set_pwm(13, 0, 0)

pwm.set_pwm(15, 0, 4095)

pwm.set_pwm(14, 0, 0)

pwm.set_pwm(3, 0, servo_min)

sleep(1000)

pwm.set_pwm(3, 0, servo_max)

sleep(1000)

This section of the experiment uses I2C communication, through the PCA9685PW chip can output 16 PWM, so we can control the output of 4-way PWM control car forward.

1.You should open the Mu software, and enter the code in the edit window, , as shown in Figure 18-1.

Figure 18-1

2.As shown in Figure 18-2, you need to click the Check button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.

Figure 18-2

3.You need to connect the micro data cable to micro:bit and the computer, then click the Flash button to download the program to micro:bit as shown in Figure 18-3.

Figure 18-3

4. The schematic diagram of the robot's PCA9685PW chip and motor is shown in Figure 18-4 and Figure 18-5. As shown in the figure, the robot's motor is connected to the LINA, LINB, RINA, and RINB pins of the PCA9685PW chip, while the PCA9685PW The SCL and SDA are connected to the P19 and P20 pins of the micro:bit chip.

Figure 14-4

Figure 18-5

Figures 18-6

The code of the experiment:14.Robot advance.rar