IR Sensor Help

13 replies [Last post]
hawkm
Title: NooBot
Joined: 04/02/2012
Posts:
BotPoints: 9
User offline. Last seen 6 years 10 weeks ago.

Hello there,
I have been trying to use the IR sensor to have the robot follow it. I have tried to use the example code in the KISS IDE, but it doesn't work either. Is there something I'm doing wrong?
Thanks,
Alex

amehta
Title: NooBot
Joined: 07/18/2011
Posts:
BotPoints: 136
User offline. Last seen 1 year 26 weeks ago.

So, just to clarify, you're trying to follow the SENSOR (perhaps from another robot or something?)

-amehta (Andrew Mehta)

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

Please post the code you're using, exactly what you expect it to do, and exactly what it's doing wrong.

-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

hawkm
Title: NooBot
Joined: 04/02/2012
Posts:
BotPoints: 9
User offline. Last seen 6 years 10 weeks ago.

@amehta: No, It needs to follow black lines that are on a white sheet.
@Jeremy Rand: Here's the code

  1. // Created on Mon Mar 5 2012
  2.  
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {//top hat IR sensor in ports 0 and 1
  7. //drives robot straight until the tophat in port 0 sees black
  8. while(analog10(0)){//900 was experimentally determined as the midpoint between white and black
  9. //while the sensor sees white
  10. mav(0,300);//move straight
  11. mav(3,300);
  12. }
  13.  
  14.  
  15. //following a line
  16. //if both see white, I want to stop and do nothing
  17. //if atleast one sees black, I want to move the robot
  18. // straight, left, or right
  19.  
  20. while(analog10(0)>900 || analog10(1)>900){//atleast one sees black
  21. if(analog10(0)<900 && analog10(1)>900){//left sees white, right sees black
  22. mav(0,300);//turn right to get on the line
  23. mav(3,200);
  24. }
  25. //only thinks about this if the above if statement was false
  26. else if(analog10(0)>900 && analog10(1)<900){//left sees black, right sees white
  27. mav(0,200);//turn left
  28. mav(3,300);
  29. }
  30. else{// only thinks about this if the above if statement was false
  31. mav(0,300);
  32. mav(3,300);
  33. }
  34. }
  35. printf("%d\t%d",analog10(0),analog10(1));//prints the values that caused the while loop to fail
  36. }
  37.  
  38.  

The robot doesn't do anything. Am I missing something?
By the way, this was the example code that KISS gives you.

[Edited by Jeremy to fix code display issue... use the < c > element for C code instead of < code >]

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

What do you mean by "doesn't do anything"? The robot doesn't move? I would expect this code to move the robot forward.

  1. //drives robot straight until the tophat in port 0 sees black
  2. while(analog10(0)){//900 was experimentally determined as the midpoint between white and black
  3. //while the sensor sees white
  4. mav(0,300);//move straight
  5. mav(3,300);
  6. }

This is suspicious. It is unlikely that analog10(0) will ever be equal to 0 (analog sensors have some random jitter), so I would expect this while loop to never terminate. Did you intend to compare the analog10 result to an int value?

Also, please use the < c > element for C code on the forum, not < code >. It works much better.

-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

hawkm
Title: NooBot
Joined: 04/02/2012
Posts:
BotPoints: 9
User offline. Last seen 6 years 10 weeks ago.

Thanks for telling the the < c > tag works here too.
Anyway, what I was doing originally was playing with the example lesson. I tried to test it and it didn't work. Then I tried to use the example code and it also did not move. I ran it through the simulator also and that also doesn't move. It does print the values however.

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

Hmm, I don't see anything in the code you posted that would prevent the robot from moving at all. If it's printing values, then it should have also evaluated the if/else statement, meaning it should execute mav. Does anyone else see anything I'm overlooking?

(The usual things such as trying a different CBC, trying different motor ports, etc., are candidates... but if it's not working in the simulator either, those probably aren't the problem....)

-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

edmyers
Title: NooBot
Joined: 06/01/2009
Posts:
BotPoints: 28
User offline. Last seen 5 years 16 weeks ago.

Change the while(analog10(3)) to while(analog10(3) < 900). That will work in the simulator. Leaving out the comparison simply does not work.

Since analog10() is defined as an int it is very possible for it to return a negative value, which would cause the bodies of while blocks in the program to not be executed.

The expression in the while() (in the absence of a comparison) evaluates to "true" when greater than zero and "false" when zero and below. (That statement in questions is not good programming style.)

In light this, the analog10() function should be defined as "unsigned int" by kipr.

Also make sure that the motor and sensor port numbers are correct. The reflective sensor on the simulator defaults to port 7, so the "3" in the analog10() calls would have to change to "7" throughout the program.

Eugene Myers
Cedarhouse Robotics

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

analog10 is defined to return a 10-bit value corresponding to the voltage of the input. signed ints have 31 bits of positive values. So analog10 will not return a negative value.

I'm also fairly sure that casting a negative number into a boolean value will result in true (it's not doing a signed comparison).

-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

hawkm
Title: NooBot
Joined: 04/02/2012
Posts:
BotPoints: 9
User offline. Last seen 6 years 10 weeks ago.

I changed the codes how you said to
It moves to the line, and stops while printing the values on the screen
Should I put more code under the IR sensor code?

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

My guess is that it's detecting white and terminating the while loop. This happens fairly often when following lines. A common solution is to enter a "search mode" when white is detected (e.g. rotating in place until black is found), rather than terminating the loop.

You can verify whether this is the problem by looking at the debug output from your program; is it detecting white right before the loop terminates?

-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