Skip to content

Commit 30e1006

Browse files
committed
feat: final TMC optimizations, project configs and build tools
1 parent 1363b78 commit 30e1006

17 files changed

Lines changed: 228 additions & 88 deletions

Firmware/FFBoard/Inc/MotorDriver.h

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
* MotorDriver.h
33
*
44
* Created on: Feb 1, 2020
5-
* Author: Yannick
5+
* Author: Yannick, Vincent
66
*/
77

88
#ifndef MOTORDRIVER_H_
99
#define MOTORDRIVER_H_
1010

11+
#define MAX_SLEW_RATE 65535
12+
1113
#include "cppmain.h"
1214
#include "ChoosableClass.h"
1315
#include "PersistentStorage.h"
1416
#include "Encoder.h"
1517
#include "memory"
1618

17-
#define MAX_SLEW_RATE 65535
18-
1919
class Encoder;
20-
21-
/**
22-
* @brief Base class for all motor drivers.
23-
* This class defines the interface for controlling a motor and optionally its integrated encoder.
24-
*/
2520
class MotorDriver : public ChoosableClass{
2621
public:
2722
MotorDriver(){};
@@ -32,75 +27,46 @@ class MotorDriver : public ChoosableClass{
3227
const ClassType getClassType() override {return ClassType::Motordriver;};
3328
static const std::vector<class_entry<MotorDriver>> all_drivers;
3429

35-
/**
36-
* @brief Sends a torque command to the motor.
37-
* @param power The torque value (signed 16-bit).
38-
*/
30+
virtual void setupDriver();
3931
virtual void turn(int16_t power);
40-
/**
41-
* @brief Stops the motor (torque = 0).
42-
*/
4332
virtual void stopMotor();
44-
/**
45-
* @brief Starts the motor driver.
46-
*/
4733
virtual void startMotor();
48-
/**
49-
* @brief Triggers an emergency stop.
50-
* @param reset If true, resets the driver after stopping.
51-
*/
5234
virtual void emergencyStop(bool reset = false);
5335

36+
virtual void setPowerLimit(uint16_t power){}; // specific motor driver manager power, this is used to send powerLimit to the driver, like TMC4671.
37+
38+
//
5439
/**
55-
* @brief Checks if the driver is ready to receive torque commands.
56-
* @return true if ready, false otherwise.
40+
* Slew rate calibration interface (no-op by default)
41+
* If driver can't calibration the max slew rate will by MAX_SLEW_RATE (65535)
42+
* @return false is the driver not implemented this process or true if calibration start
5743
*/
58-
virtual bool motorReady();
59-
44+
virtual bool startSlewRateCalibration() { return false; };
6045
/**
61-
* @brief Gets the encoder managed by this driver.
62-
* @return A pointer to the encoder instance.
46+
* Check if calibration is in process
47+
* @return the state of calibration process
6348
*/
64-
virtual Encoder* getEncoder();
65-
66-
/**
67-
* @brief Sets an external encoder for drivers that don't have an integrated one.
68-
* @param encoder A shared pointer to the external encoder.
69-
*/
70-
virtual void setEncoder(std::shared_ptr<Encoder>& encoder){drvEncoder = encoder;}
71-
72-
/**
73-
* @brief Checks if the driver has an integrated encoder.
74-
* @return true if integrated, false if external.
75-
*/
76-
virtual bool hasIntegratedEncoder();
77-
49+
virtual bool isSlewRateCalibrationInProgress() { return false; };
7850
/**
79-
* @brief Gets the hardware measured maximum slew rate of the driver.
80-
* @return The maximum slew rate in units/ms.
51+
* Get the value of the Slew Rate after a calibration
52+
* @return
8153
*/
82-
virtual uint32_t getDrvSlewRate() { return MAX_SLEW_RATE; }
54+
virtual uint16_t getDrvSlewRate() { return MAX_SLEW_RATE; };
55+
56+
virtual bool motorReady(); // Returns true if the driver is active and ready to receive commands
8357

84-
/**
85-
* @brief Starts a calibration procedure to measure the driver's max slew rate.
86-
* @return true if calibration started successfully.
87-
*/
88-
virtual bool startSlewRateCalibration() { return false; }
58+
virtual Encoder* getEncoder(); // Encoder is managed by the motor driver. Must always return an encoder
8959

9060
/**
91-
* @brief Performs initial setup and configuration of the driver.
61+
* Can pass an external encoder if driver has no integrated encoder
62+
* This allows a driver to get an external encoder assigned if it requires one and has the capability of using external encoders
9263
*/
93-
virtual void setupDriver() {}
94-
95-
/**
96-
* @brief Sets the maximum power/current limit for the driver.
97-
* @param power The power limit value.
98-
*/
99-
virtual void setPowerLimit(uint16_t power) {}
64+
virtual void setEncoder(std::shared_ptr<Encoder>& encoder){drvEncoder = encoder;}
65+
virtual bool hasIntegratedEncoder(); // Returns true if the driver has an integrated encoder. If false the axis will pass one to the driver
10066

10167

10268
protected:
103-
std::shared_ptr<Encoder> drvEncoder = std::make_shared<Encoder>(); //!< Pointer to the encoder (integrated or external).
69+
std::shared_ptr<Encoder> drvEncoder = std::make_shared<Encoder>(); // Dummy encoder
10470
};
10571

10672

Firmware/FFBoard/Inc/ffb_defs.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,25 @@ typedef struct
195195

196196
} __attribute__((packed)) reportFFB_status_t;
197197

