-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_serial.ino
101 lines (80 loc) · 2.04 KB
/
arduino_serial.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
#include <EEPROM.h>
#include <XYSteppers.h>
#define BAUD_R 9600
#define POINT_ARR_MAX 32
#define UI_SIZE 2
#define MAX_TIME 120000
#define BREAK_TIME 30000
typedef struct point {
unsigned int x;
unsigned int y;
} point;
enum readiness {READY, NOT_READY};
point point_arr[POINT_ARR_MAX];
int rec = 0;
int prev_state = NOT_READY;
long lastStart = 0;
bool firstPoint = true;
#include <XYSteppers.h>
XYSteppers steppers(800, 4, 5, 6, 7, 8, 9, 10, 11);
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_R);
steppers.idleMotors();
pinMode(LED_BUILTIN, OUTPUT);
for (int i; i < POINT_ARR_MAX; i++)
point_arr[i] = {.x = 0,.y = 0};
}
void loop() {
// put your main code here, to run repeatedly:
if (prev_state == NOT_READY) {
Serial.println("ready");
prev_state = READY;
}
byte temp_arr[POINT_ARR_MAX * 2 * UI_SIZE];
for (int i; i < POINT_ARR_MAX * 2 * UI_SIZE; i++)
temp_arr[i] = 0;
if(Serial.available() > 0) {
prev_state = NOT_READY;
rec++;
Serial.readBytes(temp_arr, POINT_ARR_MAX * 2 * UI_SIZE);
for (int i = 0; i < POINT_ARR_MAX; i++) {
point temp_p = {.x = 0,.y = 0};
for (int j = 0; j < 2; j++) {
unsigned int temp_int = 0; // calc current int
for (int k = 0; k < UI_SIZE; k++) {
if (k == 0)
temp_int += temp_arr[4*i+2*j+k];
else
temp_int += temp_arr[4*i+2*j+k] << 8;
}
// put into point
if (j == 0) {
temp_p.x = temp_int;
} else {
temp_p.y = temp_int;
}
}
point_arr[i] = temp_p;
}
for (int i = 0; i < POINT_ARR_MAX; i++) {
//print_point(i);
if (firstPoint) {
lastStart = millis();
firstPoint = false;
}
if (millis() - lastStart > MAX_TIME) {
delay(BREAK_TIME);
lastStart = millis();
}
steppers.moveToPoint(point_arr[i].x, point_arr[i].y);
}
}
steppers.idleMotors();
}
void print_point(int p_index) {
}
int process_points() {
delay(100);
return 1;
}