View unanswered posts | View active topics It is currently Thu Jun 30, 2016 12:01 pm

 Page 1 of 1 [ 10 posts ]
 Print view Previous topic | Next topic
Author Message
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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 updated help documentation and the ROBOTC Forums.

Thu Nov 14, 2013 12:54 pm
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
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.

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

Mon Nov 18, 2013 11:55 am
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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 updated help documentation and the ROBOTC Forums.

Wed Dec 04, 2013 1:23 pm
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
Yes, our teacher had us go through the basics, but i am still having trouble understanding.

Mon Dec 09, 2013 1:29 pm
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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
Rookie

Joined: Tue Nov 05, 2013 12:21 pm
Posts: 8
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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 10 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning