Reader small image

You're reading from  Robotics at Home with Raspberry Pi Pico

Product typeBook
Published inMar 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781803246079
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Driving Motors with Raspberry Pi Pico

Our robot is looking ready to run. The first real test of a robot chassis is getting its motors to drive. This chapter will bring the robot to life, testing the wiring and motors, using CircuitPython on Raspberry Pi Pico. We will start with simple tests for each motor and then use them together to make movements. Finally, we will learn more sophisticated code to control their speed and end the chapter by making a path.

In this chapter, we’re going to cover the following main topics:

  • Driving forward and back
  • Steering with two motors
  • An introduction to PWM speed control
  • Driving along a planned path

Technical requirements

For this chapter, you will need the following:

  • First is the built robot, as made in the previous chapters
  • 6 x fresh AA batteries
  • A PC or laptop with a USB micro cable
  • Mu software to write our code and upload it
  • Clear floor space with a meter or so in each direction to test the robot

All code examples are on GitHub at https://github.com/PacktPublishing/Robotics-at-Home-with-Raspberry-Pi-Pico/tree/main/ch-05.

Driving forward and back

Our motors are attached, and the robot is looking ready to power up. First, we’ll use CircuitPython to make test code to try each motor in turn. Then, when we have demonstrated the motors running, we’ll make simple code to drive the motors straight forward and then back.

Testing each motor with CircuitPython

We will start driving our robot by looking at how we connected our Raspberry Pi Pico to our motors in the following figure:

Figure 5.1 – Motor connections from Raspberry Pi Pico

Figure 5.1 shows a closer look at the robot motor connections. On the left is Raspberry Pi Pico with four connections to the motor controller. They are on GPIO 16, 17, 18, and 19. These connections result in the motor controller powering the motor via one of the motor wires. Testing each of the Pico pins should cause a motor to do something.

Let’s try this with some code, setting up one motor, and making it drive in a...

Steering with two motors

If we move one motor and not the other, the robot turns toward the wheel that isn’t moving. For example, look at the following diagram:

Figure 5.4 – Steering a robot with motors

Figure 5.4 shows two turning robots. There is a forward arrow above the left wheel in the first panel, showing the wheel is driving forward. The right wheel is stopped. A transparent arrow superimposed on this shows the turn direction and that this turn pivots on the right wheel. The right robot shows an opposite turn. A robot can turn backward in the same way by reversing a single motor instead.

As we will do more with motors, we’ll extend robot.py so that we can stop them all. Add this code at the end of robot.py:

def stop():
    motor_A1.value = False
    motor_A2.value = False
    motor_B1.value = False
    motor_B2.value = False

Ensure you copy...

An introduction to pulse width modulation speed control

Pulse Width Modulation (PWM) is how we control motor speeds from a digital control system. Instead of varying the voltage supplied to a motor, we use pulses to control it. The pulses are usually at a fixed rate, but the ratio of time-on to time-off changes. We call this the duty cycle. Controlling how much time per cycle the signal is on versus off will control the power getting to a motor. If the pulse is on for longer, the motor will go faster. The motor will go slower if the pulse is on for less time. So, at 50% time-on, the motor will be about 50% of its maximum speed.

The following diagram shows visual examples of this:

Figure 5.6 – PWM signals

The preceding diagram shows graphs of PWM signals. The top is a signal for driving a motor at half speed. The pulses on and off time are the same. The X axis is the level, and the Y axis is for time, with a solid line for the PWM signal and a dashed...

Driving along a planned path

We can use our straight-line driving motions and curved turns to make an almost square path on the floor. We can use the helper functions we’ve made to keep this short.

Putting line and turn moves together

We are going to put some of our learning together to make a simple square pattern, as the following diagram shows:

Figure 5.8 – Driving a square path

The figure shows a square made up of four straight lines and four turns. These eight instructions are four repeating sets of a straight line and then a turn. We will have to adjust the timing of the turn to make it close to 90 degrees.

We start this code with some helpers for our motions – in pwm_drive_square.py:

import time
import robot
def straight(speed, duration):
    robot.set_left(speed)
    robot.set_right(speed)
    time.sleep(duration)
def left(speed, duration):
   ...

Summary

In this chapter, we learned how a controller such as Raspberry Pi Pico uses a motor controller to drive motors. We saw how to control motor directions to drive in a straight(ish) line or make a robot turn.

We then learned about PWM control and how to vary motor speeds, creating a handy robot Python library for our robot in the process. Next, we used this to make sweeping curves and build a multi-step path example with our robot. This path code did, however, show up some accuracy shortcomings.

We have used time to estimate how much we move our motors. However, motors don’t start immediately, and they can sometimes stick. In addition, some motors are slower than others. Therefore, we are working only with approximates. In the next chapter, we will look at how to measure how much the motors have turned to get a more accurate estimation of the robot’s movement.

Exercises

Now you’ve had a taste of driving the robot motors, perhaps you can practice your skills with the following challenges:

  • Can you make other shapes with this method, such as a triangle, or, by using gentle turns, drive in a circle?
  • What is the lowest PWM value before the robot stalls on two motors?
  • Does the preceding value change on one motor?
  • How does the robot’s driving behave on a different surface, such as carpet or wood?

Further reading

These additional resources will help you learn more about the concepts covered in this chapter:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Robotics at Home with Raspberry Pi Pico
Published in: Mar 2023Publisher: PacktISBN-13: 9781803246079
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple