Skip to content

Commit 5af61bb

Browse files
committed
Prepared TMC biquad filters
1 parent ecae4db commit 5af61bb

5 files changed

Lines changed: 56 additions & 22 deletions

File tree

Firmware/FFBoard/Inc/Axis.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,7 @@ class Axis : public PersistentStorage, public CommandHandler, public ErrorHandle
177177

178178
const Error outOfBoundsError = Error(ErrorCode::axisOutOfRange,ErrorType::warning,"Axis out of bounds");
179179

180-
const TMC4671PIDConf tmcpids = TMC4671PIDConf({.fluxI = 400,
181-
.fluxP = 400,
182-
.torqueI = 400,
183-
.torqueP = 300,
184-
.velocityI = 0,
185-
.velocityP = 128,
186-
.positionI = 0,
187-
.positionP = 64,
188-
.sequentialPI = true});
180+
189181
TMC4671Limits tmclimits = TMC4671Limits({.pid_torque_flux_ddt = 32767,
190182
.pid_uq_ud = 30000,
191183
.pid_torque_flux = 30000,
@@ -194,13 +186,24 @@ class Axis : public PersistentStorage, public CommandHandler, public ErrorHandle
194186
.pid_pos_low = -2147483647,
195187
.pid_pos_high = 2147483647});
196188

197-
// Lowpass 1KHZ
198-
const TMC4671Biquad fluxbq = TMC4671Biquad({.a1 = -134913,
199-
.a2 = 536735999,
200-
.b0 = 67457,
201-
.b1 = 134913,
202-
.b2 = 67457,
203-
.enable = true});
189+
// Lowpass 500Hz Q 0.7 @ 25khz
190+
const TMC4671Biquad tmcbq_500hz_07q_25k = TMC4671Biquad(
191+
{ .a1 = 979476766,
192+
.a2 = -450370144,
193+
.b0 = 1941073,
194+
.b1 = 3882145,
195+
.b2 = 1941073,
196+
.enable = true});
197+
198+
// Lowpass 1000Hz Q 0.7 @ 25khz
199+
const TMC4671Biquad tmcbq_1000hz_07q_25k = TMC4671Biquad(
200+
{ .a1 = 886773302,
201+
.a2 = -378358476,
202+
.b0 = 7114021,
203+
.b1 = 14228043,
204+
.b2 = 7114021,
205+
.enable = true});
206+
204207

205208

206209
float encoderOffset = 0; // Offset for absolute encoders
@@ -247,7 +250,7 @@ class Axis : public PersistentStorage, public CommandHandler, public ErrorHandle
247250
const biquad_constant_t filterSpeedCst[4] = {{ 30, 55 }, { 60, 55 }, { 120, 55 }, {120, 55}};
248251
const biquad_constant_t filterAccelCst[4] = {{ 40, 30 }, { 55, 30 }, { 70, 30 }, {120, 55}};
249252
const biquad_constant_t filterDamperCst = {60, 55};
250-
uint8_t filterProfileId = 0;
253+
uint8_t filterProfileId = 1; // Default medium (1) as this is the most common encoder resolution and users can go lower or higher if required.
251254
const float filter_f = 1000; // 1khz
252255
const int32_t damperClip = 20000;
253256
uint8_t damperIntensity = 30;

Firmware/FFBoard/Inc/Filters.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ enum class BiquadType : uint8_t {
2626
highshelf
2727
};
2828

