Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * Team Id : e-YRC#3105-FF
- * Author List : Abhinav Sarkar, Shivam Bhardwaj, Tapan Khattar, Dhruv Gaba
- * Filename : Final_Code
- * Theme : Fire-Fighting
- * Functions : motor_init, forward, backward, left, right, soft_left, soft_right, stopbot, linear_distance_mm, angle_rotate_degrees,forward_mm, backward_mm, left_degrees, right_degrees, soft_left_degrees, soft_right_degrees
- velocity, incrementright, incrementleft,catchline_left, catchline_right, followlinetillbbw,followlinetillwbb,followlinetillbbb,followlinetillbbworwbb,followlinetillbbworbbb, followlinetillwbborbbb,
- servod, servof, drop_mgnet, fire_detection, room_entrance_algo, room_traversal, setup, loop
- * Global Variables : left_motor_1, right_motor_1,left_motor_2, right_motor_2,left_PWM, right_PWM, threshold, fire_detection_flag, room_entrance_flag, left_sensor, center_sensor, right_sensor
- leftshaftcount,rightshaftcount,m1,m2,m3, magnet_number, buzzer
- *
- *
- */
- #include <LiquidCrystal.h> // Including Liquid crystal Library for displaying information on the Firebird V LCD
- #define f5 255 // Highest PWM value for speed of dc motors
- #define f4 240 // Next PWM value for dc motor (mainly for inroom traversal)
- LiquidCrystal lcd(37, 35, 33, 32, 31, 30);
- int left_motor_1 = 22;
- int left_motor_2 = 23;
- int right_motor_1 = 25;
- int right_motor_2 = 24;
- int left_PWM = 46;
- int right_PWM = 45;
- int threshold = 33;
- int fire_detection_flag;
- int magnet_number= 0;
- int left_sensor, center_sensor , right_sensor, left_sharp;
- volatile unsigned long int leftshaftcount = 0;
- volatile unsigned long int rightshaftcount = 0;
- boolean m1, m2, m3; // These variables display the surface detected by line sensors. They are 1 if white is detected and 0 if black is detected
- boolean room_entrance_flag;
- /*
- *
- * Function Name : motor_init
- * Input : None
- * Output : None
- * Logic : This function initialises all the motor pins as output
- * Example Call : motor_init();
- *
- */
- void motor_init()
- {
- pinMode(left_motor_1, OUTPUT);
- pinMode(left_motor_2, OUTPUT);
- pinMode(right_motor_1, OUTPUT);
- pinMode(right_motor_2, OUTPUT);
- }
- /*
- *
- * Function Name : forward
- * Input : None
- * Output : None
- * Logic : This function makes both the motors rotate in forward direction hence allowing robot to move forward.
- * Example Call : forward();
- *
- */
- void forward()
- {
- digitalWrite(left_motor_1, 0);
- digitalWrite(left_motor_2, 1);
- digitalWrite(right_motor_1, 0);
- digitalWrite(right_motor_2, 1);
- }
- /*
- *
- * Function Name : backward
- * Input : None
- * Output : None
- * Logic : This function makes both the motors rotate in backward direction hence allowing robot to move backward.
- * Example Call : backward();
- *
- */
- void backward()
- {
- digitalWrite(left_motor_1, 1);
- digitalWrite(left_motor_2, 0);
- digitalWrite(right_motor_1, 1);
- digitalWrite(right_motor_2, 0);
- }
- /*
- *
- * Function Name : left
- * Input : None
- * Output : None
- * Logic : This function makes right motor rotate forward and left motor rotate backwards. Hence robot turns left.
- * Example Call : left();
- *
- */
- void left()
- {
- digitalWrite(left_motor_1, 1);
- digitalWrite(left_motor_2, 0);
- digitalWrite(right_motor_1, 0);
- digitalWrite(right_motor_2, 1);
- }
- /*
- *
- * Function Name : right
- * Input : None
- * Output : None
- * Logic : This function makes left motor rotate forward and right motor rotate backwards. Hence robot turns right.
- * Example Call : right();
- *
- */
- void right()
- {
- digitalWrite(left_motor_1, 0);
- digitalWrite(left_motor_2, 1);
- digitalWrite(right_motor_1, 1);
- digitalWrite(right_motor_2, 0);
- }
- /*
- *
- * Function Name : soft_left
- * Input : None
- * Output : None
- * Logic : This function makes right motor rotate forward and left motor stationary. Hence robot turns left but along the axis of left wheel.
- * Example Call : soft_left();
- *
- */
- void soft_left()
- {
- digitalWrite(left_motor_1, 0);
- digitalWrite(left_motor_2, 0);
- digitalWrite(right_motor_1, 0);
- digitalWrite(right_motor_2, 1);
- }
- /*
- *
- * Function Name : soft_right
- * Input : None
- * Output : None
- * Logic : This function makes left motor rotate forward and right motor stationary. Hence robot turns right but along the axis of right wheel.
- * Example Call : soft_right();
- *
- */
- void soft_right()
- {
- digitalWrite(left_motor_1, 0);
- digitalWrite(left_motor_2, 1);
- digitalWrite(right_motor_1, 0);
- digitalWrite(right_motor_2, 0);
- }
- /*
- *
- * Function Name : stopbot
- * Input : None
- * Output : None
- * Logic : This function makes both the motors stationary hence allowing robot to stop.
- * Example Call : stopbot();
- *
- */
- void stopbot()
- {
- digitalWrite(left_motor_1, 0);
- digitalWrite(left_motor_2, 0);
- digitalWrite(right_motor_1, 0);
- digitalWrite(right_motor_2, 0);
- }
- /*
- *
- * Function Name : velocity
- * Input : left_speed, right_speed
- * Output : None
- * Logic : This function sets the PWM values for both motors hence allowing us to control the rotation speed if each motor. The PWM values can change from 0-255 for 8-bit PWM
- * Example Call : velocity(150,150);
- *
- */
- void velocity(int left_speed, int right_speed)
- {
- analogWrite(left_PWM, left_speed);
- analogWrite(right_PWM, right_speed);
- }
- /*
- *
- * Function Name : incrementright
- * Input : None
- * Output : None
- * Logic : This is an interrupt service routine which increments a counter, rightshaftcount everytime the right motor position encoder detects a new pulse.
- * Example Call : This is an ISR so it is not called explicitly by us. Instead it is called implicitly by the program everytime interrupt occurs
- *
- */
- void incrementright()
- {
- rightshaftcount++;
- }
- /*
- *
- * Function Name : incrementleft
- * Input : None
- * Output : None
- * Logic : This is an interrupt service routine which increments a counter, leftshaftcount everytime the left motor position encoder detects a new pulse.
- * Example Call : This is an ISR so it is not called explicitly by us. Instead it is called implicitly by the program everytime interrupt occurs
- *
- */
- void incrementleft()
- {
- leftshaftcount++;
- }
- /*
- *
- * Function Name : linear_distance_mm
- * Input : distance_mm
- * Output : None
- * Logic : This function takes a distance value (in mm) and converts it into number of pulses required to cover that distance. It then sets right shaft count to zero
- and counts until it exceeds the required number of pulses.
- * Example Call : linear_mm(100);
- *
- */
- void linear_distance_mm(unsigned int distance_mm)
- {
- float reqdshaftcount = 0;
- unsigned long int Reqdshaftcount = 0;
- reqdshaftcount = distance_mm / 5.44;
- Reqdshaftcount = (unsigned long int) reqdshaftcount;
- rightshaftcount = 0;
- while (1) {
- if (rightshaftcount > Reqdshaftcount)
- {
- break;
- }
- }
- stopbot();
- }
- /*
- *
- * Function Name : angle_rotate_degrees
- * Input : degree
- * Output : None
- * Logic : This function takes angle to rotate in degrees and converts it into number of pulses required to cover that angle. It then sets both leftshaftcount and rightshaftcount
- to zero and counts until one of the two counters exceeds the required value.
- * Example Call : angle_rotate_degrees(90);
- *
- */
- void angle_rotate_degrees(unsigned int degree)
- {
- float reqdshaftcount = 0;
- unsigned long int Reqdshaftcount = 0;
- reqdshaftcount = (float) degree / 4.090;
- Reqdshaftcount = (unsigned int) reqdshaftcount;
- rightshaftcount = 0;
- leftshaftcount = 0;
- while (1)
- {
- if ((rightshaftcount >= Reqdshaftcount) | (leftshaftcount >= Reqdshaftcount))
- {
- break;
- }
- }
- stopbot();
- }
- /*
- *
- * Function Name : forward_mm
- * Input : distance_mm
- * Output : None
- * Logic : Moves the robot forward by a required distance by first setting motors in forward mode using forward() function and then counting till required number of pulses using linear_distance_mm()
- * Example Call : forward_mm(500);
- *
- */
- void forward_mm(unsigned int distance_mm)
- {
- forward();
- linear_distance_mm(distance_mm);
- }
- /*
- *
- * Function Name : backward_mm
- * Input : distance_mm
- * Output : None
- * Logic : Moves the robot backward by a required distance by first setting motors in backward mode using backward() function and then counting till required number of pulses using linear_distance_mm()
- * Example Call : backward_mm(500);
- *
- */
- void backward_mm(unsigned int distance_mm)
- {
- backward();
- linear_distance_mm(distance_mm);
- }
- /*
- *
- * Function Name : left_degrees
- * Input : degree
- * Output : None
- * Logic : Turns the robot left by required angle by first setting motors in left mode using left() function and then counting till required number of pulses using angle_rotate_degrees
- * Example Call : left_degrees(90);
- *
- */
- void left_degrees(unsigned int degree)
- {
- left();
- angle_rotate_degrees(degree);
- }
- /*
- *
- * Function Name : right_degrees
- * Input : degree
- * Output : None
- * Logic : Turns the robot right by required angle by first setting motors in right mode using right() function and then counting till required number of pulses using angle_rotate_degrees
- * Example Call : right_degrees(90);
- *
- */
- void right_degrees(unsigned int degree)
- {
- right();
- angle_rotate_degrees(degree);
- }
- /*
- *
- * Function Name : soft_left_degrees
- * Input : degree
- * Output : None
- * Logic : Turns the robot soft left by required angle by first setting motors in soft left mode using soft_left() function and then counting till required number of pulses using angle_rotate_degrees
- * Example Call : soft_left_degrees(180);
- *
- */
- void soft_left_degrees(unsigned int degree)
- {
- soft_left();
- angle_rotate_degrees(degree);
- }
- /*
- *
- * Function Name : soft_right_degrees
- * Input : degree
- * Output : None
- * Logic : Turns the robot soft right by required angle by first setting motors in soft right mode using soft_right() function and then counting till required number of pulses using angle_rotate_degrees
- * Example Call : soft_right_degrees(180);
- *
- */
- void soft_right_degrees(unsigned int degree)
- {
- soft_right();
- angle_rotate_degrees(degree);
- }
- /*
- *
- * Function Name : whitelineshow
- * Input : None
- * Output : None
- * Logic : This function first reads the analog value of the whiteline sensors for line following and stores them in variables left_sensor, center_sensor, right_sensor and then
- prints them on the lcd screen. It then compares each sensor value to the threshold and sets boolean variables m1, m2, m3 as 0 if sensor value is greater then threshold
- i.e black surface is detected and 1 if sensor value is less then threshold i.e white surface is detected. It also prints 'B' or 'W' on the lcd screen depending on value so as to make
- debugging easy.
- * Example Call : whitelineshow();
- *
- */
- void whitelineshow()
- {
- left_sensor = analogRead(A3); // Checks left sensor value
- center_sensor = analogRead(A2); // Checks center sensor value
- right_sensor = analogRead(A1); // Checks right sensor value
- lcd.setCursor(0, 0);
- lcd.print(left_sensor);
- lcd.setCursor(0, 1);
- if (left_sensor < threshold) {
- lcd.print("W");
- m1 = 1; // Sets boolean variable m1=1 if white is detected
- }
- else
- {
- lcd.print("B");
- m1 = 0; // Sets boolean variable m1=0 if black is detected
- }
- lcd.setCursor(4, 1);
- if (center_sensor < threshold) {
- lcd.print("W");
- m2 = 1; // Sets boolean variable m2=1 if white is detected
- }
- else
- {
- lcd.print("B");
- m2 = 0; // Sets boolean variable m2=0 if black is detected
- }
- lcd.setCursor(4, 0);
- lcd.print(center_sensor);
- lcd.setCursor(8, 1);
- if (right_sensor < threshold) {
- lcd.print("W");
- m3 = 1; // Sets boolean variable m3=1 if white is detected
- }
- else
- {
- lcd.print("B");
- m3 = 0; // Sets boolean variable m3=0 if black is detected
- }
- lcd.setCursor(8, 0);
- lcd.print(right_sensor);
- }
- /*
- *
- * Function Name : catchline_left
- * Input : None
- * Output : None
- * Logic : Turns the robot soft left until the center sensors detect black surface. This function is generally used to catch the line again if the robot strays to the right
- side of the line
- * Example Call : catchline_left();
- *
- */
- void catchline_left()
- {
- whitelineshow();
- while (!(m2 == 0 )) // Loop goes on until center sensor detects black
- {
- soft_left();
- whitelineshow();
- }
- stopbot();
- }
- /*
- *
- * Function Name : catchline_right
- * Input : None
- * Output : None
- * Logic : Turns the robot soft right until the center sensors detect black surface. This function is generally used to catch the line again if the robot strays to the left
- side of the line
- * Example Call : catchline_left();
- *
- */
- void catchline_right()
- {
- whitelineshow();
- while (!(m2 == 0 )) // Loop goes on until center sensor detects black
- {
- soft_right();
- whitelineshow();
- }
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillbbw
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line until the left and center sensors detect black surface and right sensor detects white surface. This condition can also
- be called BBW condition.
- * Example Call : followlinetillbbw();
- *
- */
- void followlinetillbbw()
- {
- whitelineshow();
- while (!(m1 == 0 && m2 == 0 && m3 == 1)) // Loop goes on until BBW condition is met
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WBB
- {
- right();
- }
- else if ((m1 == 0 && m2 == 0 && m3 == 0) || (m1 == 1 && m2 == 1 && m3 == 1) ) // BBB or WWW
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5); // Robot moves 5mm ahead
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillwbb
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line until the right and center sensors detect black surface and left sensor detects white surface. This condition can also
- be called WBB condition.
- * Example Call : followlinetillwbb();
- *
- */
- void followlinetillwbb()
- {
- whitelineshow();
- while (!(m1 == 1 && m2 == 0 && m3 == 0)) // Loop goes on until WBB condition is met
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WWB
- {
- right();
- }
- else if ((m1 == 0 && m2 == 0 && m3 == 0) || (m1 == 1 && m2 == 1 && m3 == 1) ) // BBB or WWW
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5);
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillbbb
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line until the left and center sensors detect black surface and right sensor detects white surface. This condition can also
- be called BBB condition.
- * Example Call : followlinetillbbb();
- *
- */
- void followlinetillbbb()
- {
- whitelineshow();
- while (!(m1 == 0 && m2 == 0 && m3 == 0)) // Loop goes on until BBB condition is met
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WWB
- {
- right();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 1) // WWW
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5);
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillwbborbbb
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line till either WBB condition or BBB condition is detected by the sensors. This kind of function is useful when there is
- ambiguity on the kind of condition arising
- * Example Call : followlinetillwbborbbb();
- *
- */
- void followlinetillwbborbbb()
- {
- whitelineshow();
- while (!((m1 == 1 && m2 == 0 && m3 == 0) || (m1 == 0 && m2 == 0 && m3 == 0))) // Loop goes on until either WBB or BBB condition is met
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WWB
- {
- right();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 1) // WWW
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5);
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillbbworbbb
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line till either BBW condition or BBB condition is detected by the sensors. This kind of function is useful when there is
- ambiguity on the kind of condition arising
- * Example Call : followlinetillbbworbbb();
- *
- */
- void followlinetillbbworbbb()
- {
- whitelineshow();
- while (!((m1 == 1 && m2 == 0 && m3 == 0) || (m1 == 0 && m2 == 0 && m3 == 0))) // Loop goes on until either BBW or BBB condition is met
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WWB
- {
- right();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 1) // BBB
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5);
- stopbot();
- }
- /*
- *
- * Function Name : followlinetillbbworwbb
- * Input : None
- * Output : None
- * Logic : This function makes the robot follow the line till either BBW condition or WBB condition is detected by the sensors. This kind of function is useful when there is
- ambiguity on the kind of condition arising. Eg. Node
- * Example Call : followlinetillbbworwbb();
- *
- */
- void followlinetillbbworwbb()
- {
- whitelineshow();
- while (!((m1 == 0 && m2 == 0 && m3 == 1) || (m1 == 1 && m2 == 0 && m3 == 0))) // Loop goes on untill BBW or WBB condition is met.
- {
- if ((m1 == 1 && m2 == 0 && m3 == 1) || m1 == 0 && m2 == 1 && m3 == 0) // WBW or BWB
- {
- forward();
- }
- else if (m1 == 0 && m2 == 1 && m3 == 1 ) // BWW
- {
- left();
- }
- else if (m1 == 1 && m2 == 1 && m3 == 0) // WWB
- {
- right();
- }
- else if ((m1 == 0 && m2 == 0 && m3 == 0) || (m1 == 1 && m2 == 1 && m3 == 1) ) // BBB or WWW
- {
- forward_mm(5);
- }
- whitelineshow();
- }
- forward_mm(5);
- stopbot();
- }
- /*
- *
- * Function Name : servod
- * Input : degree
- * Output : None
- * Logic : This function generates a signal to direct the magnet dispenser servo to move to a certain angle.(Note:- We had problem using the arduino servo library and the motor
- functions together. So we created our own servo function)
- * Example Call : servod(90);
- *
- */
- void servod(int degree)
- {
- int t = 10.05 * degree + 530; // Mapping function for magnet dispenser servo. If we put the number of degrees in the equation, it generates the microsecond delay required for the servo
- for (int i = 0; i < 5; i++)
- {
- digitalWrite(12, HIGH) ;
- delayMicroseconds(t);
- digitalWrite(12, LOW) ;
- delay(20);
- }
- }
- /*
- *
- * Function Name : servof
- * Input : degree
- * Output : None
- * Logic : This function generates a signal to direct the fire sensor servo to move to a certain angle.(Note:- We had problem using the arduino servo library and the motor
- functions together. So we created our own servo function)
- * Example Call : servof(90);
- *
- */
- void servof(int n)
- {
- int angle = 8.33 * n + 560; // Mapping function for fire sensor servo. If we put the number of degrees in the equation, it generates the microsecond delay required for the servo
- for (int i = 0; i < 2; i++)
- {
- digitalWrite(11, HIGH);
- delayMicroseconds(angle);
- digitalWrite(11, LOW);
- delay(20);
- }
- }
- /*
- *
- * Function Name : drop_magnet
- * Input : magnet_number
- * Output : None
- * Logic : Upto four magnets are loaded in the magnet dispenser. Putting the magnet_number makes the servo rotate to a certain angle, hence dropping the magnet.
- * Example Call : drop_magnet(2);
- *
- */
- void drop_magnet(int magnet_number)
- {
- switch (magnet_number)
- {
- case 1: { // Drop magnet 1
- for (int i = 0; i < 40; i += 5)
- {
- servod(i);
- }
- break;
- }
- case 2: { // Drop magnet 2
- for (int i = 33; i <= 80; i += 5)
- {
- servod(i);
- }
- break;
- }
- case 3: { // Drop magnet 3
- for (int i = 77; i <= 120; i += 5)
- {
- servod(i);
- }
- break;
- }
- case 4: { // Drop magnet 4
- for (int i = 113; i <= 160; i += 5)
- {
- servod(i);
- }
- break;
- }
- default: servod(0); // Reset magnet dispenser servo to original position.
- }
- }
- /*
- *
- * Function Name : fire_detection
- * Input : room entrance
- * Output : None
- * Logic : This function first checks the value of room entrance flag. If room entrance flag is 0, then it means door position is d2 and fire servo sweeps clockwise from 80 to 180
- degrees searching for fire.When a fire is detected, it breaks out of the for loop and checks the position of fire by running a counter through a sequence of if-else conditions
- and returns a number according to position of fire with respect to the robots position. In case the room entrance flag = 1, the fire servo sweeps anticlockwise from 100 to 0
- degrees. In this case also the same process is followed.
- * Example Call : fire_detection(0);
- *
- */
- int fire_detection( boolean room_entrance)
- {
- int counter;
- if( room_entrance == 0) // If door position is d2
- {
- servof(70); // Sets initial Servo angle
- for( counter = 70 ; counter <= 180 ; counter++)
- {
- if( analogRead(A12) < 800 ) // A12 is pin on which fire sensor is connected. 800 is a threshold value under normal room lighting conditions. If analog value is less then 800, fire has been detected.
- {
- buzzer(1000); // Buzzer beep for 1 second
- break;
- }
- }
- if( counter > 80 && counter <=115)
- {
- return 1; // Fire in front of robot.
- }
- else if( counter > 115 && counter <= 155)
- {
- return 2; // Fire is placed diagonally to the right of robot
- }
- else if( counter > 155 and counter < 180)
- {
- return 3; // Fire is placed on the right side of the robot
- }
- else
- {
- return 0; // No fire present
- }
- }
- else // If door position is d1
- {
- servof(100);
- for( counter = 100 ; counter >= 0 ; counter--)
- {
- if( analogRead(A12) < 800 ) // If fire is detected
- {
- buzzer(1000); // Buzzer beep for 1 second
- break;
- }
- }
- if( counter > 0 && counter <=30)
- {
- return 3; // Fire is placed on the left side of the robot
- }
- else if( counter > 30 && counter <= 60)
- {
- return 2; // Fire is placed diagonally to the left of robot
- }
- else if( counter > 60 and counter < 100)
- {
- return 1; // Fire in front of robot.
- }
- else
- {
- return 0; // No fire present
- }
- }
- }
- /*
- *
- * Function Name : room_entrance_algo
- * Input : None
- * Output : None
- * Logic : Room_entrance_algo is called when the robot is at home. The robot moves forward and checks if there is a door. Depending on the condition, it either turns left and enters room
- in case of d2 or moves forward , turns left and then enters room in case of d1. In both cases the robot, moves till the first T-point in the room and then calls the fire detection
- algorithm and stores value returned in fire detection flag. Based on this the room traversal algorithm takes place
- degrees. In this case also the same process is followed.
- * Example Call : fire_detection(0);
- *
- */
- void room_entrance_algo()
- {
- forward_mm(550); // Move forward 55cm
- left_sharp = analogRead(A9); // The left sharp sensor is connected on pin A9. This line stores the value in the variable left sharp
- if(left_sharp < 500) // If door is detected
- {
- delay(250);
- room_entrance_flag = 0; // door is D2
- left_degrees(90); // Turn left 90 degrees.
- forward_mm(120); // Move forward 12 cm
- catchline_left(); // Move soft left until robot catches line i.e center sensors detect black
- followlinetillwbb(); // Follow line until the first T-point in room is detected. The sensor should detect WBB when they are on this T-point.
- fire_detection_flag = fire_detection(room_entrance_flag); // Call fire detection algorithm and store returned value in fire detection flag.
- }
- else // door is D1
- {
- delay(250);
- forward_mm(400); // Move forward 40cm more
- left_degrees(100); // Turn left 100 degrees
- forward_mm(180); // Move forward 18 cm
- catchline_left(); // Move soft left until robot catches line
- followlinetillbbw(); // Follow line until first T-point in room is detected. The sensors should detect BBW condition here.
- fire_detection_flag = fire_detection(room_entrance_flag); // Call fire detection algorithm and store returned value in fire detection flag.
- }
- room_traversal(room_entrance_flag, fire_detection_flag);
- }
- /*
- *
- * Function Name : room_traversal
- * Input : room_entrance, fire_Detection
- * Output : None
- * Logic : This function takes values of 2 flags and based on this values the robot traverses the whole room using a sequence of steps
- * Example Call : room_traversal(0,0);
- *
- */
- void room_traversal(boolean room_entrance, int fire_detection)
- {
- if(room_entrance == 0 && fire_detection == 0) // door D2 and no fire
- {
- right_degrees(180); // Turn right 180 degrees
- backward_mm(80); // Move backward 8cm
- velocity(f2, f2 - 5); // Set velocity to f2
- for (int i = 0; i < 3; i++) // Iterate this loop 3 times(this is used to set the bot in orientation with the line)
- {
- catchline_right(); // Turn right till robot catches line
- forward_mm(20); // Move forward 2cm
- }
- forward_mm(300); // Move forward 30cm
- right_degrees(90); // Turn right 90 degrees
- forward_mm(230); // Move forward 23cm
- catchline_right(); // Turn right till robot catches line
- velocity(f3, f3 - 5); // Set velocity to f3
- followlinetillbbb(); // Follow line till the next node ( black square at home)
- }
- else if(room_entrance == 0 && fire_detection == 1) // door D2 and fire on front
- {
- right_degrees(20); // Turn right 20 degrees
- forward_mm(60); // Move forward 6cm
- catchline_left(); // move left till robot catches line
- followlinetillbbworwbb(); // Follow line till the next node ( black square)
- backward_mm(10); // Move back 1cm to position magnet dispenser over deposition slot
- drop_magnet(magnet_number); // Drop next magnet
- magnet_number++;
- right_degrees(180); // Turn sharp right 180 degrees
- backward_mm(70); // Move back 7cm
- velocity(f4, f4-5); // Set velocity to f2
- catchline_right(); // Move right till robot catches right
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(10); // Move 1 cm forward
- followlinetillbbworwbb(); // Followline till next node or T-point
- velocity(f5, f5 - 5); // Set velocity to f5
- forward_mm(550); // Move forward 55cm to exit room
- right_degrees(90); // Turn right 90 degrees
- forward_mm(210); // Move forward 21cm
- catchline_right(); // Move right till robot catches line
- velocity(f4, f4 - 5); // Set velocity to f3
- followlinetillbbb(); // Followline till robot returns to home/ center node
- }
- else if(room_entrance == 0 && fire_detection == 2) // door D2 and fire diagonally to the right
- {
- right_degrees(20); // Turn right 20 degrees
- forward_mm(60); // Move forward 6cm
- catchline_left(); // Move left to catch line
- velocity(f4, f4-5); // Set velocity to f4
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(55); // Move forward 5.5cm
- soft_right_degrees(180); // Turn soft right 180 degree
- forward_mm(40); // Move forward 4cm
- catchline_left(); // Catch line left
- followlinetillbbworwbb(); // Move till next node
- backward_mm(10); // Move back 1cm to position magnet dispenser
- drop_magnet(magnet_number); // Drop magnet
- magnet_number++;
- right_degrees(220); // Turn right 220 degree
- catchline_left(); // Catch line to the left
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(55); // Forward 5.5cm
- soft_left_degrees(200); // Turn left
- backward_mm(30); // Move back 3cm
- catchline_right();
- followlinetillbbworwbb(); // Follow line till next node
- velocity(f5,f5-5 ); // Set velocity to f5
- forward_mm(575); // Move forward to exit room
- right_degrees(90); // Turn right 90 degrees
- forward_mm(210); // Forward 21cm
- catchline_right(); // Catch line right
- velocity(f4, f4-5 ); // Set velocity to f4
- followlinetillbbb(); // Follow line till robot reaches home node
- }
- else if(room_entrance == 0 && fire_detection == 3) // door D2 and fire on the right side
- {
- right_degrees(100); // Turn right 100 degrees
- forward_mm(60); // Move forward 6cm
- catchline_left(); // move left till robot catches line
- followlinetillbbworwbb(); // Follow line till the next node ( black square)
- backward_mm(10); // Move back 1cm to position magnet dispenser over deposition slot
- drop_magnet(magnet_number); // Drop next magnet
- magnet_number++;
- left_degrees(180); // Turn sharp left 180 degrees
- backward_mm(70); // Move back 7cm
- velocity(f2, f2 - 5); // Set velocity to f2
- catchline_left(); // Move left till robot catches left
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(10); // Move 1 cm forward
- followlinetillbbworwbb(); // Followline till next node or T-point
- left_degrees(90); // Turn sharp right 90 degrees
- forward_mm(300); // Move forward 30cm
- left_degrees(90); // Turn left 90 degrees
- forward_mm(230); // Move forward 23cm
- catchline_right(); // Turn right till robot catches line
- velocity(f3, f3 - 5); // Set velocity to f3
- followlinetillbbb(); // Follow line till the next node ( black square at home)
- }
- else if(room_entrance == 1 && fire_detection == 0) // door D1 and no fire
- {
- left_degrees(180); // Turn left 180 degrees
- backward_mm(80); // Move backward 8cm
- velocity(f2, f2 - 5); // Set velocity to f2
- for (int i = 0; i < 3; i++) // Iterate this loop 3 times(this is used to set the bot in orientation with the line)
- {
- catchline_left(); // Turn left till robot catches line
- forward_mm(20); // Move forward 2cm
- }
- forward_mm(300); // Move forward 30cm
- right_degrees(90); // Turn right 90 degrees
- forward_mm(760); // Move forward 76cm
- catchline_right(); // Turn right till robot catches line
- velocity(f3, f3 - 5); // Set velocity to f3
- followlinetillbbb(); // Follow line till the next node ( black square at home)
- }
- }
- else if(room_entrance == 1 && fire_detection == 1) // door D1 and fire on front
- {
- left_degrees(20); // Turn left 20 degrees
- forward_mm(60); // Move forward 6cm
- catchline_right(); // Move right till robot catches line
- followlinetillbbworwbb(); // Follow line till the next node ( black square)
- backward_mm(10); // Move back 1cm to position magnet dispenser over deposition slot
- drop_magnet(magnet_number); // Drop next magnet
- magnet_number++
- left_degrees(180); // Turn sharp left 180 degrees
- backward_mm(70); // Move back 7cm
- velocity(f2, f2-5); // Set velocity to f2
- catchline_left(); // Move left till robot catches right
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(10); // Move 1 cm forward
- followlinetillbbworwbb(); // Followline till next node or T-point
- velocity(f5, f5 - 5); // Set velocity to f5
- forward_mm(550); // Move forward 55cm to exit room
- right_degrees(90); // Turn right 90 degrees
- forward_mm(760); // Move forward 95cm
- catchline_right(); // Move right till robot catches line
- velocity(f3, f3 - 5); // Set velocity to f3
- followlinetillbbb(); // Followline till robot returns to home/ center node
- }
- else if(room_entrance == 1 && fire_detection == 2) // door D1 and fire diagonally to the left
- {
- left_degrees(20); // Turn left 20 degrees
- forward_mm(60); // Move forward 6cm
- catchline_right(); // Move right to catch line
- velocity(f4, f4-5); // Set velocity to f4
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(55); // Move forward 5.5cm
- soft_left_degrees(180); // Turn soft left 180 degree
- forward_mm(40); // Move forward 4cm
- catchline_right(); // Catch line right
- followlinetillbbworwbb(); // Move till next node
- backward_mm(10); // Move back 1cm to position magnet dispenser
- drop_magnet(a); // Drop magnet
- left_degrees(220); // Turn left 220 degree
- catchline_right(); // Catch line to the left
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(55); // Forward 5.5cm
- soft_right_degrees(200); // Turn right
- backward_mm(30); // Move back 3cm
- catchline_right();
- followlinetillbbworwbb(); // Follow line till next node
- velocity(f5,f5-5 ); // Set velocity to f5
- forward_mm(575); // Move forward to exit room
- right_degrees(90); // Turn right 90 degrees
- forward_mm(760); // Forward 76cm
- catchline_right(); // Catch line right
- velocity(f4, f4-5 ); // Set velocity to f4
- followlinetillbbb(); // Follow line till robot reaches home node
- }
- else if(room_entrance == 1 && fire_detection == 3) // door D1 and fire on the left side
- {
- left_degrees(100); // Turn left 100 degrees
- forward_mm(60); // Move forward 6cm
- catchline_right(); // move right till robot catches line
- followlinetillbbworwbb(); // Follow line till the next node ( black square)
- backward_mm(10); // Move back 1cm to position magnet dispenser over deposition slot
- drop_magnet(magnet_number); // Drop next magnet
- magnet_number++;
- right_degrees(180); // Turn sharp right 180 degrees
- backward_mm(70); // Move back 7cm
- velocity(f2, f2 - 5); // Set velocity to f2
- catchline_right(); // Move right till robot catches right
- followlinetillbbworwbb(); // Follow line till next node
- forward_mm(10); // Move 1 cm forward
- followlinetillbbworwbb(); // Followline till next node or T-point
- left_degrees(90); // Turn sharp right 90 degrees
- forward_mm(300); // Move forward 30cm
- right_degrees(90); // Turn right 90 degrees
- forward_mm(760); // Move forward 76cm
- catchline_right(); // Turn right till robot catches line
- velocity(f3, f3 - 5); // Set velocity to f3
- followlinetillbbb(); // Follow line till the next node ( black square at home)
- }
- }
- /*
- *
- * Function Name : buzzer
- * Input : n
- * Output : None
- * Logic : This function takes number of milliseconds and beeps buzzer for that duration
- * Example Call : buzzer(1000);
- *
- */
- void buzzer(int n)
- {
- digitalWrite(34, HIGH);
- delay(n);
- digitalWrite(34, LOW);
- }
- /*
- *
- * Function Name : setup
- * Input : None
- * Output : None
- * Logic : This function initialises all the pins and interrupts and the main code also takes place here
- * Example Call : N/A
- *
- */
- void setup()
- {
- pinMode(36, OUTPUT);
- digitalWrite(36, 0); //Pin on which LCD RW pin in connected. It is set as LOW
- lcd.begin(16, 2);
- pinMode(34, OUTPUT);
- pinMode(11, OUTPUT); // Pin on which fire servo is connected
- pinMode(12, OUTPUT); // Pin on which magnet dispenser servo is connected
- motor_init();
- attachInterrupt(1, incrementright , FALLING); //Right encoder interrupt attach
- attachInterrupt(0, incrementleft, FALLING); // Left encoder interupt attach
- servod(0); // Reset both servos
- servof(0);
- /******************************************************************************************/
- room_entrance_algo(); // Traverse first room
- room_entrance_algo(); // Traverse second room
- forward_mm(60);
- right_degrees(100);
- room_entrance_algo(); // Traverse third room
- room_entrance_algo(); // Traverse fourth room
- buzzer(5000);
- }
- /*
- *
- * Function Name : loop
- * Input : None
- * Output : None
- * Logic : No code is used here
- * Example Call : N/A
- *
- */
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement