|
| 1 | +"""The Arduino module provides an interface to the Arduino firmware.""" |
| 2 | +from enum import Enum, IntEnum |
| 3 | + |
| 4 | +from sbot.logging import log_to_debug |
| 5 | +from sbot.serial_wrapper import SerialWrapper |
| 6 | +from sbot.utils import map_to_float |
| 7 | + |
| 8 | +from .utils import BoardManager |
| 9 | + |
| 10 | + |
| 11 | +class GPIOPinMode(str, Enum): |
| 12 | + """The possible modes for a GPIO pin.""" |
| 13 | + |
| 14 | + INPUT = 'INPUT' |
| 15 | + INPUT_PULLUP = 'INPUT_PULLUP' |
| 16 | + OUTPUT = 'OUTPUT' |
| 17 | + |
| 18 | + |
| 19 | +class AnalogPin(IntEnum): |
| 20 | + """The analog pins on the Arduino.""" |
| 21 | + |
| 22 | + A0 = 14 |
| 23 | + A1 = 15 |
| 24 | + A2 = 16 |
| 25 | + A3 = 17 |
| 26 | + A4 = 18 |
| 27 | + A5 = 19 |
| 28 | + |
| 29 | + |
| 30 | +DISABLED_PINS = (0, 1) |
| 31 | +AVAILABLE_PINS = range(0, max(AnalogPin) + 1) |
| 32 | + |
| 33 | +ADC_MAX = 1023 # 10 bit ADC |
| 34 | +ADC_MIN = 0 |
| 35 | + |
| 36 | + |
| 37 | +class Arduino: |
| 38 | + """ |
| 39 | + The Arduino board interface. |
| 40 | +
|
| 41 | + This is intended to be used with Arduino Uno boards running the sbot firmware. |
| 42 | +
|
| 43 | + :param boards: The BoardManager object containing the arduino board references. |
| 44 | + """ |
| 45 | + |
| 46 | + __slots__ = ('_boards',) |
| 47 | + |
| 48 | + def __init__(self, boards: BoardManager): |
| 49 | + # Obtain a reference to the arduino |
| 50 | + # This is contained in a list to allow for it to be populated later |
| 51 | + self._boards = boards.arduino |
| 52 | + |
| 53 | + @log_to_debug |
| 54 | + def set_pin_mode(self, pin: int, mode: GPIOPinMode) -> None: |
| 55 | + """ |
| 56 | + Set the mode of the pin. |
| 57 | +
|
| 58 | + To do analog or digital reads set the mode to INPUT or INPUT_PULLUP. |
| 59 | + To do digital writes set the mode to OUTPUT. |
| 60 | +
|
| 61 | + :param pin: The pin to set the mode of. |
| 62 | + :param value: The mode to set the pin to. |
| 63 | + :raises IOError: If the pin mode is not a GPIOPinMode. |
| 64 | + :raises IOError: If this pin cannot be controlled. |
| 65 | + """ |
| 66 | + port = self._get_port() |
| 67 | + self._validate_pin(pin) |
| 68 | + if not isinstance(mode, GPIOPinMode): |
| 69 | + raise IOError('Pin mode only supports being set to a GPIOPinMode') |
| 70 | + port.write(f'PIN:{pin}:MODE:SET:{mode.value}') |
| 71 | + |
| 72 | + @log_to_debug |
| 73 | + def digital_read(self, pin: int) -> bool: |
| 74 | + """ |
| 75 | + Perform a digital read on the pin. |
| 76 | +
|
| 77 | + :param pin: The pin to read from. |
| 78 | + :raises IOError: If the pin's current mode does not support digital read |
| 79 | + :raises IOError: If this pin cannot be controlled. |
| 80 | + :return: The digital value of the pin. |
| 81 | + """ |
| 82 | + port = self._get_port() |
| 83 | + self._validate_pin(pin) |
| 84 | + response = port.query(f'PIN:{pin}:DIGITAL:GET?') |
| 85 | + return (response == '1') |
| 86 | + |
| 87 | + @log_to_debug |
| 88 | + def digital_write(self, pin: int, value: bool) -> None: |
| 89 | + """ |
| 90 | + Write a digital value to the pin. |
| 91 | +
|
| 92 | + :param pin: The pin to write to. |
| 93 | + :param value: The value to write to the pin. |
| 94 | + :raises IOError: If the pin's current mode does not support digital write. |
| 95 | + :raises IOError: If this pin cannot be controlled. |
| 96 | + """ |
| 97 | + port = self._get_port() |
| 98 | + self._validate_pin(pin) |
| 99 | + try: |
| 100 | + if value: |
| 101 | + port.write(f'PIN:{pin}:DIGITAL:SET:1') |
| 102 | + else: |
| 103 | + port.write(f'PIN:{pin}:DIGITAL:SET:0') |
| 104 | + except RuntimeError as e: |
| 105 | + # The firmware returns a NACK if the pin is not in OUTPUT mode |
| 106 | + if 'is not supported in' in str(e): |
| 107 | + raise IOError(str(e)) |
| 108 | + |
| 109 | + @log_to_debug |
| 110 | + def analog_read(self, pin: int) -> float: |
| 111 | + """ |
| 112 | + Get the analog voltage on the pin. |
| 113 | +
|
| 114 | + This is returned in volts. Only pins A0-A5 support analog reads. |
| 115 | +
|
| 116 | + :param pin: The pin to read from. |
| 117 | + :raises IOError: If the pin or its current mode does not support analog read. |
| 118 | + :raises IOError: If this pin cannot be controlled. |
| 119 | + :return: The analog voltage on the pin, ranges from 0 to 5. |
| 120 | + """ |
| 121 | + port = self._get_port() |
| 122 | + self._validate_pin(pin) |
| 123 | + if pin not in AnalogPin: |
| 124 | + raise IOError('Pin does not support analog read') |
| 125 | + try: |
| 126 | + response = port.query(f'PIN:{pin}:ANALOG:GET?') |
| 127 | + except RuntimeError as e: |
| 128 | + # The firmware returns a NACK if the pin is not in INPUT mode |
| 129 | + if 'is not supported in' in str(e): |
| 130 | + raise IOError(str(e)) |
| 131 | + # map the response from the ADC range to the voltage range |
| 132 | + return map_to_float(int(response), ADC_MIN, ADC_MAX, 0.0, 5.0) |
| 133 | + |
| 134 | + @log_to_debug |
| 135 | + def measure_ultrasound_distance(self, pulse_pin: int, echo_pin: int) -> int: |
| 136 | + """ |
| 137 | + Measure the distance to an object using an ultrasound sensor. |
| 138 | +
|
| 139 | + The sensor can only measure distances up to 4000mm. |
| 140 | +
|
| 141 | + :param pulse_pin: The pin to send the ultrasound pulse from. |
| 142 | + :param echo_pin: The pin to read the ultrasound echo from. |
| 143 | + :raises ValueError: If either of the pins are invalid |
| 144 | + :return: The distance measured by the ultrasound sensor in mm. |
| 145 | + """ |
| 146 | + port = self._get_port() |
| 147 | + try: # bounds check |
| 148 | + self._validate_pin(pulse_pin) |
| 149 | + except (IndexError, IOError): |
| 150 | + raise ValueError("Invalid pulse pin provided") from None |
| 151 | + try: |
| 152 | + self._validate_pin(echo_pin) |
| 153 | + except (IndexError, IOError): |
| 154 | + raise ValueError("Invalid echo pin provided") from None |
| 155 | + |
| 156 | + response = port.query(f'ULTRASOUND:{pulse_pin}:{echo_pin}:MEASURE?') |
| 157 | + return int(response) |
| 158 | + |
| 159 | + def _validate_pin(self, pin: int) -> None: |
| 160 | + if pin in DISABLED_PINS: |
| 161 | + raise IOError('This pin cannot be controlled.') |
| 162 | + if pin not in AVAILABLE_PINS: |
| 163 | + raise IndexError(f'Pin {pin} is not available on the Arduino.') |
| 164 | + |
| 165 | + def _get_port(self) -> SerialWrapper: |
| 166 | + if len(self._boards) == 0: |
| 167 | + raise RuntimeError("No Arduino connected") |
| 168 | + return self._boards[0] |
| 169 | + |
| 170 | + def __repr__(self) -> str: |
| 171 | + try: |
| 172 | + port = self._get_port() |
| 173 | + except RuntimeError: |
| 174 | + return f"<{self.__class__.__qualname__} no arduino connected>" |
| 175 | + else: |
| 176 | + return f"<{self.__class__.__qualname__} {port}>" |
0 commit comments