-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define example program `//examples:vesc_cmd` that sends a 1.0F current reference to a motor controller connected to `/dev/ttyS0`. This program is implicitly constrained to the Raspberry Pi via the dependencies of `//third_party/vesc_uart`. Change-Id: I8cbf7d674e1f47d2e0ebce7f795a1b5d353f918e
- Loading branch information
Showing
8 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_binary") | ||
|
||
cc_binary( | ||
name = "vesc_cmd", | ||
srcs = ["vesc_cmd.cpp"], | ||
deps = ["//third_party/vesc_uart"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "third_party/vesc_uart/VescUart.h" | ||
|
||
auto main() -> int | ||
{ | ||
::VescUartSetCurrent(1.0f); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "uart_abstraction/HardwareSerial.hpp" | ||
|
||
#include <asio.hpp> | ||
#include <cassert> | ||
#include <optional> | ||
#include <stdexcept> | ||
#include <sys/select.h> | ||
|
||
namespace { | ||
auto ctx = asio::io_context{}; | ||
auto underlying = std::optional<asio::serial_port>{}; | ||
} // namespace | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
HardwareSerial Serial{"/dev/ttyS0"}; | ||
|
||
HardwareSerial::HardwareSerial(const char* device) | ||
{ | ||
underlying.emplace(ctx, device); | ||
|
||
using opt = asio::serial_port_base; | ||
|
||
underlying->set_option(opt::baud_rate{115200}); | ||
underlying->set_option(opt::character_size{8}); | ||
underlying->set_option(opt::parity{opt::parity::none}); | ||
underlying->set_option(opt::stop_bits{opt::stop_bits::one}); | ||
|
||
assert(underlying->is_open() and "underlying device not open"); | ||
} | ||
|
||
auto HardwareSerial::available() const -> bool | ||
{ | ||
auto native = underlying->native_handle(); | ||
|
||
::fd_set rfds; | ||
auto tv = ::timeval{{}, {}}; | ||
|
||
FD_ZERO(&rfds); | ||
FD_SET(native, &rfds); | ||
|
||
const auto retval = ::select(1, &rfds, NULL, NULL, &tv); | ||
assert(retval >= 0 and "error in checking serial availability"); | ||
|
||
return retval > 0; | ||
} | ||
|
||
auto HardwareSerial::read() const -> std::uint8_t | ||
{ | ||
char c; | ||
|
||
for (auto n = std::size_t{}; n != 1;) { | ||
n = underlying->read_some(asio::buffer(&c, 1)); | ||
assert(n <= 1 and "read more than 1 byte"); | ||
} | ||
|
||
return static_cast<std::uint8_t>(c); | ||
} | ||
|
||
auto HardwareSerial::write(std::uint8_t* buffer, std::size_t size) const -> void | ||
{ | ||
for (auto buf = asio::buffer(buffer, size); buf.size() != 0;) { | ||
buf += underlying->write_some(buf); | ||
} | ||
} |