From 0529759a809f3de8f21632562d53f219ec4d00ac Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Mon, 18 Dec 2017 20:30:56 -0800 Subject: [PATCH] Fix ultrasonic value to use cm instead of 0.1 cm units (#110) * Fix simulator * use 250 instead of 255 --- libs/ultrasonic-sensor/ultrasonic.ts | 4 ++-- sim/state/analog.ts | 2 +- sim/state/uart.ts | 2 +- sim/state/ultrasonic.ts | 4 ++-- sim/state/util.ts | 4 ++-- sim/visuals/controls/distanceSlider.ts | 14 +++++++++++--- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index e7cda4b6..2b2e78c5 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -25,7 +25,7 @@ namespace sensors { } _query(): number { - return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; + return (this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) * 0.1; // range is 0..2550, in 0.1 cm increments. } _update(prev: number, curr: number) { @@ -86,7 +86,7 @@ namespace sensors { distance(): number { // it supposedly also has an inch mode, but we stick to cm this._setMode(0) - return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff; // range is 0..255 + return (this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) * 0.1; // range is 0..2550, in 0.1 cm increments. } } diff --git a/sim/state/analog.ts b/sim/state/analog.ts index 12a48029..f39cb865 100644 --- a/sim/state/analog.ts +++ b/sim/state/analog.ts @@ -38,7 +38,7 @@ namespace pxsim { data[AnalogOff.InConn + port] = node.isUart() ? DAL.CONN_INPUT_UART : DAL.CONN_INPUT_DUMB; if (node.isAnalog() && node.hasData()) { //data[AnalogOff.InPin6 + 2 * port] = node.getValue(); - util.map16Bit(data, AnalogOff.InPin6 + 2 * port, node.getValue()) + util.map16Bit(data, AnalogOff.InPin6 + 2 * port, Math.floor(node.getValue())); } } } diff --git a/sim/state/uart.ts b/sim/state/uart.ts index eaf8fb92..eeabdb9d 100644 --- a/sim/state/uart.ts +++ b/sim/state/uart.ts @@ -91,7 +91,7 @@ namespace pxsim { if (node) { // Actual const index = 0; //UartOff.Actual + port * 2; - data[UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index] = node.getValue(); + util.map16Bit(data, UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index, Math.floor(node.getValue())) // Status data[UartOff.Status + port] = node.valueChange() ? UartStatus.UART_PORT_CHANGED : UartStatus.UART_DATA_READY; } diff --git a/sim/state/ultrasonic.ts b/sim/state/ultrasonic.ts index dcee3213..714a1712 100644 --- a/sim/state/ultrasonic.ts +++ b/sim/state/ultrasonic.ts @@ -4,7 +4,7 @@ namespace pxsim { export class UltrasonicSensorNode extends UartSensorNode { id = NodeType.UltrasonicSensor; - private distance: number = 50; + private distance: number = 127; // in cm constructor(port: number) { super(port); @@ -25,7 +25,7 @@ namespace pxsim { } getValue() { - return this.distance; + return this.distance * 10; // convert to 0.1 cm } } } \ No newline at end of file diff --git a/sim/state/util.ts b/sim/state/util.ts index 31ce4feb..083ce856 100644 --- a/sim/state/util.ts +++ b/sim/state/util.ts @@ -1,7 +1,7 @@ namespace pxsim.util { export function map16Bit(buffer: Uint8Array, index: number, value: number) { - buffer[index] = (value >> 8) & 0xff; - buffer[index+1] = value & 0xff; + buffer[index] = value & 0xFF; + buffer[index + 1] = (value >> 8) & 0xFF; } } \ No newline at end of file diff --git a/sim/visuals/controls/distanceSlider.ts b/sim/visuals/controls/distanceSlider.ts index 5535fb0c..466cd24e 100644 --- a/sim/visuals/controls/distanceSlider.ts +++ b/sim/visuals/controls/distanceSlider.ts @@ -83,8 +83,8 @@ namespace pxsim.visuals { return; } const node = this.state; - const percentage = node.getValue(); - const y = this.getContentHeight() * percentage / 100; + const percentage = node.getValue() / 10; /* convert back to cm */ + const y = this.getContentHeight() * percentage / this.getMax(); this.slider.setAttribute("transform", `translate(0, ${y - DistanceSliderControl.SLIDER_HANDLE_HEIGHT / 2})`); } @@ -104,7 +104,15 @@ namespace pxsim.visuals { let t = Math.max(0, Math.min(1, (this.getTopPadding() + height + this.top / this.scaleFactor - cur.y / this.scaleFactor) / height)) const state = this.state; - state.setDistance((1 - t) * (100)); + state.setDistance((1 - t) * (this.getMax())); + } + + private getMin() { + return 0; + } + + private getMax() { + return 250; //cm } private getGradientDefinition(): LinearGradientDefinition {