-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStage.cpp
More file actions
270 lines (234 loc) · 5.82 KB
/
Stage.cpp
File metadata and controls
270 lines (234 loc) · 5.82 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Stage.cpp - Library for controlling the stage on the OpenLabTools microscope
Written by James Ritchie for OpenLabTools
github.com/OpenLabTools/Microscope
*/
#include "Arduino.h"
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "Stage.h"
#include "TouchScreen.h"
// Create the motor shield objects
Adafruit_MotorShield lower_afms = Adafruit_MotorShield(0x60);
Adafruit_MotorShield upper_afms = Adafruit_MotorShield(0x61);
// Create stepper motors for each axis (200 steps per rev)
Adafruit_StepperMotor *xy_a_motor = lower_afms.getStepper(200,1);
Adafruit_StepperMotor *xy_b_motor = lower_afms.getStepper(200,2);
Adafruit_StepperMotor *z_1_motor = upper_afms.getStepper(200, 1);
Adafruit_StepperMotor *z_2_motor = upper_afms.getStepper(200, 2);
//Create the touchscreen object
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Stage::Stage() {
//Initialize AccelStepper object with wrapper functions and parameters.
calibrated = false;
}
void Stage::begin()
{
//Initiliaze the motor shield
lower_afms.begin();
upper_afms.begin();
//Setup pins for input
pinMode(Z_ULIMIT_SWITCH, INPUT_PULLUP);
pinMode(Z_LLIMIT_SWITCH, INPUT_PULLUP);
_x_pos = 0;
_y_pos = 0;
_z_pos = 0;
_xy_interval = 15;
_z_interval = 15;
}
void Stage::loop()
{
//Check for manual commands
manualControl();
//Test limit switches to prevent driving stage past limits
if(!digitalRead(Z_ULIMIT_SWITCH) && (getDistanceToGo(Z_STEPPER) > 0)){
Move(Z_STEPPER,0);
}
if(!digitalRead(Z_LLIMIT_SWITCH) && (getDistanceToGo(Z_STEPPER) < 0)){
Move(Z_STEPPER,0);;
}
//Called on every loop to enable non-blocking control of steppers
if((millis()-_z_last_step)>_z_interval){
if((_z_target-_z_pos)>0){
z_1_motor->onestep(FORWARD, DOUBLE);
z_2_motor->onestep(FORWARD, DOUBLE);
_z_last_step = millis();
_z_pos++;
}
else if((_z_target-_z_pos)<0){
z_1_motor->onestep(BACKWARD, DOUBLE);
z_2_motor->onestep(BACKWARD, DOUBLE);
_z_last_step = millis();
_z_pos--;
}
}
if((millis()-_xy_last_step)>_xy_interval){
if((_x_target-_x_pos)>0 && (_y_target-_y_pos)>0){
//Move up and right
xy_a_motor->onestep(FORWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos++;
_y_pos++;
}
else if((_x_target-_x_pos)>0 && (_y_target-_y_pos)<0){
//Move down and right
xy_b_motor->onestep(FORWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos++;
_y_pos--;
}
else if((_x_target-_x_pos)<0 && (_y_target-_y_pos)>0){
//Move up and left
xy_b_motor->onestep(BACKWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos--;
_y_pos++;
}
else if((_x_target-_x_pos)<0 && (_y_target-_y_pos)<0){
//Move down and left
xy_a_motor->onestep(BACKWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos--;
_y_pos--;
}
else if((_x_target-_x_pos)==0 && (_y_target-_y_pos)>0){
//Move up
xy_a_motor->onestep(FORWARD, INTERLEAVE);
xy_b_motor->onestep(BACKWARD, INTERLEAVE);
_xy_last_step = millis();
_y_pos++;
}
else if((_x_target-_x_pos)==0 && (_y_target-_y_pos)<0){
//Move down
xy_a_motor->onestep(BACKWARD, INTERLEAVE);
xy_b_motor->onestep(FORWARD, INTERLEAVE);
_xy_last_step = millis();
_y_pos--;
}
else if((_x_target-_x_pos)>0 && (_y_target-_y_pos)==0){
//Move right
xy_a_motor->onestep(FORWARD, INTERLEAVE);
xy_b_motor->onestep(FORWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos++;
}
else if((_x_target-_x_pos)<0 && (_y_target-_y_pos)==0){
//Move left
xy_a_motor->onestep(BACKWARD, INTERLEAVE);
xy_b_motor->onestep(BACKWARD, INTERLEAVE);
_xy_last_step = millis();
_x_pos--;
}
}
}
void Stage::manualControl()
{
//Get pressure point from TS.
p = ts.getPoint();
if(p.z > 10){
//If pressed, move stage depending on sector
if(p.y<400 && p.x>600){
//Move forwards
_y_target = _y_pos - 1;
}
if(p.y>700 && p.x>600){
//Move backwards
_y_target = _y_pos + 1;
}
if(p.x<700 &&p.x>380&&p.y>450&&p.y<650){
//Move right
_x_target = _x_pos + 1;
}
if(p.x>750&&p.y>450&&p.y<650){
//Move left
_x_target = _x_pos - 1;
}
if(p.x<310 && p.y<500){
//Move up
_z_target = _z_pos + 1;
}
if(p.x<310 && p.y>500){
//Move down
_z_target = _z_pos - 1;
}
}
}
void Stage::calibrate()
{
}
long Stage::getPosition(int stepper)
{
if(calibrated)
{
switch(stepper) {
case X_STEPPER:
return _x_pos;
break;
case Y_STEPPER:
return _y_pos;
break;
case Z_STEPPER:
return _z_pos;
break;
}
}
}
long Stage::getDistanceToGo(int stepper)
{
switch(stepper) {
case X_STEPPER:
return _x_target - _x_pos;
break;
case Y_STEPPER:
return _y_target - _y_pos;
break;
case Z_STEPPER:
return _z_target - _z_pos;
break;
}
}
long Stage::getLength(int stepper)
{
switch(stepper) {
case X_STEPPER:
return _x_length;
break;
case Y_STEPPER:
return _y_length;
break;
case Z_STEPPER:
return _z_length;
break;
}
}
void Stage::Move(int stepper, long steps)
{
switch(stepper) {
case X_STEPPER:
_x_target = _x_pos + steps;
break;
case Y_STEPPER:
_y_target = _y_pos + steps;
break;
case Z_STEPPER:
_z_target = _z_pos + steps;
break;
}
}
void Stage::MoveTo(int stepper, long position)
{
//Move the z stepper to a position
if(calibrated){
switch(stepper) {
case X_STEPPER:
_x_target = position;
break;
case Y_STEPPER:
_y_target = position;
break;
case Z_STEPPER:
_z_target = position;
break;
}
}
}