
Robot code for searching an object and charging at it.
Dependencies: HCSR04 Motor mbed
functions.cpp
- Committer:
- abdsha01
- Date:
- 2015-05-23
- Revision:
- 4:0507835a3dce
- Parent:
- 1:bd88d4062c97
- Child:
- 6:af897173cb75
File content as of revision 4:0507835a3dce:
// 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" #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); // 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 temp_t; // Activate the line sensor and then read line1.output(); line1 = 1; wait_us(15); line1.input(); // Start timer temp_t.start(); // Time how long the input is HIGH, but quit // after 1ms as nothing happens after that while (line1 == 1 && temp_t.read_us() < 1000); return temp_t.read_us(); } int read_line2() { // Time to record how long input is active Timer temp_t; // Activate the line sensor and then read line2.output(); line2 = 1; wait_us(15); line2.input(); // Start timer temp_t.start(); // Time how long the input is HIGH, but quit // after 1ms as nothing happens after that while (line2 == 1 && temp_t.read_us() < 1000); return temp_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(float speed) { printf("Reverse\n"); MotorLeft.speed((speed)); MotorRight.speed(-(speed)); wait_ms(1000); return; } void reverseandturn(float speed) { printf("Reverse and turn\n"); MotorLeft.speed((speed-0.3)); MotorRight.speed(-(speed-0.1)); wait_ms(2000); return; } void charge(float speed) { MotorLeft.speed(speed); MotorRight.speed(speed); return; } void stop() { MotorLeft.speed(0.0); MotorRight.speed(0.0); return; } int detect_object(int range, float speed) { // Start a timer - finds an object for 10 seconds // if it doesn't find anything returns 0 Timer usensor_t; usensor_t.start(); // Variable to store sensed value int sense, dist; sense = 0; dist = 0; while (usensor_t.read_ms() < 10000) { // Start the ultrasonic sensor usensor.start(); dist = usensor.get_dist_cm(); // If an object is detected based on out set range return 1 if (dist <= range && dist >= 1) { sense = 1; break; } else { sense = 0; MotorLeft.speed(speed); MotorRight.speed(-(speed)); } } return sense; }