Now that we have tested the motors, servos, camera, and LEDs, it's time to modify the code into classes to give it more unity. In this section, we will make T.A.R.A.S dance.
Let's start by encapsulating the code that moves the wheels on the robot car:
- Open up Thonny from
Application Menu
|Programming
|Thonny Python IDE
- Click on the
New
icon to create a new file - Type the following code into the file:
from gpiozero import Robot from time import sleep class RobotWheels: robot = Robot(left=(5, 6), right=(22, 27)) def __init__(self): pass def move_forward(self): self.robot.forward(0.2) def move_backwards(self): self.robot.backward(0.2) def turn_right(self): self.robot.right(0.2) def turn_left(self): self.robot.left(0.2) def dance(self): self.move_forward() sleep(0.5) self.stop() self.move_backwards() sleep(0.5) self.stop()...