· 5 years ago · Jun 06, 2020, 10:36 AM
1/*
2 Arduino Yún Bridge example
3
4 This example for the YunShield/Yún shows how
5 to use the Bridge library to access the digital and
6 analog pins on the board through REST calls.
7 It demonstrates how you can create your own API when
8 using REST style calls through the browser.
9
10 Possible commands created in this shetch:
11
12 "/arduino/digital/13" -> digitalRead(13)
13 "/arduino/digital/13/1" -> digitalWrite(13, HIGH)
14 "/arduino/analog/2/123" -> analogWrite(2, 123)
15 "/arduino/analog/2" -> analogRead(2)
16 "/arduino/mode/13/input" -> pinMode(13, INPUT)
17 "/arduino/mode/13/output" -> pinMode(13, OUTPUT)
18
19 This example code is part of the public domain
20
21 http://www.arduino.cc/en/Tutorial/Bridge
22
23*/
24
25#include <Bridge.h>
26#include <BridgeServer.h>
27#include <BridgeClient.h>
28#include <Console.h>
29
30#include <SharpIR.h>
31
32#define IR1 A5 //lewy przod
33#define IR2 A4 //prawy przod
34#define IR3 A3 // lewy tyl
35#define IR4 A2 //prawy tyl
36
37#define model1 430 //od 4cm do 30cm
38#define model2 430
39#define model3 430
40#define model4 430
41
42SharpIR lewyPrzod = SharpIR(IR1, model1);
43SharpIR prawyPrzod = SharpIR(IR2, model2);
44SharpIR lewyTyl= SharpIR(IR3, model3);
45SharpIR prawyTyl = SharpIR(IR4, model4);
46
47// Listen to the default port 5555, the Yún webserver
48// will forward there all the HTTP requests you send
49BridgeServer server;
50
51//łączenie silników do Arduino
52//pierwszy silnik
53int enA = 10;
54int in1 = 9;
55int in2= 8;
56
57//drugi silnik
58int enB = 5;
59int in3 = 7;
60int in4 = 6;
61
62void setup() {
63 // Bridge startup
64 pinMode(13, OUTPUT);
65 digitalWrite(13, LOW);
66 Bridge.begin();
67 digitalWrite(13, HIGH);
68
69 // ustawienie wszystkich pinów silnika na wyjście
70 pinMode(enA, OUTPUT);
71 pinMode(enB, OUTPUT);
72 pinMode(in1, OUTPUT);
73 pinMode(in2, OUTPUT);
74 pinMode(in3, OUTPUT);
75 pinMode(in4, OUTPUT);
76
77 Console.begin();
78
79 // Listen for incoming connection only from localhost
80 // (no one from the external network could connect)
81 server.listenOnLocalhost();
82 server.begin();
83}
84
85void moveForward() {
86 digitalWrite(in1, HIGH);
87 digitalWrite(in2, LOW);
88 analogWrite(enA, 150);
89
90 digitalWrite(in3, LOW);
91 digitalWrite(in4, HIGH);
92 analogWrite(enB, 150);
93}
94
95void moveBackward() {
96 digitalWrite(in1, LOW);
97 digitalWrite(in2, HIGH);
98 analogWrite(enA, 150);
99
100 digitalWrite(in3, HIGH);
101 digitalWrite(in4, LOW);
102 analogWrite(enB, 150);
103}
104
105void turnLeft() {
106 digitalWrite(in1, LOW);
107 digitalWrite(in2, HIGH);
108 analogWrite(enA, 150);
109
110 digitalWrite(in3, LOW);
111 digitalWrite(in4, HIGH);
112 analogWrite(enB, 150);
113}
114
115void turnRight() {
116 digitalWrite(in1, HIGH);
117 digitalWrite(in2, LOW);
118 analogWrite(enA, 150);
119
120 digitalWrite(in3, HIGH);
121 digitalWrite(in4, LOW);
122 analogWrite(enB, 150);
123}
124
125void stap() {
126 digitalWrite(in1, LOW);
127 digitalWrite(in2, LOW);
128 analogWrite(enA, 0);
129
130 digitalWrite(in3, LOW);
131 digitalWrite(in4, LOW);
132 analogWrite(enB, 0);
133}
134
135void loop() {
136 // Get clients coming from server
137 BridgeClient client = server.accept();
138
139 // There is a new client?
140 if (client) {
141 // Process request
142 process(client);
143
144 // Close connection and free resources.
145 client.stop();
146 }
147
148 delay(50); // Poll every 50ms
149}
150
151void process(BridgeClient client) {
152 // read the command
153 String command = client.readStringUntil('/');
154 Console.print(command);
155
156 // is "digital" command?
157 if (command == "digital") {
158 digitalCommand(client);
159 }
160
161 // is "analog" command?
162 if (command == "analog") {
163 analogCommand(client);
164 }
165
166 // is "mode" command?
167 if (command == "mode") {
168 modeCommand(client);
169 }
170
171 // is "move" command?
172 if(command == "move") {
173 moveCommand(client);
174 }
175}
176
177void digitalCommand(BridgeClient client) {
178 int pin, value;
179
180 // Read pin number
181 pin = client.parseInt();
182
183 // If the next character is a '/' it means we have an URL
184 // with a value like: "/digital/13/1"
185 if (client.read() == '/') {
186 value = client.parseInt();
187 digitalWrite(pin, value);
188 } else {
189 value = digitalRead(pin);
190 }
191
192 // Send feedback to client
193 client.print(F("Pin D"));
194 client.print(pin);
195 client.print(F(" set to "));
196 client.println(value);
197
198 // Update datastore key with the current pin value
199 String key = "D";
200 key += pin;
201 Bridge.put(key, String(value));
202}
203
204void analogCommand(BridgeClient client) {
205 int pin, value;
206
207 // Read pin number
208 pin = client.parseInt();
209
210 // If the next character is a '/' it means we have an URL
211 // with a value like: "/analog/5/120"
212 if (client.read() == '/') {
213 // Read value and execute command
214 value = client.parseInt();
215 analogWrite(pin, value);
216
217 // Send feedback to client
218 client.print(F("Pin D"));
219 client.print(pin);
220 client.print(F(" set to analog "));
221 client.println(value);
222
223 // Update datastore key with the current pin value
224 String key = "D";
225 key += pin;
226 Bridge.put(key, String(value));
227 } else {
228 // Read analog pin
229 value = analogRead(pin);
230
231 // Send feedback to client
232 client.print(F("Pin A"));
233 client.print(pin);
234 client.print(F(" reads analog "));
235 client.println(value);
236
237 // Update datastore key with the current pin value
238 String key = "A";
239 key += pin;
240 Bridge.put(key, String(value));
241 }
242}
243
244void modeCommand(BridgeClient client) {
245 int pin;
246
247 // Read pin number
248 pin = client.parseInt();
249
250 // If the next character is not a '/' we have a malformed URL
251 if (client.read() != '/') {
252 client.println(F("error"));
253 return;
254 }
255
256 String mode = client.readStringUntil('\r');
257
258 if (mode == "input") {
259 pinMode(pin, INPUT);
260 // Send feedback to client
261 client.print(F("Pin D"));
262 client.print(pin);
263 client.print(F(" configured as INPUT!"));
264 return;
265 }
266
267 if (mode == "output") {
268 pinMode(pin, OUTPUT);
269 // Send feedback to client
270 client.print(F("Pin D"));
271 client.print(pin);
272 client.print(F(" configured as OUTPUT!"));
273 return;
274 }
275
276 client.print(F("error: invalid mode "));
277 client.print(mode);
278}
279
280void moveCommand(BridgeClient client) {
281 String value;
282
283 //Czytanie odleglosci
284 float volt1 = analogRead(IR1) * 0.0048828125;
285 float volt2 = analogRead(IR2) * 0.0048828125;
286 float volt3 = analogRead(IR3) * 0.0048828125;
287 float volt4 = analogRead(IR4) * 0.0048828125;
288 int disLP = 13*pow(volt1, -1); //lewy przod
289 int disPP = 13*pow(volt2, -1); //prawy przod
290 int disLT = 13*pow(volt3, -1); //lewy tyl
291 int disPT = 13*pow(volt4, -1); //prawy tyl
292
293 Console.print("LP: ");
294 Console.println(disLP);
295 Console.print("PP: ");
296 Console.println(disPP);
297
298 Console.print("LT: ");
299 Console.println(disLT);
300 Console.print("LT: ");
301 Console.println(disLT);
302
303 value = client.readStringUntil("\r");
304 value.trim();
305
306 Console.print("Move: ");
307 Console.println(value);
308
309 if (value == "forward" && disLP > 8 && disPP > 8) {
310 moveForward();
311 }
312
313 if (value == "backward" && disLT > 8 && disPT > 8) {
314 moveBackward();
315 }
316
317 if (value == "left") {
318 turnLeft();
319 }
320
321 if (value == "right") {
322 turnRight();
323 }
324
325 if (value == "stop") {
326 stap();
327 }
328}