Move Straight With Gyro

Introduction

Rotating using a wheel is not precise. The wheel can slip or the motors can be slightly different. With the help of the gyro you can detect and correct deviations in your trajectory.

Step 1 Calibration

Add a ||sensors:calibrate gyro|| block in a ||brick:on button enter pressed|| block so that you can manually start a calibration process. Run the calibration at least once after connecting the gyro.

brick.showPorts()
sensors.gyro2.calibrate()

Step 2 Compute the error

Make a new error variable and drag the ||sensors:gyro rate|| and multiply it by -1. Since the rate shows the rotation rate, we will counter it by negating it.

let error = 0
brick.showPorts()
sensors.gyro2.calibrate()
while (true) {
    error = sensors.gyro2.rate() * -1
}

Step 3 Steer with feedback

Drag a ||motors:steer motors|| block under the variable and pass the error variable into the turn ratio section.

If the robot is turning right, the gyro will report a positive rotation rate and the turn ratio will be negative which will the turn the robot left!

let error = 0
brick.showPorts()
sensors.gyro2.calibrate()
while (true) {
    error = sensors.gyro2.rate() * -1
    motors.largeBC.steer(error, 50)
}

Step 4 Run it!

Download to your brick and test out if the robot is going straight.

This kind of technique is called a proportional controller; it corrects the inputs (motor speed) with a feedback proportional to the output (rotation rate).