2017-11-30 18:38:04 +01:00
|
|
|
enum UltrasonicSensorEvent {
|
2017-10-27 05:51:13 +02:00
|
|
|
//% block="object detected"
|
2017-11-30 18:38:04 +01:00
|
|
|
ObjectDetected = 10,
|
|
|
|
//% block="object near"
|
2018-01-19 22:11:11 +01:00
|
|
|
ObjectNear = sensors.ThresholdState.Low,
|
2017-11-30 18:38:04 +01:00
|
|
|
//% block="object far"
|
2018-01-19 22:11:11 +01:00
|
|
|
ObjectFar = sensors.ThresholdState.High
|
2017-10-27 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-01-19 22:11:11 +01:00
|
|
|
private promixityThreshold: sensors.ThresholdDetector;
|
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)
|
2018-01-19 22:11:11 +01:00
|
|
|
this.promixityThreshold = new sensors.ThresholdDetector(this.id(), 0, 255, 10, 100); // range is 0..255cm
|
2017-10-31 05:39:50 +01:00
|
|
|
this.movementThreshold = 1;
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_deviceType() {
|
2017-07-11 16:18:59 +02:00
|
|
|
return DAL.DEVICE_TYPE_ULTRASONIC
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 05:38:17 +02:00
|
|
|
_query(): number {
|
2017-12-19 23:26:57 +01:00
|
|
|
return ((this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) / 10) >> 0; // range is 0..2550, in 0.1 cm increments.
|
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?
|
2017-11-30 18:38:04 +01:00
|
|
|
this.promixityThreshold.setLevel(curr);
|
2017-10-31 05:39:50 +01:00
|
|
|
|
|
|
|
// did something change?
|
|
|
|
if (Math.abs(prev - curr) > this.movementThreshold)
|
2017-11-30 18:38:04 +01:00
|
|
|
control.raiseEvent(this._id, UltrasonicSensorEvent.ObjectDetected);
|
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
|
|
|
|
*/
|
2018-02-11 18:01:46 +01:00
|
|
|
//% help=sensors/ultrasonic/on-event
|
2017-10-31 05:39:50 +01:00
|
|
|
//% blockId=ultrasonicOn
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="on %sensor|%event"
|
2017-11-16 21:58:37 +01:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2017-12-25 02:46:58 +01:00
|
|
|
//% sensor.fieldEditor="ports"
|
2017-10-27 05:38:17 +02:00
|
|
|
//% weight=100 blockGap=8
|
|
|
|
//% group="Ultrasonic Sensor"
|
2017-11-30 18:38:04 +01:00
|
|
|
onEvent(event: UltrasonicSensorEvent, handler: () => void) {
|
2017-10-31 05:39:50 +01:00
|
|
|
control.onEvent(this._id, event, handler);
|
2017-10-27 05:38:17 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 05:39:50 +01:00
|
|
|
/**
|
|
|
|
* Waits for the event to occur
|
|
|
|
*/
|
2018-02-11 18:01:46 +01:00
|
|
|
//% help=sensors/ultrasonic/pause-until
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="pause until %sensor| %event"
|
2017-10-31 05:39:50 +01:00
|
|
|
//% blockId=ultrasonicWait
|
2017-11-16 21:58:37 +01:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-31 05:39:50 +01:00
|
|
|
//% blockNamespace=sensors
|
2017-12-25 02:46:58 +01:00
|
|
|
//% sensor.fieldEditor="ports"
|
2017-10-31 05:39:50 +01:00
|
|
|
//% weight=99 blockGap=8
|
|
|
|
//% group="Ultrasonic Sensor"
|
2017-12-12 19:46:56 +01:00
|
|
|
pauseUntil(event: UltrasonicSensorEvent) {
|
2017-11-30 18:38:04 +01:00
|
|
|
control.waitForEvent(this._id, event);
|
2017-10-27 05:57:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 00:33:28 +02:00
|
|
|
/**
|
2017-11-30 18:41:34 +01:00
|
|
|
* Gets the distance from the sonar in centimeters
|
2017-10-25 00:35:42 +02:00
|
|
|
* @param sensor the ultrasonic sensor port
|
2017-10-25 00:33:28 +02:00
|
|
|
*/
|
2018-02-11 18:01:46 +01:00
|
|
|
//% help=sensors/ultrasonic/distance
|
2017-12-19 20:37:33 +01:00
|
|
|
//% block="%sensor|distance"
|
2017-10-25 00:33:28 +02:00
|
|
|
//% blockId=sonarGetDistance
|
2017-11-16 21:58:37 +01:00
|
|
|
//% parts="ultrasonicsensor"
|
2017-10-28 18:13:02 +02:00
|
|
|
//% blockNamespace=sensors
|
2017-12-25 02:46:58 +01:00
|
|
|
//% sensor.fieldEditor="ports"
|
2018-01-03 23:00:08 +01:00
|
|
|
//% weight=65
|
2017-10-25 01:52:13 +02:00
|
|
|
//% group="Ultrasonic Sensor"
|
2017-11-30 18:38:04 +01:00
|
|
|
distance(): number {
|
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-12-19 23:26:57 +01:00
|
|
|
return this._query();
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
2018-01-09 21:46:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a threshold value
|
|
|
|
* @param condition the dark or bright light condition
|
|
|
|
* @param value the value threshold
|
|
|
|
*/
|
2018-01-10 20:45:08 +01:00
|
|
|
//% blockId=ultrasonicSetThreshold block="set %sensor|%condition|to %value"
|
2018-01-30 17:27:23 +01:00
|
|
|
//% group="Threshold" blockGap=8 weight=80
|
2018-01-10 20:45:08 +01:00
|
|
|
//% value.min=0 value.max=255
|
2018-01-30 17:27:23 +01:00
|
|
|
setThreshold(condition: UltrasonicSensorEvent, value: number) {
|
|
|
|
switch (condition) {
|
2018-01-09 21:46:48 +01:00
|
|
|
case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.setLowThreshold(value); break;
|
|
|
|
case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.setHighThreshold(value); break;
|
|
|
|
case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold = value; break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 17:27:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the threshold value
|
|
|
|
* @param condition the proximity condition
|
|
|
|
*/
|
|
|
|
//% blockId=ultrasonicGetThreshold block="%sensor|%condition"
|
|
|
|
//% group="Threshold" blockGap=8 weight=79
|
|
|
|
//% sensor.fieldEditor="ports"
|
|
|
|
threshold(condition: UltrasonicSensorEvent): number {
|
|
|
|
switch (condition) {
|
|
|
|
case UltrasonicSensorEvent.ObjectNear: this.promixityThreshold.threshold(ThresholdState.Low); break;
|
|
|
|
case UltrasonicSensorEvent.ObjectFar: this.promixityThreshold.threshold(ThresholdState.Low); break;
|
|
|
|
case UltrasonicSensorEvent.ObjectDetected: this.movementThreshold; break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|
2018-01-30 17:27:23 +01:00
|
|
|
|
2018-01-06 06:29:52 +01:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic 4" jres=icons.port4
|
|
|
|
export const ultrasonic4: UltraSonicSensor = new UltraSonicSensor(4)
|
2018-01-30 17:27:23 +01:00
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic 1" jres=icons.port1
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic1: UltraSonicSensor = new UltraSonicSensor(1)
|
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic 2" jres=icons.port2
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic2: UltraSonicSensor = new UltraSonicSensor(2)
|
|
|
|
|
2017-12-19 20:37:33 +01:00
|
|
|
//% fixedInstance whenUsed block="ultrasonic 3" jres=icons.port3
|
2017-10-24 20:58:52 +02:00
|
|
|
export const ultrasonic3: UltraSonicSensor = new UltraSonicSensor(3)
|
2017-07-10 16:07:23 +02:00
|
|
|
}
|