Skip to content

Commit

Permalink
basic serial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeno12 committed Oct 11, 2023
1 parent a304592 commit d7c22b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ IF(${CMAKE_BUILD_MODE} MATCHES "Omega")
ROSBUILD_ADD_EXECUTABLE(radio_driver
src/radio_driver/radio_driver_node.cpp
src/radio_driver/radio_driver.cpp
src/radio_driver/radio_interface.cpp)
src/radio_driver/radio_interface.cpp
src/vesc_driver/serial.cc)
TARGET_LINK_LIBRARIES(radio_driver ${libs})
ENDIF()

Expand Down
37 changes: 19 additions & 18 deletions src/radio_driver/radio_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

namespace radio_driver {

RadioInterface::RadioInterface() : connected_(false) {
RadioInterface::RadioInterface() : buffer_size_(0), connected_(false) {
buffer_ = new char[12]; // TODO: check if 12 is enough
}

RadioInterface::~RadioInterface() {
if (!disconnect()) {
LOG(ERROR) << "Failed to cleanly disconnect from radio on shutdown";
}

delete[] buffer_;
}
RadioInterface::~RadioInterface() { delete[] buffer_; }

bool RadioInterface::isConnected() const { return connected_; }

Expand All @@ -25,20 +19,25 @@ bool RadioInterface::sendControl(float throttle, float steering) {
}

commandToBuffer(throttle, steering);
// TODO: send buf to serial device
return true;
if (buffer_size_) {
serial_.write(buffer_, buffer_size_);
return true;
} else {
return false;
}
}

bool RadioInterface::connect(const std::string& port) {
// TODO: actually connect to serial device
connected_ = true;
return true;
bool RadioInterface::connect(const std::string& port, int baud) {
connected_ = serial_.open(port.c_str(), baud);
if (!connected_) {
LOG(ERROR) << "Failed to connect to radio on serial port " << port;
}
return connected_;
}

bool RadioInterface::disconnect() {
// TODO: actually disconnect from serial device
void RadioInterface::disconnect() {
serial_.close();
connected_ = false;
return true;
}

void RadioInterface::commandToBuffer(float throttle, float steering) {
Expand All @@ -56,7 +55,9 @@ void RadioInterface::commandToBuffer(float throttle, float steering) {
} else {
std::ostringstream ss;
ss << throttle_pwm << "," << steering_pwm << "\n";
ss.str().copy(buffer_, ss.str().size());
std::string data = ss.str();
data.copy(buffer_, data.size());
buffer_size_ = data.size();
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/radio_driver/radio_interface.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <string>

#include "vesc_driver/serial.h"

namespace radio_driver {

class RadioInterface {
Expand All @@ -15,14 +17,16 @@ class RadioInterface {
bool sendControl(float throttle, float steering);

private:
bool connect(const std::string& port);
bool disconnect();
bool connect(const std::string& port, int baud);
void disconnect();

// convert throttle and steering commands to ascii bytes in the format arduino expects
// throttle and steering are in the range [-1, 1]
void commandToBuffer(float throttle, float steering);

Serial serial_;
char* buffer_;
size_t buffer_size_;
bool connected_;
};

Expand Down

0 comments on commit d7c22b4

Please sign in to comment.