2017-11-30 09:38:04 -08:00
|
|
|
enum UltrasonicSensorEvent {
|
2017-10-26 20:51:13 -07:00
|
|
|
//% block="object detected"
|
2017-11-30 09:38:04 -08:00
|
|
|
ObjectDetected = 10,
|
|
|
|
//% block="object near"
|
2018-04-05 17:07:14 -07:00
|
|
|
ObjectNear = sensors.ThresholdState.Low
|
2017-10-26 20:38:17 -07:00
|
|
|
}
|
|
|
|
|
2017-10-28 09:13:02 -07:00
|
|
|
namespace sensors {
|
2017-07-10 15:07:23 +01:00
|
|
|
|
2017-10-24 15:33:28 -07:00
|
|
|
//% fixedInstances
|
2017-07-10 15:07:23 +01:00
|
|
|
export class UltraSonicSensor extends internal.UartSensor {
|
2018-01-19 13:11:11 -08:00
|
|
|
private promixityThreshold: sensors.ThresholdDetector;
|
2017-10-30 21:39:50 -07:00
|
|
|
private movementThreshold: number;
|
2017-10-26 20:38:17 -07:00
|
|
|
|
2017-10-24 19:58:52 +01:00
|
|
|
constructor(port: number) {
|
|
|
|
super(port)
|
2018-01-19 13:11:11 -08:00
|
|
|
this.promixityThreshold = new sensors.ThresholdDetector(this.id(), 0, 255, 10, 100); // range is 0..255cm
|
2017-10-30 21:39:50 -07:00
|
|
|
this.movementThreshold = 1;
|
2018-03-28 08:50:14 -07:00
|
|
|
this._setMode(0);
|
2017-07-10 15:07:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
return DAL.DEVICE_TYPE_ULTRASONIC
|
2017-07-10 15:07:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-26 20:38:17 -07:00
|
|
|
_query(): number {
|
2017-12-19 14:26:57 -08:00
|
|
|
return ((this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) / 10) >> 0; // range is 0..2550, in 0.1 cm increments.
|
2017-10-26 20:38:17 -07:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:50:14 -07:00
|
|
|
_info(): string {
|
|
|
|
return `${this.distance()}cm`
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:38:17 -07:00
|
|
|
_update(prev: number, curr: number) {
|
2017-10-30 21:39:50 -07:00
|
|
|
// is there an object near?
|
2017-11-30 09:38:04 -08:00
|
|
|
this.promixityThreshold.setLevel(curr);
|
2017-10-30 21:39:50 -07:00
|
|
|
|
|
|
|
// did something change?
|
|
|
|
if (Math.abs(prev - curr) > this.movementThreshold)
|
2017-11-30 09:38:04 -08:00
|
|
|
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected);
|
2017-10-26 20:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-05 17:07:14 -07:00
|
|
|
* Registers code to run when an object is close or detected
|
2017-10-26 20:38:17 -07:00
|
|
|
* @param handler the code to run when detected
|
|
|
|
*/
|
2018-02-11 09:01:46 -08:00
|
|
|
//% help=sensors/ultrasonic/on-event
|
2017-10-30 21:39:50 -07:00
|
|
|
//% blockId=ultrasonicOn
|
2018-02-26 11:33:50 -05:00
|
|
|
//% block="on **ultrasonic** %this|%event"
|
2017-11-16 12:58:37 -08:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-28 09:13:02 -07:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 11:33:50 -05:00
|
|
|
//% this.fieldEditor="ports"
|
2017-10-26 20:38:17 -07:00
|
|
|
//% weight=100 blockGap=8
|
|
|
|
//% group="Ultrasonic Sensor"
|
2017-11-30 09:38:04 -08:00
|
|
|
onEvent(event: UltrasonicSensorEvent, handler: () => void) {
|
2017-10-30 21:39:50 -07:00
|
|
|
control.onEvent(this._id, event, handler);
|
2017-10-26 20:38:17 -07:00
|
|
|
}
|
|
|
|
|
2017-10-30 21:39:50 -07:00
|
|
|
/**
|
|
|
|
* Waits for the event to occur
|
|
|
|
*/
|
2018-02-11 09:01:46 -08:00
|
|
|
//% help=sensors/ultrasonic/pause-until
|
2018-02-26 11:33:50 -05:00
|
|
|
//% block="pause until **ultrasonic** %this| %event"
|
2017-10-30 21:39:50 -07:00
|
|
|
//% blockId=ultrasonicWait
|
2017-11-16 12:58:37 -08:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-30 21:39:50 -07:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 11:33:50 -05:00
|
|
|
//% this.fieldEditor="ports"
|
2017-10-30 21:39:50 -07:00
|
|
|
//% weight=99 blockGap=8
|
2018-02-26 11:33:50 -05:00
|
|
|
//% group="Ultrasonic Sensor"
|
2017-12-12 10:46:56 -08:00
|
|
|
pauseUntil(event: UltrasonicSensorEvent) {
|
2017-11-30 09:38:04 -08:00
|
|
|
control.waitForEvent(this._id, event);
|
2017-10-26 20:57:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-24 15:33:28 -07:00
|
|
|
/**
|
2017-11-30 09:41:34 -08:00
|
|
|
* Gets the distance from the sonar in centimeters
|
2017-10-24 15:35:42 -07:00
|
|
|
* @param sensor the ultrasonic sensor port
|
2017-10-24 15:33:28 -07:00
|
|
|
*/
|
2018-02-11 09:01:46 -08:00
|
|
|
//% help=sensors/ultrasonic/distance
|
2018-02-26 11:33:50 -05:00
|
|
|
//% block="**ultrasonic** %this|distance"
|
2017-10-24 15:33:28 -07:00
|
|
|
//% blockId=sonarGetDistance
|
2017-11-16 12:58:37 -08:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-28 09:13:02 -07:00
|
|
|
//% blockNamespace=sensors
|
2018-02-26 11:33:50 -05:00
|
|
|
//% this.fieldEditor="ports"
|
2018-01-03 14:00:08 -08:00
|
|
|
//% weight=65
|
2018-02-26 11:33:50 -05:00
|
|
|
//% group="Ultrasonic Sensor"
|
2017-11-30 09:38:04 -08:00
|
|
|
distance(): number {
|
2019-09-17 14:30:02 -07:00
|
|
|
this.poke();
|
2017-10-30 21:39:50 -07:00
|
|
|
// it supposedly also has an inch mode, but we stick to cm
|
2017-07-10 15:07:23 +01:00
|
|
|
this._setMode(0)
|
2017-12-19 14:26:57 -08:00
|
|
|
return this._query();
|
2017-07-10 15:07:23 +01:00
|
|
|
}
|
2018-01-09 12:46:48 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a threshold value
|
2018-04-05 17:07:14 -07:00
|
|
|
* @param condition the near or detected condition
|
2018-01-09 12:46:48 -08:00
|
|
|
* @param value the value threshold
|
|
|
|
*/
|
2018-02-26 11:33:50 -05:00
|
|
|
//% blockId=ultrasonicSetThreshold block="set **ultrasonic** %this|%condition|to %value"
|
2018-04-12 14:25:44 -07:00
|
|
|
//% group="Calibration" blockGap=8 weight=80
|
2018-01-10 11:45:08 -08:00
|
|
|
//% value.min=0 value.max=255
|
2018-02-26 11:33:50 -05:00
|
|
|
//% this.fieldEditor="ports"
|
2018-01-30 08:27:23 -08:00
|
|
|
setThreshold(condition: UltrasonicSensorEvent, value: number) {
|
|
|
|
switch (condition) {
|
2018-01-09 12:46:48 -08:00
|
|
|
case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.setLowThreshold(value); break;
|
|
|
|
case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold = value; break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 08:27:23 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the threshold value
|
|
|
|
* @param condition the proximity condition
|
|
|
|
*/
|
2018-02-26 11:33:50 -05:00
|
|
|
//% blockId=ultrasonicGetThreshold block="**ultrasonic** %this|%condition"
|
2018-04-12 14:25:44 -07:00
|
|
|
//% group="Calibration" weight=79
|
2018-02-26 11:33:50 -05:00
|
|
|
//% this.fieldEditor="ports"
|
2018-01-30 08:27:23 -08:00
|
|
|
threshold(condition: UltrasonicSensorEvent): number {
|
|
|
|
switch (condition) {
|
2018-04-11 09:58:34 -06:00
|
|
|
case UltrasonicSensorEvent.ObjectNear: return this.promixityThreshold.threshold(ThresholdState.Low);
|
|
|
|
case UltrasonicSensorEvent.ObjectDetected: return this.movementThreshold;
|
2018-01-30 08:27:23 -08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-10 15:07:23 +01:00
|
|
|
}
|
2018-01-30 08:27:23 -08:00
|
|
|
|
2018-02-26 11:33:50 -05:00
|
|
|
//% fixedInstance whenUsed block="4" jres=icons.port4
|
2018-01-05 21:29:52 -08:00
|
|
|
export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4)
|
2018-01-30 08:27:23 -08:00
|
|
|
|
2018-02-26 11:33:50 -05:00
|
|
|
//% fixedInstance whenUsed block="1" jres=icons.port1
|
2017-10-24 19:58:52 +01:00
|
|
|
export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)
|
|
|
|
|
2018-02-26 11:33:50 -05:00
|
|
|
//% fixedInstance whenUsed block="2" jres=icons.port2
|
2017-10-24 19:58:52 +01:00
|
|
|
export const ultrasonic2: UltraSonicSensor = new UltraSonicSensor(2)
|
|
|
|
|
2018-02-26 11:33:50 -05:00
|
|
|
//% fixedInstance whenUsed block="3" jres=icons.port3
|
2017-10-24 19:58:52 +01:00
|
|
|
export const ultrasonic3: UltraSonicSensor = new UltraSonicSensor(3)
|
2017-07-10 15:07:23 +01:00
|
|
|
}
|