Skip to content

Commit

Permalink
Add library for communication with the motor controller (#8)
Browse files Browse the repository at this point in the history
This commit also adds a "fake" implementation of the required UART
abstraction needed by the third party library. That is, an
implementation that has the correct API for building with, but that
won't be useful at runtime.

Additionally, this commit adds support for making `clang-format` ignore
specified directories, and then proceeds to make it ignore the
third-party vesc-uart library.

This commit also updates `clang-tidy` so that system include are
correctly ordered when using `--spawn_strategy=local`.

Change-Id: I350bf62912454d7b815a9dedcc048a63c2107c46
  • Loading branch information
andebjor authored Jul 22, 2023
1 parent e50ab07 commit cf9f724
Show file tree
Hide file tree
Showing 20 changed files with 1,479 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ build:gcc12 --extra_toolchains=//toolchain:gcc12
build:clang-format --aspects @bazel_clang_format//:defs.bzl%clang_format_aspect
build:clang-format --@bazel_clang_format//:binary=@llvm_16_toolchain//:clang-format
build:clang-format --@bazel_clang_format//:config=//:format_config
build:clang-format --@bazel_clang_format//:ignore=//:format_ignore
build:clang-format --output_groups=report
build:clang-format --keep_going

build:clang-tidy-base --config=clang16
build:clang-tidy-base --aspects @bazel_clang_tidy//clang_tidy:clang_tidy.bzl%clang_tidy_aspect
build:clang-tidy-base --@bazel_clang_tidy//:clang_tidy_config=//:tidy_config
build:clang-tidy-base --output_groups=report
build:clang-tidy-base --keep_going
build:clang-tidy-base --spawn_strategy=local

build:verbose-clang-tidy --config=clang-tidy-base
build:verbose-clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=//tools:verbose-clang-tidy
Expand Down
9 changes: 9 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ filegroup(
visibility = ["//visibility:public"],
)

filegroup(
name = "format_ignore",
srcs = [
"//third_party/vesc_uart",
],
visibility = ["//visibility:public"],
)

filegroup(
name = "tidy_config",
srcs = [".clang-tidy"],
Expand All @@ -20,6 +28,7 @@ clang_format_update(
name = "clang-format",
binary = "@llvm_16_toolchain//:clang-format",
config = ":format_config",
ignore = ":format_ignore",
)

clang_tidy_apply_fixes(
Expand Down
8 changes: 4 additions & 4 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,20 @@ cc_library(
url = "https://github.com/boost-ext/ut/archive/%s.tar.gz" % BOOST_UT_VERSION,
)

BAZEL_CLANG_FORMAT_VERSION = "f4198b68887699a4d1862e44458e4969ad69fc8a"
BAZEL_CLANG_FORMAT_VERSION = "0436f63ce242cc3682b4e8dc8455a4ff725b5c8f"

http_archive(
name = "bazel_clang_format",
sha256 = "bd75df6dae8d290a716e1812c463ef4ab36869b5557d8a6dd6abb8315acfc6ac",
sha256 = "604a32981039812f3c61f0212114c8bcc1f73289e5fc8c0c40ced1c9280e62d2",
strip_prefix = "bazel_clang_format-%s" % BAZEL_CLANG_FORMAT_VERSION,
url = "https://github.com/oliverlee/bazel_clang_format/archive/%s.tar.gz" % BAZEL_CLANG_FORMAT_VERSION,
)

BAZEL_CLANG_TIDY_VERSION = "aae87699cca19d8f6e84538576ab47587043d1d2"
BAZEL_CLANG_TIDY_VERSION = "7a77cacef6294e8df644c66c19190ed95d3d35cf"

http_archive(
name = "bazel_clang_tidy",
sha256 = "ee7d89375b5c6554b40ea1b1132a8cf7e3e269f7c2f6b2f595e4c7181d44b736",
sha256 = "b4df2f62a839dfe991bfa22750afc7577ce76740aee44efb321ea4a79ecd5146",
strip_prefix = "bazel_clang_tidy-%s" % BAZEL_CLANG_TIDY_VERSION,
url = "https://github.com/oliverlee/bazel_clang_tidy/archive/%s.tar.gz" % BAZEL_CLANG_TIDY_VERSION,
)
Expand Down
Empty file added third_party/BUILD.bazel
Empty file.
5 changes: 5 additions & 0 deletions third_party/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Third party code
================
This directory contains a collection of third party code that is being checked
into this repository rather than downloaded as dependencies referenced in the
WORKSPACE file.
9 changes: 9 additions & 0 deletions third_party/vesc_uart/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Checks: >
-*,
# dummy, see below,
misc-definitions-in-headers,
CheckOptions:
- key: HeaderFileExtensions
value: "x"
32 changes: 32 additions & 0 deletions third_party/vesc_uart/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "vesc_uart",
srcs = [
"src/VescUart.cpp",
"src/buffer.cpp",
"src/compatability/WProgram.h",
"src/crc.cpp",
"src/includes/buffer.h",
"src/includes/crc.h",
"src/includes/datatypes.h",
"src/includes/local_datatypes.h",
],
hdrs = ["VescUart.h"],
copts = [
"-Wno-conversion",
"-Wno-double-promotion",
"-Wno-old-style-cast",
"-Wno-unused-but-set-parameter",
"-xc++",
],
includes = [
".",
"src/compatability",
"src/includes",
],
visibility = ["//:__subpackages__"],
deps = [
"//uart_abstraction:fake",
],
)
20 changes: 20 additions & 0 deletions third_party/vesc_uart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Interface library for UART interaction with the VESC motor controller
=====================================================================
The library has a high-level API for sending commands to the motor controller.
It requires a UART abstraction library with the following API in order to
build.

```cpp
// HardwareSerial.hpp

struct HardwareSerial {
auto available() const -> bool;

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

auto write(std::uint8_t* buffer, std::size_t size) const -> void;
};
```
A global variable named `Serial` of the above type has to be provided when
linking the code.
125 changes: 125 additions & 0 deletions third_party/vesc_uart/VescUart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2015 - 2017 Andreas Chaitidis [email protected]
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _VESCUART_h
#define _VESCUART_h

//#include "Config.h"

/*TThis library was created on an Adruinio 2560 with different serial ports to have a better possibility
to debug. The serial ports are define with #define:
#define SERIALIO Serial1 for the UART port to VESC
#define DEBUGSERIAL Serial for debuging over USB
So you need here to define the right serial port for your arduino.
If you want to use debug, uncomment DEBUGSERIAL and define a port.*/


#ifndef _CONFIG_h

#ifdef __AVR_ATmega2560__
#define SERIALIO Serial1
#define SERIALIO2 Serial2
#define SERIALIO3 Serial3
#define DEBUGSERIAL Serial
#endif

#ifdef ARDUINO_AVR_NANO
#define SERIALIO Serial
#define DEBUGSERIAL Serial
#endif
#endif

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include "datatypes.h"
#include "local_datatypes.h"
///PackSendPayload Packs the payload and sends it over Serial.
///Define in a Config.h a SERIAL with the Serial in Arduino Style you want to you
///@param: payload as the payload [unit8_t Array] with length of int lenPayload
///@return the number of bytes send

int PackSendPayload(uint8_t* payload, int lenPay, int num);

///ReceiveUartMessage receives the a message over Serial
///Define in a Config.h a SERIAL with the Serial in Arduino Style you want to you
///@parm the payload as the payload [unit8_t Array]
///@return the number of bytes receeived within the payload

int ReceiveUartMessage(uint8_t* payloadReceived, int num);

///Help Function to print struct bldcMeasure over Serial for Debug
///Define in a Config.h the DEBUGSERIAL you want to use

void SerialPrint(const struct bldcMeasure& values);

///Help Function to print uint8_t array over Serial for Debug
///Define in a Config.h the DEBUGSERIAL you want to use

void SerialPrint(uint8_t* data, int len);

///Sends a command to VESC and stores the returned data
///@param bldcMeasure struct with received data
//@return true if success
bool VescUartGetValue(struct bldcMeasure& values, int num);
bool VescUartGetValue(bldcMeasure& values);

///Sends a command to VESC to control the motor current
///@param current as float with the current for the motor
///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)

void VescUartSetCurrent(float current, int num);
void VescUartSetCurrent(float current);

///Sends a command to VESC to control the motor brake
///@param breakCurrent as float with the current for the brake
///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)

void VescUartSetCurrentBrake(float brakeCurrent, int num);
void VescUartSetCurrentBrake(float brakeCurrent);

///Sends values of a joystick and 2 buttons to VESC to control the nunchuk app

void VescUartSetNunchukValues(remotePackage& data, int num);
void VescUartSetNunchukValues(remotePackage& data);

///Sends a command to VESC to control the motor position
///@param position as float with the position in degrees for the motor
///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)

void VescUartSetPosition(float position, int num) ;
void VescUartSetPosition(float position) ;

///Sends a command to VESC to control the motor duty cycle
///@param duty as float with the duty cycle for the motor
///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)

void VescUartSetDuty(float duty, int num) ;
void VescUartSetDuty(float duty) ;

///Sends a command to VESC to control the motor rotational speed
///@param rpm as float with the revolutions per second for the motor
///@param num as integer with the serial port in use (0=Serial; 1=Serial1; 2=Serial2; 3=Serial3;)

void VescUartSetRPM(float rpm, int num);
void VescUartSetRPM(float rpm);

#endif
Loading

0 comments on commit cf9f724

Please sign in to comment.