My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMC26XStepper.h 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. // ensure this library description is only included once
  22. #ifndef TMC26XStepper_h
  23. #define TMC26XStepper_h
  24. //! return value for TMC26XStepper.getOverTemperature() if there is a overtemperature situation in the TMC chip
  25. /*!
  26. * This warning indicates that the TCM chip is too warm.
  27. * It is still working but some parameters may be inferior.
  28. * You should do something against it.
  29. */
  30. #define TMC26X_OVERTEMPERATURE_PREWARING 1
  31. //! return value for TMC26XStepper.getOverTemperature() if there is a overtemperature shutdown in the TMC chip
  32. /*!
  33. * This warning indicates that the TCM chip is too warm to operate and has shut down to prevent damage.
  34. * It will stop working until it cools down again.
  35. * If you encouter this situation you must do something against it. Like reducing the current or improving the PCB layout
  36. * and/or heat management.
  37. */
  38. #define TMC26X_OVERTEMPERATURE_SHUTDOWN 2
  39. //which values can be read out
  40. /*!
  41. * Selects to readout the microstep position from the motor.
  42. *\sa readStatus()
  43. */
  44. #define TMC26X_READOUT_POSITION 0
  45. /*!
  46. * Selects to read out the StallGuard value of the motor.
  47. *\sa readStatus()
  48. */
  49. #define TMC26X_READOUT_STALLGUARD 1
  50. /*!
  51. * Selects to read out the current current setting (acc. to CoolStep) and the upper bits of the StallGuard value from the motor.
  52. *\sa readStatus(), setCurrent()
  53. */
  54. #define TMC26X_READOUT_CURRENT 3
  55. /*!
  56. * Define to set the minimum current for CoolStep operation to 1/2 of the selected CS minium.
  57. *\sa setCoolStepConfiguration()
  58. */
  59. #define COOL_STEP_HALF_CS_LIMIT 0
  60. /*!
  61. * Define to set the minimum current for CoolStep operation to 1/4 of the selected CS minium.
  62. *\sa setCoolStepConfiguration()
  63. */
  64. #define COOL_STEP_QUARTDER_CS_LIMIT 1
  65. /*!
  66. * \class TMC26XStepper
  67. * \brief Class representing a TMC26X stepper driver
  68. *
  69. * In order to use one fo those drivers in your Arduino code you have to create an object of that class:
  70. * \code
  71. * TMC26XStepper stepper = TMC26XStepper(200,1,2,3,500);
  72. * \endcode
  73. * see TMC26XStepper(int number_of_steps, int cs_pin, int dir_pin, int step_pin, unsigned int rms_current)
  74. *
  75. * Keep in mind that you need to start the driver with start() in order to get the TMC26X configured.
  76. *
  77. * The most important function is the move(). It checks if the motor has to do a step or not.
  78. * It is important that you call move() as often as possible in your Arduino loop() routine. I suggest
  79. * to use a very fast loop routine and always call it at the beginning or the end.
  80. *
  81. * In order to move you have to provide a movement speed with setSpeed(). The speed is a positive value setting
  82. * the rotations per minute.
  83. *
  84. * To really move the motor you have to call step() to tell the driver to move the motor the given number
  85. * of steps in the given direction. Positive values move the motor in one direction, negative values in the other direction.
  86. *
  87. * You can check with isMoving() if the mototr is still moving or stop it apruptely with stop().
  88. */
  89. class TMC26XStepper {
  90. public:
  91. /*!
  92. * \brief creates a new represenatation of a stepper motor connected to a TMC26X stepper driver
  93. *
  94. * This is the main constructor. If in doubt use this. You must provide all parameters as described below.
  95. *
  96. * \param number_of_steps the number of steps the motor has per rotation.
  97. * \param cs_pin The Arduino pin you have connected the Cient Select Pin (!CS) of the TMC26X for SPI
  98. * \param dir_pin the number of the Arduino pin the Direction input of the TMC26X is connected
  99. * \param step_pin the number of the Arduino pin the step pin of the TMC26X driver is connected.
  100. * \param rms_current the maximum current to privide to the motor in mA (!). A value of 200 would send up to 200mA to the motor
  101. * \param resistor the current sense resistor in milli Ohm, defaults to ,15 Ohm ( or 150 milli Ohm) as in the TMC260 Arduino Shield
  102. *
  103. * Keep in mind that you must also call TMC26XStepper.start() in order to configure the stepper driver for use.
  104. *
  105. * By default the Constant Off Time chopper is used, see TCM262Stepper.setConstantOffTimeChopper() for details.
  106. * This should work on most motors (YMMV). You may want to configure and use the Spread Cycle Chopper, see setSpreadCycleChopper().
  107. *
  108. * By default a microstepping of 1/32th is used to provide a smooth motor run, while still giving a good progression per step.
  109. * You can select a different stepping with setMicrosteps() to aa different value.
  110. * \sa start(), setMicrosteps()
  111. */
  112. TMC26XStepper(int number_of_steps, int cs_pin, int dir_pin, int step_pin, unsigned int current, unsigned int resistor=150);
  113. /*!
  114. * \brief configures and starts the TMC26X stepper driver. Before you called this function the stepper driver is in nonfunctional mode.
  115. *
  116. * This routine configures the TMC26X stepper driver for the given values via SPI.
  117. * Most member functions are non functional if the driver has not been started.
  118. * Therefore it is best to call this in your Arduino setup() function.
  119. */
  120. void start();
  121. /*!
  122. * \brief resets the stepper in unconfigured mode.
  123. *
  124. * This routine enables you to call start again. It does not change anything
  125. * in the internal stepper configuration or the desired configuration.
  126. * It just marks the stepper as not yet startet. You do not have to reconfigure
  127. * the stepper to start it again, but it is not reset to any factory settings
  128. * this has to be configured back by yourself.
  129. * (Hint: Normally you do not need this function)
  130. */
  131. void un_start();
  132. /*!
  133. * \brief Sets the rotation speed in revolutions per minute.
  134. * \param whatSpeed the desired speed in rotations per minute.
  135. */
  136. void setSpeed(unsigned int whatSpeed);
  137. /*!
  138. * \brief reads out the currently selected speed in revolutions per minute.
  139. * \sa setSpeed()
  140. */
  141. unsigned int getSpeed(void);
  142. /*!
  143. * \brief Set the number of microsteps in 2^i values (rounded) up to 256
  144. *
  145. * This method set's the number of microsteps per step in 2^i interval.
  146. * This means you can select 1, 2, 4, 16, 32, 64, 128 or 256 as valid microsteps.
  147. * If you give any other value it will be rounded to the next smaller number (3 would give a microstepping of 2).
  148. * You can always check the current microstepping with getMicrosteps().
  149. */
  150. void setMicrosteps(int number_of_steps);
  151. /*!
  152. * \brief returns the effective current number of microsteps selected.
  153. *
  154. * This function always returns the effective number of microsteps.
  155. * This can be a bit different than the micro steps set in setMicrosteps() since it is rounded to 2^i.
  156. *
  157. * \sa setMicrosteps()
  158. */
  159. int getMicrosteps(void);
  160. /*!
  161. * \brief Initiate a movement for the given number of steps. Positive numbers move in one, negative numbers in the other direction.
  162. *
  163. * \param number_of_steps The number of steps to move the motor.
  164. * \return 0 if the motor was not moving and moves now. -1 if the motor is moving and the new steps could not be set.
  165. *
  166. * If the previous movement is not finished yet the function will return -1 and not change the steps to move the motor.
  167. * If the motor does not move it return 0
  168. *
  169. * The direction of the movement is indicated by the sign of the steps parameter. It is not determinable if positive values are right
  170. * or left This depends on the internal construction of the motor and how you connected it to the stepper driver.
  171. *
  172. * You can always verify with isMoving() or even use stop() to stop the motor before giving it new step directions.
  173. * \sa isMoving(), getStepsLeft(), stop()
  174. */
  175. char step(int number_of_steps);
  176. /*!
  177. * \brief Central movement method, must be called as often as possible in the lopp function and is very fast.
  178. *
  179. * This routine checks if the motor still has to move, if the waiting delay has passed to send a new step command to the motor
  180. * and manages the number of steps yet to move to fulfill the current move command.
  181. *
  182. * This function is implemented to be as fast as possible to call it as often as possible in your loop routine.
  183. * The more regurlarly you call this function the better. In both senses of 'regularly': Calling it as often as
  184. * possible is not a bad idea and if you even manage that the intervals you call this function are not too irregular helps too.
  185. *
  186. * You can call this routine even if you know that the motor is not miving. It introduces just a very small penalty in your code.
  187. * You must not call isMoving() to determine if you need to call this function, since taht is done internally already and only
  188. * slows down you code.
  189. *
  190. * How often you call this function directly influences your top miving speed for the motor. It may be a good idea to call this
  191. * from an timer overflow interrupt to ensure proper calling.
  192. * \sa step()
  193. */
  194. char move(void);
  195. /*!
  196. * \brief checks if the motor still has to move to fulfill the last movement command.
  197. * \return 0 if the motor stops, -1 if the motor is moving.
  198. *
  199. * This method can be used to determine if the motor is ready for new movements.
  200. *\sa step(), move()
  201. */
  202. char isMoving(void);
  203. /*!
  204. * \brief Get the number of steps left in the current movement.
  205. * \return The number of steps left in the movement. This number is always positive.
  206. */
  207. unsigned int getStepsLeft(void);
  208. /*!
  209. * \brief Stops the motor regardless if it moves or not.
  210. * \return -1 if the motor was moving and is really stoped or 0 if it was not moving at all.
  211. *
  212. * This method directly and apruptely stops the motor and may be used as an emergency stop.
  213. */
  214. char stop(void);
  215. /*!
  216. * \brief Sets and configure the classical Constant Off Timer Chopper
  217. * \param constant_off_time The off time setting controls the minimum chopper frequency. For most applications an off time within the range of 5μs to 20μs will fit. Setting this parameter to zero completely disables all driver transistors and the motor can free-wheel. 0: chopper off, 1:15: off time setting (1 will work with minimum blank time of 24 clocks)
  218. * \param blank_time Selects the comparator blank time. This time needs to safely cover the switching event and the duration of the ringing on the sense resistor. For most low current drivers, a setting of 1 or 2 is good. For high current applications with large MOSFETs, a setting of 2 or 3 will be required. 0 (min setting) … (3) amx setting
  219. * \param fast_decay_time_setting Fast decay time setting. Controls the portion of fast decay for each chopper cycle. 0: slow decay only, 1…15: duration of fast decay phase
  220. * \param sine_wave_offset Sine wave offset. Controls the sine wave offset. A positive offset corrects for zero crossing error. -3…-1: negative offset, 0: no offset,1…12: positive offset
  221. * \param use_curreent_comparator Selects usage of the current comparator for termination of the fast decay cycle. If current comparator is enabled, it terminates the fast decay cycle in case the current reaches a higher negative value than the actual positive value. (0 disable, -1 enable).
  222. *
  223. * The classic constant off time chopper uses a fixed portion of fast decay following each on phase.
  224. * While the duration of the on time is determined by the chopper comparator, the fast decay time needs
  225. * to be set by the user in a way, that the current decay is enough for the driver to be able to follow
  226. * the falling slope of the sine wave, and on the other hand it should not be too long, in order to minimize
  227. * motor current ripple and power dissipation. This best can be tuned using an oscilloscope or
  228. * trying out motor smoothness at different velocities. A good starting value is a fast decay time setting
  229. * similar to the slow decay time setting.
  230. * After tuning of the fast decay time, the offset should be determined, in order to have a smooth zero transition.
  231. * This is necessary, because the fast decay phase leads to the absolute value of the motor current being lower
  232. * than the target current (see figure 17). If the zero offset is too low, the motor stands still for a short
  233. * moment during current zero crossing, if it is set too high, it makes a larger microstep.
  234. * Typically, a positive offset setting is required for optimum operation.
  235. *
  236. * \sa setSpreadCycleChoper() for other alternatives.
  237. * \sa setRandomOffTime() for spreading the noise over a wider spectrum
  238. */
  239. void setConstantOffTimeChopper(char constant_off_time, char blank_time, char fast_decay_time_setting, char sine_wave_offset, unsigned char use_current_comparator);
  240. /*!
  241. * \brief Sets and configures with spread cycle chopper.
  242. * \param constant_off_time The off time setting controls the minimum chopper frequency. For most applications an off time within the range of 5μs to 20μs will fit. Setting this parameter to zero completely disables all driver transistors and the motor can free-wheel. 0: chopper off, 1:15: off time setting (1 will work with minimum blank time of 24 clocks)
  243. * \param blank_time Selects the comparator blank time. This time needs to safely cover the switching event and the duration of the ringing on the sense resistor. For most low current drivers, a setting of 1 or 2 is good. For high current applications with large MOSFETs, a setting of 2 or 3 will be required. 0 (min setting) … (3) amx setting
  244. * \param hysteresis_start Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value. 1 … 8
  245. * \param hysteresis_end Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by hysteresis_decrement. The sum hysteresis_start + hysteresis_end must be <16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.
  246. * \param hysteresis_decrement Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time. 0 (fast decrement) … 3 (slow decrement).
  247. *
  248. * The spreadCycle chopper scheme (pat.fil.) is a precise and simple to use chopper principle, which automatically determines
  249. * the optimum fast decay portion for the motor. Anyhow, a number of settings can be made in order to optimally fit the driver
  250. * to the motor.
  251. * Each chopper cycle is comprised of an on-phase, a slow decay phase, a fast decay phase and a second slow decay phase.
  252. * The slow decay phases limit the maximum chopper frequency and are important for low motor and driver power dissipation.
  253. * The hysteresis start setting limits the chopper frequency by forcing the driver to introduce a minimum amount of
  254. * current ripple into the motor coils. The motor inductivity determines the ability to follow a changing motor current.
  255. * The duration of the on- and fast decay phase needs to cover at least the blank time, because the current comparator is
  256. * disabled during this time.
  257. *
  258. * \sa setRandomOffTime() for spreading the noise over a wider spectrum
  259. */
  260. void setSpreadCycleChopper(char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement);
  261. /*!
  262. * \brief Use random off time for noise reduction (0 for off, -1 for on).
  263. * \param value 0 for off, -1 for on
  264. *
  265. * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized.
  266. * The frequency of each chopper mainly depends on the coil current and the position dependant motor coil inductivity,
  267. * thus it depends on the microstep position. With some motors a slightly audible beat can occur between the chopper
  268. * frequencies, especially when they are near to each other. This typically occurs at a few microstep positions within
  269. * each quarter wave.
  270. * This effect normally is not audible when compared to mechanical noise generated by ball bearings,
  271. * etc. Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.
  272. * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided.
  273. * It modulates the slow decay time setting when switched on. The random off time feature further spreads the chopper spectrum,
  274. * reducing electromagnetic emission on single frequencies.
  275. */
  276. void setRandomOffTime(char value);
  277. /*!
  278. * \brief set the maximum motor current in mA (1000 is 1 Amp)
  279. * Keep in mind this is the maximum peak Current. The RMS current will be 1/sqrt(2) smaller. The actual current can also be smaller
  280. * by employing CoolStep.
  281. * \param current the maximum motor current in mA
  282. * \sa getCurrent(), getCurrentCurrent()
  283. */
  284. void setCurrent(unsigned int current);
  285. /*!
  286. * \brief readout the motor maximum current in mA (1000 is an Amp)
  287. * This is the maximum current. to get the current current - which may be affected by CoolStep us getCurrentCurrent()
  288. *\return the maximum motor current in milli amps
  289. * \sa getCurrentCurrent()
  290. */
  291. unsigned int getCurrent(void);
  292. /*!
  293. * \brief set the StallGuard threshold in order to get sensible StallGuard readings.
  294. * \param stall_guard_threshold -64 … 63 the StallGuard threshold
  295. * \param stall_guard_filter_enabled 0 if the filter is disabled, -1 if it is enabled
  296. *
  297. * The StallGuard threshold is used to optimize the StallGuard reading to sensible values. It should be at 0 at
  298. * the maximum allowable load on the otor (but not before). = is a good starting point (and the default)
  299. * If you get Stall Gaurd readings of 0 without any load or with too little laod increase the value.
  300. * If you get readings of 1023 even with load decrease the setting.
  301. *
  302. * If you switch on the filter the StallGuard reading is only updated each 4th full step to reduce the noise in the
  303. * reading.
  304. *
  305. * \sa getCurrentStallGuardReading() to read out the current value.
  306. */
  307. void setStallGuardThreshold(char stall_guard_threshold, char stall_guard_filter_enabled);
  308. /*!
  309. * \brief reads out the StallGuard threshold
  310. * \return a number between -64 and 63.
  311. */
  312. char getStallGuardThreshold(void);
  313. /*!
  314. * \brief returns the current setting of the StallGuard filter
  315. * \return 0 if not set, -1 if set
  316. */
  317. char getStallGuardFilter(void);
  318. /*!
  319. * \brief This method configures the CoolStep smart energy operation. You must have a proper StallGuard configuration for the motor situation (current, voltage, speed) in rder to use this feature.
  320. * \param lower_SG_threshold Sets the lower threshold for stallGuard2TM reading. Below this value, the motor current becomes increased. Allowed values are 0...480
  321. * \param SG_hysteresis Sets the distance between the lower and the upper threshold for stallGuard2TM reading. Above the upper threshold (which is lower_SG_threshold+SG_hysteresis+1) the motor current becomes decreased. Allowed values are 0...480
  322. * \param current_decrement_step_size Sets the current decrement steps. If the StallGuard value is above the threshold the current gets decremented by this step size. 0...32
  323. * \param current_increment_step_size Sets the current increment step. The current becomes incremented for each measured stallGuard2TM value below the lower threshold. 0...8
  324. * \param lower_current_limit Sets the lower motor current limit for coolStepTM operation by scaling the CS value. Values can be COOL_STEP_HALF_CS_LIMIT, COOL_STEP_QUARTER_CS_LIMIT
  325. * The CoolStep smart energy operation automatically adjust the current sent into the motor according to the current load,
  326. * read out by the StallGuard in order to provide the optimum torque with the minimal current consumption.
  327. * You configure the CoolStep current regulator by defining upper and lower bounds of StallGuard readouts. If the readout is above the
  328. * limit the current gets increased, below the limit the current gets decreased.
  329. * You can specify the upper an lower threshold of the StallGuard readout in order to adjust the current. You can also set the number of
  330. * StallGuard readings neccessary above or below the limit to get a more stable current adjustement.
  331. * The current adjustement itself is configured by the number of steps the current gests in- or decreased and the absolut minimum current
  332. * (1/2 or 1/4th otf the configured current).
  333. * \sa COOL_STEP_HALF_CS_LIMIT, COOL_STEP_QUARTER_CS_LIMIT
  334. */
  335. void setCoolStepConfiguration(unsigned int lower_SG_threshold, unsigned int SG_hysteresis, unsigned char current_decrement_step_size,
  336. unsigned char current_increment_step_size, unsigned char lower_current_limit);
  337. /*!
  338. * \brief enables or disables the CoolStep smart energy operation feature. It must be configured before enabling it.
  339. * \param enabled true if CoolStep should be enabled, false if not.
  340. * \sa setCoolStepConfiguration()
  341. */
  342. void setCoolStepEnabled(boolean enabled);
  343. /*!
  344. * \brief check if the CoolStep feature is enabled
  345. * \sa setCoolStepEnabled()
  346. */
  347. boolean isCoolStepEnabled();
  348. /*!
  349. * \brief returns the lower StallGuard threshold for the CoolStep operation
  350. * \sa setCoolStepConfiguration()
  351. */
  352. unsigned int getCoolStepLowerSgThreshold();
  353. /*!
  354. * \brief returns the upper StallGuard threshold for the CoolStep operation
  355. * \sa setCoolStepConfiguration()
  356. */
  357. unsigned int getCoolStepUpperSgThreshold();
  358. /*!
  359. * \brief returns the number of StallGuard readings befor CoolStep adjusts the motor current.
  360. * \sa setCoolStepConfiguration()
  361. */
  362. unsigned char getCoolStepNumberOfSGReadings();
  363. /*!
  364. * \brief returns the increment steps for the current for the CoolStep operation
  365. * \sa setCoolStepConfiguration()
  366. */
  367. unsigned char getCoolStepCurrentIncrementSize();
  368. /*!
  369. * \brief returns the absolut minium current for the CoolStep operation
  370. * \sa setCoolStepConfiguration()
  371. * \sa COOL_STEP_HALF_CS_LIMIT, COOL_STEP_QUARTER_CS_LIMIT
  372. */
  373. unsigned char getCoolStepLowerCurrentLimit();
  374. /*!
  375. * \brief Get the current microstep position for phase A
  376. * \return The current microstep position for phase A 0…255
  377. *
  378. * Keep in mind that this routine reads and writes a value via SPI - so this may take a bit time.
  379. */
  380. int getMotorPosition(void);
  381. /*!
  382. * \brief Reads the current StallGuard value.
  383. * \return The current StallGuard value, lesser values indicate higher load, 0 means stall detected.
  384. * Keep in mind that this routine reads and writes a value via SPI - so this may take a bit time.
  385. * \sa setStallGuardThreshold() for tuning the readout to sensible ranges.
  386. */
  387. int getCurrentStallGuardReading(void);
  388. /*!
  389. * \brief Reads the current current setting value as fraction of the maximum current
  390. * Returns values between 0 and 31, representing 1/32 to 32/32 (=1)
  391. * \sa setCoolStepConfiguration()
  392. */
  393. unsigned char getCurrentCSReading(void);
  394. /*!
  395. *\brief a convenience method to determine if the current scaling uses 0.31V or 0.165V as reference.
  396. *\return false if 0.13V is the reference voltage, true if 0.165V is used.
  397. */
  398. boolean isCurrentScalingHalfed();
  399. /*!
  400. * \brief Reads the current current setting value and recalculates the absolute current in mA (1A would be 1000).
  401. * This method calculates the currently used current setting (either by setting or by CoolStep) and reconstructs
  402. * the current in mA by usinge the VSENSE and resistor value. This method uses floating point math - so it
  403. * may not be the fastest.
  404. * \sa getCurrentCSReading(), getResistor(), isCurrentScalingHalfed(), getCurrent()
  405. */
  406. unsigned int getCurrentCurrent(void);
  407. /*!
  408. * \brief checks if there is a StallGuard warning in the last status
  409. * \return 0 if there was no warning, -1 if there was some warning.
  410. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  411. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  412. *
  413. * \sa setStallGuardThreshold() for tuning the readout to sensible ranges.
  414. */
  415. boolean isStallGuardOverThreshold(void);
  416. /*!
  417. * \brief Return over temperature status of the last status readout
  418. * return 0 is everything is OK, TMC26X_OVERTEMPERATURE_PREWARING if status is reached, TMC26X_OVERTEMPERATURE_SHUTDOWN is the chip is shutdown, -1 if the status is unknown.
  419. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  420. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  421. */
  422. char getOverTemperature(void);
  423. /*!
  424. * \brief Is motor channel A shorted to ground detected in the last status readout.
  425. * \return true is yes, false if not.
  426. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  427. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  428. */
  429. boolean isShortToGroundA(void);
  430. /*!
  431. * \brief Is motor channel B shorted to ground detected in the last status readout.
  432. * \return true is yes, false if not.
  433. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  434. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  435. */
  436. boolean isShortToGroundB(void);
  437. /*!
  438. * \brief iIs motor channel A connected according to the last statu readout.
  439. * \return true is yes, false if not.
  440. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  441. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  442. */
  443. boolean isOpenLoadA(void);
  444. /*!
  445. * \brief iIs motor channel A connected according to the last statu readout.
  446. * \return true is yes, false if not.
  447. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  448. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  449. */
  450. boolean isOpenLoadB(void);
  451. /*!
  452. * \brief Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
  453. * \return true is yes, false if not.
  454. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  455. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  456. */
  457. boolean isStandStill(void);
  458. /*!
  459. * \brief checks if there is a StallGuard warning in the last status
  460. * \return 0 if there was no warning, -1 if there was some warning.
  461. * Keep in mind that this method does not enforce a readout but uses the value of the last status readout.
  462. * You may want to use getMotorPosition() or getCurrentStallGuardReading() to enforce an updated status readout.
  463. *
  464. * \sa isStallGuardOverThreshold()
  465. * TODO why?
  466. *
  467. * \sa setStallGuardThreshold() for tuning the readout to sensible ranges.
  468. */
  469. boolean isStallGuardReached(void);
  470. /*!
  471. *\brief enables or disables the motor driver bridges. If disabled the motor can run freely. If enabled not.
  472. *\param enabled a boolean value true if the motor should be enabled, false otherwise.
  473. */
  474. void setEnabled(boolean enabled);
  475. /*!
  476. *\brief checks if the output bridges are enabled. If the bridges are not enabled the motor can run freely
  477. *\return true if the bridges and by that the motor driver are enabled, false if not.
  478. *\sa setEnabled()
  479. */
  480. boolean isEnabled();
  481. /*!
  482. * \brief Manually read out the status register
  483. * This function sends a byte to the motor driver in order to get the current readout. The parameter read_value
  484. * seletcs which value will get returned. If the read_vlaue changes in respect to the previous readout this method
  485. * automatically send two bytes to the motor: one to set the redout and one to get the actual readout. So this method
  486. * may take time to send and read one or two bits - depending on the previous readout.
  487. * \param read_value selects which value to read out (0..3). You can use the defines TMC26X_READOUT_POSITION, TMC_262_READOUT_STALLGUARD, or TMC_262_READOUT_CURRENT
  488. * \sa TMC26X_READOUT_POSITION, TMC_262_READOUT_STALLGUARD, TMC_262_READOUT_CURRENT
  489. */
  490. void readStatus(char read_value);
  491. /*!
  492. * \brief Returns the current sense resistor value in milliohm.
  493. * The default value of ,15 Ohm will return 150.
  494. */
  495. int getResistor();
  496. /*!
  497. * \brief Prints out all the information that can be found in the last status read out - it does not force a status readout.
  498. * The result is printed via Serial
  499. */
  500. void debugLastStatus(void);
  501. /*!
  502. * \brief library version
  503. * \return the version number as int.
  504. */
  505. int version(void);
  506. private:
  507. unsigned int steps_left; //the steps the motor has to do to complete the movement
  508. int direction; // Direction of rotation
  509. unsigned long step_delay; // delay between steps, in ms, based on speed
  510. int number_of_steps; // total number of steps this motor can take
  511. unsigned int speed; // we need to store the current speed in order to change the speed after changing microstepping
  512. unsigned int resistor; //current sense resitor value in milliohm
  513. unsigned long last_step_time; // time stamp in ms of when the last step was taken
  514. unsigned long next_step_time; // time stamp in ms of when the last step was taken
  515. //driver control register copies to easily set & modify the registers
  516. unsigned long driver_control_register_value;
  517. unsigned long chopper_config_register;
  518. unsigned long cool_step_register_value;
  519. unsigned long stall_guard2_current_register_value;
  520. unsigned long driver_configuration_register_value;
  521. //the driver status result
  522. unsigned long driver_status_result;
  523. //helper routione to get the top 10 bit of the readout
  524. inline int getReadoutValue();
  525. //the pins for the stepper driver
  526. unsigned char cs_pin;
  527. unsigned char step_pin;
  528. unsigned char dir_pin;
  529. //status values
  530. boolean started; //if the stepper has been started yet
  531. int microsteps; //the current number of micro steps
  532. char constant_off_time; //we need to remember this value in order to enable and disable the motor
  533. unsigned char cool_step_lower_threshold; // we need to remember the threshold to enable and disable the CoolStep feature
  534. boolean cool_step_enabled; //we need to remember this to configure the coolstep if it si enabled
  535. //SPI sender
  536. inline void send262(unsigned long datagram);
  537. };
  538. #endif