Skip to content

Update #6

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

Merged
merged 28 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2add419
Added delay in seria print for stability
vbratasiuk Apr 28, 2022
efee469
[fix] 4PWM wrong GenericParam pin2a <-> pin1B
arturohernandez10 May 22, 2022
f2b6b90
[fix] 4PWM wrong GenericParam pin2a <-> pin1B
arturohernandez10 May 22, 2022
7e65e51
Merge pull request #182 from arturohernandez10/master
runger1101001 May 23, 2022
8d110e3
make setPhaseVoltage public
May 25, 2022
c053265
change getVelocity() to remember velocity
May 25, 2022
a248ae0
Merge pull request #186 from runger1101001/BLDCmotor_change
runger1101001 May 30, 2022
4247ac2
leonardo small memory issues
askuric Jun 14, 2022
24bc913
bug #187 fix, typo
askuric Jun 14, 2022
c7d2a20
leonardo and mega2560 #190 compile error + added the autocompile to t…
askuric Jun 14, 2022
1080a4f
workflow issue
askuric Jun 14, 2022
b910ff9
Merge pull request #175 from vbratasiuk/example1
askuric Jun 18, 2022
6e2ad88
Merge branch 'dev' of github.com:askuric/Arduino-FOC into dev
askuric Jun 14, 2022
3dcf236
Correct the commands label to "target velocity"
yuyangwang-git Jun 19, 2022
55027df
Merge pull request #194 from yuyangwang-git/dev
runger1101001 Jun 20, 2022
ab8d239
Merge pull request #195 from runger1101001/velocity_change
runger1101001 Jun 23, 2022
4767385
Create CITATION.cff
askuric Jun 26, 2022
a56ef53
Update CITATION.cff
askuric Jun 26, 2022
b065098
bgc pnum chnaged
askuric Jun 28, 2022
dffb330
Merge branch 'dev' of github.com:askuric/Arduino-FOC into dev
askuric Jun 28, 2022
211825b
bgc pnum chnaged url forgotten
askuric Jun 28, 2022
a38db58
revert changes bg431
askuric Jun 28, 2022
d217ef0
bg431 opamp flags
askuric Jun 28, 2022
2e6901a
bg431 schetch specified for build flags
askuric Jun 28, 2022
d19da87
a try to fix the stm version to 2.2.0
askuric Jun 28, 2022
11a2f12
added build flags to CI steps
askuric Jun 28, 2022
2cf533b
Merge branch 'dev'
askuric Jun 28, 2022
64d8fac
Update README.md
askuric Jun 30, 2022
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
13 changes: 12 additions & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
arduino-boards-fqbn:
- arduino:avr:uno # arudino uno
- arduino:sam:arduino_due_x # arduino due
- arduino:avr:mega # arduino mega2650
- arduino:avr:leonardo # arduino leonardo
- arduino:samd:nano_33_iot # samd21
- adafruit:samd:adafruit_metro_m4 # samd51
- esp32:esp32:esp32doit-devkit-v1 # esp32
Expand All @@ -29,6 +31,12 @@ jobs:
- arduino-boards-fqbn: arduino:sam:arduino_due_x # arduino due - one full example
sketch-names: single_full_control_example.ino

- arduino-boards-fqbn: arduino:avr:leonardo # arduino leonardo - one full example
sketch-names: open_loop_position_example.ino

- arduino-boards-fqbn: arduino:avr:mega # arduino mega2660 - one full example
sketch-names: single_full_control_example.ino

- arduino-boards-fqbn: arduino:samd:nano_33_iot # samd21
sketch-names: nano33IoT_velocity_control.ino, smartstepper_control.ino

Expand All @@ -54,6 +62,9 @@ jobs:
- arduino-boards-fqbn: STMicroelectronics:stm32:Disco:pnum=B_G431B_ESC1 # B-G431-ESC1
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
sketch-names: B_G431B_ESC1.ino
build-properties:
B_G431B_ESC1:
-DHAL_OPAMP_MODULE_ENABLED

