Motor Control
In the previous chapter, we tried to make our robots move forward in a straight line and land on a specific target location. More than likely, however, your robot veered off course. In this chapter, we will learn how to use wheel encoders to measure the distance traveled by the robot. This information will allow us to correct the robot’s trajectory and make it move more predictably.
What you will learn
- About the fundamentals of DC motors
- How to use wheel encoders to measure the distance traveled by the robot
- How to implement proportional control of a DC motor
Terminology
- DC (direct current) motor
- a type of electric motor that converts direct current electrical power into rotational power
- H-bridge
- an electronic circuit that enables a voltage to be applied across a load in either direction
- PWM (pulse-width modulation)
- a technique for producing analog-like signals using square wave pulses of varying duty cycles
- quadrature encoder
- an incremental encoder that enables measuring both the speed and direction of a motor axle
Lecture
Links
In this chapter, we’ll
- first review the fundamentals of the DC motors that move our robots,
- then explore the basic electronics enabling directional control (H-bridge), speed control (PWM), and feedback (quadrature encoder),
- and finally we’ll cover the basics of proportional control of a DC motor.
Reviewing a DC motor model
Keys from the video
- motor torque (and therefore speed) is proportional to the current flowing through the motor
- the direction of rotation is determined by the direction of the current
- there is a minimum voltage level needed to overcome the motor’s internal resistance
Exploring the electronics enabling motor control
Keys from the video
- an H-bridge enables the motor to rotate in either direction
- pulse-width modulation (PWM) is used to control the speed of the motor
- a quadrature encoder provides feedback on the motor’s speed and direction
Proportional control of a DC motor
Keys from the video
- proportional control adjusts the motor’s speed based on the difference between the desired and actual speed
- the proportional gain determines how quickly the motor responds to changes in speed
- the proportional gain must be tuned to achieve the desired performance
Exercise
This week’s goal is to complete the go-to-goal task with a greater degree of accuracy. You will use the proportional control of the DC motor to adjust the robot’s speed based on the difference between the desired and actual speed. Today, you will implement a Toit class that controls the speed of both motors.
You will submit your responses on gradescope. Only one partner should submit. The submitter will add the other partner through the gradescope interface.
Additional details for using gradescope can be found here:
You should open the gradescope assignment now so that you know what to work complete.
Grading
I will grade all exercises using a scale of “Nailed It” / “Not Yet”. See the course grading policy for more information, and check gradescope for deadlines.
Overview
The goal this class period is simple: measure a few characteristics of your robot’s motors and start working on a proportional control algorithm. Specifically, you will need to determine the minimum PWM duty factor needed to get your robot to move and the maximum speed of your robot.
These values are important for the proportional control algorithm you will implement in the next class period.
Determine Your Minimum PWM Duty Factors
Your first task is to discover the minimum duty factor needed to get your robot to move. You will need to measure this separately for both motors, as there might be slight differences in how they respond to motor commands (i.e., PWM duty cycles).
I recommend taking the following approach:
- Create a new program.
- Import the
motor
package. - Ideally you will also use a heartbeat to keep your program running.
- Write a
main
function that increments the value of the PWM signal every 2 seconds using theset-pwm-duty-factor
method. You should first read that function and try to understand the input it expects.
You could also try running your motors in reverse using the set-direction
method.
Determine Your Robot’s Maximum Speed
Your second task is to determine the maximum speed of your robot. This is the speed at which your robot moves as close to straight forward as possible at the highest PWM duty factor for each motor as possible. Due to differences in your left and right motors, I would expect one duty factor to be set to 1.0
and the other to be slightly less than 1.0
.
I recommend taking the following approach:
- Create a new program.
- Import the
motor
package. - Ideally you will also use a heartbeat to keep your program running.
- Write a
main
function that sets the PWM duty factor to1.0
for both motors and determine which motor needs to be slowed down. - Find a lower PWM duty factor that allows your robot to move straight forward.
You should also try running your motors in reverse.
Implement Proportional Control
If you have time, you should consider starting on the proportional control algorithm. You will need to create a new class that controls the speed of both motors based on the difference between the desired and actual speed.
Wrap-Up
Now that you have a proportional control class, you are ready to take another attempt at the go-to-goal task.