-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslaveCode.ino
217 lines (169 loc) · 4.84 KB
/
slaveCode.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
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
#include <Servo.h>
#include <SoftwareSerial.h>
//SoftwareSerial btSerial(2, 3);
#define btSerial Serial1
Servo servo1_1;
Servo servo1_2;
Servo servo1_3;
Servo servo2_1;
Servo servo2_2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int xRecieve1 = 90;
int xRecieve2 = 90;
int yRecieve1 = 90;
int yRecieve2 = 90;
int flexRecieve1 = 90;
int flexRecieve2 = 90;
boolean newData = false;
float currentAngle[6] = { 90,90,90,90,90,90 };
float targetAngle[6] = { 90,90,90,90,90,90 };
float moveSteps = 50;
//============
void setup()
{
btSerial.begin(38400);
Serial.begin(38400);
Serial.println("begin servo");
targetAngle[0] = 90;
targetAngle[1] = 90;
targetAngle[2] = 90;
targetAngle[3] = 90;
targetAngle[4] = 90;
targetAngle[5] = 90;
servo1_1.attach(2);
servo1_2.attach(3);
servo1_3.attach(4);
servo2_1.attach(5);
servo2_2.attach(6);
servo3.attach(7);
servo4.attach(8);
servo5.attach(9);
servo6.attach(10);
Serial.println("servo started");
}
void loop()
{
recvWithStartEndMarkers(); // проверили, есть ли данные на серийном порту и приняли их с маркерами
if (newData == true)
{
strcpy(tempChars, receivedChars);
newData = false;
parseData(); // спарсили в переменные
//Serial.println("parse data");
/*
Serial.print(xRecieve1);
Serial.print("\t");
Serial.print(xRecieve2);
Serial.print("\t");
Serial.print(yRecieve1);
Serial.print("\t");
Serial.print(yRecieve2);
Serial.print("\t");
Serial.print(flexRecieve1);
Serial.print("\t");
Serial.println(flexRecieve2);
*/
xRecieve1 = constrain(xRecieve1, -5, 110);
xRecieve1 = map(xRecieve1, -5, 110, 0, 180);
targetAngle[3] = xRecieve1;
xRecieve2 = constrain(xRecieve2, -180, 180);
xRecieve2 = map(xRecieve2, -180, 180, 0, 180);
targetAngle[5] = xRecieve2;
//targetAngle[4] = 90;
yRecieve2 = constrain(yRecieve2, -90, 90);
yRecieve2 = map(yRecieve2, -90, 90, 0, 180);
yRecieve1 = constrain(yRecieve1, -90, 40);
yRecieve1 = map(yRecieve1, -90, 40, 0, 180);
targetAngle[1] = yRecieve1;
targetAngle[0] = yRecieve2;
flexRecieve1 = constrain(flexRecieve1, 0, 180);
targetAngle[5] = flexRecieve1;
flexRecieve2 = constrain(flexRecieve2, 0, 180);
targetAngle[2] = flexRecieve2;
}
ServoMove();
}
void ServoMove()
{
for (int i = 0; i < 6; i++)
{
float difference = targetAngle[i] - currentAngle[i];
currentAngle[i] += difference / moveSteps;
Serial.print(currentAngle[i]);
Serial.print(" ");
}
Serial.println();
servo1_1.write(currentAngle[0]);
servo1_2.write(currentAngle[0]);
servo1_3.write(currentAngle[0]);
servo2_1.write(180 - currentAngle[1]);
servo2_2.write(currentAngle[1]);
servo3.write(180-currentAngle[2]);
servo4.write(currentAngle[3]);
servo5.write(currentAngle[4]);
servo6.write(currentAngle[5]);
}
//============
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (btSerial.available() > 0 && newData == false)
{
delay(10);
rc = btSerial.read();
//Serial.print(rc);
ServoMove();
delay(10);
if (recvInProgress == true) {
if (rc != endMarker) {
if (rc == startMarker) continue;
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData()
{
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
xRecieve1 = atoi(strtokIndx); // convert this part to a int, atoi = перевод в int
strtokIndx = strtok(NULL, ",");
xRecieve2 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
yRecieve1 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
yRecieve2 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
flexRecieve1 = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
flexRecieve2 = atoi(strtokIndx);
}
long mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}