Skip to content

Commit

Permalink
reflectance wrapper to switch between legacy and new sensors, issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
AnselChang committed Sep 17, 2022
1 parent 1355d43 commit b938a82
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 19 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified WPILib/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions WPILib/WPILib.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions WPILib/_abstract_reflectance.py
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
35 changes: 35 additions & 0 deletions WPILib/_analog_reflectance.py
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)
2 changes: 1 addition & 1 deletion WPILib/_drivetrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions WPILib/_grove_reflectance.py
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()
16 changes: 0 additions & 16 deletions WPILib/_reflectance.py

This file was deleted.

47 changes: 47 additions & 0 deletions WPILib/_reflectance_wrapper.py
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()

0 comments on commit b938a82

Please sign in to comment.