From 7b8d574d07cd80c2c52f52d6ecd9660367fbae4e Mon Sep 17 00:00:00 2001 From: Witty-Wizard Date: Sat, 5 Oct 2024 22:32:33 +0530 Subject: [PATCH 1/2] Added : Implementation for HBridge Control of Motor --- src/DriveMaster.cpp | 5 ++++- src/DriveMaster.h | 31 ++++++++++++++++++++++++++++--- src/HBridge.cpp | 26 ++++++++++++++++++++++++++ src/HBridge.h | 21 +++++++++++++++++++++ src/dshot.cpp | 5 ++++- src/dshot.h | 10 +++++++--- 6 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/HBridge.cpp create mode 100644 src/HBridge.h diff --git a/src/DriveMaster.cpp b/src/DriveMaster.cpp index fdeaae2..5dfe309 100644 --- a/src/DriveMaster.cpp +++ b/src/DriveMaster.cpp @@ -1,12 +1,15 @@ #include "DriveMaster.h" -DriveMaster::DriveMaster(int pin) : _pin(pin) {} +DriveMaster::DriveMaster(int pin) : _pin(pin), _dir_pin(-1) {} + +DriveMaster::DriveMaster(int pin, int dir_pin) : _pin(pin), _dir_pin(dir_pin) {} DriveMaster::~DriveMaster() {} void DriveMaster::begin() {} void DriveMaster::write(uint16_t value, bool telemetery) {} +void DriveMaster::write(int16_t value) {} void DriveMaster::sendCommand(uint16_t value) {} void DriveMaster::sendValue(uint16_t value) {} \ No newline at end of file diff --git a/src/DriveMaster.h b/src/DriveMaster.h index 04c7ce5..362e663 100644 --- a/src/DriveMaster.h +++ b/src/DriveMaster.h @@ -12,32 +12,55 @@ /** * @brief Base class for motor control. */ -class DriveMaster { +class DriveMaster +{ public: /** - * @brief Construct a new DriveMaster object with the specified pin. + * @brief Construct a new DriveMaster object with only the pin. * @param pin The pin number for motor control. */ DriveMaster(int pin); + + /** + * @brief Construct a new DriveMaster object with the specified pin and direction pin. + * @param pin The pin number for motor control. + * @param dir_pin The pin number for direction control. + */ + DriveMaster(int pin, int dir_pin); + /** * @brief Destroy the DriveMaster object. */ virtual ~DriveMaster(); + /** * @brief Initialize the motor control. */ virtual void begin(); + /** * @brief Write a command to the motor. * @param value The command value to write. * @param telemetry Flag indicating telemetry presence. */ virtual void write(uint16_t value, bool telemetery = false); + + /** + * @brief Write a command to the motor using an 8-bit signed integer value. + * The value ranges from -255 to 255, where: + * - 255 is full speed forward + * - -255 is full speed reverse + * - 0 is stop + * @param value The signed 8-bit command value to write (-255 to 255). + */ + virtual void write(int16_t value); + /** * @brief Send a command to the motor. * @param value The command value to send. */ virtual void sendCommand(uint16_t value); + /** * @brief Send a value to the motor. * @param value The value to send. @@ -45,7 +68,9 @@ class DriveMaster { virtual void sendValue(uint16_t value); protected: - int _pin; /**< Pin number for motor control. */ + int _pin; /**< Pin number for motor control. */ + int _dir_pin; /**< Pin number for direction control. */ }; #include "dshot.h" +#include "HBridge.h" #endif diff --git a/src/HBridge.cpp b/src/HBridge.cpp new file mode 100644 index 0000000..dcf7eeb --- /dev/null +++ b/src/HBridge.cpp @@ -0,0 +1,26 @@ +#include + +HBridge::HBridge(int pin, int dir_pin) : DriveMaster(pin, dir_pin) +{ +} + +void HBridge::begin() +{ + pinMode(_pin, OUTPUT); + pinMode(_dir_pin, OUTPUT); +} + +void HBridge::write(int16_t value) +{ + value = value % 256; + if (value >= 0) + { + digitalWrite(_dir_pin, LOW); + } + else + { + digitalWrite(_dir_pin, HIGH); + value = 255 + value; + } + analogWrite(_pin, value); +} \ No newline at end of file diff --git a/src/HBridge.h b/src/HBridge.h new file mode 100644 index 0000000..ddeb029 --- /dev/null +++ b/src/HBridge.h @@ -0,0 +1,21 @@ +/** + * @file HBridge.h + * @brief Header file for the HBridge class. + */ + +#pragma once +#ifndef HBRIDGE_H +#define HBRIDGE_H + +#include "DriveMaster.h" + +class HBridge : public DriveMaster{ + private: + + public: + explicit HBridge(int pin, int dir_pin); + void begin() override; + void write(int16_t value) override; +}; + +#endif \ No newline at end of file diff --git a/src/dshot.cpp b/src/dshot.cpp index 2c4298b..8f94873 100644 --- a/src/dshot.cpp +++ b/src/dshot.cpp @@ -1,5 +1,7 @@ #include "dshot.h" +#if defined(ARDUINO_ARCH_ESP32) + dshot::dshot(int pin, DShotType type) : DriveMaster(pin) { switch (type) { case DSHOT_150: @@ -44,4 +46,5 @@ uint8_t dshot::calculateCrc(uint16_t value) { } void dshot::sendCommand(uint16_t value) { write(value, true); } -void dshot::sendValue(uint16_t value) { write(value, false); } \ No newline at end of file +void dshot::sendValue(uint16_t value) { write(value, false); } +#endif \ No newline at end of file diff --git a/src/dshot.h b/src/dshot.h index f57a360..3500e50 100644 --- a/src/dshot.h +++ b/src/dshot.h @@ -8,13 +8,16 @@ #define DSHOT_H #include "DriveMaster.h" + +#if defined(ARDUINO_ARCH_ESP32) #include #define DSHOT_FRAME_LENGTH 16 /** * @brief Enumeration for different DShot types. */ -enum DShotType { +enum DShotType +{ DSHOT_150, /**< DShot150 protocol */ DSHOT_300 /**< DShot300 protocol */ }; @@ -22,7 +25,8 @@ enum DShotType { /** * @brief Class representing DShot communication. */ -class dshot : public DriveMaster { +class dshot : public DriveMaster +{ private: rmt_data_t _data[DSHOT_FRAME_LENGTH]; /**< RMT data array for DShot frames */ uint16_t _timingDuration0; /**< Timing duration for logic 0 */ @@ -68,5 +72,5 @@ class dshot : public DriveMaster { */ void sendValue(uint16_t value) override; }; - +#endif #endif From 47cef61df12453b7570f59c8cc24ad9e665cefa1 Mon Sep 17 00:00:00 2001 From: Witty-Wizard Date: Sat, 5 Oct 2024 22:38:20 +0530 Subject: [PATCH 2/2] Added: AVR Architecture in the config file --- examples/basic_dshot/arduino-ci.yml | 3 +++ library.properties | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 examples/basic_dshot/arduino-ci.yml diff --git a/examples/basic_dshot/arduino-ci.yml b/examples/basic_dshot/arduino-ci.yml new file mode 100644 index 0000000..2a8a52b --- /dev/null +++ b/examples/basic_dshot/arduino-ci.yml @@ -0,0 +1,3 @@ +compile: + platforms: + - esp32 \ No newline at end of file diff --git a/library.properties b/library.properties index 60d2b34..d96f68a 100644 --- a/library.properties +++ b/library.properties @@ -10,4 +10,4 @@ maintainer=Witty Wizard license=GPL-3.0-only frameworks=arduino platforms=espressif32 -architectures=esp32 +architectures=esp32, avr