Interupts

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
PinkInk
Posts: 61
Joined: Tue Mar 11, 2014 3:42 pm

Interupts

Post by PinkInk » Fri Aug 01, 2014 6:45 am

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?

User avatar
dhylands
Posts: 3140
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Interupts

Post by dhylands » Fri Aug 01, 2014 6:59 am

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).

User avatar
JonHylands
Posts: 64
Joined: Sun Dec 29, 2013 1:33 am

Re: Interupts

Post by JonHylands » Fri Aug 01, 2014 11:44 am

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
Attachments
encoder.zip
(1.83 KiB) Downloaded 121 times

PinkInk
Posts: 61
Joined: Tue Mar 11, 2014 3:42 pm

Re: Interupts

Post by PinkInk » Sun Aug 03, 2014 3:31 am

Guys, thanks, will test these approaches when I get back from holiday.

Post Reply