 # Implementing the final program for moving straight with the Gyro Sensor ProPreview

This video tutorial contains the final 2 programs for moving straight with a LEGO Mindstorms EV3 robot. The first program is for proportional compensation that just keeps the robot orientation straight, while the second program is for Integral compensation that returns the robot to the straight line when the robot makes a mistake.

• #661
• 10 Jan 2018
• 6:27

### Check out the whole course

This video is part of the course Moving Straight with LEGO Mindstorms EV3 robots. You can use the programs directly, but we very much recommend that you move through the course and check the explanation and reasoning behind why the robot works like this.

### Program for keeping the robot straight with the Gyro Sensor (Proportional compensation)

Proportional implementation for keeping the LEGO Mindstorms robot straight. The program will take the value of the Mindstorms Gyro sensor and will apply this value to the steering block. This will make the robot steer in a direction that would put the robot in a straight position again.

### Program for returning to the Straight line after the Robot makes an error (Integral compensating)

This is an EV3-G project that contains two programs implementing an Integral compensation - integral part of the PID algorithm. The first program is for a Five Minute Bot and the second program is for Box Robot. The things that you should be careful when using the program for your robot are the direction of the motors in the steering block; whether the motors in the steering block are written as "B+C" or "C+B" and the coefficients in the two math blocks. The coefficients that we've chosen should work for most of the robots, but will probably not work for some of them. If they don't work, write to us, comment below in the comment section or drop us an email.

### English

So, this is it. The final program for moving forward with a LEGO Mindstorms EV3 robot and for keeping a straight line. And this program implements the integral part of a PID algorithm and it behaves much better than a proportional part. I would have to warn you that you might not be able to understand the program if you've not seen the previous sections but you can just follow it. It's a pretty simple program with a very interesting behavior and feel free to move back and to understand the behavior of the program from the previous videos so that you can now understand the implementation of this program. I'll open a new program and move step by step. First, zoom in so that you can see. Second, it is always a good idea for such programs to have some wait at the beginning. Then we need a new variable and this variable is called integral. And this will store the values for the integral part. And initially it has a value of 0. And then we need a loop. And in this loop what we should do is to calculate a value for the steering. So, we have steering right here and we want this to be on

and we want to do some calculations for this steering right here. And at the end these calculations should be applied to the steering. So, we start, we have a loop, we do something in the loop, at the end we set the steering with the steering block and we set it using this math block. So, this is the basic idea. The other second thing is that before we do any calculations we want to make sure that each calculation is equally important and that's why we implement a wait for tick as we've done in some of the previous videos. So, we reset timer 1. And at the end we wait for timer 1. Right here we wait for timer 1 to be

greater that or equal to 0.1 second. And this will make sure that each of the calculations takes at least 0.1 so they are equally important. And the calculations are the following. We want the integral part to read a value from this integral part and to multiply this value by some coefficient. And this is the coefficient 'b'. So, we'll do some calculation right here for the integral part and we apply this integral part to the steering block. And we apply by multiplying with 1. And we can change this coefficient and we've discussed what is the importance of these coefficients in the previous videos. Now, what are the calculations that we must do? We'll now take the value of the gyro sensor.

This one here is connected on port 4. And we take this value and we add this value to the integral part because initially the integral part is with value 0 we read this value and we add the gyro sensor to the integral part using a math block. And in this way we accumulate the error. And the error is actually the gyro sensor.

And we want some advanced block here. And the advanced block is the following. We take the integral part, we take the gyro sensor and we want to add the gyro sensor but with a coefficient. And this coefficient is 0.25 for our robot. It might be quite different for other robots but not very much and this is something that you should experiment with. And then we write again the result in the integral part. We just write the calculated value. And that's the whole program. We measure the gyro sensor and we add the error that the gyro sensor gives us to the integral part and in this way we accumulate in the integral part. So, if the integral part is positive and the gyro sensor is negative, this means that we are moving towards the line. If the integral part is negative and the gyro sensor is positive, this means that we are moving towards the line but from the other side. And if the integral part is 0 and the gyro sensor detects 0, 0 + 0 will stay 0 and this means that it will have 0 steering at the end. So, if we have something at the integral part, this is the steering. But if the integral part is 0, this means that we are exactly at the line and the gyro sensor is detecting 0. And this is where we want to be. And that's the final program. You can do some modifications here. You can for example say that you would like this program to run for let's say 5 seconds or some other conditions and at the end you must not forget to stop your motors. And this condition is when you reach a mission model for example. And in this way we have a very simple program using the integral part and if you modify these coefficients and find the coefficients for your robot, this program will keep your robot straight and if the robot makes a mistake, it will return the robot to the straight line.

## Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons  • 24
• 102:09
• 3  • 1
• 0
• 0
• 3d_rotation 0