Our team bought the Ping))) sensor for distance sensing. As per this forum post, we could use a PWM Y-cable to connect it to the digital sidecar. How do we code this in Java? I believe that the WPILib Ultrasonic class is meant for those sensors with an input and output.
The Ping))) has a built-in SX microcontroller with the job of generating the pulse and patiently waiting for that pulse to make it’s way back. It then sends back a pulse, with a width that translates into the distance, by the multiplication of a specific constant, 2260 for centimeters and 890 for inches.
Note that the time units are in microseconds.
How do I send 30 microsecond pulse to a digital output and read back the input pulse, probably from a different pin if I use the Y-cable method?
*DigitalOutput*.set(true); //Needs to be a properly constructed DigitalOutput object.
Timer.delay(0.00003); //This is a static method.
*DigitalOutput**.set(false); //Same object from above.
while {*DigitalInput*.get()=false} ////Needs to be a properly constructed DigitalInput object.
double starttime=Timer.getFPGATimestamp(); //This is a static method. You can use a Timer object, but I like this method better.
while{*DigitalInput*.get() = true} //Same object from above.
double pingtime = Timer.getFPGATimestamp() - starttime; //Same method from above.
double distance = pingtime * *(appropriate scalar)*;
This should be separately threaded so the microsecond timing doesn’t depend on the control loop and so the wait loops don’t cause a loss of robot control.
I’m not sure this will work for a few reasons. First, I doubt that our setup with Java on the cRIO (or Java running on anything) will time the 30 microsecond pulse very accurately if you set it up the way you have it now.
Also, you’d need to run this loop really quickly to capture the pulse. If you’re one inch away, your pulse is less than a millisecond away. It’s possible to miss pulses entirely.
You really should use interrupts for this, or you should just add an arduino board that communicates with the cRIO with serial. I bet somebody has written an arduino library for this sensor.
Also, your timing method doesn’t seem to compare the two FPGA timestamps to find the duration of the pulse. I don’t think your two while loops will work the way you’d like them to.
I was going to use a Parallax Propeller board. I’ve never used an Arduino, but have used many of Parallax products, including this sensor in the past. Parallax itself provides libraries which should be sufficient to perform this task.
That was the one thing I was wondering. If you disregard the start time, the measurement would be quite wrong!
It is a good starting point and I am happy that you shared this. We are pressed for time in our team and we just got a bot ready to program using. That means that our programming team is heavily behind at this moment.
I am considering telling my team not to waste time on sonar. I am already able to make vision systems that are able to mimmic this functionality, but without the main issues such as range, accuracy and constant calibration. This sensor is also quite slow, not fast enough for powering automated movement. The main use for this is obstacle detection while moving when video stabilization is not enough to remove the motion blur.