0

I'm new to using nXc to code my NXT 2.0 robot and I need it to follow other robots and check for obstacles at the same time. However, the two tasks sometimes conflict with each other and when I meet an obstacle it treats it as an object and continues moving forward instead of stopping.

Aim of my project: Follow a leader robot, stop when it meets an obstacle and after the obstacle is cleared, continue to follow the leader robot that is in front of it.

Below is my code:

    #define NEAR 50
    #define TURNTIME 540
    #define MOVETIME 1000

    mutex moveMutex;

    //check for robot in front of it
    task check_robot(void)
    {
         while(true)
         {
             if (SensorUS(IN_2)<=NEAR)
             {
                 Acquire(moveMutex);
                 OnFwd(OUT_BC,50);  //if it is within range of 50cm, move
                 Release(moveMutex);
             }
             if (SensorUS(IN_2)>NEAR)
             {
                 Acquire(moveMutex);  //this section allows the robot to turn
                 OnFwd(OUT_C,50);     //left and right to look for the 
                 Wait(TURNTIME);      //leader
                 OnRev(OUT_B,50);
                 Wait(TURNTIME);
                 OnRev(OUT_C,50);
                 Wait(TURNTIME);
                 OnFwd(OUT_B,50);
                 Wait(TURNTIME);
                 if (SensorUS(IN_2)<=NEAR)
                 {
                      Acquire(moveMutex);
                      OnFwd(OUT_BC,50);   //if it finds the leader, it moves
                      Release(moveMutex);
                 }
                 else
                 {
                      Acquire(moveMutex);
                      OnFwd(OUT_C,50);       //repeats the search process
                      Wait(TURNTIME);
                      OnRev(OUT_B,50);
                      Wait(TURNTIME);
                      OnRev(OUT_C,50);
                      Wait(TURNTIME);
                      OnFwd(OUT_B,50);
                      Wait(TURNTIME);
                      Release(moveMutex);
                 }
             }
             Release(moveMutex);
         }
    }

    task check_obstacles(void)
    {
         Acquire(moveMutex);
         until(((SENSOR_1)==1)||((SENSOR_3)==1));   //when any one of the
         OnRev(OUT_BC,50);                          //touch sensors are
         Wait(1000);                                //pressed, it is supposed
         Off(OUT_BC);                               //to stop until both
         until(((SENSOR_1)==0)&&((SENSOR_3)==0));   //are released, then
         OnFwd(OUT_BC,50);                          //continue moving
         Release(moveMutex);
    }

    task main(void)
    {
         SetSensorTouch(IN_1);
         SetSensorUltrasonic(IN_2);
         SetSensorTouch(IN_3);
         SetSensorColorFull(IN_4);

         while(true)
         {
             Precedes(check_robot,check_obstacles);
         }
    }

Any thoughts on how I can improve this? Would it be better to use subroutines instead of tasks and what is the benefit of using subroutines or tasks?

Sandra Ng
  • 1
  • 3
  • 2
    Create the smallest program you can that uses mutexes and that you have trouble with. Then post the code. Without code, it's hard for anyone to give a good answer. – Toby Speight Sep 21 '15 at 16:24
  • Have you seen [this page](http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/mutex.html)? It provides a clear explanation of when mutexes should be used, and provides an example. – shea Sep 22 '15 at 11:44
  • Thanks for your replies! Yes i have seen the page but i want to know exactly how a mutex works and the benefits of using mutexes over subroutines and vice versa. I have edited my code at the top! – Sandra Ng Sep 27 '15 at 03:54

1 Answers1

0

You can not use the nested function to acquire the same mutex variable that causes a crash in their task. Acquire function suspends the task until the mutex variable is released this link to read you understand how the tool works acquire. Sorry my English used the google tranlate. :)

  • Thanks Lucas! Does this mean i have to define different mutex variables for each task? Also, if i want the robot to do two things at the same time (in this case check for the leader and check for the black line) would it be better to use subroutines or mutexes if i don't want the tasks to clash? – Sandra Ng Sep 29 '15 at 06:06