Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Files at this revision

API Documentation at this revision

Comitter:
alex0612
Date:
Wed Oct 12 11:13:45 2016 +0000
Parent:
36:0a69dc7bfcf9
Child:
38:decff231d886
Commit message:
Cleaned up functions names and added functions to turn and light LEDs

Changed in this revision

functions.cpp Show annotated file Show diff for this revision Revisions of this file
functions.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/functions.cpp	Tue Jun 09 10:28:16 2015 +0000
+++ b/functions.cpp	Wed Oct 12 11:13:45 2016 +0000
@@ -28,6 +28,27 @@
 // Example: usensor(Trigger, Echo)
 HCSR04 usensor(p25,p26);
 
+void turn_led_on(int LED) {
+        switch(LED) {
+            case 1:
+                myled1 = 1;
+                wait(0.1);
+                myled1 = 0;
+            case 2:
+                myled2 = 1;
+                wait(0.1);
+                myled2 = 0;
+            case 3:
+                myled3 = 1;
+                wait(0.1);
+                myled3 = 0;
+            case 4:
+                myled4 = 1;
+                wait(0.1);
+                myled4 = 0;
+        }
+}
+
 void turn_leds_on() {
         myled1 = 1;
         myled2 = 1;
@@ -146,19 +167,25 @@
     MotorRight.speed(-(speed));
 }
 
-void turn(float speed) {
-    //printf("Turning\n");
+void turn_left(float speed) {
+    //printf("Turning left\n");
     MotorLeft.speed(speed);
     MotorRight.speed(-(speed));
 }
 
+void turn_right(float speed) {
+    //printf("Turning right\n");
+    MotorLeft.speed(-(speed));
+    MotorRight.speed(speed);
+}
+
 void reverseandturn(float speed) {
     //printf("Reverse and turn\n");
     MotorLeft.speed((speed-0.3));
     MotorRight.speed(-(speed-0.1));
 }
 
-void move_forward(float speed, float adj) {
+void forwards(float speed, float adj) {
     MotorLeft.speed(speed+adj);
     MotorRight.speed(speed);
 }
@@ -232,7 +259,7 @@
             break;
         } else {
             sense = 0;
-            turn(speed);
+            turn_left(speed);
         }
         
         if (inner_t.read_ms() >=100) {
@@ -258,10 +285,10 @@
     int detect = 0;
     
     if(fwd_bck == 1) {
-        move_forward(speed);
+        forwards(speed);
         wait_ms(wait);
     } else {
-        turn(speed + 0.2);
+        turn_left(speed + 0.2);
     }
     
     while (t.read_ms() < time) {
--- a/functions.h	Tue Jun 09 10:28:16 2015 +0000
+++ b/functions.h	Wed Oct 12 11:13:45 2016 +0000
@@ -22,6 +22,7 @@
 
 
 void flash_leds();
+void turn_led_on(int LED);
 void turn_leds_on();
 void turn_led_right();
 void turn_led_left();
@@ -30,9 +31,10 @@
 int detect_line();
 void move_random(float speed = forwardspeed);
 void reverse(float speed = reversespeed, float adj = ADJUSTMENT);
-void turn(float speed = forwardspeed);
+void turn_left(float speed = forwardspeed);
+void turn_right(float speed = forwardspeed);
 void reverse_and_turn(float speed = reversespeed);
-void move_forward(float speed = forwardspeed, float adj = ADJUSTMENT);
+void forwards(float speed = forwardspeed, float adj = ADJUSTMENT);
 void stop();
 int detect_object(int range_t = range, float speed = searchspeed);
 void move_detect(float speed, int fwd_bck, int time, int wait=0);
\ No newline at end of file
--- a/main.cpp	Tue Jun 09 10:28:16 2015 +0000
+++ b/main.cpp	Wed Oct 12 11:13:45 2016 +0000
@@ -13,11 +13,25 @@
 // by detetecting an object and charging towards it.
 // It uses the following basic functions:
 //
-// move_forward(speed)
-//                      -   Used to move the robot toward a detected object.
-//                          The robot will move forwards in a straight line
-//                          until it detects the arena line where
-//                          it will use reverse() to move back
+// forwards(speed)
+//                      -   Sets the motor speeds in order to go forwards.
+//
+// reverse(speed)
+//                      -   Sets the motor speeds in order to go backwards.
+//
+// turn_left(speed)
+//                      -   Sets the motor speeds to turn the robot to the left.
+//
+// turn_right(speed)
+//                      -   Sets the motor speeds to turn the robot to the right.
+//
+// stop()
+//                      -   Sets the motor speeds to 0.
+//
+// turn_led_on(LED)
+//                      -   Turns on the LED on the mbed specified by the 
+//                          number passed as the argument
+//                              Values need to be between 1 and 4
 //
 // detect_object(range, speed)
 //                      -   Used to detect an object, the robot will
@@ -31,12 +45,6 @@
 //                              1  - if line detected from the front
 //                              -1 - if line detected from the back
 //
-// reverse(speed)
-//                      -   Reverses the robot in a straight line at given speed.
-//
-// stop()
-//                      -   Stops the robot.
-//
 // move_random(speed)
 //                      -   Used to move the robot randomly: the robot will either
 //                          move forward, move backward, or turn around. The movement 
@@ -104,7 +112,7 @@
 
         if (detect_o == 1) {
 
-            move_forward(forwardspeed);
+            forwards(forwardspeed);
 
             while (true) {