From 9ffb59200f4aab9c876d0f748cf0b28dc7d99ac6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 20 Jan 2024 15:57:14 +0000 Subject: [PATCH 1/2] Fix ultrasound units It actually returns a voltage, wich needs mapping to a distance --- simulator/simulated_robot.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/simulator/simulated_robot.md b/simulator/simulated_robot.md index b0a16e99..39695390 100644 --- a/simulator/simulated_robot.md +++ b/simulator/simulated_robot.md @@ -86,10 +86,17 @@ Analogous to [ultrasound sensors](https://robocraze.com/blogs/post/what-is-ultra | A4 | Front | `INPUT` | | A5 | Back | `INPUT` | -These are shown as blue boards with silver transceivers on the robot. They can see in a narrow cone up to a maximum of about 5m away. +These are shown as blue boards with silver transceivers on the robot. They can see in a narrow cone up to a maximum of about 2m away. Since these sensors rely on echoes being reflected back from objects, if the angle of incidence between the sensor's pulse and the contacted surface exceeds 22.5 degrees then the sensor will be unable to detect the object. -You can access the ultrasound sensors using `robot.arduino.pins[AX]`, where '`AX`' is between `A0` and `A5`. Make sure you set the pin mode to `INPUT`. The `analog_read` method will return the distance in metres. You can read more in the [arduino programming docs page][arduino-programming]. +You can access the ultrasound sensors using `robot.arduino.pins[AX]`, where '`AX`' is between `A0` and `A5`. Make sure you set the pin mode to `INPUT`. The `analog_read` method will return a voltage (0-5V) proportional to the distance. You can read more in the [arduino programming docs page][arduino-programming]. + +```python +reading = R.arduino.pins[A5].analog_read() +# convert reading from volts to meters +measurement = reading / 2.5 +print(f"Rear ultrasound distance {measurement:.2f} meters") +``` ### LEDs From ec162bc92018bcd51a2e7f7b0dad90b395aaf476 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 22 Jan 2024 17:23:37 +0000 Subject: [PATCH 2/2] Expand calculation to avoid magic numbers --- simulator/simulated_robot.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulator/simulated_robot.md b/simulator/simulated_robot.md index 39695390..0b1a57ca 100644 --- a/simulator/simulated_robot.md +++ b/simulator/simulated_robot.md @@ -94,7 +94,7 @@ You can access the ultrasound sensors using `robot.arduino.pins[AX]`, where '`AX ```python reading = R.arduino.pins[A5].analog_read() # convert reading from volts to meters -measurement = reading / 2.5 +measurement = reading / (5/2) print(f"Rear ultrasound distance {measurement:.2f} meters") ```