My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

_t_m_c26_x_stepper_8cpp_source.html 118KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <title>Trinamic TMC26X Stepper Driver for Arduino: TMC26XStepper.cpp Source File</title>
  7. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  8. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  9. </head>
  10. <body>
  11. <div id="top"><!-- do not remove this div! -->
  12. <div id="titlearea">
  13. <table cellspacing="0" cellpadding="0">
  14. <tbody>
  15. <tr style="height: 56px;">
  16. <td style="padding-left: 0.5em;">
  17. <div id="projectname">Trinamic TMC26X Stepper Driver for Arduino
  18. </div>
  19. </td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </div>
  24. <!-- Generated by Doxygen 1.7.6.1 -->
  25. <div id="navrow1" class="tabs">
  26. <ul class="tablist">
  27. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  28. <li><a href="annotated.html"><span>Classes</span></a></li>
  29. <li class="current"><a href="files.html"><span>Files</span></a></li>
  30. </ul>
  31. </div>
  32. <div id="navrow2" class="tabs2">
  33. <ul class="tablist">
  34. <li><a href="files.html"><span>File&#160;List</span></a></li>
  35. <li><a href="globals.html"><span>File&#160;Members</span></a></li>
  36. </ul>
  37. </div>
  38. </div>
  39. <div class="header">
  40. <div class="headertitle">
  41. <div class="title">TMC26XStepper.cpp</div> </div>
  42. </div><!--header-->
  43. <div class="contents">
  44. <a href="_t_m_c26_x_stepper_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
  45. <a name="l00002"></a>00002 <span class="comment"> TMC26XStepper.cpp - - TMC26X Stepper library for Wiring/Arduino - Version 0.1</span>
  46. <a name="l00003"></a>00003 <span class="comment"> </span>
  47. <a name="l00004"></a>00004 <span class="comment"> based on the stepper library by Tom Igoe, et. al.</span>
  48. <a name="l00005"></a>00005 <span class="comment"> </span>
  49. <a name="l00006"></a>00006 <span class="comment"> Copyright (c) 2011, Interactive Matter, Marcus Nowotny</span>
  50. <a name="l00007"></a>00007 <span class="comment"> </span>
  51. <a name="l00008"></a>00008 <span class="comment"> Permission is hereby granted, free of charge, to any person obtaining a copy</span>
  52. <a name="l00009"></a>00009 <span class="comment"> of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
  53. <a name="l00010"></a>00010 <span class="comment"> in the Software without restriction, including without limitation the rights</span>
  54. <a name="l00011"></a>00011 <span class="comment"> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
  55. <a name="l00012"></a>00012 <span class="comment"> copies of the Software, and to permit persons to whom the Software is</span>
  56. <a name="l00013"></a>00013 <span class="comment"> furnished to do so, subject to the following conditions:</span>
  57. <a name="l00014"></a>00014 <span class="comment"> </span>
  58. <a name="l00015"></a>00015 <span class="comment"> The above copyright notice and this permission notice shall be included in</span>
  59. <a name="l00016"></a>00016 <span class="comment"> all copies or substantial portions of the Software.</span>
  60. <a name="l00017"></a>00017 <span class="comment"> </span>
  61. <a name="l00018"></a>00018 <span class="comment"> THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
  62. <a name="l00019"></a>00019 <span class="comment"> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
  63. <a name="l00020"></a>00020 <span class="comment"> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
  64. <a name="l00021"></a>00021 <span class="comment"> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
  65. <a name="l00022"></a>00022 <span class="comment"> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
  66. <a name="l00023"></a>00023 <span class="comment"> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
  67. <a name="l00024"></a>00024 <span class="comment"> THE SOFTWARE.</span>
  68. <a name="l00025"></a>00025 <span class="comment"> </span>
  69. <a name="l00026"></a>00026 <span class="comment"> */</span>
  70. <a name="l00027"></a>00027
  71. <a name="l00028"></a>00028 <span class="preprocessor">#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100</span>
  72. <a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor"> #include &lt;Arduino.h&gt;</span>
  73. <a name="l00030"></a>00030 <span class="preprocessor">#else</span>
  74. <a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor"> #include &lt;WProgram.h&gt;</span>
  75. <a name="l00032"></a>00032 <span class="preprocessor">#endif</span>
  76. <a name="l00033"></a>00033 <span class="preprocessor"></span><span class="preprocessor">#include &lt;SPI.h&gt;</span>
  77. <a name="l00034"></a>00034 <span class="preprocessor">#include &quot;<a class="code" href="_t_m_c26_x_stepper_8h.html">TMC26XStepper.h</a>&quot;</span>
  78. <a name="l00035"></a>00035
  79. <a name="l00036"></a>00036 <span class="comment">//some default values used in initialization</span>
  80. <a name="l00037"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6560b3471273e99e280ba795e3469ede">00037</a> <span class="preprocessor">#define DEFAULT_MICROSTEPPING_VALUE 32</span>
  81. <a name="l00038"></a>00038 <span class="preprocessor"></span>
  82. <a name="l00039"></a>00039 <span class="comment">//TMC26X register definitions</span>
  83. <a name="l00040"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">00040</a> <span class="preprocessor">#define DRIVER_CONTROL_REGISTER 0x0ul</span>
  84. <a name="l00041"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">00041</a> <span class="preprocessor"></span><span class="preprocessor">#define CHOPPER_CONFIG_REGISTER 0x80000ul</span>
  85. <a name="l00042"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">00042</a> <span class="preprocessor"></span><span class="preprocessor">#define COOL_STEP_REGISTER 0xA0000ul</span>
  86. <a name="l00043"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a478d9bde09a6528eef6af6ffeeb6caba">00043</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD2_LOAD_MEASURE_REGISTER 0xC0000ul</span>
  87. <a name="l00044"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#af35f569d42ea3b1d634901a3b6a908ee">00044</a> <span class="preprocessor"></span><span class="preprocessor">#define DRIVER_CONFIG_REGISTER 0xE0000ul</span>
  88. <a name="l00045"></a>00045 <span class="preprocessor"></span>
  89. <a name="l00046"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a3b02ee1f518b0c90c16488f937abd443">00046</a> <span class="preprocessor">#define REGISTER_BIT_PATTERN 0xFFFFFul</span>
  90. <a name="l00047"></a>00047 <span class="preprocessor"></span>
  91. <a name="l00048"></a>00048 <span class="comment">//definitions for the driver control register</span>
  92. <a name="l00049"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8f5cb0c066109ffb18cefc0e85ee1d1b">00049</a> <span class="preprocessor">#define MICROSTEPPING_PATTERN 0xFul</span>
  93. <a name="l00050"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa64245f223209654c60588e4558e0bab">00050</a> <span class="preprocessor"></span><span class="preprocessor">#define STEP_INTERPOLATION 0x200ul</span>
  94. <a name="l00051"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a770601bf0153e4bc639b9c3005b15af7">00051</a> <span class="preprocessor"></span><span class="preprocessor">#define DOUBLE_EDGE_STEP 0x100ul</span>
  95. <a name="l00052"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">00052</a> <span class="preprocessor"></span><span class="preprocessor">#define VSENSE 0x40ul</span>
  96. <a name="l00053"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a143b7757272f07866d9655bde8303d9a">00053</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_MICROSTEP_POSTION 0x0ul</span>
  97. <a name="l00054"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">00054</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_STALL_GUARD_READING 0x10ul</span>
  98. <a name="l00055"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">00055</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_STALL_GUARD_AND_COOL_STEP 0x20ul</span>
  99. <a name="l00056"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">00056</a> <span class="preprocessor"></span><span class="preprocessor">#define READ_SELECTION_PATTERN 0x30ul</span>
  100. <a name="l00057"></a>00057 <span class="preprocessor"></span>
  101. <a name="l00058"></a>00058 <span class="comment">//definitions for the chopper config register</span>
  102. <a name="l00059"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a57418a67ff726d540b813230bca1d536">00059</a> <span class="preprocessor">#define CHOPPER_MODE_STANDARD 0x0ul</span>
  103. <a name="l00060"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">00060</a> <span class="preprocessor"></span><span class="preprocessor">#define CHOPPER_MODE_T_OFF_FAST_DECAY 0x4000ul</span>
  104. <a name="l00061"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">00061</a> <span class="preprocessor"></span><span class="preprocessor">#define T_OFF_PATTERN 0xful</span>
  105. <a name="l00062"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">00062</a> <span class="preprocessor"></span><span class="preprocessor">#define RANDOM_TOFF_TIME 0x2000ul</span>
  106. <a name="l00063"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">00063</a> <span class="preprocessor"></span><span class="preprocessor">#define BLANK_TIMING_PATTERN 0x18000ul</span>
  107. <a name="l00064"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">00064</a> <span class="preprocessor"></span><span class="preprocessor">#define BLANK_TIMING_SHIFT 15</span>
  108. <a name="l00065"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">00065</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_DECREMENT_PATTERN 0x1800ul</span>
  109. <a name="l00066"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a36e554a87785ce6ba998b79aae9e74e0">00066</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_DECREMENT_SHIFT 11</span>
  110. <a name="l00067"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">00067</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_LOW_VALUE_PATTERN 0x780ul</span>
  111. <a name="l00068"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">00068</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_LOW_SHIFT 7</span>
  112. <a name="l00069"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">00069</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_START_VALUE_PATTERN 0x78ul</span>
  113. <a name="l00070"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">00070</a> <span class="preprocessor"></span><span class="preprocessor">#define HYSTERESIS_START_VALUE_SHIFT 4</span>
  114. <a name="l00071"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">00071</a> <span class="preprocessor"></span><span class="preprocessor">#define T_OFF_TIMING_PATERN 0xFul</span>
  115. <a name="l00072"></a>00072 <span class="preprocessor"></span>
  116. <a name="l00073"></a>00073 <span class="comment">//definitions for cool step register</span>
  117. <a name="l00074"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8a261a77d198b85f6dd8416387b354b3">00074</a> <span class="preprocessor">#define MINIMUM_CURRENT_FOURTH 0x8000ul</span>
  118. <a name="l00075"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbe13a0464355e42fbe786ca5f58ed8d">00075</a> <span class="preprocessor"></span><span class="preprocessor">#define CURRENT_DOWN_STEP_SPEED_PATTERN 0x6000ul</span>
  119. <a name="l00076"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac8f748bf735c447dbed7dd4c7b631a87">00076</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_MAX_PATTERN 0xF00ul</span>
  120. <a name="l00077"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aba6c07e5672e34e618bb3a550ab0d2bc">00077</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_CURRENT_STEP_WIDTH_PATTERN 0x60ul</span>
  121. <a name="l00078"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae1862dfb958c03698b0abd95fda033ea">00078</a> <span class="preprocessor"></span><span class="preprocessor">#define SE_MIN_PATTERN 0xful</span>
  122. <a name="l00079"></a>00079 <span class="preprocessor"></span>
  123. <a name="l00080"></a>00080 <span class="comment">//definitions for stall guard2 current register</span>
  124. <a name="l00081"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">00081</a> <span class="preprocessor">#define STALL_GUARD_FILTER_ENABLED 0x10000ul</span>
  125. <a name="l00082"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae631457932894a974334892704550ecc">00082</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_TRESHHOLD_VALUE_PATTERN 0x17F00ul</span>
  126. <a name="l00083"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">00083</a> <span class="preprocessor"></span><span class="preprocessor">#define CURRENT_SCALING_PATTERN 0x1Ful</span>
  127. <a name="l00084"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">00084</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_CONFIG_PATTERN 0x17F00ul</span>
  128. <a name="l00085"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6a1cb1fd61cf7c570f94376fa11fe55b">00085</a> <span class="preprocessor"></span><span class="preprocessor">#define STALL_GUARD_VALUE_PATTERN 0x7F00ul</span>
  129. <a name="l00086"></a>00086 <span class="preprocessor"></span>
  130. <a name="l00087"></a>00087 <span class="comment">//definitions for the input from the TCM260</span>
  131. <a name="l00088"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">00088</a> <span class="preprocessor">#define STATUS_STALL_GUARD_STATUS 0x1ul</span>
  132. <a name="l00089"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbb23d2f055c9eab55eac29d1a75deb4">00089</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OVER_TEMPERATURE_SHUTDOWN 0x2ul</span>
  133. <a name="l00090"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa317fd77f2f26fdfbfd331e21d9069e8">00090</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OVER_TEMPERATURE_WARNING 0x4ul</span>
  134. <a name="l00091"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8e03041302a092174fa33b3cf837dca2">00091</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_SHORT_TO_GROUND_A 0x8ul</span>
  135. <a name="l00092"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a903c3eba99695a32c6736463dcfd93ae">00092</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_SHORT_TO_GROUND_B 0x10ul</span>
  136. <a name="l00093"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae9cbbe5af7188e6bff8fe412f8e42f59">00093</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OPEN_LOAD_A 0x20ul</span>
  137. <a name="l00094"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab29dc5cd6c6c4e5bf99e71bd563e1be1">00094</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_OPEN_LOAD_B 0x40ul</span>
  138. <a name="l00095"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab96ed1635faee6650e9cce73598a2773">00095</a> <span class="preprocessor"></span><span class="preprocessor">#define STATUS_STAND_STILL 0x80ul</span>
  139. <a name="l00096"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a5c3d76da63f585e37813c32be2e11ab7">00096</a> <span class="preprocessor"></span><span class="preprocessor">#define READOUT_VALUE_PATTERN 0xFFC00ul</span>
  140. <a name="l00097"></a>00097 <span class="preprocessor"></span>
  141. <a name="l00098"></a>00098 <span class="comment">//default values</span>
  142. <a name="l00099"></a><a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">00099</a> <span class="preprocessor">#define INITIAL_MICROSTEPPING 0x3ul //32th microstepping</span>
  143. <a name="l00100"></a>00100 <span class="preprocessor"></span>
  144. <a name="l00101"></a>00101 <span class="comment">//debuging output</span>
  145. <a name="l00102"></a>00102 <span class="comment">//#define DEBUG</span>
  146. <a name="l00103"></a>00103
  147. <a name="l00104"></a>00104 <span class="comment">/*</span>
  148. <a name="l00105"></a>00105 <span class="comment"> * Constructor</span>
  149. <a name="l00106"></a>00106 <span class="comment"> * number_of_steps - the steps per rotation</span>
  150. <a name="l00107"></a>00107 <span class="comment"> * cs_pin - the SPI client select pin</span>
  151. <a name="l00108"></a>00108 <span class="comment"> * dir_pin - the pin where the direction pin is connected</span>
  152. <a name="l00109"></a>00109 <span class="comment"> * step_pin - the pin where the step pin is connected</span>
  153. <a name="l00110"></a>00110 <span class="comment"> */</span>
  154. <a name="l00111"></a><a class="code" href="class_t_m_c26_x_stepper.html#a3ef40763b8b8ab2b6ed4978c0647906c">00111</a> <a class="code" href="class_t_m_c26_x_stepper.html#a3ef40763b8b8ab2b6ed4978c0647906c" title="creates a new represenatation of a stepper motor connected to a TMC26X stepper driver">TMC26XStepper::TMC26XStepper</a>(<span class="keywordtype">int</span> number_of_steps, <span class="keywordtype">int</span> cs_pin, <span class="keywordtype">int</span> dir_pin, <span class="keywordtype">int</span> step_pin, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> current, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> resistor)
  155. <a name="l00112"></a>00112 {
  156. <a name="l00113"></a>00113 <span class="comment">//we are not started yet</span>
  157. <a name="l00114"></a>00114 started=<span class="keyword">false</span>;
  158. <a name="l00115"></a>00115 <span class="comment">//by default cool step is not enabled</span>
  159. <a name="l00116"></a>00116 cool_step_enabled=<span class="keyword">false</span>;
  160. <a name="l00117"></a>00117
  161. <a name="l00118"></a>00118 <span class="comment">//save the pins for later use</span>
  162. <a name="l00119"></a>00119 this-&gt;cs_pin=cs_pin;
  163. <a name="l00120"></a>00120 this-&gt;dir_pin=dir_pin;
  164. <a name="l00121"></a>00121 this-&gt;step_pin = step_pin;
  165. <a name="l00122"></a>00122
  166. <a name="l00123"></a>00123 <span class="comment">//store the current sense resistor value for later use</span>
  167. <a name="l00124"></a>00124 this-&gt;resistor = resistor;
  168. <a name="l00125"></a>00125
  169. <a name="l00126"></a>00126 <span class="comment">//initizalize our status values</span>
  170. <a name="l00127"></a>00127 this-&gt;steps_left = 0;
  171. <a name="l00128"></a>00128 this-&gt;direction = 0;
  172. <a name="l00129"></a>00129
  173. <a name="l00130"></a>00130 <span class="comment">//initialize register values</span>
  174. <a name="l00131"></a>00131 driver_control_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">DRIVER_CONTROL_REGISTER</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>;
  175. <a name="l00132"></a>00132 chopper_config_register=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">CHOPPER_CONFIG_REGISTER</a>;
  176. <a name="l00133"></a>00133
  177. <a name="l00134"></a>00134 <span class="comment">//setting the default register values</span>
  178. <a name="l00135"></a>00135 driver_control_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a108f18bf4a30a0e0f0991ac0e4ce0579">DRIVER_CONTROL_REGISTER</a>|<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>;
  179. <a name="l00136"></a>00136 microsteps = (1 &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a54a6d12e96d851361974b10614a00e45">INITIAL_MICROSTEPPING</a>);
  180. <a name="l00137"></a>00137 chopper_config_register=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a70a540d1090b989b8600b5e4776659fe">CHOPPER_CONFIG_REGISTER</a>;
  181. <a name="l00138"></a>00138 cool_step_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">COOL_STEP_REGISTER</a>;
  182. <a name="l00139"></a>00139 stall_guard2_current_register_value=<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a478d9bde09a6528eef6af6ffeeb6caba">STALL_GUARD2_LOAD_MEASURE_REGISTER</a>;
  183. <a name="l00140"></a>00140 driver_configuration_register_value = <a class="code" href="_t_m_c26_x_stepper_8cpp.html#af35f569d42ea3b1d634901a3b6a908ee">DRIVER_CONFIG_REGISTER</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>;
  184. <a name="l00141"></a>00141
  185. <a name="l00142"></a>00142 <span class="comment">//set the current</span>
  186. <a name="l00143"></a>00143 <a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2" title="set the maximum motor current in mA (1000 is 1 Amp) Keep in mind this is the maximum peak Current...">setCurrent</a>(current);
  187. <a name="l00144"></a>00144 <span class="comment">//set to a conservative start value</span>
  188. <a name="l00145"></a>00145 <a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649" title="Sets and configure the classical Constant Off Timer Chopper.">setConstantOffTimeChopper</a>(7, 54, 13,12,1);
  189. <a name="l00146"></a>00146 <span class="comment">//set a nice microstepping value</span>
  190. <a name="l00147"></a>00147 <a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0" title="Set the number of microsteps in 2^i values (rounded) up to 256.">setMicrosteps</a>(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6560b3471273e99e280ba795e3469ede">DEFAULT_MICROSTEPPING_VALUE</a>);
  191. <a name="l00148"></a>00148 <span class="comment">//save the number of steps</span>
  192. <a name="l00149"></a>00149 this-&gt;number_of_steps = number_of_steps;
  193. <a name="l00150"></a>00150 }
  194. <a name="l00151"></a>00151
  195. <a name="l00152"></a>00152
  196. <a name="l00153"></a>00153 <span class="comment">/*</span>
  197. <a name="l00154"></a>00154 <span class="comment"> * start &amp; configure the stepper driver</span>
  198. <a name="l00155"></a>00155 <span class="comment"> * just must be called.</span>
  199. <a name="l00156"></a>00156 <span class="comment"> */</span>
  200. <a name="l00157"></a><a class="code" href="class_t_m_c26_x_stepper.html#aad1ed82b3e05940bde5a6c7ed3d3e8f7">00157</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aad1ed82b3e05940bde5a6c7ed3d3e8f7" title="configures and starts the TMC26X stepper driver. Before you called this function the stepper driver i...">TMC26XStepper::start</a>() {
  201. <a name="l00158"></a>00158
  202. <a name="l00159"></a>00159 <span class="preprocessor">#ifdef DEBUG </span>
  203. <a name="l00160"></a>00160 <span class="preprocessor"></span> Serial.println(<span class="stringliteral">&quot;TMC26X stepper library&quot;</span>);
  204. <a name="l00161"></a>00161 Serial.print(<span class="stringliteral">&quot;CS pin: &quot;</span>);
  205. <a name="l00162"></a>00162 Serial.println(cs_pin);
  206. <a name="l00163"></a>00163 Serial.print(<span class="stringliteral">&quot;DIR pin: &quot;</span>);
  207. <a name="l00164"></a>00164 Serial.println(dir_pin);
  208. <a name="l00165"></a>00165 Serial.print(<span class="stringliteral">&quot;STEP pin: &quot;</span>);
  209. <a name="l00166"></a>00166 Serial.println(step_pin);
  210. <a name="l00167"></a>00167 Serial.print(<span class="stringliteral">&quot;current scaling: &quot;</span>);
  211. <a name="l00168"></a>00168 Serial.println(current_scaling,DEC);
  212. <a name="l00169"></a>00169 <span class="preprocessor">#endif</span>
  213. <a name="l00170"></a>00170 <span class="preprocessor"></span> <span class="comment">//set the pins as output &amp; its initial value</span>
  214. <a name="l00171"></a>00171 pinMode(step_pin, OUTPUT);
  215. <a name="l00172"></a>00172 pinMode(dir_pin, OUTPUT);
  216. <a name="l00173"></a>00173 pinMode(cs_pin, OUTPUT);
  217. <a name="l00174"></a>00174 digitalWrite(step_pin, LOW);
  218. <a name="l00175"></a>00175 digitalWrite(dir_pin, LOW);
  219. <a name="l00176"></a>00176 digitalWrite(cs_pin, HIGH);
  220. <a name="l00177"></a>00177
  221. <a name="l00178"></a>00178 <span class="comment">//configure the SPI interface</span>
  222. <a name="l00179"></a>00179 SPI.setBitOrder(MSBFIRST);
  223. <a name="l00180"></a>00180 SPI.setClockDivider(SPI_CLOCK_DIV8);
  224. <a name="l00181"></a>00181 <span class="comment">//todo this does not work reliably - find a way to foolprof set it (e.g. while communicating</span>
  225. <a name="l00182"></a>00182 <span class="comment">//SPI.setDataMode(SPI_MODE3);</span>
  226. <a name="l00183"></a>00183 SPI.begin();
  227. <a name="l00184"></a>00184
  228. <a name="l00185"></a>00185 <span class="comment">//set the initial values</span>
  229. <a name="l00186"></a>00186 send262(driver_control_register_value);
  230. <a name="l00187"></a>00187 send262(chopper_config_register);
  231. <a name="l00188"></a>00188 send262(cool_step_register_value);
  232. <a name="l00189"></a>00189 send262(stall_guard2_current_register_value);
  233. <a name="l00190"></a>00190 send262(driver_configuration_register_value);
  234. <a name="l00191"></a>00191
  235. <a name="l00192"></a>00192 <span class="comment">//save that we are in running mode</span>
  236. <a name="l00193"></a>00193 started=<span class="keyword">true</span>;
  237. <a name="l00194"></a>00194 }
  238. <a name="l00195"></a>00195
  239. <a name="l00196"></a>00196 <span class="comment">/*</span>
  240. <a name="l00197"></a>00197 <span class="comment"> Mark the driver as unstarted to be able to start it again</span>
  241. <a name="l00198"></a>00198 <span class="comment"> */</span>
  242. <a name="l00199"></a><a class="code" href="class_t_m_c26_x_stepper.html#af968e70a13068f1e71ac0fa6865630c5">00199</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af968e70a13068f1e71ac0fa6865630c5" title="resets the stepper in unconfigured mode.">TMC26XStepper::un_start</a>() {
  243. <a name="l00200"></a>00200 started=<span class="keyword">false</span>;
  244. <a name="l00201"></a>00201 }
  245. <a name="l00202"></a>00202
  246. <a name="l00203"></a>00203
  247. <a name="l00204"></a>00204 <span class="comment">/*</span>
  248. <a name="l00205"></a>00205 <span class="comment"> Sets the speed in revs per minute</span>
  249. <a name="l00206"></a>00206 <span class="comment"></span>
  250. <a name="l00207"></a>00207 <span class="comment">*/</span>
  251. <a name="l00208"></a><a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd">00208</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd" title="Sets the rotation speed in revolutions per minute.">TMC26XStepper::setSpeed</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> whatSpeed)
  252. <a name="l00209"></a>00209 {
  253. <a name="l00210"></a>00210 this-&gt;speed = whatSpeed;
  254. <a name="l00211"></a>00211 this-&gt;step_delay = (60UL * 1000UL * 1000UL) / ((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)this-&gt;number_of_steps * (<span class="keywordtype">unsigned</span> long)whatSpeed * (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)this-&gt;microsteps);
  255. <a name="l00212"></a>00212 <span class="preprocessor">#ifdef DEBUG</span>
  256. <a name="l00213"></a>00213 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Step delay in micros: &quot;</span>);
  257. <a name="l00214"></a>00214 Serial.println(this-&gt;step_delay);
  258. <a name="l00215"></a>00215 <span class="preprocessor">#endif</span>
  259. <a name="l00216"></a>00216 <span class="preprocessor"></span> <span class="comment">//update the next step time</span>
  260. <a name="l00217"></a>00217 this-&gt;next_step_time = this-&gt;last_step_time+this-&gt;step_delay;
  261. <a name="l00218"></a>00218
  262. <a name="l00219"></a>00219 }
  263. <a name="l00220"></a>00220
  264. <a name="l00221"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa564f5cc0218d30ef897c2830c768c29">00221</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa564f5cc0218d30ef897c2830c768c29" title="reads out the currently selected speed in revolutions per minute.">TMC26XStepper::getSpeed</a>(<span class="keywordtype">void</span>) {
  265. <a name="l00222"></a>00222 <span class="keywordflow">return</span> this-&gt;speed;
  266. <a name="l00223"></a>00223 }
  267. <a name="l00224"></a>00224
  268. <a name="l00225"></a>00225 <span class="comment">/*</span>
  269. <a name="l00226"></a>00226 <span class="comment"> Moves the motor steps_to_move steps. If the number is negative, </span>
  270. <a name="l00227"></a>00227 <span class="comment"> the motor moves in the reverse direction.</span>
  271. <a name="l00228"></a>00228 <span class="comment"> */</span>
  272. <a name="l00229"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac073a742496885f1f60751f9fb9c395d">00229</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac073a742496885f1f60751f9fb9c395d" title="Initiate a movement for the given number of steps. Positive numbers move in one, negative numbers in ...">TMC26XStepper::step</a>(<span class="keywordtype">int</span> steps_to_move)
  273. <a name="l00230"></a>00230 {
  274. <a name="l00231"></a>00231 <span class="keywordflow">if</span> (this-&gt;steps_left==0) {
  275. <a name="l00232"></a>00232 this-&gt;steps_left = abs(steps_to_move); <span class="comment">// how many steps to take</span>
  276. <a name="l00233"></a>00233
  277. <a name="l00234"></a>00234 <span class="comment">// determine direction based on whether steps_to_mode is + or -:</span>
  278. <a name="l00235"></a>00235 <span class="keywordflow">if</span> (steps_to_move &gt; 0) {
  279. <a name="l00236"></a>00236 this-&gt;direction = 1;
  280. <a name="l00237"></a>00237 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (steps_to_move &lt; 0) {
  281. <a name="l00238"></a>00238 this-&gt;direction = 0;
  282. <a name="l00239"></a>00239 }
  283. <a name="l00240"></a>00240 <span class="keywordflow">return</span> 0;
  284. <a name="l00241"></a>00241 } <span class="keywordflow">else</span> {
  285. <a name="l00242"></a>00242 <span class="keywordflow">return</span> -1;
  286. <a name="l00243"></a>00243 }
  287. <a name="l00244"></a>00244 }
  288. <a name="l00245"></a>00245
  289. <a name="l00246"></a><a class="code" href="class_t_m_c26_x_stepper.html#aed5d81f1549615529c723600a68ba415">00246</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#aed5d81f1549615529c723600a68ba415" title="Central movement method, must be called as often as possible in the lopp function and is very fast...">TMC26XStepper::move</a>(<span class="keywordtype">void</span>) {
  290. <a name="l00247"></a>00247 <span class="comment">// decrement the number of steps, moving one step each time:</span>
  291. <a name="l00248"></a>00248 <span class="keywordflow">if</span>(this-&gt;steps_left&gt;0) {
  292. <a name="l00249"></a>00249 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> time = micros();
  293. <a name="l00250"></a>00250 <span class="comment">// move only if the appropriate delay has passed:</span>
  294. <a name="l00251"></a>00251 <span class="keywordflow">if</span> (time &gt;= this-&gt;next_step_time) {
  295. <a name="l00252"></a>00252 <span class="comment">// increment or decrement the step number,</span>
  296. <a name="l00253"></a>00253 <span class="comment">// depending on direction:</span>
  297. <a name="l00254"></a>00254 <span class="keywordflow">if</span> (this-&gt;direction == 1) {
  298. <a name="l00255"></a>00255 digitalWrite(step_pin, HIGH);
  299. <a name="l00256"></a>00256 } <span class="keywordflow">else</span> {
  300. <a name="l00257"></a>00257 digitalWrite(dir_pin, HIGH);
  301. <a name="l00258"></a>00258 digitalWrite(step_pin, HIGH);
  302. <a name="l00259"></a>00259 }
  303. <a name="l00260"></a>00260 <span class="comment">// get the timeStamp of when you stepped:</span>
  304. <a name="l00261"></a>00261 this-&gt;last_step_time = time;
  305. <a name="l00262"></a>00262 this-&gt;next_step_time = time+this-&gt;step_delay;
  306. <a name="l00263"></a>00263 <span class="comment">// decrement the steps left:</span>
  307. <a name="l00264"></a>00264 steps_left--;
  308. <a name="l00265"></a>00265 <span class="comment">//disable the step &amp; dir pins</span>
  309. <a name="l00266"></a>00266 digitalWrite(step_pin, LOW);
  310. <a name="l00267"></a>00267 digitalWrite(dir_pin, LOW);
  311. <a name="l00268"></a>00268 }
  312. <a name="l00269"></a>00269 <span class="keywordflow">return</span> -1;
  313. <a name="l00270"></a>00270 }
  314. <a name="l00271"></a>00271 <span class="keywordflow">return</span> 0;
  315. <a name="l00272"></a>00272 }
  316. <a name="l00273"></a>00273
  317. <a name="l00274"></a><a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e">00274</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e" title="checks if the motor still has to move to fulfill the last movement command.">TMC26XStepper::isMoving</a>(<span class="keywordtype">void</span>) {
  318. <a name="l00275"></a>00275 <span class="keywordflow">return</span> (this-&gt;steps_left&gt;0);
  319. <a name="l00276"></a>00276 }
  320. <a name="l00277"></a>00277
  321. <a name="l00278"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa6c3211f85301ca0fb2e7b73cb8142a7">00278</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa6c3211f85301ca0fb2e7b73cb8142a7" title="Get the number of steps left in the current movement.">TMC26XStepper::getStepsLeft</a>(<span class="keywordtype">void</span>) {
  322. <a name="l00279"></a>00279 <span class="keywordflow">return</span> this-&gt;steps_left;
  323. <a name="l00280"></a>00280 }
  324. <a name="l00281"></a>00281
  325. <a name="l00282"></a><a class="code" href="class_t_m_c26_x_stepper.html#a6315c18eadbc6bf4f3d81a6f80296123">00282</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a6315c18eadbc6bf4f3d81a6f80296123" title="Stops the motor regardless if it moves or not.">TMC26XStepper::stop</a>(<span class="keywordtype">void</span>) {
  326. <a name="l00283"></a>00283 <span class="comment">//note to self if the motor is currently moving</span>
  327. <a name="l00284"></a>00284 <span class="keywordtype">char</span> state = <a class="code" href="class_t_m_c26_x_stepper.html#a880d602be8414b7b965287c1790cd50e" title="checks if the motor still has to move to fulfill the last movement command.">isMoving</a>();
  328. <a name="l00285"></a>00285 <span class="comment">//stop the motor</span>
  329. <a name="l00286"></a>00286 this-&gt;steps_left = 0;
  330. <a name="l00287"></a>00287 this-&gt;direction = 0;
  331. <a name="l00288"></a>00288 <span class="comment">//return if it was moving</span>
  332. <a name="l00289"></a>00289 <span class="keywordflow">return</span> state;
  333. <a name="l00290"></a>00290 }
  334. <a name="l00291"></a>00291
  335. <a name="l00292"></a><a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2">00292</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aaa35fac83417c16b3a941fa168e4a4d2" title="set the maximum motor current in mA (1000 is 1 Amp) Keep in mind this is the maximum peak Current...">TMC26XStepper::setCurrent</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> current) {
  336. <a name="l00293"></a>00293 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_scaling = 0;
  337. <a name="l00294"></a>00294 <span class="comment">//calculate the current scaling from the max current setting (in mA)</span>
  338. <a name="l00295"></a>00295 <span class="keywordtype">double</span> mASetting = (double)current;
  339. <a name="l00296"></a>00296 <span class="keywordtype">double</span> resistor_value = (double) this-&gt;resistor;
  340. <a name="l00297"></a>00297 <span class="comment">// remove vesense flag</span>
  341. <a name="l00298"></a>00298 this-&gt;driver_configuration_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>);
  342. <a name="l00299"></a>00299 <span class="comment">//this is derrived from I=(cs+1)/32*(Vsense/Rsense)</span>
  343. <a name="l00300"></a>00300 <span class="comment">//leading to cs = CS = 32*R*I/V (with V = 0,31V oder 0,165V and I = 1000*current)</span>
  344. <a name="l00301"></a>00301 <span class="comment">//with Rsense=0,15</span>
  345. <a name="l00302"></a>00302 <span class="comment">//for vsense = 0,310V (VSENSE not set)</span>
  346. <a name="l00303"></a>00303 <span class="comment">//or vsense = 0,165V (VSENSE set)</span>
  347. <a name="l00304"></a>00304 current_scaling = (byte)((resistor_value*mASetting*32.0/(0.31*1000.0*1000.0))-0.5); <span class="comment">//theoretically - 1.0 for better rounding it is 0.5</span>
  348. <a name="l00305"></a>00305
  349. <a name="l00306"></a>00306 <span class="comment">//check if the current scalingis too low</span>
  350. <a name="l00307"></a>00307 <span class="keywordflow">if</span> (current_scaling&lt;16) {
  351. <a name="l00308"></a>00308 <span class="comment">//set the csense bit to get a use half the sense voltage (to support lower motor currents)</span>
  352. <a name="l00309"></a>00309 this-&gt;driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>;
  353. <a name="l00310"></a>00310 <span class="comment">//and recalculate the current setting</span>
  354. <a name="l00311"></a>00311 current_scaling = (byte)((resistor_value*mASetting*32.0/(0.165*1000.0*1000.0))-0.5); <span class="comment">//theoretically - 1.0 for better rounding it is 0.5</span>
  355. <a name="l00312"></a>00312 <span class="preprocessor">#ifdef DEBUG</span>
  356. <a name="l00313"></a>00313 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;CS (Vsense=1): &quot;</span>);
  357. <a name="l00314"></a>00314 Serial.println(current_scaling);
  358. <a name="l00315"></a>00315 } <span class="keywordflow">else</span> {
  359. <a name="l00316"></a>00316 Serial.print(<span class="stringliteral">&quot;CS: &quot;</span>);
  360. <a name="l00317"></a>00317 Serial.println(current_scaling);
  361. <a name="l00318"></a>00318 <span class="preprocessor">#endif</span>
  362. <a name="l00319"></a>00319 <span class="preprocessor"></span> }
  363. <a name="l00320"></a>00320
  364. <a name="l00321"></a>00321 <span class="comment">//do some sanity checks</span>
  365. <a name="l00322"></a>00322 <span class="keywordflow">if</span> (current_scaling&gt;31) {
  366. <a name="l00323"></a>00323 current_scaling=31;
  367. <a name="l00324"></a>00324 }
  368. <a name="l00325"></a>00325 <span class="comment">//delete the old value</span>
  369. <a name="l00326"></a>00326 stall_guard2_current_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">CURRENT_SCALING_PATTERN</a>);
  370. <a name="l00327"></a>00327 <span class="comment">//set the new current scaling</span>
  371. <a name="l00328"></a>00328 stall_guard2_current_register_value |= current_scaling;
  372. <a name="l00329"></a>00329 <span class="comment">//if started we directly send it to the motor</span>
  373. <a name="l00330"></a>00330 <span class="keywordflow">if</span> (started) {
  374. <a name="l00331"></a>00331 send262(driver_configuration_register_value);
  375. <a name="l00332"></a>00332 send262(stall_guard2_current_register_value);
  376. <a name="l00333"></a>00333 }
  377. <a name="l00334"></a>00334 }
  378. <a name="l00335"></a>00335
  379. <a name="l00336"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0c544e23efe3e4a912aacf57de84b71f">00336</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0c544e23efe3e4a912aacf57de84b71f" title="readout the motor maximum current in mA (1000 is an Amp) This is the maximum current. to get the current current - which may be affected by CoolStep us getCurrentCurrent()">TMC26XStepper::getCurrent</a>(<span class="keywordtype">void</span>) {
  380. <a name="l00337"></a>00337 <span class="comment">//we calculate the current according to the datasheet to be on the safe side</span>
  381. <a name="l00338"></a>00338 <span class="comment">//this is not the fastest but the most accurate and illustrative way</span>
  382. <a name="l00339"></a>00339 <span class="keywordtype">double</span> result = (double)(stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99dcb8c6d98b0b54c23699a3f90450e4">CURRENT_SCALING_PATTERN</a>);
  383. <a name="l00340"></a>00340 <span class="keywordtype">double</span> resistor_value = (double)this-&gt;resistor;
  384. <a name="l00341"></a>00341 <span class="keywordtype">double</span> voltage = (driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>)? 0.165:0.31;
  385. <a name="l00342"></a>00342 result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
  386. <a name="l00343"></a>00343 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)result;
  387. <a name="l00344"></a>00344 }
  388. <a name="l00345"></a>00345
  389. <a name="l00346"></a><a class="code" href="class_t_m_c26_x_stepper.html#af1a5abc23757860baf8ff421689a425a">00346</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af1a5abc23757860baf8ff421689a425a" title="set the StallGuard threshold in order to get sensible StallGuard readings.">TMC26XStepper::setStallGuardThreshold</a>(<span class="keywordtype">char</span> stall_guard_threshold, <span class="keywordtype">char</span> stall_guard_filter_enabled) {
  390. <a name="l00347"></a>00347 <span class="keywordflow">if</span> (stall_guard_threshold&lt;-64) {
  391. <a name="l00348"></a>00348 stall_guard_threshold = -64;
  392. <a name="l00349"></a>00349 <span class="comment">//We just have 5 bits </span>
  393. <a name="l00350"></a>00350 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (stall_guard_threshold &gt; 63) {
  394. <a name="l00351"></a>00351 stall_guard_threshold = 63;
  395. <a name="l00352"></a>00352 }
  396. <a name="l00353"></a>00353 <span class="comment">//add trim down to 7 bits</span>
  397. <a name="l00354"></a>00354 stall_guard_threshold &amp;=0x7f;
  398. <a name="l00355"></a>00355 <span class="comment">//delete old stall guard settings</span>
  399. <a name="l00356"></a>00356 stall_guard2_current_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">STALL_GUARD_CONFIG_PATTERN</a>);
  400. <a name="l00357"></a>00357 <span class="keywordflow">if</span> (stall_guard_filter_enabled) {
  401. <a name="l00358"></a>00358 stall_guard2_current_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">STALL_GUARD_FILTER_ENABLED</a>;
  402. <a name="l00359"></a>00359 }
  403. <a name="l00360"></a>00360 <span class="comment">//Set the new stall guard threshold</span>
  404. <a name="l00361"></a>00361 stall_guard2_current_register_value |= (((<span class="keywordtype">unsigned</span> long)stall_guard_threshold &lt;&lt; 8) &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a99ac04f0615556fc13c0c9f3e1c1b49d">STALL_GUARD_CONFIG_PATTERN</a>);
  405. <a name="l00362"></a>00362 <span class="comment">//if started we directly send it to the motor</span>
  406. <a name="l00363"></a>00363 <span class="keywordflow">if</span> (started) {
  407. <a name="l00364"></a>00364 send262(stall_guard2_current_register_value);
  408. <a name="l00365"></a>00365 }
  409. <a name="l00366"></a>00366 }
  410. <a name="l00367"></a>00367
  411. <a name="l00368"></a><a class="code" href="class_t_m_c26_x_stepper.html#a056661f444725c3ae15696d1e8d91def">00368</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a056661f444725c3ae15696d1e8d91def" title="reads out the StallGuard threshold">TMC26XStepper::getStallGuardThreshold</a>(<span class="keywordtype">void</span>) {
  412. <a name="l00369"></a>00369 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> stall_guard_threshold = stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a6a1cb1fd61cf7c570f94376fa11fe55b">STALL_GUARD_VALUE_PATTERN</a>;
  413. <a name="l00370"></a>00370 <span class="comment">//shift it down to bit 0</span>
  414. <a name="l00371"></a>00371 stall_guard_threshold &gt;&gt;=8;
  415. <a name="l00372"></a>00372 <span class="comment">//convert the value to an int to correctly handle the negative numbers</span>
  416. <a name="l00373"></a>00373 <span class="keywordtype">char</span> result = stall_guard_threshold;
  417. <a name="l00374"></a>00374 <span class="comment">//check if it is negative and fill it up with leading 1 for proper negative number representation</span>
  418. <a name="l00375"></a>00375 <span class="keywordflow">if</span> (result &amp; _BV(6)) {
  419. <a name="l00376"></a>00376 result |= 0xC0;
  420. <a name="l00377"></a>00377 }
  421. <a name="l00378"></a>00378 <span class="keywordflow">return</span> result;
  422. <a name="l00379"></a>00379 }
  423. <a name="l00380"></a>00380
  424. <a name="l00381"></a><a class="code" href="class_t_m_c26_x_stepper.html#a47e3443e3e786314c1099b8f14a91b8a">00381</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a47e3443e3e786314c1099b8f14a91b8a" title="returns the current setting of the StallGuard filter">TMC26XStepper::getStallGuardFilter</a>(<span class="keywordtype">void</span>) {
  425. <a name="l00382"></a>00382 <span class="keywordflow">if</span> (stall_guard2_current_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#afdbbefabd0c29c4b6e403c4663d0f0be">STALL_GUARD_FILTER_ENABLED</a>) {
  426. <a name="l00383"></a>00383 <span class="keywordflow">return</span> -1;
  427. <a name="l00384"></a>00384 } <span class="keywordflow">else</span> {
  428. <a name="l00385"></a>00385 <span class="keywordflow">return</span> 0;
  429. <a name="l00386"></a>00386 }
  430. <a name="l00387"></a>00387 }
  431. <a name="l00388"></a>00388 <span class="comment">/*</span>
  432. <a name="l00389"></a>00389 <span class="comment"> * Set the number of microsteps per step.</span>
  433. <a name="l00390"></a>00390 <span class="comment"> * 0,2,4,8,16,32,64,128,256 is supported</span>
  434. <a name="l00391"></a>00391 <span class="comment"> * any value in between will be mapped to the next smaller value</span>
  435. <a name="l00392"></a>00392 <span class="comment"> * 0 and 1 set the motor in full step mode</span>
  436. <a name="l00393"></a>00393 <span class="comment"> */</span>
  437. <a name="l00394"></a><a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0">00394</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a21041579c7f9284567ee2e2a55a3afd0" title="Set the number of microsteps in 2^i values (rounded) up to 256.">TMC26XStepper::setMicrosteps</a>(<span class="keywordtype">int</span> number_of_steps) {
  438. <a name="l00395"></a>00395 <span class="keywordtype">long</span> setting_pattern;
  439. <a name="l00396"></a>00396 <span class="comment">//poor mans log</span>
  440. <a name="l00397"></a>00397 <span class="keywordflow">if</span> (number_of_steps&gt;=256) {
  441. <a name="l00398"></a>00398 setting_pattern=0;
  442. <a name="l00399"></a>00399 microsteps=256;
  443. <a name="l00400"></a>00400 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=128) {
  444. <a name="l00401"></a>00401 setting_pattern=1;
  445. <a name="l00402"></a>00402 microsteps=128;
  446. <a name="l00403"></a>00403 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=64) {
  447. <a name="l00404"></a>00404 setting_pattern=2;
  448. <a name="l00405"></a>00405 microsteps=64;
  449. <a name="l00406"></a>00406 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=32) {
  450. <a name="l00407"></a>00407 setting_pattern=3;
  451. <a name="l00408"></a>00408 microsteps=32;
  452. <a name="l00409"></a>00409 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=16) {
  453. <a name="l00410"></a>00410 setting_pattern=4;
  454. <a name="l00411"></a>00411 microsteps=16;
  455. <a name="l00412"></a>00412 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=8) {
  456. <a name="l00413"></a>00413 setting_pattern=5;
  457. <a name="l00414"></a>00414 microsteps=8;
  458. <a name="l00415"></a>00415 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=4) {
  459. <a name="l00416"></a>00416 setting_pattern=6;
  460. <a name="l00417"></a>00417 microsteps=4;
  461. <a name="l00418"></a>00418 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&gt;=2) {
  462. <a name="l00419"></a>00419 setting_pattern=7;
  463. <a name="l00420"></a>00420 microsteps=2;
  464. <a name="l00421"></a>00421 <span class="comment">//1 and 0 lead to full step</span>
  465. <a name="l00422"></a>00422 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (number_of_steps&lt;=1) {
  466. <a name="l00423"></a>00423 setting_pattern=8;
  467. <a name="l00424"></a>00424 microsteps=1;
  468. <a name="l00425"></a>00425 }
  469. <a name="l00426"></a>00426 <span class="preprocessor">#ifdef DEBUG</span>
  470. <a name="l00427"></a>00427 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Microstepping: &quot;</span>);
  471. <a name="l00428"></a>00428 Serial.println(microsteps);
  472. <a name="l00429"></a>00429 <span class="preprocessor">#endif</span>
  473. <a name="l00430"></a>00430 <span class="preprocessor"></span> <span class="comment">//delete the old value</span>
  474. <a name="l00431"></a>00431 this-&gt;driver_control_register_value &amp;=0xFFFF0ul;
  475. <a name="l00432"></a>00432 <span class="comment">//set the new value</span>
  476. <a name="l00433"></a>00433 this-&gt;driver_control_register_value |=setting_pattern;
  477. <a name="l00434"></a>00434
  478. <a name="l00435"></a>00435 <span class="comment">//if started we directly send it to the motor</span>
  479. <a name="l00436"></a>00436 <span class="keywordflow">if</span> (started) {
  480. <a name="l00437"></a>00437 send262(driver_control_register_value);
  481. <a name="l00438"></a>00438 }
  482. <a name="l00439"></a>00439 <span class="comment">//recalculate the stepping delay by simply setting the speed again</span>
  483. <a name="l00440"></a>00440 this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a9478f43090995c8d5cdb4d4e8c07cdbd" title="Sets the rotation speed in revolutions per minute.">setSpeed</a>(this-&gt;speed);
  484. <a name="l00441"></a>00441 }
  485. <a name="l00442"></a>00442
  486. <a name="l00443"></a>00443 <span class="comment">/*</span>
  487. <a name="l00444"></a>00444 <span class="comment"> * returns the effective number of microsteps at the moment</span>
  488. <a name="l00445"></a>00445 <span class="comment"> */</span>
  489. <a name="l00446"></a><a class="code" href="class_t_m_c26_x_stepper.html#a5808551ced98b79c09bbb4bf47ecfec3">00446</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a5808551ced98b79c09bbb4bf47ecfec3" title="returns the effective current number of microsteps selected.">TMC26XStepper::getMicrosteps</a>(<span class="keywordtype">void</span>) {
  490. <a name="l00447"></a>00447 <span class="keywordflow">return</span> microsteps;
  491. <a name="l00448"></a>00448 }
  492. <a name="l00449"></a>00449
  493. <a name="l00450"></a>00450 <span class="comment">/*</span>
  494. <a name="l00451"></a>00451 <span class="comment"> * constant_off_time: The off time setting controls the minimum chopper frequency. </span>
  495. <a name="l00452"></a>00452 <span class="comment"> * For most applications an off time within the range of 5μs to 20μs will fit.</span>
  496. <a name="l00453"></a>00453 <span class="comment"> * 2...15: off time setting</span>
  497. <a name="l00454"></a>00454 <span class="comment"> *</span>
  498. <a name="l00455"></a>00455 <span class="comment"> * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the</span>
  499. <a name="l00456"></a>00456 <span class="comment"> * duration of the ringing on the sense resistor. For</span>
  500. <a name="l00457"></a>00457 <span class="comment"> * 0: min. setting 3: max. setting</span>
  501. <a name="l00458"></a>00458 <span class="comment"> *</span>
  502. <a name="l00459"></a>00459 <span class="comment"> * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.</span>
  503. <a name="l00460"></a>00460 <span class="comment"> * 0: slow decay only</span>
  504. <a name="l00461"></a>00461 <span class="comment"> * 1...15: duration of fast decay phase</span>
  505. <a name="l00462"></a>00462 <span class="comment"> *</span>
  506. <a name="l00463"></a>00463 <span class="comment"> * sine_wave_offset: Sine wave offset. With CHM=1, these bits control the sine wave offset. </span>
  507. <a name="l00464"></a>00464 <span class="comment"> * A positive offset corrects for zero crossing error.</span>
  508. <a name="l00465"></a>00465 <span class="comment"> * -3..-1: negative offset 0: no offset 1...12: positive offset</span>
  509. <a name="l00466"></a>00466 <span class="comment"> *</span>
  510. <a name="l00467"></a>00467 <span class="comment"> * use_current_comparator: Selects usage of the current comparator for termination of the fast decay cycle. </span>
  511. <a name="l00468"></a>00468 <span class="comment"> * If current comparator is enabled, it terminates the fast decay cycle in case the current </span>
  512. <a name="l00469"></a>00469 <span class="comment"> * reaches a higher negative value than the actual positive value.</span>
  513. <a name="l00470"></a>00470 <span class="comment"> * 1: enable comparator termination of fast decay cycle</span>
  514. <a name="l00471"></a>00471 <span class="comment"> * 0: end by time only</span>
  515. <a name="l00472"></a>00472 <span class="comment"> */</span>
  516. <a name="l00473"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649">00473</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac2d8a2bbae2aba3ed7c98e3ff1a06649" title="Sets and configure the classical Constant Off Timer Chopper.">TMC26XStepper::setConstantOffTimeChopper</a>(<span class="keywordtype">char</span> constant_off_time, <span class="keywordtype">char</span> blank_time, <span class="keywordtype">char</span> fast_decay_time_setting, <span class="keywordtype">char</span> sine_wave_offset, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> use_current_comparator) {
  517. <a name="l00474"></a>00474 <span class="comment">//perform some sanity checks</span>
  518. <a name="l00475"></a>00475 <span class="keywordflow">if</span> (constant_off_time&lt;2) {
  519. <a name="l00476"></a>00476 constant_off_time=2;
  520. <a name="l00477"></a>00477 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (constant_off_time&gt;15) {
  521. <a name="l00478"></a>00478 constant_off_time=15;
  522. <a name="l00479"></a>00479 }
  523. <a name="l00480"></a>00480 <span class="comment">//save the constant off time</span>
  524. <a name="l00481"></a>00481 this-&gt;constant_off_time = constant_off_time;
  525. <a name="l00482"></a>00482 <span class="keywordtype">char</span> blank_value;
  526. <a name="l00483"></a>00483 <span class="comment">//calculate the value acc to the clock cycles</span>
  527. <a name="l00484"></a>00484 <span class="keywordflow">if</span> (blank_time&gt;=54) {
  528. <a name="l00485"></a>00485 blank_value=3;
  529. <a name="l00486"></a>00486 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=36) {
  530. <a name="l00487"></a>00487 blank_value=2;
  531. <a name="l00488"></a>00488 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=24) {
  532. <a name="l00489"></a>00489 blank_value=1;
  533. <a name="l00490"></a>00490 } <span class="keywordflow">else</span> {
  534. <a name="l00491"></a>00491 blank_value=0;
  535. <a name="l00492"></a>00492 }
  536. <a name="l00493"></a>00493 <span class="keywordflow">if</span> (fast_decay_time_setting&lt;0) {
  537. <a name="l00494"></a>00494 fast_decay_time_setting=0;
  538. <a name="l00495"></a>00495 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fast_decay_time_setting&gt;15) {
  539. <a name="l00496"></a>00496 fast_decay_time_setting=15;
  540. <a name="l00497"></a>00497 }
  541. <a name="l00498"></a>00498 <span class="keywordflow">if</span> (sine_wave_offset &lt; -3) {
  542. <a name="l00499"></a>00499 sine_wave_offset = -3;
  543. <a name="l00500"></a>00500 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sine_wave_offset&gt;12) {
  544. <a name="l00501"></a>00501 sine_wave_offset = 12;
  545. <a name="l00502"></a>00502 }
  546. <a name="l00503"></a>00503 <span class="comment">//shift the sine_wave_offset</span>
  547. <a name="l00504"></a>00504 sine_wave_offset +=3;
  548. <a name="l00505"></a>00505
  549. <a name="l00506"></a>00506 <span class="comment">//calculate the register setting</span>
  550. <a name="l00507"></a>00507 <span class="comment">//first of all delete all the values for this</span>
  551. <a name="l00508"></a>00508 chopper_config_register &amp;= ~((1&lt;&lt;12) | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">BLANK_TIMING_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">HYSTERESIS_DECREMENT_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">HYSTERESIS_LOW_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">HYSTERESIS_START_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">T_OFF_TIMING_PATERN</a>);
  552. <a name="l00509"></a>00509 <span class="comment">//set the constant off pattern</span>
  553. <a name="l00510"></a>00510 chopper_config_register |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">CHOPPER_MODE_T_OFF_FAST_DECAY</a>;
  554. <a name="l00511"></a>00511 <span class="comment">//set the blank timing value</span>
  555. <a name="l00512"></a>00512 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
  556. <a name="l00513"></a>00513 <span class="comment">//setting the constant off time</span>
  557. <a name="l00514"></a>00514 chopper_config_register |= constant_off_time;
  558. <a name="l00515"></a>00515 <span class="comment">//set the fast decay time</span>
  559. <a name="l00516"></a>00516 <span class="comment">//set msb</span>
  560. <a name="l00517"></a>00517 chopper_config_register |= (((<span class="keywordtype">unsigned</span> long)(fast_decay_time_setting &amp; 0x8))&lt;&lt;<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a36e554a87785ce6ba998b79aae9e74e0">HYSTERESIS_DECREMENT_SHIFT</a>);
  561. <a name="l00518"></a>00518 <span class="comment">//other bits</span>
  562. <a name="l00519"></a>00519 chopper_config_register |= (((<span class="keywordtype">unsigned</span> long)(fast_decay_time_setting &amp; 0x7))&lt;&lt;<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">HYSTERESIS_START_VALUE_SHIFT</a>);
  563. <a name="l00520"></a>00520 <span class="comment">//set the sine wave offset</span>
  564. <a name="l00521"></a>00521 chopper_config_register |= (<span class="keywordtype">unsigned</span> long)sine_wave_offset &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">HYSTERESIS_LOW_SHIFT</a>;
  565. <a name="l00522"></a>00522 <span class="comment">//using the current comparator?</span>
  566. <a name="l00523"></a>00523 <span class="keywordflow">if</span> (!use_current_comparator) {
  567. <a name="l00524"></a>00524 chopper_config_register |= (1&lt;&lt;12);
  568. <a name="l00525"></a>00525 }
  569. <a name="l00526"></a>00526 <span class="comment">//if started we directly send it to the motor</span>
  570. <a name="l00527"></a>00527 <span class="keywordflow">if</span> (started) {
  571. <a name="l00528"></a>00528 send262(driver_control_register_value);
  572. <a name="l00529"></a>00529 }
  573. <a name="l00530"></a>00530 }
  574. <a name="l00531"></a>00531
  575. <a name="l00532"></a>00532 <span class="comment">/*</span>
  576. <a name="l00533"></a>00533 <span class="comment"> * constant_off_time: The off time setting controls the minimum chopper frequency. </span>
  577. <a name="l00534"></a>00534 <span class="comment"> * For most applications an off time within the range of 5μs to 20μs will fit.</span>
  578. <a name="l00535"></a>00535 <span class="comment"> * 2...15: off time setting</span>
  579. <a name="l00536"></a>00536 <span class="comment"> *</span>
  580. <a name="l00537"></a>00537 <span class="comment"> * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the</span>
  581. <a name="l00538"></a>00538 <span class="comment"> * duration of the ringing on the sense resistor. For</span>
  582. <a name="l00539"></a>00539 <span class="comment"> * 0: min. setting 3: max. setting</span>
  583. <a name="l00540"></a>00540 <span class="comment"> *</span>
  584. <a name="l00541"></a>00541 <span class="comment"> * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.</span>
  585. <a name="l00542"></a>00542 <span class="comment"> * 1...8</span>
  586. <a name="l00543"></a>00543 <span class="comment"> *</span>
  587. <a name="l00544"></a>00544 <span class="comment"> * hysteresis_end: Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by HDEC. </span>
  588. <a name="l00545"></a>00545 <span class="comment"> * The sum HSTRT+HEND must be &lt;16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.</span>
  589. <a name="l00546"></a>00546 <span class="comment"> * -3..-1: negative HEND 0: zero HEND 1...12: positive HEND</span>
  590. <a name="l00547"></a>00547 <span class="comment"> *</span>
  591. <a name="l00548"></a>00548 <span class="comment"> * hysteresis_decrement: Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time.</span>
  592. <a name="l00549"></a>00549 <span class="comment"> * 0: fast decrement 3: very slow decrement</span>
  593. <a name="l00550"></a>00550 <span class="comment"> */</span>
  594. <a name="l00551"></a>00551
  595. <a name="l00552"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa152bb7ddb72a2bc8465553a39232df2">00552</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa152bb7ddb72a2bc8465553a39232df2" title="Sets and configures with spread cycle chopper.">TMC26XStepper::setSpreadCycleChopper</a>(<span class="keywordtype">char</span> constant_off_time, <span class="keywordtype">char</span> blank_time, <span class="keywordtype">char</span> hysteresis_start, <span class="keywordtype">char</span> hysteresis_end, <span class="keywordtype">char</span> hysteresis_decrement) {
  596. <a name="l00553"></a>00553 <span class="comment">//perform some sanity checks</span>
  597. <a name="l00554"></a>00554 <span class="keywordflow">if</span> (constant_off_time&lt;2) {
  598. <a name="l00555"></a>00555 constant_off_time=2;
  599. <a name="l00556"></a>00556 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (constant_off_time&gt;15) {
  600. <a name="l00557"></a>00557 constant_off_time=15;
  601. <a name="l00558"></a>00558 }
  602. <a name="l00559"></a>00559 <span class="comment">//save the constant off time</span>
  603. <a name="l00560"></a>00560 this-&gt;constant_off_time = constant_off_time;
  604. <a name="l00561"></a>00561 <span class="keywordtype">char</span> blank_value;
  605. <a name="l00562"></a>00562 <span class="comment">//calculate the value acc to the clock cycles</span>
  606. <a name="l00563"></a>00563 <span class="keywordflow">if</span> (blank_time&gt;=54) {
  607. <a name="l00564"></a>00564 blank_value=3;
  608. <a name="l00565"></a>00565 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=36) {
  609. <a name="l00566"></a>00566 blank_value=2;
  610. <a name="l00567"></a>00567 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (blank_time&gt;=24) {
  611. <a name="l00568"></a>00568 blank_value=1;
  612. <a name="l00569"></a>00569 } <span class="keywordflow">else</span> {
  613. <a name="l00570"></a>00570 blank_value=0;
  614. <a name="l00571"></a>00571 }
  615. <a name="l00572"></a>00572 <span class="keywordflow">if</span> (hysteresis_start&lt;1) {
  616. <a name="l00573"></a>00573 hysteresis_start=1;
  617. <a name="l00574"></a>00574 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_start&gt;8) {
  618. <a name="l00575"></a>00575 hysteresis_start=8;
  619. <a name="l00576"></a>00576 }
  620. <a name="l00577"></a>00577 hysteresis_start--;
  621. <a name="l00578"></a>00578
  622. <a name="l00579"></a>00579 <span class="keywordflow">if</span> (hysteresis_end &lt; -3) {
  623. <a name="l00580"></a>00580 hysteresis_end = -3;
  624. <a name="l00581"></a>00581 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_end&gt;12) {
  625. <a name="l00582"></a>00582 hysteresis_end = 12;
  626. <a name="l00583"></a>00583 }
  627. <a name="l00584"></a>00584 <span class="comment">//shift the hysteresis_end</span>
  628. <a name="l00585"></a>00585 hysteresis_end +=3;
  629. <a name="l00586"></a>00586
  630. <a name="l00587"></a>00587 <span class="keywordflow">if</span> (hysteresis_decrement&lt;0) {
  631. <a name="l00588"></a>00588 hysteresis_decrement=0;
  632. <a name="l00589"></a>00589 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (hysteresis_decrement&gt;3) {
  633. <a name="l00590"></a>00590 hysteresis_decrement=3;
  634. <a name="l00591"></a>00591 }
  635. <a name="l00592"></a>00592
  636. <a name="l00593"></a>00593 <span class="comment">//first of all delete all the values for this</span>
  637. <a name="l00594"></a>00594 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#aaf1b564ced7de8ff3245c964e3775826">CHOPPER_MODE_T_OFF_FAST_DECAY</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a42cb2ce84258587d514ec3268548ba89">BLANK_TIMING_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a424c248097b38c1e29e6a58ad48e6bd9">HYSTERESIS_DECREMENT_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ad9d2302f6d61cd84a612a2e2bcdeb56e">HYSTERESIS_LOW_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a0de4e98b412dced62c3a4452b7483af3">HYSTERESIS_START_VALUE_PATTERN</a> | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a7659d842c803e47ba911a2a6e26327f3">T_OFF_TIMING_PATERN</a>);
  638. <a name="l00595"></a>00595
  639. <a name="l00596"></a>00596 <span class="comment">//set the blank timing value</span>
  640. <a name="l00597"></a>00597 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
  641. <a name="l00598"></a>00598 <span class="comment">//setting the constant off time</span>
  642. <a name="l00599"></a>00599 chopper_config_register |= constant_off_time;
  643. <a name="l00600"></a>00600 <span class="comment">//set the hysteresis_start</span>
  644. <a name="l00601"></a>00601 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)hysteresis_start) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac2c1c939256126e605396c4aaee3c804">HYSTERESIS_START_VALUE_SHIFT</a>;
  645. <a name="l00602"></a>00602 <span class="comment">//set the hysteresis end</span>
  646. <a name="l00603"></a>00603 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)hysteresis_end) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a38ce0bb0fa20db28351ac9167f28db98">HYSTERESIS_LOW_SHIFT</a>;
  647. <a name="l00604"></a>00604 <span class="comment">//set the hystereis decrement</span>
  648. <a name="l00605"></a>00605 chopper_config_register |= ((<span class="keywordtype">unsigned</span> long)blank_value) &lt;&lt; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#abdac78f7f2c506972265a8e5883e1eae">BLANK_TIMING_SHIFT</a>;
  649. <a name="l00606"></a>00606 <span class="comment">//if started we directly send it to the motor</span>
  650. <a name="l00607"></a>00607 <span class="keywordflow">if</span> (started) {
  651. <a name="l00608"></a>00608 send262(driver_control_register_value);
  652. <a name="l00609"></a>00609 }
  653. <a name="l00610"></a>00610 }
  654. <a name="l00611"></a>00611
  655. <a name="l00612"></a>00612 <span class="comment">/*</span>
  656. <a name="l00613"></a>00613 <span class="comment"> * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized. </span>
  657. <a name="l00614"></a>00614 <span class="comment"> * 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. </span>
  658. <a name="l00615"></a>00615 <span class="comment"> * 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 </span>
  659. <a name="l00616"></a>00616 <span class="comment"> * few microstep positions within each quarter wave. This effect normally is not audible when compared to mechanical noise generated by ball bearings, etc. </span>
  660. <a name="l00617"></a>00617 <span class="comment"> * Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.</span>
  661. <a name="l00618"></a>00618 <span class="comment"> * Hint: A common factor, which can cause motor noise, is a bad PCB layout causing coupling of both sense resistor voltages </span>
  662. <a name="l00619"></a>00619 <span class="comment"> * (please refer to sense resistor layout hint in chapter 8.1).</span>
  663. <a name="l00620"></a>00620 <span class="comment"> * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided. </span>
  664. <a name="l00621"></a>00621 <span class="comment"> * It modulates the slow decay time setting when switched on by the RNDTF bit. The RNDTF feature further spreads the chopper spectrum, </span>
  665. <a name="l00622"></a>00622 <span class="comment"> * reducing electromagnetic emission on single frequencies.</span>
  666. <a name="l00623"></a>00623 <span class="comment"> */</span>
  667. <a name="l00624"></a><a class="code" href="class_t_m_c26_x_stepper.html#a7ffd602cf4bf385847cba034417d5f0a">00624</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a7ffd602cf4bf385847cba034417d5f0a" title="Use random off time for noise reduction (0 for off, -1 for on).">TMC26XStepper::setRandomOffTime</a>(<span class="keywordtype">char</span> value) {
  668. <a name="l00625"></a>00625 <span class="keywordflow">if</span> (value) {
  669. <a name="l00626"></a>00626 chopper_config_register |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">RANDOM_TOFF_TIME</a>;
  670. <a name="l00627"></a>00627 } <span class="keywordflow">else</span> {
  671. <a name="l00628"></a>00628 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a64520580cffd416668f3b91bd60f84e1">RANDOM_TOFF_TIME</a>);
  672. <a name="l00629"></a>00629 }
  673. <a name="l00630"></a>00630 <span class="comment">//if started we directly send it to the motor</span>
  674. <a name="l00631"></a>00631 <span class="keywordflow">if</span> (started) {
  675. <a name="l00632"></a>00632 send262(driver_control_register_value);
  676. <a name="l00633"></a>00633 }
  677. <a name="l00634"></a>00634 }
  678. <a name="l00635"></a>00635
  679. <a name="l00636"></a><a class="code" href="class_t_m_c26_x_stepper.html#a381fbcce7c586ca2f1da8f9e704df14e">00636</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a381fbcce7c586ca2f1da8f9e704df14e" title="This method configures the CoolStep smart energy operation. You must have a proper StallGuard configu...">TMC26XStepper::setCoolStepConfiguration</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> lower_SG_threshold, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> SG_hysteresis, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_decrement_step_size,
  680. <a name="l00637"></a>00637 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> current_increment_step_size, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> lower_current_limit) {
  681. <a name="l00638"></a>00638 <span class="comment">//sanitize the input values</span>
  682. <a name="l00639"></a>00639 <span class="keywordflow">if</span> (lower_SG_threshold&gt;480) {
  683. <a name="l00640"></a>00640 lower_SG_threshold = 480;
  684. <a name="l00641"></a>00641 }
  685. <a name="l00642"></a>00642 <span class="comment">//divide by 32</span>
  686. <a name="l00643"></a>00643 lower_SG_threshold &gt;&gt;=5;
  687. <a name="l00644"></a>00644 <span class="keywordflow">if</span> (SG_hysteresis&gt;480) {
  688. <a name="l00645"></a>00645 SG_hysteresis=480;
  689. <a name="l00646"></a>00646 }
  690. <a name="l00647"></a>00647 <span class="comment">//divide by 32</span>
  691. <a name="l00648"></a>00648 SG_hysteresis &gt;&gt;=5;
  692. <a name="l00649"></a>00649 <span class="keywordflow">if</span> (current_decrement_step_size&gt;3) {
  693. <a name="l00650"></a>00650 current_decrement_step_size=3;
  694. <a name="l00651"></a>00651 }
  695. <a name="l00652"></a>00652 <span class="keywordflow">if</span> (current_increment_step_size&gt;3) {
  696. <a name="l00653"></a>00653 current_increment_step_size=3;
  697. <a name="l00654"></a>00654 }
  698. <a name="l00655"></a>00655 <span class="keywordflow">if</span> (lower_current_limit&gt;1) {
  699. <a name="l00656"></a>00656 lower_current_limit=1;
  700. <a name="l00657"></a>00657 }
  701. <a name="l00658"></a>00658 <span class="comment">//store the lower level in order to enable/disable the cool step</span>
  702. <a name="l00659"></a>00659 this-&gt;cool_step_lower_threshold=lower_SG_threshold;
  703. <a name="l00660"></a>00660 <span class="comment">//if cool step is not enabled we delete the lower value to keep it disabled</span>
  704. <a name="l00661"></a>00661 <span class="keywordflow">if</span> (!this-&gt;cool_step_enabled) {
  705. <a name="l00662"></a>00662 lower_SG_threshold=0;
  706. <a name="l00663"></a>00663 }
  707. <a name="l00664"></a>00664 <span class="comment">//the good news is that we can start with a complete new cool step register value</span>
  708. <a name="l00665"></a>00665 <span class="comment">//and simply set the values in the register</span>
  709. <a name="l00666"></a>00666 cool_step_register_value = ((<span class="keywordtype">unsigned</span> long)lower_SG_threshold) | (((<span class="keywordtype">unsigned</span> long)SG_hysteresis)&lt;&lt;8) | (((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)current_decrement_step_size)&lt;&lt;5)
  710. <a name="l00667"></a>00667 | (((<span class="keywordtype">unsigned</span> long)current_increment_step_size)&lt;&lt;13) | (((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)lower_current_limit)&lt;&lt;15)
  711. <a name="l00668"></a>00668 <span class="comment">//and of course we have to include the signature of the register</span>
  712. <a name="l00669"></a>00669 | <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab9828bfaa075a0a8647c709136016317">COOL_STEP_REGISTER</a>;
  713. <a name="l00670"></a>00670 <span class="comment">//Serial.println(cool_step_register_value,HEX);</span>
  714. <a name="l00671"></a>00671 <span class="keywordflow">if</span> (started) {
  715. <a name="l00672"></a>00672 send262(cool_step_register_value);
  716. <a name="l00673"></a>00673 }
  717. <a name="l00674"></a>00674 }
  718. <a name="l00675"></a>00675
  719. <a name="l00676"></a><a class="code" href="class_t_m_c26_x_stepper.html#a15bf0ed5a166a5d9a41f90f3ccbc6157">00676</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a15bf0ed5a166a5d9a41f90f3ccbc6157" title="enables or disables the CoolStep smart energy operation feature. It must be configured before enablin...">TMC26XStepper::setCoolStepEnabled</a>(<span class="keywordtype">boolean</span> enabled) {
  720. <a name="l00677"></a>00677 <span class="comment">//simply delete the lower limit to disable the cool step</span>
  721. <a name="l00678"></a>00678 cool_step_register_value &amp;= ~<a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae1862dfb958c03698b0abd95fda033ea">SE_MIN_PATTERN</a>;
  722. <a name="l00679"></a>00679 <span class="comment">//and set it to the proper value if cool step is to be enabled</span>
  723. <a name="l00680"></a>00680 <span class="keywordflow">if</span> (enabled) {
  724. <a name="l00681"></a>00681 cool_step_register_value |=this-&gt;cool_step_lower_threshold;
  725. <a name="l00682"></a>00682 }
  726. <a name="l00683"></a>00683 <span class="comment">//and save the enabled status</span>
  727. <a name="l00684"></a>00684 this-&gt;cool_step_enabled = enabled;
  728. <a name="l00685"></a>00685 <span class="comment">//save the register value</span>
  729. <a name="l00686"></a>00686 <span class="keywordflow">if</span> (started) {
  730. <a name="l00687"></a>00687 send262(cool_step_register_value);
  731. <a name="l00688"></a>00688 }
  732. <a name="l00689"></a>00689 }
  733. <a name="l00690"></a>00690
  734. <a name="l00691"></a><a class="code" href="class_t_m_c26_x_stepper.html#a6de2306b6d8dc1fa2e50fccb66d8e66d">00691</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a6de2306b6d8dc1fa2e50fccb66d8e66d" title="check if the CoolStep feature is enabled">TMC26XStepper::isCoolStepEnabled</a>(<span class="keywordtype">void</span>) {
  735. <a name="l00692"></a>00692 <span class="keywordflow">return</span> this-&gt;cool_step_enabled;
  736. <a name="l00693"></a>00693 }
  737. <a name="l00694"></a>00694
  738. <a name="l00695"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa7469949deaa39a58038b3ddef532bc8">00695</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa7469949deaa39a58038b3ddef532bc8" title="returns the lower StallGuard threshold for the CoolStep operation">TMC26XStepper::getCoolStepLowerSgThreshold</a>() {
  739. <a name="l00696"></a>00696 <span class="comment">//we return our internally stored value - in order to provide the correct setting even if cool step is not enabled</span>
  740. <a name="l00697"></a>00697 <span class="keywordflow">return</span> this-&gt;cool_step_lower_threshold&lt;&lt;5;
  741. <a name="l00698"></a>00698 }
  742. <a name="l00699"></a>00699
  743. <a name="l00700"></a><a class="code" href="class_t_m_c26_x_stepper.html#ac61298fd658773c28823d33ab04e970f">00700</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ac61298fd658773c28823d33ab04e970f" title="returns the upper StallGuard threshold for the CoolStep operation">TMC26XStepper::getCoolStepUpperSgThreshold</a>() {
  744. <a name="l00701"></a>00701 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac8f748bf735c447dbed7dd4c7b631a87">SE_MAX_PATTERN</a>)&gt;&gt;8)&lt;&lt;5;
  745. <a name="l00702"></a>00702 }
  746. <a name="l00703"></a>00703
  747. <a name="l00704"></a><a class="code" href="class_t_m_c26_x_stepper.html#ababe688a15f087d23d4ff2094fcee883">00704</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#ababe688a15f087d23d4ff2094fcee883" title="returns the increment steps for the current for the CoolStep operation">TMC26XStepper::getCoolStepCurrentIncrementSize</a>() {
  748. <a name="l00705"></a>00705 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbe13a0464355e42fbe786ca5f58ed8d">CURRENT_DOWN_STEP_SPEED_PATTERN</a>)&gt;&gt;13);
  749. <a name="l00706"></a>00706 }
  750. <a name="l00707"></a>00707
  751. <a name="l00708"></a><a class="code" href="class_t_m_c26_x_stepper.html#aad44ee5ae73bf8e69af05674a304ba46">00708</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#aad44ee5ae73bf8e69af05674a304ba46" title="returns the number of StallGuard readings befor CoolStep adjusts the motor current.">TMC26XStepper::getCoolStepNumberOfSGReadings</a>() {
  752. <a name="l00709"></a>00709 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aba6c07e5672e34e618bb3a550ab0d2bc">SE_CURRENT_STEP_WIDTH_PATTERN</a>)&gt;&gt;5);
  753. <a name="l00710"></a>00710 }
  754. <a name="l00711"></a>00711
  755. <a name="l00712"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0c7e8541abc120a3910e35c6fbf2167c">00712</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0c7e8541abc120a3910e35c6fbf2167c" title="returns the absolut minium current for the CoolStep operation">TMC26XStepper::getCoolStepLowerCurrentLimit</a>() {
  756. <a name="l00713"></a>00713 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((cool_step_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8a261a77d198b85f6dd8416387b354b3">MINIMUM_CURRENT_FOURTH</a>)&gt;&gt;15);
  757. <a name="l00714"></a>00714 }
  758. <a name="l00715"></a>00715
  759. <a name="l00716"></a><a class="code" href="class_t_m_c26_x_stepper.html#a4472cd86ad5b65dec5ec45ce69158305">00716</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#a4472cd86ad5b65dec5ec45ce69158305" title="enables or disables the motor driver bridges. If disabled the motor can run freely. If enabled not.">TMC26XStepper::setEnabled</a>(<span class="keywordtype">boolean</span> enabled) {
  760. <a name="l00717"></a>00717 <span class="comment">//delete the t_off in the chopper config to get sure</span>
  761. <a name="l00718"></a>00718 chopper_config_register &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">T_OFF_PATTERN</a>);
  762. <a name="l00719"></a>00719 <span class="keywordflow">if</span> (enabled) {
  763. <a name="l00720"></a>00720 <span class="comment">//and set the t_off time</span>
  764. <a name="l00721"></a>00721 chopper_config_register |= this-&gt;constant_off_time;
  765. <a name="l00722"></a>00722 }
  766. <a name="l00723"></a>00723 <span class="comment">//if not enabled we don&#39;t have to do anything since we already delete t_off from the register</span>
  767. <a name="l00724"></a>00724 <span class="keywordflow">if</span> (started) {
  768. <a name="l00725"></a>00725 send262(chopper_config_register);
  769. <a name="l00726"></a>00726 }
  770. <a name="l00727"></a>00727 }
  771. <a name="l00728"></a>00728
  772. <a name="l00729"></a><a class="code" href="class_t_m_c26_x_stepper.html#a15796c0cbdeab23a343c3f25327283b6">00729</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a15796c0cbdeab23a343c3f25327283b6" title="checks if the output bridges are enabled. If the bridges are not enabled the motor can run freely...">TMC26XStepper::isEnabled</a>() {
  773. <a name="l00730"></a>00730 <span class="keywordflow">if</span> (chopper_config_register &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa4e49237f2671e7f28aa34ae0e89da8d">T_OFF_PATTERN</a>) {
  774. <a name="l00731"></a>00731 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  775. <a name="l00732"></a>00732 } <span class="keywordflow">else</span> {
  776. <a name="l00733"></a>00733 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  777. <a name="l00734"></a>00734 }
  778. <a name="l00735"></a>00735 }
  779. <a name="l00736"></a>00736
  780. <a name="l00737"></a>00737 <span class="comment">/*</span>
  781. <a name="l00738"></a>00738 <span class="comment"> * reads a value from the TMC26X status register. The value is not obtained directly but can then </span>
  782. <a name="l00739"></a>00739 <span class="comment"> * be read by the various status routines.</span>
  783. <a name="l00740"></a>00740 <span class="comment"> *</span>
  784. <a name="l00741"></a>00741 <span class="comment"> */</span>
  785. <a name="l00742"></a><a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967">00742</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">TMC26XStepper::readStatus</a>(<span class="keywordtype">char</span> read_value) {
  786. <a name="l00743"></a>00743 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> old_driver_configuration_register_value = driver_configuration_register_value;
  787. <a name="l00744"></a>00744 <span class="comment">//reset the readout configuration</span>
  788. <a name="l00745"></a>00745 driver_configuration_register_value &amp;= ~(<a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">READ_SELECTION_PATTERN</a>);
  789. <a name="l00746"></a>00746 <span class="comment">//this now equals TMC26X_READOUT_POSITION - so we just have to check the other two options</span>
  790. <a name="l00747"></a>00747 <span class="keywordflow">if</span> (read_value == <a class="code" href="_t_m_c26_x_stepper_8h.html#ac864ff8886123039c7d2d3c617f7ef87">TMC26X_READOUT_STALLGUARD</a>) {
  791. <a name="l00748"></a>00748 driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>;
  792. <a name="l00749"></a>00749 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (read_value == <a class="code" href="_t_m_c26_x_stepper_8h.html#a01760ad15e3846536526a990efe47094">TMC26X_READOUT_CURRENT</a>) {
  793. <a name="l00750"></a>00750 driver_configuration_register_value |= <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">READ_STALL_GUARD_AND_COOL_STEP</a>;
  794. <a name="l00751"></a>00751 }
  795. <a name="l00752"></a>00752 <span class="comment">//all other cases are ignored to prevent funny values</span>
  796. <a name="l00753"></a>00753 <span class="comment">//check if the readout is configured for the value we are interested in</span>
  797. <a name="l00754"></a>00754 <span class="keywordflow">if</span> (driver_configuration_register_value!=old_driver_configuration_register_value) {
  798. <a name="l00755"></a>00755 <span class="comment">//because then we need to write the value twice - one time for configuring, second time to get the value, see below</span>
  799. <a name="l00756"></a>00756 send262(driver_configuration_register_value);
  800. <a name="l00757"></a>00757 }
  801. <a name="l00758"></a>00758 <span class="comment">//write the configuration to get the last status </span>
  802. <a name="l00759"></a>00759 send262(driver_configuration_register_value);
  803. <a name="l00760"></a>00760 }
  804. <a name="l00761"></a>00761
  805. <a name="l00762"></a><a class="code" href="class_t_m_c26_x_stepper.html#a1019f6f889acfd3176eecd60a0a20125">00762</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#a1019f6f889acfd3176eecd60a0a20125" title="Get the current microstep position for phase A.">TMC26XStepper::getMotorPosition</a>(<span class="keywordtype">void</span>) {
  806. <a name="l00763"></a>00763 <span class="comment">//we read it out even if we are not started yet - perhaps it is useful information for somebody</span>
  807. <a name="l00764"></a>00764 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#aff05d4a47ef8821322ccc2a20785fbee">TMC26X_READOUT_POSITION</a>);
  808. <a name="l00765"></a>00765 <span class="keywordflow">return</span> getReadoutValue();
  809. <a name="l00766"></a>00766 }
  810. <a name="l00767"></a>00767
  811. <a name="l00768"></a>00768 <span class="comment">//reads the stall guard setting from last status</span>
  812. <a name="l00769"></a>00769 <span class="comment">//returns -1 if stallguard information is not present</span>
  813. <a name="l00770"></a><a class="code" href="class_t_m_c26_x_stepper.html#aed570ce3eea640e087b046333015de1e">00770</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aed570ce3eea640e087b046333015de1e" title="Reads the current StallGuard value.">TMC26XStepper::getCurrentStallGuardReading</a>(<span class="keywordtype">void</span>) {
  814. <a name="l00771"></a>00771 <span class="comment">//if we don&#39;t yet started there cannot be a stall guard value</span>
  815. <a name="l00772"></a>00772 <span class="keywordflow">if</span> (!started) {
  816. <a name="l00773"></a>00773 <span class="keywordflow">return</span> -1;
  817. <a name="l00774"></a>00774 }
  818. <a name="l00775"></a>00775 <span class="comment">//not time optimal, but solution optiomal:</span>
  819. <a name="l00776"></a>00776 <span class="comment">//first read out the stall guard value</span>
  820. <a name="l00777"></a>00777 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#ac864ff8886123039c7d2d3c617f7ef87">TMC26X_READOUT_STALLGUARD</a>);
  821. <a name="l00778"></a>00778 <span class="keywordflow">return</span> getReadoutValue();
  822. <a name="l00779"></a>00779 }
  823. <a name="l00780"></a>00780
  824. <a name="l00781"></a><a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1">00781</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1" title="Reads the current current setting value as fraction of the maximum current Returns values between 0 a...">TMC26XStepper::getCurrentCSReading</a>(<span class="keywordtype">void</span>) {
  825. <a name="l00782"></a>00782 <span class="comment">//if we don&#39;t yet started there cannot be a stall guard value</span>
  826. <a name="l00783"></a>00783 <span class="keywordflow">if</span> (!started) {
  827. <a name="l00784"></a>00784 <span class="keywordflow">return</span> 0;
  828. <a name="l00785"></a>00785 }
  829. <a name="l00786"></a>00786 <span class="comment">//not time optimal, but solution optiomal:</span>
  830. <a name="l00787"></a>00787 <span class="comment">//first read out the stall guard value</span>
  831. <a name="l00788"></a>00788 <a class="code" href="class_t_m_c26_x_stepper.html#af95a824bfdf49ef979b5354798e52967" title="Manually read out the status register This function sends a byte to the motor driver in order to get ...">readStatus</a>(<a class="code" href="_t_m_c26_x_stepper_8h.html#a01760ad15e3846536526a990efe47094">TMC26X_READOUT_CURRENT</a>);
  832. <a name="l00789"></a>00789 <span class="keywordflow">return</span> (getReadoutValue() &amp; 0x1f);
  833. <a name="l00790"></a>00790 }
  834. <a name="l00791"></a>00791
  835. <a name="l00792"></a><a class="code" href="class_t_m_c26_x_stepper.html#aa00741168a7def0a7a9d2f2c9d3b99d7">00792</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#aa00741168a7def0a7a9d2f2c9d3b99d7" title="Reads the current current setting value and recalculates the absolute current in mA (1A would be 1000...">TMC26XStepper::getCurrentCurrent</a>(<span class="keywordtype">void</span>) {
  836. <a name="l00793"></a>00793 <span class="keywordtype">double</span> result = (double)<a class="code" href="class_t_m_c26_x_stepper.html#a1a939fb495d747c2c11be99a740371e1" title="Reads the current current setting value as fraction of the maximum current Returns values between 0 a...">getCurrentCSReading</a>();
  837. <a name="l00794"></a>00794 <span class="keywordtype">double</span> resistor_value = (double)this-&gt;resistor;
  838. <a name="l00795"></a>00795 <span class="keywordtype">double</span> voltage = (driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>)? 0.165:0.31;
  839. <a name="l00796"></a>00796 result = (result+1.0)/32.0*voltage/resistor_value*1000.0*1000.0;
  840. <a name="l00797"></a>00797 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)result;
  841. <a name="l00798"></a>00798 }
  842. <a name="l00799"></a>00799
  843. <a name="l00800"></a>00800 <span class="comment">/*</span>
  844. <a name="l00801"></a>00801 <span class="comment"> return true if the stallguard threshold has been reached</span>
  845. <a name="l00802"></a>00802 <span class="comment">*/</span>
  846. <a name="l00803"></a><a class="code" href="class_t_m_c26_x_stepper.html#aea4c6e1fac909116c6b55f902d6cff41">00803</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#aea4c6e1fac909116c6b55f902d6cff41" title="checks if there is a StallGuard warning in the last status">TMC26XStepper::isStallGuardOverThreshold</a>(<span class="keywordtype">void</span>) {
  847. <a name="l00804"></a>00804 <span class="keywordflow">if</span> (!this-&gt;started) {
  848. <a name="l00805"></a>00805 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  849. <a name="l00806"></a>00806 }
  850. <a name="l00807"></a>00807 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">STATUS_STALL_GUARD_STATUS</a>);
  851. <a name="l00808"></a>00808 }
  852. <a name="l00809"></a>00809
  853. <a name="l00810"></a>00810 <span class="comment">/*</span>
  854. <a name="l00811"></a>00811 <span class="comment"> returns if there is any over temperature condition:</span>
  855. <a name="l00812"></a>00812 <span class="comment"> OVER_TEMPERATURE_PREWARING if pre warning level has been reached</span>
  856. <a name="l00813"></a>00813 <span class="comment"> OVER_TEMPERATURE_SHUTDOWN if the temperature is so hot that the driver is shut down</span>
  857. <a name="l00814"></a>00814 <span class="comment"> Any of those levels are not too good.</span>
  858. <a name="l00815"></a>00815 <span class="comment">*/</span>
  859. <a name="l00816"></a><a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7">00816</a> <span class="keywordtype">char</span> <a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">TMC26XStepper::getOverTemperature</a>(<span class="keywordtype">void</span>) {
  860. <a name="l00817"></a>00817 <span class="keywordflow">if</span> (!this-&gt;started) {
  861. <a name="l00818"></a>00818 <span class="keywordflow">return</span> 0;
  862. <a name="l00819"></a>00819 }
  863. <a name="l00820"></a>00820 <span class="keywordflow">if</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#adbb23d2f055c9eab55eac29d1a75deb4">STATUS_OVER_TEMPERATURE_SHUTDOWN</a>) {
  864. <a name="l00821"></a>00821 <span class="keywordflow">return</span> <a class="code" href="_t_m_c26_x_stepper_8h.html#adae814ce848677abd87758c7ac79a436" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature shutdown in the TMC...">TMC26X_OVERTEMPERATURE_SHUTDOWN</a>;
  865. <a name="l00822"></a>00822 }
  866. <a name="l00823"></a>00823 <span class="keywordflow">if</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa317fd77f2f26fdfbfd331e21d9069e8">STATUS_OVER_TEMPERATURE_WARNING</a>) {
  867. <a name="l00824"></a>00824 <span class="keywordflow">return</span> <a class="code" href="_t_m_c26_x_stepper_8h.html#add42eee34f674f92c19bcd5266d2445f" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature situation in the TM...">TMC26X_OVERTEMPERATURE_PREWARING</a>;
  868. <a name="l00825"></a>00825 }
  869. <a name="l00826"></a>00826 <span class="keywordflow">return</span> 0;
  870. <a name="l00827"></a>00827 }
  871. <a name="l00828"></a>00828
  872. <a name="l00829"></a>00829 <span class="comment">//is motor channel A shorted to ground</span>
  873. <a name="l00830"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf">00830</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf" title="Is motor channel A shorted to ground detected in the last status readout.">TMC26XStepper::isShortToGroundA</a>(<span class="keywordtype">void</span>) {
  874. <a name="l00831"></a>00831 <span class="keywordflow">if</span> (!this-&gt;started) {
  875. <a name="l00832"></a>00832 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  876. <a name="l00833"></a>00833 }
  877. <a name="l00834"></a>00834 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a8e03041302a092174fa33b3cf837dca2">STATUS_SHORT_TO_GROUND_A</a>);
  878. <a name="l00835"></a>00835 }
  879. <a name="l00836"></a>00836
  880. <a name="l00837"></a>00837 <span class="comment">//is motor channel B shorted to ground</span>
  881. <a name="l00838"></a><a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b">00838</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b" title="Is motor channel B shorted to ground detected in the last status readout.">TMC26XStepper::isShortToGroundB</a>(<span class="keywordtype">void</span>) {
  882. <a name="l00839"></a>00839 <span class="keywordflow">if</span> (!this-&gt;started) {
  883. <a name="l00840"></a>00840 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  884. <a name="l00841"></a>00841 }
  885. <a name="l00842"></a>00842 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a903c3eba99695a32c6736463dcfd93ae">STATUS_SHORT_TO_GROUND_B</a>);
  886. <a name="l00843"></a>00843 }
  887. <a name="l00844"></a>00844
  888. <a name="l00845"></a>00845 <span class="comment">//is motor channel A connected</span>
  889. <a name="l00846"></a><a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c">00846</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c" title="iIs motor channel A connected according to the last statu readout.">TMC26XStepper::isOpenLoadA</a>(<span class="keywordtype">void</span>) {
  890. <a name="l00847"></a>00847 <span class="keywordflow">if</span> (!this-&gt;started) {
  891. <a name="l00848"></a>00848 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  892. <a name="l00849"></a>00849 }
  893. <a name="l00850"></a>00850 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ae9cbbe5af7188e6bff8fe412f8e42f59">STATUS_OPEN_LOAD_A</a>);
  894. <a name="l00851"></a>00851 }
  895. <a name="l00852"></a>00852
  896. <a name="l00853"></a>00853 <span class="comment">//is motor channel B connected</span>
  897. <a name="l00854"></a><a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303">00854</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303" title="iIs motor channel A connected according to the last statu readout.">TMC26XStepper::isOpenLoadB</a>(<span class="keywordtype">void</span>) {
  898. <a name="l00855"></a>00855 <span class="keywordflow">if</span> (!this-&gt;started) {
  899. <a name="l00856"></a>00856 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  900. <a name="l00857"></a>00857 }
  901. <a name="l00858"></a>00858 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab29dc5cd6c6c4e5bf99e71bd563e1be1">STATUS_OPEN_LOAD_B</a>);
  902. <a name="l00859"></a>00859 }
  903. <a name="l00860"></a>00860
  904. <a name="l00861"></a>00861 <span class="comment">//is chopper inactive since 2^20 clock cycles - defaults to ~0,08s</span>
  905. <a name="l00862"></a><a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0">00862</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0" title="Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s.">TMC26XStepper::isStandStill</a>(<span class="keywordtype">void</span>) {
  906. <a name="l00863"></a>00863 <span class="keywordflow">if</span> (!this-&gt;started) {
  907. <a name="l00864"></a>00864 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  908. <a name="l00865"></a>00865 }
  909. <a name="l00866"></a>00866 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ab96ed1635faee6650e9cce73598a2773">STATUS_STAND_STILL</a>);
  910. <a name="l00867"></a>00867 }
  911. <a name="l00868"></a>00868
  912. <a name="l00869"></a>00869 <span class="comment">//is chopper inactive since 2^20 clock cycles - defaults to ~0,08s</span>
  913. <a name="l00870"></a><a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7">00870</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7" title="checks if there is a StallGuard warning in the last status">TMC26XStepper::isStallGuardReached</a>(<span class="keywordtype">void</span>) {
  914. <a name="l00871"></a>00871 <span class="keywordflow">if</span> (!this-&gt;started) {
  915. <a name="l00872"></a>00872 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  916. <a name="l00873"></a>00873 }
  917. <a name="l00874"></a>00874 <span class="keywordflow">return</span> (driver_status_result &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aa09ef662fd19bf2d063d6bd0f48eca14">STATUS_STALL_GUARD_STATUS</a>);
  918. <a name="l00875"></a>00875 }
  919. <a name="l00876"></a>00876
  920. <a name="l00877"></a>00877 <span class="comment">//reads the stall guard setting from last status</span>
  921. <a name="l00878"></a>00878 <span class="comment">//returns -1 if stallguard inforamtion is not present</span>
  922. <a name="l00879"></a>00879 <span class="keywordtype">int</span> TMC26XStepper::getReadoutValue(<span class="keywordtype">void</span>) {
  923. <a name="l00880"></a>00880 <span class="keywordflow">return</span> (<span class="keywordtype">int</span>)(driver_status_result &gt;&gt; 10);
  924. <a name="l00881"></a>00881 }
  925. <a name="l00882"></a>00882
  926. <a name="l00883"></a><a class="code" href="class_t_m_c26_x_stepper.html#ae1db5ec2ec9bfbfaea83c659e006692e">00883</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ae1db5ec2ec9bfbfaea83c659e006692e" title="Returns the current sense resistor value in milliohm. The default value of ,15 Ohm will return 150...">TMC26XStepper::getResistor</a>() {
  927. <a name="l00884"></a>00884 <span class="keywordflow">return</span> this-&gt;resistor;
  928. <a name="l00885"></a>00885 }
  929. <a name="l00886"></a>00886
  930. <a name="l00887"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad435db189ebb101fb2de90a484f33905">00887</a> <span class="keywordtype">boolean</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad435db189ebb101fb2de90a484f33905" title="a convenience method to determine if the current scaling uses 0.31V or 0.165V as reference.">TMC26XStepper::isCurrentScalingHalfed</a>() {
  931. <a name="l00888"></a>00888 <span class="keywordflow">if</span> (this-&gt;driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a4fb1c008e2ff76eee9362600eed112e1">VSENSE</a>) {
  932. <a name="l00889"></a>00889 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  933. <a name="l00890"></a>00890 } <span class="keywordflow">else</span> {
  934. <a name="l00891"></a>00891 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  935. <a name="l00892"></a>00892 }
  936. <a name="l00893"></a>00893 }
  937. <a name="l00894"></a>00894 <span class="comment">/*</span>
  938. <a name="l00895"></a>00895 <span class="comment"> version() returns the version of the library:</span>
  939. <a name="l00896"></a>00896 <span class="comment"> */</span>
  940. <a name="l00897"></a><a class="code" href="class_t_m_c26_x_stepper.html#ab040d9df1e85d6fb0c105205a36b0215">00897</a> <span class="keywordtype">int</span> <a class="code" href="class_t_m_c26_x_stepper.html#ab040d9df1e85d6fb0c105205a36b0215" title="library version">TMC26XStepper::version</a>(<span class="keywordtype">void</span>)
  941. <a name="l00898"></a>00898 {
  942. <a name="l00899"></a>00899 <span class="keywordflow">return</span> 1;
  943. <a name="l00900"></a>00900 }
  944. <a name="l00901"></a>00901
  945. <a name="l00902"></a><a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3">00902</a> <span class="keywordtype">void</span> <a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3" title="Prints out all the information that can be found in the last status read out - it does not force a st...">TMC26XStepper::debugLastStatus</a>() {
  946. <a name="l00903"></a>00903 <span class="preprocessor">#ifdef DEBUG </span>
  947. <a name="l00904"></a>00904 <span class="preprocessor"></span><span class="keywordflow">if</span> (this-&gt;started) {
  948. <a name="l00905"></a>00905 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">getOverTemperature</a>()&amp;<a class="code" href="_t_m_c26_x_stepper_8h.html#add42eee34f674f92c19bcd5266d2445f" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature situation in the TM...">TMC26X_OVERTEMPERATURE_PREWARING</a>) {
  949. <a name="l00906"></a>00906 Serial.println(<span class="stringliteral">&quot;WARNING: Overtemperature Prewarning!&quot;</span>);
  950. <a name="l00907"></a>00907 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a7662c2fbc03d1f5a7da5cabcc153b2d7" title="Return over temperature status of the last status readout return 0 is everything is OK...">getOverTemperature</a>()&amp;<a class="code" href="_t_m_c26_x_stepper_8h.html#adae814ce848677abd87758c7ac79a436" title="return value for TMC26XStepper.getOverTemperature() if there is a overtemperature shutdown in the TMC...">TMC26X_OVERTEMPERATURE_SHUTDOWN</a>) {
  951. <a name="l00908"></a>00908 Serial.println(<span class="stringliteral">&quot;ERROR: Overtemperature Shutdown!&quot;</span>);
  952. <a name="l00909"></a>00909 }
  953. <a name="l00910"></a>00910 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#ad329fa4693d3139dea241ebe3d0f33cf" title="Is motor channel A shorted to ground detected in the last status readout.">isShortToGroundA</a>()) {
  954. <a name="l00911"></a>00911 Serial.println(<span class="stringliteral">&quot;ERROR: SHORT to ground on channel A!&quot;</span>);
  955. <a name="l00912"></a>00912 }
  956. <a name="l00913"></a>00913 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a0ccb54d40cce0d802aa56ff6261f9f3b" title="Is motor channel B shorted to ground detected in the last status readout.">isShortToGroundB</a>()) {
  957. <a name="l00914"></a>00914 Serial.println(<span class="stringliteral">&quot;ERROR: SHORT to ground on channel A!&quot;</span>);
  958. <a name="l00915"></a>00915 }
  959. <a name="l00916"></a>00916 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#af97b2ab9d1ba36765ac6f17cf25ec45c" title="iIs motor channel A connected according to the last statu readout.">isOpenLoadA</a>()) {
  960. <a name="l00917"></a>00917 Serial.println(<span class="stringliteral">&quot;ERROR: Channel A seems to be unconnected!&quot;</span>);
  961. <a name="l00918"></a>00918 }
  962. <a name="l00919"></a>00919 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#a303590124f5ac6d6a06d0ec60d0b5303" title="iIs motor channel A connected according to the last statu readout.">isOpenLoadB</a>()) {
  963. <a name="l00920"></a>00920 Serial.println(<span class="stringliteral">&quot;ERROR: Channel B seems to be unconnected!&quot;</span>);
  964. <a name="l00921"></a>00921 }
  965. <a name="l00922"></a>00922 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#afdeded501ec2cabeffde33d31b6573f7" title="checks if there is a StallGuard warning in the last status">isStallGuardReached</a>()) {
  966. <a name="l00923"></a>00923 Serial.println(<span class="stringliteral">&quot;INFO: Stall Guard level reached!&quot;</span>);
  967. <a name="l00924"></a>00924 }
  968. <a name="l00925"></a>00925 <span class="keywordflow">if</span> (this-&gt;<a class="code" href="class_t_m_c26_x_stepper.html#ab26602f360a4fb6ec6d262011675b2b0" title="Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s.">isStandStill</a>()) {
  969. <a name="l00926"></a>00926 Serial.println(<span class="stringliteral">&quot;INFO: Motor is standing still.&quot;</span>);
  970. <a name="l00927"></a>00927 }
  971. <a name="l00928"></a>00928 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> readout_config = driver_configuration_register_value &amp; <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a88a4b45fa6385eba8aa4f0342334b832">READ_SELECTION_PATTERN</a>;
  972. <a name="l00929"></a>00929 <span class="keywordtype">int</span> value = getReadoutValue();
  973. <a name="l00930"></a>00930 <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#a143b7757272f07866d9655bde8303d9a">READ_MICROSTEP_POSTION</a>) {
  974. <a name="l00931"></a>00931 Serial.print(<span class="stringliteral">&quot;Microstep postion phase A: &quot;</span>);
  975. <a name="l00932"></a>00932 Serial.println(value);
  976. <a name="l00933"></a>00933 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#ac1bd4da94fab7ce1049be2f866211819">READ_STALL_GUARD_READING</a>) {
  977. <a name="l00934"></a>00934 Serial.print(<span class="stringliteral">&quot;Stall Guard value:&quot;</span>);
  978. <a name="l00935"></a>00935 Serial.println(value);
  979. <a name="l00936"></a>00936 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readout_config == <a class="code" href="_t_m_c26_x_stepper_8cpp.html#aef62b7fdcbac0b33b2d6e9cea4b5f9b2">READ_STALL_GUARD_AND_COOL_STEP</a>) {
  980. <a name="l00937"></a>00937 <span class="keywordtype">int</span> stallGuard = value &amp; 0xf;
  981. <a name="l00938"></a>00938 <span class="keywordtype">int</span> current = value &amp; 0x1F0;
  982. <a name="l00939"></a>00939 Serial.print(<span class="stringliteral">&quot;Approx Stall Guard: &quot;</span>);
  983. <a name="l00940"></a>00940 Serial.println(stallGuard);
  984. <a name="l00941"></a>00941 Serial.print(<span class="stringliteral">&quot;Current level&quot;</span>);
  985. <a name="l00942"></a>00942 Serial.println(current);
  986. <a name="l00943"></a>00943 }
  987. <a name="l00944"></a>00944 }
  988. <a name="l00945"></a>00945 <span class="preprocessor">#endif</span>
  989. <a name="l00946"></a>00946 <span class="preprocessor"></span>}
  990. <a name="l00947"></a>00947
  991. <a name="l00948"></a>00948 <span class="comment">/*</span>
  992. <a name="l00949"></a>00949 <span class="comment"> * send register settings to the stepper driver via SPI</span>
  993. <a name="l00950"></a>00950 <span class="comment"> * returns the current status</span>
  994. <a name="l00951"></a>00951 <span class="comment"> */</span>
  995. <a name="l00952"></a>00952 <span class="keyword">inline</span> <span class="keywordtype">void</span> TMC26XStepper::send262(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> datagram) {
  996. <a name="l00953"></a>00953 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> i_datagram;
  997. <a name="l00954"></a>00954
  998. <a name="l00955"></a>00955 <span class="comment">//preserver the previous spi mode</span>
  999. <a name="l00956"></a>00956 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> oldMode = SPCR &amp; SPI_MODE_MASK;
  1000. <a name="l00957"></a>00957
  1001. <a name="l00958"></a>00958 <span class="comment">//if the mode is not correct set it to mode 3</span>
  1002. <a name="l00959"></a>00959 <span class="keywordflow">if</span> (oldMode != SPI_MODE3) {
  1003. <a name="l00960"></a>00960 SPI.setDataMode(SPI_MODE3);
  1004. <a name="l00961"></a>00961 }
  1005. <a name="l00962"></a>00962
  1006. <a name="l00963"></a>00963 <span class="comment">//select the TMC driver</span>
  1007. <a name="l00964"></a>00964 digitalWrite(cs_pin,LOW);
  1008. <a name="l00965"></a>00965
  1009. <a name="l00966"></a>00966 <span class="comment">//ensure that only valid bist are set (0-19)</span>
  1010. <a name="l00967"></a>00967 <span class="comment">//datagram &amp;=REGISTER_BIT_PATTERN;</span>
  1011. <a name="l00968"></a>00968
  1012. <a name="l00969"></a>00969 <span class="preprocessor">#ifdef DEBUG</span>
  1013. <a name="l00970"></a>00970 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Sending &quot;</span>);
  1014. <a name="l00971"></a>00971 Serial.println(datagram,HEX);
  1015. <a name="l00972"></a>00972 <span class="preprocessor">#endif</span>
  1016. <a name="l00973"></a>00973 <span class="preprocessor"></span>
  1017. <a name="l00974"></a>00974 <span class="comment">//write/read the values</span>
  1018. <a name="l00975"></a>00975 i_datagram = SPI.transfer((datagram &gt;&gt; 16) &amp; 0xff);
  1019. <a name="l00976"></a>00976 i_datagram &lt;&lt;= 8;
  1020. <a name="l00977"></a>00977 i_datagram |= SPI.transfer((datagram &gt;&gt; 8) &amp; 0xff);
  1021. <a name="l00978"></a>00978 i_datagram &lt;&lt;= 8;
  1022. <a name="l00979"></a>00979 i_datagram |= SPI.transfer((datagram) &amp; 0xff);
  1023. <a name="l00980"></a>00980 i_datagram &gt;&gt;= 4;
  1024. <a name="l00981"></a>00981
  1025. <a name="l00982"></a>00982 <span class="preprocessor">#ifdef DEBUG</span>
  1026. <a name="l00983"></a>00983 <span class="preprocessor"></span> Serial.print(<span class="stringliteral">&quot;Received &quot;</span>);
  1027. <a name="l00984"></a>00984 Serial.println(i_datagram,HEX);
  1028. <a name="l00985"></a>00985 <a class="code" href="class_t_m_c26_x_stepper.html#ad5e5b1bf5a46d02577dd548083877ec3" title="Prints out all the information that can be found in the last status read out - it does not force a st...">debugLastStatus</a>();
  1029. <a name="l00986"></a>00986 <span class="preprocessor">#endif</span>
  1030. <a name="l00987"></a>00987 <span class="preprocessor"></span> <span class="comment">//deselect the TMC chip</span>
  1031. <a name="l00988"></a>00988 digitalWrite(cs_pin,HIGH);
  1032. <a name="l00989"></a>00989
  1033. <a name="l00990"></a>00990 <span class="comment">//restore the previous SPI mode if neccessary</span>
  1034. <a name="l00991"></a>00991 <span class="comment">//if the mode is not correct set it to mode 3</span>
  1035. <a name="l00992"></a>00992 <span class="keywordflow">if</span> (oldMode != SPI_MODE3) {
  1036. <a name="l00993"></a>00993 SPI.setDataMode(oldMode);
  1037. <a name="l00994"></a>00994 }
  1038. <a name="l00995"></a>00995
  1039. <a name="l00996"></a>00996
  1040. <a name="l00997"></a>00997 <span class="comment">//store the datagram as status result</span>
  1041. <a name="l00998"></a>00998 driver_status_result = i_datagram;
  1042. <a name="l00999"></a>00999 }
  1043. </pre></div></div><!-- contents -->
  1044. <hr class="footer"/><address class="footer"><small>
  1045. Generated on Mon Nov 19 2012 20:26:21 for Trinamic TMC26X Stepper Driver for Arduino by &#160;<a href="http://www.doxygen.org/index.html">
  1046. <img class="footer" src="doxygen.png" alt="doxygen"/>
  1047. </a> 1.7.6.1
  1048. </small></address>
  1049. </body>
  1050. </html>