I have a relaisboard connected to an arduino via firmata protocol using Python bindings. The communication works without problems using pyfirmata (https://github.com/tino/pyFirmata).
The relaisboard has 16 relais. Every group of 3 relais is a channel. Every channel is connected to a Device Under Test input or output. This just to have a rough description of the purpose of the releboard.
Below you can find a skeleton of the code.
#!/usr/bin/env python
__version__ = '0.1'
# Fault Injection Unit
# Power is connected to Fault Bus 1
# Ground is connected to Fault Bus 2
from pyfirmata import Arduino
class FaultInsertionBoard(object):
def __init__ (self, comPort = 'COM3'):
"""Initalize the Fault insertion Board
Open communication with host via serial port
Arguments:
comPort -- The serial port used to connect the board to the host.
"""
self.board = Arduino(comPort)
class Channel(object):
def __init__ (self, aChannel):
""" Create a Channel"""
pass
def NoFault():
""" Set the channel to the "No fault" condition
No Fault condition is:
-- DUT channel connected to the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel disconnected from the Fault bus 2
"""
pass
def OpenCircuit():
""" Set the channel to the "Open Circuit fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel disconnected from the Fault bus 2
"""
pass
def ShortToGround():
""" Set the channel to the "Short to Ground fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem
-- DUT channel disconnected from the Fault bus 1
-- DUT channel connected to the Fault bus 2
"""
pass
def ShortToPower():
""" Set the channel to the "Short to Ground fault" condition
Open Circuit fault condition is:
-- DUT channel disconnected from the testing sistem: channel relay is open
-- DUT channel connected to the Fault bus 1: Fault Bus 1 relay is closed
-- DUT channel disconnected from the Fault bus 2: Fault Bus 1 relay is open
"""
pass
def main():
FaultBoard = FaultInsertionBoard('COM3')
VoutSensor = FaultBoard.Channel(0)
IOutSensor = FaultBoard.Channel(1)
VoutSensor.NoFault()
IOutSensor.NoFault()
VoutSensor.ShortToGround()
IOutSensor.ShortToPower()
if __name__ == "__main__":
main()
Where:
FaultInsertionBoard
is a simple wrapper of theArduino
class inFirmata
.Channel(n)
identifies then
-th group of three relaisNoFault
,ShortToPower
,ShortToGround
are various configurations of the three relais of each channel (it does not matter the actual configuration).
Now the question: I have a very good experience with embedded firmware written in C, far less in Python. Obviously the code above is not correct.
Can someone suggest me a class framework in order to get the above functionality? In other words, how can I write the Python code in order to drive the relais as described above?
PS: alternatively I could write something like this:
FaultBoard = FaultInsertionBoard('COM3')
FaultBoard.Channel(0).NoFault()
but I think it is less elegant and clear.