198-
198+
/**
199+
* @brief Contient l'état et les tampons pour
200+
* l'interpolation d'un seul paramètre (ex: magnitude ou offset).
201+
*/
202+
typedef struct
203+
{
204+
#ifdef USE_DSP_FUNCTIONS
205+
float32_t spline_x[4] = {0}; // Buffer time(en us)
206+
float32_t spline_y[4] = {0}; // Buffer value
207+
float32_t spline_y2[4] = {0}; // Buffer for Spline Natural
208+
float32_t spline_scratch[8] = {0}; // Buffer for Spline Natural
209+
arm_spline_instance_f32 spline_instance; // Instance pour CMSIS-DSP
210+
bool spline_arm_initialized = false; // CMSIS-DSP initialized ?
211+
#else
212+
float spline_x[4] = {0}; // Buffer time(en us)
213+
float spline_y[4] = {0}; // Buffer value
214+
#endif
215+
bool isSplineReady = false; // Le tampon est-il plein ?
216+
} ReconFilterState;
199217

200218
typedef struct
201219
{
@@ -333,6 +351,9 @@ typedef struct
333351
uint16_t samplePeriod = 0;
334352
bool useEnvelope = false;
335353
bool useSingleCondition = true;
354+
355+
ReconFilterState recon_magnitude; // State pour Magnitude (ou Amplitude)
356+
ReconFilterState recon_offset; // State pour Offset (périodiques)
336357
} FFB_Effect;
337358

338359

Firmware/FFBoard/Inc/flash_helpers.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* flash_helpers.h
33
*
44
* Created on: 31.01.2020
5-
* Author: Yannick
5+
* Author: Yannick, Vincent
66
*/
77

88
#ifndef FLASH_HELPERS_H_
@@ -32,6 +32,11 @@ bool Flash_ReadWriteDefault(uint16_t adr,uint16_t *buf,uint16_t def); // returns
3232
void Flash_Dump(std::vector<std::tuple<uint16_t,uint16_t>> *result,bool includeAll = false);
3333
bool Flash_Format();
3434

35+
#ifdef COGGING_TABLE_FLASH_START_ADDRESS
36+
bool Flash_WriteCoggingTable(uint8_t table_idx, int16_t* data);
37+
bool Flash_ReadCoggingTable(uint8_t table_idx, int16_t* data);
38+
#endif
39+
3540
bool OTP_Write(uint16_t adroffset,uint64_t dat);
3641
bool OTP_Read(uint16_t adroffset,uint64_t* dat);
3742

