Any examples of blocking on two digital input signals?

262 views
Skip to first unread message

Curt Thompson

unread,
Apr 7, 2012, 10:58:15 AM4/7/12
to ioio-...@googlegroups.com

Are there any examples of an IOIO app that waits for two digital inputs at the same time.  I would like to count the pulses on two digital inputs.  I’d like to use two "in.waitForValue(true/false)” method calls.  Can I instantiate two “loop()” methods?  Should I create a second execution thread? What happens when the IOIO disconnects/connects while I’m blocking on the second thread?

 

Curt

Ytai Ben-Tsvi

unread,
Apr 9, 2012, 3:08:11 AM4/9/12
to ioio-...@googlegroups.com
Inline

On Sat, Apr 7, 2012 at 7:58 AM, Curt Thompson <charliev...@hotmail.com> wrote:

Are there any examples of an IOIO app that waits for two digital inputs at the same time.  I would like to count the pulses on two digital inputs.  I’d like to use two "in.waitForValue(true/false)” method calls.  Can I instantiate two “loop()” methods?

No.
 

Should I create a second execution thread?


Yes. Create another thread from setup() and pass the IOIO instance or the DigitalInput instance to it.
 

What happens when the IOIO disconnects/connects while I’m blocking on the second thread?


waitForValue() will throw a ConnectionLostException. You can catch it and exit the thread.

Having said that, if you want to save some pain, check out the implementation of DigitalInput, and you'll see that there's a setValue method that will be called every time the pin value changes. You can slightly modify the implementation to count pulses, and add an API method to get the number of pulses. In addition to saving you the extra thread, this will also guarantee that you never miss a pulse.
 

 

Curt

--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To post to this group, send email to ioio-...@googlegroups.com.
To unsubscribe from this group, send email to ioio-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ioio-users?hl=en.

Curt Thompson

unread,
Apr 9, 2012, 3:08:33 PM4/9/12
to ioio-...@googlegroups.com

Excellent!  Thanks for the tips.  I’ll let you know how it goes.

 

Curt

Curt Thompson

unread,
Apr 10, 2012, 11:43:39 PM4/10/12
to ioio-...@googlegroups.com

Ytai,

 

That works pretty well.  Modified setValue() in the library to count the number of digital input state changes.  I have  sensor probes wired to the front and back wheel of my car.  I can count wheel movement and derive the odometer value.  I did a couple of tests over the same 3 mile course.  Ended up with the same odo value within about 2 meters.  I tried to go at two different speeds to see if I might be missing pulses at the higher speed – it looks very good.

 

I also have it timing the duration between four digital input state changes (one tire revolution).  I get the car speed from this.  It’s a little noisy.  I think this is because the SystemClock.elapsedRealtime() call that I’m using probably isn’t reporting time continuously (updating its millisecond value by something other than every millisecond).

 

Wondering if I could use a Digital Input and a Pulse Input at the same time on the same pin?  Then I could let the IOIO firmware measure the duration between pulses (Pulse Input) and then count the Digital Input state transitions at the same time.

 

Thanks again.

 

Curt

 

p.s. Just got a PICkit3.  Might try my hand at some IOIO firmware hacking.

--

Ytai Ben-Tsvi

unread,
Apr 10, 2012, 11:53:27 PM4/10/12
to ioio-...@googlegroups.com

Ideally, there would be an encoder function that can give you both.
For now, you can wire the same line to two pins and then open one for digital input and the other for pulse input.

Curt Thompson

unread,
Apr 11, 2012, 12:00:48 AM4/11/12
to ioio-...@googlegroups.com

I should have thought of that.  You are the best!

 

Thanks,

 

Curt

 

From: ioio-...@googlegroups.com [mailto:ioio-...@googlegroups.com] On Behalf Of Ytai Ben-Tsvi
Sent: Tuesday, April 10, 2012 8:53 PM
To: ioio-...@googlegroups.com
Subject: RE: Any examples of blocking on two digital input signals?

 

Ideally, there would be an encoder function that can give you both.

Curt Thompson

unread,
Apr 11, 2012, 11:30:49 PM4/11/12
to ioio-...@googlegroups.com

Ytai,

 

I wired a DigitalInput and a PulseInput to the same wheel sensor.  That works great.  The car odometer and speed are very stable now.

 

Calls to the read functions like getFrequency() seem to block until I get the first digital signal transition.  Is there something I call (poll the first time) so the code doesn’t hang there when I start up with the car stopped?

 

Is there any way to have the pulse input stop counting when it reaches its max count?  Maybe even reporting its frequency as zero when that happens?  When the counter rolls over after its max count it causes grief as I get a bogus frequency value and display a weird speed value for a few seconds.

 

Curt

 

 

From: ioio-...@googlegroups.com [mailto:ioio-...@googlegroups.com] On Behalf Of Curt Thompson
Sent: Tuesday, April 10, 2012 9:01 PM
To: ioio-...@googlegroups.com
Subject: RE: Any examples of blocking on two digital input signals?

 

I should have thought of that.  You are the best!

 

Thanks,

 

Curt

 

From: ioio-...@googlegroups.com [mailto:ioio-...@googlegroups.com] On Behalf Of Ytai Ben-Tsvi
Sent: Tuesday, April 10, 2012 8:53 PM
To: ioio-...@googlegroups.com
Subject: RE: Any examples of blocking on two digital input signals?

 

Ideally, there would be an encoder function that can give you both.
For now, you can wire the same line to two pins and then open one for digital input and the other for pulse input.

--

Ytai Ben-Tsvi

unread,
Apr 12, 2012, 3:13:54 AM4/12/12
to ioio-...@googlegroups.com


On Apr 11, 2012 8:30 PM, "Curt Thompson" <charliev...@hotmail.com> wrote:
>
> Ytai,
>
>  
>
> I wired a DigitalInput and a PulseInput to the same wheel sensor.  That works great.  The car odometer and speed are very stable now.
>
>  
>
> Calls to the read functions like getFrequency() seem to block until I get the first digital signal transition.  Is there something I call (poll the first time) so the code doesn’t hang there when I start up with the car stopped?
>
>  

You could wrap getFrequency() with your owm method that sets a timer task to interrupt the calling thread after some timeout, and return 0 in this case.


>
> Is there any way to have the pulse input stop counting when it reaches its max count?  Maybe even reporting its frequency as zero when that happens?  When the counter rolls over after its max count it causes grief as I get a bogus frequency value and display a weird speed value for a few seconds.
>
>  

Are you using double precision? If so, you can interrupt as mentioned above after a certain timeout and consider this a 0 freq. No? Only possible problem is that your next read might be off. I don't remember this code so well, but this is likely fixable.

Reply all
Reply to author
Forward
0 new messages