Robot code for searching an object and charging at it.

Dependencies:   HCSR04 Motor mbed

main.cpp

Committer:
alex0612
Date:
2015-06-07
Revision:
35:89e13f72cd84
Parent:
32:775eec44ba4f
Child:
37:a08d2e37b7e6

File content as of revision 35:89e13f72cd84:

// Code written by:
// Jon Baker
// Alessandro Grande
// Abdul-Rehman Sharif
// Lucinda Hartfield

// Circuitry made by:
// Jon Baker
// Yucando Navarrete
// Vivekanand Gupta

// The following code controls a robotic car
// 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
//
// detect_object(range, speed)
//                      -   Used to detect an object, the robot will
//                          move in a circle to find an object. This function
//                          returns 1 if an object is detected, otherwise it will 
//                          return 0. The search will be carried out for 15 seconds.
//
// detect_line ()
//                      -   Used to detect a line, it returns the following values:
//                              0  - if no line is detected
//                              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 
//                          will be carried out for a random time.
//

// Libraries for using the above functions and more ...
#include "mbed.h"
#include "Motor.h"
#include "hcsr04.h"
#include "functions.h"

// Set for debugging purpose
// Example: pc(TX, RX)
//Serial pc(USBTX, USBRX);

// Global parameters

float forwardspeed;
float reversespeed;
float searchspeed;
unsigned int range;

void initialise()
{
    // Each speed value can be set from 0 to 1
    
    // Speed at which it moves forward
    // optimum value: 0.5 to 0.8
    forwardspeed = 0.6;
    // Speed at which it reverses
    // optimum value: 0.4 to 0.6
    reversespeed = 0.5;
    // Speed at which it rotates to find an object
    // optimum value: 0.4 to 0.6
    searchspeed = 0.5;
    // Range of detection
    // optimum value: 30 to 50
    range = 30;

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

    int detect_l = 0;
    int detect_o = 0;

    while(true) {
        // Sample code to detect and object and move_forward at it
        
        wait(1);
        
        detect_o = detect_object(range, searchspeed);

        if (detect_o == 1) {

            move_forward(forwardspeed);

            while (true) {

                detect_l = detect_line();
                // If line is detected from front then reverse
                if(detect_l == 1 || detect_l == -1) {
                    stop();
                    turn_led_right();
                    reverse(reversespeed);
                    wait(1);
                    detect_l = 0;
                    break;
                }
            }

        } else {

            move_random();

        }

        detect_o = 0;
        stop();
    } 
}