Skip to content

Commit a3250a7

Browse files
committed
Added helper method to set motor current by RMS.
The code is copied from the TMCStepper project. The MIT licenses are compatible and a source mention has been made in code. This helper method simplifies correctly configuring the current.
1 parent a988619 commit a3250a7

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/TMC2209.h

+11
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ class TMC2209
8686
void setAllCurrentValues(uint8_t run_current_percent,
8787
uint8_t hold_current_percent,
8888
uint8_t hold_delay_percent);
89+
void setRMSCurrent(uint16_t mA,
90+
float rSense,
91+
float holdMultiplier = 0.5f);
8992

9093
void enableDoubleEdge();
9194
void disableDoubleEdge();
9295

96+
void enableVSense();
97+
void disableVSense();
98+
9399
void enableInverseMotorDirection();
94100
void disableInverseMotorDirection();
95101

@@ -279,6 +285,9 @@ class TMC2209
279285
const static uint8_t STEPPER_DRIVER_FEATURE_OFF = 0;
280286
const static uint8_t STEPPER_DRIVER_FEATURE_ON = 1;
281287

288+
const static int MAX_READ_RETRIES = 5;
289+
const static uint32_t READ_RETRY_DELAY_MS = 20;
290+
282291
// Datagrams
283292
const static uint8_t WRITE_READ_REPLY_DATAGRAM_SIZE = 8;
284293
const static uint8_t DATA_SIZE = 4;
@@ -504,6 +513,8 @@ class TMC2209
504513
const static uint8_t MRES_001 = 0b1000;
505514
const static uint8_t DOUBLE_EDGE_DISABLE = 0;
506515
const static uint8_t DOUBLE_EDGE_ENABLE = 1;
516+
const static uint8_t VSENSE_DISABLE = 0;
517+
const static uint8_t VSENSE_ENABLE = 1;
507518

508519
const static size_t MICROSTEPS_PER_STEP_MIN = 1;
509520
const static size_t MICROSTEPS_PER_STEP_MAX = 256;

src/TMC2209/TMC2209.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ void TMC2209::setAllCurrentValues(uint8_t run_current_percent,
223223
writeStoredDriverCurrent();
224224
}
225225

226+
void TMC2209::setRMSCurrent(uint16_t mA,
227+
float rSense,
228+
float holdMultiplier)
229+
{
230+
// Taken from https://github.com/teemuatlut/TMCStepper/blob/74e8e6881adc9241c2e626071e7328d7652f361a/src/source/TMCStepper.cpp#L41.
231+
232+
uint8_t CS = 32.0*1.41421*mA/1000.0*(rSense+0.02)/0.325 - 1;
233+
// If Current Scale is too low, turn on high sensitivity R_sense and calculate again
234+
if (CS < 16) {
235+
enableVSense();
236+
CS = 32.0*1.41421*mA/1000.0*(rSense+0.02)/0.180 - 1;
237+
} else { // If CS >= 16, turn off high_sense_r
238+
disableVSense();
239+
}
240+
241+
if (CS > 31) {
242+
CS = 31;
243+
}
244+
245+
driver_current_.irun = CS;
246+
driver_current_.ihold = CS*holdMultiplier;
247+
writeStoredDriverCurrent();
248+
}
249+
226250
void TMC2209::enableDoubleEdge()
227251
{
228252
chopper_config_.double_edge = DOUBLE_EDGE_ENABLE;
@@ -235,6 +259,18 @@ void TMC2209::disableDoubleEdge()
235259
writeStoredChopperConfig();
236260
}
237261

262+
void TMC2209::enableVSense()
263+
{
264+
chopper_config_.vsense = VSENSE_ENABLE;
265+
writeStoredChopperConfig();
266+
}
267+
268+
void TMC2209::disableVSense()
269+
{
270+
chopper_config_.vsense = VSENSE_DISABLE;
271+
writeStoredChopperConfig();
272+
}
273+
238274
void TMC2209::enableInverseMotorDirection()
239275
{
240276
global_config_.shaft = 1;

0 commit comments

Comments
 (0)