Can anyone explain the C program
#include <conio.h>
#include <unistd.h>
#include <dbutton.h>
#include <dsensor.h>
#include <dmotor.h>
#include <sys/tm.h>
#include <rom/system.h>
#include <time.h>
#include <stdlib.h>
void motor_control(void);
void forward_run(void);//20
void direction_change(void);//20
void obstacle_handling(void);//20
void change_driving_command(int , enum dir_t, int, long int);
time_t time0;
enum dir_t{turn_left=0, turn_right, forward, backward, stop, puzzle };
struct driving_command
{
int priority;
enum dir_t direction;
int speed;
long int duration;
}command;
////////////////////////////////////////
// Main Function
int main()
{
srandom(sys_time);
command.priority = 0;
command.speed= 200;
command.duration = 100;
command.direction = forward;
time0 = sys_time;
// Infinite Loop
while(1)
{
forward_run();//20
direction_change();//15
obstacle_handling();//20
motor_control();
time0=time0 + 100;
msleep(time0-sys_time);
}
return 0;
}
/////////////////////////////////////////////////////
//Change Driving Command Function
void change_driving_command(int prio, enum dir_t dir, int speed, long
int dur)
{
if (command.priority <=prio)
{
command.priority = prio;
command.speed= speed;
command.duration = dur;
command.direction = dir;
}
}
////////////////////////////////////////////////////
//Obstacle Handling Function Uses Touch Sensors
void obstacle_handling(void)
{
if (TOUCH_1 !=0)
{
change_driving_command(2,1,100,20);//(int prio = 3, enum dir_t dir =
turn_right, int speed =100, long int dur = 25);
}
if (TOUCH_3 !=0)
{
change_driving_command(2,0,100,20);//(int prio = 3, enum dir_t dir =
turn_left, int speed = 100, long int dur = 25);
}
}
///////////////////////////////////////////////////
//Forward Run Function
void forward_run(void)
{
change_driving_command(0,2,255,20);//(int prio = 0, enum dir_t dir =
forward, int speed = 200, long int dur = 25);
}
///////////////////////////////////////////////////
// Directon Change Function Changes the direction of vehicle randomly
void direction_change(void)
{
int enter = random()%15;
int rnd;
if (enter ==5)
{
rnd = random()%6;
change_driving_command(1,rnd,150,15);//(int prio = 2, enum dir_t dir
= rnd, int speed = 200, long int dur = 20);
}
enter=rnd;
}
////////////////////////////////////////////////////
// Motor Control Function Controlling the Motors Directly
void motor_control(void)
{
if (command.direction==0) //Turning Vehicle Left;
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
if(command.duration>=15)
{
motor_a_dir(rev);
motor_c_dir(rev);
}
else
{
motor_a_dir(rev);
motor_c_dir(fwd);
}
}
else if (command.direction==1) //Turning Vehicle Right;
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
if (command.duration>=15)
{
motor_a_dir(rev);
motor_c_dir(rev);
}
else
{
motor_a_dir(fwd);
motor_c_dir(rev);
}
}
else if (command.direction==2)
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
motor_a_dir(fwd);
motor_c_dir(fwd);
}
else if (command.direction ==3)
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
motor_a_dir(rev);
motor_c_dir(rev);
}
else if (command.direction ==4)
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
motor_a_dir(brake);
motor_c_dir(brake);
}
else
{
motor_a_speed (command.speed);
motor_c_speed (command.speed);
if(command.duration%2==0)
{
motor_a_dir(fwd);
motor_c_dir(rev);
}
else
{
motor_a_dir(rev);
motor_c_dir(fwd);
}
}
if (command.duration>0)
{
command.duration = command.duration-1;
}
if(command.duration==0)
{
command.priority =0;
}
|