Skip to content

Commit 0542fb7

Browse files
committed
Replace double literals and fns with floats.
In all cases, these literals are intended to be used in floating point operations. Floating point acceleration is much more common on devices where SimpleFOC runs. Even where no acceleration is present, soft floats are universally more efficient than soft doubles. Additionally, fix several cases where fabs or flog are used instead of fabsf or flogf.
1 parent 7f9c45f commit 0542fb7

20 files changed

Lines changed: 78 additions & 78 deletions

File tree

src/common/base_classes/CurrentSense.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver, bool m
202202
bool phases_inverted = 0;
203203

204204
float zero = 0;
205-
if(modulation_centered) zero = driver->voltage_limit/2.0;
205+
if(modulation_centered) zero = driver->voltage_limit/2.0f;
206206

207207
// set phase A active and phases B and C down
208208
// 300 ms of ramping
209209
for(int i=0; i < 100; i++){
210-
bldc_driver->setPwm(voltage/100.0*((float)i)+zero , zero, zero);
210+
bldc_driver->setPwm(voltage/100.0f*((float)i)+zero , zero, zero);
211211
_delay(3);
212212
}
213213
_delay(500);
@@ -318,7 +318,7 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver, bool m
318318
// set phase B active and phases A and C down
319319
// 300 ms of ramping
320320
for(int i=0; i < 100; i++){
321-
bldc_driver->setPwm(zero, voltage/100.0*((float)i)+zero, zero);
321+
bldc_driver->setPwm(zero, voltage/100.0f*((float)i)+zero, zero);
322322
_delay(3);
323323
}
324324
_delay(500);
@@ -430,7 +430,7 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
430430
// set phase A active and phases B down
431431
// ramp 300ms
432432
for(int i=0; i < 100; i++){
433-
stepper_driver->setPwm(voltage/100.0*((float)i), 0);
433+
stepper_driver->setPwm(voltage/100.0f*((float)i), 0);
434434
_delay(3);
435435
}
436436
_delay(500);
@@ -465,7 +465,7 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
465465
// set phase B active and phases A down
466466
// ramp 300ms
467467
for(int i=0; i < 100; i++){
468-
stepper_driver->setPwm(0, voltage/100.0*((float)i));
468+
stepper_driver->setPwm(0, voltage/100.0f*((float)i));
469469
_delay(3);
470470
}
471471
_delay(500);
@@ -511,7 +511,7 @@ int CurrentSense::alignHybridDriver(float voltage, BLDCDriver* bldc_driver, bool
511511
// set phase A active and phases B active, and C down
512512
// ramp 300ms
513513
for(int i=0; i < 100; i++){
514-
bldc_driver->setPwm(voltage/100.0*((float)i), voltage/100.0*((float)i), 0);
514+
bldc_driver->setPwm(voltage/100.0f*((float)i), voltage/100.0f*((float)i), 0);
515515
_delay(3);
516516
}
517517
_delay(500);
@@ -601,7 +601,7 @@ int CurrentSense::alignHybridDriver(float voltage, BLDCDriver* bldc_driver, bool
601601
// set phase A active and phases B down
602602
// ramp 300ms
603603
for(int i=0; i < 100; i++){
604-
bldc_driver->setPwm(voltage/100.0*((float)i), 0, 0);
604+
bldc_driver->setPwm(voltage/100.0f*((float)i), 0, 0);
605605
_delay(3);
606606
}
607607
_delay(500);
@@ -682,7 +682,7 @@ int CurrentSense::alignHybridDriver(float voltage, BLDCDriver* bldc_driver, bool
682682
// set phase B active and phases A down
683683
// ramp 300ms
684684
for(int i=0; i < 100; i++){
685-
bldc_driver->setPwm(0, voltage/100.0*((float)i), 0);
685+
bldc_driver->setPwm(0, voltage/100.0f*((float)i), 0);
686686
_delay(3);
687687
}
688688
_delay(500);

src/common/base_classes/FOCMotor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
122122
// 300 ms of ramping
123123
current_electric_angle = electricalAngle();
124124
for(int i=0; i < 100; i++){
125-
setPhaseVoltage(0, voltage/100.0*((float)i), current_electric_angle);
125+
setPhaseVoltage(0, voltage/100.0f*((float)i), current_electric_angle);
126126
_delay(3);
127127
}
128128
_delay(10);
@@ -205,7 +205,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
205205
continue;
206206
}
207207

208-
inductanced += fabsf(- (resistance * dt) / log((voltage - resistance * (l_currents.d - zerocurrent.d)) / voltage))/correction_factor;
208+
inductanced += fabsf(- (resistance * dt) / logf((voltage - resistance * (l_currents.d - zerocurrent.d)) / voltage))/correction_factor;
209209

210210
qcurrent+= l_currents.q - zerocurrent.q; // average the measured currents
211211
dcurrent+= l_currents.d - zerocurrent.d;
@@ -217,7 +217,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
217217

218218

219219
inductanced /= cycles;
220-
Ltemp = i < 2 ? inductanced : Ltemp * 0.6 + inductanced * 0.4;
220+
Ltemp = i < 2 ? inductanced : Ltemp * 0.6f + inductanced * 0.4f;
221221

222222
float timeconstant = fabsf(Ltemp / resistance); // Timeconstant of an RL circuit (L/R)
223223
// SIMPLEFOC_MOTOR_DEBUG("Estimated time constant in us: ", 1000000.0f * timeconstant);
@@ -254,7 +254,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
254254
// Average the d-axis angle further for calculating the electrical zero later
255255
if (axis)
256256
{
257-
d_electrical_angle = i < 2 ? current_electric_angle : d_electrical_angle * 0.9 + current_electric_angle * 0.1;
257+
d_electrical_angle = i < 2 ? current_electric_angle : d_electrical_angle * 0.9f + current_electric_angle * 0.1f;
258258
}
259259

260260
}
@@ -883,7 +883,7 @@ int FOCMotor::alignSensor() {
883883
// setPhaseVoltage(0, 0, 0);
884884
_delay(200);
885885
// determine the direction the sensor moved
886-
float moved = fabs(mid_angle - end_angle);
886+
float moved = fabsf(mid_angle - end_angle);
887887
if (moved<MIN_ANGLE_DETECT_MOVEMENT) { // minimum angle to detect movement
888888
SIMPLEFOC_MOTOR_ERROR("Failed to notice movement");
889889
return 0; // failed calibration
@@ -895,7 +895,7 @@ int FOCMotor::alignSensor() {
895895
sensor_direction = Direction::CW;
896896
}
897897
// check pole pair number
898-
pp_check_result = !(fabs(moved*pole_pairs - _2PI) > 0.5f); // 0.5f is arbitrary number it can be lower or higher!
898+
pp_check_result = !(fabsf(moved*pole_pairs - _2PI) > 0.5f); // 0.5f is arbitrary number it can be lower or higher!
899899
if( pp_check_result==false ) {
900900
SIMPLEFOC_MOTOR_WARN("PP check: fail - est. pp: ", _2PI/moved);
901901
} else {

src/common/base_classes/FOCMotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class FOCMotor
260260
PIDController PID_velocity{DEF_PID_VEL_P,DEF_PID_VEL_I,DEF_PID_VEL_D,DEF_PID_VEL_RAMP,DEF_PID_VEL_LIMIT};//!< parameter determining the velocity PID configuration
261261
PIDController P_angle{DEF_P_ANGLE_P,0,0,0,DEF_VEL_LIM}; //!< parameter determining the position PID configuration
262262
LowPassFilter LPF_velocity{DEF_VEL_FILTER_Tf};//!< parameter determining the velocity Low pass filter configuration
263-
LowPassFilter LPF_angle{0.0};//!< parameter determining the angle low pass filter configuration
263+
LowPassFilter LPF_angle{0.0f};//!< parameter determining the angle low pass filter configuration
264264
unsigned int motion_downsample = DEF_MOTION_DOWNSMAPLE; //!< parameter defining the ratio of downsampling for move commad
265265
unsigned int motion_cnt = 0; //!< counting variable for downsampling for move commad
266266

src/common/base_classes/Sensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Sensor{
106106
/**
107107
* Minimum time between updates to velocity. If time elapsed is lower than this, the velocity is not updated.
108108
*/
109-
float min_elapsed_time = 0.000100; // default is 100 microseconds, or 10kHz
109+
float min_elapsed_time = 0.000100f; // default is 100 microseconds, or 10kHz
110110

111111
protected:
112112
/**

src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void* IRAM_ATTR _driverSyncLowSide(void* driver_params, void* cs_params){
187187
if (cs->pretrig_comparator){
188188
// Calculate pwm duty cycle ticks for pre-trigger channel
189189
// TODO: verify the timing it seems to be correct between 15 and 20kHz (but needs better testing)
190-
uint32_t pwm_duty_cycle = p->mcpwm_period * (0.75 - ((float)p->pwm_frequency*SIMPLEFOC_CS_PRETRIGGER_US)/1e6/2.0);
190+
uint32_t pwm_duty_cycle = p->mcpwm_period * (0.75f - ((float)p->pwm_frequency*SIMPLEFOC_CS_PRETRIGGER_US)/1e6f/2.0f);
191191
// set up the comparator duty cycle
192192
CHECK_CS_ERR(mcpwm_comparator_set_compare_value((mcpwm_cmpr_handle_t)cs->pretrig_comparator, pwm_duty_cycle),
193193
"Failed to set pretrigger compare value");

src/current_sense/hardware_specific/generic_mcu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ __attribute__((weak)) void* _configureADCInline(const void* driver_params, cons
2626
// function reading an ADC value and returning the read voltage
2727
__attribute__((weak)) float _readADCVoltageLowSide(const int pinA, const void* cs_params){
2828
SIMPLEFOC_DEBUG("ERR: Low-side cs not supported!");
29-
return 0.0;
29+
return 0.0f;
3030
}
3131

3232
// Configure low side for generic mcu

src/current_sense/hardware_specific/samd/samd21_mcu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ float _readADCVoltageLowSide(const int pin, const void* cs_params) {
161161
i++;
162162
}
163163

164-
return 0.0; // pin not available
164+
return 0.0f; // pin not available
165165
}
166166

167167
void* _driverSyncLowSide(void* driver_params, void* cs_params) {

src/current_sense/hardware_specific/teensy/teensy4_mcu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void adc_etc_init(int pin1, int pin2, int pin3=NOT_SET) {
143143
// function reading an ADC value and returning the read voltage
144144
float _readADCVoltageLowSide(const int pinA, const void* cs_params){
145145

146-
if(!_isset(pinA)) return 0.0; // if the pin is not set return 0
146+
if(!_isset(pinA)) return 0.0f; // if the pin is not set return 0
147147
GenericCurrentSenseParams* params = (GenericCurrentSenseParams*) cs_params;
148148
float adc_voltage_conv = params->adc_voltage_conv;
149149
if (pinA == params->pins[0]) {
@@ -153,7 +153,7 @@ float _readADCVoltageLowSide(const int pinA, const void* cs_params){
153153
}else if (pinA == params->pins[2]) {
154154
return val2 * adc_voltage_conv;
155155
}
156-
return 0.0;
156+
return 0.0f;
157157
}
158158

159159
// Configure low side for generic mcu

src/drivers/hardware_specific/atmega/atmega2560_mcu.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void _pinHighFrequency(const int pin, const long frequency){
1717
bool high_fq = false;
1818
// set 32kHz frequency if requested freq is higher than the middle of the range (14kHz)
1919
// else set the 4kHz
20-
if(frequency >= 0.5*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
20+
if(frequency >= 0.5f*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
2121
// High PWM frequency
2222
// https://sites.google.com/site/qeewiki/books/avr-guide/timers-on-the-ATmega2560
2323
// https://forum.arduino.cc/index.php?topic=72092.0
@@ -170,7 +170,7 @@ int _configureComplementaryPair(const int pinH,const int pinL, long frequency) {
170170
bool high_fq = false;
171171
// set 32kHz frequency if requested freq is higher than the middle of the range (14kHz)
172172
// else set the 4kHz
173-
if(frequency >= 0.5*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
173+
if(frequency >= 0.5f*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
174174

175175
// configure pin pairs
176176
if( (pinH == 4 && pinL == 13 ) || (pinH == 13 && pinL == 4 ) ){
@@ -274,9 +274,9 @@ void _setPwmPair(int pinH, int pinL, float val, int dead_time, PhaseState ps)
274274
// - hardware specific
275275
// supports Arduino/ATmega328
276276
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params){
277-
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[0]);
278-
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[1]);
279-
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[2]);
277+
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[0]);
278+
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[1]);
279+
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[2]);
280280
}
281281

282282
#endif

src/drivers/hardware_specific/atmega/atmega328_mcu.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void _pinHighFrequency(const int pin, const long frequency){
1515
bool high_fq = false;
1616
// set 32kHz frequency if requested freq is higher than the middle of the range (14kHz)
1717
// else set the 4kHz
18-
if(frequency >= 0.5*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
18+
if(frequency >= 0.5f*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
1919
// High PWM frequency
2020
// https://www.arxterra.com/9-atmega328p-timers/
2121
if (pin == 5 || pin == 6 ) {
@@ -167,7 +167,7 @@ int _configureComplementaryPair(const int pinH, const int pinL, long frequency)
167167
bool high_fq = false;
168168
// set 32kHz frequency if requested freq is higher than the middle of the range (14kHz)
169169
// else set the 4kHz
170-
if(frequency >= 0.5*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
170+
if(frequency >= 0.5f*(_PWM_FREQUENCY_MAX-_PWM_FREQUENCY_MIN)) high_fq=true;
171171

172172
// configure pins
173173
if( (pinH == 5 && pinL == 6 ) || (pinH == 6 && pinL == 5 ) ){
@@ -251,9 +251,9 @@ void _setPwmPair(int pinH, int pinL, float val, int dead_time, PhaseState ps)
251251
// - hardware specific
252252
// supports Arduino/ATmega328
253253
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params){
254-
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[0]);
255-
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[1]);
256-
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0, phase_state[2]);
254+
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[0]);
255+
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[1]);
256+
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0f, ((GenericDriverParams*)params)->dead_zone*255.0f, phase_state[2]);
257257
}
258258

259259
#endif

0 commit comments

Comments
 (0)