Help with a line following program

Discussion specific to projects ideas and support.
Post Reply
okami-ninja
Posts: 4
Joined: 22 Nov 2011, 14:36

Help with a line following program

Post by okami-ninja »

Hi
I'm new to programming and lego mindstorms. I'm trying to write a program to make the robot move forward until it touches the line and then based on the angle it's touched it at position itself to follow the line. Then it stops until it here's a clap and then follows the line until there's another clap.

I've tried writing a program where the robot hits the line and turns left until it touches white again. Then if the turn time was less than the time taken to turn 90 degrees it stops otherwise it goes back on itsef until it touches the white. I tried running the program but the robot either just stops at the line or keeps going.

Does anyone have a better suggestion for the angle part or would be interested in looking at my code?
I'm thinking of geting the robot to turn left and store a light value and then do the same with the right. It would then compare the values and turn towards the greater one

Thanks a lot

okami
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Help with a line following program

Post by mattallen37 »

Yes, please post your code.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
okami-ninja
Posts: 4
Joined: 22 Nov 2011, 14:36

Re: Help with a line following program

Post by okami-ninja »

I'm using bricx cc. Here's the code:

Code: Select all

#define Threshold 50      //define all thresholds and constants here
long maxTurn = 1300;
#define SOUND_THRESHOLD 75
#define MIC SENSOR_2

task main()
{
    long t0, time;
    t0 = CurrentTick();
    SetSensorLight(IN_3);
    SetSensorType(IN_3, SENSOR_TYPE_LIGHT_ACTIVE);//turns sensor on
    SetSensorSound(IN_2);

    OnFwd(OUT_BC,45);
    until (SENSOR_3<Threshold);  //goes forward till hits line
    /*
    *  the next part makes it turn left until it leaves the line
    *  then it works out how long it took to leave the line and if it took
    * longer than when it turns 90 degrees it goes back on itself as we want it
    * to turn right in that circumstance
    */
    OnFwd(OUT_B,45);
    OnRev(OUT_C,45);
    until (SENSOR_3>Threshold);
    time = CurrentTick()-t0;
    if(time>=maxTurn){
       OnFwd(OUT_C,45); Wait(time);
       OnFwd(OUT_C,45);
       until(SENSOR_3>Threshold);
       until(MIC > SOUND_THRESHOLD ); //hears clap and then follows line
       Wait(100);
       while(MIC < SOUND_THRESHOLD)
      {
         OnFwd(OUT_BC,25);
         if (SENSOR_3<Threshold){
            OnFwd(OUT_C,40);//these make it turn when touches line
            OnRev(OUT_B,80);
         }
         else if(SENSOR_3>Threshold){
              OnFwd(OUT_B,90);
              OnRev(OUT_C,40  );
         }
         Off(OUT_BC);
         Wait(300);
      }
    }
    else {  //if it turns less than 90 degrees it has turned the right way and so goes straight to tracking
       until(MIC > SOUND_THRESHOLD ); //hears clap and then follows line
       Wait(100);
       while(MIC < SOUND_THRESHOLD)
       {
         OnFwd(OUT_BC,25);
         if (SENSOR_3<Threshold){
            OnFwd(OUT_B,40);//these make it turn when touches line
            OnRev(OUT_C,80);
         }
         else if(SENSOR_3>Threshold){
              OnFwd(OUT_C,90);
              OnRev(OUT_B,40  );
         }
         Off(OUT_BC);
         Wait(300);
      }
    }
  Off(OUT_BC);    //turns the robot off
}
I'm going to write a different version that uses the second idea for finding the angle.

Thanks
Last edited by okami-ninja on 24 Nov 2011, 14:08, edited 1 time in total.
okami-ninja
Posts: 4
Joined: 22 Nov 2011, 14:36

Re: Help with a line following program

Post by okami-ninja »

I've just finished writing a different version that uses 2 variables to compare the light in different places. Here's the code for the new one

Code: Select all

#define Threshold 50        //defines variables for sound and light
#define SOUND_THRESHOLD 75
#define MIC SENSOR_2
 int normSpeed = 45;  //defines the different speeds used
 int fwdSpeed = 90;
 int revSpeed = 40;
 
 task main()
  {
     SetSensorLight(IN_3);               //set the two sensors and turn light on to measure reflected light
     SetSensorType(IN_3, SENSOR_TYPE_LIGHT_ACTIVE);
     SetSensorSound(IN_2);
     
     OnFwd(OUT_BC,normSpeed);
     until (SENSOR_3<Threshold);//it stops at the line
     
     /*The next part will find the angle
     *The robot turns slightly to the left and stores the light reading as a variable
     *It then does the same for the right
     *Then it compares the 2 values and turns towards the greater one and stops when touches white
     */
     OnFwd(OUT_B,normSpeed); Wait(100);
     int lightLeft=SENSOR_3;

     OnFwd(OUT_C,normSpeed); Wait(100);
     int lightRight=SENSOR_3;
     
     //the comparison
     if(lightLeft>lightRight)     //it's lighter on the left so it turns that way
      {
         OnFwd(OUT_B,fwdSpeed);
         OnRev(OUT_C,revSpeed);
         until (SENSOR_3>Threshold);//stops when it leaves the line
         
         until (MIC > SOUND_THRESHOLD); //hears clap and then follows line
         Wait(100);
         while(MIC < SOUND_THRESHOLD)   //tracks until the second clap
         {
              OnFwd(OUT_BC,25);
              if(SENSOR_3>Threshold)
               {
                   OnFwd(OUT_C,fwdSpeed);
                   OnRev(OUT_B,revSpeed);
               }
              else if(SENSOR_3>Threshold)
               {
                    OnFwd(OUT_B,fwdSpeed);
                    OnRev(OUT_C,revSpeed);
               }
         }
         Off(OUT_BC);   //stops at 2nd clap
      }
      
      else if(lightLeft<lightRight)  //this is for when it should turn right
       {
        OnFwd(OUT_C,fwdSpeed);
        OnRev(OUT_C,revSpeed);
        until (SENSOR_3>Threshold);
        
        until (MIC > SOUND_THRESHOLD); //hears clap and then follows line
        Wait(100);
        while(MIC < SOUND_THRESHOLD)   //tracks until the second clap
         {
              OnFwd(OUT_BC,25);
              if(SENSOR_3>Threshold)
               {
                    OnFwd(OUT_B,fwdSpeed);
                    OnRev(OUT_C,revSpeed);
               }
              else if(SENSOR_3<Threshold);
               {
                    OnFwd(OUT_C,fwdSpeed);
                    OnRev(OUT_B,revSpeed);
               }
         }
         Off(OUT_BC);
      }
  }
Last edited by okami-ninja on 24 Nov 2011, 14:09, edited 1 time in total.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Help with a line following program

Post by mattallen37 »

Could you please use code tags as well so it keeps white-space?

Code: Select all

[code]
[/code]
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
okami-ninja
Posts: 4
Joined: 22 Nov 2011, 14:36

Re: Help with a line following program

Post by okami-ninja »

Sorry, I didn't realise you could do that. Thanks
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests