pxt-ev3/docs/tutorials/drifter.md
Peli de Halleux 374bbb8304
Drift-compensated angle in gyro (#931)
* compute angle based on undrifted rate

* add is calibrating function

* fix integrator

* added example

* docs

* poll faster
2019-10-01 10:11:58 -07:00

1.0 KiB

Drifter

Use this program to try out the gyro sensor and the effect of drifting.

// this loop shows the rate, angle and drift of the robot
forever(() => {
    brick.showValue("rate", sensors.gyro2.rate(), 1)
    brick.showValue("angle", sensors.gyro2.angle(), 2)
    brick.showValue("drift", sensors.gyro2.drift(), 3)
})
// this loop shows wheter the sensor is calibrating
forever(() => {
    brick.showString(sensors.gyro2.isCalibrating() ? "calibrating..." : "", 4)
})
// instructions on how to use the buttons
brick.showString("ENTER: calibrate", 7)
brick.showString("       (reset+drift)", 8)
brick.showString("LEFT: reset", 9)
brick.showString("RIGHT: compute drift", 10)

// enter -> calibrate
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
    sensors.gyro2.calibrate()
})
// right -> compute drift
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
    sensors.gyro2.computeDrift()
})
// left -> reset
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
    sensors.gyro2.reset()
})