Skip to content

Commit

Permalink
Make bd_tools accessible through one interface (#43)
Browse files Browse the repository at this point in the history
* Make bd_tools accessible through one interface

Signed-off-by: Greg Balke <gbalke@berkeley.edu>

* Run linting with python3.8

Signed-off-by: Greg Balke <gbalke@berkeley.edu>

* Lock down click version

Signed-off-by: Greg Balke <gbalke@berkeley.edu>

* Remove unecessary code

Signed-off-by: Greg Balke <gbalke@berkeley.edu>
  • Loading branch information
gbalke authored May 15, 2022
1 parent 416c4e9 commit 515726a
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 35 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/cpplint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ on: [pull_request]
jobs:
cpplint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- run: pip install -r requirements.txt
- run: make lint_cpp
python_lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- run: pip install -r requirements.txt
- run: make lint_python
3 changes: 3 additions & 0 deletions bd_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""The betz drive tools package."""

import bd_tools.boards
import bd_tools.comms # noqa: F401
53 changes: 53 additions & 0 deletions bd_tools/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""The betz drive tools package."""
import argparse
import sys
from pathlib import Path

BIN_PATH = Path(__file__).parent / "bin"


def get_tools():
"""Returns all scripts in the bin directory"""
return [tool.stem for tool in BIN_PATH.glob("*.py")]


def parser_args(tools):
parser = argparse.ArgumentParser(description="BetzDrive Tools Package")
parser.add_argument(
"tool", type=str, choices=tools, help="Name of the tool to use."
)
return parser.parse_args()


def action(args):
file_name = BIN_PATH / (args.tool + ".py")
with open(file_name) as f:
# NOTE(greg): Shifts argv down one (and deletes the 0th arg) so the
# sub-tool does not see its own name as the 1st arg.
sys.argv = sys.argv[1:]
# Correctly make arg0 the path to the file we're executing.
sys.argv[0] = str(file_name)
code = compile(f.read(), file_name.name, "exec")
exec(code, globals())


if __name__ == "__main__":
# NOTE(greg): We have to hack the help to make sure it only operates on
# this argparser if its the first arg.
tool_help = False
if "-h" in sys.argv or "--help" in sys.argv:
if not (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
tool_help = True
if "-h" in sys.argv:
sys.argv.remove("-h")
if "--help" in sys.argv:
sys.argv.remove("--help")

args = parser_args(get_tools())

# If we requested a tool help, inject it back into the sys args for the
# sub-tool to process.
if tool_help:
sys.argv.append("--help")

action(args)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pytest
pytest-mock

# Linting and formatting requirements.
click == 7.1.2
black == 21.5b1
isort == 5.8.0
flake8 == 3.9.2
Expand Down
53 changes: 22 additions & 31 deletions tests/tools/unit/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import pytest

from bd_tools import (
calibrate_encoder,
comms,
control_motor,
read_sensor,
update_calibration,
upload_bootloader,
upload_firmware,
view_control_loop,
)
import bd_tools


@dataclasses.dataclass
Expand All @@ -20,7 +11,7 @@ class BaseArgs:

serial: str = "/dev/ttyUSB0"
board_ids: str = "1"
baud_rate: int = comms.COMM_DEFAULT_BAUD_RATE
baud_rate: int = bd_tools.comms.COMM_DEFAULT_BAUD_RATE


@pytest.fixture
Expand Down Expand Up @@ -56,7 +47,7 @@ class Args(BaseArgs):
board_id: int = 1
delay: float = 0.1

mocker.patch("bd_tools.calibrate_encoder.serial")
mocker.patch("bd_tools.bin.calibrate_encoder.serial")
mocker.patch("bd_tools.boards.initMotor")
mocker.patch("bd_tools.boards.driveMotor")
mocker.patch("bd_tools.comms.BLDCControllerClient.writeRegisters")
Expand All @@ -75,7 +66,7 @@ class Args(BaseArgs):
mocker.patch(
"bd_tools.comms.BLDCControllerClient.getRecorderElement",
lambda self, bids, indexes: [
(0.0,) * comms.COMM_NUM_RECORDER_ELEMENTS
(0.0,) * bd_tools.comms.COMM_NUM_RECORDER_ELEMENTS
],
)

Expand All @@ -95,11 +86,11 @@ def grad(client, bids):
grad_generator(),
)

mocker.patch("bd_tools.calibrate_encoder.input")
mocker.patch("bd_tools.calibrate_encoder.open")
mocker.patch("bd_tools.calibrate_encoder.json")
mocker.patch("bd_tools.bin.calibrate_encoder.input")
mocker.patch("bd_tools.bin.calibrate_encoder.open")
mocker.patch("bd_tools.bin.calibrate_encoder.json")

calibrate_encoder.action(Args())
bd_tools.bin.calibrate_encoder.action(Args())


def test_control_motor(mocker, default_mock_comms):
Expand All @@ -113,10 +104,10 @@ class Args(BaseArgs):
mode: str = "torque"
actuations: str = "0.1"

mocker.patch("bd_tools.control_motor.serial")
mocker.patch("bd_tools.bin.control_motor.serial")
mocker.patch("bd_tools.boards.initMotor")

control_motor.action(Args())
bd_tools.bin.control_motor.action(Args())


def test_read_sensor(mocker, default_mock_comms):
Expand All @@ -129,9 +120,9 @@ class Args(BaseArgs):
sensor: str = "temp"
num_iters: int = 1

mocker.patch("bd_tools.read_sensor.serial")
mocker.patch("bd_tools.bin.read_sensor.serial")

read_sensor.action(Args())
bd_tools.bin.read_sensor.action(Args())


def test_update_calibration(mocker, default_mock_comms):
Expand All @@ -141,7 +132,7 @@ def test_update_calibration(mocker, default_mock_comms):
class Args(BaseArgs):
"""Args for this tool."""

mocker.patch("bd_tools.update_calibration.serial")
mocker.patch("bd_tools.bin.update_calibration.serial")
mocker.patch(
"bd_tools.comms.BLDCControllerClient.getTorqueConstant",
lambda x, y: [0.0] * len(y),
Expand All @@ -160,7 +151,7 @@ class Args(BaseArgs):
mocker.patch("bd_tools.comms.BLDCControllerClient.setVelocityLimit")
mocker.patch("bd_tools.comms.BLDCControllerClient.storeCalibration")

update_calibration.action(Args())
bd_tools.bin.update_calibration.action(Args())


def test_upload_bootloader(mocker, default_mock_comms):
Expand All @@ -173,12 +164,12 @@ class Args(BaseArgs):
bin_file: str = "file.bin"
offset: int = 0x08000000

mocker.patch("bd_tools.upload_bootloader.serial")
mocker.patch("bd_tools.upload_bootloader.open")
mocker.patch("bd_tools.bin.upload_bootloader.serial")
mocker.patch("bd_tools.bin.upload_bootloader.open")
mocker.patch("bd_tools.comms.BLDCControllerClient.getFlashSectorMap")
mocker.patch("bd_tools.comms.BLDCControllerClient.writeFlash")

upload_bootloader.action(Args())
bd_tools.bin.upload_bootloader.action(Args())


def test_upload_firmware(mocker, default_mock_comms):
Expand All @@ -191,12 +182,12 @@ class Args(BaseArgs):
bin_file: str = "file.bin"
offset: int = 0x08000000

mocker.patch("bd_tools.upload_firmware.serial")
mocker.patch("bd_tools.upload_firmware.open")
mocker.patch("bd_tools.bin.upload_firmware.serial")
mocker.patch("bd_tools.bin.upload_firmware.open")
mocker.patch("bd_tools.comms.BLDCControllerClient.getFlashSectorMap")
mocker.patch("bd_tools.comms.BLDCControllerClient.writeFlash")

upload_firmware.action(Args())
bd_tools.bin.upload_firmware.action(Args())


def test_view_control_loop(mocker, default_mock_comms):
Expand All @@ -209,9 +200,9 @@ class Args(BaseArgs):
mode: str = "torque"
actuations: str = "0.1"

mocker.patch("bd_tools.view_control_loop.serial")
mocker.patch("bd_tools.bin.view_control_loop.serial")
mocker.patch("bd_tools.boards.initMotor")
mocker.patch("bd_tools.boards.driveMotor")
mocker.patch("bd_tools.livegraph.LiveGraph.start")

view_control_loop.action(Args())
bd_tools.bin.view_control_loop.action(Args())

0 comments on commit 515726a

Please sign in to comment.