pxt-ev3/libs/gyro-sensor/gyro.ts

109 lines
3.4 KiB
TypeScript
Raw Normal View History

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;
2017-10-24 20:58:52 +02:00
constructor(port: number) {
super(port)
2018-01-05 08:21:19 +01:00
this.calibrating = false;
2017-07-10 16:07:23 +02:00
}
_deviceType() {
return DAL.DEVICE_TYPE_GYRO
2017-07-10 16:07:23 +02:00
}
setMode(m: GyroSensorMode) {
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
*/
//% help=input/gyro/angle
//% 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"
//% weight=64 blockGap=8
//% 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);
2017-07-10 16:07:23 +02:00
this.setMode(GyroSensorMode.Angle)
return this.getNumber(NumberFormat.Int16LE, 0)
}
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-01-05 08:21:19 +01:00
//% help=input/gyro/rotation-rate
//% block="%sensor|rotation 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
//% group="Gyro Sensor"
2018-01-05 08:21:19 +01:00
rotationRate(): number {
if (this.calibrating)
pauseUntil(() => !this.calibrating, 2000);
2017-07-10 16:07:23 +02:00
this.setMode(GyroSensorMode.Rate)
return this.getNumber(NumberFormat.Int16LE, 0)
}
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.
*/
//% help=input/gyro/calibrate
//% block="%sensor|calibrate"
//% blockId=gyroCalibrate
//% parts="gyroscope"
//% blockNamespace=sensors
//% sensor.fieldEditor="ports"
//% weight=50 blockGap=8
2018-01-05 08:21:19 +01:00
//% group="Gyro Sensor"
calibrate(): void {
if (this.calibrating) return; // already in calibration mode
this.calibrating = true;
// may be triggered by a button click, give time to settle
loops.pause(500);
// send a reset command
this.reset();
// we need to switch mode twice to perform a calibration
if (this.mode == GyroSensorMode.Rate)
this.setMode(GyroSensorMode.Angle);
else
this.setMode(GyroSensorMode.Rate);
// switch back and wait
if (this.mode == GyroSensorMode.Rate)
this.setMode(GyroSensorMode.Angle);
else
this.setMode(GyroSensorMode.Rate);
this.calibrating = false;
}
}
2017-10-24 20:58:52 +02: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)
//% fixedInstance whenUsed block="gyro 3" jres=icons.port3
2017-10-24 20:58:52 +02:00
export const gyro3: GyroSensor = new GyroSensor(3)
//% 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
}