top of page

Gyro Steering

Juan Jose

Juan C.

Hi,

I'm Juan and in this lesson I'll teach you how to control the robot's movements using the gyroscope.

This is the second of three lessons dedicated to controlled movements.

To understand the content of the lesson, it is essential to know how to create loops with exit conditions and how to create custom blocks.

Unlike the previous lesson, in this lesson we will directly insert the code into a custom block that you can use to program your missions on the competition field.

Introduction

When we send the robot to complete missions far from our launch area, it is essential to precisely control the direction in which we are sending it.

For example, if the robot makes a turn and does not align perfectly with the direction it needs to go, it will tend to deviate from the desired trajectory, compromising the mission's outcome.

To prevent this from happening, programs called Gyro turn can be created. These programs measure the turn angle using the gyroscope, ensuring greater precision in turns.

Even during straight sections, unexpected events can occur that deviate the robot from its trajectory. Imagine, for example, if it interacts laterally with a mission model. To ensure the maintenance of a straight trajectory, programs called Gyro Straight are used.

The Gyro Steering program combines the functionalities of the two previously described, ensuring precision in both turns and straight sections.

The Gyro Sensor

In Lego robots with a Gyroscope, we refer to a sensor that allows us to measure the angular position of the robot.

In Spike models, the sensor is integrated into the control unit, so it is not necessary to install it separately like the color sensors.

It is a very sensitive instrument that takes the angular position at which the robot is located at the start of the program as the zero value. Thanks to the command Set yaw angle to zero, we can reset the value: always make sure to perform this operation when the robot is stationary since the gyroscope uses accelerations to perform its measurements.

Program logic

To create a program that adjusts the trajectory using the Gyroscope, we will use the start movement block that allows us to insert a trajectory.

Instead of inserting a fixed value for the trajectory, we will write a comparison between the direction we want to move and the one that the gyroscope is actually measuring.

Creating the movement

Let's start by creating a custom block that will always contain Speed and Rotations. This time, however, we will insert the angle to which we want to move as a parameter. This angle is not the angle we want to turn but the angle between the direction we want to move and the direction in which the gyroscope was reset.

gyro block

Once the Define Block has been created, we insert a cycle with a rotation exit condition in which we will insert the Start Movement block.

 

gyro block

Trajectory control

Now we need to write the part of the code that manages the trajectory.

When we enter the value zero in the movement block, the robot moves straight. It moves to the right if we enter positive values and to the left when we enter negative values.

Similarly, we want the robot to move straight when the angle measured by the gyroscope is equal to the one we set. It should turn right when the measured angle is less and left when it is greater than the set angle.

By calculating the difference between the target angle and the angle measured by the gyroscope, we get zero when they are equal, a negative number when the measured angle is greater, and a positive number when the target angle is greater.

gyro block

By inserting this operation inside the block that regulates the trajectory we obtain a movement that tends to align with the established direction.

In particular, the greater the difference between the target angle and the measured one, the greater the steering that the robot makes to get closer to the target angle.

Programs that perform correction proportionally to an error fall into the category of proportional programs and are the most effective in First Lego League.

gyro block
gyro block

Proportional Constant

The simple difference between the target angle and the one measured by the gyroscope could cause corrections that are too strong or too weak depending on the width of the wheelbase of our robot.

To adjust the intensity of the correction, we can add a proportionality constant to the program that multiplies the correction factor. With numbers greater than one, we will have more pronounced corrections, while with smaller values, we get lighter corrections.

To add the constant, we modify the definition of the custom block and insert a new parameter that we call Kg. This parameter will be our proportionality constant.

We insert Kg and the correction factor into a Multiplication block and reinsert everything into the Start Movement block.

gyro block
gyro blocco

Conclusions

The Gyro Steering Block ensures precise and reliable movements and can be implemented to perform accelerated movements. Try combining the things you have learned in the last lessons to create a function that guarantees maximum precision on the competition field.

bottom of page