nanpy / nanpy

Use your Arduino board with Python.
http://nanpy.github.io/
MIT License
233 stars 79 forks source link

Add interrupts support #14

Open astagi opened 10 years ago

astagi commented 10 years ago

From Cameron's mail:

I'm building a power control unit that uses phase chopping to 'dim' the power to a high power heating element. To do this you fire a triac at a frequency that only allows a portion of the sine wave to reach the load. To do this you need to syncronize the firing of the triac with the sin wave zero crossing: http://en.wikipedia.org/wiki/Phase_fired_controllers

The example line of code we would like to replicate is the following:

attachInterrupt(0, zero_crosss_int, RISING);

where we designate interrupt pin 0 (different pin numbering for the interrupts..) is chosen so that when the signal is RISING it will execute the 'zero_cross_int' function regardless of what else the arduino is doing.

KyawZinLatt commented 9 years ago

How can I use the INTERRUPT function in nanpy? Which class do I need to import from nanpy? And how can I use it? Plz show with example if possible because I am a beginner using nanpy

astagi commented 9 years ago

At the moment Nanpy hasn't any interrupt support. I'll schedule it for 0.9.4 release, thanks :)

KyawZinLatt commented 9 years ago

this means that I cannot use nanpy right now if I want to use for sensor that need interrupt?

astagi commented 9 years ago

Adding interrupt support is not a simple task, I'm still working on the implementation.

What kind of sensor you want to use? Do you have a C library to control it?

KyawZinLatt commented 9 years ago

Here is the sensor that I need to use http://www.adafruit.com/product/828 This sensor need to use Interrupt

astagi commented 9 years ago

Uh I see. I'll try working on interrupts, anyway I suggest to wrap anything in a library or try fetching the pin value at specific intervals (polling)

On Thu, Feb 26, 2015 at 4:36 PM, KyawZinLatt notifications@github.com wrote:

Here is the sensor that I need to use http://www.adafruit.com/product/828 This sensor need to use Interrupt

— Reply to this email directly or view it on GitHub https://github.com/nanpy/nanpy/issues/14#issuecomment-76199112.

Andrea Stagi (@4stagi) - Develover @Nephila Job profile: http://linkedin.com/in/andreastagi Website: http://4spills.blogspot.it/ Github: http://github.com/astagi

KyawZinLatt commented 9 years ago

I dont know about wraping anything in a library but for fetching the pin value at specific intervals (poling) will result in, in my opinion, inaccurate data result. Because the sensor need to calculate the amount of water whenever the water comes in. (just my opinion, how do you think?)

KyawZinLatt commented 9 years ago

https://github.com/adafruit/Adafruit-Flow-Meter/blob/master/Adafruit_FlowMeter.pde This is the example code I am using for this sensor http://www.adafruit.com/product/828 Do you think I can get the correct data without using the Interrupt? I do not know how to change the code without using Interrupt because I am not so familiar with Arduino yet

astagi commented 9 years ago

Yes, interrupts are better in terms of efficiency. I don't know what to suggest, just try :) You can write a class and store pulses using a custom ISR, then store them into a variable and polling with Nanpy, but yeah it's not so easy to do :(

We're talking about Nanpy, that it's "just" a library that calls remote methods from your PC or Raspberry Pi to Arduino. This is not hard to do, you just need to define a protocol that specifies how to call functions/methods, send parameters and receive a return value, but imagine the interrupt case:attaching an interrupt in Arduino is straightforward, you just need to call a function (http://arduino.cc/en/Reference/AttachInterrupt), and this is something that Nanpy can easily do. Using Nanpy you can call something like this:

def blink():
    # manage your interrupt
    pass

Interrupts.attachInterrupt(0, blink, Interrupts.CHANGE)

The main problem here is the ISR (interrupt service routine) parameter that this function wants: to attach an interrupt you need to define a function and pass its address to the attachInterrupt, also it must be a function without parameters, so within the function you won't know which type of interrupt and/or trigger is managing, so routing is not easy!

The only thing I can do is defining a set of static functions using a combination of interrupts and trigger types, and put them into a matrix of function pointers, something like:

interrupt_routers[0][0] = interrupt_0_change
interrupt_routers[1][0] = interrupt_1_change
interrupt_routers[0][1] = interrupt_0_low
interrupt_routers[1][1] = interrupt_1_low

calling Interrupts.attachInterrupt you will select a function from matrix and then you will call firmware attachInterrupt with the selected function (and the type of the interrupt and the trigger).

void interrupt_0_change() {
    Serial.println("0|CHANGE");
}

void interrupt_1_low() {
    Serial.println("1|LOW");
}

Trigger types are strings for clarity.

I just need to verify if Serial communication can be used within ISRs. Another big problem is that Nanpy needs to keep listening from Arduino. Each function will notify Nanpy that the interrupt callback has been called and then your python callback will be executed. @ponty any thoughts?

ponty commented 9 years ago

I would use a wrapper code for interrupts and polling which seems to be much easier than rewriting the communication.

jonbarlo commented 9 years ago

Hell guys, im just wondering if some one is working on this, i would love to use nanpy instead of arduino as slave, it is 10 times easier to connect usb than a bunch of cables...

thanks in advance for this effort it will make the community really happy!

cheragnb commented 8 years ago

75

I figured out a way to overcome this here

amrsohil commented 4 years ago

It's 2019 and i cannot find a single example online on interrupt support! , i'm trying to use the code mentioned here https://www.circuitar.com/nanoshields/modules/zero-cross/ using python ?

shayanb commented 4 years ago

Bump

er1z commented 4 years ago

@amrsohil — sometimes I think that project is kinda abandoned.

astagi commented 4 years ago

Hi everybody, I created Nanpy a long time ago and now I'm not the active maintainer. I don't know if @ponty is still checking issues and PR. If not I'll try to come back active in the project and help with issues and support requests.