2017-07-10 16:07:23 +02:00
|
|
|
const enum GyroSensorMode {
|
2019-08-28 01:34:55 +02:00
|
|
|
None = 0,
|
2017-07-10 16:07:23 +02:00
|
|
|
Angle = 0,
|
|
|
|
Rate = 1,
|
|
|
|
}
|
|
|
|
|
2017-10-28 18:13:02 +02:00
|
|
|
namespace sensors {
|
2017-10-25 00:22:07 +02:00
|
|
|
//% fixedInstances
|
2017-07-10 16:07:23 +02:00
|
|
|
export class GyroSensor extends internal.UartSensor {
|
2018-01-05 08:21:19 +01:00
|
|
|
private calibrating: boolean;
|
2018-01-13 17:31:10 +01:00
|
|
|
private _drift: number;
|
2017-10-24 20:58:52 +02:00
|
|
|
constructor(port: number) {
|
|
|
|
super(port)
|
2018-01-05 08:21:19 +01:00
|
|
|
this.calibrating = false;
|
2018-01-13 17:31:10 +01:00
|
|
|
this._drift = 0;
|
|
|
|
this.setMode(GyroSensorMode.Rate);
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
return DAL.DEVICE_TYPE_GYRO
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 17:31:10 +01:00
|
|
|
_query(): number {
|
|
|
|
return this.getNumber(NumberFormat.Int16LE, 0);
|
|
|
|
}
|
|
|
|
|
2017-07-10 16:07:23 +02:00
|
|
|
setMode(m: GyroSensorMode) {
|
2018-01-13 17:31:10 +01:00
|
|
|
if (m == GyroSensorMode.Rate && this.mode != m)
|
|
|
|
this._drift = 0;
|
2017-07-10 16:07:23 +02:00
|
|
|
this._setMode(m)
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:30:05 +02:00
|
|
|
/**
|
|
|
|
* Get the current angle from the gyroscope.
|
2017-10-25 00:37:48 +02:00
|
|
|
* @param sensor the gyroscope to query the request
|
2017-10-24 14:30:05 +02:00
|
|
|
*/
|
2018-02-08 18:35:47 +01:00
|
|
|
//% help=sensors/gyro/angle
|
2018-02-26 17:33:50 +01:00
|
|
|
//% block="**gyro** %this|angle"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% blockId=gyroGetAngle
|
|
|
|
//% parts="gyroscope"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2019-09-29 18:49:13 +02:00
|
|
|
//% weight=64
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Gyro Sensor"
|
2017-11-30 19:34:34 +01:00
|
|
|
angle(): number {
|
2019-09-17 23:30:02 +02:00
|
|
|
this.poke();
|
2018-01-05 08:21:19 +01:00
|
|
|
if (this.calibrating)
|
|
|
|
pauseUntil(() => !this.calibrating, 2000);
|
|
|
|
|
2018-01-13 17:31:10 +01:00
|
|
|
this.setMode(GyroSensorMode.Angle);
|
|
|
|
return this._query();
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 14:30:05 +02:00
|
|
|
/**
|
|
|
|
* Get the current rotation rate from the gyroscope.
|
2017-10-25 00:37:48 +02:00
|
|
|
* @param sensor the gyroscope to query the request
|
2017-10-24 14:30:05 +02:00
|
|
|
*/
|
2018-02-08 18:35:47 +01:00
|
|
|
//% help=sensors/gyro/rate
|
2018-02-26 17:33:50 +01:00
|
|
|
//% block="**gyro** %this|rate"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% blockId=gyroGetRate
|
|
|
|
//% parts="gyroscope"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
|
|
|
//% weight=65 blockGap=8
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Gyro Sensor"
|
2018-01-11 07:29:35 +01:00
|
|
|
rate(): number {
|
2019-09-17 23:30:02 +02:00
|
|
|
this.poke();
|
2018-01-05 08:21:19 +01:00
|
|
|
if (this.calibrating)
|
|
|
|
pauseUntil(() => !this.calibrating, 2000);
|
|
|
|
|
2018-01-13 17:31:10 +01:00
|
|
|
this.setMode(GyroSensorMode.Rate);
|
2019-09-29 18:49:13 +02:00
|
|
|
return this._query() - this._drift;
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
2017-12-07 07:34:11 +01:00
|
|
|
|
2018-01-05 08:21:19 +01:00
|
|
|
/**
|
2019-10-01 07:43:50 +02:00
|
|
|
* Detects if calibration is necessary and performs a full reset, drift computation.
|
2019-08-30 19:58:49 +02:00
|
|
|
* Must be called when the sensor is completely still.
|
2018-01-05 08:21:19 +01:00
|
|
|
*/
|
2019-08-30 19:58:49 +02:00
|
|
|
//% help=sensors/gyro/calibrate
|
|
|
|
//% block="calibrate **gyro** %this|"
|
|
|
|
//% blockId=gyroCalibrate
|
2018-01-05 08:21:19 +01:00
|
|
|
//% parts="gyroscope"
|
|
|
|
//% blockNamespace=sensors
|
2018-02-26 17:33:50 +01:00
|
|
|
//% this.fieldEditor="ports"
|
2019-08-30 19:58:49 +02:00
|
|
|
//% weight=51 blockGap=8
|
2018-01-05 08:21:19 +01:00
|
|
|
//% group="Gyro Sensor"
|
2019-08-30 19:58:49 +02:00
|
|
|
calibrate(): void {
|
2018-01-05 08:21:19 +01:00
|
|
|
if (this.calibrating) return; // already in calibration mode
|
|
|
|
|
2018-12-04 01:37:40 +01:00
|
|
|
const statusLight = brick.statusLight(); // save current status light
|
|
|
|
brick.setStatusLight(StatusLight.Orange);
|
|
|
|
|
2018-01-05 08:21:19 +01:00
|
|
|
this.calibrating = true;
|
2018-02-26 17:33:50 +01:00
|
|
|
// may be triggered by a button click,
|
2018-01-13 17:31:10 +01:00
|
|
|
// give time for robot to settle
|
2018-02-15 01:05:31 +01:00
|
|
|
pause(700);
|
2019-08-30 19:58:49 +02:00
|
|
|
|
2019-10-01 07:43:50 +02:00
|
|
|
// compute drift
|
|
|
|
this.computeDriftNoCalibration();
|
|
|
|
if (Math.abs(this.drift()) < 0.1) {
|
|
|
|
// no drift, skipping calibration
|
|
|
|
brick.setStatusLight(StatusLight.Green); // success
|
|
|
|
pause(1000);
|
|
|
|
brick.setStatusLight(statusLight); // resture previous light
|
|
|
|
|
|
|
|
// and we're done
|
|
|
|
this.calibrating = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:58:49 +02:00
|
|
|
// calibrating
|
|
|
|
brick.setStatusLight(StatusLight.OrangePulse);
|
|
|
|
|
2018-01-05 08:21:19 +01:00
|
|
|
// send a reset command
|
2018-01-11 20:17:23 +01:00
|
|
|
super.reset();
|
2019-08-30 19:58:49 +02:00
|
|
|
// wait till sensor is live
|
|
|
|
pauseUntil(() => this.isActive(), 7000);
|
|
|
|
// mode toggling
|
|
|
|
this.setMode(GyroSensorMode.Rate);
|
|
|
|
this.setMode(GyroSensorMode.Angle);
|
2018-12-04 01:37:40 +01:00
|
|
|
|
|
|
|
// check sensor is ready
|
|
|
|
if (!this.isActive()) {
|
|
|
|
brick.setStatusLight(StatusLight.RedFlash); // didn't work
|
|
|
|
pause(2000);
|
|
|
|
brick.setStatusLight(statusLight); // restore previous light
|
2019-08-30 19:58:49 +02:00
|
|
|
this.calibrating = false;
|
2018-12-04 01:37:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-29 18:49:13 +02:00
|
|
|
// switch to rate mode
|
|
|
|
this.computeDriftNoCalibration();
|
|
|
|
// switch back to the desired mode
|
|
|
|
this.setMode(this.mode);
|
2018-12-04 01:37:40 +01:00
|
|
|
|
2019-09-29 18:49:13 +02:00
|
|
|
// and done
|
2018-12-04 01:37:40 +01:00
|
|
|
brick.setStatusLight(StatusLight.Green); // success
|
|
|
|
pause(1000);
|
|
|
|
brick.setStatusLight(statusLight); // resture previous light
|
|
|
|
|
2018-01-13 17:31:10 +01:00
|
|
|
// and we're done
|
|
|
|
this.calibrating = false;
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:58:49 +02:00
|
|
|
/**
|
|
|
|
* Forces a calibration of the gyro. Must be called when the sensor is completely still.
|
|
|
|
*/
|
|
|
|
//% help=sensors/gyro/reset
|
|
|
|
//% block="reset **gyro** %this|"
|
|
|
|
//% blockId=gyroReset
|
|
|
|
//% parts="gyroscope"
|
|
|
|
//% blockNamespace=sensors
|
|
|
|
//% this.fieldEditor="ports"
|
2019-09-29 18:49:13 +02:00
|
|
|
//% weight=50 blockGap=8
|
2019-08-30 19:58:49 +02:00
|
|
|
//% group="Gyro Sensor"
|
|
|
|
reset(): void {
|
|
|
|
if (this.calibrating) return; // already in calibration mode
|
|
|
|
|
|
|
|
this.calibrating = true;
|
|
|
|
// send a reset command
|
|
|
|
super.reset();
|
|
|
|
// and done
|
|
|
|
this.calibrating = false;
|
|
|
|
}
|
|
|
|
|
2018-01-13 17:31:10 +01:00
|
|
|
/**
|
|
|
|
* Gets the computed rate drift
|
|
|
|
*/
|
2019-09-29 18:49:13 +02:00
|
|
|
//% help=sensors/gyro/drift
|
|
|
|
//% block="**gyro** %this|drift"
|
|
|
|
//% blockId=gyroDrift
|
|
|
|
//% parts="gyroscope"
|
|
|
|
//% blockNamespace=sensors
|
|
|
|
//% this.fieldEditor="ports"
|
|
|
|
//% weight=9 blockGap=8
|
|
|
|
//% group="Gyro Sensor"
|
2018-01-13 17:31:10 +01:00
|
|
|
drift(): number {
|
|
|
|
return this._drift;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-29 18:49:13 +02:00
|
|
|
* Computes the current sensor drift when using rate measurements.
|
2018-01-13 17:31:10 +01:00
|
|
|
*/
|
2019-09-29 18:49:13 +02:00
|
|
|
//% help=sensors/gyro/compute-drift
|
|
|
|
//% block="compute **gyro** %this|drift"
|
|
|
|
//% blockId=gyroComputeDrift
|
|
|
|
//% parts="gyroscope"
|
|
|
|
//% blockNamespace=sensors
|
|
|
|
//% this.fieldEditor="ports"
|
|
|
|
//% weight=10 blockGap=8
|
|
|
|
//% group="Gyro Sensor"
|
|
|
|
computeDrift() {
|
|
|
|
if (this.calibrating)
|
|
|
|
pauseUntil(() => !this.calibrating, 2000);
|
2019-09-30 08:08:46 +02:00
|
|
|
pause(1000); // let the robot settle
|
2019-09-29 18:49:13 +02:00
|
|
|
this.computeDriftNoCalibration();
|
|
|
|
}
|
|
|
|
|
|
|
|
private computeDriftNoCalibration() {
|
|
|
|
// clear drift
|
|
|
|
this.setMode(GyroSensorMode.Rate);
|
2019-08-30 19:58:49 +02:00
|
|
|
this._drift = 0;
|
2019-09-30 23:33:57 +02:00
|
|
|
const n = 10;
|
2019-09-29 18:49:13 +02:00
|
|
|
let d = 0;
|
|
|
|
for (let i = 0; i < n; ++i) {
|
|
|
|
d += this._query();
|
2019-09-30 23:33:57 +02:00
|
|
|
pause(20);
|
2019-09-29 18:49:13 +02:00
|
|
|
}
|
2019-09-30 23:33:57 +02:00
|
|
|
this._drift = d / n;
|
2019-09-29 18:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_info(): string {
|
|
|
|
if (this.calibrating)
|
|
|
|
return "cal...";
|
|
|
|
|
|
|
|
switch (this.mode) {
|
|
|
|
case GyroSensorMode.Angle:
|
|
|
|
return `${this._query()}>`;
|
|
|
|
case GyroSensorMode.Rate:
|
2019-09-30 08:08:46 +02:00
|
|
|
let r = `${this._query()}r`;
|
|
|
|
if (this._drift != 0)
|
2019-09-29 18:49:13 +02:00
|
|
|
r += `-${this._drift | 0}`;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return "";
|
2018-01-05 08:21:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2018-02-26 17:33:50 +01:00
|
|
|
//% fixedInstance whenUsed block="2" weight=95 jres=icons.port2
|
2017-11-16 21:58:37 +01:00
|
|
|
export const gyro2: GyroSensor = new GyroSensor(2)
|
2017-12-07 07:34:11 +01:00
|
|
|
|
2018-02-26 17:33:50 +01:00
|
|
|
//% fixedInstance whenUsed block="1" jres=icons.port1
|
2018-01-05 08:21:19 +01:00
|
|
|
export const gyro1: GyroSensor = new GyroSensor(1)
|
|
|
|
|
2018-02-26 17:33:50 +01:00
|
|
|
//% fixedInstance whenUsed block="3" jres=icons.port3
|
2017-10-24 20:58:52 +02:00
|
|
|
export const gyro3: GyroSensor = new GyroSensor(3)
|
|
|
|
|
2018-02-26 17:33:50 +01:00
|
|
|
//% fixedInstance whenUsed block="4" jres=icons.port4
|
2017-10-24 20:58:52 +02:00
|
|
|
export const gyro4: GyroSensor = new GyroSensor(4)
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|