From b938a82ea9e351c33ad1e9cf4c6293ae34c5fdd3 Mon Sep 17 00:00:00 2001 From: Ansel Chang Date: Sat, 17 Sep 2022 18:03:13 -0400 Subject: [PATCH] reflectance wrapper to switch between legacy and new sensors, issue #1 --- .DS_Store | Bin 6148 -> 6148 bytes WPILib/.DS_Store | Bin 6148 -> 6148 bytes WPILib/WPILib.py | 4 +-- WPILib/_abstract_reflectance.py | 25 +++++++++++++++++ WPILib/_analog_reflectance.py | 35 ++++++++++++++++++++++++ WPILib/_drivetrain.py | 2 +- WPILib/_grove_reflectance.py | 31 +++++++++++++++++++++ WPILib/_reflectance.py | 16 ----------- WPILib/_reflectance_wrapper.py | 47 ++++++++++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 19 deletions(-) create mode 100755 WPILib/_abstract_reflectance.py create mode 100755 WPILib/_analog_reflectance.py create mode 100755 WPILib/_grove_reflectance.py delete mode 100755 WPILib/_reflectance.py create mode 100644 WPILib/_reflectance_wrapper.py diff --git a/.DS_Store b/.DS_Store index a795dbcfb1461b47ea529fc05e2430d7365e75ae..e551028357f0e60c6260e54bd511256a93af6091 100644 GIT binary patch literal 6148 zcmeHKJxc>Y5PhqO25m$v?7Uwf+KJT}BO+J?QV2H27|~$D1^lQFY_+xVhuB$(_&fXu zVrA!>-9<9V#lk{FW?=W-&Ccw+dzahI0&vyK_9`$3pu#3tnqgO9GA?bzCZcD7=wy!W z$!g;$>-9$>&0z|d0)J5f`F2ZK#tAyOP~UI)c6)HFm(^;mZq}lYi$Q+BcJ{QP$J^hI z_fH4bhNU`FQD)r^+Bn9cs*Lip9@Vw+y0GtS59If=hf135LaE>En!a{SH zcpr_>Jo1<81J7e<9?fN3l;$JrFJ{odA$sVeOHDS5Sk1qUi__e26W>qaoSNbHv6|e#_9-(E|rcK0wmKE&%|J^XIe9GXx5SQMjpIL|}4nEhMrz%A6-wcJYMbe0BO09}brWwB8gj1+)T_;dV~;|E=-of1PA^rhqB%uM}{lTBBCw zBgMUSB)qF;Q%yo+2a9#(>?7j69QhSR~_EUaeqYU|?a;V@PMnWGKnacX3I| z$xi}`aja3h(X}z|h$E_e3SRkw48!2${M-Vd9tH+UhRMw=hbQl2;hbE`%0D@dm5+tt wkx10!0#^0Sq8vgj6AR)tvvcrs0A03OkmEb^WPTAx4vB)qu~2NHo}wrl0|Nsi1A_nqLo!1m5N9x?GQ@9YWL(Zz50c_$hzG&~ zhDu~V&;Rd4`tuSNC|M|`SN;Hr%XD=)}E sc7_1M#vX6h&Fmcf96CJ*u~2NHo}wrd0|Nsi1A_nqLlHwMLncEBLp(#q#6tDS1|lq*_cM90 mZgyZcVA{;i!OsEIwAqmPJM(0I5kp3X$u>OFn`1;)FarPq$P*s` diff --git a/WPILib/WPILib.py b/WPILib/WPILib.py index 41f6a5b..45b72d2 100755 --- a/WPILib/WPILib.py +++ b/WPILib/WPILib.py @@ -1,7 +1,7 @@ import board as _board from . import _drivetrain from . import _grove_ultrasonic -from . import _reflectance +from . import _reflectance_wrapper from . import _servo from . import _buttons from . import _encoded_motor @@ -18,7 +18,7 @@ # Publicly-accessible objects drivetrain = _drivetrain.Drivetrain(_leftMotor, _rightMotor) # units in cm -reflectance = _reflectance.Reflectance(_board.GP26, _board.GP27) +reflectance = _reflectance_wrapper.ReflectanceWrapper() sonar = _grove_ultrasonic.GroveUltrasonicRanger(_board.GP28) led = _led.RGBLED(_board.GP18) servo = _servo.Servo(_board.GP12, actuationRange = 135) diff --git a/WPILib/_abstract_reflectance.py b/WPILib/_abstract_reflectance.py new file mode 100755 index 0000000..7aab9c5 --- /dev/null +++ b/WPILib/_abstract_reflectance.py @@ -0,0 +1,25 @@ + +class AbstractReflectance: + + """ + An interface for reading the two reflectance sensors. + At the moment, there are the grove ultrasonic and analog implementations. + """ + + # @abstractmethod because circuitpython does not support abc + def get_left_reflectance(self) -> float: + """ + Gets the the reflectance of the left reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + pass + + # @abstractmethod because circuitpython does not support abc + def get_right_reflectance(self) -> float: + """ + Gets the the reflectance of the right reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + pass \ No newline at end of file diff --git a/WPILib/_analog_reflectance.py b/WPILib/_analog_reflectance.py new file mode 100755 index 0000000..c2581a7 --- /dev/null +++ b/WPILib/_analog_reflectance.py @@ -0,0 +1,35 @@ +from analogio import AnalogIn +from . import _abstract_reflectance + +class AnalogReflectance(_abstract_reflectance.AbstractReflectance): + + """ + Implements for the new reflectance sensor. + Reads from analog in and converts to a float from 0 (white) to 1 (black) + """ + + def __init__(self, leftPin, rightPin): + self._leftReflectance = AnalogIn(leftPin) + self._rightReflectance = AnalogIn(rightPin) + + def _get_value(self, sensor: AnalogIn) -> float: + MAX_ANALOG_VALUE: int = 65535 + return sensor.value / MAX_ANALOG_VALUE + + # Implements AbstractReflectance + def get_left_reflectance(self) -> float: + """ + Gets the the reflectance of the left reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._get_value(self._leftReflectance) + + # Implements AbstractReflectance + def get_right_reflectance(self) -> float: + """ + Gets the the reflectance of the right reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._get_value(self._rightReflectance) \ No newline at end of file diff --git a/WPILib/_drivetrain.py b/WPILib/_drivetrain.py index 5976d92..1a10d76 100755 --- a/WPILib/_drivetrain.py +++ b/WPILib/_drivetrain.py @@ -46,7 +46,7 @@ def set_wheel_spacing(self, wheel_spacing: float) -> bool: Set the space between wheels :param wheel_spacing: The distance between the drive wheels in centimeters - type diameter: float + type wheel_spacing: float """ self.wheelSpacing = wheel_spacing diff --git a/WPILib/_grove_reflectance.py b/WPILib/_grove_reflectance.py new file mode 100755 index 0000000..cd544cf --- /dev/null +++ b/WPILib/_grove_reflectance.py @@ -0,0 +1,31 @@ +from . import _grove_ultrasonic +from . import _abstract_reflectance + +class GroveReflectance(_abstract_reflectance.AbstractReflectance): + + """ + Supports the old reflectance sensor. A wrapper for the GroveUltrasonic class, because + for some reason the implementation for the reflectance sensor is identical to GroveUltrasonic + """ + + def __init__(self, leftPin, rightPin): + self._leftReflectance = _grove_ultrasonic.GroveUltrasonicRanger(leftPin) + self._rightReflectance = _grove_ultrasonic.GroveUltrasonicRanger(rightPin) + + # Implements AbstractReflectance + def get_left_reflectance(self) -> float: + """ + Gets the the reflectance of the left reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._leftReflectance.get_distance() + + # Implements AbstractReflectance + def get_right_reflectance(self) -> float: + """ + Gets the the reflectance of the right reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._rightReflectance.get_distance() \ No newline at end of file diff --git a/WPILib/_reflectance.py b/WPILib/_reflectance.py deleted file mode 100755 index 7b73f0a..0000000 --- a/WPILib/_reflectance.py +++ /dev/null @@ -1,16 +0,0 @@ -from analogio import AnalogIn - -class Reflectance: - def __init__(self, leftPin, rightPin): - self._leftReflectance = AnalogIn(leftPin) - self._rightReflectance = AnalogIn(rightPin) - - def _get_value(self, sensor: AnalogIn): - MAX_ANALOG_VALUE: int = 65535 - return sensor.value / MAX_ANALOG_VALUE - - def get_left_reflectance(self): - return self._get_value(self._leftReflectance) - - def get_right_reflectance(self): - return self._get_value(self._rightReflectance) diff --git a/WPILib/_reflectance_wrapper.py b/WPILib/_reflectance_wrapper.py new file mode 100644 index 0000000..6f5d3fc --- /dev/null +++ b/WPILib/_reflectance_wrapper.py @@ -0,0 +1,47 @@ +from . import _abstract_reflectance +from . import _analog_reflectance +from . import _grove_reflectance +from . import _analog_reflectance +from . import _abstract_reflectance +import board + +class ReflectanceWrapper: + + """ + A wrapper for an object that stores either the legacy or new implementation of the reflectance sensor + User can switch between the two modes, and get the value of either reflectance sensor + """ + + def __init__(self): + + # Default to new sensor + self.set_legacy_mode(False) + + def set_legacy_mode(self, is_legacy: bool = True) -> None: + """ + Set whether the reflectance sensor is the old or the new analog one + :param is_legacy: True if using the old version, False if using the new one + :type is_legacy: bool + """ + if is_legacy: + self._reflectanceObject: _abstract_reflectance.AbstractReflectance = _grove_reflectance.GroveReflectance(board.GP26, board.GP27) + else: + self._reflectanceObject: _abstract_reflectance.AbstractReflectance = _analog_reflectance.AnalogReflectance(board.GP26, board.GP27) + + + def get_left_reflectance(self) -> float: + """ + Gets the the reflectance of the left reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._reflectanceObject.get_left_reflectance() + + + def get_right_reflectance(self) -> float: + """ + Gets the the reflectance of the right reflectance sensor + :return: The reflectance ranging from 0 (white) to 1 (black) + :rtype: float + """ + return self._reflectanceObject.get_right_reflectance() \ No newline at end of file