Skip to content

Commit

Permalink
Read values from VESC motor controller
Browse files Browse the repository at this point in the history
Change-Id: Icaad5bd270827f1fa1b22a77d0cd0e23dbf999e5
  • Loading branch information
oliverlee committed Jul 27, 2023
1 parent 8d86f57 commit 702107d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
5 changes: 4 additions & 1 deletion examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "vesc_cmd",
srcs = ["vesc_cmd.cpp"],
deps = ["//third_party/vesc_uart"],
deps = [
"//third_party/vesc_uart",
"@fmt",
],
)

cc_binary(
Expand Down
51 changes: 51 additions & 0 deletions examples/vesc_cmd.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
#include "third_party/vesc_uart/VescUart.h"
//#include ""

#include <cstdint>
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <chrono>
#include <thread>
#include <array>
#include <span>

auto buffer = std::array<std::uint8_t, 256>{};

template <std::size_t N>
auto read_payload(std::span<std::uint8_t, N> buf) -> void
{
static constexpr auto serial_device = 0;

static auto cmd_get_values = std::uint8_t{ COMM_GET_VALUES };
::PackSendPayload(&cmd_get_values, sizeof(cmd_get_values), serial_device);

{
const auto version = Serial.read();
assert(version == 2 and "only version 2 is handled");
}

const auto payload_size = Serial.read();
static constexpr auto footer_size = 3;

fmt::print("payload_size: {}\n", payload_size);

Serial.read(std::span{buf.data(), payload_size + footer_size});

fmt::print("got data\n");
}

auto main() -> int
{
::VescUartSetCurrent(1.0F);

while (true) {
const auto format = Serial.read();
assert(format == 2 and "format 3 is not handled");

auto payload_size = Serial.read();

constexpr auto footer_size = 3;

for (auto i = 0; i != footer_size + payload_size; ++i) {
buffer[std::size_t(i)] = Serial.read();
}

fmt::print("payload_size: {}\n", payload_size);

std::this_thread::sleep_for(std::chrono::milliseconds{100});
}

return 0;
}
6 changes: 5 additions & 1 deletion uart_abstraction/HardwareSerial.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Fake API for UART read/write.
// API for UART read/write.

#pragma once

#include <cstddef>
#include <cstdint>
#include <span>

struct HardwareSerial
{
Expand All @@ -14,6 +15,9 @@ struct HardwareSerial

auto read() const -> std::uint8_t;

template <std::size_t N>
auto read(std::span<std::uint8_t, N> buffer) const -> void;

auto write(std::uint8_t* buffer, std::size_t size) const -> void;
};

Expand Down
12 changes: 12 additions & 0 deletions uart_abstraction/rpi/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ auto HardwareSerial::read() const -> std::uint8_t
return static_cast<std::uint8_t>(c);
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
template <std::size_t N>
auto HardwareSerial::read(std::span<std::uint8_t, N> buf) const -> void
{
auto buf_ = asio::buffer(buf.data(), buf.size());

while (buf_.size() != 0) {
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
buf_ += underlying->read_some(buf_);
}
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
auto HardwareSerial::write(std::uint8_t* buffer, std::size_t size) const -> void
{
Expand Down

0 comments on commit 702107d

Please sign in to comment.