diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d2dc4cd..6d8182d 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -14,7 +14,9 @@ jobs: # - windows-latest compiler: - llvm-18 - # - gcc-13 # something wrong with boost and gcc-13 + - llvm-20 + # - gcc-15 + # - gcc-13 # something wrong with boost and gcc-* # - msvc # you can specify the version after `-` like `llvm-13.0.0`. steps: diff --git a/CMakeLists.txt b/CMakeLists.txt index bbdde2e..66986d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,14 @@ add_subdirectory(libs/async_serial) # aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/libs/async_serial/ SUB_SOURCES) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/async_serial/include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/) +find_package (Git) +execute_process(COMMAND + "${GIT_EXECUTABLE}" describe --match=NeVeRmAtCh --always --abbrev=40 --dirty + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_SHA1_TMX + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) +message("SHA1 tmx_cpp:" ${GIT_SHA1_TMX}) +add_compile_definitions(GIT_SHA1_TMX="${GIT_SHA1_TMX}") add_library(tmx_cpp STATIC @@ -26,12 +34,15 @@ add_library(tmx_cpp src/modules/HiwonderServo.cpp src/modules/PCA9685.cpp src/modules/SSD1306_oled.cpp + src/modules/Shutdown_relay.cpp + src/sensors/MPU6050.cpp src/sensors.cpp src/sensors/ADXL345.cpp src/sensors/INA226.cpp src/sensors/MPU9250.cpp src/sensors/VEML6040.cpp src/sensors/AS5600.cpp + src/sensors/BNO055.cpp src/tmx_components/Neopixel.cpp ) diff --git a/include/tmx_cpp/modules/Module_types.hpp b/include/tmx_cpp/modules/Module_types.hpp index 9a42e02..ddbba22 100644 --- a/include/tmx_cpp/modules/Module_types.hpp +++ b/include/tmx_cpp/modules/Module_types.hpp @@ -7,7 +7,7 @@ namespace tmx_cpp { enum MODULE_TYPE : uint8_t { PCA9685 = 0, HIWONDER_SERVO = 1, - SHUTDOWN_RELAY = 2, // Not implemented, TODO + SHUTDOWN_RELAY = 2, TMX_SSD1306 = 3, MAX }; diff --git a/include/tmx_cpp/modules/PCA9685.hpp b/include/tmx_cpp/modules/PCA9685.hpp index 88a77f9..e5dc0a1 100644 --- a/include/tmx_cpp/modules/PCA9685.hpp +++ b/include/tmx_cpp/modules/PCA9685.hpp @@ -24,6 +24,7 @@ class PCA9685_module : public Module_type { void data_callback(std::vector data); void attach_send_module(std::function)> send_module); + uint16_t get_high(); private: uint8_t i2c_port; diff --git a/include/tmx_cpp/modules/Shutdown_relay.hpp b/include/tmx_cpp/modules/Shutdown_relay.hpp new file mode 100644 index 0000000..0f1a02e --- /dev/null +++ b/include/tmx_cpp/modules/Shutdown_relay.hpp @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include + +#include "tmx_cpp/modules/Module_t.hpp" + +using namespace std::chrono_literals; + +namespace tmx_cpp { + +class Shutdown_relay_module : public Module_type { +public: + std::function)> send_module; + Shutdown_relay_module(uint8_t pin, uint8_t turn_off_time_sec, + bool turn_off_value); + + virtual std::vector init_data() override; + virtual void data_callback(std::vector data) override; + void send_shutdown_signal(bool enable) { + if (this->send_module) { + this->send_module({enable ? (uint8_t)1 : (uint8_t)0}); + } else { + std::cerr << "Error: send_module function is not set for shutdown relay " + "module, cannot send shutdown signal." + << std::endl; + } + } + // FIXME: Maybe give this a default implementation, since all implementations + // are the same. + virtual void attach_send_module( + std::function)> send_module) override; + +private: + uint8_t pin; + uint8_t turn_off_time_sec; + bool turn_off_value; +}; + +} // namespace tmx_cpp \ No newline at end of file diff --git a/include/tmx_cpp/sensors/BNO055.hpp b/include/tmx_cpp/sensors/BNO055.hpp new file mode 100644 index 0000000..e1d6d74 --- /dev/null +++ b/include/tmx_cpp/sensors/BNO055.hpp @@ -0,0 +1,38 @@ +#pragma once +#include "tmx_cpp/sensors/Sensor_t.hpp" +#include +#include +#include + +namespace tmx_cpp { +struct BNO055_MOD_data { + // data is already in the correct units (accel in m/s^2, gyro in rad/s, mag in + // uT, temp in C) std::vector euler; + std::vector gyro; + // std::vector linear_accel; + std::vector mag; + std::vector accel; + // std::vector gravity; + struct { + float x; + float y; + float z; + float w; + } quat; + uint8_t temp; +}; +using BNO055_cb_t = std::function; +class BNO055_module : public Sensor_type { +public: + std::function)> send_module; + BNO055_module(uint8_t i2c_port, uint8_t address, BNO055_cb_t data_cb); + + std::vector init_data(); + void data_callback(std::vector data); + BNO055_cb_t data_cb; + +private: + uint8_t i2c_port = 0; +}; + +} // namespace tmx_cpp \ No newline at end of file diff --git a/include/tmx_cpp/sensors/MPU6050.hpp b/include/tmx_cpp/sensors/MPU6050.hpp new file mode 100644 index 0000000..550c26a --- /dev/null +++ b/include/tmx_cpp/sensors/MPU6050.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "tmx_cpp/sensors/Sensor_t.hpp" +#include +#include +#include + +namespace tmx_cpp { + +using MPU6050_cb_t = + std::function acceleration, + std::array gyro, float temperature)>; +class MPU6050_module : public Sensor_type { +public: + std::function)> send_module; + MPU6050_module(uint8_t i2c_port, uint8_t address, MPU6050_cb_t data_cb); + + std::vector init_data() override; + virtual void data_callback(std::vector data) override; + MPU6050_cb_t data_cb; + +private: + uint8_t i2c_port = 0; + uint8_t address = 0x68; +}; + +} // namespace tmx_cpp diff --git a/include/tmx_cpp/sensors/Sensor_types.hpp b/include/tmx_cpp/sensors/Sensor_types.hpp index 5e8e47e..ff38577 100644 --- a/include/tmx_cpp/sensors/Sensor_types.hpp +++ b/include/tmx_cpp/sensors/Sensor_types.hpp @@ -11,6 +11,9 @@ enum SENSOR_TYPE : uint8_t { INA226 = 0x06, // HMC5883 = 0x07, // TODO: Not implemented AS5600 = 0x08, + + MPU6050_t = 9, + BNO055_t = 10, SENSOR_MAX }; diff --git a/include/tmx_cpp/tmx.hpp b/include/tmx_cpp/tmx.hpp index 5b7699c..5b5f810 100644 --- a/include/tmx_cpp/tmx.hpp +++ b/include/tmx_cpp/tmx.hpp @@ -106,7 +106,7 @@ class TMX { uint16_t analog_differential = 0); void digitalWrite(uint8_t pin, bool value); void pwmWrite(uint8_t pin, uint16_t value); - + void pwmWrite(std::vector> pin_values); bool attach_encoder(uint8_t pin_A, uint8_t pin_B, callback_func_pin_int callback); @@ -177,7 +177,9 @@ class TMX { parse_buffer_for_message(std::vector &buffer, uint8_t len, uint8_t type); std::pair> - get_feature(const enum MESSAGE_TYPE type); + get_feature(const enum MESSAGE_TYPE type, + std::chrono::duration timeout = + std::chrono::milliseconds(50000)); bool is_stopped = false; private: /* Ping related elements */ diff --git a/src/modules/PCA9685.cpp b/src/modules/PCA9685.cpp index c59c22d..92ce4c0 100644 --- a/src/modules/PCA9685.cpp +++ b/src/modules/PCA9685.cpp @@ -70,6 +70,11 @@ std::vector PCA9685_module::init_data() { return data; } +uint16_t PCA9685_module::get_high() { + return (uint16_t)4096; // The PCA9685 has 12-bit resolution, so the maximum + // high value is 4096 (2^12). +} + void PCA9685_module::data_callback(std::vector data) { // it should not receive any data return; diff --git a/src/modules/Shutdown_relay.cpp b/src/modules/Shutdown_relay.cpp new file mode 100644 index 0000000..8266bf5 --- /dev/null +++ b/src/modules/Shutdown_relay.cpp @@ -0,0 +1,46 @@ +#include +#include + +#include "tmx_cpp/modules/Shutdown_relay.hpp" +#include "tmx_cpp/types.hpp" + +using namespace tmx_cpp; + +Shutdown_relay_module::Shutdown_relay_module(uint8_t pin, + uint8_t turn_off_time_sec, + bool turn_off_value) + : pin(pin), turn_off_time_sec(turn_off_time_sec), + turn_off_value(turn_off_value) { + if (turn_off_time_sec > 200) { + std::cerr << "Turn off time is too high for the shutdown relay module, " + "capping it at 200 seconds." + << std::endl; + this->turn_off_time_sec = 200; + } + if (turn_off_time_sec < 1) { + std::cerr << "Turn off time is too low for the shutdown relay module, " + "setting it to 20 seconds." + << std::endl; + this->turn_off_time_sec = 20; + } + type = MODULE_TYPE::SHUTDOWN_RELAY; +} + +void Shutdown_relay_module::data_callback(std::vector data) { + // should not receive any data, since this module is write only, but just in + // case, ignore it + std::cerr << "Warning: Shutdown relay module received data, but this module " + "is write only. Ignoring data." + << std::endl; + return; +} + +void Shutdown_relay_module::attach_send_module( + std::function)> send_module) { + this->send_module = send_module; +} + +std::vector Shutdown_relay_module::init_data() { + return {this->pin, static_cast(this->turn_off_value ? 1 : 0), + this->turn_off_time_sec}; +} \ No newline at end of file diff --git a/src/sensors/BNO055.cpp b/src/sensors/BNO055.cpp new file mode 100644 index 0000000..2b77251 --- /dev/null +++ b/src/sensors/BNO055.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include +#include + +using namespace tmx_cpp; + +// struct BNO055_MOD_data { +// float voltage; +// float current; +// } +BNO055_module::BNO055_module(uint8_t i2c_port, uint8_t address, + BNO055_cb_t data_cb) + : data_cb(data_cb), i2c_port(i2c_port) { + std::cout << "Creating BNO055 module on port " << (int)i2c_port << std::endl; + // this-> + this->type = SENSOR_TYPE::BNO055_t; +} + +std::vector BNO055_module::init_data() { + std::cout << "bno port: " << (int)i2c_port << std::endl; + this->type = SENSOR_TYPE::BNO055_t; + + return {i2c_port}; +} + +void BNO055_module::data_callback(std::vector data) { + static_assert(sizeof(float) == sizeof(uint32_t)); + static_assert(sizeof(float) == 4); + // assert(data.size() == ((3*3+4)*sizeof(float) + 1)); // 3 axes, each with 3 + // floats (accel, gyro, mag) + temp + auto data_span = std::span(data); + // std::cout << "recevied bno data!!!" << std::endl; + // for(auto byte : data) { + // std::cout << std::hex << (int)byte << " "; + // } + // std::cout << std::dec << std::endl; + auto out_floats = std::vector(data.size() / sizeof(float)); + for (size_t i = 0; i < out_floats.size(); i++) { + out_floats[i] = decode_float( + data_span.subspan(i * sizeof(float)).first()); + // std::cout << "float " << i << ": " << out_floats[i] << std::endl; + } + // std::cout << "temp: " << (int) data[data.size()-1] << std::endl; + + std::vector> sub_vectors; + // split out_floats into 3 vectors of 3 floats each + size_t subvec_size = 3; + size_t num_subvecs = 3; + for (std::size_t i(0); i < num_subvecs; ++i) { + sub_vectors.emplace_back(out_floats.begin() + i * subvec_size, + out_floats.begin() + (i + 1) * subvec_size); + } + // order of data sent by pico: + // Adafruit_BNO055::VECTOR_GYROSCOPE, + // Adafruit_BNO055::VECTOR_MAGNETOMETER, + // Adafruit_BNO055::VECTOR_ACCELEROMETER, + // quaternion (4 floats) + // temp (1 byte) + BNO055_MOD_data mod_data{ + // .euler = sub_vectors[0], + .gyro = sub_vectors[0], + // .linear_accel = sub_vectors[2], + .mag = sub_vectors[1], + .accel = sub_vectors[2], + // .gravity = sub_vectors[5], + .quat = + { + .x = out_floats[3 * 3], + .y = out_floats[3 * 3 + 1], + .z = out_floats[3 * 3 + 2], + .w = out_floats[3 * 3 + 3], + }, + .temp = data[data.size() - 1], + }; + data_cb(mod_data); +} diff --git a/src/sensors/MPU6050.cpp b/src/sensors/MPU6050.cpp new file mode 100644 index 0000000..1a23d6b --- /dev/null +++ b/src/sensors/MPU6050.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +#include +#include +#include + +using namespace tmx_cpp; + +MPU6050_module::MPU6050_module(uint8_t i2c_port, uint8_t address, + MPU6050_cb_t data_cb) + : data_cb(data_cb), i2c_port(i2c_port), address(address) { + this->type = SENSOR_TYPE::MPU6050_t; +} + +std::vector MPU6050_module::init_data() { + this->type = SENSOR_TYPE::MPU6050_t; + + return {i2c_port, address}; +} +#include +void MPU6050_module::data_callback(std::vector data) { + static_assert(sizeof(float) == sizeof(uint32_t)); + static_assert(sizeof(float) == 4); + // std::cout << "tmx MPU6050_module received data callback with " << + // data.size() << " bytes" + // << std::endl; + // 2 measurements x 3 axes + Temperature (1) + assert(data.size() == sizeof(uint16_t) * (3 * 2 + 1)); + auto data_span = std::span(data); + + std::array acceleration; + std::array gyro; + float temperature; + + for (int acc_idx = 0; acc_idx < 3; acc_idx++) { + // pico sets it to +-2g range, so scale accordingly (see datasheet) + acceleration[acc_idx] = + decode_i16(data_span.subspan(acc_idx * sizeof(uint16_t)) + .first()) / + std::pow(2, 15) * 2; // scale to g's + } + + for (int gyro_idx = 3; gyro_idx < 6; gyro_idx++) { + // pico sets it to +-250dps range, so scale accordingly (see datasheet) + gyro[gyro_idx - 3] = + decode_i16(data_span.subspan(gyro_idx * sizeof(uint16_t)) + .first()) / + std::pow(2, 15) * 250; // scale to degrees/s + } + + temperature = + decode_i16( + data_span.subspan(6 * sizeof(uint16_t)).first()) / + 340.0 + + 36.53; // scale to degrees Celsius + + data_cb(acceleration, gyro, temperature); +} diff --git a/src/tmx.cpp b/src/tmx.cpp index ad12782..97184da 100644 --- a/src/tmx.cpp +++ b/src/tmx.cpp @@ -108,7 +108,7 @@ TMX::TMX(std::function stop_func, std::string port, module_sys(std::make_shared(this)), sensors_sys(std::make_shared(this)), is_stopped(false) { using namespace std::placeholders; - + std::cout << "TMX version " << GIT_SHA1_TMX << std::endl; this->serial = std::make_shared(port, 115200); // sleep for a second this->serial->setCallback([](const char *data, size_t len) { @@ -617,6 +617,23 @@ void TMX::pwmWrite(uint8_t pin, uint16_t value) { append_range(message, encode_u16(value)); this->sendMessage(MESSAGE_TYPE::PWM_WRITE, message); } +void TMX::pwmWrite(std::vector> pin_values) { + auto feature = this->get_feature(MESSAGE_TYPE::PWM_WRITE); + if (!feature.first || feature.second.size() < 1 || feature.second[0] == 0) { + // apparently this hw doesnt support streamlined pwm write + // this might overload the usb connection... + for (auto const &pin : pin_values) { + this->pwmWrite(pin.first, pin.second); + } + return; + } + std::vector message; + for (const auto &pv : pin_values) { + message.push_back(pv.first); + append_range(message, encode_u16(pv.second)); + } + this->sendMessage(MESSAGE_TYPE::PWM_WRITE, message); +} void TMX::add_callback( MESSAGE_IN_TYPE type, std::function &)> callback) { @@ -1127,7 +1144,15 @@ bool TMX::set_id(const TMX::serial_port &port, uint8_t id) { void TMX::ping_task() { std::cout << "ping task started" << std::endl; - this->get_feature(MESSAGE_TYPE::BOOTLOADER_RESET); + if (!this->get_feature(MESSAGE_TYPE::SET_PIN_MODE, + std::chrono::milliseconds(1000)) + .first) { + std::cout << "get_feature probably stuck, so shutdown!" << std::endl; + this->is_stopped = true; + this->stop_func(); + return; + } + std::cout << "got feature ping task!" << std::endl; if (!this->get_feature(MESSAGE_TYPE::PING).first) { std::cout << "ping not supported" << std::endl; return; @@ -1167,7 +1192,6 @@ void TMX::ping_callback(const std::vector message) { void TMX::feature_detect_task() { this->feature_detected = false; - for (auto i = 0; i < (int)MESSAGE_TYPE::MAX && !this->is_stopped; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(30)); this->sendMessage(MESSAGE_TYPE::FEATURE_REQUEST, {(uint8_t)i}); @@ -1195,15 +1219,22 @@ void TMX::feature_detect_task() { this->sensors_sys->check_features(); } -std::pair> TMX::get_feature(MESSAGE_TYPE type) { +std::pair> +TMX::get_feature(MESSAGE_TYPE type, + std::chrono::duration timeout) { if (!this->feature_detected && this->feature_index < (int)type) { std::unique_lock lk(this->feature_mutex); TMX_DEBUG std::cout << "Waiting " << (int)type << " " << this->feature_index << "... \n"; - this->feature_cv.wait(lk, [this, type] { + auto result = this->feature_cv.wait_for(lk, timeout, [this, type] { return this->feature_index >= (int)type || this->feature_detected || this->is_stopped; }); + if (!result) { + std::cout << "Timeout while waiting for feature: " << (int)type + << std::endl; + return {false, {}}; + } TMX_DEBUG std::cout << "done waiting " << (int)type << " " << this->feature_index << std::endl; } diff --git a/src/tmx_components/Neopixel.cpp b/src/tmx_components/Neopixel.cpp index fb150e2..9c3538c 100644 --- a/src/tmx_components/Neopixel.cpp +++ b/src/tmx_components/Neopixel.cpp @@ -20,7 +20,8 @@ bool TMX::attach_neopixel(uint8_t pin, uint8_t num_pixels, uint8_t type, std::cout << "num_pixels out of range (1-255)" << std::endl; return false; } - auto message = std::vector{pin, num_pixels, type}; + auto message = + std::vector{pin, num_pixels}; // type not yet added to msg auto [r, g, b] = color; auto l = {r, g, b}; if (std::any_of(l.begin(), l.end(), [](uint8_t c) { return c > 255; })) { @@ -32,7 +33,11 @@ bool TMX::attach_neopixel(uint8_t pin, uint8_t num_pixels, uint8_t type, message.push_back(b); this->sendMessage(MESSAGE_TYPE::INITIALIZE_NEO_PIXELS, message); - + // std::cout << "init neopixel" << std::endl; + // for(int i = 0; i < message.size(); i++) { + // std::cout << std::hex << (int)message[i] << " "; + // } + // std::cout << std::endl; this->neopixel_pin = pin; this->neopixel_len = num_pixels; this->neopixel_attached = true;