29+
class TMC4671Biquad;
2930
class Biquad{
31+
friend TMC4671Biquad;
3032
public:
3133
Biquad();
3234
Biquad(BiquadType type, float Fc, float Q, float peakGainDB);

Firmware/FFBoard/Src/Axis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void Axis::setupTMC4671()
329329
drv->restoreFlash();
330330
tmclimits.pid_torque_flux = getPower();
331331
drv->setLimits(tmclimits);
332-
//drv->setBiquadFlux(fluxbq);
332+
//drv->setBiquadTorque(tmcbq_500hz_07q_25k);
333333
drv->setExternalEncoderAllowed(true);
334334

335335

Firmware/FFBoard/UserExtensions/Inc/TMC4671.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "semaphore.hpp"
2525
#include "OutputPin.h"
2626
#include "cpp_target_config.h"
27+
#include "Filters.h"
2728

2829
#define SPITIMEOUT 500
2930
#define TMC_THREAD_MEM 256
@@ -157,10 +158,10 @@ struct TMC4671MainConfig{
157158
};
158159

159160
struct TMC4671PIDConf{
160-
uint16_t fluxI = 512;
161-
uint16_t fluxP = 512;
162-
uint16_t torqueI = 512;
163-
uint16_t torqueP = 512;
161+
uint16_t fluxI = 800;
162+
uint16_t fluxP = 700;
163+
uint16_t torqueI = 800;
164+
uint16_t torqueP = 700;
164165
uint16_t velocityI = 0;
165166
uint16_t velocityP = 256;
166167
uint16_t positionI = 0;
@@ -257,13 +258,26 @@ struct TMC4671HALLConf{
257258
uint16_t dPhiMax = 10922;
258259
};
259260

261+
// Use TMCL IDE with TMC4671 devkit to generate values
262+
// TODO rewrite as class to allow conversion from biquad
260263
struct TMC4671Biquad{
264+
261265
int32_t a1 = 0;
262266
int32_t a2 = 0;
263267
int32_t b0 = 0;
264268
int32_t b1 = 0;
265269
int32_t b2 = 0;
266270
bool enable = false;
271+
272+
void from_bq(Biquad& bq,bool enable){
273+
// Note: trinamic swapped the naming of b and a from the regular convention in the datasheet
274+
a1 = (int32_t)(bq.b1 * (1 << 29));
275+
a2 = (int32_t)(bq.b2 * (1 << 29));
276+
b0 = (int32_t)(bq.a0 * (1 << 29));
277+
b1 = (int32_t)(bq.a1 * (1 << 29));
278+
b2 = (int32_t)(bq.a2 * (1 << 29));
279+
this->enable = enable;
280+
};
267281
};
268282

269283

Firmware/FFBoard/UserExtensions/Src/TMC4671.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,9 @@ TMC4671Limits TMC4671::getLimits(){
19661966
return curLimits;
19671967
}
19681968

1969+
/**
1970+
* Applies a biquad filter to the flux target
1971+
*/
19691972
void TMC4671::setBiquadFlux(TMC4671Biquad bq){
19701973
writeReg(0x4E, 25);
19711974
writeReg(0x4D, bq.a1);
@@ -1980,6 +1983,10 @@ void TMC4671::setBiquadFlux(TMC4671Biquad bq){
19801983
writeReg(0x4E, 31);
19811984
writeReg(0x4D, bq.enable & 0x1);
19821985
}
1986+
1987+
/**
1988+
* Applies a biquad filter to the pos target
1989+
*/
19831990
void TMC4671::setBiquadPos(TMC4671Biquad bq){
19841991
writeReg(0x4E, 1);
19851992
writeReg(0x4D, bq.a1);
@@ -1994,6 +2001,10 @@ void TMC4671::setBiquadPos(TMC4671Biquad bq){
19942001
writeReg(0x4E, 7);
19952002
writeReg(0x4D, bq.enable & 0x1);
19962003
}
2004+
2005+
/**
2006+
* Applies a biquad filter to the actual measured velocity
2007+
*/
19972008
void TMC4671::setBiquadVel(TMC4671Biquad bq){
19982009
writeReg(0x4E, 9);
19992010
writeReg(0x4D, bq.a1);
@@ -2008,6 +2019,10 @@ void TMC4671::setBiquadVel(TMC4671Biquad bq){
20082019
writeReg(0x4E, 15);
20092020
writeReg(0x4D, bq.enable & 0x1);
20102021
}
2022+
2023+
/**
2024+
* Applies a biquad filter to the torque target
2025+
*/
20112026
void TMC4671::setBiquadTorque(TMC4671Biquad bq){
20122027
writeReg(0x4E, 17);
20132028
writeReg(0x4D, bq.a1);

0 commit comments

Comments
 (0)