Skip to content

Commit 0e45c00

Browse files
committed
leonardo remove profiling
1 parent 1676c15 commit 0e45c00

5 files changed

Lines changed: 70 additions & 24 deletions

File tree

src/common/base_classes/FOCMotor.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "../flags.h"
12
#include "FOCMotor.h"
23
#include "../../communication/SimpleFOCDebug.h"
34

@@ -25,9 +26,11 @@ FOCMotor::FOCMotor()
2526

2627
// default target value
2728
target = 0;
29+
#ifndef SIMPLEFOC_NO_PROFILING
2830
profile_state.position = 0;
2931
profile_state.velocity = 0;
3032
profile_state.initialized = false;
33+
#endif
3134
open_loop_velocity = 0;
3235
voltage.d = 0;
3336
voltage.q = 0;
@@ -189,7 +192,7 @@ int FOCMotor::characteriseMotor(float voltage, float correction_factor=1.0f){
189192
unsigned int iterations = 40; // how often the algorithm gets repeated.
190193
unsigned int cycles = 3; // averaged measurements for each iteration
191194
unsigned int risetime_us = 200; // initially short for worst case scenario with low inductance
192-
unsigned int settle_us = 100000; // initially long for worst case scenario with high inductance
195+
uint32_t settle_us = 100000; // initially long for worst case scenario with high inductance
193196

194197
// Pre-rotate the angle to the q-axis (only useful with sensor, else no harm in doing it)
195198
current_electric_angle += 0.5f * _PI;
@@ -421,11 +424,9 @@ float FOCMotor::velocityOpenloop(float target_velocity){
421424
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
422425

423426
// if acceleration limit is set, use it to calculate the target velocity
424-
if (_isset(acceleration_limit) && acceleration_limit > 0.0f) {
427+
if (acceleration_limit > 0.0f) {
425428
// limit the change in velocity to the acceleration limit
426-
float dv_max = acceleration_limit * Ts;
427-
float dv = _constrain(target_velocity - open_loop_velocity, -dv_max, dv_max);
428-
open_loop_velocity += dv;
429+
open_loop_velocity += _constrain(target_velocity - open_loop_velocity, -acceleration_limit * Ts, acceleration_limit * Ts);
429430
} else {
430431
open_loop_velocity = target_velocity;
431432
}
@@ -454,34 +455,31 @@ float FOCMotor::angleOpenloop(float target_angle){
454455
// calculate the sample time from last call
455456
float Ts = (now_us - open_loop_timestamp) * 1e-6f;
456457
// quick fix for strange cases (micros overflow + timestamp not defined)
457-
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
458+
if(Ts <= 0.0f || Ts > 0.5f) Ts = 1e-3f;
458459

459-
float err = target_angle - shaft_angle;
460-
float target_velocity = 0.0f;
461460
// if acceleration limit is set, use it to calculate the target velocity
462461
if (acceleration_limit > 0.0f) {
463462
// calculate the necessary velocity to stop at the target angle with the given acceleration limit
464-
float stop_vel = sqrtf(2.0f * acceleration_limit * fabsf(err));
463+
float stop_vel = sqrtf(2.0f * acceleration_limit * fabsf(target_angle - shaft_angle));
465464
// limit the target velocity to the velocity limit
466-
target_velocity = _sign(err) * _constrain(stop_vel, 0.0f, velocity_limit);
465+
float target_velocity = _sign(target_angle - shaft_angle) * _constrain(stop_vel, 0.0f, velocity_limit);
467466

468467
// limit the change in velocity to the acceleration limit
469-
float dv_max = acceleration_limit * Ts;
470-
float dv = _constrain(target_velocity - open_loop_velocity, -dv_max, dv_max);
471-
472-
// update the open loop velocity
473-
open_loop_velocity += dv;
468+
open_loop_velocity += _constrain(target_velocity - open_loop_velocity, -acceleration_limit * Ts, acceleration_limit * Ts);
474469

475470
// integrate the open loop velocity to get the new shaft angle
476471
float step = open_loop_velocity * Ts;
477-
if ((fabsf(err) <= fabsf(step)) || (fabsf(err) < 1e-6f && fabsf(open_loop_velocity) <= dv_max)) {
472+
if ((fabsf(target_angle - shaft_angle) <= fabsf(step)) ||
473+
(fabsf(target_angle - shaft_angle) < 1e-6f &&
474+
fabsf(open_loop_velocity) <= (acceleration_limit * Ts))) {
478475
shaft_angle = target_angle;
479476
open_loop_velocity = 0.0f;
480477
} else {
481478
shaft_angle += step;
482479
}
483480
shaft_velocity = open_loop_velocity;
484-
} else {
481+
}
482+
else {
485483
// if no acceleration limit is set, use the velocity limit to calculate the target velocity
486484
// calculate the necessary angle to move from current position towards target angle
487485
// with maximal velocity (velocity_limit)
@@ -599,13 +597,15 @@ void FOCMotor::updateMotionControlType(MotionControlType new_motion_controller)
599597
break;
600598
}
601599

600+
#ifndef SIMPLEFOC_NO_PROFILING
602601
if (new_motion_controller == MotionControlType::angle_profile) {
603602
trajectoryResetTrapezoidal(profile_state, shaft_angle, 0.0f, target);
604603
profile_state.initialized = false;
605604
} else if (controller == MotionControlType::angle_profile) {
606605
profile_state.velocity = 0.0f;
607606
profile_state.initialized = false;
608607
}
608+
#endif
609609

610610
if (new_motion_controller == MotionControlType::angle_openloop || new_motion_controller == MotionControlType::velocity_openloop) {
611611
open_loop_velocity = 0.0f;
@@ -813,7 +813,9 @@ void FOCMotor::move(float new_target) {
813813
current_sp = P_angle(shaft_angle_sp - LPF_angle(shaft_angle));
814814
break;
815815
}
816-
case MotionControlType::angle_profile: {
816+
case MotionControlType::angle_profile:
817+
#ifndef SIMPLEFOC_NO_PROFILING
818+
{
817819
// TODO sensor precision: this calculation is not numerically precise. The target value cannot express precise positions when
818820
// the angles are large. This results in not being able to command small changes at high position values.
819821
// to solve this, the delta-angle has to be calculated in a numerically precise way.
@@ -853,6 +855,7 @@ void FOCMotor::move(float new_target) {
853855
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity);
854856
break;
855857
}
858+
#endif
856859
case MotionControlType::angle:
857860
// TODO sensor precision: this calculation is not numerically precise. The target value cannot express precise positions when
858861
// the angles are large. This results in not being able to command small changes at high position values.

src/common/base_classes/FOCMotor.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
#include "Sensor.h"
66
#include "CurrentSense.h"
77

8+
#include "../flags.h"
9+
10+
811
#include "../time_utils.h"
912
#include "../foc_utils.h"
1013
#include "../defaults.h"
1114
#include "../pid.h"
1215
#include "../lowpass_filter.h"
16+
#ifndef SIMPLEFOC_NO_PROFILING
1317
#include "../trajectory.h"
18+
#endif
1419

1520
#define MOT_ERR "ERR-MOT:"
1621
#define MOT_WARN "WARN-MOT:"
@@ -55,7 +60,9 @@ enum MotionControlType : uint8_t {
5560
angle_openloop = 0x04,
5661
angle_nocascade = 0x05, //!< Position/angle motion control without velocity cascade
5762
custom = 0x06, //!< Custom control method - control method added by user
63+
//#ifndef SIMPLEFOC_NO_PROFILING
5864
angle_profile = 0x07 //!< Position/angle motion control with trapezoidal profile
65+
//#endif
5966
};
6067

6168
/**
@@ -452,9 +459,11 @@ class FOCMotor
452459
uint32_t open_loop_timestamp;
453460
float open_loop_velocity = 0.0f;
454461

462+
#ifndef SIMPLEFOC_NO_PROFILING
455463
// angle profile state
456-
TrapezoidalProfileState profile_state;
457-
464+
TrapezoidalProfileState 2;
465+
#endif
466+
458467
// function pointer for custom control method
459468
float (*customMotionControlCallback)(FOCMotor* motor) = nullptr;
460469

src/common/flags.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef SIMPLEFOC_FLAGS_H
2+
#define SIMPLEFOC_FLAGS_H
3+
4+
5+
#include "Arduino.h"
6+
#if defined(__AVR_ATmega32U4__) && !defined(SIMPLEFOC_NO_PROFILING)
7+
#define SIMPLEFOC_NO_PROFILING
8+
#endif
9+
10+
#endif

src/common/trajectory.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "trajectory.h"
2-
3-
// for more information about the trapezoidal profile, see https://en.wikipedia.org/wiki/Trapezoidal_velocity_profile
2+
#ifndef SIMPLEFOC_NO_PROFILING
3+
// for more information about the trapezoidal profile, see https://mechatronics.studio/blog/bang-bang-motion-profile/
4+
// and the main motivation :D - https://www.youtube.com/watch?v=qYJpl7SNoww
45

56
// Resets the trapezoidal profile state to the current position and velocity,
67
// and sets the target position.
@@ -12,6 +13,24 @@ void trajectoryResetTrapezoidal(TrapezoidalProfileState& state,
1213
state.velocity = current_velocity;
1314
state.initialized = true;
1415
}
16+
17+
#ifdef __AVR_ATmega32U4__
18+
// For AVR ATmega32U4 platforms, we provide a simplified version of the trapezoidal profile step function
19+
TrapezoidalProfileOutput trajectoryStepTrapezoidal(TrapezoidalProfileState& state,
20+
float current_position,
21+
float current_velocity,
22+
float dt,
23+
float target_position,
24+
float velocity_limit,
25+
float acceleration_limit) {
26+
TrapezoidalProfileOutput out;
27+
out.position = target_position;
28+
out.velocity = 0.0f;
29+
return out;
30+
}
31+
#else
32+
// For non-AVR platforms, we can use the standard library functions for absolute value and
33+
1534
// Bang-bang acceleration control with trapezoidal velocity profile
1635
TrapezoidalProfileOutput trajectoryStepTrapezoidal(TrapezoidalProfileState& state,
1736
float current_position,
@@ -100,4 +119,7 @@ TrapezoidalProfileOutput trajectoryStepTrapezoidal(TrapezoidalProfileState& stat
100119
out.position = state.position;
101120
out.velocity = state.velocity;
102121
return out;
103-
}
122+
}
123+
124+
#endif
125+
#endif

src/common/trajectory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef TRAJECTORY_H
22
#define TRAJECTORY_H
33

4+
#include "flags.h"
45
#include "foc_utils.h"
5-
6+
#ifndef SIMPLEFOC_NO_PROFILING
67
/**
78
* State of a trapezoidal motion profile
89
*/
@@ -55,4 +56,5 @@ TrapezoidalProfileOutput trajectoryStepTrapezoidal(TrapezoidalProfileState& stat
5556
float velocity_limit,
5657
float acceleration_limit);
5758

59+
#endif
5860
#endif

0 commit comments

Comments
 (0)