pxt-ev3/libs/core/ultrasonic.ts

101 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-10-31 05:39:50 +01:00
const enum UltrasonicSensorEvent {
2017-10-27 05:51:13 +02:00
//% block="object near"
ObjectNear = 1,
//% block="object detected"
2017-10-27 05:38:17 +02:00
ObjectDetected = 2
}
2017-10-28 18:13:02 +02:00
namespace sensors {
2017-07-10 16:07:23 +02:00
2017-10-25 00:33:28 +02:00
//% fixedInstances
2017-07-10 16:07:23 +02:00
export class UltraSonicSensor extends internal.UartSensor {
2017-10-27 05:51:13 +02:00
private promixityThreshold: number;
2017-10-31 05:39:50 +01:00
private movementThreshold: number;
2017-10-27 05:38:17 +02:00
2017-10-24 20:58:52 +02:00
constructor(port: number) {
super(port)
2017-10-27 05:51:13 +02:00
this.promixityThreshold = 10;
2017-10-31 05:39:50 +01:00
this.movementThreshold = 1;
2017-07-10 16:07:23 +02:00
}
_deviceType() {
return DAL.DEVICE_TYPE_ULTRASONIC
2017-07-10 16:07:23 +02:00
}
2017-10-27 05:38:17 +02:00
_query(): number {
2017-10-31 05:39:50 +01:00
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
2017-10-27 05:38:17 +02:00
}
_update(prev: number, curr: number) {
2017-10-31 05:39:50 +01:00
// is there an object near?
if (prev >= this.promixityThreshold && curr < this.promixityThreshold)
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectNear); // TODO proper HI-LO sensor
// did something change?
if (Math.abs(prev - curr) > this.movementThreshold)
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected); // TODO debouncing
2017-10-27 05:38:17 +02:00
}
/**
* Registers code to run when the given color is close
* @param handler the code to run when detected
*/
2017-10-31 05:39:50 +01:00
//% help=input/ultrasonic/on
//% block="on %sensor|%event"
//% blockId=ultrasonicOn
2017-10-27 05:51:13 +02:00
//% parts="infraredsensor"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
2017-10-27 05:38:17 +02:00
//% weight=100 blockGap=8
//% group="Ultrasonic Sensor"
2017-10-31 05:39:50 +01:00
on(event: UltrasonicSensorEvent, handler: () => void) {
control.onEvent(this._id, event, handler);
if (event == UltrasonicSensorEvent.ObjectNear
&& this.distance() < this.promixityThreshold)
2017-10-27 05:38:17 +02:00
control.runInBackground(handler);
}
2017-10-31 05:39:50 +01:00
/**
* Waits for the event to occur
*/
//% help=input/ultrasonic/wait
//% block="wait %sensor|for %event"
//% blockId=ultrasonicWait
//% parts="infraredsensor"
//% blockNamespace=sensors
//% weight=99 blockGap=8
//% group="Ultrasonic Sensor"
wait(event: UltrasonicSensorEvent) {
// TODO
2017-10-27 05:57:18 +02:00
}
2017-10-25 00:33:28 +02:00
/**
* Gets the distance from the sonar in millimeters
2017-10-25 00:35:42 +02:00
* @param sensor the ultrasonic sensor port
2017-10-25 00:33:28 +02:00
*/
//% help=input/ultrasonic/distance
2017-10-25 00:35:42 +02:00
//% block="%sensor|distance"
2017-10-25 00:33:28 +02:00
//% blockId=sonarGetDistance
//% parts="ultrasonic"
2017-10-28 18:13:02 +02:00
//% blockNamespace=sensors
2017-10-25 00:33:28 +02:00
//% weight=65 blockGap=8
//% group="Ultrasonic Sensor"
2017-10-25 00:33:28 +02:00
distance() {
2017-10-31 05:39:50 +01:00
// it supposedly also has an inch mode, but we stick to cm
2017-07-10 16:07:23 +02:00
this._setMode(0)
2017-10-27 05:38:17 +02:00
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff;
2017-07-10 16:07:23 +02:00
}
}
//% fixedInstance whenUsed block="ultrasonic sensor 4"
export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4)
2017-10-25 00:35:42 +02:00
//% fixedInstance whenUsed block="ultrasonic sensor 1"
2017-10-24 20:58:52 +02:00
export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)
//% fixedInstance whenUsed block="ultrasonic sensor 2"
2017-10-24 20:58:52 +02:00
export const ultrasonic2: UltraSonicSensor = new UltraSonicSensor(2)
//% fixedInstance whenUsed block="ultrasonic sensor 3"
2017-10-24 20:58:52 +02:00
export const ultrasonic3: UltraSonicSensor = new UltraSonicSensor(3)
2017-07-10 16:07:23 +02:00
}