pxt-ev3/docs/tutorials/move-straight-with-gyro.md
Peli de Halleux 80b9c715b2
Gyro tutorials (#930)
* gyro tutorials

* tutorials

* fix gyro simulator

* images

* updated image

* fix svg errors
2019-09-30 22:43:50 -07:00

1.9 KiB

Move Straight With Gyro

Introduction @fullscreen

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