Posted on

Robot 4WD Roboromania autonom ocolire obstacole cu senzor cu ultrasunete

Robot 4WD Roboromania autonom ocolire obstacole cu senzor cu ultrasunete

Aveti nevoie de :
o Placă de dezvoltare tip Arduino UNO (oricare)
un Modul driver motoare L293N
un Senzor cu ultrasunete HCSR04
un Kit șasiu 4WD
cabluri Dupont

kit-4wd-ultra-robo

Cum asamblăm șasiul : https://www.youtube.com/watch?v=uW8YVcBjPGU

Cum instalam o platforma Arduino UNO :

https://roboromania.ro/manuale/Arduino-1-Starter-Kit-manual-roboromania.pdf

sau

https://roboromania.ro/arduino_books/Introduction-Arduino-2013.pdf

 

Cel mai simplu cod :

//––––––––––––––

// Robot 2WD Roboromania autonom ocolire obstacole
// roboromania.ro pentru ID Arduino.1.6.x atentie sa aveti „libraries” – > „NewPing”

// ––––––––––––––-

// Robot 2WD Roboromania autonom ocolire obstacole – versiunea corectata (si testata) 2018.02.03
// roboromania.ro

#include <NewPing.h>

#define TRIG_PIN 8
#define ECHO_PIN 7
#define MAX_DISTANCE 400
#define COLL_DIST 20 // distanta de coliziune la care robot stop si inapoi este de : 20cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

// L298n module
// 1(+)inainte dreapta = DrFr
// 2(+)inapoi dreapta = DrSp
// 3(+)inainte stanga = StFr
// 4(+)inapoi stanga = StSp

int DrFr = 3;
int DrSp = 2;
int StFr = 4;
int StSp = 5;

void setup() {
Serial.begin(9600);
pinMode(DrFr,OUTPUT);
pinMode(StFr,OUTPUT);
pinMode(DrSp,OUTPUT);
pinMode(StSp,OUTPUT);
digitalWrite(DrFr,LOW);
digitalWrite(StFr,LOW);
digitalWrite(DrSp,LOW);
digitalWrite(StSp,LOW);
}

int scan() {
return (sonar.ping() / US_ROUNDTRIP_CM); //masurare distanta in cm
}

void loop() {
int Dist = scan(); // masuram distanta curenta
// Serial.println(Dist);
if (( Dist > 0 ) || ( Dist < COLL_DIST )) { // daca distanta curenta < decit distanta de coliziune
moveStop();
moveBackward();
delay(500);
turnRight();
delay(300);
} else {
moveForward();
}
}

void moveStop() {
digitalWrite(DrFr,LOW);
digitalWrite(StFr,LOW);
digitalWrite(DrSp,LOW);
digitalWrite(StSp,LOW);
}

void moveForward() {
digitalWrite(DrFr,HIGH);
digitalWrite(StFr,HIGH);
digitalWrite(DrSp,LOW);
digitalWrite(StSp,LOW);
}

void moveBackward() {
digitalWrite(DrFr,LOW);
digitalWrite(StFr,LOW);
digitalWrite(DrSp,HIGH);
digitalWrite(StSp,HIGH);
}

void turnRight() {
digitalWrite(DrFr,LOW);
digitalWrite(StFr,HIGH);
digitalWrite(DrSp,HIGH);
digitalWrite(StSp,LOW);
}

void turnLeft() {
digitalWrite(DrFr,HIGH);
digitalWrite(StFr,LOW);
digitalWrite(DrSp,LOW);
digitalWrite(StSp,HIGH);
}

 

//––––––––––-

Alt exemplu de cod :

https://roboromania.ro/2018/02/04/robot-2wd-roboromania-autonom-ocolire-obstacole-realizat-de-stefan-ziegler-din-arad/

Lasă un răspuns