Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling interrupts in OOP
#1
This is a project in process control. Originally a software simulator to keep my busy during long winter nights (two years ago). Over time the project has moved to real hardware to control and has kind of gotten out of hand. Top level looks like this.

[Image: TopLevel.png]

My current focus is on the Raspberry Pi, Debian (Linux), python 3.7, . The Arduino runs a working application in C++ and currently the laptop application is written in Java. These two JMRI programs work fine, but don’t do exactly what I want. Also I ran out of I/O pins on the Arduino for sensors and want to control 56 servos. So I added the Raspberry in the middle.
My (current) problem is checking the status of hardware sensor. I want to be able inquire the current state of a single sensor and have changes reported by interrupt when a change occurs. No problem doing one or the other but my OO structure, who imports who, prevents me from doing both.
[Image: Softwar-3.png]
The problem is with the path from Traffic -> Command ->Sensor -> WabbitFB -> (the real hardware).
TT3 just starts an instance of Traffic and Fan.
The Traffic Class handles all the communication between the three processors and “knows” only that an instructions from the laptop ends with a “>”. Traffic ask Command what to do with a received instruction – process locally, send to the Arduino or both.
The Command Class processes instructions from the laptop. Some instructions I have added, some are subset of original instruction. All instruction format and validity is checked then passed to Sensor or Signals.
The Sensor Class handles instructions related to the sensors used to report the current state of the hardware available from class WabbitFB. Following the original sensor instructions between the laptop and the Arduino this class receives instructions to map from sensor ID (used by the laptop) and GPIO pins (used by the hardware/WabbitFB class). Command/hardware status is returned to the laptop.
Class WabbitFB sets up the hardware GPIO pins, interrupts.
So Traffic imports Command, Command imports Sensor, Sensor imports WabbitFB. Sensor can see into WabbitFB to call a function to retrieve the current status of a sensor. The problem is WabbitFB can not see into Sensor to report a change in status flagged by an interrupt. OR I don’t know how to code the circular reference. If I try to also import Sensor into WabbitFB I get an error.
Trying to turn things around, importing Sensor into WabbitFB, just moves the problem between Command and Sensor.
The Arduino package avoids this problem by scanning each sensor and not using interrupts. I am thinking that interrupts have elegances.
Creating a new path from the hardware (WabbitFB) back to Traffic seems to require a copy of the GPIO-to-sensor ID mapping. This at some point also results in a circular reference.
Is there a way to program both 1) an inquiry of status on demand and 2) reporting status change triggered by interrupt?
Say what you will about Sisyphus. He always has work.
Reply
#2
Well I found an answer. Not elegant, but it works (so far – still testing).

To refresh from my Original Post, the project is a headless Raspberry Pi setting between a laptop with GUI and an headless Arduino. Both the Raspberry and Arduino control and get status from the hardware. Originally the laptop was connected directly to the Arduino micro-processer.

Connected to the Raspberry Pi are 24 sensors. The WabbitFB module/Class configures manages that interface. Following the original design I want/need to be able to 1) send request from the laptop to check sensor status and 2) when a sensor changes automatically send changes to the laptop. Module Sensor maintains a mapping from sensor ID used by the laptop to hardware connection (Raspberry Pi add on board/pin).

The intent of the structure is to separate/isolate functionality and data. The TT3 module creates an instance of Traffic getting things started, among some other things.

[Image: New_flow.png]

The Traffic module manages all traffic between the three processors. Traffic only knows that a command ends with a “>”.

The Command module separates sensor commands from others.

Sensor maps the sensor ID used by the laptop to the hardware pin needed by module WabbitFB.

Module WabbitFB returns the current status of the sensor passed.

The problem is/has been also reporting the asynchronous interrupt driven status changes. The hardware information from WabbitFB needs to go through Sensor to be translated from hardware pin to sensor ID. This circular reference Sensor import WabbitFB and WabbitFB importing Sensor is flagged as a error. “WabbitFB can’t import Sensor.”

My clue was that the same type of loop between Traffic, Command and Sensor is not flagged as an error. In the picture black arrows show the flow for laptop initiated commands. Red arrows show the flow for hardware initiated data flow.

Inserting the dummy module gets around the error seen by PYCharm and Python 3.7. So it runs. However, Sphinx sees this as errors when all modules are included in the same documentation run.

NOT elegant. Not safe and may break for some unknown reason. Working on simple code to demonstrate the error and fix but not intuitively obvious. For example I had to move Class Sensor and Class Command to different modules to clear all errors. Don't understand why.
Reply
#3
Thanks for sharing
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,373 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Problems with Interrupts/ callback functions in Python for an Alarm Clock project Henry1 1 4,945 Dec-16-2016, 10:17 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020