* disable drift correction by default

* disable drift correction

* better calibration sequence

* add comments

* updated comment about beta
This commit is contained in:
Peli de Halleux 2019-08-30 10:58:49 -07:00 committed by GitHub
parent 42fe96aa5a
commit 140ba64462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 173 additions and 62 deletions

View File

@ -6,6 +6,10 @@ For teams participating in the Open Software Platform Pilot utilizing MakeCode,
## FAQ
### I found a bug what do I do?
If you found a bug, please try if it hasn't been fixed yet! Go to https://makecode.mindstorms.com/beta and try if the bug is corrected. Otherwise, please open an issue at https://github.com/microsoft/pxt-ev3/issues.
### How do I use MakeCode with my EV3?
* You will need to install the latest EV3 firmware on your brick. Instructions on how to do that are located here: https://makecode.mindstorms.com/troubleshoot.

View File

@ -3,6 +3,7 @@
```cards
sensors.gyro1.angle();
sensors.gyro1.rate();
sensors.gyro1.calibrate();
sensors.gyro1.reset();
```
@ -10,4 +11,6 @@ sensors.gyro1.reset();
[angle](/reference/sensors/gyro/angle),
[rate](/reference/sensors/gyro/rate),
[reset](/reference/sensors/gyro/calibrate),
[reset](/reference/sensors/gyro/reset)

View File

@ -12,7 +12,7 @@ When the brick changes its position, it's moved in the direction of one of the a
## Accuracy and calibration
Gyro sensors aren't perfectly accurate. Sometimes, because of temperature and changes in the way elecricity behaves in the sensor, the gyro returns a small error in it's measurement. This causes the gyro sensor to return an incorrect value for the amount of angle it detected. This might make your robot drive off course and not go to where you want it to.
Gyro sensors aren't perfectly accurate. Sometimes, because of temperature and changes in the way electricity behaves in the sensor, the gyro returns a small error in it's measurement. This causes the gyro sensor to return an incorrect value for the amount of angle it detected. This might make your robot drive off course and not go to where you want it to.
### Drift
@ -20,7 +20,7 @@ If you want to turn the tank or robot you built to the left by 45 degrees, you m
The problem is that when you need to read the angle measurement frequently, the amount of error in the angle measurement may continue to increase. If the sensor thought it moved by 45 degrees the first time instead of really 49 degrees, your second turn will put you at 98 degrees when the sensor said 90 degrees. If you want a robot to turn right 90 degrees and drive for 5 meters, it might actually turn 98 degrees and drive 0.7 meters off course before it stops. This error in the sensor's measurement is called _drift_.
### Time to reset
### Calibration
If errors in the angle values returned by the gyro sensor are making your project not work right, then it's time to **[reset](/reference/sensors/gyro/reset)**. A reset will return the gyro sensor's current angle value back to `0` and _calibrate_ for drift. Calibration is the process of finding out how much error there is in a sensor's measurement and then removing the error from the value returned to your program.
@ -42,7 +42,14 @@ Turn the brick and press ENTER to see the current rotation angle of `gyro 2`.
```blocks
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
brick.showNumber(sensors.gyro2.angle(), 1)
sensors.gyro2.reset()
})
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
sensors.gyro2.calibrate()
})
forever(function () {
brick.showNumber(control.millis(), 1)
brick.showNumber(sensors.gyro2.angle(), 2)
})
```

View File

