-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroscopeController.ino
More file actions
571 lines (522 loc) · 17.2 KB
/
MicroscopeController.ino
File metadata and controls
571 lines (522 loc) · 17.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
* an arduino sketch to interface with a ps/2 mouse.
* Also uses serial protocol to talk back to the host
* and report what it finds.
*/
// Control is based on three states:
// RUN is the main operational state. The microscope is used normally
// XY_SPEED_SET state allows for the fast movement of the XY mechanical stage.
// It is meant to allow the user to position the slide quickly. The
// rotary encoder sets the speed X and Y movement.
// FOCUS_SPEED_SET state allows for the fast movement of the fine focus.
// It is meant to allow the user to focus more quickly. The
// rotary encoder sets the focus speed.
enum SWITCH_STATE {RUN = 1, HOME_FAST = 2, XY_SPEED_SET = 3, FOCUS_SPEED_SET = 4};
byte switchState = RUN; // Starting state
enum RUN_STATE {SLOW = 1, XY_FAST = 2, FOCUS_FAST = 3};
byte runState = SLOW;
// RGB LEDs are common-anode; 255 is off, 0 is full on.
// ledValue[] stores the 8-bit analogue value for the LEDs.
enum LEDS_ADDR {RED = 0, GREEN = 1, BLUE = 2};
byte ledValue[3] = {255, 255, 255}; // all off default
// Mode button and LEDs
#define PIN_MODE_BUTTON 35
#define PIN_MODE_LED0 36
#define PIN_MODE_LED1 37
// Extra button
#define PIN_EXTRA_BUTTON 4
// Home sensors
#define HOMED 0
// digital pin 2 has a pushbutton attached to it. Give it a name:
int hallSensorX = 28;
int hallSensorY = 29;
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define PIN_MOTOR1_IN1 51 // IN1 on the ULN2003 driver 1
#define PIN_MOTOR1_IN2 50 // IN2 on the ULN2003 driver 1
#define PIN_MOTOR1_IN3 52 // IN3 on the ULN2003 driver 1
#define PIN_MOTOR1_IN4 53 // IN4 on the ULN2003 driver 1
#define PIN_MOTOR2_IN1 47 // IN1 on the ULN2003 driver 2
#define PIN_MOTOR2_IN2 46 // IN2 on the ULN2003 driver 2
#define PIN_MOTOR2_IN3 48 // IN3 on the ULN2003 driver 2
#define PIN_MOTOR2_IN4 49 // IN4 on the ULN2003 driver 2
#define PIN_MOTOR3_IN1 43 // IN1 on the ULN2003 driver 3
#define PIN_MOTOR3_IN2 42 // IN2 on the ULN2003 driver 3
#define PIN_MOTOR3_IN3 44 // IN3 on the ULN2003 driver 3
#define PIN_MOTOR3_IN4 45 // IN4 on the ULN2003 driver 3
#define STEPPER_MAX_SPEED 3000
#define STEPPER_FAST STEPPER_MAX_SPEED
#define STEPPER_SLOW (STEPPER_MAX_SPEED/2)
int speedMultiplier = 1;
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepperX(HALFSTEP, PIN_MOTOR1_IN1, PIN_MOTOR1_IN3, PIN_MOTOR1_IN2, PIN_MOTOR1_IN4);
AccelStepper stepperY(HALFSTEP, PIN_MOTOR2_IN1, PIN_MOTOR2_IN3, PIN_MOTOR2_IN2, PIN_MOTOR2_IN4);
AccelStepper stepperF(HALFSTEP, PIN_MOTOR3_IN1, PIN_MOTOR3_IN3, PIN_MOTOR3_IN2, PIN_MOTOR3_IN4);
long homing_step;
volatile long xpos = 0;
volatile long ypos = 0;
volatile long fpos = 0;
void setup()
{
Serial.begin(115200);
// *********************************************
// Mouse and steppers initialization
// *********************************************
mouse_init();
stepperX.setMaxSpeed(STEPPER_MAX_SPEED);
stepperX.setAcceleration(300.0);
stepperX.setSpeed(STEPPER_FAST);
stepperX.disableOutputs();
stepperX.setCurrentPosition(xpos);
stepperY.setMaxSpeed(STEPPER_MAX_SPEED);
stepperY.setAcceleration(300.0);
stepperY.setSpeed(STEPPER_FAST);
stepperY.disableOutputs();
stepperY.setCurrentPosition(ypos);
stepperF.setMaxSpeed(STEPPER_MAX_SPEED);
stepperF.setAcceleration(300.0);
stepperF.setSpeed(STEPPER_FAST);
stepperF.disableOutputs();
stepperF.setCurrentPosition(fpos);
encoder_init();
// Set up the Mode button and LED pins
pinMode(PIN_MODE_BUTTON, INPUT);
digitalWrite(PIN_MODE_BUTTON, LOW); // Disable internal pull-up
pinMode(PIN_MODE_LED0, OUTPUT); // upper LED
digitalWrite(PIN_MODE_LED0, LOW); // set off
pinMode(PIN_MODE_LED1, OUTPUT); // upper LED
digitalWrite(PIN_MODE_LED1, LOW); // set off
// Set up extra button
pinMode(PIN_EXTRA_BUTTON, INPUT_PULLUP); // Enable internal pull-up
// set the encoder RGB to RUN state
encoderRGB(0,0,0);
delay(200);
Serial.print("initial encoderPosXYspeed : ");
Serial.println(getEncoderPosXYspeed());
Serial.print("initial encoderPosFOCUSspeed : ");
Serial.println(getEncoderPosFOCUSspeed());
// bounce the LED ring
ledPartialRingBounce();
// flash the LEDs
delay(100);
digitalWrite(PIN_MODE_LED0, HIGH); // set off
delay(100);
digitalWrite(PIN_MODE_LED1, HIGH); // set off
delay(100);
digitalWrite(PIN_MODE_LED0, LOW); // set off
delay(100);
digitalWrite(PIN_MODE_LED1, LOW); // set off
delay(100);
digitalWrite(PIN_MODE_LED0, HIGH); // set off
delay(100);
digitalWrite(PIN_MODE_LED1, HIGH); // set off
delay(100);
digitalWrite(PIN_MODE_LED0, LOW); // set off
delay(100);
digitalWrite(PIN_MODE_LED1, LOW); // set off
delay(100);
xyHome();
}
/*
* get a reading from the mouse and report it back to the
* host via the serial line.
*/
void NOTloop() {
Serial.print("loop: ");
Serial.println(xpos++);
delay(1000);
}
void loop()
{
int extraButton = digitalRead(PIN_EXTRA_BUTTON);
if (extraButton == LOW)
{
ledPartialRingBounce();
xyCentre();
}
// If the switch is pressed, switch state
if (encoderSwitchPressed()) {
delay(10);
if (encoderSwitchPressed()) {
Serial.println("HIGH");
switch (switchState) {
case RUN:
stepperX.stop();
stepperX.disableOutputs();
stepperY.stop();
stepperY.disableOutputs();
stepperF.stop();
stepperF.disableOutputs();
switchState = HOME_FAST;
Serial.println("State is now HOME_FAST");
delay(100);
encoderRGB(255,255,255);
break;
case HOME_FAST:
switchState = XY_SPEED_SET;
enableEncoder();
Serial.println("State is now XY_SPEED_SET");
delay(100);
encoderRGB(255,0,255);
setEncoderPosition(getEncoderPosXYspeed());
break;
case XY_SPEED_SET:
switchState = FOCUS_SPEED_SET;
Serial.println("State is now FOCUS_SPEED_SET");
// interrupts already enabled by change from RUN to XY_SPEED_SET state
// No need to enable here
saveXY_SPEED_SETSetting(getEncoderPosXYspeed());
delay(100);
encoderRGB(255,255,0);
setEncoderPosition(getEncoderPosFOCUSspeed());
break;
case FOCUS_SPEED_SET:
switchState = RUN;
Serial.println("State is now RUN");
saveFOCUS_SPEED_SETSetting(getEncoderPosFOCUSspeed());
delay(100);
// However, because something gets masked or something else, have to
// resort to a kludge to ensure the motors will still run...
swReset();
break;
default:
break;
}
}
while(encoderSwitchPressed())
; // do nothing
// Update the partial circle bar graph
ledPartialRingFiller();
}
if (switchState == RUN) {
// If the Home/Mode button is pressed, switch state
if (digitalRead(PIN_MODE_BUTTON) == LOW) {
delay(10);
if (digitalRead(PIN_MODE_BUTTON) == LOW) {
// TODO actually change the speed!
Serial.println("HOME/MODE Pressed");
switch (runState) {
case SLOW:
runState = XY_FAST;
digitalWrite(PIN_MODE_LED0, HIGH);
digitalWrite(PIN_MODE_LED1, LOW);
Serial.println("New HOME mode is XY_FAST");
speedMultiplier = 50;
break;
case XY_FAST:
runState = FOCUS_FAST;
digitalWrite(PIN_MODE_LED0, LOW);
digitalWrite(PIN_MODE_LED1, HIGH);
Serial.println("New HOME mode is FOCUS_FAST");
break;
case FOCUS_FAST:
runState = SLOW;
digitalWrite(PIN_MODE_LED0, LOW);
digitalWrite(PIN_MODE_LED1, LOW);
Serial.println("New HOME mode is SLOW");
speedMultiplier = 1;
break;
default:
break;
}
}
while(digitalRead(PIN_MODE_BUTTON) == LOW)
; // do nothing
}
// Mouse handling
char mstat;
int mx;
int my;
/* get a position and button reading from the mouse */
mouse_write(0xeb); /* give me data! */
mouse_read(); /* ignore ack */
mstat = mouse_read();
mx = mouse_read()*speedMultiplier;
my = mouse_read()*speedMultiplier;
/* send the data back up */
if (abs(mx) > 0 || abs (my) > 0 || abs(stepperX.distanceToGo()) > 0 || abs(stepperY.distanceToGo()) > 0 || (mstat & 0x03) != 0)
{
Serial.print(mstat, HEX);
Serial.print("\tX=");
Serial.print(mx, DEC);
Serial.print("\tY=");
Serial.print(my, DEC);
Serial.print("\txpos=");
Serial.print(xpos, DEC);
Serial.print("\txRem=");
Serial.print(stepperX.distanceToGo(), DEC);
Serial.print("\typos=");
Serial.print(ypos, DEC);
Serial.print("\tyRem=");
Serial.print(stepperY.distanceToGo(), DEC);
Serial.println("");
}
if (stepperX.distanceToGo() != 0)
stepperX.enableOutputs();
else
stepperX.disableOutputs();
xpos += mx;
if (xpos <= 0) xpos = 0;
if (xpos > 46600) xpos = 46600;
if (xpos != stepperX.currentPosition())
{
stepperX.moveTo(xpos);
if (abs(stepperX.distanceToGo()) > 1000)
while (abs(stepperX.distanceToGo()) > 1000) stepperX.run();
else if (abs(stepperX.distanceToGo()) > 100)
while (abs(stepperX.distanceToGo()) > 100) stepperX.run();
else if (abs(stepperX.distanceToGo()) > 50)
while (abs(stepperX.distanceToGo()) > 50) stepperX.run();
else
stepperX.run();
}
if (stepperY.distanceToGo() != 0)
stepperY.enableOutputs();
else
stepperY.disableOutputs();
ypos -= my;
if (ypos >= 0) ypos = 0;
if (ypos < -12600) ypos = -12600;
if (ypos != stepperY.currentPosition())
{
stepperY.moveTo(ypos);
if (abs(stepperY.distanceToGo()) > 1000)
while (abs(stepperY.distanceToGo()) > 1000) stepperY.run();
else if (abs(stepperY.distanceToGo()) > 100)
while (abs(stepperY.distanceToGo()) > 100) stepperY.run();
else if (abs(stepperY.distanceToGo()) > 50)
while (abs(stepperY.distanceToGo()) > 50) stepperY.run();
else
stepperY.run();
}
if (stepperF.distanceToGo() != 0) {
// Serial.print("F enabled ");
// Serial.println(stepperF.distanceToGo());
stepperF.enableOutputs();
}
else {
// Serial.println("F disabled");
stepperF.disableOutputs();
}
if (fpos != stepperF.currentPosition())
{
stepperF.moveTo(fpos);
if (abs(stepperF.distanceToGo()) > 100)
while (abs(stepperF.distanceToGo()) > 100) stepperF.run();
else if (abs(stepperF.distanceToGo()) > 10)
while (abs(stepperF.distanceToGo()) > 10) stepperF.run();
else if (abs(stepperF.distanceToGo()) > 5)
while (abs(stepperF.distanceToGo()) > 5) stepperF.run();
else
stepperF.run();
}
if ((mstat & 0x03) != 0) {
if ((mstat & 0x01) != 0)
fpos += 10;
if ((mstat & 0x02) != 0)
fpos -= 10;
}
}
// else if (switchState == HOME_FAST) {
//
// } else
// ledPartialRingFiller();
}
void swReset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}
#define XCENTER_POS 18175
#define YCENTER_POS -2950
void xyCentre()
{
// The center positions are empirical
stepperX.enableOutputs();
long pos = xpos; // stepperX.currentPosition();
long s = (XCENTER_POS - pos) / 5;
Serial.print("x pos = ");
Serial.print(pos);
Serial.print(" step = ");
Serial.println(s);
long x, y;
for (x = pos; x <= XCENTER_POS; x += s)
{
Serial.print("X move to ");
Serial.println(x);
stepperX.moveTo(x);
while (stepperX.distanceToGo() > 0) stepperX.run();
delay(500);
}
stepperX.disableOutputs();
xpos = XCENTER_POS;
delay(500);
stepperY.enableOutputs();
pos = ypos; // stepperY.currentPosition();
s = (YCENTER_POS - pos) / 5;
Serial.print("y pos = ");
Serial.print(pos);
Serial.print(" step = ");
Serial.println(s);
for (y = pos; y > YCENTER_POS; y += s)
{
Serial.print("Y move to ");
Serial.println(y);
stepperY.moveTo(y);
while (stepperY.distanceToGo() < 0) stepperY.run();
delay(500);
}
stepperY.disableOutputs();
ypos = YCENTER_POS;
}
void xyHome()
{
//-----------------------------------------------------
// X stage homing
//-----------------------------------------------------
// if already near home, move well base home and then
// find the threshold position for sensor trigger. Then
// move to true home
int pos_sensor_homed = digitalRead(hallSensorX);
homing_step = 1000;
if (pos_sensor_homed == HOMED) {
Serial.println("X -- Initially close to home");
xpos = 0;
stepperX.enableOutputs();
stepperX.setCurrentPosition(xpos);
// move well past home
xpos = 4500;
stepperX.moveTo(xpos);
while (stepperX.distanceToGo() > 0) stepperX.run();
stepperX.disableOutputs();
delay(50);
pos_sensor_homed = digitalRead(hallSensorX);
} // if initially near home
else {
// not already near home and no idea how far away.
// but, we can certainly move 4000 steps
Serial.println("X -- Initially away from home");
xpos = 0;
stepperX.enableOutputs();
stepperX.setCurrentPosition(xpos);
while (pos_sensor_homed != HOMED) {
Serial.println("move -4000");
xpos -= 4000;
stepperX.moveTo(xpos);
while (stepperX.distanceToGo() < 0) stepperX.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorX);
}
}
// get closer to home
stepperX.setSpeed(STEPPER_SLOW);
while (abs(homing_step) > 15) {
while (pos_sensor_homed == HOMED) {
Serial.print("move past another ");
Serial.println(homing_step);
xpos += homing_step;
stepperX.moveTo(xpos);
while (stepperX.distanceToGo() > 0) stepperX.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorX);
}
homing_step = -homing_step / 2;
while (pos_sensor_homed != HOMED) {
Serial.print("move toward another ");
Serial.println(homing_step);
xpos += homing_step;
stepperX.moveTo(xpos);
while (stepperX.distanceToGo() < 0) stepperX.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorX);
}
homing_step = -homing_step / 2;
}
Serial.print("xpos = ");
Serial.println(xpos);
xpos -= 3250;
stepperX.moveTo(xpos);
while (stepperX.distanceToGo() < 0) stepperX.run();
Serial.print("xpos = ");
Serial.println(xpos);
Serial.println("setting xpos to zero");
stepperX.setCurrentPosition(0);
xpos = 0;
stepperX.disableOutputs();
//-----------------------------------------------------
// Y stage homing
//-----------------------------------------------------
// if already near home, move well base home and then
// find the threshold position for sensor trigger. Then
// move to true home
// 1230 to 1240 step from home threshold to home
pos_sensor_homed = digitalRead(hallSensorY);
homing_step = -900;
if (pos_sensor_homed == HOMED) {
Serial.println("Y -- Initially close to home");
ypos = 0;
stepperY.enableOutputs();
stepperY.setCurrentPosition(ypos);
// move well past home
ypos = -1500;
stepperY.moveTo(ypos);
while (stepperY.distanceToGo() < 0) stepperY.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorY);
if (pos_sensor_homed == HOMED)
Serial.println("Y -- HOMED");
else
Serial.println("Y -- not HOMED");
} // if initially near home
else {
// not already near home and no idea how far away.
// but, we can certainly move 4000 steps
Serial.println("Y -- Initially away from home");
ypos = 0;
stepperY.enableOutputs();
stepperY.setCurrentPosition(ypos);
while (pos_sensor_homed != HOMED) {
Serial.println("move +1100");
ypos += 1100;
stepperY.moveTo(ypos);
while (stepperY.distanceToGo() > 0) stepperY.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorY);
}
}
// get closer to home
stepperY.setSpeed(STEPPER_SLOW);
while (abs(homing_step) > 15) {
while (pos_sensor_homed == HOMED) {
Serial.print("move past another ");
Serial.println(homing_step);
ypos += homing_step;
stepperY.moveTo(ypos);
while (stepperY.distanceToGo() < 0) stepperY.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorY);
}
homing_step = -homing_step / 2;
while (pos_sensor_homed != HOMED) {
Serial.print("move toward another ");
Serial.println(homing_step);
ypos += homing_step;
stepperY.moveTo(ypos);
while (stepperY.distanceToGo() > 0) stepperY.run();
delay(50);
pos_sensor_homed = digitalRead(hallSensorY);
}
homing_step = -homing_step / 2;
}
Serial.print("ypos = ");
Serial.println(ypos);
ypos += 1200;
stepperY.moveTo(ypos);
while (stepperY.distanceToGo() > 0) stepperY.run();
Serial.print("ypos = ");
Serial.println(ypos);
Serial.println("setting ypos to zero");
stepperY.setCurrentPosition(0);
ypos = 0;
stepperY.disableOutputs();
}