pxt-ev3/libs/core/gyro.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-07-10 15:07:23 +01:00
const enum GyroSensorMode {
None = -1,
Angle = 0,
Rate = 1,
}
namespace input {
2017-10-24 15:22:07 -07:00
//% fixedInstances
2017-07-10 15:07:23 +01:00
export class GyroSensor extends internal.UartSensor {
2017-10-24 19:58:52 +01:00
constructor(port: number) {
super(port)
2017-07-10 15:07:23 +01:00
}
_deviceType() {
return DAL.DEVICE_TYPE_GYRO
2017-07-10 15:07:23 +01:00
}
setMode(m: GyroSensorMode) {
this._setMode(m)
}
2017-10-24 05:30:05 -07:00
/**
* Get the current angle from the gyroscope.
2017-10-24 15:37:48 -07:00
* @param sensor the gyroscope to query the request
2017-10-24 05:30:05 -07:00
*/
//% help=input/gyro/angle
2017-10-24 15:37:48 -07:00
//% block="%sensor|angle"
2017-10-24 05:30:05 -07:00
//% blockId=gyroGetAngle
//% parts="gyroscope"
//% blockNamespace=input
2017-10-24 15:22:07 -07:00
//% weight=65 blockGap=8
//% group="Gyro Sensor"
2017-10-24 05:30:05 -07:00
angle() {
2017-07-10 15:07:23 +01:00
this.setMode(GyroSensorMode.Angle)
return this.getNumber(NumberFormat.Int16LE, 0)
}
2017-10-24 05:30:05 -07:00
/**
* Get the current rotation rate from the gyroscope.
2017-10-24 15:37:48 -07:00
* @param sensor the gyroscope to query the request
2017-10-24 05:30:05 -07:00
*/
//% help=input/gyro/rate
2017-10-24 15:37:48 -07:00
//% block="%sensor|rotation rate"
2017-10-24 05:30:05 -07:00
//% blockId=gyroGetRate
//% parts="gyroscope"
//% blockNamespace=input
//% weight=65 blockGap=8
//% group="Gyro Sensor"
2017-10-24 05:30:05 -07:00
rate() {
2017-07-10 15:07:23 +01:00
this.setMode(GyroSensorMode.Rate)
return this.getNumber(NumberFormat.Int16LE, 0)
}
}
2017-10-24 15:37:48 -07:00
//% fixedInstance whenUsed block="gyro sensor 2"
2017-10-24 19:58:52 +01:00
export const gyro2: GyroSensor = new GyroSensor(2)
//% fixedInstance whenUsed block="gyro sensor 1"
export const gyro1: GyroSensor = new GyroSensor(1)
2017-10-24 19:58:52 +01:00
2017-10-24 15:37:48 -07:00
//% fixedInstance whenUsed block="gyro sensor 3"
2017-10-24 19:58:52 +01:00
export const gyro3: GyroSensor = new GyroSensor(3)
2017-10-24 15:37:48 -07:00
//% fixedInstance whenUsed block="gyro sensor 4"
2017-10-24 19:58:52 +01:00
export const gyro4: GyroSensor = new GyroSensor(4)
2017-07-10 15:07:23 +01:00
}