Sonar

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

We are trying to use the sonar sensor. We cant seem to find any sort of code or example to work from. If someone could just give us a piece of code to check what the sensor is sensing.

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

I haven't used the new Maxbotics sonar, but I think it works fairly similar to the SRF04 sonar on the HB/XBC. There's a function called sonar(), which returns the distance (in millimeters) between the sonar unit and the nearest object within its field of view.

The following code will drive forward until the sensor is within 200mm of the nearest object:

  1. fd(0);
  2. fd(1); // Replace these fd statements with whatever you use to drive forward
  3. while(sonar() > 200) msleep(10);
  4. ao(); // Replace this ao statement with whatever you use to stop

Feel free to ask more questions either here or on the Botballer's Chat. :-)

-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

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

You can poll the new SONAR with the standard analog10(x) function where x is the analog port number between 0 and 7. The sonar is a floating point sensor, so to get good values, you need to first use set_each_analog_state(x,x,x,x,x,x,x,x), where each x represents an analog port. Set it to 1 for floating and 0 for not floating. You only need to call set_each_analog_state once at the very beginning of you code and then call the analog10 function every time you want to take a reading.

So for a SONAR plugged into port 3 you would need two lines of code:

set_each_analog_state(0,0,0,1,0,0,0,0); //sets port 3 to floating

analog10(3); //returns current sonar value between 0 and 1023

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

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

Matt, I hope your comment about a "floating point sensor" was a typo, because there is no such thing. Floating-point means a method of storing real numbers in a computer; it has nothing to do with the floating vs pullup characteristics of an analog input port. I see this error all over the Botball FAQ; it's confusing to newbies (since the analog10() function does not return a floating-point value, regardless of the floating/pullup status of the port), and it is flat-out incorrect. Perhaps KIPR staff could be more careful not to mention floating-point sensors in the future? Thanks.

-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