-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reflectance wrapper to switch between legacy and new sensors, issue #1
- Loading branch information
1 parent
1355d43
commit b938a82
Showing
9 changed files
with
141 additions
and
19 deletions.
There are no files selected for viewing
Binary file not shown.
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,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 |
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,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) |
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,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() |
This file was deleted.
Oops, something went wrong.
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,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() |