ANEXA 1: Schema de conectare a componentelor robotului la Arduino 55 ANEXA 2: Diagrama robotului 56 ANEXA 3: Codul pentru Arduino //includere… [625470]

54
4. ANEXE
ANEXA 1: Schema de conectare a componentelor robotului la Arduino

55

ANEXA 2: Diagrama robotului

56
ANEXA 3: Codul pentru Arduino
//includere librarii
#include <SoftwareSerial.h>
#include <NewPing.h> //= librarie specifica senzorului ultrasonic

//definire pini de comunicare cu modul wifi
SoftwareSerial esp8266(3, 2); //RX pin = 3, TX pin = 2

//definirea variabilelor
#define DEBUG true //arata mesajele dintre ESP8266(modul WIFI) si Arduino prin
Serial Monitor

// definirea pinilor senzorului ultrasonic si a distantei maxime
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 400

//definirea starii initiale ale robotului (5 = stand -by)
int state = 5;

//definire pini motoare
const int motor1Pin1 = 5;
const int motor1Pin2 = 6;
const int motor2Pin1 = 9;
const int motor2Pin2 = 10;

//viteza motoarelor (PWM)
int motorSpeed = 200;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

//*****
//definire
//*****
void setup()
{
//definire mod pini motoare
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);

//pornire comunicare cu arduino si modul Wifi
Serial.begin(9600);
esp8266.begin(9600);

//sendData( "AT+UART_DEF=9600,8,1,0,0 \r\n", 1000, DEBUG ) ; // definirea ratei
de comunicare a modului wifi cu Arduino (se foloseste o singura data,la prima scriere)
//delay(5000) ;

57

sendData("AT+RST \r\n", 2000, DEBUG); //resetare modul wifi
sendData("AT+CWMODE= 1\r\n", 1000, DEBUG); //selectare mod de operare
modul wifi
sendData("AT+CWJAP= \"ASUS@home \",\"bogdisefu112 \"\r\n", 2000, DEBUG);
//conectarea la reteaua Wifi definita in parametrii
delay(5000); //asteapta conexiunea cu routerul

sendData("AT+CIFSR \r\n", 1000, DEBUG); //arata adresa IP
sendData("AT+CIPMUX=1 \r\n", 1000, DEBUG); //permite conexiuni multiple
sendData("AT+CIPSERVER=1,80 \r\n", 1000, DEBUG); // pornire server
comunicatie pe portul 80
}

//*********
// LOOP PRINCIPAL
//*********

void lo op()
{
if (esp8266.available()) //verificare date primite
{
if (esp8266.find("+IPD,")) //arata daca este ceva mesaj de la modul
{
String msg;
esp8266.find("?"); //cauta(cere) mesajul
msg = esp8266.readStringUntil(' '); // citirea mesajulu in intregime
String command = msg.substring(0, 3); //primele 3 caractere = comanda
Serial.println(command);

//comanda pentru miscare in fata
if (command == "cm2") {
state = 1;
}

//comanda pentr u miscare in spate
if (command == "cm1") {
state = 2;
}

//comanda pentru vireaza dreapta
if (command == "cm3") {
state = 3;
}

//comanda pentru vireaza stanga
if (command == "cm4") {
state = 4;
}

58
//nu face nimic(stand by)
if (command == "cm5") {
state = 5;
}
}
}

unsigned int distance = sonar.ping_cm(); // citirea distantei in cm de la senzor
//Serial.print(distance); // afisarea distantei citite ( pentru testare )
// Serial.println("cm");

//STATUT 1 = merge in spate
if (state == 1) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATUT 2 = merge in fata
if (state == 2) {
if (distance != 0) { // daca distanta este diferita de 0 , merge in fata
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
digitalWrite(motor2Pin1, LOW);
analogWrite( motor2Pin2, motorSpeed);
}
if (distance > 5) { // daca distanta este mai mare decat 5cm, merge in fata
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
digitalWrite(motor2Pin1, LOW);
analogWrite( motor2Pin2, motorSpeed);
}
if (distance < 5 ) { // daca distanta este mai mica decat 5cm, merge in spate
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite( motor2Pin2, LOW);
}
}
//STATUT 3 = vireaza dreapta
if (state == 3) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed);
}
//STATUT 4 = vireaz a stanga
if (state == 4) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);