@ -0,0 +1,51 @@
# calibrate
Reset the zero reference for the gyro to current position of the brick.
```sig
sensors.gyro2.calibrate()
```
To make the gyro measure rotation angle from the current position of the brick, it is recalibrated. That is, the brick's current position is set to `0` degrees and rotation angle measurements start from there.
This function adds a few pauses to ensure that the robot is still. If you only want to reset the sensor, use [reset](/reference/gyro-sensor/reset).
## ~hint
The current position is considered to be the [_horizon_](https://en.wikipedia.org/wiki/Attitude_indicator) or a place that is the _plane of reference_ (this is possibly someplace that's flat for a horizontal reference).
## ~
## ~hint
**Important**
To properly reset the gyro, the brick must remain still (undisturbed) while the reset operation takes place.
## ~
## Calibration states
Calibration happens in the following phases and each phase is tracked by the brick status light.
* **orange**: sensor initialization. This phase ensures that the sensor is in the desired mode and ready to collect data.
* **orange pulse**: data collection. Light information is being collected, move the sensor over the various light sources to detect.
* **green**: calibration success. The calibration data has been saved.
* **red flash**: sensor failure. We were unable to connect to the sensor.
## Example
Set the brick on a flat surface. Reset `gyro 2` and tilt the brick slightly. Reset it again while it's still tilted. Lay the brick down flat again and display the angle measurement.
```blocks
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
sensors.gyro2.calibrate()
})
forever(function() {
brick.showNumber(sensors.gyro2.angle(), 1)
})
```
## See also
[angle](/reference/sensors/gyro/angle), [rate](/reference/sensors/gyro/rate)

View File

@ -13,12 +13,39 @@ When the brick is in motion, it moves in the direction of one of axes used to me
* a [number](/types/number) that is the current rate of rotation in degrees per second.
## ~hint
## Accuracy and calibration
Gyro sensors aren't perfectly accurate. Sometimes, because of temperature and changes in the way electricity behaves in the sensor, the gyro returns a small error in it's measurement. This causes the gyro sensor to return an incorrect value for the amount of angle it detected. This might make your robot drive off course and not go to where you want it to.
### Drift
If you want to turn the tank or robot you built to the left by 45 degrees, you might run the motor on the right side until the gyro reported that you turned by 45 degrees. What if the gyro was getting an error of 4 degrees every time it measured an angle? You may have actually turned 49 degrees when you expected to turn 45 degrees. Well, that might not be too bad if you use the gyro's angle value only once. It's fine if you just wanted to turn and stop or drive a short distance in only that direction.
The problem is that when you need to read the angle measurement frequently, the amount of error in the angle measurement may continue to increase. If the sensor thought it moved by 45 degrees the first time instead of really 49 degrees, your second turn will put you at 98 degrees when the sensor said 90 degrees. If you want a robot to turn right 90 degrees and drive for 5 meters, it might actually turn 98 degrees and drive 0.7 meters off course before it stops. This error in the sensor's measurement is called _drift_.
### Calibration
If errors in the angle values returned by the gyro sensor are making your project not work right, then it's time to **[reset](/reference/sensors/gyro/reset)**. A reset will return the gyro sensor's current angle value back to `0` and _calibrate_ for drift. Calibration is the process of finding out how much error there is in a sensor's measurement and then removing the error from the value returned to your program.
Are you using a gyro sensor in your project and need accuracy for your angle values? You should reset the gyro sensor at a regular intervals to improve precision in the values reported to your program.
## ~
## Example
Flash the status light to red if the roll rate of `gyro 2` is more that `30` degrees per second.
```blocks
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
sensors.gyro2.reset()
})
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
sensors.gyro2.calibrate()
})
forever(function () {
brick.showNumber(sensors.gyro2.rate(), 2)
if (sensors.gyro2.rate() > 30) {
brick.setStatusLight(StatusLight.RedFlash)
} else {

View File

@ -1,6 +1,6 @@
# reset
Reset the zero reference for the gyro to current position of the brick.
Reset the gyro sensor.
```sig
sensors.gyro2.reset()
@ -8,6 +8,8 @@ sensors.gyro2.reset()
To make the gyro measure rotation angle from the current position of the brick, it is recalibrated. That is, the brick's current position is set to `0` degrees and rotation angle measurements start from there.
This function only resets the sensor; if you wish to have progress indication and a more robust calibration sequence, use [calibrate](/reference/gyro-sensor/calibrate).
## ~hint
The current position is considered to be the [_horizon_](https://en.wikipedia.org/wiki/Attitude_indicator) or a place that is the _plane of reference_ (this is possibly someplace that's flat for a horizontal reference).
@ -18,27 +20,18 @@ The current position is considered to be the [_horizon_](https://en.wikipedia.or
**Important**
To properly reset the gyro, the brick must remain still (undistrurbed) while the reset operation takes place.
To properly reset the gyro, the brick must remain still (undisturbed) while the reset operation takes place.
## ~
## Calibration states
Calibration happens in the following phases and each phase is tracked by the brick status light.
* **orange**: sensor initialization. This phase ensures that the sensor is in the desired mode and ready to collect data.
* **orange pulse**: data collection. Light information is being collected, move the sensor over the various light sources to detect.
* **green**: calibration success. The calibration data has been saved.
* **red flash**: sensor failure. We were unable to connect to the sensor.
## Example
Set the brick on a flat surface. Reset `gyro 2` and tilt the brick slighly. Reset it again while it's still tilted. Lay the brick down flat again and display the angle measurement.
Set the brick on a flat surface. Reset `gyro 2` and tilt the brick slightly. Reset it again while it's still tilted. Lay the brick down flat again and display the angle measurement.
```blocks
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
sensors.gyro2.reset()
})
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
forever(function() {
brick.showNumber(sensors.gyro2.angle(), 1)
})
```

View File

@ -9,12 +9,12 @@ namespace sensors {
export class GyroSensor extends internal.UartSensor {
private calibrating: boolean;
private _drift: number;
private _drifting: boolean;
private _driftCorrection: boolean;
constructor(port: number) {
super(port)
this.calibrating = false;
this._drift = 0;
this._drifting = true;
this._driftCorrection = false;
this.setMode(GyroSensorMode.Rate);
}
@ -70,14 +70,78 @@ namespace sensors {
this.setMode(GyroSensorMode.Rate);
let curr = this._query();
if (Math.abs(curr) < 20) {
const p = 0.0005;
if (Math.abs(curr) < 4 && this._driftCorrection) {
const p = 0.01;
this._drift = (1 - p) * this._drift + p * curr;
curr -= this._drift;
curr = Math.round(curr - this._drift);
}
return curr;
}
/**
* Forces a calibration of the with light progress indicators.
* Must be called when the sensor is completely still.
*/
//% help=sensors/gyro/calibrate
//% block="calibrate **gyro** %this|"
//% blockId=gyroCalibrate
//% parts="gyroscope"
//% blockNamespace=sensors
//% this.fieldEditor="ports"
//% weight=51 blockGap=8
//% group="Gyro Sensor"
calibrate(): void {
if (this.calibrating) return; // already in calibration mode
const statusLight = brick.statusLight(); // save current status light
brick.setStatusLight(StatusLight.Orange);
this.calibrating = true;
// may be triggered by a button click,
// give time for robot to settle
pause(700);
// calibrating
brick.setStatusLight(StatusLight.OrangePulse);
// send a reset command
super.reset();
// wait till sensor is live
pauseUntil(() => this.isActive(), 7000);
// mode toggling
this.setMode(GyroSensorMode.Rate);
this.setMode(GyroSensorMode.Angle);
// switch back to the desired mode
this.setMode(this.mode);
// check sensor is ready
if (!this.isActive()) {
brick.setStatusLight(StatusLight.RedFlash); // didn't work
pause(2000);
brick.setStatusLight(statusLight); // restore previous light
this.calibrating = false;
return;
}
// compute drift
this._drift = 0;
if (this._driftCorrection && this.mode == GyroSensorMode.Rate) {
const n = 100;
for (let i = 0; i < n; ++i) {
this._drift += this._query();
pause(4);
}
this._drift /= n;
}
brick.setStatusLight(StatusLight.Green); // success
pause(1000);
brick.setStatusLight(statusLight); // resture previous light
// and we're done
this.calibrating = false;
}
/**
* Forces a calibration of the gyro. Must be called when the sensor is completely still.
*/
@ -92,49 +156,10 @@ namespace sensors {
reset(): void {
if (this.calibrating) return; // already in calibration mode
const statusLight = brick.statusLight(); // save current status light
brick.setStatusLight(StatusLight.Orange);
this.calibrating = true;
// may be triggered by a button click,
// give time for robot to settle
pause(700);
// send a reset command
super.reset();
// switch back to the desired mode
this.setMode(this.mode);
// wait till sensor is live
pauseUntil(() => this.isActive(), 5000);
// check sensor is ready
if (!this.isActive()) {
brick.setStatusLight(StatusLight.RedFlash); // didn't work
pause(2000);
brick.setStatusLight(statusLight); // restore previous light
return;
}
// give it a bit of time to init
pause(1000)
// calibrating
brick.setStatusLight(StatusLight.OrangePulse);
// compute drift
this._drift = 0;
if (this.mode == GyroSensorMode.Rate) {
for (let i = 0; i < 200; ++i) {
this._drift += this._query();
pause(4);
}
this._drift /= 200;
}
brick.setStatusLight(StatusLight.Green); // success
pause(1000);
brick.setStatusLight(statusLight); // resture previous light
// and we're done
// and done
this.calibrating = false;
}
@ -152,7 +177,8 @@ namespace sensors {
*/
//%
setDriftCorrection(enabled: boolean) {
this._drifting = enabled;
this._driftCorrection = enabled;
this._drift = 0;
}
}