New to programming and Mindstorm

Discussion specific to projects ideas and support.

New to programming and Mindstorm

Postby fcrx7 » 04 Jun 2013, 22:09

Hi there, I hope this is the right place to post this. As stated I am fairly new to NXC and also fairly new to the mindstorm robots (where have I been you may ask!).
I have been tasked with compiling a program which will allow a robot to perform several functions at the same time. From the research I have done on google, Mr. Benedettelli's site and here I have come up with a program which I hope almost does what I want it to, I am however having a few bugs with it and dont really have anyone else to turn to. I hope someone here can help.

Below is program as I have written it so far, any advice, alterations and or comments would be appreciated. I would like to achieve the robot staying on a large white page (SOP) using its light level detector, avoiding obstacles using the ultrasonic detector (dodge) and stop when it "hears" a loud noise (noisy) via the mic sensor. All these programs need to run at the same time. My thanks for any input (providing it isnt - give up :D).

Code: Select all
//Exp program for moving robot to stay on page and avoid obstacles

#define forwards(s,t) \
        OnFwdReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);
#define backwards(s,t) \
        OnRevReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);

#define turn_right(s,t) \
        OnFwd(OUT_A, s);OnRev(OUT_C, s);Wait(t);

#define turn_left(s,t) \
        OnRev(OUT_A, s);OnFwd(OUT_C, s);Wait(t);

#define turn_around(s,t) \
        OnRev(OUT_C, s); Wait(t);


#define Lightlevel 30
#define Near 20 //cm
#define Threshold 70
#define Mic (IN_3)

mutex moveMutex;

task SOP()
{
 while (true)
       {
       if (Sensor(IN_2) < Lightlevel);
       {
       Acquire(moveMutex);
       Wait(100);
       backwards (30,100);
       turn_right (45, 100);
 until(Sensor(IN_2) >= Lightlevel);
 Release(moveMutex);
}
}
}

task Dodge()
{
     while (true)
     {
      if(SensorUS(IN_4)> Near);
      {
      Acquire(moveMutex);
      turn_around(60,1000);
      Wait(300);
      Release(moveMutex);
      }
      }
      }

task Noisy()
{
 forwards(30,20000);
 {
  while (true)
  {
   if(Mic > Threshold);
   {
   Acquire(moveMutex);
   Wait(300);
   until(Mic > Threshold);
   Off(OUT_AC);
   Release(moveMutex);
   }
   }
   }
   }
task main()
{
SetSensorLight(IN_2,SENSOR_LIGHT);
SetSensorLowspeed(IN_4);
SetSensorSound(IN_3);

Precedes(Noisy, Dodge, SOP);

                }

fcrx7
 
Posts: 3
Joined: 04 Jun 2013, 21:55

Re: New to programming and Mindstorm

Postby iplewis » 05 Jun 2013, 12:41

Certainly don't give up! Here's some miscellaneous advice, which may or may not help you:

Small things:
- Tell us what this code does when you run it. It will help anyone trying to advise you.
- Indent your code. It's a lot easier to see what's going on:
- Comment your code. Same reason. I've put a snippet of indented, commented code below to illustrate. Many of my comments are questions, but that's because I don't know what your code is trying to do!

Code: Select all
task Noisy()
{
   forwards(30,20000);    //Turn motors on, wait for 20seconds before proceeding.
   {                                //This set of brackets doesn't seem to do anything.
      while (true)
      {
         if(Mic > Threshold);     //Probably should be Sensor(Mic)
         {
            Acquire(moveMutex);
            Wait(300);                     //Why wait here?
            until(Mic > Threshold);   //Not sure what's going on here. Mic was > threshold when we came in.
                                                                    //Should this be (Mic < Threshold) ?
            Off(OUT_AC);               //Turn motors off. Where do they get turned on again?
            Release(moveMutex);
         }
      }
   }
}


More importantly though, I'd say don't try to run before you can walk. Write a program to do ONE of these tasks, debug it, get it working properly. Then put that to one side and write a program to do another task. When you have all 3 tasks working separately, then you can try to make them work together.

Best of luck,

Ian.
iplewis
 
Posts: 25
Joined: 22 Mar 2012, 08:48

Re: New to programming and Mindstorm

Postby fcrx7 » 05 Jun 2013, 18:54

Thanks for your response Ian, I have made some adjustments to the code and commented my way through it.

I did initially write 3 seperate codes and had the robot working with each individually. This is my attempt at combining all 3 with some changes to try and make things a little easier. When I run the program so far all the robot does is run in a straight line meaning there is some conflict in what I have written. Unfortunately I do not have access to the robot at home so debugging with the motors in front of me becomes much harder. I think the direction is correct but as stated I am new to this so not sure if there are some points I have missed.

Here is my rejigged code.

Code: Select all
//Exp program for moving robot to stay on page and avoid obstacles

#define forwards(s) \                                           //Using #define command to try and simplify general robot movements
        OnFwdReg(OUT_AC, s, OUT_REGMODE_SYNC);

#define backwards(s,t) \
        OnRevReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);

#define turn_right(s,t) \
        OnFwd(OUT_A, s);OnRev(OUT_C, s);Wait(t);

#define turn_left(s,t) \
        OnRev(OUT_A, s);OnFwd(OUT_C, s);Wait(t);

#define turn_around(s,t) \
        RotateMotor(OUT_C, s, degrees);Wait(t);


#define Lightlevel 30                           //Level for LDR
#define Near 20                                 //Distance for ultrasonic in cms
#define Threshold 70                            //Sound level for Mic sensor
#define Mic (IN_3)                              //Is this needed or can I just use the Setsensor command in the main program?

mutex moveMutex;                                //Mutually Xclusive command to allow different programs to take control and run simultaneously

