Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 39 additions & 27 deletions src/common/base_classes/FOCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ float FOCMotor::shaftVelocity() {
if(!sensor) return shaft_velocity;
return sensor_direction*LPF_velocity(sensor->getVelocity());
}
float FOCMotor::shaftVelocity(float dt) {
// if no sensor linked return previous value ( for open loop )
if(!sensor) return shaft_velocity;
return sensor_direction*LPF_velocity(sensor->getVelocity(), dt);
}

float FOCMotor::electricalAngle(){
// if no sensor linked return previous value ( for open loop )
Expand Down Expand Up @@ -400,17 +405,19 @@ float FOCMotor::velocityOpenloop(float target_velocity){
unsigned long now_us = _micros();
// calculate the sample time from last call
float Ts = (now_us - open_loop_timestamp) * 1e-6f;
// quick fix for strange cases (micros overflow + timestamp not defined)
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
// save timestamp for next call
open_loop_timestamp = now_us;
return velocityOpenloop(target_velocity, Ts);
}

float FOCMotor::velocityOpenloop(float target_velocity, float dt){
if(dt <= 0 || dt > 0.5f) dt = 1e-3f;

// calculate the necessary angle to achieve target velocity
shaft_angle = _normalizeAngle(shaft_angle + target_velocity*Ts);
shaft_angle = _normalizeAngle(shaft_angle + target_velocity*dt);
// for display purposes
shaft_velocity = target_velocity;

// save timestamp for next call
open_loop_timestamp = now_us;

if (torque_controller == TorqueControlType::voltage)
return voltage_limit;
else
Expand All @@ -425,25 +432,27 @@ float FOCMotor::angleOpenloop(float target_angle){
unsigned long now_us = _micros();
// calculate the sample time from last call
float Ts = (now_us - open_loop_timestamp) * 1e-6f;
// quick fix for strange cases (micros overflow + timestamp not defined)
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
// save timestamp for next call
open_loop_timestamp = now_us;
return angleOpenloop(target_angle, Ts);
}

float FOCMotor::angleOpenloop(float target_angle, float dt){
if(dt <= 0 || dt > 0.5f) dt = 1e-3f;

// calculate the necessary angle to move from current position towards target angle
// with maximal velocity (velocity_limit)
// TODO sensor precision: this calculation is not numerically precise. The angle can grow to the point
// where small position changes are no longer captured by the precision of floats
// when the total position is large.
if(abs( target_angle - shaft_angle ) > abs(velocity_limit*Ts)){
shaft_angle += _sign(target_angle - shaft_angle) * abs( velocity_limit )*Ts;
if(abs( target_angle - shaft_angle ) > abs(velocity_limit*dt)){
shaft_angle += _sign(target_angle - shaft_angle) * abs( velocity_limit )*dt;
shaft_velocity = velocity_limit;
}else{
shaft_angle = target_angle;
shaft_velocity = 0;
}

// save timestamp for next call
open_loop_timestamp = now_us;

// use voltage limit or current limit
if (torque_controller == TorqueControlType::voltage)
return voltage_limit;
Expand Down Expand Up @@ -593,6 +602,8 @@ int FOCMotor::tuneCurrentController(float bandwidth) {
void FOCMotor::loopFOC() {
// update loop time measurement
updateLoopFOCTime();

float dt = last_loopfoc_time_us * 1e-6f;

// if initFOC is being executed at the moment, do nothing
if(motor_status == FOCMotorStatus::motor_calibrating) return;
Expand Down Expand Up @@ -626,7 +637,7 @@ void FOCMotor::loopFOC() {
// calculate the back-emf voltage if KV_rating available U_bemf = vel*(1/KV)
if (_isset(KV_rating)) voltage_bemf = estimateBEMF(shaft_velocity);
// filter the value values
current.q = LPF_current_q(current_sp);
current.q = LPF_current_q(current_sp, dt);
// calculate the phase voltage
voltage.q = current.q * phase_resistance + voltage_bemf;
// constrain voltage within limits
Expand All @@ -642,9 +653,9 @@ void FOCMotor::loopFOC() {
// read overall current magnitude
current.q = current_sense->getDCCurrent(electrical_angle);
// filter the value values
current.q = LPF_current_q(current.q);
current.q = LPF_current_q(current.q, dt);
// calculate the phase voltage
voltage.q = PID_current_q(current_sp - current.q) + feed_forward_voltage.q;
voltage.q = PID_current_q(current_sp - current.q, dt) + feed_forward_voltage.q;
// d voltage - lag compensation
if(_isset(axis_inductance.q)) voltage.d = _constrain( -current_sp*shaft_velocity*pole_pairs*axis_inductance.q, -voltage_limit, voltage_limit) + feed_forward_voltage.d;
else voltage.d = feed_forward_voltage.d;
Expand All @@ -656,11 +667,11 @@ void FOCMotor::loopFOC() {
// read dq currents
current = current_sense->getFOCCurrents(electrical_angle);
// filter values
current.q = LPF_current_q(current.q);
current.d = LPF_current_d(current.d);
current.q = LPF_current_q(current.q, dt);
current.d = LPF_current_d(current.d, dt);
// calculate the phase voltages
voltage.q = PID_current_q(current_sp - current.q);
voltage.d = PID_current_d(feed_forward_current.d - current.d);
voltage.q = PID_current_q(current_sp - current.q, dt);
voltage.d = PID_current_d(feed_forward_current.d - current.d, dt);
// d voltage - lag compensation
//if(_isset(axis_inductance.q)) voltage.d = _constrain( voltage.d - current_sp*shaft_velocity*pole_pairs*axis_inductance.q, -voltage_limit, voltage_limit);
// q voltage - cross coupling compensation - TODO verify
Expand Down Expand Up @@ -700,6 +711,7 @@ void FOCMotor::move(float new_target) {
// a specific frequency (or almost)
updateMotionControlTime();

float dt = last_move_time_us * 1e-6f;
// read value even if motor is disabled to keep the monitoring updated
// except for the open loop modes where the values are updated within angle/velocityOpenLoop functions

Expand All @@ -713,7 +725,7 @@ void FOCMotor::move(float new_target) {
// read the values only if the motor is not in open loop
// because in open loop the shaft angle/velocity is updated within angle/velocityOpenLoop functions
shaft_angle = shaftAngle();
shaft_velocity = shaftVelocity();
shaft_velocity = shaftVelocity(dt);
}

// if disabled do nothing
Expand All @@ -733,7 +745,7 @@ void FOCMotor::move(float new_target) {
// angle set point
shaft_angle_sp = target;
// calculate the torque command - sensor precision: this calculation is ok, but based on bad value from previous calculation
current_sp = P_angle(shaft_angle_sp - LPF_angle(shaft_angle));
current_sp = P_angle(shaft_angle_sp - LPF_angle(shaft_angle, dt), dt);
break;
case MotionControlType::angle:
// TODO sensor precision: this calculation is not numerically precise. The target value cannot express precise positions when
Expand All @@ -742,24 +754,24 @@ void FOCMotor::move(float new_target) {
// angle set point
shaft_angle_sp = target;
// calculate velocity set point
shaft_velocity_sp = feed_forward_velocity + P_angle( shaft_angle_sp - LPF_angle(shaft_angle) );
shaft_velocity_sp = feed_forward_velocity + P_angle(shaft_angle_sp - LPF_angle(shaft_angle, dt), dt);
shaft_velocity_sp = _constrain(shaft_velocity_sp, -velocity_limit, velocity_limit);
// calculate the torque command - sensor precision: this calculation is ok, but based on bad value from previous calculation
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity);
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity, dt);
break;
case MotionControlType::velocity:
// velocity set point - sensor precision: this calculation is numerically precise.
shaft_velocity_sp = target;
// calculate the torque command
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity);
current_sp = PID_velocity(shaft_velocity_sp - shaft_velocity, dt);
break;
case MotionControlType::velocity_openloop:
// velocity control in open loop - sensor precision: this calculation is numerically precise.
shaft_velocity_sp = target;
// this function updates the shaft_angle and shaft_velocity
// returns the voltage or current that is to be set to the motor (depending on torque control mode)
// returned values correspond to the voltage_limit and current_limit
current_sp = velocityOpenloop(shaft_velocity_sp);
current_sp = velocityOpenloop(shaft_velocity_sp, dt);
break;
case MotionControlType::angle_openloop:
// angle control in open loop -
Expand All @@ -770,7 +782,7 @@ void FOCMotor::move(float new_target) {
// this function updates the shaft_angle and shaft_velocity
// returns the voltage or current that is to be set to the motor (depending on torque control mode)
// returned values correspond to the voltage_limit and current_limit
current_sp = angleOpenloop(shaft_angle_sp);
current_sp = angleOpenloop(shaft_angle_sp, dt);
break;
case MotionControlType::custom:
// custom control - user provides the function that calculates the current_sp
Expand Down
3 changes: 3 additions & 0 deletions src/common/base_classes/FOCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class FOCMotor
* It implements low pass filtering
*/
float shaftVelocity();
float shaftVelocity(float dt);

/**
* Electrical angle calculation
Expand Down Expand Up @@ -365,13 +366,15 @@ class FOCMotor
* @param target_velocity - rad/s
*/
float velocityOpenloop(float target_velocity);
float velocityOpenloop(float target_velocity, float dt);
/**
* Function (iterative) generating open loop movement towards the target angle
* it uses voltage_limit and velocity_limit variables
*
* @param target_angle - rad
*/
float angleOpenloop(float target_angle);
float angleOpenloop(float target_angle, float dt);


/**
Expand Down
7 changes: 6 additions & 1 deletion src/common/lowpass_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ float LowPassFilter::operator() (float x)
timestamp_prev = timestamp;
}

return operator()(x, dt);
}

float LowPassFilter::operator() (float x, float dt)
{
// calculate the first order filer
float alpha = Tf/(Tf + dt);
float y = alpha*y_prev + (1.0f - alpha)*x;

// save the variables for the future steps
y_prev = y;
return y;
}
}
1 change: 1 addition & 0 deletions src/common/lowpass_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LowPassFilter
~LowPassFilter() = default;

float operator() (float x);
float operator() (float x, float dt);
float Tf; //!< Low pass filter time constant
float Ts; //!< Fixed sampling time (optional default NOT_SET)

Expand Down
6 changes: 6 additions & 0 deletions src/common/pid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ float PIDController::operator() (float error){
timestamp_prev = timestamp_now;
}

return operator()(error, dt);
}


float PIDController::operator() (float error, float dt)
{
// u(s) = (P + I/s + Ds)e(s)
// Discrete implementations
// proportional part
Expand Down
1 change: 1 addition & 0 deletions src/common/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PIDController
~PIDController() = default;

float operator() (float error);
float operator() (float error, float dt);
void reset();

float P; //!< Proportional gain
Expand Down
Loading