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 edae4d3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 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
52 changes: 51 additions & 1 deletion examples/vesc_cmd.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
#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>{};

auto read_payload(std::span<std::uint8_t> 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);

const auto message = std::span{buf.data(), std::size_t(payload_size + footer_size)};
Serial.read(message);

const auto front = std::span{buf.data(),
//2 + 2 + 4 + 4 + 4 + 4 + 2 + 4
30
};
fmt::print("{::X}\n", front);
// https://github.com/vedderb/bldc/blob/master/comm/commands.c#L379
}

auto main() -> int
{
::VescUartSetCurrent(1.0F);
const auto current = 1.0F;
//const auto data = std::span{reinterpret_cast<const std::uint8_t*>(&current), sizeof(current)};

while (true) {
//::VescUartSetCurrent(current += 1.0F);
::VescUartSetCurrent(current);
fmt::print("current: {}\n", std::int16_t(current));

read_payload({buffer.data(), buffer.size()});

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

return 0;
}
5 changes: 4 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,8 @@ struct HardwareSerial

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

auto read(std::span<std::uint8_t> buffer) const -> void;

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

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

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
auto HardwareSerial::read(std::span<std::uint8_t> 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 edae4d3

Please sign in to comment.