Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   Can anyone explain the C program (https://www.askmehelpdesk.com/showthread.php?t=155824)

  • Nov 25, 2007, 01:54 PM
    divya_nuthakki
    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;
    }
  • Nov 25, 2007, 02:05 PM
    asterisk_man
    What sort of question is this? Either it's homework, in which case we don't do your homework for you but we can help with specific difficulties you're having, or... well I can't really think of anything else it could really be.
  • Nov 26, 2007, 11:16 AM
    divya_nuthakki
    Quote:

    Originally Posted by asterisk_man
    What sort of question is this? Either it's homework, in which case we don't do your homework for you but we can help with specific difficulties you're having, or ... well I can't really think of anything else it could really be.

    Hello,
    Its an programm given by our teacher. But I'm new for C programming. If possibl u can explain
  • Nov 26, 2007, 01:19 PM
    asterisk_man
    I won't explain each line. I will answer specific questions you might ask though.
  • Nov 26, 2007, 02:16 PM
    ScottGem
    Quote:

    Originally Posted by divya_nuthakki
    hello,
    its an programm given by our teacher. but im new for C programming. if possibl u can explain

    What about it don't you understand. Even if you are new, your teacher wouldn't have given it toyou if he didn't feel you could decipher it. Take it line by line and try to deciper it. If you have specific questions ask.

  • All times are GMT -7. The time now is 09:29 AM.