Friday, May 21, 2021

LEGO EV3 Minimal Gyro Proportional Drive Straight for FLL Robots

How to Help Your Robot Drive Straight Using the Gyro Sensor

Wait, It Drifts Left AND Right? 
During my second season coaching FLL, the team had a robot that would sometimes drift to the left, but other times drift to the right, sometimes during the same run! This drove them crazy most of the season. Worst of all, when they put a mat on a hard floor to simulate a perfectly flat board, the drifting all but disappeared! 

We finally figured out that the northside of our regular practice board was slightly higher than the south side.  The robot had much of its weight on the back which made the back of the robot want to slide downhill while the robot was driving. This made the robot tend to drive uphill! So driving west to east the robot would drift left, but driving east to west the robot would drift right (always uphill). 

Driving straight can be one of the most challenging tasks for any robot, including those in FIRST LEGO League. The issue is that robots do not naturally drive straight. Actually very few things naturally move in a straight line (even people, see MythBusters Season 9 Episode 15: Walk a Straight Line). The article below discusses some of the reasons driving straight is such a challenge for robots.

http://www.legoengineering.com/why-doesnt-my-robot-drive-straight/

When properly applied, a Gyro Sensor can help your drive robot straight. I am going to walk you through the simplest way I know to use a Gyro sensor, on an EV3 in this example. We are going to start simple, and explain in more detail as we progress. 

The program below uses the Gyro Sensor to help the robot driving straight. We want the robot to drive straight in the direction, or heading, of 0 degrees at 50 power for a total of 4 rotations. We use 0 degrees because that is the initial reading from the Gyro Sensor anytime you run a new program.









One of the first things to notice is that we are using the Gyro Sensor in Angle mode. When the program starts the Gyro Sensor resets to 0 so we are using 0 degree as our heading ("a" in the red Math Block).

If the robot drifts left, the Gyro Sensor will report how far the robot has turned left in negative degrees. For example, if the robot is pointing 5 degrees left of where it was pointing when the program started, the Gyro Sensor will read -5. And if the robot drifts to the right the Gyro Sensor will report how far it has turned to the right in positive degrees. So if the robot is pointing 10 degrees to the right of where it was pointing when the program started, the Gyro Sensor will read  10. 

The program works by using a LOOP to read Gyro Sensor on port 2, then calculate the heading error by subtracting the gyro reading from our desired heading, 0 degrees, and using the heading error as the Steering Angle for the Move Steering block (the Motor Power is fixed at 60). This happens as fast as the Loop can run (many times per second). This continues until Motor B rotations are greater than or equal to 4, or for four rotations.

This is a Proportional Control or Proportional Feedback system. It is called this because you are changing the output, the Steering Angle, by an amount that is proportional to the error, or the difference between the desired heading (0 degrees) and the measured direction.

A control system work to keep the system error as close to zero a possible. In this case it just means that the EV3 blocks above will keep adjusting the move block's steering angle to keep the robot's gyro sensor, and the robot, pointing at a heading of zero degrees. 

Understanding Proportional Feedback to Help Your Robot Drive Straight


The Good Stuff - Proportional Feedback and Proportional Control

We are going to walk through how to use the Gyro Sensor and Proportional Feedback to make your EV3 based LEGO robot drive straight. The robot will drive straight even if the wheels slip or if the wheels are even different sizes. I am still impressed every time I push the back of the robot to make it turn off course and it turns back to follow the desired heading.

Gyro Sensor
We are going to assume that your robot's Gyro Sensor is mounted with the arrows facing up (ensuring that rotating the robot to the left will produce negative readings from the sensor) and somewhere near the centerline of the robot (ensuring that turns to the left and right each require similar adjustments to the robot's drive system.

The Gyro Sensor does have a few unique traits. First, it calibrates while the EV3 brick is booting up. This means your robot needs to be very still while booting. I recommend putting the robot on a clean piece of paper on the floor for booting up.

You can force the Gyro Sensor to recalibrate by plugging and unplugging the sensor's cable or using a specific combination of programming blocks. I will not cover software methods for resting the Gyro Sensor because it very often causes more problems than it solves.

Move Steering
The Move Steering block makes our task simpler by allowing us to change the robot's direction by adjusting the Steering Angle input.

Proportional Control simply means that you are making a change, this case steering angle, based on the size of the measured error. Here our error is the difference between the direction we want to drive and the angle the gyro says we are driving. output to a value that is proportional to the system error. In the case of the Gyro Sensor, any errors to the right, or clockwise, are read as positive angles and error to the left are negative angles. And we are going to use a Move Steering block and adjust the Steering Angle based on the Gyro Sensor reading to make the robot drive straight. So if the robot is going a little left of where we want, say -5 degrees, then we want to set the Steering Angle to steer to the right, perhaps 5 degrees.

Below is a Minimal Gyro Drive Straight program. I think it is very cool that you can build it with only five blocks, including the Start block!

Going Straight
Going in a straight line is not a natural thing for a robot, or for humans for that matter (see MythBusters episode 173: Walk a Straight Line). It turns out that humans and robots both need some kind of feedback to go in a straight line. For humans, the feedback is usually visual but you can also use a compass as a reference to keep you pointed in the same direction. Robots are similar in that you can use a Color Sensor to follow a line or use the Gyro Sensor to follow a heading.

Why Straight is Difficult for Robots
Driving straight using only mechanical design is challenging. The robot needs to have wheels of the same diameter, precisely aligned to each other and in line with the robot chassis, the motors must rotate the wheels at the same speed (be synchronized), the weight distribution must be balanced between the wheel to ensure the robot has a center of gravity centered between the wheels and the wheels cannot slip. 

Rarely if ever are all of these conditions true, so most robots, even good designs, do not actually drive straight. For example, a ball bearing castor will often provide changing forces during a straight drive and cause small changes in direction. Some of the driving straight issues can be difficult to diagnose. This is one reason that robust FLL robot strategies always include aligning on mission models, or lines or walls at multiple points during a run. Alignments and referencing are a top of their own that I hope to touch on in the future,

Potential Gyro Pitfalls and How to Avoid Them

Gyro sensors are very useful sensor but can be a little confusing, and has some pitfalls that have tripped up even advanced teams.

First, Gyro sensors usually calibrate automatically at power on, like when the EV3 boots up, which means any movement of the robot, or table, while the EV3 brick powers up can produce drifting in the gyro. I recommend setting the robot on the floor on a sheet of paper (to keep dirt off of the wheels) and let it boot up without being disturbed. 

Second, poor wire management can lead to connection glitches that cause the Gyro Sensor to restart and recalibrate. The robot may drive in sharp arcs or even spin in place follow a gyro reset due to a poor connection while the robot was being handled by the team. This can be a very tough problem to identify because the problem happens when the wires are bumped but the robot driving failure doesn't show up until you go to use the gyro later. So the robot can be great on a practice table but suffer a gyor reset while walking to the competition tables or even during a match.

I recommend NOT using a program to force the gyro to recalibrate. I have worked with many teams how had a great plan for how and when they would calibrate the gyro, and most, if not all, eventually have the gyro fail them during a competition run. I do recommend that early in the season adding some programming that will alert you if the gyro reading is changing when the robot should not be moving. This will let you find out if and when your robot's gyro starts drifting. This can warn you when someone didn't follow the EV3 startup procedure or a cable issue caused a gyro reset. 

I hope this has been helpful and encourages you to look into trying out a gyro sensor on your robot! 

No comments:

Post a Comment