Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.5 (Read 128 times) previous topic - next topic

teckel

Rewrote the pulseIn to use micros() instead of loop counting.  With that change, I got accurate data that matches exactly with the speed of sound.  So, the problem was with pulseIn.  However, I'm not sure if the problem is how it calculates time (counting processor cycles) or it has to do with the time it takes for the sensor to send out a ping.  In any case, it doesn't matter as I've completely re-written the code to use simple timing which works great.  There's not yet an option for the ping to be event driven.  But, it is much faster and more accurate now.

I should be releasing v1.2 later tonight.

Tim

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

terryking228

Quote
Rewrote the pulseIn to use micros() instead of loop counting.  With that change, I got accurate data that matches exactly with the speed of sound.  So, the problem was with pulseIn.


Beautiful, Tim.  You have challenged lots of assumptions with this and it's really good news that you are understanding what's happening. We'll all be listening for your next round!

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

robtillaart

Quote
Rewrote the pulseIn to use micros() instead of loop counting.

Be aware that micros() always round its value to a multiple of 4.

Counting the loops in pulsein works quite well, however if there are interrupt handled during this it will definitely give a wrong (too low) reading. This could (partially) explain why you need a smaller divisor in your formulas.
This "trick" is used especially to be able to read pulselengths smaller than 4 micros() precission.

Hope to see the code soon!




Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

teckel


Quote
Rewrote the pulseIn to use micros() instead of loop counting.

Be aware that micros() always round its value to a multiple of 4.

Counting the loops in pulsein works quite well, however if there are interrupt handled during this it will definitely give a wrong (too low) reading. This could (partially) explain why you need a smaller divisor in your formulas.
This "trick" is used especially to be able to read pulselengths smaller than 4 micros() precission.

Hope to see the code soon!


Let's put it this way.  The pulseIn function is from 460 to 7120 microseconds off because of the way ultrasonic sensors work.  Basically, the echo port state takes that long to settle and pulseIn counts that time.  So, I think I'll settle for only 4 microseconds off. ;-)

I may release the code in the source both ways so if you want to try it the other way, you can uncomment it.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

teckel

New in version 1.2:

Lots of code clean-up thanks to Adruino Forum members. Rebuilt the ping timing code from scratch, ditched the pulseIn code as it doesn't give correct results (at least with ping sensors). The NewPing library is now VERY accurate and the code was simplified as a bonus. Smaller and faster code as well. Fixed some issues with very close ping results when converting to inches. All functions now return 0 only when there's no ping echo (out of range) and a positive value for a successful ping. This can effectively be used to detect if something is out of range or in-range and at what distance. Now compatible with Arduino 0023.

Download NewPing v1.2

By default, it uses micros() to do the ping echo timing.  But, I also included alternate code that uses loop counting (similar to what pulseIn uses).  It's streamlined, but follows the same basic concept as pulseIn.

Let me know what you think, and if you have any issues or suggestions.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Go Up