-
Notifications
You must be signed in to change notification settings - Fork 4
/
zAxis.ino
142 lines (121 loc) · 3.19 KB
/
zAxis.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <TMC2130Stepper.h>
#include "helpers.h"
// Stepper 1 = z-axis = Motor 1 on shield
#define enPinZ 2
#define stepPinZ 3
#define dirPinZ 4
#define CS_PIN 48
#define MOSI_PIN 51 //SDI/MOSI (ICSP: 4, Uno: 11, Mega: 51)
#define MISO_PIN 50 //SDO/MISO (ICSP: 1, Uno: 12, Mega: 50)
#define SCK_PIN 52 //CLK/SCK (ICSP: 3, Uno: 13, Mega: 52)
TMC2130Stepper TMC = TMC2130Stepper(enPinZ, dirPinZ, stepPinZ, CS_PIN);
long pauseMS = 60; // pause between steps
void setupZAxis() {
// pinMode(enPinZ, OUTPUT);
// pinMode(stepPinZ, OUTPUT);
// pinMode(dirPinZ, OUTPUT);
// digitalWrite(enPinZ, LOW);
TMC.begin();
// TMC.hysteresis_start(4);
// TMC.hysteresis_end(-2);
TMC.SilentStepStick2130(500);
TMC.microsteps(16);
TMC.interpolate(1);
// TMC.stealthChop(1); // Silent stepping, not compatible with stall guard (?)
TMC.diag0_stall(1);
TMC.diag1_stall(1);
TMC.diag1_active_high(1);
TMC.coolstep_min_speed(25000);
// TMC.THIGH(0);
// TMC.semin(5);
// TMC.semax(2);
// TMC.sedn(0b01);
// TMC.sg_min(0); // Needed to parametrize
TMC.sg_stall_value(14);
// TMC.sg_filter(0);
Serial.println(TMC.stealthChop());
Serial.println(TMC.shaft_dir());
Serial.println(TMC.microsteps());
Serial.println(TMC.getCurrent());
// digitalWrite(dirPinZ, HIGH);
digitalWrite(enPinZ, HIGH); // Disable stepper
}
void handleZ(args arg) {
switch (arg.code) {
case '+':
zStep(HIGH, arg.value);
break;
case '-':
zStep(LOW, arg.value);
break;
case 'v':
TMC.sg_stall_value(arg.value);
break;
case 'p':
pauseMS = arg.value;
break;
case 'f':
TMC.sg_filter( arg.value );
break;
case 's':
TMC.microsteps(arg.value);
break;
case 'e':
enableZMotor();
break;
case 'd':
disableZMotor();
break;
case '?':
default:
Serial.println("- =up, + =down, v =stall_value, p =pauseMS, f =sg_filter, v = stall_value, s = microsteps, e = enable, d = disable");
Serial.print("stall_value= ");
Serial.print(TMC.sg_stall_value());
Serial.print(", pauseMS= ");
Serial.print(pauseMS);
Serial.print( ", filter= ");
Serial.print(TMC.sg_filter());
Serial.print(", microsteps= ");
Serial.print(TMC.microsteps());
Serial.println();
break;
}
}
void enableZMotor() {
digitalWrite(enPinZ, LOW);
delayMicroseconds(20);
Serial.println(TMC.isEnabled());
}
void disableZMotor() {
digitalWrite(enPinZ, HIGH);
}
void zStep(char dir, long steps) {
int code ;
// enableZMotor();
TMC.shaft_dir(dir);
for (long i = 0; i < steps ; i++) { // 800Steps = 1mm
for (int j = 0; j < 1 ; j++) {
delayMicroseconds(pauseMS);
digitalWrite(stepPinZ, HIGH);
delayMicroseconds(30);
digitalWrite(stepPinZ, LOW);
code = TMC.sg_result();
Serial.println(code);
if (code == 0) {
Serial.print("!!! stalled +++ j=");
Serial.println(j);
j = 200;
i = steps;
break;
}
}
Serial.println(TMC.sg_result());
if (TMC.sg_result() == 0) {
Serial.print("+++ stalled +++ i=");
Serial.println(i);
i = steps;
break;
}
}
// disableZMotor();
}