Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge back changes #4

Merged
merged 14 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!


Expand Down
1 change: 1 addition & 0 deletions examples/dc-torque-voltage/dc-torque-voltage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SimpleDCMotor
version=1.0.0
version=1.0.1
author=Simplefoc <[email protected]>
maintainer=Simplefoc <[email protected]>
sentence=A library enabling DC motor control with SimpleFOC.
Expand Down
2 changes: 1 addition & 1 deletion src/DCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion src/DCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
44 changes: 37 additions & 7 deletions src/drivers/DCDriver1PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
12 changes: 11 additions & 1 deletion src/drivers/DCDriver1PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ 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
* @param U voltage
*/
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;
};
Loading