From ff6ad3a62ab39e134828035cc0856ad1bc8c0140 Mon Sep 17 00:00:00 2001 From: Richard Unger Date: Sat, 27 May 2023 01:32:13 +0200 Subject: [PATCH 1/6] improvements to DCDriver1PWM --- src/drivers/DCDriver1PWM.cpp | 42 ++++++++++++++++++++++++++++++------ src/drivers/DCDriver1PWM.h | 12 ++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/drivers/DCDriver1PWM.cpp b/src/drivers/DCDriver1PWM.cpp index 1310219..163100f 100644 --- a/src/drivers/DCDriver1PWM.cpp +++ b/src/drivers/DCDriver1PWM.cpp @@ -10,6 +10,24 @@ 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; + 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) { + pwm_max = 1.0f - pwm_max; + pwm_min = 1.0f - pwm_min; + 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 +42,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 From 5ee56f580edb856b23b73075e29362be61651fae Mon Sep 17 00:00:00 2001 From: Richard Unger Date: Fri, 2 Jun 2023 01:01:53 +0200 Subject: [PATCH 2/6] fix bugs around active-low configuration --- src/drivers/DCDriver1PWM.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/drivers/DCDriver1PWM.cpp b/src/drivers/DCDriver1PWM.cpp index 163100f..adce0c3 100644 --- a/src/drivers/DCDriver1PWM.cpp +++ b/src/drivers/DCDriver1PWM.cpp @@ -12,13 +12,15 @@ 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) { - pwm_max = 1.0f - pwm_max; - pwm_min = 1.0f - pwm_min; + 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); @@ -62,7 +64,7 @@ void DCDriver1PWM::setPwm(float U){ void DCDriver1PWM::setPwmMicroseconds(int us){ float U = (float)us / pwm_period_us; - if (!active_high) + if (!active_high) U = 1.0f - U; U = _constrain(U, pwm_min, pwm_max); _writeDutyCycle1PWM(U, params); From 15f2523a6178f7c3f81d0ec1b6cd5040a10a2be4 Mon Sep 17 00:00:00 2001 From: Richard Unger Date: Sat, 23 Sep 2023 19:57:39 +0200 Subject: [PATCH 3/6] next library version --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 31b250530b3724520f7b92e228e8d237aeb751df Mon Sep 17 00:00:00 2001 From: Richard Unger Date: Sat, 23 Sep 2023 19:57:55 +0200 Subject: [PATCH 4/6] update DCMotor class for SimpleFOC 2.3.1 --- src/DCMotor.cpp | 2 +- src/DCMotor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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; From e48a262901b70ea845715f35f12e328ac22cce73 Mon Sep 17 00:00:00 2001 From: Richard Unger Date: Sat, 23 Sep 2023 20:00:51 +0200 Subject: [PATCH 5/6] updated README for 1.0.1 release --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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! From ce68acee6f6b2eb8b37d020e225bf38283237052 Mon Sep 17 00:00:00 2001 From: Sanjeev Hegde Date: Mon, 30 Oct 2023 16:00:40 +0530 Subject: [PATCH 6/6] Add PWM frequency during setup --- examples/dc-torque-voltage/dc-torque-voltage.ino | 1 + 1 file changed, 1 insertion(+) 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