3 Driving the robot

 

This chapter covers

  • Controlling DC motors to make robots move forward and backward
  • Implementing software-configured motor power adjustments
  • Turning the robot left and right
  • Spinning the robot in place
  • Refactoring the code using the functools library

This chapter will teach you how to move the robot in different directions using the Python code to control the power given to DC motors. The left and right wheels of the robot each have a dedicated motor attached to them. Controlling the motors makes a whole array of movements possible. Python functions will be generated for each of the main movement operations to create an easy-to-use and readable set of methods to control the robot’s movements. Once all these functions have been implemented, a number of refactoring techniques will be applied to simplify and consolidate the code base.

3.1 What’s a robot chassis kit?

Robot chassis kits are a great way to build mobile robots. In the previous chapter, we saw how the Raspberry Pi provides computing power for the robot and how the CRICKIT HAT add-on controls connected motors. The chassis kit provides the body, motors, and wheels to get your robot moving around. There are many different robot chassis kits that can be used with the Raspberry Pi. The one recommended in this book is an inexpensive and flexible option that lets you build many different robot configurations. The kit comes with the following main parts:

3.2 Hardware stack

3.3 Software stack

3.4 Writing a move forward function

3.5 Using environment variables for configuration

3.6 Controlling the speed and duration of movements

3.7 Moving backward

3.8 Turning right

3.9 Moving left and spinning in either direction

3.10 Refactoring by finding common logic

3.11 Refactoring by using functools

Summary