This shows you the differences between two versions of the page.
pm:prj2022:robert:mini_wall-e [2022/05/27 18:53] ovidiu.ghibea [Software Design] |
pm:prj2022:robert:mini_wall-e [2022/05/27 19:33] (current) ovidiu.ghibea [Jurnal] |
||
---|---|---|---|
Line 44: | Line 44: | ||
- | We begin by declaring all global variables and creating the classes needed for the player. | + | First define trig and echo pin of HC-SR04 in the program. In this project the trig pin is connected to GPIO9 and echo pin is connected to GPIO10 of Arduino NANO. |
<code cpp> | <code cpp> | ||
- | #define BUTTON 2//detonator button | + | int trigPin = 9; // trig pin of HC-SR04 |
- | #define LEDRED 5//armed LED | + | |
- | #define LEDLEFT 6 // yellow LED | + | |
- | #define LEDMIDDLE 7 // yello LED | + | |
- | #define LEDRIGHT 8 //yellow LED | + | |
- | #define SENSOR 9 //sensor input | + | |
- | #define TX 10 //TX pin on DFPlayer module | + | |
- | #define RX 11 //RX pin on DFPlayer module | + | |
- | const int time = 2124;//the delay between sequence change | + | int echoPin = 10; // Echo pin of HC-SR04 |
- | int sequenceNumber = 0; | + | |
- | + | ||
- | SoftwareSerial softwareSerial(TX, RX); | + | |
- | + | ||
- | DFRobotDFPlayerMini player; // declaration of player | + | |
</code> | </code> | ||
- | We create two functions: one that will turn on the yellow LEDs in a given configuration (i. e. left-on, middle-off, right-on) and a function that returns the value of the movement sensor. | + | Define pins for input of LM298N Motor Driver Module. The LM298N has 4 data input pins used to control the direction of motor connected to it. |
<code cpp> | <code cpp> | ||
- | //Run one LED sequence | + | int revleft4 = 4; //REVerse motion of Left motor |
- | void Sequence(bool Left, bool Middle, bool Right){ | + | int fwdleft5 = 5; //ForWarD motion of Left motor |
- | if (digitalRead(BUTTON) == LOW)//if true (button is pressed) run LED sequence | + | int revright6 = 6; //REVerse motion of Right motor |
- | { | + | int fwdright7 = 7; //ForWarD motion of Right motor |
- | if (Left) | + | |
- | { | + | |
- | digitalWrite(LEDLEFT, HIGH); | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | digitalWrite(LEDLEFT, LOW); | + | |
- | } | + | |
- | + | ||
- | if (Middle) | + | |
- | { | + | |
- | digitalWrite(LEDMIDDLE, HIGH); | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | digitalWrite(LEDMIDDLE, LOW); | + | |
- | } | + | |
- | + | ||
- | if (Right) | + | |
- | { | + | |
- | digitalWrite(LEDRIGHT, HIGH); | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | digitalWrite(LEDRIGHT, LOW); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | long TP_init(){ | + | |
- | long measurement=pulseIn (SENSOR, HIGH); //wait for the pin to get HIGH and returns measurement | + | |
- | return measurement; | + | |
- | } | + | |
</code> | </code> | ||
- | After this, we begin the setup process by starting the program and setting all LEDs on LOW. | + | In setup() function, define the data direction of utilised GPIO pins. The four Motor pins and Trig pin is set as OUTPUT and Echo Pin is set as Input. |
<code cpp> | <code cpp> | ||
- | void setup() | + | pinMode(revleft4, OUTPUT); // set Motor pins as output |
- | { | + | pinMode(fwdleft5, OUTPUT); |
- | Serial.begin(9600); | + | pinMode(revright6, OUTPUT); |
- | pinMode(SENSOR, INPUT); | + | pinMode(fwdright7, OUTPUT); |
- | softwareSerial.begin(9600); | + | pinMode(trigPin, OUTPUT); // set trig pin as output |
- | + | pinMode(echoPin, INPUT); //set echo pin as input to capture reflected waves | |
- | pinMode(LEDRED, OUTPUT); | + | |
- | pinMode(LEDLEFT, OUTPUT); | + | |
- | pinMode(LEDMIDDLE, OUTPUT); | + | |
- | pinMode(LEDRIGHT, OUTPUT); | + | |
- | + | ||
- | digitalWrite(LEDRED, LOW); | + | |
- | digitalWrite(LEDLEFT, LOW); | + | |
- | digitalWrite(LEDMIDDLE, LOW); | + | |
- | digitalWrite(LEDRIGHT, LOW); | + | |
- | + | ||
- | //now set pins to input | + | |
- | pinMode(BUTTON, INPUT_PULLUP); | + | |
- | + | ||
- | + | ||
- | // start the player | + | |
- | player.begin(softwareSerial); | + | |
- | + | ||
- | + | ||
- | } | + | |
</code> | </code> | ||
- | The loop() part of the program is made out of two scenarios. \\ | + | In loop() function, get the distance from HC-SR04 and based on the distance move the motor direction. The distance will show the object distance coming in front of the robot. The Distance is taken by bursting a beam of ultrasonic up to 10 us and receiving it after 10us. To learn more about measuring distance using Ultrasonic sensor and Arduino, follow the link. |
- | + | ||
- | In the first scenario, the switch is off (The convention for this project is that if the microswitch is off, the circuit is active. If the switch is on, then the circuit is off. The reason for this being the outer design of the toy as well as the desire to make it movie accurate) and the program begins the arming sequence. It receives the measurement from the movement sensor and if the measurement is smaller than the chosen value, it keeps on with the arming sequence. If the measurement is greater than the chosen value, it stops the previous sequence by stopping track 1 and starts the detonation sequence by playing track 2 and turning all LED’s on HIGH. | + | |
<code cpp> | <code cpp> | ||
- | // Start communication with DFPlayer Mini | + | digitalWrite(trigPin, LOW); |
- | + | delayMicroseconds(2); | |
- | // Set volume to a value between (0 to 30) | + | digitalWrite(trigPin, HIGH); // send waves for 10 us |
- | player.volume(30); | + | delayMicroseconds(10); |
- | + | duration = pulseIn(echoPin, HIGH); // receive reflected waves | |
- | if (digitalRead(BUTTON) == LOW ){ | + | distance = duration / 58.2; // convert to distance |
- | player.play(1); // play the arming sequence | + | delay(10); |
- | digitalWrite(LEDRED, HIGH);//switch armed LED (red) on | + | |
- | } | + | |
- | + | ||
- | if (digitalRead(BUTTON) == LOW && sequenceNumber == 0) | + | |
- | { | + | |
- | long measurement =pulseIn (SENSOR, HIGH); //get measurement from teh sensor | + | |
- | Serial.println(measurement); | + | |
- | if (measurement > 1000) | + | |
- | { // measurement checks | + | |
- | player.stop(); | + | |
- | player.play(2); //playing detonating sequence | + | |
- | sequenceNumber = 0; | + | |
- | digitalWrite(LEDRED, HIGH); | + | |
- | digitalWrite(LEDRIGHT, HIGH); | + | |
- | digitalWrite(LEDMIDDLE, HIGH); | + | |
- | digitalWrite(LEDLEFT, HIGH); | + | |
- | while(digitalRead(BUTTON)==LOW){ // give a chance to turn off the toy | + | |
- | delay(10000); | + | |
- | player.stop(); | + | |
- | digitalWrite(LEDRED, LOW); | + | |
- | digitalWrite(LEDRIGHT, LOW); | + | |
- | digitalWrite(LEDMIDDLE, LOW); | + | |
- | digitalWrite(LEDLEFT, LOW); | + | |
- | } | + | |
- | } | + | |
- | else | + | |
- | { // measuremnt does not check | + | |
- | delay(time); | + | |
- | Sequence(false, true, true); // continu with arming sequence | + | |
- | sequenceNumber++; | + | |
- | } | + | |
- | } | + | |
</code> | </code> | ||
- | The second scenario is when the switch is on, so the circuit is not active. | + | If the distance is greater than the defined distance means there is not obstacle in its path and it will moving in forward direction. |
<code cpp> | <code cpp> | ||
- | if (digitalRead(BUTTON) == HIGH) // toy is turned off | + | if (distance > 19) |
- | { | + | { |
- | sequenceNumber = 0; | + | digitalWrite(fwdright7, HIGH); // move forward |
- | player.stop(); | + | digitalWrite(revright6, LOW); |
- | digitalWrite(LEDRED, LOW); | + | digitalWrite(fwdleft5, HIGH); |
- | digitalWrite(LEDRIGHT, LOW); | + | digitalWrite(revleft4, LOW); |
- | digitalWrite(LEDMIDDLE, LOW); | + | } |
- | digitalWrite(LEDLEFT, LOW); | + | |
- | } | + | |
</code> | </code> | ||
- | Additionally, depending on the length of track 1 used in the arming sequence, we will choose a number of times the respective if{} statement will be repeated. In this case it, is repeated 12 times. | + | If the distance is less than the defined distance to avoid obstacle means there is some obstacle ahead. So in this situation robot will stop for a while and movebackwards after that again stop for a while and then take turn to another direction. |
+ | <code cpp> | ||
+ | if (distance < 18) | ||
+ | { | ||
+ | digitalWrite(fwdright7, LOW); //Stop | ||
+ | digitalWrite(revright6, LOW); | ||
+ | digitalWrite(fwdleft5, LOW); | ||
+ | digitalWrite(revleft4, LOW); | ||
+ | delay(500); | ||
+ | digitalWrite(fwdright7, LOW); //movebackword | ||
+ | digitalWrite(revright6, HIGH); | ||
+ | digitalWrite(fwdleft5, LOW); | ||
+ | digitalWrite(revleft4, HIGH); | ||
+ | delay(500); | ||
+ | digitalWrite(fwdright7, LOW); //Stop | ||
+ | digitalWrite(revright6, LOW); | ||
+ | digitalWrite(fwdleft5, LOW); | ||
+ | digitalWrite(revleft4, LOW); | ||
+ | delay(100); | ||
+ | digitalWrite(fwdright7, HIGH); | ||
+ | digitalWrite(revright6, LOW); | ||
+ | digitalWrite(revleft4, LOW); | ||
+ | digitalWrite(fwdleft5, LOW); | ||
+ | delay(500); | ||
+ | } | ||
+ | </code> | ||
+ | This is how a robot can avoid obstacles in its path without getting stuck anywhere. | ||
===== Rezultate Obţinute ===== | ===== Rezultate Obţinute ===== | ||
Line 196: | Line 121: | ||
===== Concluzii ===== | ===== Concluzii ===== | ||
+ | After finishing the project, I learned that:\\ | ||
+ | *While the code is simple, the hardest part was the making of the chasis. | ||
+ | |||
+ | *Some parts have very tough bolts to unscrew which led to a lot of wasted time | ||
+ | |||
+ | *It was fun to see it work after all the effort put in it | ||
+ | |||
===== Download ===== | ===== Download ===== | ||
- | <note warning> | + | {{:pm:prj2022:robert:obsav.rar|Source Code}} |
- | O arhivă (sau mai multe dacă este cazul) cu fişierele obţinute în urma realizării proiectului: surse, scheme, etc. Un fişier README, un ChangeLog, un script de compilare şi copiere automată pe uC crează întotdeauna o impresie bună ;-). | + | |
- | Fişierele se încarcă pe wiki folosind facilitatea **Add Images or other files**. Namespace-ul în care se încarcă fişierele este de tipul **:pm:prj20??:c?** sau **:pm:prj20??:c?:nume_student** (dacă este cazul). **Exemplu:** Dumitru Alin, 331CC -> **:pm:prj2009:cc:dumitru_alin**. | + | {{:pm:prj2022:robert:rob.png?linkonly|Electric Diagram}} |
- | </note> | + | |
- | ===== Jurnal ===== | ||
- | |||
- | <note tip> | ||
- | Puteți avea și o secțiune de jurnal în care să poată urmări asistentul de proiect progresul proiectului. | ||
- | </note> | ||
===== Bibliografie/Resurse ===== | ===== Bibliografie/Resurse ===== | ||
- | <note> | + | *[[https://store.arduino.cc/products/arduino-uno-rev3/|Arduino UNO]] |
- | Listă cu documente, datasheet-uri, resurse Internet folosite, eventual grupate pe **Resurse Software** şi **Resurse Hardware**. | + | *[[https://www.newark.com/stmicroelectronics/l298n/ic-bridge-driver-dual-298/dp/10WX1394?rpsku=rel1:32M1527&isexcsku=false|SparkFun Dual H-Bridge motor drivers L298]] |
- | </note> | + | *[[https://www.sparkfun.com/products/15569|SparkFun Ultrasonic Sensor - HC-SR04]] |
<html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | <html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | ||