My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TMC26XStepper.cpp 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. TMC26XStepper.cpp - - TMC26X Stepper library for Wiring/Arduino
  3. based on the stepper library by Tom Igoe, et. al.
  4. Copyright (c) 2011, Interactive Matter, Marcus Nowotny
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. #if defined(ARDUINO) && ARDUINO >= 100
  22. #include <Arduino.h>
  23. #else
  24. #include <WProgram.h>
  25. #endif
  26. #include <SPI.h>
  27. #include "TMC26XStepper.h"
  28. //some default values used in initialization
  29. #define DEFAULT_MICROSTEPPING_VALUE 32
  30. //TMC26X register definitions
  31. #define DRIVER_CONTROL_REGISTER 0x0ul
  32. #define CHOPPER_CONFIG_REGISTER 0x80000ul
  33. #define COOL_STEP_REGISTER 0xA0000ul
  34. #define STALL_GUARD2_LOAD_MEASURE_REGISTER 0xC0000ul
  35. #define DRIVER_CONFIG_REGISTER 0xE0000ul
  36. #define REGISTER_BIT_PATTERN 0xFFFFFul
  37. //definitions for the driver control register
  38. #define MICROSTEPPING_PATTERN 0xFul
  39. #define STEP_INTERPOLATION 0x200ul
  40. #define DOUBLE_EDGE_STEP 0x100ul
  41. #define VSENSE 0x40ul
  42. #define READ_MICROSTEP_POSTION 0x0ul
  43. #define READ_STALL_GUARD_READING 0x10ul
  44. #define READ_STALL_GUARD_AND_COOL_STEP 0x20ul
  45. #define READ_SELECTION_PATTERN 0x30ul
  46. //definitions for the chopper config register
  47. #define CHOPPER_MODE_STANDARD 0x0ul
  48. #define CHOPPER_MODE_T_OFF_FAST_DECAY 0x4000ul
  49. #define T_OFF_PATTERN 0xful
  50. #define RANDOM_TOFF_TIME 0x2000ul
  51. #define BLANK_TIMING_PATTERN 0x18000ul
  52. #define BLANK_TIMING_SHIFT 15
  53. #define HYSTERESIS_DECREMENT_PATTERN 0x1800ul
  54. #define HYSTERESIS_DECREMENT_SHIFT 11
  55. #define HYSTERESIS_LOW_VALUE_PATTERN 0x780ul
  56. #define HYSTERESIS_LOW_SHIFT 7
  57. #define HYSTERESIS_START_VALUE_PATTERN 0x78ul
  58. #define HYSTERESIS_START_VALUE_SHIFT 4
  59. #define T_OFF_TIMING_PATERN 0xFul
  60. //definitions for cool step register
  61. #define MINIMUM_CURRENT_FOURTH 0x8000ul
  62. #define CURRENT_DOWN_STEP_SPEED_PATTERN 0x6000ul
  63. #define SE_MAX_PATTERN 0xF00ul
  64. #define SE_CURRENT_STEP_WIDTH_PATTERN 0x60ul
  65. #define SE_MIN_PATTERN 0xful
  66. //definitions for stall guard2 current register
  67. #define STALL_GUARD_FILTER_ENABLED 0x10000ul
  68. #define STALL_GUARD_TRESHHOLD_VALUE_PATTERN 0x17F00ul
  69. #define CURRENT_SCALING_PATTERN 0x1Ful
  70. #define STALL_GUARD_CONFIG_PATTERN 0x17F00ul
  71. #define STALL_GUARD_VALUE_PATTERN 0x7F00ul
  72. //definitions for the input from the TCM260
  73. #define STATUS_STALL_GUARD_STATUS 0x1ul
  74. #define STATUS_OVER_TEMPERATURE_SHUTDOWN 0x2ul
  75. #define STATUS_OVER_TEMPERATURE_WARNING 0x4ul
  76. #define STATUS_SHORT_TO_GROUND_A 0x8ul
  77. #define STATUS_SHORT_TO_GROUND_B 0x10ul
  78. #define STATUS_OPEN_LOAD_A 0x20ul
  79. #define STATUS_OPEN_LOAD_B 0x40ul
  80. #define STATUS_STAND_STILL 0x80ul
  81. #define READOUT_VALUE_PATTERN 0xFFC00ul
  82. //default values
  83. #define INITIAL_MICROSTEPPING 0x3ul //32th microstepping
  84. //debuging output
  85. //#define DEBUG
  86. /*
  87. * Constructor
  88. * number_of_steps - the steps per rotation
  89. * cs_pin - the SPI client select pin
  90. * dir_pin - the pin where the direction pin is connected
  91. * step_pin - the pin where the step pin is connected
  92. */
  93. TMC26XStepper::TMC26XStepper(int number_of_steps, int cs_pin, int dir_pin, int step_pin, unsigned int current, unsigned int resistor)
  94. {
  95. //we are not started yet
  96. started=false;
  97. //by default cool step is not enabled
  98. cool_step_enabled=false;
  99. //save the pins for later use
  100. this->cs_pin=cs_pin;
  101. this->dir_pin=dir_pin;
  102. this->step_pin = step_pin;
  103. //store the current sense resistor value for later use
  104. this->resistor = resistor;
  105. //initizalize our status values
  106. this->steps_left = 0;
  107. this->direction = 0;
  108. //initialize register values
  109. driver_control_register_value=DRIVER_CONTROL_REGISTER | INITIAL_MICROSTEPPING;
  110. chopper_config_register=CHOPPER_CONFIG_REGISTER;
  111. //setting the default register values
  112. driver_control_register_value=DRIVER_CONTROL_REGISTER|INITIAL_MICROSTEPPING;
  113. microsteps = (1 << INITIAL_MICROSTEPPING);
  114. chopper_config_register=CHOPPER_CONFIG_REGISTER;
  115. cool_step_register_value=COOL_STEP_REGISTER;
  116. stall_guard2_current_register_value=STALL_GUARD2_LOAD_MEASURE_REGISTER;
  117. driver_configuration_register_value = DRIVER_CONFIG_REGISTER | READ_STALL_GUARD_READING;
  118. //set the current
  119. setCurrent(current);
  120. //set to a conservative start value
  121. setConstantOffTimeChopper(7, 54, 13,12,1);
  122. //set a nice microstepping value
  123. setMicrosteps(DEFAULT_MICROSTEPPING_VALUE);
  124. //save the number of steps
  125. this->number_of_steps = number_of_steps;
  126. }
  127. /*
  128. * start & configure the stepper driver
  129. * just must be called.
  130. */
  131. void TMC26XStepper::start() {
  132. #ifdef DEBUG
  133. Serial.println("TMC26X stepper library");
  134. Serial.print("CS pin: ");
  135. Serial.println(cs_pin);
  136. Serial.print("DIR pin: ");
  137. Serial.println(dir_pin);
  138. Serial.print("STEP pin: ");
  139. Serial.println(step_pin);
  140. Serial.print("current scaling: ");
  141. Serial.println(current_scaling,DEC);
  142. #endif
  143. //set the pins as output & its initial value
  144. pinMode(step_pin, OUTPUT);
  145. pinMode(dir_pin, OUTPUT);
  146. pinMode(cs_pin, OUTPUT);
  147. digitalWrite(step_pin, LOW);
  148. digitalWrite(dir_pin, LOW);
  149. digitalWrite(cs_pin, HIGH);
  150. //configure the SPI interface
  151. SPI.setBitOrder(MSBFIRST);
  152. SPI.setClockDivider(SPI_CLOCK_DIV8);
  153. //todo this does not work reliably - find a way to foolprof set it (e.g. while communicating
  154. //SPI.setDataMode(SPI_MODE3);
  155. SPI.begin();
  156. //set the initial values
  157. send262(driver_control_register_value);
  158. send262(chopper_config_register);
  159. send262(cool_step_register_value);
  160. send262(stall_guard2_current_register_value);
  161. send262(driver_configuration_register_value);
  162. //save that we are in running mode
  163. started=true;
  164. }
  165. /*
  166. Mark the driver as unstarted to be able to start it again
  167. */
  168. void TMC26XStepper::un_start() {
  169. started=false;
  170. }
  171. /*
  172. Sets the speed in revs per minute
  173. */
  174. void TMC26XStepper::setSpeed(unsigned int whatSpeed)
  175. {
  176. this->speed = whatSpeed;
  177. this->step_delay = (60UL * 1000UL * 1000UL) / ((unsigned long)this->number_of_steps * (unsigned long)whatSpeed * (unsigned long)this->microsteps);
  178. #ifdef DEBUG
  179. Serial.print("Step delay in micros: ");
  180. Serial.println(this->step_delay);
  181. #endif
  182. //update the next step time
  183. this->next_step_time = this->last_step_time+this->step_delay;
  184. }
  185. unsigned int TMC26XStepper::getSpeed(void) {
  186. return this->speed;
  187. }
  188. /*
  189. Moves the motor steps_to_move steps. If the number is negative,
  190. the motor moves in the reverse direction.
  191. */
  192. char TMC26XStepper::step(int steps_to_move)
  193. {
  194. if (this->steps_left==0) {
  195. this->steps_left = abs(steps_to_move); // how many steps to take
  196. // determine direction based on whether steps_to_mode is + or -:
  197. if (steps_to_move > 0) {
  198. this->direction = 1;
  199. } else if (steps_to_move < 0) {
  200. this->direction = 0;
  201. }
  202. return 0;
  203. } else {
  204. return -1;
  205. }
  206. }
  207. char TMC26XStepper::move(void) {
  208. // decrement the number of steps, moving one step each time:
  209. if(this->steps_left>0) {
  210. unsigned long time = micros();
  211. // move only if the appropriate delay has passed:
  212. if (time >= this->next_step_time) {
  213. // increment or decrement the step number,
  214. // depending on direction:
  215. if (this->direction == 1) {
  216. digitalWrite(step_pin, HIGH);
  217. } else {
  218. digitalWrite(dir_pin, HIGH);
  219. digitalWrite(step_pin, HIGH);
  220. }
  221. // get the timeStamp of when you stepped:
  222. this->last_step_time = time;
  223. this->next_step_time = time+this->step_delay;
  224. // decrement the steps left:
  225. steps_left--;
  226. //disable the step & dir pins
  227. digitalWrite(step_pin, LOW);
  228. digitalWrite(dir_pin, LOW);
  229. }
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. char TMC26XStepper::isMoving(void) {
  235. return (this->steps_left>0);
  236. }
  237. unsigned int TMC26XStepper::getStepsLeft(void) {
  238. return this->steps_left;
  239. }
  240. char TMC26XStepper::stop(void) {
  241. //note to self if the motor is currently moving
  242. char state = isMoving();
  243. //stop the motor
  244. this->steps_left = 0;
  245. this->direction = 0;
  246. //return if it was moving
  247. return state;
  248. }
  249. void TMC26XStepper::setCurrent(unsigned int current) {
  250. unsigned char current_scaling = 0;
  251. //calculate the current scaling from the max current setting (in mA)
  252. double mASetting = (double)current;
  253. double resistor_value = (double) this->resistor;
  254. // remove vesense flag
  255. this->driver_configuration_register_value &= ~(VSENSE);
  256. //this is derrived from I=(cs+1)/32*(Vsense/Rsense)
  257. //leading to cs = CS = 32*R*I/V (with V = 0,31V oder 0,165V and I = 1000*current)
  258. //with Rsense=0,15
  259. //for vsense = 0,310V (VSENSE not set)
  260. //or vsense = 0,165V (VSENSE set)
  261. current_scaling = (byte)((resistor_value*mASetting*32.0/(0.31*1000.0*1000.0))-0.5); //theoretically - 1.0 for better rounding it is 0.5
  262. //check if the current scalingis too low
  263. if (current_scaling<16) {
  264. //set the csense bit to get a use half the sense voltage (to support lower motor currents)
  265. this->driver_configuration_register_value |= VSENSE;
  266. //and recalculate the current setting
  267. current_scaling = (byte)((resistor_value*mASetting*32.0/(0.165*1000.0*1000.0))-0.5); //theoretically - 1.0 for better rounding it is 0.5
  268. #ifdef DEBUG
  269. Serial.print("CS (Vsense=1): ");
  270. Serial.println(current_scaling);
  271. } else {
  272. Serial.print("CS: ");
  273. Serial.println(current_scaling);
  274. #endif
  275. }
  276. //do some sanity checks
  277. if (current_scaling>31) {
  278. current_scaling=31;
  279. }
  280. //delete the old value
  281. stall_guard2_current_register_value &= ~(CURRENT_SCALING_PATTERN);
  282. //set the new current scaling
  283. stall_guard2_current_register_value |= current_scaling;
  284. //if started we directly send it to the motor
  285. if (started) {
  286. send262(driver_configuration_register_value);
  287. send262(stall_guard2_current_register_value);
  288. }
  289. }
  290. unsigned int TMC26XStepper::getCurrent(void) {
  291. //we calculate the current according to the datasheet to be on the safe side
  292. //this is not the fastest but the most accurate and illustrative way
  293. double result = (double)(stall_guard2_current_register_value & CURRENT_SCALING_PATTERN);
  294. double resistor_value = (double)this->resistor;
  295. double voltage = (driver_configuration_register_value & VSENSE)? 0.165:0.31;
  296. result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
  297. return (unsigned int)result;
  298. }
  299. void TMC26XStepper::setStallGuardThreshold(char stall_guard_threshold, char stall_guard_filter_enabled) {
  300. if (stall_guard_threshold<-64) {
  301. stall_guard_threshold = -64;
  302. //We just have 5 bits
  303. } else if (stall_guard_threshold > 63) {
  304. stall_guard_threshold = 63;
  305. }
  306. //add trim down to 7 bits
  307. stall_guard_threshold &=0x7f;
  308. //delete old stall guard settings
  309. stall_guard2_current_register_value &= ~(STALL_GUARD_CONFIG_PATTERN);
  310. if (stall_guard_filter_enabled) {
  311. stall_guard2_current_register_value |= STALL_GUARD_FILTER_ENABLED;
  312. }
  313. //Set the new stall guard threshold
  314. stall_guard2_current_register_value |= (((unsigned long)stall_guard_threshold << 8) & STALL_GUARD_CONFIG_PATTERN);
  315. //if started we directly send it to the motor
  316. if (started) {
  317. send262(stall_guard2_current_register_value);
  318. }
  319. }
  320. char TMC26XStepper::getStallGuardThreshold(void) {
  321. unsigned long stall_guard_threshold = stall_guard2_current_register_value & STALL_GUARD_VALUE_PATTERN;
  322. //shift it down to bit 0
  323. stall_guard_threshold >>=8;
  324. //convert the value to an int to correctly handle the negative numbers
  325. char result = stall_guard_threshold;
  326. //check if it is negative and fill it up with leading 1 for proper negative number representation
  327. if (result & _BV(6)) {
  328. result |= 0xC0;
  329. }
  330. return result;
  331. }
  332. char TMC26XStepper::getStallGuardFilter(void) {
  333. if (stall_guard2_current_register_value & STALL_GUARD_FILTER_ENABLED) {
  334. return -1;
  335. } else {
  336. return 0;
  337. }
  338. }
  339. /*
  340. * Set the number of microsteps per step.
  341. * 0,2,4,8,16,32,64,128,256 is supported
  342. * any value in between will be mapped to the next smaller value
  343. * 0 and 1 set the motor in full step mode
  344. */
  345. void TMC26XStepper::setMicrosteps(int number_of_steps) {
  346. long setting_pattern;
  347. //poor mans log
  348. if (number_of_steps>=256) {
  349. setting_pattern=0;
  350. microsteps=256;
  351. } else if (number_of_steps>=128) {
  352. setting_pattern=1;
  353. microsteps=128;
  354. } else if (number_of_steps>=64) {
  355. setting_pattern=2;
  356. microsteps=64;
  357. } else if (number_of_steps>=32) {
  358. setting_pattern=3;
  359. microsteps=32;
  360. } else if (number_of_steps>=16) {
  361. setting_pattern=4;
  362. microsteps=16;
  363. } else if (number_of_steps>=8) {
  364. setting_pattern=5;
  365. microsteps=8;
  366. } else if (number_of_steps>=4) {
  367. setting_pattern=6;
  368. microsteps=4;
  369. } else if (number_of_steps>=2) {
  370. setting_pattern=7;
  371. microsteps=2;
  372. //1 and 0 lead to full step
  373. } else if (number_of_steps<=1) {
  374. setting_pattern=8;
  375. microsteps=1;
  376. }
  377. #ifdef DEBUG
  378. Serial.print("Microstepping: ");
  379. Serial.println(microsteps);
  380. #endif
  381. //delete the old value
  382. this->driver_control_register_value &=0xFFFF0ul;
  383. //set the new value
  384. this->driver_control_register_value |=setting_pattern;
  385. //if started we directly send it to the motor
  386. if (started) {
  387. send262(driver_control_register_value);
  388. }
  389. //recalculate the stepping delay by simply setting the speed again
  390. this->setSpeed(this->speed);
  391. }
  392. /*
  393. * returns the effective number of microsteps at the moment
  394. */
  395. int TMC26XStepper::getMicrosteps(void) {
  396. return microsteps;
  397. }
  398. /*
  399. * constant_off_time: The off time setting controls the minimum chopper frequency.
  400. * For most applications an off time within the range of 5μs to 20μs will fit.
  401. * 2...15: off time setting
  402. *
  403. * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
  404. * duration of the ringing on the sense resistor. For
  405. * 0: min. setting 3: max. setting
  406. *
  407. * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.
  408. * 0: slow decay only
  409. * 1...15: duration of fast decay phase
  410. *
  411. * sine_wave_offset: Sine wave offset. With CHM=1, these bits control the sine wave offset.
  412. * A positive offset corrects for zero crossing error.
  413. * -3..-1: negative offset 0: no offset 1...12: positive offset
  414. *
  415. * use_current_comparator: Selects usage of the current comparator for termination of the fast decay cycle.
  416. * If current comparator is enabled, it terminates the fast decay cycle in case the current
  417. * reaches a higher negative value than the actual positive value.
  418. * 1: enable comparator termination of fast decay cycle
  419. * 0: end by time only
  420. */
  421. void TMC26XStepper::setConstantOffTimeChopper(char constant_off_time, char blank_time, char fast_decay_time_setting, char sine_wave_offset, unsigned char use_current_comparator) {
  422. //perform some sanity checks
  423. if (constant_off_time<2) {
  424. constant_off_time=2;
  425. } else if (constant_off_time>15) {
  426. constant_off_time=15;
  427. }
  428. //save the constant off time
  429. this->constant_off_time = constant_off_time;
  430. char blank_value;
  431. //calculate the value acc to the clock cycles
  432. if (blank_time>=54) {
  433. blank_value=3;
  434. } else if (blank_time>=36) {
  435. blank_value=2;
  436. } else if (blank_time>=24) {
  437. blank_value=1;
  438. } else {
  439. blank_value=0;
  440. }
  441. if (fast_decay_time_setting<0) {
  442. fast_decay_time_setting=0;
  443. } else if (fast_decay_time_setting>15) {
  444. fast_decay_time_setting=15;
  445. }
  446. if (sine_wave_offset < -3) {
  447. sine_wave_offset = -3;
  448. } else if (sine_wave_offset>12) {
  449. sine_wave_offset = 12;
  450. }
  451. //shift the sine_wave_offset
  452. sine_wave_offset +=3;
  453. //calculate the register setting
  454. //first of all delete all the values for this
  455. chopper_config_register &= ~((1<<12) | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
  456. //set the constant off pattern
  457. chopper_config_register |= CHOPPER_MODE_T_OFF_FAST_DECAY;
  458. //set the blank timing value
  459. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  460. //setting the constant off time
  461. chopper_config_register |= constant_off_time;
  462. //set the fast decay time
  463. //set msb
  464. chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x8))<<HYSTERESIS_DECREMENT_SHIFT);
  465. //other bits
  466. chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x7))<<HYSTERESIS_START_VALUE_SHIFT);
  467. //set the sine wave offset
  468. chopper_config_register |= (unsigned long)sine_wave_offset << HYSTERESIS_LOW_SHIFT;
  469. //using the current comparator?
  470. if (!use_current_comparator) {
  471. chopper_config_register |= (1<<12);
  472. }
  473. //if started we directly send it to the motor
  474. if (started) {
  475. send262(driver_control_register_value);
  476. }
  477. }
  478. /*
  479. * constant_off_time: The off time setting controls the minimum chopper frequency.
  480. * For most applications an off time within the range of 5μs to 20μs will fit.
  481. * 2...15: off time setting
  482. *
  483. * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
  484. * duration of the ringing on the sense resistor. For
  485. * 0: min. setting 3: max. setting
  486. *
  487. * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.
  488. * 1...8
  489. *
  490. * hysteresis_end: Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by HDEC.
  491. * The sum HSTRT+HEND must be <16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.
  492. * -3..-1: negative HEND 0: zero HEND 1...12: positive HEND
  493. *
  494. * hysteresis_decrement: Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time.
  495. * 0: fast decrement 3: very slow decrement
  496. */
  497. void TMC26XStepper::setSpreadCycleChopper(char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement) {
  498. //perform some sanity checks
  499. if (constant_off_time<2) {
  500. constant_off_time=2;
  501. } else if (constant_off_time>15) {
  502. constant_off_time=15;
  503. }
  504. //save the constant off time
  505. this->constant_off_time = constant_off_time;
  506. char blank_value;
  507. //calculate the value acc to the clock cycles
  508. if (blank_time>=54) {
  509. blank_value=3;
  510. } else if (blank_time>=36) {
  511. blank_value=2;
  512. } else if (blank_time>=24) {
  513. blank_value=1;
  514. } else {
  515. blank_value=0;
  516. }
  517. if (hysteresis_start<1) {
  518. hysteresis_start=1;
  519. } else if (hysteresis_start>8) {
  520. hysteresis_start=8;
  521. }
  522. hysteresis_start--;
  523. if (hysteresis_end < -3) {
  524. hysteresis_end = -3;
  525. } else if (hysteresis_end>12) {
  526. hysteresis_end = 12;
  527. }
  528. //shift the hysteresis_end
  529. hysteresis_end +=3;
  530. if (hysteresis_decrement<0) {
  531. hysteresis_decrement=0;
  532. } else if (hysteresis_decrement>3) {
  533. hysteresis_decrement=3;
  534. }
  535. //first of all delete all the values for this
  536. chopper_config_register &= ~(CHOPPER_MODE_T_OFF_FAST_DECAY | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
  537. //set the blank timing value
  538. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  539. //setting the constant off time
  540. chopper_config_register |= constant_off_time;
  541. //set the hysteresis_start
  542. chopper_config_register |= ((unsigned long)hysteresis_start) << HYSTERESIS_START_VALUE_SHIFT;
  543. //set the hysteresis end
  544. chopper_config_register |= ((unsigned long)hysteresis_end) << HYSTERESIS_LOW_SHIFT;
  545. //set the hystereis decrement
  546. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  547. //if started we directly send it to the motor
  548. if (started) {
  549. send262(driver_control_register_value);
  550. }
  551. }
  552. /*
  553. * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized.
  554. * The frequency of each chopper mainly depends on the coil current and the position dependant motor coil inductivity, thus it depends on the microstep position.
  555. * With some motors a slightly audible beat can occur between the chopper frequencies, especially when they are near to each other. This typically occurs at a
  556. * few microstep positions within each quarter wave. This effect normally is not audible when compared to mechanical noise generated by ball bearings, etc.
  557. * Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.
  558. * Hint: A common factor, which can cause motor noise, is a bad PCB layout causing coupling of both sense resistor voltages
  559. * (please refer to sense resistor layout hint in chapter 8.1).
  560. * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided.
  561. * It modulates the slow decay time setting when switched on by the RNDTF bit. The RNDTF feature further spreads the chopper spectrum,
  562. * reducing electromagnetic emission on single frequencies.
  563. */
  564. void TMC26XStepper::setRandomOffTime(char value) {
  565. if (value) {
  566. chopper_config_register |= RANDOM_TOFF_TIME;
  567. } else {
  568. chopper_config_register &= ~(RANDOM_TOFF_TIME);
  569. }
  570. //if started we directly send it to the motor
  571. if (started) {
  572. send262(driver_control_register_value);
  573. }
  574. }
  575. void TMC26XStepper::setCoolStepConfiguration(unsigned int lower_SG_threshold, unsigned int SG_hysteresis, unsigned char current_decrement_step_size,
  576. unsigned char current_increment_step_size, unsigned char lower_current_limit) {
  577. //sanitize the input values
  578. if (lower_SG_threshold>480) {
  579. lower_SG_threshold = 480;
  580. }
  581. //divide by 32
  582. lower_SG_threshold >>=5;
  583. if (SG_hysteresis>480) {
  584. SG_hysteresis=480;
  585. }
  586. //divide by 32
  587. SG_hysteresis >>=5;
  588. if (current_decrement_step_size>3) {
  589. current_decrement_step_size=3;
  590. }
  591. if (current_increment_step_size>3) {
  592. current_increment_step_size=3;
  593. }
  594. if (lower_current_limit>1) {
  595. lower_current_limit=1;
  596. }
  597. //store the lower level in order to enable/disable the cool step
  598. this->cool_step_lower_threshold=lower_SG_threshold;
  599. //if cool step is not enabled we delete the lower value to keep it disabled
  600. if (!this->cool_step_enabled) {
  601. lower_SG_threshold=0;
  602. }
  603. //the good news is that we can start with a complete new cool step register value
  604. //and simply set the values in the register
  605. cool_step_register_value = ((unsigned long)lower_SG_threshold) | (((unsigned long)SG_hysteresis)<<8) | (((unsigned long)current_decrement_step_size)<<5)
  606. | (((unsigned long)current_increment_step_size)<<13) | (((unsigned long)lower_current_limit)<<15)
  607. //and of course we have to include the signature of the register
  608. | COOL_STEP_REGISTER;
  609. //Serial.println(cool_step_register_value,HEX);
  610. if (started) {
  611. send262(cool_step_register_value);
  612. }
  613. }
  614. void TMC26XStepper::setCoolStepEnabled(boolean enabled) {
  615. //simply delete the lower limit to disable the cool step
  616. cool_step_register_value &= ~SE_MIN_PATTERN;
  617. //and set it to the proper value if cool step is to be enabled
  618. if (enabled) {
  619. cool_step_register_value |=this->cool_step_lower_threshold;
  620. }
  621. //and save the enabled status
  622. this->cool_step_enabled = enabled;
  623. //save the register value
  624. if (started) {
  625. send262(cool_step_register_value);
  626. }
  627. }
  628. boolean TMC26XStepper::isCoolStepEnabled(void) {
  629. return this->cool_step_enabled;
  630. }
  631. unsigned int TMC26XStepper::getCoolStepLowerSgThreshold() {
  632. //we return our internally stored value - in order to provide the correct setting even if cool step is not enabled
  633. return this->cool_step_lower_threshold<<5;
  634. }
  635. unsigned int TMC26XStepper::getCoolStepUpperSgThreshold() {
  636. return (unsigned char)((cool_step_register_value & SE_MAX_PATTERN)>>8)<<5;
  637. }
  638. unsigned char TMC26XStepper::getCoolStepCurrentIncrementSize() {
  639. return (unsigned char)((cool_step_register_value & CURRENT_DOWN_STEP_SPEED_PATTERN)>>13);
  640. }
  641. unsigned char TMC26XStepper::getCoolStepNumberOfSGReadings() {
  642. return (unsigned char)((cool_step_register_value & SE_CURRENT_STEP_WIDTH_PATTERN)>>5);
  643. }
  644. unsigned char TMC26XStepper::getCoolStepLowerCurrentLimit() {
  645. return (unsigned char)((cool_step_register_value & MINIMUM_CURRENT_FOURTH)>>15);
  646. }
  647. void TMC26XStepper::setEnabled(boolean enabled) {
  648. //delete the t_off in the chopper config to get sure
  649. chopper_config_register &= ~(T_OFF_PATTERN);
  650. if (enabled) {
  651. //and set the t_off time
  652. chopper_config_register |= this->constant_off_time;
  653. }
  654. //if not enabled we don't have to do anything since we already delete t_off from the register
  655. if (started) {
  656. send262(chopper_config_register);
  657. }
  658. }
  659. boolean TMC26XStepper::isEnabled() {
  660. if (chopper_config_register & T_OFF_PATTERN) {
  661. return true;
  662. } else {
  663. return false;
  664. }
  665. }
  666. /*
  667. * reads a value from the TMC26X status register. The value is not obtained directly but can then
  668. * be read by the various status routines.
  669. *
  670. */
  671. void TMC26XStepper::readStatus(char read_value) {
  672. unsigned long old_driver_configuration_register_value = driver_configuration_register_value;
  673. //reset the readout configuration
  674. driver_configuration_register_value &= ~(READ_SELECTION_PATTERN);
  675. //this now equals TMC26X_READOUT_POSITION - so we just have to check the other two options
  676. if (read_value == TMC26X_READOUT_STALLGUARD) {
  677. driver_configuration_register_value |= READ_STALL_GUARD_READING;
  678. } else if (read_value == TMC26X_READOUT_CURRENT) {
  679. driver_configuration_register_value |= READ_STALL_GUARD_AND_COOL_STEP;
  680. }
  681. //all other cases are ignored to prevent funny values
  682. //check if the readout is configured for the value we are interested in
  683. if (driver_configuration_register_value!=old_driver_configuration_register_value) {
  684. //because then we need to write the value twice - one time for configuring, second time to get the value, see below
  685. send262(driver_configuration_register_value);
  686. }
  687. //write the configuration to get the last status
  688. send262(driver_configuration_register_value);
  689. }
  690. int TMC26XStepper::getMotorPosition(void) {
  691. //we read it out even if we are not started yet - perhaps it is useful information for somebody
  692. readStatus(TMC26X_READOUT_POSITION);
  693. return getReadoutValue();
  694. }
  695. //reads the stall guard setting from last status
  696. //returns -1 if stallguard information is not present
  697. int TMC26XStepper::getCurrentStallGuardReading(void) {
  698. //if we don't yet started there cannot be a stall guard value
  699. if (!started) {
  700. return -1;
  701. }
  702. //not time optimal, but solution optiomal:
  703. //first read out the stall guard value
  704. readStatus(TMC26X_READOUT_STALLGUARD);
  705. return getReadoutValue();
  706. }
  707. unsigned char TMC26XStepper::getCurrentCSReading(void) {
  708. //if we don't yet started there cannot be a stall guard value
  709. if (!started) {
  710. return 0;
  711. }
  712. //not time optimal, but solution optiomal:
  713. //first read out the stall guard value
  714. readStatus(TMC26X_READOUT_CURRENT);
  715. return (getReadoutValue() & 0x1f);
  716. }
  717. unsigned int TMC26XStepper::getCurrentCurrent(void) {
  718. double result = (double)getCurrentCSReading();
  719. double resistor_value = (double)this->resistor;
  720. double voltage = (driver_configuration_register_value & VSENSE)? 0.165:0.31;
  721. result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
  722. return (unsigned int)result;
  723. }
  724. /*
  725. return true if the stallguard threshold has been reached
  726. */
  727. boolean TMC26XStepper::isStallGuardOverThreshold(void) {
  728. if (!this->started) {
  729. return false;
  730. }
  731. return (driver_status_result & STATUS_STALL_GUARD_STATUS);
  732. }
  733. /*
  734. returns if there is any over temperature condition:
  735. OVER_TEMPERATURE_PREWARING if pre warning level has been reached
  736. OVER_TEMPERATURE_SHUTDOWN if the temperature is so hot that the driver is shut down
  737. Any of those levels are not too good.
  738. */
  739. char TMC26XStepper::getOverTemperature(void) {
  740. if (!this->started) {
  741. return 0;
  742. }
  743. if (driver_status_result & STATUS_OVER_TEMPERATURE_SHUTDOWN) {
  744. return TMC26X_OVERTEMPERATURE_SHUTDOWN;
  745. }
  746. if (driver_status_result & STATUS_OVER_TEMPERATURE_WARNING) {
  747. return TMC26X_OVERTEMPERATURE_PREWARING;
  748. }
  749. return 0;
  750. }
  751. //is motor channel A shorted to ground
  752. boolean TMC26XStepper::isShortToGroundA(void) {
  753. if (!this->started) {
  754. return false;
  755. }
  756. return (driver_status_result & STATUS_SHORT_TO_GROUND_A);
  757. }
  758. //is motor channel B shorted to ground
  759. boolean TMC26XStepper::isShortToGroundB(void) {
  760. if (!this->started) {
  761. return false;
  762. }
  763. return (driver_status_result & STATUS_SHORT_TO_GROUND_B);
  764. }
  765. //is motor channel A connected
  766. boolean TMC26XStepper::isOpenLoadA(void) {
  767. if (!this->started) {
  768. return false;
  769. }
  770. return (driver_status_result & STATUS_OPEN_LOAD_A);
  771. }
  772. //is motor channel B connected
  773. boolean TMC26XStepper::isOpenLoadB(void) {
  774. if (!this->started) {
  775. return false;
  776. }
  777. return (driver_status_result & STATUS_OPEN_LOAD_B);
  778. }
  779. //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
  780. boolean TMC26XStepper::isStandStill(void) {
  781. if (!this->started) {
  782. return false;
  783. }
  784. return (driver_status_result & STATUS_STAND_STILL);
  785. }
  786. //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
  787. boolean TMC26XStepper::isStallGuardReached(void) {
  788. if (!this->started) {
  789. return false;
  790. }
  791. return (driver_status_result & STATUS_STALL_GUARD_STATUS);
  792. }
  793. //reads the stall guard setting from last status
  794. //returns -1 if stallguard inforamtion is not present
  795. int TMC26XStepper::getReadoutValue(void) {
  796. return (int)(driver_status_result >> 10);
  797. }
  798. int TMC26XStepper::getResistor() {
  799. return this->resistor;
  800. }
  801. boolean TMC26XStepper::isCurrentScalingHalfed() {
  802. if (this->driver_configuration_register_value & VSENSE) {
  803. return true;
  804. } else {
  805. return false;
  806. }
  807. }
  808. /*
  809. version() returns the version of the library:
  810. */
  811. int TMC26XStepper::version(void)
  812. {
  813. return 1;
  814. }
  815. void TMC26XStepper::debugLastStatus() {
  816. #ifdef DEBUG
  817. if (this->started) {
  818. if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_PREWARING) {
  819. Serial.println("WARNING: Overtemperature Prewarning!");
  820. } else if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_SHUTDOWN) {
  821. Serial.println("ERROR: Overtemperature Shutdown!");
  822. }
  823. if (this->isShortToGroundA()) {
  824. Serial.println("ERROR: SHORT to ground on channel A!");
  825. }
  826. if (this->isShortToGroundB()) {
  827. Serial.println("ERROR: SHORT to ground on channel A!");
  828. }
  829. if (this->isOpenLoadA()) {
  830. Serial.println("ERROR: Channel A seems to be unconnected!");
  831. }
  832. if (this->isOpenLoadB()) {
  833. Serial.println("ERROR: Channel B seems to be unconnected!");
  834. }
  835. if (this->isStallGuardReached()) {
  836. Serial.println("INFO: Stall Guard level reached!");
  837. }
  838. if (this->isStandStill()) {
  839. Serial.println("INFO: Motor is standing still.");
  840. }
  841. unsigned long readout_config = driver_configuration_register_value & READ_SELECTION_PATTERN;
  842. int value = getReadoutValue();
  843. if (readout_config == READ_MICROSTEP_POSTION) {
  844. Serial.print("Microstep postion phase A: ");
  845. Serial.println(value);
  846. } else if (readout_config == READ_STALL_GUARD_READING) {
  847. Serial.print("Stall Guard value:");
  848. Serial.println(value);
  849. } else if (readout_config == READ_STALL_GUARD_AND_COOL_STEP) {
  850. int stallGuard = value & 0xf;
  851. int current = value & 0x1F0;
  852. Serial.print("Approx Stall Guard: ");
  853. Serial.println(stallGuard);
  854. Serial.print("Current level");
  855. Serial.println(current);
  856. }
  857. }
  858. #endif
  859. }
  860. /*
  861. * send register settings to the stepper driver via SPI
  862. * returns the current status
  863. */
  864. inline void TMC26XStepper::send262(unsigned long datagram) {
  865. unsigned long i_datagram;
  866. //preserver the previous spi mode
  867. unsigned char oldMode = SPCR & SPI_MODE_MASK;
  868. //if the mode is not correct set it to mode 3
  869. if (oldMode != SPI_MODE3) {
  870. SPI.setDataMode(SPI_MODE3);
  871. }
  872. //select the TMC driver
  873. digitalWrite(cs_pin,LOW);
  874. //ensure that only valid bist are set (0-19)
  875. //datagram &=REGISTER_BIT_PATTERN;
  876. #ifdef DEBUG
  877. Serial.print("Sending ");
  878. Serial.println(datagram,HEX);
  879. #endif
  880. //write/read the values
  881. i_datagram = SPI.transfer((datagram >> 16) & 0xff);
  882. i_datagram <<= 8;
  883. i_datagram |= SPI.transfer((datagram >> 8) & 0xff);
  884. i_datagram <<= 8;
  885. i_datagram |= SPI.transfer((datagram) & 0xff);
  886. i_datagram >>= 4;
  887. #ifdef DEBUG
  888. Serial.print("Received ");
  889. Serial.println(i_datagram,HEX);
  890. debugLastStatus();
  891. #endif
  892. //deselect the TMC chip
  893. digitalWrite(cs_pin,HIGH);
  894. //restore the previous SPI mode if neccessary
  895. //if the mode is not correct set it to mode 3
  896. if (oldMode != SPI_MODE3) {
  897. SPI.setDataMode(oldMode);
  898. }
  899. //store the datagram as status result
  900. driver_status_result = i_datagram;
  901. }