-
Notifications
You must be signed in to change notification settings - Fork 1
/
main2.c
724 lines (613 loc) · 16.3 KB
/
main2.c
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
* vim:ts=4:sw=4:expandtab
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUD 9600
#include <util/setbaud.h>
#include <util/delay.h>
#include <util/twi.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <avr/wdt.h>
#include <limits.h>
//#define DEBUG 1
#define BUTTONCOUNT 1
// The thread pitch is 1.75mm per revolution (M12)
// 3200 steps are 1 revolution
// Limit for tilting
#define TILT_LIMIT 80000
// Limit for height
#define HEIGHT_LIMIT 190000
// ADC value when the autofocus switch is pressed.
// Significantly higher values mean broken cable.
#define CLOSED_VALUE 671
// ADC value when the autofocus switch is not pressed.
// Significyntly lower values mean short circuit.
#define OPEN_VALUE 316
// Maximum difference between ADC value and OPEN_VALUE for which the autofocus switch is considered open.
// Values outside are considered proof of pressed switch, so moving up will not work.
#define INTERVAL_SIZE 23
// Lower ADC values will be consideres proof for short circuit.
#define SHORT_CIRCUIT_VALUE 286
// Higher ADC values will be consideres proof for broken cable.
#define CABLE_BROKEN_VALUE 770
// Delay between two steps in micro seconds when starting to move.
#define INITIAL_DELAY 200
// Shortest delay between two steps allowed (i.e. fastest movement).
#define MIN_DELAY 1
// Distance between a hit of the focus switch and the point of focus.
#define FOCUS_DISTANCE 21628
// true if the USART-Receive-Buffer was completely read by the main loop. Set to false when buffer full by usart-receive-interrupt.
volatile bool buffer_read = true;
// Positions of the 3 achsis relative to the endstop. [0] has the "average position"
int32_t positions[4];
// "average position" (see above) of the autofocus switch
int32_t autofocusPosition;
#define setStep1 {PORTC |= (1<<PC1);}
#define setStep2 {PORTC |= (1<<PC3);}
#define setStep3 {PORTC |= (1<<PC5);}
#define unsetStep1 {PORTC &= ~(1<<PC1);}
#define unsetStep2 {PORTC &= ~(1<<PC3);}
#define unsetStep3 {PORTC &= ~(1<<PC5);}
#define MOTOR_1_OFFSET 0
#define MOTOR_2_OFFSET 2754
#define MOTOR_3_OFFSET 2165
#define MOTOR_MAX_OFFSET 4000
/**
* Set the direction of a stepper driver
*
* @param direction the new direction. True means up.
*/
#define setDir1(dir) {\
if (dir != (bool) (PORTC & (1<<PC0))) {\
if (dir) PORTC |= (1<<PC0); \
else PORTC &= ~(1<<PC0);\
_delay_us(0.3);\
}\
}
#define setDir2(dir) {\
if (dir != (bool) (PORTC & (1<<PC2))) {\
if (dir) PORTC |= (1<<PC2); \
else PORTC &= ~(1<<PC2);\
_delay_us(0.3);\
}\
}
#define setDir3(dir) {\
if (dir != (bool) (PORTC & (1<<PC4))) {\
if (dir) PORTC |= (1<<PC4); \
else PORTC &= ~(1<<PC4);\
_delay_us(0.3);\
}\
}
// Test if one axis reached the height limit.
#define heightLimitReached(driver) (positions[driver] > HEIGHT_LIMIT)
// Test of any of the three axis reached the height limit
#define anyHeightLimitReached() (heightLimitReached(1) || heightLimitReached(2) || heightLimitReached(3))
int32_t max(int32_t a, int32_t b) {
if (a>b) {
return a;
}
return b;
}
/**
* Check if a endstop has been reached. True means yes.
*/
#define stop1 ((bool)(PINA & (1<<PA1)))
#define stop2 ((bool)(PINA & (1<<PA2)))
#define stop3 ((bool)(PINA & (1<<PA3)))
#define fault1 ((bool)(PINA & (1<<PA5)))
#define fault2 ((bool)(PINA & (1<<PA6)))
#define fault3 ((bool)(PINA & (1<<PA7)))
#define sleeping (!(bool)(PIND & (1<<PD7)))
#define sleep() {PORTD &= ~(1<<PD7); DDRD |= (1<<PD7);}
#define wakeup() {DDRD &= ~(1<<PD7); PORTD |= (1<<PD7);}
/*!
* Check if any of the endstops has been reached. True means yes.
*/
#define anyStopReached() (stop1 || stop2 || stop3)
#define stepUp1() { \
if (!sleeping) { /* Don't touch! We need to check for externally forced sleeping! */\
positions[1]++;\
setDir1(true);\
setStep1;\
}\
}
#define stepUp2() {\
if (!sleeping) {\
positions[2]++;\
setDir2(true);\
setStep2;\
}\
}
#define stepUp3() {\
if (!sleeping) {\
positions[3]++;\
setDir3(true);\
setStep3;\
}\
}
#define stepDown1() { \
if (!sleeping) {\
positions[1]--;\
setDir1(false);\
setStep1;\
}\
}
#define stepDown2() {\
if (!sleeping) {\
positions[2]--;\
setDir2(false);\
setStep2;\
}\
}
#define stepDown3() {\
if (!sleeping) {\
positions[3]--;\
setDir3(false);\
setStep3;\
}\
}
#define stepAllUp() {positions[0]++; stepUp1(); stepUp2(); stepUp3();}
#define stepAllDown() {positions[0]--; stepDown1(); stepDown2(); stepDown3();}
// Execute one step up for each motor
static void uart_puts(const char *str);
// Timer 0 overflow
ISR(TIMER1_OVF_vect) {
}
// Timer 1 overflow
ISR(TIMER0_OVF_vect) {
}
// React to external enable signal
ISR(INT0_vect) {
}
static void uart_puts(const char *str) {
const char *walk;
for (walk = str; *walk != '\0'; walk++) {
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = *walk;
}
}
static void uart_put(char i) {
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = i;
}
static void uart_puti(int16_t val) {
char buffer[12];
sprintf(buffer, "%i", val);
uart_puts(buffer);
}
static void uart_hex8(uint8_t i) {
char chars[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uart_put(chars[0xf & (i >> 4)]);
uart_put(chars[0xf & i]);
}
static void uart_hex16(uint16_t i) {
char chars[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uart_put(chars[0xf & (i >> 12)]);
uart_put(chars[0xf & (i >> 8)]);
uart_put(chars[0xf & (i >> 4)]);
uart_put(chars[0xf & i]);
}
static void uart_hex32(uint32_t i) {
char chars[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uart_put(chars[0xf & (i >> 28)]);
uart_put(chars[0xf & (i >> 24)]);
uart_put(chars[0xf & (i >> 20)]);
uart_put(chars[0xf & (i >> 16)]);
uart_put(chars[0xf & (i >> 12)]);
uart_put(chars[0xf & (i >> 8)]);
uart_put(chars[0xf & (i >> 4)]);
uart_put(chars[0xf & i]);
}
static void uart_hex64(uint64_t i) {
char chars[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uart_put(chars[0xf & (i >> 60)]);
uart_put(chars[0xf & (i >> 56)]);
uart_put(chars[0xf & (i >> 52)]);
uart_put(chars[0xf & (i >> 48)]);
uart_put(chars[0xf & (i >> 44)]);
uart_put(chars[0xf & (i >> 40)]);
uart_put(chars[0xf & (i >> 36)]);
uart_put(chars[0xf & (i >> 32)]);
uart_put(chars[0xf & (i >> 28)]);
uart_put(chars[0xf & (i >> 24)]);
uart_put(chars[0xf & (i >> 20)]);
uart_put(chars[0xf & (i >> 16)]);
uart_put(chars[0xf & (i >> 12)]);
uart_put(chars[0xf & (i >> 8)]);
uart_put(chars[0xf & (i >> 4)]);
uart_put(chars[0xf & i]);
}
static void uart_bin(uint8_t i) {
uart_puts((i & (1<<7)) ? "1":"0");
uart_puts((i & (1<<6)) ? "1":"0");
uart_puts((i & (1<<5)) ? "1":"0");
uart_puts((i & (1<<4)) ? "1|":"0|");
uart_puts((i & (1<<3)) ? "1":"0");
uart_puts((i & (1<<2)) ? "1":"0");
uart_puts((i & (1<<1)) ? "1":"0");
uart_puts((i & 1) ? "1":"0");
}
/*!
* Initialize the ADC
*/
void adcInit() {
// Set AVCC as reference
ADMUX = (1<<REFS0);
// Set AREF as reference
ADMUX = 0;
// Enable ADC, set free running, enable interrupt, set prescaler to 1/128
//ADCSRA = (1<<ADEN) | (1<<ADSC);
//ADCSRA = (1<<ADEN) | (1<<ADSC) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
ADCSRA = (1<<ADEN) | (1<<ADSC) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
}
/*!
* Start a conversion.
*/
#define adc_start_conversion() {ADCSRA |= (1<<ADSC);} // single conversion
/*!
* Wait for the conversion to finish and return the result.
*/
uint16_t adc_wait() {
while (ADCSRA & (1<<ADSC) ) { // auf Abschluss der Konvertierung warten
}
return ADCW; // ADC auslesen und zurückgeben
}
// Buffer for receiving data via USART
#define buffer_size 16
uint8_t buffer[buffer_size];
volatile uint8_t buffer_index;
// Receive data via USART
ISR(USART0_RX_vect) {
uint8_t byte = UDR0;
if (!buffer_read) {
return;
}
if (byte == '\r' || byte == '\n') {
buffer[buffer_index] = 0;
buffer_index = 0;
buffer_read = false;
}
else {
buffer[buffer_index] = byte;
buffer_index++;
}
}
/*!
* Move down until all endstops are hit.
*/
#define gotoEndstops() {\
if (!stop1) {\
stepDown1();\
}\
else {\
positions[1] = -MOTOR_1_OFFSET;\
}\
if (!stop2) {\
stepDown2();\
}\
else {\
positions[2] = -MOTOR_2_OFFSET;\
}\
if (!stop3) {\
stepDown3();\
}\
else {\
positions[3] = -MOTOR_3_OFFSET;\
}\
}
// True if user pressed button for moving up.
#define buttonUp() (!(bool)(PINB & (1<<1)))
// True if user pressed button for moving down.
#define buttonDown() (!(bool)(PINB & (1<<3)))
// True if user pressed button for tilting front down.
#define buttonTiltFrontDown() (!(bool)(PINB & (1<<4)))
// True if user pressed button for tilting front up.
#define buttonTiltFrontUp() (!(bool)(PINB & (1<<0)))
// True if user pressed button for leveling.
#define buttonLevel() (!(bool)(PINB & (1<<2)))
// True if the user wants to got to the autofocus position
#define buttonFocus() (!(bool)(PIND & (1<<2)))
// True if user wants to do an emergency movement
#define buttonEmergency() (!(bool)(PIND & 1<<3))
// Print the positions of the axis
void printPositions() {
uart_hex32(positions[0]);
uart_puts(" ");
uart_hex32(positions[1]);
uart_puts(" ");
uart_hex32(positions[2]);
uart_puts(" ");
uart_hex32(positions[3]);
}
/*!
* Simple delay function for which the time does not need to be known at compile time.
*/
#define myDelayUs(length) {uint16_t _myDelayUs_tmp = length; while (--_myDelayUs_tmp) {_delay_us(1);}}
void initialize() {
DDRC |= (1<<DDC0); // DIR1
DDRC |= (1<<DDC1); // STEP1
DDRC |= (1<<DDC2); // DIR2
DDRC |= (1<<DDC3); // STEP2
DDRC |= (1<<DDC4); // DIR3
DDRC |= (1<<DDC5); // STEP3
// Joystick
DDRB &= 0;
PORTB |= 0xff;
// Buttons, LEDs and stuff
DDRD &= 0;
PORTD |= 0xff;
// Lower endstops and autofocus-endstop. No pullup for autofocus endstop (PA0)
DDRA &= 0;
PORTA |= 0b11111110;
// Set ADC used for switch as input and disable pullup
DDRA &= ~(1<<PA0);
PORTA &= ~(1<<PA0);
}
int main() {
initialize();
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0B = (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
// enable interrupts for the timer
sei();
/* Timer aufsetzen: nach 1 ms soll der Interrupt ausgelöst werden. */
/* 8 bit counter (TIMER0) */
/* normal mode */
TCCR0A = 0;
/* CLK/64 */
TCCR0B = (1 << CS01) | (1 << CS00);
/* timer ticks: 250 */
TCNT0 = 5;
TIMSK0 = (1 << TOIE0);
TIFR0 = (1 << TOV0);
adcInit();
int16_t ii = 0;
int16_t jj = 0;
wdt_enable(WDTO_2S);
/*
for(;;i++) {
uart_bin(PINB);
uart_puts(" ");
uart_bin(PIND);
uart_puts(" ");
uart_hex16(i);
uart_puts("\r\n");
_delay_ms(1000);
wdt_reset();
}
*/
uart_puts("Starting up\r\n");
for (ii = 0; ii < 4; ii++) {
positions[ii] = 0;
}
// Count the number of cycles the autofocus is stuck in mid-state
uint8_t mid_state_counter = 0;
// Count the number of successive movements in the same direction before increasing speed
uint16_t movement_counter = 0;
uint16_t current_delay = INITIAL_DELAY;
enum movement{NONE, UP, DOWN, TILT_DOWN, TILT_UP, LEVEL, EMERGENCY} last_movement, movement;
last_movement = NONE;
movement = NONE;
// Reset autofocus position
/*
while(1) {
adc_start_conversion(7);
const uint16_t autofocus_result = adc_wait();
uart_hex16(autofocus_result);
wdt_reset();
uart_puts("\r\n");
}
// */
uint8_t autofocus_hit_counter = 0;
movement_counter = 0;
current_delay = INITIAL_DELAY;
initialize();
adcInit();
adc_start_conversion();
uint16_t autofocus_result = adc_wait();
uint16_t no_movement_counter = 0;
autofocusPosition = 2*HEIGHT_LIMIT;
for(ii = 0; ; ii++){
wdt_reset();
//*
if (current_delay > 1) {
for (jj = 0; jj < current_delay; jj++) {
_delay_us(1);
}
}
// */
#ifdef DEBUG
uart_hex16(current_delay);
uart_puts("\r\n");
#endif
// True if autofocus endstop not yet hit.
const bool autofocus_switch_clear = (autofocus_result <= OPEN_VALUE + INTERVAL_SIZE && autofocus_result >= OPEN_VALUE - INTERVAL_SIZE);
bool autofocus_clear = true;
if (!autofocus_switch_clear) {
if (autofocus_hit_counter > 10) {
autofocus_clear = false;
if ((ii % 1024) == 0) {
uart_puts("Autofocus hit at: ");
printPositions();
autofocusPosition = positions[0];
uart_puts("\r\n");
}
if (autofocus_result < SHORT_CIRCUIT_VALUE) {
uart_puts("Autofocus has short circuit, value is: ");
uart_hex16(autofocus_result);
uart_puts("\r\n");
}
else if (autofocus_result > CABLE_BROKEN_VALUE) {
uart_puts("Autofocus cable is broken, value is: ");
uart_hex16(autofocus_result);
uart_puts("\r\n");
}
else if(autofocus_result > OPEN_VALUE + INTERVAL_SIZE && autofocus_result < CLOSED_VALUE - INTERVAL_SIZE) {
if (mid_state_counter > 10) {
uart_puts("Autofocus stuck in mid-state, value is: 0x");
uart_hex16(autofocus_result);
uart_puts("\r\n");
}
else {
mid_state_counter++;
}
}
}
// Increment autofocus_hit_counter in order to detect series of hit autofocus.
if (autofocus_hit_counter < 20) {
autofocus_hit_counter++;
}
}
else {
mid_state_counter = 0;
autofocus_hit_counter = 0;
}
movement = NONE;
if (!buttonEmergency()) {
if (buttonDown()) {
if (!anyStopReached()) {
stepAllDown();
movement = DOWN;
}
_delay_us(2.08);
}
else if (buttonUp()) {
if (!anyHeightLimitReached() && autofocus_clear) {
stepAllUp();
movement = UP;
}
}
else if (buttonTiltFrontUp()) {
if (!anyStopReached() && !anyHeightLimitReached() && (positions[3]-positions[1] < TILT_LIMIT) && autofocus_clear) {
stepUp3();
stepDown1();
movement = TILT_UP;
}
_delay_us(0.96);
}
else if (buttonTiltFrontDown()) {
if (!anyStopReached() && !anyHeightLimitReached() && (positions[1]-positions[3] < TILT_LIMIT) && autofocus_clear) {
stepDown3();
stepUp1();
movement = TILT_DOWN;
}
_delay_us(1.52);
}
else if (buttonLevel()) {
if (!anyStopReached() && autofocus_clear) {
if (positions[1] > positions[3]+1) {
stepDown1();
stepUp3();
movement = LEVEL;
_delay_us(0.72);
}
else if (positions[1]+1 < positions[3]) {
stepDown3();
stepUp1();
movement = LEVEL;
}
}
_delay_us(2.22);
}
else if (buttonFocus()) {
if ((positions[0] > (autofocusPosition - FOCUS_DISTANCE)) && !anyStopReached()) {
stepAllDown();
movement = DOWN;
}
else if ((positions[0] < (int32_t)(autofocusPosition - FOCUS_DISTANCE)) && autofocus_clear) {
stepAllUp();
movement = UP;
}
// Needed to fulfill the 2us hold constraint of the step pin
}
}
if (movement == last_movement && movement != NONE) {
if (movement_counter > 500) {
current_delay--;
if (current_delay < MIN_DELAY) {
current_delay = MIN_DELAY;
}
movement_counter = 0;
}
movement_counter += current_delay;
}
else {
current_delay = INITIAL_DELAY;
movement_counter = 0;
}
// Do all the emergency stuff
if (buttonEmergency()) {
_delay_us(17);
movement = EMERGENCY;
if (buttonEmergency() && buttonFocus()) {
gotoEndstops();
}
if (buttonUp()) {
if (buttonTiltFrontDown()) {
stepUp1();
}
if (buttonLevel()) {
stepUp2();
}
if (buttonTiltFrontUp()) {
stepUp3();
}
}
if (buttonDown()) {
if (buttonTiltFrontDown()) {
stepDown1();
}
if (buttonLevel()) {
stepDown2();
}
if (buttonTiltFrontUp()) {
stepDown3();
}
}
if (buttonLevel()) {
int32_t maxPosition = max(positions[1], max(positions[2], positions[3]));
if (positions[1] < maxPosition) {
stepUp1();
}
if (positions[2] < maxPosition) {
stepUp2();
}
if (positions[3] < maxPosition) {
stepUp3();
}
}
// In case of emergency, introduce a safe delay for the step hold time
_delay_us(2);
}
last_movement = movement;
if (movement == NONE) {
no_movement_counter++;
if (no_movement_counter > 2e3) {
sleep();
no_movement_counter = 0;
printPositions();
uart_puts("\r\n");
}
}
else {
no_movement_counter = 0;
if (sleeping) {
wakeup();
_delay_ms(100);
}
}
if(!(ADCSRA & (1<<ADSC))) { // auf Abschluss der Konvertierung warten
autofocus_result = ADCW;
adc_start_conversion();
}
_delay_us(1.08);
unsetStep1;
unsetStep2;
unsetStep3;
}
}