Firmware/FFBoard/Src/HidCommandInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void HID_CommandInterface::hidCmdCallback(HID_CMD_Data_t* data){
193193
for(CommandHandler* handler : handlers){
194194
ParsedCommand newCmd = cmd;
195195
newCmd.target = handler;
196-
if(newCmd.target == nullptr || !(cmd.target->isValidCommandId(cmd.cmdId, CMDFLAG_STR_ONLY))){
196+
if(newCmd.target == nullptr || !(newCmd.target->isValidCommandId(cmd.cmdId, CMDFLAG_STR_ONLY))){
197197
data->type = HidCmdType::notFound;
198198
//sendHidCmd(data); // Send back error
199199
this->outBuffer.push_back(*data);

Firmware/FFBoard/UserExtensions/Inc/TMC4671.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* TMC4671.h
33
*
44
* Created on: Feb 1, 2020
5-
* Author: Yannick
5+
* Author: Yannick, Vincent
66
*/
77

88
#ifndef TMC4671_H_
@@ -235,7 +235,7 @@ struct TMC4671PIDConf{
235235
struct TMC4671Limits{
236236
uint16_t pid_torque_flux_ddt = 32767;
237237
uint16_t pid_uq_ud = 30000;
238-
uint16_t pid_torque_flux = 32767;
238+
uint16_t pid_torque_flux = 30000;
239239
uint32_t pid_acc_lim = 2147483647;
240240
uint32_t pid_vel_lim = 2147483647;
241241
int32_t pid_pos_low = -2147483647;
@@ -345,13 +345,25 @@ class TMC4671Biquad{
345345
}
346346
TMC4671Biquad(const TMC4671Biquad_t bq) : params(bq){}
347347
TMC4671Biquad(const Biquad& bq,bool enable = true){
348+
#ifdef USE_DSP_FUNCTIONS
349+
const float* coeffs = bq.getCoeffs();
350+
// coeffs is in order {b0, b1, b2, -a1, -a2}
351+
// TMC expects {b0, b1, b2, a1, a2} with a1 and a2 negated
352+
this->params.b0 = (int32_t)(coeffs[0] * (float)(1 << 29));
353+
this->params.b1 = (int32_t)(coeffs[1] * (float)(1 << 29));
354+
this->params.b2 = (int32_t)(coeffs[2] * (float)(1 << 29));
355+
this->params.a1 = (int32_t)(coeffs[3] * (float)(1 << 29));
356+
this->params.a2 = (int32_t)(coeffs[4] * (float)(1 << 29));
357+
this->params.enable = bq.getFc() > 0 ? enable : false;
358+
#else
348359
// Note: trinamic swapped the naming of b and a from the regular convention in the datasheet and a and b are possibly inverse to b in our filter class
349360
this->params.a1 = -(int32_t)(bq.b1 * (float)(1 << 29));
350361
this->params.a2 = -(int32_t)(bq.b2 * (float)(1 << 29));
351362
this->params.b0 = (int32_t)(bq.a0 * (float)(1 << 29));
352363
this->params.b1 = (int32_t)(bq.a1 * (float)(1 << 29));
353364
this->params.b2 = (int32_t)(bq.a2 * (float)(1 << 29));
354365
this->params.enable = bq.getFc() > 0 ? enable : false;
366+
#endif
355367
}
356368
void enable(bool enable){
357369
params.enable = enable;
@@ -412,6 +424,7 @@ friend class TMCDebugBridge;
412424

413425
TMC4671MainConfig conf;
414426

427+
void setupDriver();
415428
bool initialize();
416429
void initializeWithPower();
417430

@@ -485,15 +498,20 @@ friend class TMCDebugBridge;
485498
void stopMotor();
486499
void startMotor();
487500

501+
void setPowerLimit(uint16_t power) override;
502+
488503
void emergencyStop(bool reset);
489504
bool emergency = false;
490505
bool estopTriggered = false;
491506
void turn(int16_t power);
507+
508+
int16_t getVelocityControllerTorque();
492509
int16_t nextFlux = 0;
493510
int16_t idleFlux = 0;
494511
uint16_t maxOffsetFlux = 0;
495512

496513
int16_t bangInitPower = 5000; // Default current in setup routines
514+
uint16_t maxPowerAxis = 0;
497515

498516
int16_t controlFluxDissipate();
499517
const float fluxDissipationLimit = 1000;
@@ -629,6 +647,7 @@ friend class TMCDebugBridge;
629647
static std::span<const TMC4671HardwareTypeConf> tmc4671_hw_configs; // Can override in external target file
630648

631649
private:
650+
uint8_t drv_address = 0;
632651
OutputPin enablePin = OutputPin(*DRV_ENABLE_GPIO_Port,DRV_ENABLE_Pin);
633652
const Error indexNotHitError = Error(ErrorCode::encoderIndexMissed,ErrorType::critical,"Encoder index missed");
634653
const Error lowVoltageError = Error(ErrorCode::undervoltage,ErrorType::warning,"Low motor voltage");
@@ -737,5 +756,3 @@ class TMC_2 : public TMC4671 {
737756
};
738757
#endif
739758
#endif /* TMC4671_H_ */
740-
741-

Firmware/FFBoard/UserExtensions/Inc/eeprom_addresses.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
#include "main.h"
1515
// Change this to the amount of currently registered variables
16-
#define NB_OF_VAR 164
16+
#define NB_OF_VAR 168
1717
extern const uint16_t VirtAddVarTab[NB_OF_VAR];
1818

1919
// Amount of variables in exportable list
20-
#define NB_EXPORTABLE_ADR 149
20+
#define NB_EXPORTABLE_ADR 153
2121
extern const uint16_t exportableFlashAddresses[NB_EXPORTABLE_ADR];
2222

2323

@@ -88,6 +88,7 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) will return 1 if
8888
#define ADR_FFB_EFFECTS1 0x284 // 0-7 inertia, 8-15 friction
8989
#define ADR_FFB_EFFECTS2 0x285 // 0-7 spring, 8-15 damper
9090
#define ADR_FFB_EFFECTS3 0x286 // 0-7 friction ramp up zone, 8-9 filterProfile
91+
#define ADR_FFB_RECONSTRUCTION_FILTER 0x287 // 0-1 recon filter mode
9192
// Button Sources:
9293
#define ADR_ADS111X_CONF1 0x290
9394
// How many axis configured 1-3
@@ -98,6 +99,7 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) will return 1 if
9899
#define ADR_AXIS1_DEGREES 0x303
99100
#define ADR_AXIS1_MAX_SPEED 0x304 // Store the max speed
100101
#define ADR_AXIS1_MAX_ACCEL 0x305 // Store the max accel
102+
#define ADR_AXIS1_MAX_SLEWRATE_DRV 0x306 // Max slew rate for drv
101103
#define ADR_AXIS1_ENDSTOP 0x307 // 0-7 endstop margin, 8-15 endstop stiffness
102104
#define ADR_AXIS1_EFFECTS1 0x308 // 0-7 idlespring, 8-15 damper
103105
#define ADR_AXIS1_SPEEDACCEL_FILTER 0x309 // Speed/Accel filter Lowpass profile
@@ -124,6 +126,7 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) will return 1 if
124126
#define ADR_AXIS2_DEGREES 0x343
125127
#define ADR_AXIS2_MAX_SPEED 0x344 // Store the max speed
126128
#define ADR_AXIS2_MAX_ACCEL 0x345 // Store the max accel
129+
#define ADR_AXIS2_MAX_SLEWRATE_DRV 0x346 // Max slew rate for drv
127130
#define ADR_AXIS2_ENDSTOP 0x347 // 0-7 endstop margin, 8-15 endstop stiffness
128131
#define ADR_AXIS2_EFFECTS1 0x348 // 0-7 idlespring, 8-15 damper
129132
#define ADR_AXIS2_SPEEDACCEL_FILTER 0x349 // Speed/Accel filter Lowpass profile
@@ -150,6 +153,7 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) will return 1 if
150153
#define ADR_AXIS3_DEGREES 0x383
151154
#define ADR_AXIS3_MAX_SPEED 0x384 // Store the max speed
152155
#define ADR_AXIS3_MAX_ACCEL 0x385 // Store the max accel
156+
#define ADR_AXIS3_MAX_SLEWRATE_DRV 0x386 // Max slew rate for drv
153157
#define ADR_AXIS3_ENDSTOP 0x387 // 0-7 endstop margin, 8-15 endstop stiffness
154158
#define ADR_AXIS3_EFFECTS1 0x388 // 0-7 idlespring, 8-15 damper
155159
#define ADR_AXIS3_SPEEDACCEL_FILTER 0x389 // Speed/Accel filter Lowpass profile

Firmware/FFBoard/UserExtensions/Src/FFBHIDMain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* FFBWheel.cpp
33
*
44
* Created on: 31.01.2020
5-
* Author: Yannick / Lidders
5+
* Author: Yannick / Lidders / Vincent
66
*/
77

88
#include <FFBHIDMain.h>
@@ -29,7 +29,7 @@ const FFBHIDMain::FFB_update_rates FFBHIDMain::ffbrates; // Default rates
2929
* setFFBEffectsCalc must be called in constructor of derived class to finish the setup
3030
*/
3131
FFBHIDMain::FFBHIDMain(uint8_t axisCount,bool hidAxis32b) :
32-
Thread("FFBMAIN", 256, 30),
32+
Thread("FFBMAIN", 312, 30),
3333
SelectableInputs(ButtonSource::all_buttonsources,AnalogSource::all_analogsources),
3434
axisCount(axisCount),hidAxis32b(hidAxis32b)
3535
{

Firmware/FFBoard/UserExtensions/Src/FFBoardMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* FFBoardMain.cpp
33
*
44
* Created on: 23.01.2020
5-
* Author: Yannick
5+
* Author: Yannick, Vincent
66
*/
77

88
#include "FFBoardMain.h"

0 commit comments

Comments
 (0)