Skip to content

Commit ddc6d04

Browse files
committed
commander add loop times
1 parent d283f2a commit ddc6d04

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

src/BLDCMotor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ int BLDCMotor::absoluteZeroSearch() {
335335
// Iterative function looping FOC algorithm, setting Uq on the Motor
336336
// The faster it can be run the better
337337
void BLDCMotor::loopFOC() {
338+
// update loop time measurement
339+
updateLoopTime();
340+
338341
// update sensor - do this even in open-loop mode, as user may be switching between modes and we could lose track
339342
// of full rotations otherwise.
340343
if (sensor) sensor->update();

src/HybridStepperMotor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ int HybridStepperMotor::absoluteZeroSearch()
313313
// Iterative function looping FOC algorithm, setting Uq on the Motor
314314
// The faster it can be run the better
315315
void HybridStepperMotor::loopFOC() {
316+
// update loop time measurement
317+
updateLoopTime();
316318

317319
// update sensor - do this even in open-loop mode, as user may be switching between modes and we could lose track
318320
// of full rotations otherwise.

src/StepperMotor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ int StepperMotor::absoluteZeroSearch() {
294294
// Iterative function looping FOC algorithm, setting Uq on the Motor
295295
// The faster it can be run the better
296296
void StepperMotor::loopFOC() {
297+
// update loop time measurement
298+
updateLoopTime();
297299

298300
// update sensor - do this even in open-loop mode, as user may be switching between modes and we could lose track
299301
// of full rotations otherwise.

src/common/base_classes/FOCMotor.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,23 @@ class FOCMotor
256256

257257
// monitoring functions
258258
Print* monitor_port; //!< Serial terminal variable if provided
259+
260+
//!< time between two loopFOC executions in microseconds
261+
uint32_t loop_time_us = 0; //!< filtered loop times
262+
// last loop time measurement update
263+
void updateLoopTime() {
264+
uint32_t now = _micros();
265+
last_loop_time_us = now - last_loop_timestamp_us;
266+
loop_time_us = 0.9f * loop_time_us + 0.1f * last_loop_time_us;
267+
last_loop_timestamp_us = now;
268+
}
269+
259270
private:
260271
// monitor counting variable
261272
unsigned int monitor_cnt = 0 ; //!< counting variable
273+
274+
uint32_t last_loop_timestamp_us = 0; //!< timestamp of the last loopFOC execution in microseconds
275+
uint32_t last_loop_time_us = 0; //!< time between two loopFOC executions in microseconds
262276
};
263277

264278

src/communication/Commander.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,23 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
266266
break;
267267
}
268268
break;
269+
case CMD_FOC_PARAMS:
270+
printVerbose(F("FOC | "));
271+
switch (sub_cmd){
272+
case SCMD_LOOPFOC_TIME: // loopFOC execution time
273+
printVerbose(F("loop time: "));
274+
println((int)motor->loop_time_us);
275+
break;
276+
case SCMD_REINIT_FOC:
277+
printVerbose(F("Reinit!"));
278+
motor->initFOC();
279+
println(F("done"));
280+
break;
281+
default:
282+
printError();
283+
break;
284+
}
285+
break;
269286
case CMD_MONITOR: // get current values of the state variables
270287
printVerbose(F("Monitor | "));
271288
switch (sub_cmd){

src/communication/commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define CMD_INDUCTANCE 'I' //!< motor phase inductance
1919
#define CMD_KV_RATING 'K' //!< motor kv rating
2020
#define CMD_PWMMOD 'W' //!< pwm modulation
21+
#define CMD_FOC_PARAMS 'F' //!< time parameters
2122

2223
// commander configuration
2324
#define CMD_SCAN '?' //!< command scaning the network - only for commander
@@ -48,5 +49,7 @@
4849
#define SCMD_PWMMOD_TYPE 'T' //!<< Pwm modulation type
4950
#define SCMD_PWMMOD_CENTER 'C' //!<< Pwm modulation center flag
5051

52+
#define SCMD_LOOPFOC_TIME 'L' //!< loopFOC execution time
53+
#define SCMD_REINIT_FOC 'R' //!< reinitialize FOC
5154

5255
#endif

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
148148
SET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_START_SAR_M);
149149

150150
// wait for conversion
151-
while (GET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DONE_SAR) == 0);
151+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DONE_SAR) == 0);
152152
// read the value
153153
value = GET_PERI_REG_BITS2(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S);
154154
break;

0 commit comments

Comments
 (0)