loops

7 replies [Last post]
Roombie
Title: NooBot+
Joined: 04/22/2010
Posts:
BotPoints: 20
User offline. Last seen 12 years 12 weeks ago.

What is wrong with this program?

First of all, we have no idea how to use the loop. Second of all, every time we run this something new comes up, and it never loops as we want to. Sometimes it loops 3 times, sometimes it goes backwards when we never told it to drive backwards, but most of the time it just stops the loops at 1. There is absolutely no consistency, which we've never encountered. We've tried isolating some of the program, but when isolated, it usually works, but when we put it together it doesn't work at all. And about the comments, we've been just taking things from various programs that we've found, so we're not actually very sure if they're correct or not.

By the way, we're currently using Interactive C, and many on the forums are using KISS. Is there any difference between these 2? Would KISS be easier for students?

void main()
{ int num;
num = 1; /* initialize counter */
while(num <= 30)/* loop while 1 is = 1*/
{
iROBOinit(BIGEMPTYWORLD); // initialize w/world
sleep(1.0); // let graphics initialize
// (or get finger away from start button)
create_connect(); // connect Create
sleep(0.5);

create_drive_straight(1000);

while(gc_lbump==1 && gc_rbump==1) {create_stop();} // stop moving
while(!gc_lbump && !gc_rbump) // continue until bump

create_sensor_update();
create_bumpdrop();

create_stop();

beep ();
sleep(0.5);

create_spin_CCW(100);
sleep(2.0);

create_disconnect(); // return control to IC

num = num + 1; }

}

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
Posts:
BotPoints: 1168
User offline. Last seen 7 years 12 weeks ago.

I'm not sure exactly what your code is supposed to be doing. Can you post the expected behavior?

However, a few things that are weird in your code, which you might want to look at:

create_drive_straight(1000);

while(gc_lbump==1 && gc_rbump==1) {create_stop();} // stop moving
while(!gc_lbump && !gc_rbump) // continue until bump

create_sensor_update();

While loops run the content of the loop until the boolean expression inside the parentheses evaluates to false. You appear to be trying to use it to wait for the expression to be true. The bump variables may not be initialized when the while loops run, since the first call to create_sensor_update is after the loops. There is no obvious reason that I can see to call create_stop inside a loop like that. If you intended create_stop() to execute after the while loop terminated, you should put create_stop() after the curly braces instead of inside them. The second while loop will repeatedly call create_sensor_update, since it runs the first statement that is after the while() statement if no curly braces or semicolon is present after the while() statement.

That's what I see immediately. Try fixing those, and if you still have problems, post your new code along with the expected and observed behavior.

Good luck!

-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS

Roombie
Title: NooBot+
Joined: 04/22/2010
Posts:
BotPoints: 20
User offline. Last seen 12 years 12 weeks ago.

Well basically, we're trying to get it to do what the Roomba's originally programmed to do.

First it goes forward until it hits something, when it should spin and beep. Then it would loop.

We actually have no idea what we're supposed to fix. Originally, we tried this program only to bump once and then just disconnect, but after we tried to add a loop (which to be completely honest, we don't know how to do), it just stopped working, and for some reason we got inconsistent results every time the program ran.

Our original program w/ no loop;

void main() {
iROBOinit(BIGEMPTYWORLD); // initialize w/world
sleep(1.0); // let graphics initialize
// (or get finger away from start button)
create_connect(); // connect Create

create_drive_straight(1000);

while(gc_lbump==1 && gc_rbump==1) {create_stop();} // stop moving
while(!gc_lbump && !gc_rbump) // continue until bump

create_sensor_update();
create_bumpdrop();

create_disconnect(); // return control to IC
}

One member in our group is really reluctant to change anything because the bump in the program is the one thing that we knows that actually works. And what we don't know... is what we're supposed to place after what you said, outside the curly braces, like how much or what parts of the program.

Basically, we have no idea what we're doing.

KIPR Matthew
KIPR Matthew's picture
Title: KIPR Staff
Joined: 06/04/2009
Posts:
BotPoints: 154
User offline. Last seen 4 years 49 weeks ago.

These changes seemed to work for me.

void main()
{ int num;
iROBOinit(BIGEMPTYWORLD); // initialize w/world
sleep(1.0); // let graphics initialize
// (or get finger away from start button)
create_connect(); // connect Create
sleep(0.5); //allow time for create connection

for(num=1;num<=30;num++)// loop starting at 1, increment each time, until num=30
{
create_drive_straight(1000);
create_bumpdrop();//update sensors before entering test
while(!gc_lbump && !gc_rbump){create_bumpdrop();} // update bump sensor status until sensor pressed

create_stop();

beep ();
sleep(0.5);

create_spin_CCW(100);
sleep(2.0);
}
create_stop();
create_disconnect(); // return control to IC
}

The first thing I did was to move all connection and disconnection events outside of your loop. We only want to connect and disconnect once per run of the robot. If we connect and disconnect too many times, bad things could happen.

Next, I swapped your while loop for a for loop, which did the same thing, just a little more efficiently.

Fist thing in the loop is the drive straight code.

That brings us to the actual bump sensor checking code. First we check the state of the bump sensor with the bumpdrop line. Then we enter the while loop. The while loop continuously updates the bump sensor readings, and will continue to do this until the sensor is pressed. Since we have not given the Create any more driving instructions, it will continue to drive straight during all of the checking.

When the sensor is pressed, the while argument becomes true and so we move to the next line of code. So it will stop beep and turn, then it will go back to the top to start again, until this has happened 30 times.

Hope this helps.

Join the Botball folding at home team!
http://folding.stanford.edu/
Team 87314 "Botball"
Stats: http://folding.extremeoverclocking.com/team_summary.php?s=&t=87314

Roombie
Title: NooBot+
Joined: 04/22/2010
Posts:
BotPoints: 20
User offline. Last seen 12 years 12 weeks ago.

@KIPR Matthew:

We don't get why you can take out one while(!gc_lbump && !gc_rbump) and why you don't have state the variable outside the beginning. When we put int anywhere else, then we would get errors. What is the exception to this ?

Roombie
Title: NooBot+
Joined: 04/22/2010
Posts:
BotPoints: 20
User offline. Last seen 12 years 12 weeks ago.

We would also like to know how to make it beep as many times as it has looped. Basically counting the number of loops. Would we have to introduce a new variable or can we continue to use num? And should we use for or while loops?

KIPR Matthew
KIPR Matthew's picture
Title: KIPR Staff
Joined: 06/04/2009
Posts:
BotPoints: 154
User offline. Last seen 4 years 49 weeks ago.

I initialize the num variable after int main. The syntax to make a for loop work is: for(initialize variable value; boolean expression; variable incriment){program to run while true}

Num is a valid variable to use elsewhere and is a useful side effect of the for.

I would add the lines:
int j; //add somewhere outside of other for loop

for(j=0;j < num;j++){//add inside for loop
beep();
sleep(0.1);
}

This bill beep a corresponding number of time equal to the loop number. There is one issue, and that is while you are beeping, you will not be able to tell if the bump sensor is triggered, so when you beep 20 times, you will have to drive straight for at least 2 seconds.

Join the Botball folding at home team!
http://folding.stanford.edu/
Team 87314 "Botball"
Stats: http://folding.extremeoverclocking.com/team_summary.php?s=&t=87314

Roombie
Title: NooBot+
Joined: 04/22/2010
Posts:
BotPoints: 20
User offline. Last seen 12 years 12 weeks ago.

Thank You !