59
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATUT 5 = nu face nimic(stand -by)
if (state == 5) {
digitalW rite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
}

//*******************
//Functie auxiliare – utilizata la afisarea " raspunsului " modului WIfI catre placa
arduino si afisata in SerialMonitor
//*******************
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266.a vailable())
{
char c = esp8266.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

60
ANEXA 4: Codul HTML al interfe ței
<html>
<head>
<meta http -equiv="Content -Type" content="text/html; charset=UTF -8" />
<meta name="author" content="PASC BOGDAN -FLORIN">
<title>Interfata de control a robotului</title>
<script src="jquery.js"></script>
<script src="myscript.js"></script>
</head>
<body>
<body style="background -color:#F5DEB3;">
<p style="text -align:center;font -size:150%;"><strong>Interfata de control a
robotului</strong></p>
<div id="main">
<div id="video_and_audio" style="position: absolute; top: 50px; width:25%">
<form name="myform" action="" method="GET">IP Camera:<input type="text"
name="i nputbox" value="192.168.0.0">
<input type="button" name="button1" value="Incarca"
onClick="readUrlAV(this.form)">
</form>
<object id="video" type="text/html" data="http://192.168.0.5:8080/video"
style="position: absolute; width:100%; height:480px;" ></objec t>
<object id="audio" type="text/html" data="http://192.168.0.5:8080/audio.opus"
style="position: absolute; width:388px; height:30px; left:8px; top:425px;"></object>
</div>
<div id="commands" style="position: absolute; top: 50px; left:35%; width:65%">
<form name="myform2" action="" method="GET">IP Modul Arduino:
<input type="text" name="inputbox" Value="192.168.0.0"><input type="button"
name="button3" Value="Testeaza" onClick="testarArduino(this.form)">
</form>
<img src="images/tastatura.jpg" style="margin -left:100px; margin –
top:50px;width:200px;height:120px;">
<h4> Apăsați tastele săgeată(de la tastatură) pentru a controla robotul </h4>
<h5><P> În momentul În care conexiunea este stabilită, robotul trebuie să se
rotească in cerc!
<p>Dacă face asta, Robotul așteapt următoarele comenzi. 🙂 </h5>
<img src="images/IMG_20190609_112106(1)2.jpg" alt="robotul RoWi"
style="width:450px;height:250px;align=center">
</div>
</body>
</html>

61
ANEXA 5: Codul Java al interfeț ei
$.ajaxSetup({timeout:100});

var latch = false;

function readUrlAV (form) {
TextVar = form.inputbox.value;
VideoVar = "http://"+TextVar+":8080/video";
AudioVar = "http://"+TextVar+":8080/audio.opus";
document.getElementById("video").setAttribute('data', VideoVar);
document.getEl ementById("audio").setAttribute('data', AudioVar);
}
function testarArduino (form) {
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80";
$.get( ArduinoVar, { "cm3": 7000 }) ;
{Connection: close};
}

document.onkeydown = check KeyDown;
document.onkeyup = checkKeyUp;

function checkKeyDown(e) {

e = e || window.event;

if (e.keyCode == '38') {
// sageata sus
if (latch == false) {
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80";
$.get( ArduinoVar, { "cm1": 1000 }) ;
{Connection: close};
latch = true;
}
}
else if (e.keyCode == '40') {
// sageata jos
if (latch == false) {
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80";
$.get( ArduinoVar, { "cm2": 1000 }) ;
{Connection: close};
latch = true;
}
}
else if (e.keyCode == '37') {
// sageata satanga
if (latch == false) {

62
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80" ;
$.get( ArduinoVar, { "cm3": 1000 }) ;
{Connection: close};
latch = true;
}

}
else if (e.keyCode == '39') {
// sageata dreapta
if (latch == false) {
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80";
$.get( ArduinoVar, { "cm4": 1000 }) ;
{Connection: close};
latch = true;
}
}
}

function checkKeyUp(e) {
e = e || window.event;

if ((e.keyCode == '38')||(e.keyCode == '40')||(e.keyCode == '37')||(e.keyCode ==
'39')) {
// sageata sus
setTimeout(doNothing, 200);
}
}

function doNothing(){
TextVar = myform2.inputbox.value;
ArduinoVar = "http://" + TextVar + ":80";
$.get( ArduinoVar, { "cm5": 1000 }) ;
{Connection: close};
latch = false;
}

Similar Posts