- arduino-boards-fqbn: STMicroelectronics:stm32:GenF4:pnum=GENERIC_F405RGTX # stm32f405 - odrive
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
Expand All @@ -63,7 +74,6 @@ jobs:
platform-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
sketch-names: single_full_control_example.ino, stm32_spi_alt_example.ino, sdouble_full_control_example.ino



# Do not cancel all jobs / architectures if one job fails
fail-fast: false
Expand All @@ -78,3 +88,4 @@ jobs:
platform-url: ${{ matrix.platform-url }}
sketch-names: ${{ matrix.sketch-names }}
sketches-exclude: ${{ matrix.sketches-exclude }}
build-properties: ${{ toJson(matrix.build-properties) }}
48 changes: 48 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cff-version: 1.0.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Skuric"
given-names: "Antun"
orcid: "https://orcid.org/0000-0002-3323-4482"
- family-names: "Bank"
given-names: "Hasan Sinan"
orcid: "https://orcid.org/0000-0002-0626-2664"
- family-names: "Unger"
given-names: "Richard"
- family-names: "Williams"
given-names: "Owen"
- family-names: "González-Reyes"
given-names: "David"
orcid: "https://orcid.org/0000-0002-1535-3007"
title: "SimpleFOC: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors"
version: 2.2.2
doi: 10.21105/joss.04232
date-released: 2022-06-26
url: "https://github.com/simplefoc/Arduino-FOC"

