Go Down

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

cyclegadget


can someone help me... =(


First step, make Arduino work with basic code for NewPing.

After you understand how the sketch works you can use variables to calculate speed.

Quoted from this link http://www.unitarium.com/speed-calculator

Quote
Assume, V - stands for average speed (velocity), T - stands for traveling time and D - stands for traveled distance.

Average Speed can be calculated by using the formula: V = D/T
Traveled distance can be calculated as: D = V*T
Traveling time can be calculated using: T = D/V

Using the formulas remember about the units. For example, if the distance is given in meters and the time is given in seconds, calculated average speed is given in meters per second.

matt121187



can someone help me... =(


First step, make Arduino work with basic code for NewPing.

After you understand how the sketch works you can use variables to calculate speed.

Quoted from this link http://www.unitarium.com/speed-calculator

Quote
Assume, V - stands for average speed (velocity), T - stands for traveling time and D - stands for traveled distance.

Average Speed can be calculated by using the formula: V = D/T
Traveled distance can be calculated as: D = V*T
Traveling time can be calculated using: T = D/V

Using the formulas remember about the units. For example, if the distance is given in meters and the time is given in seconds, calculated average speed is given in meters per second.



sorry, im new with arduino..what do u mean by " First step, make Arduino work with basic code for NewPing."? from the ping sensor coding, i can get the distance, but how to get the value of times?

cyclegadget

Quote
Assume, V - stands for average speed (velocity), T - stands for traveling time and D - stands for traveled distance.

Average Speed can be calculated by using the formula: V = D/T
Traveled distance can be calculated as: D = V*T
Traveling time can be calculated using: T = D/V


You may want to start a thread in the programming questions area for your project. Below, is some help to get you started.
You have a sensor that can provide distance information. This is your "D" variable.

You can use millis() or micros() to measure your time between distance checks. This is your "T" variable. FirstMillis() - SecondMillis() = time between distance checks.

V = D/T......."V" velocity, = "D" distance / "T" time.

Before you ask more questions, you need to read this thread so that you will get the best help possible. http://arduino.cc/forum/index.php/topic,97455.0.html

Good luck, your project is easily possible but, you need to put in some time to make it happen.


teckel

sorry, im new with arduino..what do u mean by " First step, make Arduino work with basic code for NewPing."? from the ping sensor coding, i can get the distance, but how to get the value of times?


You create a time stamp for each ping.  Then, looking at multiple ping times, and the comparison to the ping time stamp, you can easily calculate the speed (also if it's getting closer or further away).  For example, you have two pings like the following:

Ping 1: Timestamp 50,000uS - Ping time 8,000uS
Ping 2: Timestamp 100,000uS - Ping time 7,000uS

So, two pings that are 50ms apart, the first distance is 8000uS (140cm), sencond ping is 7,000uS (123cm).  So, in 50ms, it got 17cm closer (140-123).  17cm/50ms = .17/0.05 m/s or 3.4 m/s or 7.6 miles/hour.

Only the previous and current ping timestamp and distance are needed.  If you use scheduled polling so you always know exactly how far apart each ping is (for example, exactly 50,000uS) then you don't even need to know the timestamp, just the current and previous ping distance.  Just don't use delay(50) to make them 50ms apart, that won't be accurate.  You'll need to either use an interrupt timer or polling to keep things accurate.  Interrupt timer would be ideal but more complicated unless you used a timer library would would greatly simplify things.

I've written a little sketch for you that uses the NewPing and TimerOne library that outputs the speed via serial and uses the timer interrupt to make the ping timing very accurate.  I added lots of comments so you should be able to follow along.  Speed values are in meters per second, with positive values coming at you and negative moving away.  Anyway, here's the sketch:

Code: [Select]
#include <TimerOne.h>
#include <NewPing.h>

#define TRIGGER_PIN  12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11 // Arduino pin tied to echo pin on the ultrasonic sensor.

NewPing sonar(TRIGGER_PIN, ECHO_PIN); // NewPing setup of pins.

volatile int previous = 0, current = 0; // Must be volatile because these change inside the interrupt code.
volatile float mps = 0.0;
volatile boolean newping = false;

void setup() {
 delay(1000);                        // Wait a second before we start.
 Timer1.initialize(50000);           // 50000uS = 50ms.
 Timer1.attachInterrupt(pingSensor); // pingSensor to run every 50ms.
 Serial.begin(115200);               // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
 if (newping) { // There a new ping, print speed.
   /* START - This section would be replaced with code that would do something with the result other than just output to serial. */
   if (mps) {   // Non-zero speed, show result.
     Serial.print(mps);
     Serial.println(" m/s");
   } else Serial.println("-"); // Can't determine speed, output "-" instead of speed.
   /* END */
   newping = false; // Set to false till next ping.
 }
}

void pingSensor() {
 previous = current;
 current = sonar.ping_cm(); // Send out a ping and get the results in CM.
 if (previous && current) { // If either are zero, we had a no-ping result or on the first ping.
   mps = (float) (previous - current) * 0.2; // Calculate speed in m/s (positive values are coming at you, negative moving away).
 } else mps = 0; // Can't calculate speed.
 newping = true; // Flag that there's a new ping.
}


Hope this helps!

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

notsoyyo

Hi,

First of all THANK YOU TIM for this library!!!!!!!!!!!!!!!!!!!!
And thank you to all the enthusiasts who have been posting here!

I have been running into a slight problem that I have not been able to fix, despite dowloading the lib again, googling it a million times, etc. I am trying to get readings from a PING))) sensor and from a maxbotic MAX SONAR (which can be PWM, An or BW, I have it as PWM) and arduino throws the same error:
'NewPing' does not name a type

sketch_jan06a:3: error: 'NewPing' does not name a type
sketch_jan06a:4: error: 'NewPing' does not name a type
sketch_jan06a.cpp: In function 'void loop()':
sketch_jan06a:19: error: 'sonar1' was not declared in this scope
sketch_jan06a:23: error: 'sonar2' was not declared in this scope

The code I used is the modified sample one provided:
Code: [Select]
#include <NewPing.h>
//barely modified sketch from Tim Eckel's original sketch

NewPing sonar1(11, 12, 200); // Sensor 1: trigger pin, echo pin, maximum distance in cm
NewPing sonar2(9, 10, 200); // Sensor 2: same stuff

#define pingSpeed 100 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
unsigned long pingTimer1, pingTimer2;

void setup() {
  Serial.begin(9600);
  // Do other stuff here
  pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 100ms (pingSpeed)
  pingTimer2 = pingTimer1 + (pingSpeed / 2); // Sensor 2 fires 50ms later
}

void loop() {
  if (millis() >= pingTimer1) {
    pingTimer1 += pingSpeed; // Make sensor 1 fire again 100ms later (pingSpeed)
    int in1 = sonar1.ping_in();
  }
  if (millis() >= pingTimer2) {
    pingTimer2 = pingTimer1 + (pingSpeed / 2); // Make sensor 2 fire again 50ms after sensor 1 fires
    int in2 = sonar2.ping_in();
    // Both sensors pinged, process results here
  }
  // Do other stuff here, notice how there's no delays in this sketch, so you have processing cycles to do other things :)
}


Any suggestions would be greatly greatly appreciated..... Thank you!!!!!

Go Up