diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index fc16dff..4310a32 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -26,32 +26,32 @@ jobs: - arduino:mbed_rp2040:pico # rpi pico include: - arduino-boards-fqbn: arduino:avr:uno - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: arduino:sam:arduino_due_x - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: arduino:samd:nano_33_iot - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: arduino:mbed_rp2040:pico - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: adafruit:samd:adafruit_metro_m4 platform-url: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers # - arduino-boards-fqbn: esp32:esp32:esp32doit-devkit-v1 # platform-url: https://dl.espressif.com/dl/package_esp32_index.json # required-libraries: Simple FOC # sketch-names: '**.ino' - arduino-boards-fqbn: esp32:esp32:esp32 # esp32 - platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json - required-libraries: Simple FOC + platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: esp32:esp32:esp32s2 # esp32s2 - platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json - required-libraries: Simple FOC + platform-url: https://espressif.github.io/arduino-esp32/package_esp32_index.json + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: STMicroelectronics:stm32:GenF1:pnum=BLUEPILL_F103C8 platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers - arduino-boards-fqbn: STMicroelectronics:stm32:Nucleo_64:pnum=NUCLEO_F411RE platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json - required-libraries: Simple FOC + required-libraries: Simple FOC,SimpleFOCDrivers # Do not cancel all jobs / architectures if one job fails fail-fast: false steps: diff --git a/README.md b/README.md index 00b5b18..98d5e76 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # SimpleDCMotor +![Library Compile](https://github.com/simplefoc/Arduino-FOC-dcmotor/workflows/Library%20Compile/badge.svg) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) +![Release](https://www.ardu-badge.com/badge/SimpleDCMotor.svg?) + + +Release 1.0.2 for SimpleFOC 2.3.1 or later + :warning: code in development! Please help us test it! @@ -123,6 +130,7 @@ void setup() { driver.voltage_power_supply = 10.0f; driver.voltage_limit = 10.0f; + driver.pwm_frequency = 5000; motor.torque_controller = TorqueControlType::voltage; motor.controller = MotionControlType::torque; diff --git a/examples/dc-open-loop/dc-open-loop.ino b/examples/dc-open-loop/dc-open-loop.ino index 10f4ab5..9e8ab12 100644 --- a/examples/dc-open-loop/dc-open-loop.ino +++ b/examples/dc-open-loop/dc-open-loop.ino @@ -13,12 +13,12 @@ #include #include "SimpleFOC.h" -#include "SimpleFOCDCMotor.h" +#include "SimpleDCMotor.h" // DCDriver object - this is the only thing needed for open-loop control. // There are different types to choose from, please select the correct one // that matches your motor driver hardware. -DCDriverSpeedDir driver = DCDriverSpeedDir(2, 3); +DCDriver1PWM1Dir driver = DCDriver1PWM1Dir(2, 3); /** * Setup function, in which you should intialize the driver. diff --git a/examples/dc-torque-voltage/dc-torque-voltage.ino b/examples/dc-torque-voltage/dc-torque-voltage.ino index 2b06e1e..82e570b 100644 --- a/examples/dc-torque-voltage/dc-torque-voltage.ino +++ b/examples/dc-torque-voltage/dc-torque-voltage.ino @@ -16,7 +16,7 @@ #include #include "SimpleFOC.h" -#include "SimpleFOCDCMotor.h" +#include "SimpleDCMotor.h" // DCMotor object @@ -46,6 +46,8 @@ void setup() { // if you want, you can limit the voltage used by the driver. // This value has to be same as or lower than the power supply voltage. driver.voltage_limit = 10.0f; + // often, you will want to set a lower PWM frequency than the default + driver.pwm_frequency = 5000; // init driver driver.init(); // init sensor diff --git a/library.properties b/library.properties index 60eb2ae..4754ea2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SimpleDCMotor -version=1.0.0 +version=1.0.2 author=Simplefoc maintainer=Simplefoc sentence=A library enabling DC motor control with SimpleFOC. diff --git a/src/DCMotor.cpp b/src/DCMotor.cpp index 72d2d5e..451e13a 100644 --- a/src/DCMotor.cpp +++ b/src/DCMotor.cpp @@ -56,7 +56,7 @@ void DCMotor::init() { enable(); _delay(50); - if (!_isset(sensor_direction)) { + if (sensor_direction==Direction::UNKNOWN) { sensor_direction = Direction::CW; SIMPLEFOC_DEBUG("MOT: Sensor Dir: CW"); } @@ -140,7 +140,7 @@ void DCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) { }; -int DCMotor::initFOC(float zero_electric_offset, Direction sensor_direction) { +int DCMotor::initFOC() { // nothing to do for DC motors return 0; // always return failure }; diff --git a/src/DCMotor.h b/src/DCMotor.h index 252e032..2adec5a 100644 --- a/src/DCMotor.h +++ b/src/DCMotor.h @@ -56,7 +56,7 @@ class DCMotor : public FOCMotor { virtual void setPhaseVoltage(float Uq, float Ud, float angle_el) override; // not implemented for DC motors - virtual int initFOC(float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW) override; + virtual int initFOC() override; // not implemented for DC motors virtual void loopFOC() override; diff --git a/src/drivers/DCDriver1PWM.cpp b/src/drivers/DCDriver1PWM.cpp index 1310219..adce0c3 100644 --- a/src/drivers/DCDriver1PWM.cpp +++ b/src/drivers/DCDriver1PWM.cpp @@ -10,6 +10,26 @@ DCDriver1PWM::DCDriver1PWM(int pinPWM, float threshold, int pinEN) { }; +void DCDriver1PWM::configureMicroseconds(int hz, int min_us, int zero_us, int max_us, bool active_high){ + pwm_frequency = hz; + this->active_high = active_high; + pwm_period_us = 1000000.0f / hz; + threshold = (float)zero_us / pwm_period_us; + pwm_max = (float)max_us / pwm_period_us; + pwm_min = (float)min_us / pwm_period_us; + if (!active_high) { + float temp = 1.0f - pwm_min; + pwm_min = 1.0f - pwm_max; + pwm_max = temp; + threshold = 1.0f - threshold; + } + pwm_max = _constrain(pwm_max, 0.0f, 1.0f); + pwm_min = _constrain(pwm_min, 0.0f, 1.0f); + threshold = _constrain(threshold, 0.0f, 1.0f); +}; + + + int DCDriver1PWM::init() { if (pinEN!=NOT_SET) { pinMode(pinEN, OUTPUT); @@ -24,18 +44,28 @@ int DCDriver1PWM::init() { void DCDriver1PWM::setPwm(float U){ U = _constrain(U, -voltage_limit, voltage_limit); - if (U>0.0f) { - U = ( U/voltage_power_supply ) * (1.0f-threshold); - U = _constrain(U,threshold,1.0f); + if (U>dead_zone) { + U = threshold + ( U/voltage_power_supply ) * (pwm_max - threshold); _writeDutyCycle1PWM(U, params); - } else if (U<0.0f) { + U = _constrain(U, pwm_min, pwm_max); + } else if (U<-dead_zone) { if (!scale_reverse) - U = ( -U/voltage_power_supply ) * (1.0f-threshold); // same scale as forward + U = threshold + ( U/voltage_power_supply ) * (pwm_max - threshold); // same scale as forward else - U = ( -U/voltage_power_supply ) * (threshold); // full reverse scale - U = _constrain(U,threshold,1.0f); + U = threshold + ( U/voltage_power_supply ) * (threshold - pwm_min); // full reverse scale + U = _constrain(U, pwm_min, pwm_max); _writeDutyCycle1PWM(U, params); } else { _writeDutyCycle1PWM(threshold, params); } +}; + + + +void DCDriver1PWM::setPwmMicroseconds(int us){ + float U = (float)us / pwm_period_us; + if (!active_high) + U = 1.0f - U; + U = _constrain(U, pwm_min, pwm_max); + _writeDutyCycle1PWM(U, params); }; \ No newline at end of file diff --git a/src/drivers/DCDriver1PWM.h b/src/drivers/DCDriver1PWM.h index 4c202e0..160ace4 100644 --- a/src/drivers/DCDriver1PWM.h +++ b/src/drivers/DCDriver1PWM.h @@ -17,6 +17,8 @@ class DCDriver1PWM : public DCDriver { virtual int init() override; + void configureMicroseconds(int hz, int min_us, int zero_us, int max_us, bool active_high = false); + /** * Set phase voltages to the harware * Positive voltages are associated with the "forward" direction, negative voltages with the "reverse" direction @@ -24,7 +26,15 @@ class DCDriver1PWM : public DCDriver { */ virtual void setPwm(float U) override; + void setPwmMicroseconds(int us); + int pinPWM; bool scale_reverse = true; //!< if true, the reverse scale is full reverse, if false, the reverse scale is the same as the forward scale - float threshold = 0.5; //!< duty cycle above which the motor is considered to be "forward" + float threshold = 0.5f; //!< duty cycle above which the motor is considered to be "forward" + float pwm_max = 1.0f; + float pwm_min = 0.0f; + bool active_high = true; + float dead_zone = 0.0f; + protected: + float pwm_period_us = NOT_SET; }; \ No newline at end of file