Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

main.cpp

Committer:
abdsha01
Date:
2015-05-23
Revision:
0:15664f71b21f
Child:
1:bd88d4062c97

File content as of revision 0:15664f71b21f:

// 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();
        }

    }

}