task SOP()                                      //Stay on page program - Robot has to stay on a large white area (black borders)
     {
     while (true)
                  {
                  if (Sensor(IN_2) < Lightlevel);                      //If LDR sensor drops below defined level
                                   {
                                    Acquire(moveMutex);
                                    Wait(100);                         //I have included wait command here as a pause helped the robot stop and change direction
                                    backwards (30, 100);
                                    turn_right (45, 100);
                                    until(Sensor(IN_2) >= Lightlevel);       //When sensor has returned to >= Lightlevel robot needs to return to moving forwards. Does this conflict what I have written?
                                    Release(moveMutex);
                                    }
                   }
     }
     
task Dodge()                                    //Program to avoid obstacles. No complex movement on sensor but this may be an option later
{
     while (true)
     {
           if(SensorUS(IN_4)> Near);                 //If ultrasonic sensor detects objects within defined limit
           {
                              Acquire(moveMutex);
                              turn_around(35, -180, 300);                     //I wanted the robot to turn 180 degrees when it senses an object
                              Release(moveMutex);                       //Again I want the robot to keep moving in a straight line after turning so maybe need some other command line here?
                              }
           }
}

task Noisy()                                     //Program to stop the robot when it hears a loud / sharp noise
 {
      while (true)
      {
                  if(Sensor(IN_3) < Threshold);                          //This sub-routine has precedent. Robot should move all the time the sensor is < Threshold
                  {
                           Acquire(moveMutex);
                           forwards(30);
                           Release(moveMutex);
                  }
                  else(Sensor(IN_3) >= Threshold);                        //I need the robot to stop after it "hears" very loud sounds, before continuing forwards
                  {
                           Acquire(moveMutex);
                           Off(OUT_AC);
                           Wait(500);                                     //Waits in case sound is still above threshold
                           Release(moveMutex);
                  }

      }
 }

task main()
{
     SetSensorLight(IN_2,SENSOR_LIGHT);
     SetSensorLowspeed(IN_4);
     SetSensorSound(IN_3);

     Precedes(Noisy, SOP, Dodge);                       //These are the priorities I would like for each task
}
Last edited by fcrx7 on 09 Jun 2013, 15:31, edited 1 time in total.
fcrx7
 
Posts: 3
Joined: 04 Jun 2013, 21:55

Re: New to programming and Mindstorm

Postby fcrx7 » 09 Jun 2013, 14:50

Any more help on this would be greatly appreciated.

Here is a slightly changed code

Code: Select all
//Program for moving robot to stay on page, avoid obstacles and stop at lound noises

//Using define command to try and simplify general robot movements
#define forwards(s) \
        OnFwdReg(OUT_AC, s, OUT_REGMODE_SYNC);

#define backwards(s,t) \
        OnRevReg(OUT_AC, s,OUT_REGMODE_SYNC);Wait(t);

#define turn_right(s,t) \
        OnFwd(OUT_A, s);OnRev(OUT_C, s);Wait(t);

#define turn_left(s,t) \
        OnRev(OUT_A, s);OnFwd(OUT_C, s);Wait(t);

#define turn_around(s, degrees, t) \
        RotateMotor(OUT_C, s, degrees);Wait(t);


#define Lightlevel 30                           //Level for LDR
#define Near 20                                 //Distance for ultrasonic in cms
#define Threshold 70                            //Sound level for Mic sensor
#define Mic (IN_3)                              //Is this needed or can I just use the Setsensor command in the main program?

mutex moveMutex;                                //Mutually Xclusive command to allow different programs to take control and run simultaneously

task Noisy()                                     //Program to stop the robot when it hears a loud / sharp noise
 {
      while (true)
      {
                  if(Sensor(IN_3) <= Threshold)                          //This sub-routine has precedent. Robot should move all the time the sensor is <= Threshold
                  {
                           Acquire(moveMutex);
                           forwards(30);
                           Release(moveMutex);
                  }
                  else                                               //I need the robot to stop after it hears very loud sounds, before continuing forwards
                  {
                           Acquire(moveMutex);
                           Off(OUT_AC);
                           Wait(500);                                     //Waits in case sound is still above threshold
                           Release(moveMutex);
                  }

      }
 }

task SOP()                                      //Stay on page program - Robot has to stay on a large white area (black borders)
     {
     while (true)
                  {
                  if (Sensor(IN_2) < Lightlevel)                      //If LDR sensor drops below defined level
                                   {
                                    Acquire(moveMutex);
                                    Wait(100);                         //I have included wait command here as a pause helped the robot stop and change direction
                                    backwards (30, 100);
                                    turn_right (45, 100);
                                    until(Sensor(IN_2) >= Lightlevel);       //When sensor has returned to >= Lightlevel robot needs to return to moving forwards. Does this conflict what I have written?
                                    Release(moveMutex);
                                    }
                   }
     }
     
task Dodge()                                    //Program to avoid obstacles. No complex movement on sensor but this may be an option later
{
     while (true)
     {
           if(SensorUS(IN_4)> Near)                 //If ultrasonic sensor detects objects within defined limit
           {
                              Acquire(moveMutex);
                              turn_around(35, -180, 300);                     //I wanted the robot to turn 180 degrees when it senses an object
                              Release(moveMutex);                       //Again I want the robot to keep moving in a straight line after turning so maybe need some other command line here?
                              }
           }
}


task main()
{
     SetSensorLight(IN_2,SENSOR_LIGHT);
     SetSensorLowspeed(IN_4);
     SetSensorSound(IN_3);

     Precedes(Noisy, SOP, Dodge);                       //These are the priorities I would like for each task
}


fcrx7
 
Posts: 3
Joined: 04 Jun 2013, 21:55


Return to Mindstorms Projects

Who is online

Users browsing this forum: No registered users and 24 guests

cron