Checking both bumpers

5 replies [Last post]
vaironl
vaironl's picture
Title: NooBot+
Joined: 04/09/2011
Posts:
BotPoints: 27
User offline. Last seen 10 years 22 weeks ago.

I keep wondering, why is my robot not making a reaction to one bump only.
As some of you know sometimes the angle the create robot is facing might only detect one bump. I will like to check for both, I used the or (||) operator but it still does not work. Any ideas of why is this happening?

Code:

while(get_create_lbump(0.1) <1 || get_create_rbump(0.1) < 1)
{
create_drive_straight(200);
}

Doing science.

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

Are you trying to make the robot stop when either of the bump sensors is triggered, or when both are triggered simultaneously?

-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

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 7 years 20 weeks ago.

I believe one thing that may help but might not be the problem would be replacing the while(get_create_lbump(0.1) <1 || get_create_rbump(0.1) < 1) with while(!(get_create_lbump(0.1) || get_create_rbump(0.1))
And the bump may be pushing the other one also they can be kind of sensitive I've seen.
also I'd advise this just as more optimized version You could also remove the 0.1 and make it 0.0 for complete accuracy(well as good as it gets) and having both of them wait may cause problems but I'm not sure
create_drive_straight(200);
while(get_create_lbump(0.1) <1 || get_create_rbump(0.1) < 1){}

EDIT: and at jeremy rand it would seem that he just wants it to stop moving or go on in the program after 1 bump but I may be wrong

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

@vaironl: Assuming that ruler501 is correct that you want it to terminate the loop after either of the bump sensors is triggered, here's your problem:

while(get_create_lbump(0.1) <1 || get_create_rbump(0.1) < 1)

The above will terminate when the following condition is false:

get_create_lbump(0.1) <1 || get_create_rbump(0.1) < 1

Let's make what is called a truth table:

Name this x:
get_create_lbump(0.1) <1
And this y:
get_create_rbump(0.1) < 1

If x==0 and y==0, then x||y==0
If x==0 and y==1, then x||y==1
If x==1 and y==0, then x||y==1
If x==1 and y==1, then x||y==1

Meaning that the loop will terminate when x==0 and y==0.

x==0 and y==0 when get_create_lbump(0.1) >= 1 and get_create_rbump(0.1) >= 1.

So the loop will terminate when both bumpers are returning 1 simultaneously.

So, what do you want? Let's look at what happens when you use && rather than ||.

get_create_lbump(0.1) <1 && get_create_rbump(0.1) < 1

Truth table:

If x==0 and y==0, then x&&y==0
If x==0 and y==1, then x&&y==0
If x==1 and y==0, then x&&y==0
If x==1 and y==1, then x&&y==1

From this, you'll see that the loop will terminate as long as either x or y is 0. That's what you want.

You may be wondering why || requires both bumpers, while && requires only one -- isn't that the opposite of the words "or" and "and"? This is because when you apply a Not operation (which happens in this case because you want the while condition to be false), || becomes && and && becomes ||. This is a property of logic called De Morgan's Law.

Hope this helps.

-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

Beta
Beta's picture
Title: The Magnificent
Joined: 02/24/2012
Posts:
BotPoints: 266
User offline. Last seen 8 years 32 weeks ago.

Another thing to note here is that even if your while loop exits correctly, you never change the create's behavior.

To highlight on this, I would write the code this way:

  1. create_drive_straight(200);
  2. while(...)
  3. {
  4. }
  5. create_stop();

create_drive_straight only needs to be called once. Putting it in a while loop doesn't really do anything. Afterwards, you need to call create_stop or another create command to change the behavior, or your robot will continue to drive straight as if it is still in the loop.

This might be because this is just a code snippet, but I thought it would be worth noting.

Braden McDorman

Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.

Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.

vaironl
vaironl's picture
Title: NooBot+
Joined: 04/09/2011
Posts:
BotPoints: 27
User offline. Last seen 10 years 22 weeks ago.

Yeah, sorry if i did not specify it clearly.

Thanks for the truth table I learned how to make those in class about a week ago. Now I understand how to apply them in real problems.

Doing science.