Your explanation of what's happening is correct. Your solution may work if you get rid of the ioio.waitForConnect() and move the endBatch to where it used to be.
However, I don't understand why you're saying that you'll be missing readings with the approach I proposed. You're only discarding pulses which are not legitimate readings and the getDurationBuffered() method is supposed to give you *every* pulse. On Thu, Apr 17, 2014 at 8:01 PM, Vic Wintriss <[email protected]>wrote: > My theory was that the getDuration() call was coming too quickly and > actually reading the end of the trigger pulse. I thought that by delaying > the getDuration() a bit (or not opening it until the trigger pulse was > finished) I could avoid that. Does that hypothesis sound likely to you? > > Setting a threshold makes me miss a lot of readings. When the robot is > moving fast, I need as many valid readings as possible. > > > On Apr 17, 2014, at 6:54 PM, Ytai Ben-Tsvi <[email protected]> wrote: > > What's with the waitForConnect()? And why are you closing the pins? > The approach you took before is OK. Why did you change it? I think the > fact that you set the threshold to 0.1us is a problem. You should set it to > whatever you think is the minimum pulse width you're ever going to get. > Also, I'd have this function simply return the result as opposed to set a > field. > > > On Thu, Apr 17, 2014 at 1:15 PM, Vic Wintriss <[email protected]>wrote: > >> Sorry about the Java error…very embarrassing! >> Corrected the code, but still returns mostly zeroes. Tried this, but >> stalls after a read or two…don’t know why: >> >> public void read() throws ConnectionLostException, InterruptedException, >> IncompatibilityException >> { >> ioio.beginBatch(); >> leftStrobe.write(true); >> leftStrobe.write(false); >> leftInput = ioio.openPulseInput(LEFT_ULTRASONIC_INPUT_PIN, PulseMode. >> POSITIVE); >> ioio.waitForConnect(); >> leftDistance = leftInput.getDuration(); >> leftInput.close(); >> ioio.endBatch(); >> } >> On Apr 16, 2014, at 4:50 PM, Ytai Ben-Tsvi <[email protected]> wrote: >> >> Your local leftDistance variable is shadowing the class field... >> On Apr 16, 2014 3:18 PM, "Vic Wintriss" <[email protected]> wrote: >> >>> It’s not too important that we get every pulse. >>> I tried the following code, but still get all zeroes: >>> public class UltraSonicSensors >>> { >>> private static final int LEFT_ULTRASONIC_INPUT_PIN = 35; >>> private static final int LEFT_STROBE_ULTRASONIC_OUTPUT_PIN = 15; >>> private final PulseInput leftInput; >>> private DigitalOutput leftStrobe; >>> private float leftDistance; >>> private IOIO ioio; >>> >>> public UltraSonicSensors(IOIO ioio) throws ConnectionLostException >>> { >>> this.leftInput = ioio.openPulseInput(LEFT_ULTRASONIC_INPUT_PIN, >>> PulseMode.POSITIVE); >>> this.leftStrobe = ioio.openDigitalOutput( >>> LEFT_STROBE_ULTRASONIC_OUTPUT_PIN); >>> this.ioio = ioio; >>> } >>> >>> public void read() throws ConnectionLostException, InterruptedException >>> { >>> read(leftStrobe, leftInput); >>> } >>> >>> private void read(DigitalOutput strobe, PulseInput input) >>> throwsConnectionLostException, InterruptedException >>> { >>> ioio.beginBatch(); >>> strobe.write(true); >>> strobe.write(false); >>> ioio.endBatch(); >>> while (true) >>> { >>> float leftDistance = leftInput.getDurationBuffered(); >>> if (leftDistance > .0000001) >>> { >>> break; >>> } >>> } >>> } >>> >>> public float getLeftDistance() >>> { >>> return leftDistance; >>> } >>> } >>> What should I try next? >>> Thanks for the lead to the 4-pin sensors. I have ordered some…but since >>> a lot of the kids already have the 3-pin versions, I’m stuck with having to >>> make that work, too. >>> I use use a pretty simple circuit to separate the input from the >>> output…attached. >>> >>> Thanks for all the great help. >>> >>> On Apr 15, 2014, at 9:41 PM, Ytai Ben-Tsvi <[email protected]> wrote: >>> >>> Yes, that's what I meant when I said that you may miss the actual pulse. >>> >>> What about the following approach: >>> beginBatch(); >>> sendPulse(); >>> endBatch(); >>> while (true) { >>> float duration = pulseIn.GetDurationBuffered(); >>> if (duration > MIN_DURATION) return duration; >>> } >>> >>> Of course, you can timeout if you want. >>> With this approach, every pulse is captured and considered, and you're >>> filtering out the really short ones that indicate the input pulses. >>> >>> BTW, there are equivalent ultrasonic range finders that have a more >>> pleasant interface (for example, analog, or similar to the one you have but >>> the feedback is on a separate pin). Here's one (available on DX for <$5): >>> http://www.micropik.com/PDF/HCSR04.pdf >>> >>> >>> >>> On Tue, Apr 15, 2014 at 7:00 PM, Vic Wintriss <[email protected]>wrote: >>> >>>> Ytai: >>>> >>>> With the sync() call after the endBatch() and before the getDuration() >>>> the program hangs up and I get no readings. >>>> >>>> Vic >>>> >>>> On Apr 15, 2014, at 5:32 PM, Ytai Ben-Tsvi <[email protected]> wrote: >>>> >>>> This code doesn't have the sync() call I proposed. It should come after >>>> the batch. >>>> On Apr 15, 2014 4:43 PM, "Vic Wintriss" <[email protected]> wrote: >>>> >>>>> Ytai: >>>>> >>>>> This code produces only 1.6875E-5…reading 2 times per second. >>>>> >>>>> import ioio.lib.api.DigitalOutput; >>>>> import ioio.lib.api.IOIO; >>>>> import ioio.lib.api.PulseInput; >>>>> import ioio.lib.api.PulseInput.PulseMode; >>>>> import ioio.lib.api.exception.ConnectionLostException; >>>>> >>>>> public class UltraSonicSensors >>>>> { >>>>> private static final int LEFT_ULTRASONIC_INPUT_PIN = 35; >>>>> private static final int LEFT_STROBE_ULTRASONIC_OUTPUT_PIN = 15; >>>>> private final PulseInput leftInput; >>>>> private DigitalOutput leftStrobe; >>>>> private float leftDistance; >>>>> private IOIO ioio; >>>>> public UltraSonicSensors(IOIO ioio) throws ConnectionLostException >>>>> { >>>>> this.leftInput = ioio.openPulseInput(LEFT_ULTRASONIC_INPUT_PIN, >>>>> PulseMode.POSITIVE); >>>>> this.leftStrobe = >>>>> ioio.openDigitalOutput(LEFT_STROBE_ULTRASONIC_OUTPUT_PIN); >>>>> this.ioio = ioio; >>>>> } >>>>> >>>>> public void read() throws ConnectionLostException, >>>>> InterruptedException >>>>> { >>>>> read(leftStrobe, leftInput); >>>>> } >>>>> >>>>> private void read(DigitalOutput strobe, PulseInput input) throws >>>>> ConnectionLostException, InterruptedException >>>>> { >>>>> ioio.beginBatch(); >>>>> strobe.write(true); >>>>> strobe.write(false); >>>>> ioio.endBatch(); >>>>> this.leftDistance = input.getDurationSync(); >>>>> } >>>>> >>>>> public float getLeftDistance() >>>>> { >>>>> return leftDistance; >>>>> } >>>>> } >>>>> On Apr 15, 2014, at 4:27 PM, Ytai Ben-Tsvi <[email protected]> wrote: >>>>> >>>>> Can you send me the most "correct" version of your code that >>>>> demonstrates the issue? >>>>> I can probably make the necessary changes to sync () if this is indeed >>>>> a problem. >>>>> On Apr 15, 2014 4:03 PM, "Vic Wintriss" <[email protected]> wrote: >>>>> >>>>>> Thanks for the ideas. I've tried many combinations of synch, >>>>>> readBuffered...etc, but unfortunately it still reads very small >>>>>> numbers...probably measuring the last part of the trigger pulse. Is there >>>>>> any way that I can delay the read by a couple of hundred micro seconds? >>>>>> Could you do the..... sync() call "send" half inside the batch and >>>>>> "wait" >>>>>> half outside thing? >>>>>> >>>>>> Thanks for all the help. The kids are trying to use these sensors >>>>>> for iARoC 2014 (the International Autonomous Robot Competition) here in >>>>>> San >>>>>> Diego. >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "ioio-users" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to [email protected]. >>>>>> To post to this group, send email to [email protected]. >>>>>> Visit this group at http://groups.google.com/group/ioio-users. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> >>>>> -- >>>>> You received this message because you are subscribed to a topic in the >>>>> Google Groups "ioio-users" group. >>>>> To unsubscribe from this topic, visit >>>>> https://groups.google.com/d/topic/ioio-users/3MDLEEKtejY/unsubscribe. >>>>> To unsubscribe from this group and all its topics, send an email to >>>>> [email protected]. >>>>> To post to this group, send email to [email protected]. >>>>> Visit this group at http://groups.google.com/group/ioio-users. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>>> >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "ioio-users" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to [email protected]. >>>>> To post to this group, send email to [email protected]. >>>>> Visit this group at http://groups.google.com/group/ioio-users. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> -- >>>> You received this message because you are subscribed to a topic in the >>>> Google Groups "ioio-users" group. >>>> To unsubscribe from this topic, visit >>>> https://groups.google.com/d/topic/ioio-users/3MDLEEKtejY/unsubscribe. >>>> To unsubscribe from this group and all its topics, send an email to >>>> [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at http://groups.google.com/group/ioio-users. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>>> >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "ioio-users" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> To post to this group, send email to [email protected]. >>>> Visit this group at http://groups.google.com/group/ioio-users. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> >>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "ioio-users" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/ioio-users/3MDLEEKtejY/unsubscribe. >>> To unsubscribe from this group and all its topics, send an email to >>> [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/ioio-users. >>> For more options, visit https://groups.google.com/d/optout. >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "ioio-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/ioio-users. >>> For more options, visit https://groups.google.com/d/optout. >>> >>> >>> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "ioio-users" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/ioio-users/3MDLEEKtejY/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/ioio-users. >> For more options, visit https://groups.google.com/d/optout. >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "ioio-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/ioio-users. >> For more options, visit https://groups.google.com/d/optout. >> > > > -- > You received this message because you are subscribed to a topic in the > Google Groups "ioio-users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/ioio-users/3MDLEEKtejY/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/ioio-users. > For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "ioio-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/ioio-users. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "ioio-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/ioio-users. For more options, visit https://groups.google.com/d/optout.
