diff --git a/README.md b/README.md index 00b5b18..b9ec1a9 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.1 for SimpleFOC 2.3.1 + :warning: code in development! Please help us test it! diff --git a/examples/dc-torque-voltage/dc-torque-voltage.ino b/examples/dc-torque-voltage/dc-torque-voltage.ino index 2b06e1e..e69fcad 100644 --- a/examples/dc-torque-voltage/dc-torque-voltage.ino +++ b/examples/dc-torque-voltage/dc-torque-voltage.ino @@ -46,6 +46,7 @@ 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; + driver.pwm_frequency = 5000; // init driver driver.init(); // init sensor diff --git a/library.properties b/library.properties index 60eb2ae..1dd34fd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SimpleDCMotor -version=1.0.0 +version=1.0.1 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..e16f1f7 100644 --- a/src/DCMotor.cpp +++ b/src/DCMotor.cpp @@ -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