Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

Files at this revision

API Documentation at this revision

Comitter:
abdsha01
Date:
Sat May 23 14:13:09 2015 +0000
Child:
1:bd88d4062c97
Commit message:
Draft version

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
Motor.lib Show annotated file Show diff for this revision Revisions of this file
functions.cpp 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Sat May 23 14:13:09 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/prabhuvd/code/HCSR04/#71da0dbf4400
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.lib	Sat May 23 14:13:09 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Motor/#f265e441bcd9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.cpp	Sat May 23 14:13:09 2015 +0000
@@ -0,0 +1,92 @@
+// The following file implements all the functions necessary 
+// for the robot.
+// Please don't modify any of these functions!
+
+#include "mbed.h"
+#include "Motor.h"
+#include "hcsr04.h"
+
+// Returns value from the QRE1113
+// lower numbers mean more refleacive
+// more than 3000 means nothing was reflected.
+int read_line1()
+{
+    // Time to record how long input is active
+    Timer t;
+    
+    // Activate the line sensor and then read
+    line1.output();
+    line1 = 1;
+    wait_us(15);
+    line1.input();
+    
+    // Start timer
+    t.start();
+
+    // Time how long the input is HIGH, but quit
+    // after 1ms as nothing happens after that
+    while (line1 == 1 && t.read_us() < 1000);
+    return t.read_us();
+}
+
+int read_line2()
+{
+    // Time to record how long input is active
+    Timer t;
+    
+    // Activate the line sensor and then read
+    line2.output();
+    line2 = 1;
+    wait_us(15);
+    line2.input();
+    
+    // Start timer
+    t.start();
+
+    // Time how long the input is HIGH, but quit
+    // after 1ms as nothing happens after that
+    while (line2 == 1 && t.read_us() < 1000);
+    return t.read_us();
+}
+
+int detect_line()
+{
+    int line1val = read_line1();
+    int line2val = read_line2();
+    
+    if ((line1val-line2val) > 50) {
+        printf("Line detected from front");
+        return 1;
+    } else if ((line1val-line2val) < -50) {
+        printf("Line detected from back");
+        return -1;
+    } else {
+        printf("Line not detected");
+        return 0;
+    }
+}
+
+void reverse()
+{
+    printf("Reverse\n");
+    MotorLeft.speed((chargespeed));
+    MotorRight.speed(-(chargespeed));
+    wait_ms(1000);
+    return;
+}
+
+void reverseandturn()
+{
+    printf("Reverse and turn\n");
+    MotorLeft.speed((chargespeed-0.2));
+    MotorRight.speed(-(chargespeed-0.1));
+    wait_ms(2000);
+    return;
+}
+
+void charge()
+{
+    MotorLeft.speed(chargespeed);
+    MotorRight.speed(chargespeed);
+    return;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat May 23 14:13:09 2015 +0000
@@ -0,0 +1,135 @@
+// Code written by:
+// Jon Baker
+// Alessandro Grande
+// Abdul-Rehman Sharif
+// Lucinda Hartfield
+
+// Circuitry made by:
+// Yucando Navarrete
+// Vivekanand Gupta
+
+// The following code will control a robotic car
+// by detetecting an object and charging towards it
+// it uses basic functions as:
+// charge()        - used to charge on an object detected 
+//                   the robot will move in a straight line
+//                   until it detects the arena line where
+//                   it will use reverse() to move back
+// detect_object() - used to detect an object, the robot will
+//                   move in a circle to find an object and 
+//                   return 1 if it finds something and return
+//                   0 if it does not find anything - the search
+//                   will be carried out for 10 seconds.
+// detect_line ()  - used to detect a line, it returns the following
+//                   an int value as follows:
+//                   0  - if no line is detected
+//                   1  - if line detected from the front
+//                   -1 - if line detected from the back
+
+// Libraries for using the above functions and more ...
+#include "mbed.h"
+#include "functions.h"
+
+// Two sensors are used to detect a line, line_sens1 and line_sens2
+// if there is a difference between the readings of these sensors 
+// the robot has detected a line.
+// Setting pins for line sensor
+DigitalInOut line1(p20);
+DigitalInOut line2(p19);
+
+// Setting pins for motor, as follows:
+// Example: Motor____(PWM, Forward, Reverse)
+Motor MotorLeft(p23, p28, p27);
+Motor MotorRight(p22, p29, p30);
+
+// Setting pins for ultrasonic sensor, as follows:
+// Example: usensor(Trigger, Echo)
+HCSR04  usensor(p25,p26);
+
+// Set for debugging purpose
+// Example: pc(TX, RX)
+//Serial pc(USBTX, USBRX);
+
+
+// Global parameters
+// Speed at which it charged an object
+// optimum value: 0.4 to 0.8
+float chargespeed;
+// Speed at which it rotates to find an object
+// optimum value: 0.3 to 0.5
+float searchspeed;
+// Range of detection
+// optimum value: 30 to 50
+int range;
+
+void initialise()
+{
+    chargespeed = 0.5;
+    searchspeed = 0.3;   
+    range = 40;
+    
+    // Wait for 5 seconds to move away from robot
+    wait(5);
+}
+
+// The main loop - please write your code here
+int main()
+{
+    // Initialise the code
+    initialise();
+
+    printf("Starting the robot...");
+
+    Timer t;
+    t.start();
+
+    while(1) { // main loop
+        if (dist <= 35 && dist >= 1) {
+            MotorLeft.speed(-0.4);
+            MotorRight.speed(-0.4);
+            Timer t3;
+
+            t3.start();
+
+            //printf("%d,%d,%d\r\n",line1val,line2val,line1val-line2val);
+
+            while (t3.read_ms() < 100) {
+
+                if (((line1val-line2val) > 50) || ((line1val-line2val) < -50)) {
+                    MotorLeft.speed(0.4);
+                    MotorRight.speed(0.4);
+
+                    wait_ms(500);
+
+                    //return 0;
+                    printf("STOP\r\n");
+                }
+                //t.reset();
+            }
+
+        } else {
+            MotorLeft.speed(motorspeed);
+            MotorRight.speed(-(motorspeed));
+        }
+
+        usensor.start();
+        if (t.read_ms() >=100) {
+            dist=usensor.get_dist_cm();
+            motorspeed = -0.8;
+            if (reverse==2) {
+                motorspeed = -0.7;
+                reverse = 0;
+                //motorspeed = -(motorspeed);
+                //reverse=0;
+            } else {
+                motorspeed = 0.0;
+
+            }
+            reverse++;
+            t.reset();
+        }
+
+    }
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat May 23 14:13:09 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557
\ No newline at end of file