Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dfa4407
First Commit
niiquaye Jan 1, 2021
25e2a46
Merge branch 'master' into orson/createURM04Driver
niiquaye Jan 1, 2021
d3ed65d
Second Commit - More Updates
niiquaye Jan 3, 2021
1e3cc25
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 3, 2021
5696acc
Third-Commit
niiquaye Jan 3, 2021
fe2c004
Fourth Commit - Younes' Requested Changes
niiquaye Jan 3, 2021
a279500
Fifth Commit - Younes' Updated Requested Changes
niiquaye Jan 3, 2021
dd18b99
Sixth Commit - Younes' Updated Request Changes Pt2.
niiquaye Jan 4, 2021
631162c
Seventh Commit - Cindy's Requested Changes
niiquaye Jan 4, 2021
5a62e19
Eigth Commit - Cindy's Requested Changes (Updated Constructor) PT2
niiquaye Jan 4, 2021
30ef990
Ninth Commit - Added RS485 TX/RX Mode with TriggerPin
niiquaye Jan 4, 2021
c4541c0
Ninth Commit PT2 - Error checking for when distance read is out of range
niiquaye Jan 4, 2021
b321611
Tenth Commit - Added Cindy's Requested Changes
niiquaye Jan 10, 2021
99c8d4d
Minor Changes to Constructor/ Serial Communication / Command Buffer -…
niiquaye Jan 12, 2021
162e05c
Minor Changes to Constructor/ Serial Communication / Command Buffer -…
niiquaye Jan 12, 2021
ad2999b
Merge branch 'master' into orson/createURM04Driver
niiquaye Jan 12, 2021
a5693c7
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 12, 2021
c3dd8b5
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 12, 2021
26ffa55
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 12, 2021
6b303be
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 12, 2021
9c9aa6b
Fixing Git Issues
niiquaye Jan 12, 2021
8caefc5
Removed Flushing the Serial buffer - Cindy's Requested Changes
niiquaye Jan 12, 2021
660e32a
Merge branch 'orson/createURM04Driver' of https://github.com/uwroboti…
niiquaye Jan 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ add_subdirectory(lib/pid)
add_subdirectory(lib/sensors)
add_subdirectory(lib/servo)


if (NOT DEFINED APP)
message(FATAL_ERROR "APP variable not set in CACHE. Please invoke CMake with \"-DAPP=<app-name>\"")
elseif (NOT EXISTS "${CMAKE_SOURCE_DIR}/apps/${APP}")
Expand Down Expand Up @@ -172,3 +173,4 @@ add_subdirectory(apps/test-moisture)
add_subdirectory(apps/test-pid)
add_subdirectory(apps/test-pwm)
add_subdirectory(apps/test-pwmin)
add_subdirectory(apps/test-urm04)
1 change: 1 addition & 0 deletions apps/test-pwm/CMakeLists.txt
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)

4 changes: 4 additions & 0 deletions apps/test-urm04/CMakeLists.txt
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)
14 changes: 14 additions & 0 deletions apps/test-urm04/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "URM04Sensor.h"
int main() {
// D2 - trigpin
// D1 - TX
// D0 - RX
URM04Sensor::URM04Sensor sensor(D2, D0, D1);
while (1) {
sensor.trigger_sensor();
sensor.read_distance();
ThisThread::sleep_for(std::chrono::milliseconds(10));
}

return 0;
}
5 changes: 5 additions & 0 deletions lib/sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ add_library(QEI STATIC)
target_sources(QEI PRIVATE src/QEI.cpp)
target_include_directories(QEI PUBLIC include)
target_set_mbed_dependency(QEI)

add_library(URM04Sensor STATIC)
target_sources(URM04Sensor PRIVATE src/URM04Sensor.cpp)
target_include_directories(URM04Sensor PUBLIC include)
target_set_mbed_dependency(URM04Sensor)
42 changes: 42 additions & 0 deletions lib/sensors/include/URM04Sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "mbed.h"

namespace URM04Sensor {

class URM04Sensor {
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;
// serial
BufferedSerial serial;
// variable to check if success_read
bool success;
// sensor distance
float m_distance;

public:
// constructor
URM04Sensor(PinName trig_pin, PinName _RX, PinName _TX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cindyli-13 what are the trig_pins on the pdb? i didnt see em in the ioc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PDB doesn't have the trig pin, it just has RX and TX signals. I think the arduino example this driver is based off of had that extra trig pin, so it's a bit different for us.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I checked over the PCB design again and we might need to do some rework to add the "trig pin". I think I made a mistake with the design and the trig signal is needed, sorry about that.


// Trigger Sensor
void trigger_sensor();

// Read Distance
float read_distance();
};

} // namespace URM04Sensor
124 changes: 124 additions & 0 deletions lib/sensors/src/URM04Sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#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) {
// 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;
* }
*
***/

memset(&cmdst[0], 0, sizeof(cmdst));
}

void URM04Sensor::URM04Sensor::trigger_sensor() {
/************************************ 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just do cmdst[5] = cmdst[0] + cmdst[1] + cmdst[2] + cmdst[3] + cmdst[4]; and remove the checkSum var. Same with the other checksum instances


/******** SEND COMMANDS OVER SERIAL *********************/
int num_bytes;
// returns the number of bytes if successful write
num_bytes = serial.write(&cmdst[0], sizeof(cmdst));

// 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) {
success = false;
}

// flush the buffer from serial
serial.sync();

// wait for at least 30 ms after calling trigger function
ThisThread::sleep_for(std::chrono::milliseconds(35));

// reset command buffer - fill whole array with zeros
memset(&cmdst[0], 0, sizeof(cmdst));
}

float URM04Sensor::URM04Sensor::read_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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace sizeof(cmdst) with 6 since you're writing 6 bytes, not 10


// 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) {
success = false;
// return -1 indicating read command has failed
return -1;
}

// flush buffer from serial
serial.sync();

// reset command buffer - fill whole array with zeros
memset(&cmdst[0], 0, sizeof(cmdst));
/******* 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));

// 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) {
success = false;
return -1;
}
/******** 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]) {
success = false;
// return -1 if checksum failed
return -1;
} else {
// get distance from sensor
m_distance = (float)(cmdst[5] << 8) + (float)cmdst[6];
// --------------------- or --------------------------
// m_distance = (float)(cmdst[5]*256) + (float)cmdst[6];
return m_distance;
}
}
}
3 changes: 3 additions & 0 deletions supported_build_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ test-pwmin:
# - gamepad
# - gimbtonomy
# - science

test-urm04:
- nucleo