2017-07-10 16:07:23 +02:00
|
|
|
const enum GyroSensorMode {
|
|
|
|
None = -1,
|
|
|
|
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;
|
|
|
|
private _drifting: boolean;
|
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._drifting = true;
|
|
|
|
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
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%sensor|angle"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% blockId=gyroGetAngle
|
|
|
|
//% parts="gyroscope"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2017-12-25 02:46:58 +01:00
|
|
|
//% sensor.fieldEditor="ports"
|
2018-01-09 21:46:48 +01:00
|
|
|
//% weight=64 blockGap=8
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Gyro Sensor"
|
2017-11-30 19:34:34 +01:00
|
|
|
angle(): number {
|
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-01-11 07:29:35 +01:00
|
|
|
//% block="%sensor|rate"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% blockId=gyroGetRate
|
|
|
|
//% parts="gyroscope"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2017-12-25 02:46:58 +01:00
|
|
|
//% sensor.fieldEditor="ports"
|
2017-10-24 14:30:05 +02:00
|
|
|
//% 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 {
|
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);
|
|
|
|
let curr = this._query();
|
|
|
|
if (Math.abs(curr) < 20) {
|
|
|
|
const p = 0.0005;
|
|
|
|
this._drift = (1 - p) * this._drift + p * curr;
|
|
|
|
curr -= this._drift;
|
|
|
|
}
|
|
|
|
return curr;
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
2017-12-07 07:34:11 +01:00
|
|
|
|
2018-01-05 08:21:19 +01:00
|
|
|
/**
|
|
|
|
* Forces a calibration of the gyro. Must be called when the sensor is completely still.
|
|
|
|
*/
|
2018-02-08 18:35:47 +01:00
|
|
|
//% help=sensors/gyro/reset
|
2018-02-02 01:21:08 +01:00
|
|
|
//% block="reset %sensor|"
|
2018-01-11 07:29:35 +01:00
|
|
|
//% blockId=gyroReset
|
2018-01-05 08:21:19 +01:00
|
|
|
//% parts="gyroscope"
|
|
|
|
//% blockNamespace=sensors
|
|
|
|
//% sensor.fieldEditor="ports"
|
2018-02-02 01:21:08 +01:00
|
|
|
//% weight=50
|
2018-01-05 08:21:19 +01:00
|
|
|
//% group="Gyro Sensor"
|
2018-01-11 07:29:35 +01:00
|
|
|
reset(): void {
|
2018-01-05 08:21:19 +01:00
|
|
|
if (this.calibrating) return; // already in calibration mode
|
|
|
|
|
|
|
|
this.calibrating = true;
|
2018-01-13 17:31:10 +01:00
|
|
|
// may be triggered by a button click,
|
|
|
|
// give time for robot to settle
|
2018-02-15 01:05:31 +01:00
|
|
|
pause(700);
|
2018-01-05 08:21:19 +01:00
|
|
|
// send a reset command
|
2018-01-11 20:17:23 +01:00
|
|
|
super.reset();
|
2018-01-13 17:31:10 +01:00
|
|
|
// switch back to the desired mode
|
|
|
|
this.setMode(this.mode);
|
|
|
|
// wait till sensor is live
|
|
|
|
pauseUntil(() => this.isActive());
|
|
|
|
// give it a bit of time to init
|
2018-02-15 01:05:31 +01:00
|
|
|
pause(1000)
|
2018-01-13 17:31:10 +01:00
|
|
|
// compute drift
|
|
|
|
this._drift = 0;
|
|
|
|
if (this.mode == GyroSensorMode.Rate) {
|
|
|
|
for (let i = 0; i < 200; ++i) {
|
|
|
|
this._drift += this._query();
|
2018-02-15 01:05:31 +01:00
|
|
|
pause(4);
|
2018-01-13 17:31:10 +01:00
|
|
|
}
|
|
|
|
this._drift /= 200;
|
|
|
|
}
|
|
|
|
// and we're done
|
|
|
|
this.calibrating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the computed rate drift
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
drift(): number {
|
|
|
|
return this._drift;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disable drift correction
|
|
|
|
* @param enabled
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
setDriftCorrection(enabled: boolean) {
|
|
|
|
this._drifting = enabled;
|
2018-01-05 08:21:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 20:58:52 +02:00
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="gyro 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-01-05 08:21:19 +01:00
|
|
|
//% fixedInstance whenUsed block="gyro 1" jres=icons.port1
|
|
|
|
export const gyro1: GyroSensor = new GyroSensor(1)
|
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="gyro 3" jres=icons.port3
|
2017-10-24 20:58:52 +02:00
|
|
|
export const gyro3: GyroSensor = new GyroSensor(3)
|
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="gyro 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
|
|
|
}
|