-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull in a few useful fixes, particularly for Python 3.12
- Loading branch information
Showing
9 changed files
with
452 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
Define real-time i/o blocks for use in block diagrams. These are blocks that: | ||
- have inputs or outputs | ||
- have no state variables | ||
- are a subclass of ``SourceBlock`` or ``SinkBlock`` | ||
""" | ||
# The constructor of each class ``MyClass`` with a ``@block`` decorator becomes a method ``MYCLASS()`` of the BlockDiagram instance. | ||
|
||
from bdsim.components import SinkBlock, SourceBlock | ||
import time | ||
import sys | ||
|
||
|
||
class FirmataIO: | ||
board = None | ||
port = "/dev/cu.usbmodem1441401" | ||
|
||
def __init__(self): | ||
from pyfirmata import Arduino, util | ||
|
||
if FirmataIO.board is None: | ||
print(f"connecting to Arduino/firmata node on {self.port}...", end="") | ||
sys.stdout.flush() | ||
FirmataIO.board = Arduino(self.port) | ||
print(" done") | ||
|
||
# start a background thread to read inputs | ||
iterator = util.Iterator(FirmataIO.board) | ||
iterator.start() | ||
time.sleep(0.25) # allow time for the iterator thread to start | ||
|
||
def pin(self, name): | ||
return FirmataIO.board.get_pin(name) | ||
|
||
|
||
class AnalogIn(SourceBlock): | ||
nin = 0 | ||
nout = 1 | ||
|
||
def __init__(self, pin=None, scale=1.0, offset=0.0, **blockargs): | ||
super().__init__(**blockargs) | ||
self.board = FirmataIO() | ||
self.pin = self.board.pin(f"a:{pin}:i") | ||
self.scale = scale | ||
self.offset = offset | ||
|
||
# deal with random None values at startup | ||
while self.pin.read() == None: | ||
time.sleep(0.1) | ||
|
||
def output(self, t, inports, x): | ||
return [self.scale * self.pin.read() + self.offset] | ||
|
||
|
||
class AnalogOut(SinkBlock): | ||
nin = 1 | ||
nout = 0 | ||
|
||
def __init__(self, pin=None, scale=1.0, offset=0.0, **blockargs): | ||
super().__init__(**blockargs) | ||
self.board = FirmataIO() | ||
self.pin = self.board.pin(f"d:{pin}:p") # PWM output | ||
self.scale = scale | ||
self.offset = offset | ||
|
||
def step(self, t, inports): | ||
self.pin.write(self.scale * inports[0] + self.offset) | ||
|
||
|
||
class DigitalIn(FirmataIO, SourceBlock): | ||
nin = 0 | ||
nout = 1 | ||
|
||
def __init__(self, pin=None, bool=False, **blockargs): | ||
super().__init__(**blockargs) | ||
self.pin = self.board.get_pin(f"d:{pin}:i") | ||
|
||
def output(self, t, inports, x): | ||
if self.bool: | ||
return [self.pin.read()] | ||
else: | ||
return [self.pin.read() > 0] | ||
|
||
|
||
class DigitalOut(FirmataIO, SinkBlock): | ||
nin = 1 | ||
nout = 0 | ||
|
||
def __init__(self, pin=None, **blockargs): | ||
super().__init__(**blockargs) | ||
self.pin = self.board.get_pin(f"d:{pin}:o") | ||
|
||
def step(self, t, inports): | ||
self.pin.write(inports[0] > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
This folder contains "drivers" for particular i/o hardware configurations. | ||
|
||
Each file contains a set of class definitions which are imported in the normal | ||
Python fashion, not dynamically as are most bdsim blocks. | ||
|
||
| File | Purpose | | ||
|------|---------| | ||
|Firmata.py | analog and digital i/o for Arduino + Firmata | | ||
|
||
Notes: | ||
|
||
- For [Firmata](https://github.com/firmata/protocol) you need to load a Firmata sketch onto the Arduino. | ||
- For now the port and the baudrate (57600) are hardwired. Perhaps the i/o system is initialized by a | ||
separate function, or options passed through BDRealTime. | ||
- There are myriad Firmata variants, but StandardFirmata is provided as a built-in | ||
example with the Arduino IDE and does digital and analog i/o. It runs fine on a Uno. | ||
- ConfigurableFirmata has more device options but needs to be installed, and its default | ||
baud rate is 115200. It does not include quadrature encoders :( | ||
- The Firmata interface is [pyfirmata](https://github.com/tino/pyFirmata), old but quite solid and efficient. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.