# Keep the LEGO Mindstorms robot orientation straight by using the Gyro Sensor. Proportional Part. ProPreview

The first part of making the robot move straight is to keep it oriented straight. While it moves it could make an error and turn slightly to the right and then the program should turn in back to the left to make its orientation straight. In this video tutorial, we would discuss how to implement a program to keep the robot orientation straight even when we are pushing or pulling it to either side and in the same time it has different wheels.

• #653
• 10 Jan 2018
• 5:13

### Proportional Part

As the robot moves slightly to the left we will steer it to the right. If it moves slightly to the right we will steer it to the left. We get the angle that the gyro is detecting. If this angle is 0, this means we are moving forward. If this angle is not zero, this means we are not oriented straight. So we must take the difference and apply this difference to the steering block. The farther it moves from the 0, the greater the steering should be. This is what proportional means. As the error gets larger we should proportionally try to move in the reverse direction.

### Different wheels

The robot has different wheels. The experiment will keep the robot straight even though it has different wheels because it will be constantly compensating for the turning.

### Pushing/pulling the robot while moving

While the robot is moving we can push or pull it and it will correct for this very large error and continue moving forward.

### 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.

### English

The first part of making the robot move straight is to keep it oriented straight. While it moves it could make an error and turn slightly to the right and then the program should turn it back to the left and make it's orientation straight. In this video tutorial we'll discuss how to implement a program to keep the robot orientation straight even when we are pushing or pulling it to either side and in the same time it has different wheels. What we are doing here is called the proportional part of the algorithm. So, we have a gyro sensor - this one here and we just mount it on the front and we know how to use it and there is no drift. So, these are the preconditions. Now, we have our straight line and let's discuss the proportional part. As the robot moves like this it can move slightly to the right. This is my right. And when it moves slightly to the right we must return the robot to the left. We get the angle from the gyro sensor. The gyro sensor is detecting the angle and if the angle is 0, this means that we are moving forward and we are keeping a straight line. If this angle is not 0, this means that we are not oriented straight. So, if this angle is not 0 like this, we are not oriented straight. So, we must take the difference between 0 which is in this direction and the direction of the gyro sensor and we must take this difference and apply it to the robot. The further it moves from the 0, so if we are for example like this, the greater the steering should be. So, if we are far away from our 0, so if you've turned to the right pretty much like 90 degrees, we must apply this difference to the steering and it will return us back to the initial position. This is what proportional means. As the error gets larger, we should proportionally try to move in the reverse direction. Let's implement this program. Starting with the program. First, we take the block for detecting the value of the gyro sensor. And in my case the gyro sensor is connected on port 4. Then we must move with the robot and we just take a steering block. And in this steering block we just move forward with motors B and C. And if the gyro sensor detects 0, then we must move with the steering 0. But if the gyro sensor detects - or + or any difference, we must apply this difference to the steering block. So, we just add this difference to the steering block and then we put this whole program in a loop like this. And we said that we would like the loop to finish in about 5 seconds. So, this simple program is the proportional part of an algorithm where you can keep the robot orientation straight. It's very simple. We just do the following thing - we take the value of the gyro sensor and we apply this value of the gyro sensor to the steering. If the gyro sensor is with value 0, then we just move forward. But if the gyro sensor detects something, we must turn in the reverse direction. So, it also depends - check out your motors if these are B - C or C - B to know whether the steering is applied in the right way. But for our robot this is OK. Let's run the program and see how the robot behaves.

What you just saw are the two robots - the Box Robot and the Five Minute Robot and they are keeping the orientation straight and even if we push or pull them from one of the sides, they will try to return so that they are oriented straight ahead so the gyro sensor detects 0. It is worth mentioning that this would even work if the two wheels are with different size which is quite funny. You might have a robot where one of the wheels is larger than the other and it will still try to move forward and it will move forward very well.