preferred-citation:
type: article
authors:
- family-names: "Skuric"
given-names: "Antun"
orcid: "https://orcid.org/0000-0002-3323-4482"
- family-names: "Bank"
given-names: "Hasan Sinan"
orcid: "https://orcid.org/0000-0002-0626-2664"
- family-names: "Unger"
given-names: "Richard"
- family-names: "Williams"
given-names: "Owen"
- family-names: "González-Reyes"
given-names: "David"
orcid: "https://orcid.org/0000-0002-1535-3007"
doi: "10.21105/joss.04232"
journal: "Journal of Open Source Software"
url: "https://doi.org/10.21105/joss.04232"
month: 6
start: 4232 # First page number
end: 4232 # Last page number
title: "SimpleFOC: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors"
volume: 7
issue: 74
year: 2022
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ Therefore this is an attempt to:
- *Medium-power* BLDC driver (<30Amps): [Arduino <span class="simple">Simple<b>FOC</b>PowerShield</span> ](https://github.com/simplefoc/Arduino-SimpleFOC-PowerShield).
- See also [@byDagor](https://github.com/byDagor)'s *fully-integrated* ESP32 based board: [Dagor Brushless Controller](https://github.com/byDagor/Dagor-Brushless-Controller)


<h3>NEWS 📢: SimpleFOClibrary has been published in the Journal of Open Source Software</h3>
<p>
<b>SimpleFOC</b>: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors.<br>
A. Skuric, HS. Bank, R. Unger, O. Williams, D. González-Reyes<br>
Journal of Open Source Software, 7(74), 4232, https://doi.org/10.21105/joss.04232
</p>

<blockquote class="info">
<p class="heading">NEW RELEASE 📢: <span class="simple">Simple<span class="foc">FOC</span>library</span> v2.2.2 <a href="https://github.com/simplefoc/Arduino-FOC/releases/tag/v2.2.2">see release</a></p>
Expand Down Expand Up @@ -215,6 +220,34 @@ Here are some of the *Simple**FOC**library* and *Simple**FOC**Shield* applicatio
</p>


## Citing the *SimpleFOC*

We are very happy that *Simple**FOC**library* has been used as a component of several research project and has made its way to several scientific papers. We are hoping that this trend is going to continue as the project matures and becomes more robust!
A short resume paper about *Simple**FOC*** has been published in the Journal of Open Source Software:
<p>
<b><i>SimpleFOC</i></b>: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors.<br>
A. Skuric, HS. Bank, R. Unger, O. Williams, D. González-Reyes<br>
Journal of Open Source Software, 7(74), 4232, https://doi.org/10.21105/joss.04232
</p>

If you are interested in citing *Simple**FOC**library* or some other component of *Simple**FOC**project* in your research, we suggest you to cite our paper:

```bib
@article{simplefoc2022,
doi = {10.21105/joss.04232},
url = {https://doi.org/10.21105/joss.04232},
year = {2022},
publisher = {The Open Journal},
volume = {7},
number = {74},
pages = {4232},
author = {Antun Skuric and Hasan Sinan Bank and Richard Unger and Owen Williams and David González-Reyes},
title = {SimpleFOC: A Field Oriented Control (FOC) Library for Controlling Brushless Direct Current (BLDC) and Stepper Motors},
journal = {Journal of Open Source Software}
}

```


## Arduino FOC repo structure
Branch | Description | Status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void setup() {
motor.initFOC();

// add target command T
command.add('T', doTarget, "target voltage");
command.add('T', doTarget, "target velocity");

Serial.println(F("Motor ready."));
Serial.println(F("Set the target velocity using serial terminal:"));
Expand All @@ -103,4 +103,4 @@ void loop() {

// user communication
command.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// HallSensor(int hallA, int hallB , int cpr, int index)
// - hallA, hallB, hallC - HallSensor A, B and C pins
// - pp - pole pairs
HallSensor sensor = HallSensor(2, 3, 4, 11);
HallSensor sensor = HallSensor(2, 3, 4, 14);

// Interrupt routine intialisation
// channel A and B callbacks
Expand Down Expand Up @@ -44,4 +44,5 @@ void loop() {
Serial.print(sensor.getAngle());
Serial.print("\t");
Serial.println(sensor.getVelocity());
delay(100);
}
9 changes: 5 additions & 4 deletions src/BLDCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ class BLDCMotor: public FOCMotor
float Ua, Ub, Uc;//!< Current phase voltages Ua,Ub and Uc set to motor
float Ualpha, Ubeta; //!< Phase voltages U alpha and U beta used for inverse Park and Clarke transform


private:
// FOC methods
/**
/**
* Method using FOC to set Uq to the motor at the optimal angle
* Heart of the FOC algorithm
*
Expand All @@ -82,6 +79,10 @@ class BLDCMotor: public FOCMotor
* @param angle_el current electrical angle of the motor
*/
void setPhaseVoltage(float Uq, float Ud, float angle_el);

private:
// FOC methods

/** Sensor alignment to electrical 0 angle of the motor */
int alignSensor();
/** Current sense and motor phase alignment */
Expand Down
14 changes: 7 additions & 7 deletions src/common/base_classes/Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../foc_utils.h"
#include "../time_utils.h"

// TODO add an init method to make the startup smoother by initializing internal variables to current values rather than 0


void Sensor::update() {
float val = getSensorAngle();
Expand All @@ -18,17 +18,17 @@ void Sensor::update() {
float Sensor::getVelocity() {
// calculate sample time
float Ts = (angle_prev_ts - vel_angle_prev_ts)*1e-6;
// quick fix for strange cases (micros overflow)
if(Ts <= 0) Ts = 1e-3f;
// velocity calculation
float vel = ( (float)(full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
// save variables for future pass
if (Ts<minDeltaT) return velocity; // don't update velocity if deltaT is too small

velocity = ( (float)(full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
vel_angle_prev = angle_prev;
vel_full_rotations = full_rotations;
vel_angle_prev_ts = angle_prev_ts;
return vel;
return velocity;
}



void Sensor::init() {
// initialize all the internal variables of Sensor to ensure a "smooth" startup (without a 'jump' from zero)
getSensorAngle(); // call once
Expand Down
11 changes: 9 additions & 2 deletions src/common/base_classes/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ class Sensor{
* 1 - ecoder with index (with index not found yet)
*/
virtual int needsSearch();

/**
* Minimum time between updates to velocity. If time elapsed is lower than this, the velocity is not updated.
*/
float minDeltaT = 0.000100; // default is 100 microseconds, or 10kHz

protected:
/**
* Get current shaft angle from the sensor hardware, and
Expand All @@ -120,9 +126,10 @@ class Sensor{
virtual void init();

// velocity calculation variables
float angle_prev=0; // result of last call to getSensorAngle(), used for full rotations and velocity
float velocity=0.0f;
float angle_prev=0.0f; // result of last call to getSensorAngle(), used for full rotations and velocity
long angle_prev_ts=0; // timestamp of last call to getAngle, used for velocity
float vel_angle_prev=0; // angle at last call to getVelocity, used for velocity
float vel_angle_prev=0.0f; // angle at last call to getVelocity, used for velocity
long vel_angle_prev_ts=0; // last velocity calculation timestamp
int32_t full_rotations=0; // full rotation tracking
int32_t vel_full_rotations=0; // previous full rotation value for velocity calculation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ uint32_t _timerToRegularTRGO(HardwareTimer* timer){
return ADC_EXTERNALTRIGCONV_T3_TRGO;
#ifdef TIM8 // if defined timer 8
else if(timer->getHandle()->Instance == TIM8)
return ADC_EXTERNALTRIGINJECCONV_T8_TRGO;
return ADC_EXTERNALTRIGCONV_T8_TRGO;
#endif
else
return _TRGO_NOT_AVAILABLE;
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/hardware_specific/atmega2560_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void* _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const
_pinHighFrequency(pin2A);
_pinHighFrequency(pin2B);
GenericDriverParams* params = new GenericDriverParams {
.pins = { pin1A, pin2A, pin2A, pin2B },
.pins = { pin1A, pin1B, pin2A, pin2B },
.pwm_frequency = pwm_frequency
};
return params;
Expand Down Expand Up @@ -146,8 +146,8 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
ret_flag += _configureComplementaryPair(pinC_h, pinC_l);
if (ret_flag!=0) return SIMPLEFOC_DRIVER_INIT_FAILED;
GenericDriverParams* params = new GenericDriverParams {
.pins = { pinA_h,, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l },
.pwm_frequency = pwm_frequency
.pins = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l },
.pwm_frequency = pwm_frequency,
.dead_zone = dead_zone
};
return params;
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/hardware_specific/atmega328_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void* _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const
_pinHighFrequency(pin2A);
_pinHighFrequency(pin2B);
GenericDriverParams* params = new GenericDriverParams {
.pins = { pin1A, pin2A, pin2A, pin2B },
.pins = { pin1A, pin1B, pin2A, pin2B },
.pwm_frequency = pwm_frequency
};
return params;
Expand Down Expand Up @@ -170,4 +170,4 @@ void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params){
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
}

#endif
#endif
6 changes: 3 additions & 3 deletions src/drivers/hardware_specific/atmega32u4_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void* _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const
_pinHighFrequency(pin2A);
_pinHighFrequency(pin2B);
GenericDriverParams* params = new GenericDriverParams {
.pins = { pin1A, pin2A, pin2A, pin2B },
.pins = { pin1A, pin1B, pin2A, pin2B },
.pwm_frequency = pwm_frequency
};
return params;
Expand Down Expand Up @@ -158,8 +158,8 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
ret_flag += _configureComplementaryPair(pinC_h, pinC_l);
if (ret_flag!=0) return SIMPLEFOC_DRIVER_INIT_FAILED;
GenericDriverParams* params = new GenericDriverParams {
.pins = { pinA_h,, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l },
.pwm_frequency = pwm_frequency
.pins = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l },
.pwm_frequency = pwm_frequency,
.dead_zone = dead_zone
};
return params;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/hardware_specific/esp8266_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const in
_setHighFrequency(pwm_frequency, pinC);
_setHighFrequency(pwm_frequency, pinD);
GenericDriverParams* params = new GenericDriverParams {
.pins = { pin1A, pin2A, pin2A, pin2B },
.pins = { pin1A, pin1B, pin2A, pin2B },
.pwm_frequency = pwm_frequency
};
return params;
Expand Down