Skip to content

Commit

Permalink
Fixed more codestyle errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
imsenthur committed Oct 3, 2024
1 parent 835a631 commit 39bd5a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
18 changes: 12 additions & 6 deletions opensourceleg/sensors/loadcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ def __init__(
bus: int = 1,
i2c_address: int = 0x66,
) -> None:
'''
"""
TODO: Write docstring for initial values
'''
"""
# Check that parameters are set correctly:
if calibration_matrix.shape != (6, 6):
LOGGER.info(f"[{self.__repr__()}] calibration_matrix must be a 6x6 array of np.double.")
LOGGER.info(
f"[{self.__repr__()}] calibration_matrix must be a 6x6 array of np.double."
)
raise TypeError("calibration_matrix must be a 6x6 array of np.double.")
if amp_gain <= 0:
LOGGER.info(f"[{self.__repr__()}] amp_gain must be a floating point value greater than 0.")
LOGGER.info(
f"[{self.__repr__()}] amp_gain must be a floating point value greater than 0."
)
raise ValueError("amp_gain must be a floating point value greater than 0.")
if exc <= 0 :
LOGGER.info(f"[{self.__repr__()}] exc must be a floating point value greater than 0.")
if exc <= 0:
LOGGER.info(
f"[{self.__repr__()}] exc must be a floating point value greater than 0."
)
raise ValueError("exc must be a floating point value greater than 0.")

self._amp_gain: float = amp_gain
Expand Down
50 changes: 30 additions & 20 deletions tests/test_sensors/test_loadcell.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# Global Units Dictionary
import enum
import time
from dataclasses import dataclass

import numpy as np
from numpy import typing as npt
import pytest
import time
from numpy import typing as npt
from smbus2 import SMBus

from opensourceleg.sensors import loadcell
from smbus2 import SMBus

VALUES = np.append(np.array([0, 1, -1, 1000, -1000]), np.random.random(5))
DEFAULT_CAL_MATRIX = np.ones(shape=(6,6), dtype = np.double)
DEFAULT_CAL_MATRIX = np.ones(shape=(6, 6), dtype=np.double)

# TODO: Test loadcell not responding exception


def test_SRILoadcell_init():

invalid_cal_matrix = np.ones(shape=(5,6), dtype = np.double)
invalid_cal_matrix = np.ones(shape=(5, 6), dtype=np.double)
with pytest.raises(TypeError):
SRI = loadcell.SRILoadcell(calibration_matrix=invalid_cal_matrix)
with pytest.raises(ValueError):
SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX, amp_gain=0)
with pytest.raises(ValueError):
SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX, exc = 0)
SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX, exc=0)

SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX)

Expand All @@ -33,35 +34,40 @@ def test_SRILoadcell_init():
assert SRI._bus == 1
assert SRI._i2c_address == 0x66


def test_SRILoadcell_start():

#Initialize SRILoadcell object
# Initialize SRILoadcell object
SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX)

start = time.time()
SRI.start()
end = time.time()

#assert SRI._bus == SMBus(SRI._bus) #?
assert (end - start) >= 1 # TODO: Link to var
# assert SRI._bus == SMBus(SRI._bus) #?
assert (end - start) >= 1 # TODO: Link to var
assert SRI._is_streaming == True

# Test start with bus or i2c_address set to None
SRI = loadcell.SRILoadcell(DEFAULT_CAL_MATRIX, 125.0, 5, 1, None)
result = SRI.start() # TODO: Am I doing this correctly?
result = SRI.start() # TODO: Am I doing this correctly?
assert result == None


def test_SRILoadcell_reset():

SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX)
SRI._calibration_offset == np.ones(shape=(1, 6), dtype=np.double)
SRI.reset()
assert np.array_equal(SRI._calibration_offset, np.zeros(shape=(1, 6), dtype=np.double))

assert np.array_equal(
SRI._calibration_offset, np.zeros(shape=(1, 6), dtype=np.double)
)


def test_SRILoadcell_update():

# Test basic call execution
SRI = loadcell.SRILoadcell(calibration_matrix = DEFAULT_CAL_MATRIX)
SRI = loadcell.SRILoadcell(calibration_matrix=DEFAULT_CAL_MATRIX)
SRI.update(data_callback=_read_data)

# Ensuring self._calibration_offset was used
Expand All @@ -78,18 +84,21 @@ def test_SRILoadcell_update():


def test_SRILoadcell_calibrate():
#Test reset, else statement
# Test reset, else statement

return


# Function to bypass _read_compressed_strain() with an array of ones
def _read_data() -> npt.NDArray[np.uint8]:
return np.ones(shape=(1,6))
return np.ones(shape=(1, 6))


# Function to bypass _read_compressed_strain() with random data
def _read_random_data() -> npt.NDArray[np.uint8]:
return np.random.randint(low=0, high=255, size=(1, 6), dtype=np.uint8)


# Function to run update calculations from inside the update method
def _update_calculations(SRI: loadcell.SRILoadcell, calibration_offset: float):
test_data = _read_data()
Expand All @@ -101,5 +110,6 @@ def _update_calculations(SRI: loadcell.SRILoadcell, calibration_offset: float):
)
return data

if __name__ == '__main__':
test_SRILoadcell_update()

if __name__ == "__main__":
test_SRILoadcell_update()

0 comments on commit 39bd5a9

Please sign in to comment.