Skip to content

Commit cceec5a

Browse files
committed
initial commit
1 parent 4d117ba commit cceec5a

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

src/BLDCMotor.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ int BLDCMotor::absoluteZeroSearch() {
315315
// Iterative function looping FOC algorithm, setting Uq on the Motor
316316
// The faster it can be run the better
317317
void BLDCMotor::loopFOC() {
318+
float now = _micros();
319+
if (timestamp_prev == 0) timestamp_prev = now; // First time
320+
Ts = ( now - timestamp_prev)*1e-6f;
321+
if (Ts < 0.0f ) Ts = 1e-3f;
322+
timestamp_prev = now;
323+
318324
// update sensor - do this even in open-loop mode, as user may be switching between modes and we could lose track
319325
// of full rotations otherwise.
320326
if (sensor) sensor->update();
@@ -350,11 +356,11 @@ void BLDCMotor::loopFOC() {
350356
// read dq currents
351357
current = current_sense->getFOCCurrents(electrical_angle);
352358
// filter values
353-
current.q = LPF_current_q(current.q);
354-
current.d = LPF_current_d(current.d);
359+
current.q = LPF_current_q(current.q,Ts);
360+
current.d = LPF_current_d(current.d,Ts);
355361
// calculate the phase voltages
356-
voltage.q = PID_current_q(current_sp - current.q);
357-
voltage.d = PID_current_d(-current.d);
362+
voltage.q = PID_current_q(current_sp - current.q,Ts);
363+
voltage.d = PID_current_d(-current.d,Ts);
358364
// d voltage - lag compensation - TODO verify
359365
// if(_isset(phase_inductance)) voltage.d = _constrain( voltage.d - current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
360366
break;

src/common/base_classes/FOCMotor.h

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ class FOCMotor
161161
DQVoltage_s voltage;//!< current d and q voltage set to the motor
162162
DQCurrent_s current;//!< current d and q current measured
163163
float voltage_bemf; //!< estimated backemf voltage (if provided KV constant)
164+
float Ts=0;
165+
float timestamp_prev=0;
164166

165167
// motor configuration parameters
166168
float voltage_sensor_align;//!< sensor and motor align voltage parameter

src/common/lowpass_filter.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ LowPassFilter::LowPassFilter(float time_constant)
88
}
99

1010

11-
float LowPassFilter::operator() (float x)
11+
float LowPassFilter::operator() (float x,float Ts)
1212
{
13-
unsigned long timestamp = _micros();
14-
float dt = (timestamp - timestamp_prev)*1e-6f;
13+
unsigned long timestamp = 0;
14+
15+
if (Ts==0){
16+
timestamp = _micros();
17+
Ts = (timestamp - timestamp_prev)*1e-6f;
18+
if (Ts < 0.0f ) Ts = 1e-3f;
19+
}
1520

16-
if (dt < 0.0f ) dt = 1e-3f;
17-
else if(dt > 0.3f) {
21+
else if(Ts > 0.3f) {
1822
y_prev = x;
1923
timestamp_prev = timestamp;
2024
return x;
2125
}
2226

23-
float alpha = Tf/(Tf + dt);
27+
float alpha = Tf/(Tf + Ts);
2428
float y = alpha*y_prev + (1.0f - alpha)*x;
2529
y_prev = y;
2630
timestamp_prev = timestamp;

src/common/lowpass_filter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LowPassFilter
1717
LowPassFilter(float Tf);
1818
~LowPassFilter() = default;
1919

20-
float operator() (float x);
20+
float operator() (float x,float Ts=0);
2121
float Tf; //!< Low pass filter time constant
2222

2323
protected:

src/common/pid.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ PIDController::PIDController(float P, float I, float D, float ramp, float limit)
1414
}
1515

1616
// PID controller function
17-
float PIDController::operator() (float error){
18-
// calculate the time from the last call
19-
unsigned long timestamp_now = _micros();
20-
float Ts = (timestamp_now - timestamp_prev) * 1e-6f;
21-
// quick fix for strange cases (micros overflow)
22-
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
17+
float PIDController::operator() (float error,float Ts){
18+
unsigned long timestamp_now = 0;
19+
20+
if (Ts == 0){
21+
// calculate the time from the last call
22+
timestamp_now = _micros();
23+
Ts = (timestamp_now - timestamp_prev) * 1e-6f;
24+
// quick fix for strange cases (micros overflow)
25+
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
26+
}
2327

2428
// u(s) = (P + I/s + Ds)e(s)
2529
// Discrete implementations

src/common/pid.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PIDController
2222
PIDController(float P, float I, float D, float ramp, float limit);
2323
~PIDController() = default;
2424

25-
float operator() (float error);
25+
float operator() (float error,float Ts=0);
2626
void reset();
2727

2828
float P; //!< Proportional gain

0 commit comments

Comments
 (0)