Skip to content

Commit 905eda6

Browse files
committed
Merge branch 'dev' into feat_additional_position_sensor
2 parents 247cae3 + 9608741 commit 905eda6

30 files changed

Lines changed: 288 additions & 240 deletions

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
@@ -140,7 +140,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
140140
// 300 ms of ramping
141141
current_electric_angle = electricalAngle();
142142
for(int i=0; i < 100; i++){
143-
setPhaseVoltage(0, voltage/100.0*((float)i), current_electric_angle);
143+
setPhaseVoltage(0, voltage/100.0f*((float)i), current_electric_angle);
144144
_delay(3);
145145
}
146146
_delay(10);
@@ -225,7 +225,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
225225
continue;
226226
}
227227

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

230230
qcurrent+= l_currents.q - zerocurrent.q; // average the measured currents
231231
dcurrent+= l_currents.d - zerocurrent.d;
@@ -237,7 +237,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
237237

238238

239239
inductanced /= cycles;
240-
Ltemp = i < 2 ? inductanced : Ltemp * 0.6 + inductanced * 0.4;
240+
Ltemp = i < 2 ? inductanced : Ltemp * 0.6f + inductanced * 0.4f;
241241

242242
float timeconstant = fabsf(Ltemp / resistance); // Timeconstant of an RL circuit (L/R)
243243
// SIMPLEFOC_MOTOR_DEBUG("Estimated time constant in us: ", 1000000.0f * timeconstant);
@@ -274,7 +274,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
274274
// Average the d-axis angle further for calculating the electrical zero later
275275
if (axis)
276276
{
277-
d_electrical_angle = i < 2 ? current_electric_angle : d_electrical_angle * 0.9 + current_electric_angle * 0.1;
277+
d_electrical_angle = i < 2 ? current_electric_angle : d_electrical_angle * 0.9f + current_electric_angle * 0.1f;
278278
}
279279

280280
}
@@ -916,7 +916,7 @@ int FOCMotor::alignSensor() {
916916
// setPhaseVoltage(0, 0, 0);
917917
_delay(200);
918918
// determine the direction the sensor moved
919-
float moved = fabs(mid_angle - end_angle);
919+
float moved = fabsf(mid_angle - end_angle);
920920
if (moved<MIN_ANGLE_DETECT_MOVEMENT) { // minimum angle to detect movement
921921
SIMPLEFOC_MOTOR_ERROR("Failed to notice movement");
922922
return 0; // failed calibration
@@ -928,7 +928,7 @@ int FOCMotor::alignSensor() {
928928
sensor_direction = Direction::CW;
929929
}
930930
// check pole pair number
931-
pp_check_result = !(fabs(moved*pole_pairs - _2PI) > 0.5f); // 0.5f is arbitrary number it can be lower or higher!
931+
pp_check_result = !(fabsf(moved*pole_pairs - _2PI) > 0.5f); // 0.5f is arbitrary number it can be lower or higher!
932932
if( pp_check_result==false ) {
933933
SIMPLEFOC_MOTOR_WARN("PP check: fail - est. pp: ", _2PI/moved);
934934
} else {

src/common/base_classes/FOCMotor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ class FOCMotor
230230
float voltage_bemf; //!< estimated backemf voltage (if provided KV constant)
231231
float Ualpha, Ubeta; //!< Phase voltages U alpha and U beta used for inverse Park and Clarke transform
232232

233-
DQCurrent_s feed_forward_current;//!< current d and q current measured
234-
DQVoltage_s feed_forward_voltage;//!< current d and q voltage set to the motor
233+
DQCurrent_s feed_forward_current = {0.0f, 0.0f};//!< current d and q current measured
234+
DQVoltage_s feed_forward_voltage = {0.0f, 0.0f};//!< current d and q voltage set to the motor
235235

236236
// motor configuration parameters
237237
float voltage_sensor_align;//!< sensor and motor align voltage parameter
@@ -271,7 +271,7 @@ class FOCMotor
271271
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
272272
PIDController P_angle{DEF_P_ANGLE_P,0,0,0,DEF_VEL_LIM}; //!< parameter determining the position PID configuration
273273
LowPassFilter LPF_velocity{DEF_VEL_FILTER_Tf};//!< parameter determining the velocity Low pass filter configuration
274-
LowPassFilter LPF_angle{0.0};//!< parameter determining the angle low pass filter configuration
274+
LowPassFilter LPF_angle{0.0f};//!< parameter determining the angle low pass filter configuration
275275
unsigned int motion_downsample = DEF_MOTION_DOWNSMAPLE; //!< parameter defining the ratio of downsampling for move commad
276276
unsigned int motion_cnt = 0; //!< counting variable for downsampling for move commad
277277

src/common/base_classes/Sensor.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,25 @@ float Sensor::getVelocity() {
2929
}
3030
if (Ts < min_elapsed_time) return velocity; // don't update velocity if deltaT is too small
3131

32-
float current_angle = 0.0f;
33-
float prev_angle = 0.0f;
34-
// Avoid floating point precision loss for large full_rotations
35-
// this is likely optional
36-
if (full_rotations == vel_full_rotations) {
37-
current_angle = angle_prev;
38-
prev_angle = vel_angle_prev;
39-
} else {
40-
current_angle = (float) full_rotations * _2PI + angle_prev;
41-
prev_angle = (float) vel_full_rotations * _2PI + vel_angle_prev;
32+
// Calculate change in angle. Handles `full_rotations` integer wrap-arounds,
33+
// and avoids float precision loss issues by keeping numbers small.
34+
float delta_angle = angle_prev - vel_angle_prev;
35+
const int32_t delta_full_rotations = full_rotations - vel_full_rotations;
36+
if (delta_full_rotations) {
37+
delta_angle += delta_full_rotations * _2PI;
4238
}
43-
const float delta_angle = current_angle - prev_angle;
4439

4540
// floating point equality checks are bad, so instead we check that the angle change is very small
4641
if (fabsf(delta_angle) > 1e-8f) {
4742
velocity = delta_angle / Ts;
48-
49-
vel_angle_prev = angle_prev;
50-
vel_full_rotations = full_rotations;
51-
vel_angle_prev_ts = angle_prev_ts;
43+
} else {
44+
velocity = 0.0f;
5245
}
46+
47+
// Always advance the velocity reference sample to avoid stale deltas/time windows.
48+
vel_angle_prev = angle_prev;
49+
vel_full_rotations = full_rotations;
50+
vel_angle_prev_ts = angle_prev_ts;
5351

5452
return velocity;
5553
}

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/common/foc_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ __attribute__((weak)) float _atan2(float y, float x) {
7575

7676
// normalizing radian angle to [0,2PI]
7777
__attribute__((weak)) float _normalizeAngle(float angle){
78-
float a = fmod(angle, _2PI);
78+
float a = fmodf(angle, _2PI);
7979
return a >= 0 ? a : (a + _2PI);
8080
}
8181

src/communication/Commander.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ void Commander::target(FOCMotor* motor, char* user_cmd, char* separator){
582582
float pos, vel, torque;
583583
char* next_value;
584584
switch(motor->controller){
585+
case MotionControlType::custom:
585586
case MotionControlType::angle_nocascade:
586587
case MotionControlType::torque: // setting torque target
587588
torque = atof(strtok (user_cmd, separator));

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) {

0 commit comments

Comments
 (0)