Page 1 of 1

Interupts

Posted: Fri Aug 01, 2014 6:45 am
by PinkInk
I'm looking to implement two different ways of controlling my nxt motor shield;

a) just drive the motor via pwm and read speed in rpm from the rotary encoder (done - http://wiki.micropython.org/contributedmodules/nxtmotor)
b) tell the motor to move a certain number of degrees, tracking it with the rotary encoder and stopping when it has

The first method uses an irq_rising on the pin that one of the encoder/tacks (well that's what lego call it) is connected to and reads the other tack Pin hi/lo status.

The second ideally should read both the rising and falling edge of one of the tacks (and do the same read of the other tack to determine direction) to give 1deg rather than 2 deg precision (notwithstanding motor spin-down), but that would either require;

- changing the interrupt function depending on mode, but once a function's been associated there's no direct way to remove, replace or update it (I've tried passing in a variable that directs to a function to pyb.ExtInt and then later changing the function the variable points at ... but that doesn't work)

- registering a single function, but one that can differentiate having been called by a rising or falling edge ... but I can't see a way to achieve that on an interrupt set as IRQ_RISING_FALLING)

Anyone got any ideas or suggestion on potential workarounds?

Re: Interupts

Posted: Fri Aug 01, 2014 6:59 am
by dhylands
You can use pyb.IRQ_RISING_FALLING if you want to detect both edges.

You can change the IRQ by setting it to None first, and then setting it to the other function.

At the processor level the EXTI IRQ doesn't tell us which edge caused the interrupt, just that an edge occurred. You could read the appropriate GPIO line from within the IRQ routine to see what the current level is.

If you use the Quadrature Encoder portion of the timer, it will (or rather it is supposed to) automatically increment and decrement the timer register appropriately. I haven't used the quadrature yet (which is why I say supposed to).

Re: Interupts

Posted: Fri Aug 01, 2014 11:44 am
by JonHylands
Here's my hacked-together pre-stmhal version of quadrature encoders, using just straight interrupt-on-change. Not very useful, except it works and shows how to set up the interrupts and registers for this kind of thing,

- Jon

Re: Interupts

Posted: Sun Aug 03, 2014 3:31 am
by PinkInk
Guys, thanks, will test these approaches when I get back from holiday.