Go Down

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

bulut



Have you tried connecting it and using the sample NewPing sketch?  By the pin labels, it sounds like it could work as-is.  The 120V is a little confusing, but the specs seems a little thin to begin with.  Got a picture or more information?

Tim
[/quote]

i tried simple new ping sketch but it just reads up to 40cm and not so much accurate , in you sketch the ping pulse is 1 pulse with 10us width i think if we manipulate the code to 2 or 3 pulses with 16us width it will solve my problem,and i think we have to increase the frequency too.how should we change the code?

bulut

i think we have to change this part of code
Code: [Select]
*_triggerOutput &= ~_triggerBit; // Set the trigger pin low, should already be low, but this will make sure it is.
delayMicroseconds(4);            // Wait for pin to go low, testing shows it needs 4uS to work every time.
*_triggerOutput |= _triggerBit;  // Set trigger pin high, this tells the sensor to send out a ping.
delayMicroseconds(10);           // Wait long enough for the sensor to realize the trigger pin is high. Sensor specs say to wait 10uS.
*_triggerOutput &= ~_triggerBit; // Set trigger pin back to low.


i need 16 bursts with period of 21us  and frequency of 40KHZ for my trigger , how should i change the code?


i tried simple new ping sketch but it just reads up to 40cm and not so much accurate , in you sketch the ping pulse is 1 pulse with 10us width i think if we manipulate the code to 2 or 3 pulses with 16us width it will solve my problem,and i think we have to increase the frequency too.how should we change the code?


The 1 pulse / 10uS width is just to trigger the sensor.  The sensor detects this and then sends out it's own multiple pulses.  In other words, the sensor doesn't just send out one 10uS pulse, we just trigger it.  I have not seen a sensor that doesn't automatically send out multiple pulses from a trigger.  Are you sure it's not ready doing this?  The fact that it reads distances appears that it's working.  Do you even know the maximum sensor distance?  Maybe it's only 40cm.  Also, you may need to change the uS to CM conversion if your sensor puts out different values.  The URM37 sensor is a good example of this.   For the URM37 you set US_ROUNDTRIP_CM to 50, as that's how it converts and outputs the distance values.

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

CyklKent


bulut


Quote
The 1 pulse / 10uS width is just to trigger the sensor.  The sensor detects this and then sends out it's own multiple pulses.  In other words, the sensor doesn't just send out one 10uS pulse, we just trigger it.  I have not seen a sensor that doesn't automatically send out multiple pulses from a trigger.

i know it how the sensor works but this sensor dosnt operate with 1 pulse / 10 us it works with 16 pulses with period of 21us  and frequency of 40KHZ if i can apply this to my sensor it will work, now i just want to know how should i produce this 16 pulses(40KHZ)

Quote
Do you even know the maximum sensor distance?  Maybe it's only 40cm


the maximum is 5 meter , it reads 40cm but not accurate it has 13cm error.




Do your sensors look like these?:



exactly the same


So, you're trying to use just the sensor without the control board?  Many people have tried doing this and I have yet to hear of anyone who was successful.  The same is true with the typical HC-SR04 type sensors.  You can't just use the sensors without the control board.

Basically, if you want to use these sensors, you're going to need to interface with the control board, not the sensors directly.  It's not something that can be fixed with software.  You need hardware to amplify the signals, issue the pings, etc.  You can't just connect them to an Arduino, they won't work that way.

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

bulut


Quote
So, you're trying to use just the sensor without the control board?


no i am not trying to use the sensor without the board , in my first post i noted that i am using the sensor board . i know if i connect the sensor directly to the arduino it wont work.

Quote
It's not something that can be fixed with software

believe me if we apply the 16 pulses with 21us period with 40KHZ frequency i will be work i am sure, so please tell me how to change the code to produce this pulses .

Quote
You need hardware to amplify the signals

the sensor board has the amplifyer inside.

check the pictures


believe me if we apply the 16 pulses with 21us period with 40KHZ frequency i will be work i am sure, so please tell me how to change the code to produce this pulses .


The NewPing library doesn't do the ultrasonic pulses.  It only triggers the control board which does the pulses.  It's very odd that this control board doesn't do these pulses (that's one of the duties of the control board). I've never heard of a control board that doesn't do the required pulses that the control board's decoder requires.  It only makes sense that the control board would issue the correct pulses that it needs on the decode side.  It could be how you're doing the ping trigger.  Maybe you're not doing the trigger, but actually just a single fire?  In other words, maybe you're connecting to the wrong place on the control board.

In any case, doing these pulses is outside the scope of the NewPing library as it would drastically change things and be specific to one individual control board.

What you'll need to do is create your own control function or library.  In order to do the pulses with accuracy, you'll need to look into PWM.  I don't think the standard Arduino PWM will work as you need to set a frequency of around 47 kHz (21uS) while the maximum Arduino PWM is 1 kHz.  Using timers you can get as high as 64 kHz, but I believe the next step would be 32 kHz so you could still have a problem.

I'd really try to get the control board to do the correct pulses (that's its job anyway).  If you can't, try looking for PWM library that can do high frequency pulses.

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

