View unanswered posts | View active topics It is currently Fri Aug 29, 2014 7:11 pm






Reply to topic  [ 10 posts ] 
Stop and continue program. (URGENT) (PLEASE HELP) 
Author Message
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Stop and continue program. (URGENT) (PLEASE HELP)
Is it possible for me to program a robot to reach a certain destination, but stop for objects in its path until they are removed in a loop. It is import that the robot continues from the point it left off at.
Code:
#pragma config(Sensor, S3,     ,               sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
while (true)
   {
       if (SensorValue(sensorSONAR)>25)
       {
          motor[motorB] = 100;      //straight
          motor[motorC] = 100;
          wait10Msec (1);
          
          motor[motorB] = 0;       //turn left
          motor[motorC] = 100;
          wait10Msec (1);
          
          
          motor[motorB] = 100;     //straight
          motor[motorC] = 100;
          wait10Msec (1);
          
          
          motor[motorB] = 0;            // stop
          motor[motorC] = 0;
          wait10Msec (1);
          
       }
   else
     {
        motor[motorB] = 0;  // halt
        motor[motorC] = 0;
    }
   }

}


Last edited by f6e9a on Tue Dec 03, 2013 4:10 pm, edited 1 time in total.



Tue Nov 12, 2013 12:50 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
Post Re: Stop and continue program.
This is definitely doable; in fact, it's a very similar challenge to the Sentry Simulation 3 in the ROBOTC Curriculum for TETRIX and LEGO MINDSTORMS :)

To start, let's plan out some psuedocode; list, in order, exactly what you want the robot to do step-by-step. This will help us outline the actions we want the robot to make and the order we want them to be executed in. Once we have that, we can start determining the best way to have the robot perform those actions, the coding tools we can use to control those actions (how long/how far the robot should move for, when to stop, when to turn, etc), and finally the exact lines of code to use for each action.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Nov 14, 2013 12:54 pm
Profile
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Re: Stop and continue program.
Sorry for the late replay but.

Psuedocode


go straight 3 ft.
turn left 90 degrees.
continue 4 ft.
turn right 90 degrees.
Stop.

also if something is in its path is it possible for the robot to produce a beeping sound.

Thanks for all your help :D

I understand feet is not a variable and we will most probably use the wait10Msec.


Mon Nov 18, 2013 11:55 am
Profile
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Re: Stop and continue program.
someone pls help me or show me an example code. pls

I am willing to learn, someone help.


Wed Nov 27, 2013 12:46 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
Post Re: Stop and continue program.
Have you gone through the ROBOTC Curriculum for TETRIX and LEGO Mindstorms yet? It will walk you through the steps needed to program this challenge, step by step; you may have to calculate the distance the robot needs to travel for this particular challenge, but once you have completed the Sentry Simulation 3 challenge this should be fairly easy to work out.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Wed Dec 04, 2013 1:23 pm
Profile
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Re: Stop and continue program. (URGENT) (PLEASE HELP)
Yes, our teacher had us go through the basics, but i am still having trouble understanding.


Mon Dec 09, 2013 1:29 pm
Profile
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Re: Stop and continue program. (URGENT) (PLEASE HELP)
My exact problem is.

If i put the code in a while loop, making the robot lets say go in a square as long as nothing is in let's say 5 cm of the sonar sensor. If something comes in it's way it will stop and end the loop.

I could use a function.

I just don't understand what command or variable to use to get the robot to recall where it left off and continue from there.
Code:
task main()
{
while (true)
{
   if (SensorValue(sonarSensor)>20)
   {
                  motor[motorB] = 10;
              motor[motorC] = 10;
         wait1Msec (5000);
         motor[motorB] = 10;
         motor[motorC] = 0;
         wait1Msec (5000);
      
   }
else
{
   motor[motorC] = 0;
   motor[motorB] = 0;

}
}
}



Tue Dec 10, 2013 1:12 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Stop and continue program. (URGENT) (PLEASE HELP)
f6e9a wrote:
Code:
task main()
{
    while (true)
    {
        if (SensorValue(sonarSensor)>20)
        {
            motor[motorB] = 10;
            motor[motorC] = 10;
            wait1Msec (5000);
            motor[motorB] = 10;
            motor[motorC] = 0;
            wait1Msec (5000);
        }
        else
        {
            motor[motorC] = 0;
            motor[motorB] = 0;
        }
    }
}

