Browse Source

dcStep demo

JonathanS 5 months ago
parent
commit
5903a79bde
1 changed files with 49 additions and 15 deletions
  1. 49
    15
      src/getting_started.ino

+ 49
- 15
src/getting_started.ino View File

@@ -26,16 +26,44 @@ v0.1 - it works
26 26
 #include <Trinamic_TMC2130.h>
27 27
 
28 28
 // pin configuration (this is the X-axis on a standard RAMPS 1.4)
29
-#define CS_PIN 21
30
-#define EN_PIN 18 //enable (CFG6)
31
-#define DIR_PIN 19 //direction
32
-#define STEP_PIN 20  //step
29
+#define CS_PIN 7
30
+#define EN_PIN 8 //enable (CFG6)
31
+#define DIR_PIN 9 //direction
32
+#define STEP_PIN 10  //step
33 33
 
34 34
 Trinamic_TMC2130 myStepper(CS_PIN);
35 35
 
36
+#define PRINT_REG(NAME, regval)                \
37
+    Serial.print(#NAME ": ");                   \
38
+    myStepper.read_REG(NAME, &regval);  \
39
+    Serial.println(regval, BIN);                  \
40
+
41
+void print_regdump()
42
+{
43
+  uint32_t regval;
44
+
45
+  PRINT_REG(TMC_REG_GCONF, regval)
46
+  PRINT_REG(TMC_REG_GSTAT, regval)
47
+  PRINT_REG(TMC_REG_IOIN, regval)
48
+  PRINT_REG(TMC_REG_TSTEP, regval)
49
+  PRINT_REG(TMC_REG_XDIRECT, regval)
50
+  PRINT_REG(TMC_REG_MSCNT, regval)
51
+  PRINT_REG(TMC_REG_MSCURACT, regval)
52
+  PRINT_REG(TMC_REG_CHOPCONF, regval)
53
+  PRINT_REG(TMC_REG_DRV_STATUS, regval)
54
+  PRINT_REG(TMC_REG_PWM_SCALE, regval)
55
+  PRINT_REG(TMC_REG_LOST_STEPS, regval)
56
+
57
+}
58
+
36 59
 void setup(){
60
+  Serial.begin(115200);
61
+  Serial.println("Waiting for serial...");
62
+  
37 63
   while (!Serial);
38 64
 
65
+  Serial.println("Hello");
66
+
39 67
   // pins
40 68
   pinMode(EN_PIN, OUTPUT);
41 69
   pinMode(DIR_PIN, OUTPUT);
@@ -43,18 +71,13 @@ void setup(){
43 71
   digitalWrite(EN_PIN, HIGH); // disable driver
44 72
   digitalWrite(DIR_PIN, LOW); // chose direction
45 73
   digitalWrite(STEP_PIN, LOW); // no step yet
46
-
47
-  
48 74
   digitalWrite(EN_PIN, LOW); // enable driver
49 75
 
50 76
   // stepper
51 77
   myStepper.init();
52
-  
53
-  Serial.print("REG_IOIN: ");
54
-  uint32_t ioin = 0;
55
-  myStepper.read_REG(TMC_REG_IOIN, &ioin);
56
-  Serial.println(ioin, BIN);
57 78
 
79
+  uint32_t ioin;
80
+  myStepper.read_REG(TMC_REG_IOIN, &ioin); 
58 81
   if (ioin >> 24 != 0x11) {
59 82
       Serial.println("SPI Communication failed or incompatible firmware");
60 83
       while (true);
@@ -120,8 +143,8 @@ void setup(){
120 143
 
121 144
   myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
122 145
 
123
-  
124
-
146
+  Serial.println("Finished Configuration: ");
147
+  print_regdump();
125 148
 
126 149
   // get ready
127 150
   digitalWrite(EN_PIN, LOW); // enable driver
@@ -137,21 +160,31 @@ void loop(){
137 160
   static int sometimes = 0;
138 161
   if (sometimes++ > 100) {
139 162
     sometimes = 0;
163
+    
164
+    while (!Serial);
140 165
 
141 166
     uint32_t drv_status = 0;
142 167
     myStepper.read_REG(TMC_REG_DRV_STATUS, &drv_status);
143 168
     uint32_t stallguard = drv_status & 0x1ff;  
144 169
     uint32_t current = (drv_status >> 16) & 0x1f;
145 170
 
171
+    Serial.print("Stallguard value: ");
172
+    Serial.println(stallguard);
173
+
174
+    Serial.print("Current: ");
175
+    Serial.println(current);
176
+
146 177
     int32_t lost_steps;
147 178
     myStepper.read_REG(TMC_REG_LOST_STEPS, &lost_steps);
148 179
 
149 180
     static int32_t last_lost_steps = 0;
150
-
181
+    
182
+    Serial.print("Lost steps: ");
183
+    Serial.println(lost_steps);
151 184
     
152 185
     const int STEP_THRESHOLD = 5;
153 186
     const int DIRCHANGE_HOLDOFF = 2;
154
-    
187
+    /*
155 188
     static int dirchange_timeout = DIRCHANGE_HOLDOFF;
156 189
 
157 190
     if ((lost_steps >= last_lost_steps + STEP_THRESHOLD) || (lost_steps <= last_lost_steps - STEP_THRESHOLD)  ) {
@@ -172,5 +205,6 @@ void loop(){
172 205
         dirchange_timeout = DIRCHANGE_HOLDOFF;
173 206
       }
174 207
     }
208
+    */
175 209
   }
176 210
 }