-
Notifications
You must be signed in to change notification settings - Fork 59
Closes #195: Create A Driver for URM04 Sensor #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
dfa4407
25e2a46
d3ed65d
1e3cc25
5696acc
fe2c004
a279500
dd18b99
631162c
5a62e19
30ef990
c4541c0
b321611
99c8d4d
162e05c
ad2999b
a5693c7
c3dd8b5
26ffa55
6b303be
9c9aa6b
8caefc5
660e32a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| add_executable(test-pwm.${TARGET}-board.elf) | ||
| target_sources(test-pwm.${TARGET}-board.elf PRIVATE src/main.cpp) | ||
| target_set_firmware_properties(test-pwm.${TARGET}-board.elf) | ||
|
|
||
niiquaye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| add_executable(test-urm04.${TARGET}-board.elf) | ||
| target_sources(test-urm04.${TARGET}-board.elf PRIVATE src/main.cpp) | ||
| target_link_libraries(test-urm04.${TARGET}-board.elf PRIVATE URM04Sensor uwrt-mars-rover-hw-bridge) | ||
| target_set_firmware_properties(test-urm04.${TARGET}-board.elf) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #include "URM04Sensor.h" | ||
| int main() { | ||
| // D2 - trigpin | ||
| // D1 - TX | ||
| // D0 - RX | ||
| URM04Sensor::URM04Sensor sensor(D2, D0, D1); | ||
| float get_distance; | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while (1) { | ||
| sensor.read_distance(get_distance); | ||
| // wait 10 milliseconds | ||
| ThisThread::sleep_for(10ms); | ||
| printf("Distance: %f cm\n", get_distance); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include "mbed.h" | ||
|
|
||
| namespace URM04Sensor { | ||
|
|
||
| class URM04Sensor { | ||
niiquaye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| protected: | ||
| // constants | ||
| static constexpr int BAUD_RATE = 19200; | ||
| static constexpr int START_ADDRESS = 0x11; | ||
| static constexpr int LOW = 0; | ||
|
|
||
| private: | ||
| // trigger pin | ||
| DigitalOut m_trigPin; | ||
| // start address | ||
| uint8_t startAddr; | ||
| // command buffer | ||
| uint8_t cmdst[10]; | ||
| // UART protocol pins | ||
| PinName RX; | ||
| PinName TX; | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // serial | ||
| BufferedSerial serial; | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Trigger Sensor | ||
| void trigger_sensor(float& distance); | ||
|
|
||
| public: | ||
| // constructor | ||
| URM04Sensor(PinName trig_pin, PinName _RX, PinName _TX); | ||
|
||
|
|
||
| // Read Distance in CENTIMETER returns true if successful read | ||
| // pass by reference a variable to hold the distance | ||
| bool read_distance(float& distance); | ||
| }; | ||
|
|
||
| } // namespace URM04Sensor | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #include "URM04Sensor.h" | ||
|
|
||
| // instantiate pin connected to the URM04 URM04Sensor | ||
| URM04Sensor::URM04Sensor::URM04Sensor(PinName trig_pin, PinName _RX, PinName _TX) | ||
| : m_trigPin(trig_pin), startAddr(START_ADDRESS), RX(_RX), TX(_TX), serial(TX, RX, BAUD_RATE) { | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // write low to pin to start instructions | ||
| m_trigPin.write(LOW); | ||
|
|
||
| // instantiate command buffer with all zeros | ||
|
|
||
| /*** | ||
| * for(int i{0}; i<10; i++){ | ||
| * cmdst[i] = 0; | ||
| * } | ||
| * | ||
| ***/ | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| memset(&cmdst[0], 0, sizeof(cmdst)); | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| void URM04Sensor::URM04Sensor::trigger_sensor(float& distance) { | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /************ INSTANTIATE COMMANDS TO BE SENT OVER SERIAL*********/ | ||
|
|
||
| // check sum represents the final bit in command buffer - made by adding all previous bits in command buffer | ||
| uint8_t checkSum; | ||
| // buffer header | ||
| cmdst[0] = 0x055; | ||
| cmdst[1] = 0xAA; | ||
| // device address | ||
| cmdst[2] = startAddr; | ||
| // length | ||
| cmdst[3] = 0x00; | ||
| // the command itself | ||
| cmdst[4] = 0x01; | ||
| // checksum bit | ||
| checkSum = cmdst[0] + cmdst[1] + cmdst[2] + cmdst[3] + cmdst[4]; | ||
| // instantiate the last element in the command buffer with checksum value | ||
| cmdst[5] = checkSum; | ||
|
||
|
|
||
| /******** SEND COMMANDS OVER SERIAL *********************/ | ||
| int num_bytes; | ||
| // returns the number of bytes if successful write | ||
| num_bytes = serial.write(&cmdst[0], sizeof(cmdst)); | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // else if numb_btyes < 0 serial failed or if number of bytes written != 6 (because 6 bytes sent over serial) | ||
| if (num_bytes < 0 || num_bytes != 6) { | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // error occured in trigger step | ||
| distance = -1; | ||
| } else { | ||
| // no error occured in trigger step | ||
| distance = 0; | ||
| } | ||
|
||
|
|
||
| // flush the buffer from serial | ||
| serial.sync(); | ||
|
|
||
| // wait for at least 30 ms after calling trigger function | ||
| ThisThread::sleep_for(35ms); | ||
|
|
||
| // reset command buffer - fill whole array with zeros | ||
| memset(&cmdst[0], 0, sizeof(cmdst)); | ||
| } | ||
|
|
||
| // pass by reference a variable to hold the distance value | ||
| bool URM04Sensor::URM04Sensor::read_distance(float& distance) { | ||
| /****************** TRIGGER SENSOR BEFORE READING DISTANCE ********************/ | ||
| trigger_sensor(distance); | ||
|
|
||
| /************ INSTANTIATE COMMANDS TO BE SENT OVER SERIAL************/ | ||
| // check sum represents the final bit in command buffer - made by adding all previous bits in command buffer | ||
| uint8_t checkSum; | ||
| // buffer header | ||
| cmdst[0] = 0x55; | ||
| cmdst[1] = 0xAA; | ||
| // device address | ||
| cmdst[2] = startAddr; | ||
| // command length | ||
| cmdst[3] = 0x00; | ||
| // the command itself | ||
| cmdst[4] = 0x02; | ||
| // checksum bit | ||
| checkSum = cmdst[0] + cmdst[1] + cmdst[2] + cmdst[3] + cmdst[4]; | ||
| // instantiate the last element in the command buffer with checksum value | ||
| cmdst[5] = checkSum; | ||
|
|
||
| /************** SEND COMMANDS OVER SERIAL***********/ | ||
| int w_num_bytes; | ||
| // returns the number of bytes if successful read | ||
| w_num_bytes = serial.write(&cmdst[0], sizeof(cmdst)); | ||
|
||
|
|
||
| // else if numb_btyes < 0 serial failed or if number of bytes read != 6 (because 6 bytes sent over serial) | ||
| if (w_num_bytes < 0 || w_num_bytes != 6) { | ||
| // return false indicating read command has failed | ||
| distance = -1; | ||
| return false; | ||
| } | ||
|
|
||
| // flush buffer from serial | ||
| serial.sync(); | ||
|
|
||
| // reset command buffer - fill whole array with zeros | ||
| memset(&cmdst[0], 0, sizeof(cmdst)); | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /******* READ RETURN VALUE FROM SERIAL**************/ | ||
| int r_num_bytes; | ||
| // returns the number of bytes if successful read | ||
| r_num_bytes = serial.read(&cmdst[0], sizeof(cmdst)); | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // else if numb_btyes < 0 serial failed or if number of bytes read != 8 (because 8 bytes sent over serial) | ||
| if (r_num_bytes < 0 || r_num_bytes != 8) { | ||
| distance = -1; | ||
| return false; | ||
| } | ||
| /******** PARSE THROUGH THE DATA READ FROM SERIAL*********/ | ||
| else { | ||
| // create the checksum | ||
| checkSum = cmdst[0] + cmdst[1] + cmdst[2] + cmdst[3] + cmdst[4] + cmdst[5] + cmdst[6]; | ||
|
|
||
| // check if the checksum is incorrect | ||
| if (checkSum != cmdst[7]) { | ||
niiquaye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // return false if checksum failed | ||
| distance = -1; | ||
| return false; | ||
| } else { | ||
| // get distance from sensor | ||
| distance = (float)(cmdst[5] << 8) + (float)cmdst[6]; | ||
| // --------------------- or -------------------------- | ||
| // distance = (float)(cmdst[5]*256) + (float)cmdst[6]; | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,3 +74,6 @@ test-pwmin: | |
| # - gamepad | ||
| # - gimbtonomy | ||
| # - science | ||
|
|
||
| test-urm04: | ||
| - nucleo | ||
Uh oh!
There was an error while loading. Please reload this page.