If you look at your code carefully, you will see it is basically running forward for 5 seconds, then turn for 5 seconds in a loop. Assuming turning for 5 seconds will turn 90 degrees (I think it's a bit too long), then it is doing a rectangle. However, since you are using dead reckoning and busy waiting for the robot to go forward and turn, your code does not check for sonar value until the turn is complete. Essentially, imagine you are the robot and you are closing your eyes and walk forward for some distance and then turn 90 degrees then you open your eyes and see if there is anything in front of you. If not, you close your eyes again and walk forward for some distance and then turn 90 degrees again. Then you open your eyes again to check for obstacles. This go on and on. What problems do you see? What if there is an obstacle while you are walking forward? Your eyes are closed while walking forward and you never see the obstacle. The proper way to do it is to keep your eyes always open when walking and turning. I also recommend using encoders instead of time because while doing wait1Msec(5000), you are essentially closing your eyes and not looking. To make your life easier, use a top-down design approach. Imagine if you have two functions, one for driving forward and one for turning. Then you can call them in a loop.
Code:
while (true)
{
    Drive(distance, power);
    Turn(angle, power);
}

Then the next step is to figure out how to write those two functions so that it will also check for obstacles. Basically, the drive function should say "while not reaching the distance, if no obstacle in front keep driving, otherwise stop".
Code:
void Drive(int distance, int power)
{
    //
    // Adjust power so that it is only negative if distance is negative.
    //
    power = abs(power);
    if (distance < 0)
    {
        power = -power;
    }
    //
    // reset encoders.
    //
    nMotorEncoder[motorB] = 0;
    nMotorEncoder[motorC] = 0;
    //
    // while not reaching target.
    //
    while (abs(nMotorEncoder[motorB] + nMotorEncoder[motorC])/2 < distance)
    {
        //
        // Check for obstacle.
        //
        if (SensorValue[sonarSensor] > 20)
        {
            motor[motorB] = power;
            motor[motorC] = power;
        }
        else
        {
            motor[motorB] = 0;
            motor[motorC] = 0;
        }
        wait1Msec(10);
    }
}

So now do the same for turning. The code is almost identical except for checking the encoders for turned angle instead of distance.
Code:
void Turn(int angle, int power)
{
    //
    // Adjust power so that it is only negative if angle is negative.
    //
    power = abs(power);
    if (angle < 0)
    {
        power = -power;
    }
    //
    // reset encoders.
    //
    nMotorEncoder[motorB] = 0;
    nMotorEncoder[motorC] = 0;
    //
    // while not reaching target.
    //
    while (abs(nMotorEncoder[motorB] - nMotorEncoder[motorC])/2 < angle)
    {
        //
        // Check for obstacle.
        //
        if (SensorValue[sonarSensor] > 20)
        {
            //
            // Assuming motorB is the left motor and motorC is the right motor.
            //
            motor[motorB] = power;
            motor[motorC] = -power;
        }
        else
        {
            motor[motorB] = 0;
            motor[motorC] = 0;
        }
        wait1Msec(10);
    }
}


Tue Dec 10, 2013 2:51 pm
Profile
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Post Re: Stop and continue program. (URGENT) (PLEASE HELP)
Thanks, what you said makes perfect sense.
I understand the code, but what i don't understand is how you are declaring 2 parameters per function. (Angle, Power, Distance.) and for me to declare these variables i would need to do it before the functions right?
like so.


Code:
int distance;
int power;
int angle;


before the starting of the code, but would i enter the actual integers here or inside the Task Main after the functions are called forth?

Sorry kind of a newb. Thanks for all the help though.


Wed Dec 11, 2013 11:13 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Stop and continue program. (URGENT) (PLEASE HELP)
The distance parameter in the Drive function is in terms of encoder counts. The values of distance and angle depend on how far you want to go or how much you want to turn. You can use real numbers in place of distance and angle, I just don't know what they should be. You need to experiment to find the correct values. So for example,
Code:
while (true)
{
    Drive(2400, 10);
    Turn(500, 10);
}


Thu Dec 12, 2013 1:52 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.