TMCStepper
TMCStepper.cpp
Go to the documentation of this file.
1 #include "TMCStepper.h"
2 #include "TMC_MACROS.h"
3 
4 /*
5  Requested current = mA = I_rms/1000
6  Equation for current:
7  I_rms = (CS+1)/32 * V_fs/(R_sense+0.02ohm) * 1/sqrt(2)
8  Solve for CS ->
9  CS = 32*sqrt(2)*I_rms*(R_sense+0.02)/V_fs - 1
10 
11  Example:
12  vsense = 0b0 -> V_fs = 0.325V
13  mA = 1640mA = I_rms/1000 = 1.64A
14  R_sense = 0.10 Ohm
15  ->
16  CS = 32*sqrt(2)*1.64*(0.10+0.02)/0.325 - 1 = 26.4
17  CS = 26
18 */
19 
20 uint16_t TMCStepper::cs2rms(uint8_t CS) {
21  return (float)(CS+1)/32.0 * (vsense() ? 0.180 : 0.325)/(Rsense+0.02) / 1.41421 * 1000;
22 }
23 
24 void TMCStepper::rms_current(uint16_t mA) {
25  uint8_t CS = 32.0*1.41421*mA/1000.0*(Rsense+0.02)/0.325 - 1;
26  // If Current Scale is too low, turn on high sensitivity R_sense and calculate again
27  if (CS < 16) {
28  vsense(true);
29  CS = 32.0*1.41421*mA/1000.0*(Rsense+0.02)/0.180 - 1;
30  } else { // If CS >= 16, turn off high_sense_r
31  vsense(false);
32  }
33 
34  if (CS > 31)
35  CS = 31;
36 
37  irun(CS);
39  //val_mA = mA;
40 }
41 void TMCStepper::rms_current(uint16_t mA, float mult) {
42  holdMultiplier = mult;
43  rms_current(mA);
44 }
45 
47  return cs2rms(irun());
48 }
49 
51  uint32_t drv_status = DRV_STATUS();
52  switch (drv_status) {
53  case 0xFFFFFFFF: return 1;
54  case 0: return 2;
55  default: return 0;
56  }
57 }
58 
59 void TMCStepper::hysteresis_end(int8_t value) { hend(value+3); }
60 int8_t TMCStepper::hysteresis_end() { return hend()-3; };
61 
62 void TMCStepper::hysteresis_start(uint8_t value) { hstrt(value-1); }
63 uint8_t TMCStepper::hysteresis_start() { return hstrt()+1; }
64 
65 void TMCStepper::microsteps(uint16_t ms) {
66  switch(ms) {
67  case 256: mres(0); break;
68  case 128: mres(1); break;
69  case 64: mres(2); break;
70  case 32: mres(3); break;
71  case 16: mres(4); break;
72  case 8: mres(5); break;
73  case 4: mres(6); break;
74  case 2: mres(7); break;
75  case 0: mres(8); break;
76  default: break;
77  }
78 }
79 
81  switch(mres()) {
82  case 0: return 256;
83  case 1: return 128;
84  case 2: return 64;
85  case 3: return 32;
86  case 4: return 16;
87  case 5: return 8;
88  case 6: return 4;
89  case 7: return 2;
90  case 8: return 0;
91  }
92  return 0;
93 }
94 
95 void TMCStepper::blank_time(uint8_t value) {
96  switch (value) {
97  case 16: tbl(0b00); break;
98  case 24: tbl(0b01); break;
99  case 36: tbl(0b10); break;
100  case 54: tbl(0b11); break;
101  }
102 }
103 
105  switch (tbl()) {
106  case 0b00: return 16;
107  case 0b01: return 24;
108  case 0b10: return 36;
109  case 0b11: return 54;
110  }
111  return 0;
112 }
113 
115 // R+C: GSTAT
117 void TMCStepper::GSTAT(uint8_t){ write(GSTAT_t::address, 0b111); }
118 bool TMCStepper::reset() { GSTAT_t r; r.sr = GSTAT(); return r.reset; }
119 bool TMCStepper::drv_err() { GSTAT_t r; r.sr = GSTAT(); return r.drv_err; }
120 bool TMCStepper::uv_cp() { GSTAT_t r; r.sr = GSTAT(); return r.uv_cp; }
122 // W: TPOWERDOWN
123 uint8_t TMCStepper::TPOWERDOWN() { return TPOWERDOWN_register.sr; }
124 void TMCStepper::TPOWERDOWN(uint8_t input) {
125  TPOWERDOWN_register.sr = input;
126  write(TPOWERDOWN_register.address, TPOWERDOWN_register.sr);
127 }
129 // R: TSTEP
130 uint32_t TMCStepper::TSTEP() { return read(TSTEP_t::address); }
132 // W: TPWMTHRS
133 uint32_t TMCStepper::TPWMTHRS() { return TPWMTHRS_register.sr; }
134 void TMCStepper::TPWMTHRS(uint32_t input) {
135  TPWMTHRS_register.sr = input;
136  write(TPWMTHRS_register.address, TPWMTHRS_register.sr);
137 }
138 
139 uint16_t TMCStepper::MSCNT() {
140  return read(MSCNT_t::address);
141 }
142 
144 int16_t TMCStepper::cur_a() {
145  MSCURACT_t r{0};
146  r.sr = MSCURACT();
147  int16_t value = r.cur_a;
148  if (value > 255) value -= 512;
149  return value;
150 }
151 int16_t TMCStepper::cur_b() {
152  MSCURACT_t r{0};
153  r.sr = MSCURACT();
154  int16_t value = r.cur_b;
155  if (value > 255) value -= 512;
156  return value;
157 }
TMCStepper::mres
virtual uint8_t mres()=0
GSTAT_t::reset
bool reset
Definition: TMC2130_bitfields.h:60
MSCURACT_t::address
constexpr static uint8_t address
Definition: TMC2208_bitfields.h:90
MSCURACT_t::sr
uint32_t sr
Definition: TMC2208_bitfields.h:92
TMCStepper::tbl
virtual uint8_t tbl()=0
MSCURACT_t
Definition: TMC2208_bitfields.h:89
GSTAT_t::uv_cp
bool uv_cp
Definition: TMC2130_bitfields.h:62
TMCStepper::uv_cp
bool uv_cp()
Definition: TMCStepper.cpp:120
TMCStepper::read
virtual uint32_t read(uint8_t)=0
TMCStepper.h
TMCStepper::rms_current
uint16_t rms_current()
Definition: TMCStepper.cpp:46
TMCStepper::MSCNT_t::address
constexpr static uint8_t address
Definition: TMCStepper.h:117
TMCStepper::Rsense
const float Rsense
Definition: TMCStepper.h:133
TMCStepper::hstrt
virtual uint8_t hstrt()=0
TMCStepper::blank_time
uint8_t blank_time()
Definition: TMCStepper.cpp:104
TMCStepper::TSTEP
uint32_t TSTEP()
Definition: TMCStepper.cpp:130
TMCStepper::cs2rms
uint16_t cs2rms(uint8_t CS)
Definition: TMCStepper.cpp:20
TMCStepper::write
virtual void write(uint8_t, uint32_t)=0
TMC_MACROS.h
TMCStepper::MSCNT
uint16_t MSCNT()
Definition: TMCStepper.cpp:139
GSTAT_t::sr
uint8_t sr
Definition: TMC2130_bitfields.h:58
TMCStepper::cur_a
int16_t cur_a()
Definition: TMCStepper.cpp:144
GSTAT_t::drv_err
bool drv_err
Definition: TMC2130_bitfields.h:61
TMCStepper::vsense
virtual bool vsense(void)=0
TMCStepper::MSCURACT
uint32_t MSCURACT()
Definition: TMCStepper.cpp:143
TMCStepper::TSTEP_t::address
constexpr static uint8_t address
Definition: TMCStepper.h:116
TMCStepper::hysteresis_end
int8_t hysteresis_end()
Definition: TMCStepper.cpp:60
TMCStepper::hend
virtual uint8_t hend()=0
TMCStepper::microsteps
uint16_t microsteps()
Definition: TMCStepper.cpp:80
GSTAT_t
Definition: TMC2130_bitfields.h:55
TMCStepper::reset
bool reset()
Definition: TMCStepper.cpp:118
TMCStepper::irun
uint8_t irun()
Definition: IHOLD_IRUN.cpp:19
TMCStepper::TPOWERDOWN
uint8_t TPOWERDOWN()
Definition: TMCStepper.cpp:123
GSTAT_t::address
constexpr static uint8_t address
Definition: TMC2130_bitfields.h:56
TMCStepper::DRV_STATUS
virtual uint32_t DRV_STATUS()=0
TMCStepper::holdMultiplier
float holdMultiplier
Definition: TMCStepper.h:134
TMCStepper::test_connection
uint8_t test_connection()
Definition: TMCStepper.cpp:50
TMCStepper::GSTAT
uint8_t GSTAT()
Definition: TMCStepper.cpp:116
TMCStepper::drv_err
bool drv_err()
Definition: TMCStepper.cpp:119
TMCStepper::hysteresis_start
uint8_t hysteresis_start()
Definition: TMCStepper.cpp:63
TMCStepper::cur_b
int16_t cur_b()
Definition: TMCStepper.cpp:151
TMCStepper::ihold
uint8_t ihold()
Definition: IHOLD_IRUN.cpp:18
TMCStepper::TPWMTHRS
uint32_t TPWMTHRS()
Definition: TMCStepper.cpp:133