-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCU.ino
219 lines (192 loc) · 5.06 KB
/
MCU.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
218
219
#include <FS.h> //Include File System Headers
#include <Arduino_JSON.h>
#define COUNT_LOW 500
#define COUNT_HIGH 2400
#define TIMER_WIDTH 16
#include <ESP32Servo.h>
#include <WiFi.h>
//#include <Servo.h>
int HEADOffset = 0;
int LF1Offset = 0;
int LF2Offset = 6;
int RF1Offset =-7;
int RF2Offset = 0;
int LB1Offset = 0;
int LB2Offset = -2;
int RB1Offset =-4;
int RB2Offset = 0;
const char* ssid = "FAST_513E";
const char* password = "";
const uint16_t port = 9696;
WiFiServer wifiServer(port);
Servo HEAD;
Servo LF1;
Servo LF2;
Servo RF1;
Servo RF2;
Servo LB1;
Servo LB2;
Servo RB1;
Servo RB2;
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
wifiServer.begin();
HEAD.setPeriodHertz(50);
LF1.setPeriodHertz(50);
LF2.setPeriodHertz(50);
RF1.setPeriodHertz(50);
RF2.setPeriodHertz(50);
LB1.setPeriodHertz(50);
LB2.setPeriodHertz(50);
RB1.setPeriodHertz(50);
RB2.setPeriodHertz(50);
//node mcu
//HEAD.attach(16, COUNT_LOW, COUNT_HIGH);
//LF1.attach(5, COUNT_LOW, COUNT_HIGH);
//LF2.attach(4, COUNT_LOW, COUNT_HIGH);
//RF1.attach(0, COUNT_LOW, COUNT_HIGH);
//RF2.attach(2, COUNT_LOW, COUNT_HIGH);
//LB1.attach(14, COUNT_LOW, COUNT_HIGH);
//LB2.attach(12, COUNT_LOW, COUNT_HIGH);
//RB1.attach(13, COUNT_LOW, COUNT_HIGH);
//RB2.attach(15, COUNT_LOW, COUNT_HIGH);
//esp32
HEAD.attach(25, COUNT_LOW, COUNT_HIGH);
LF1.attach(23, COUNT_LOW, COUNT_HIGH);
LF2.attach(22, COUNT_LOW, COUNT_HIGH);
RF1.attach(32, COUNT_LOW, COUNT_HIGH);
RF2.attach(33, COUNT_LOW, COUNT_HIGH);
LB1.attach(19, COUNT_LOW, COUNT_HIGH);
LB2.attach(18, COUNT_LOW, COUNT_HIGH);
RB1.attach(26, COUNT_LOW, COUNT_HIGH);
RB2.attach(27, COUNT_LOW, COUNT_HIGH);
Serial.println();
}
JSONVar jsonData;
//int ret = 0;
//400
const byte buffSize =2000;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
void loop() {
WiFiClient client = wifiServer.available();
if (client) {
//Serial.println("client ok");
while (client.connected()) {
//Serial.println("connected() ok");
while (client.available() > 0) {
char x = client.read();
if (x == startMarker) {
client.readBytesUntil(endMarker, inputBuffer, buffSize);
parseData();
//client.flush();
//memset(inputBuffer, 0, buffSize);
}
//Serial.println("available() ok");
//char x = client.read();
//if (x == endMarker) {
// readInProgress = false;
// newDataFromPC = true;
// inputBuffer[bytesRecvd] = 0;
// parseData();
// //reset
// client.flush();
// memset(inputBuffer, 0, buffSize);
//}
//if (readInProgress) {
// inputBuffer[bytesRecvd] = x;
// bytesRecvd++;
// if (bytesRecvd == buffSize) {
// bytesRecvd = buffSize - 1;
// }
//}
//if (x == startMarker) {
// bytesRecvd = 0;
// readInProgress = true;
//}
}
}
}
}
//<[{"objects": {"HEAD": 12}, "time": 0.0}]>
//<[{"objects": {"BODY": 0, "LB1": 0, "RF1": 0, "LB2": 0, "RF2": 0, "RB2": 0, "RB1": 0, "HEAD": 0, "LF1": 0, "LF2": 0}, "time": 0.0}]>
int angle =0;
String servo = "";
void parseData() {
Serial.print("input: ");
Serial.println(inputBuffer);
jsonData = JSON.parse(inputBuffer);
Serial.print("json data: ");
Serial.println(jsonData[0]);
JSONVar k = jsonData[0]["objects"].keys();
for (int i = 0; i < k.length(); i++) {
String servo = JSON.stringify(k[i]);
Serial.print("servo: ");
Serial.println(servo);
angle=jsonData[0]["objects"][k[i]];
Serial.print("angle: ");
Serial.println(angle);
doAction(servo, angle);
}
}
void doAction(String servo,int angle) {
if (servo == "\"LF1\"") {
angle = 90-angle+LF1Offset;
LF1.write(angle);
Serial.println("LF1");
Serial.println(angle);
}
else if (servo == "\"LF2\""){
angle = 90 + angle+LF2Offset;
LF2.write(angle);
Serial.println("LF2");
Serial.println(angle);
}
else if (servo == "\"RF1\"") {
angle = 90 - angle+RF1Offset;
RF1.write(angle);
Serial.println("RF1");
Serial.println(angle);
}
else if (servo == "\"RF2\"") {
angle = 90 + angle+RF2Offset;
RF2.write(angle);
}
else if (servo == "\"LB1\"") {
angle = 90 - angle+LB1Offset;
LB1.write(angle);
}
else if (servo == "\"LB2\"") {
angle = 90 - angle+LB2Offset;
LB2.write(angle);
}
else if (servo == "\"RB1\"") {
angle = 90 - angle+RB1Offset;
RB1.write(angle);
}
else if (servo == "\"RB2\"") {
angle = 90 - angle+RB2Offset;
RB2.write(angle);
}
else if (servo == "\"HEAD\"") {
//angle = 90 - angle+HEADOffset;
HEAD.write(90 - angle + HEADOffset);
Serial.println("LF1");
Serial.println(90 - angle + HEADOffset);
}
else {
Serial.println("not find servo name");
}
}