Demo entry 6721530

12

   

Submitted by 12 on Mar 21, 2018 at 23:26
Language: C. Code size: 7.1 kB.

  1 #include <p30F6014A.h>
  2 #include <motor_led/e_epuck_ports.h>
  3 #include <motor_led/e_init_port.h>
  4 #include <a_d/advance_ad_scan/e_prox.h>
  5 #include <a_d/advance_ad_scan/e_ad_conv.h>
  6 #include <uart/e_uart_char.h>
  7 #include <stdio.h>
  8 
  9 void Wait(long);
 10 int GetSelector(void);
 11 char Buffer[100];
 12 char sensor[100];
 13 int Buff_Len; 
 14 int sensor_len;
 15 int i, t, deg,p;
 16 int min=0; 
 17 int max=7;
 18 
 19     static void go_forward() //the function make the robot go stright foward
 20     {
 21         e_set_speed_left(900); //set the left wheel speed to 900 
 22         e_set_speed_right(900); //set the right wheel speed to 900 
 23     }
 24     static void stop()  //the function make the robot stop for 0.5 second
 25     {
 26         e_set_speed_left(0); //set the left wheel speed to 0
 27         e_set_speed_right(0);//set the right wheel speed to 0
 28         Wait(500000);   // delay for 0.5 second 
 29     }
 30     static void go_back()//the function make the robot go stright backward
 31     {
 32         e_set_speed_left(-500);//set the left wheel speed to -500
 33         e_set_speed_right(-500);//set the right wheel speed to -500
 34         Wait(1000000); // delay for 1 second
 35     }
 36     
 37     static void turn_left()//the function make the robot turn left
 38     {
 39         e_set_speed_left(-500);//set the left wheel speed to -500
 40         e_set_speed_right(500);//set the right wheel speed to 500
 41     }
 42     
 43     static void turn_right()//the function make the robot turn right
 44     {
 45         e_set_speed_left(500);//set the left wheel speed to 500
 46         e_set_speed_right(-500);//set the right wheel speed to -500
 47     }
 48 
 49 int main()
 50 {
 51     // Initialization. 
 52     e_init_port(); // Initializes the ports.
 53     e_init_ad_scan(ALL_ADC); // Initializes the analogue to digital converters.
 54     e_led_clear(); // Turn off all the LEDs in the initialization stage.
 55     e_start_agendas_processing(); // Allows the robot to execute multiple tasks.
 56     e_init_uart1(); // Initializes the UART. Uncomment when you use Bluetooth communication.
 57     e_init_motors(); // Initializes the motors. Uncomment when you use the motors.
 58 
 59     //Proximity Sensors
 60     e_calibrate_ir(); // Uncomment when you use Proximity Sensors.
 61     
 62     while(1)
 63         {
 64         
 65         //Wheels
 66         go_forward(); //go stright forward in the beginning  
 67         if (e_get_prox(7)>400&&e_get_prox(7)>e_get_prox(0)) //the condition that the sensor 7 is too close to the obstacles and closer than the sensor 0
 68         {
 69             e_set_led(0,1);// turn on the LED 0
 70             e_set_led(7,1);// turn on the LED 7
 71             t = (rand() % 5);// random number t from 1 to 5
 72             Buff_Len = sprintf(Buffer, "Selector: %d\r\n", e_get_prox(7));// the robot send the value of sensor 7 throuth the bluetooth  
 73             e_send_uart1_char(Buffer, Buff_Len);
 74             stop(); // stop
 75             p=0;   //  a flag p to make sure the robot turn a unit angle t times
 76             while(p<=t)
 77                 {
 78                 	turn_right();//turn right for 1 second
 79                 	Wait(100000);
 80                     p++;
 81                 }
 82         }
 83         else
 84         {
 85             e_set_led(0,0);// turn off the LED 0
 86             e_set_led(7,0);// turn off the LED 7
 87         }
 88         if (e_get_prox(0)>400&&e_get_prox(0)>e_get_prox(7))//the condition that the sensor 0 is too close to the obstacles and closer than the sensor 7
 89         {
 90             e_set_led(0,1);// turn on the LED 0
 91             e_set_led(1,1);// turn on the LED 1
 92             t = (rand() % 5);// random number t from 1 to 5
 93             Buff_Len = sprintf(Buffer, "Selector: %d\r\n", e_get_prox(0));// the robot send the value of sensor 0 throuth the bluetooth  
 94             e_send_uart1_char(Buffer, Buff_Len);
 95             stop();// stop
 96             p=0;  // a flag p to make sure the robot turn a unit angle t times  
 97             while(p<=t)
 98                 {
 99                 	turn_left(); //turn left for 1 second
100                 	Wait(100000);
101                 	p++;
102                 }
103         }
104         else
105         {
106             e_set_led(0,0);// turn off the LED 0
107             e_set_led(1,0);// turn off the LED 1
108         }
109         if (e_get_prox(5)>1000)//the condition that the sensor 5 is too close to the obstacles
110         {
111             e_set_led(6,1);// turn on the LED 6
112             while(e_get_prox(5)>1100)//the condition that the sensor 5 is closer to the obstacles, in order to prevent the LED blink 
113                 {
114                 	turn_right();//turn right for 1 second
115                 	Wait(100000);
116                 }
117         }
118         else
119         {
120             e_set_led(6,0);// turn off the LED 6
121         }
122         if (e_get_prox(2)>1000)//the condition that the sensor 2 is too close to the obstacles
123         {
124             e_set_led(2,1);// turn on the LED 2
125             while(e_get_prox(2)>1100)//the condition that the sensor 2 is closer to the obstacles, in order to prevent the LED blink 
126                 {
127                 	turn_left();//turn left for 1 second
128                 	Wait(100000);
129                 }
130         }
131         else
132         {
133             e_set_led(2,0);// turn off the LED 2
134         }
135         if (e_get_prox(6)>800)//the condition that the sensor 6 is too close to the obstacles
136         {
137             e_set_led(7,1);// turn on the LED 7
138             while(e_get_prox(6)>900)//the condition that the sensor 6 is closer to the obstacles, in order to prevent the LED blink 
139                 {
140                 	turn_right();//turn right for 1 second
141                 	Wait(100000);
142                 }
143         }
144         else
145         {
146             e_set_led(7,0);// turn off the LED 7
147         }
148         if (e_get_prox(1)>800)//the condition that the sensor 1 is too close to the obstacles
149         {
150             e_set_led(1,1);// turn on the LED 1
151             while(e_get_prox(1)>900)//the condition that the sensor 1 is closer to the obstacles, in order to prevent the LED blink 
152                 {
153                 	turn_left();//turn left for 1 second
154                 	Wait(100000);
155                 }
156         }
157         else
158         {
159             e_set_led(1,0);// turn off the LED 1
160         }
161     }
162 }
163        
164 
165 void Wait(long Duration) // the function to delay 
166 {
167     long i;
168     for(i=0;i<Duration;i++);
169 }
170 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).