hbaxton

I am using the new Ping library with my code and have hit a dead end.  My code is running a SparkFun stepper motor in a loop where it slowly increments the RPM.  I eventually want to control the speed based on the distance measurement from my Ping((( sensor, but I've dumbed it down to find the error.  It's not the best code for my  motor, but for example sake it works.  The problem is that even though I use the ping_timer like in the example calling:

sonar.ping_timer(echoCheck);

stops the motor from moving.  once I comment that line out the motor starts working again.  below is the code.  any ideas?
Code: [Select]

#include <NewPing.h>
#include <AFMotor.h>


#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

AF_Stepper motor(200, 1);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
unsigned int pingSpeed = 1000; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer;     // Holds the next ping time.
int rpm =10;

void setup(){
  Serial.begin(115200);
  pingTimer = millis(); // Start now.
 
  Serial.println("Stepper test!");
  motor.setSpeed(100);  // 100 rpm   
  motor.step(100, FORWARD, SINGLE);
  Serial.println("step done");
  motor.release();
  delay(1000);
 
}

void loop(){
  // Notice how there's no delays in this sketch to allow you to do other processing in-line while doing distance pings.
 
  if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  }
 
 
  Serial.print("setting RMP = ");
  Serial.println(rpm);
  motor.setSpeed(rpm);
  Serial.println();
  Serial.println();
  motor.step(100, FORWARD, SINGLE);
  rpm = rpm +10;
  delay(1000);
}
  void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  // Don't do anything here!
  if (sonar.check_timer()) { // This is how you check to see if the ping was received.
    // Here's where you can add code.
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
    Serial.println("cm");
  }
 
 
}


cyclegadget

I think this part of the code is incorrect. It will always check as true. That is all that I have found at the moment.

Code: [Select]
if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  }


Take a look at the blink without delay example.
Code: [Select]
unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis; 
   
   
    // run function here
   
  }
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

hbaxton

that code was taken straight from the NewPingEventTimer example code, and as far as I can tell should not always be true.


I am using the new Ping library with my code and have hit a dead end.  My code is running a SparkFun stepper motor in a loop where it slowly increments the RPM.  I eventually want to control the speed based on the distance measurement from my Ping((( sensor, but I've dumbed it down to find the error.  It's not the best code for my  motor, but for example sake it works.  The problem is that even though I use the ping_timer like in the example calling:

sonar.ping_timer(echoCheck);

stops the motor from moving.  once I comment that line out the motor starts working again.  below is the code.  any ideas?


I'd start debugging this by removing the Serial.print/Serial.println stuff inside the echoCheck subroutine.  This is only there for a test, you should eventually replace it with code that processes the ping result.  But for now you can simply delete it.  If it still doesn't work correctly, it could be that the AFMotor library is also using the same timer that NewPing is using to do the stepper motor control.

Also, cyclegadget is correct, you're not really using the pingTimer correctly.  Both ping_timer() and the pingTimer variable are designed to be used WITHOUT delays.  If you're using delays, it's going to not work correctly.

Basically, if you're going to use delays, you should not use any of the polling (pingTimer) stuff and shouldn't use the ping_timer() method.  You should just use the standard ping() method.  If you want to create a more event driven sketch, you need to be comfortable with not using delay commands.

I simplified your sketch and used the standard ping() method (well, ping_cm() which is ping that converts the result to centimeters).  This may make more sense and be a closer fit to what you're trying to do.

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

AF_Stepper motor(200, 1);
NewPing sonar(7, 7, 200); // NewPing setup of pins and maximum distance.
int rpm = 10;

void setup(){
 Serial.begin(115200);
 Serial.println("Stepper test!");
 motor.setSpeed(100);  // 100 rpm  
 motor.step(100, FORWARD, SINGLE);
 Serial.println("step done");
 motor.release();
 delay(1000);
}

void loop(){
 int cm = sonar.ping_cm();
 Serial.print(cm);
 Serial.println(" cm");
 
 Serial.print("setting RMP = ");
 Serial.println(rpm);
 motor.setSpeed(rpm);
 Serial.println();
 Serial.println();
 motor.step(100, FORWARD, SINGLE);
 rpm += 10;
 delay(1000);
}
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone


that code was taken straight from the NewPingEventTimer example code, and as far as I can tell should not always be true.


It is always true, because you're adding delays and telling it to start right away, yet then adding a full second delay.  Basically, you need to either create an event driven/polling type sketch, or one with delays.  Combining the two could give you problems.

It doesn't seem you're comfortable with an event driven/polling type sketch.  So, you shouldn't really try to use any example NewPing sketch that uses the ping_timer() method.  Stick with ping(), ping_cm(), ping_in(), and ping_median() till you're comfortable with not using the delay() command in your sketch.

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

hbaxton

hi teckel & cyclegadget,

thank you for your response.  i am new to events.  so i tried taking out all the delays and also tried calling the ping function directly but i had the same results.  i'm going to look into the adafruit motor library to see if it is using the timer as the new ping library.

thanks again!  i'll report back.

Go Up