Skip to content

Commit

Permalink
Merge pull request #60 from BetzDrive/remove_make
Browse files Browse the repository at this point in the history
[Buildsystem] Remove makefiles
  • Loading branch information
gbalke authored Aug 21, 2022
2 parents 4958363 + f54d7e6 commit 9dce2d6
Show file tree
Hide file tree
Showing 63 changed files with 210 additions and 719 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/compile.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
name: compile
on: [pull_request]
jobs:
makefile:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
with:
submodules: true
- name: Install Arm GCC
uses: fiam/arm-none-eabi-gcc@v1
with:
release: '9-2019-q4' # The arm-none-eabi-gcc release to use.
- name: Install Protoc
run: sudo apt-get install protobuf-compiler
- name: Install Protoc Tools
run: python3 -m pip install protobuf grpcio-tools
- run: make -C bootloader/
- run: make -C firmware/
bazel-bootloader:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Python package
name: bd tools (install and tests)

on: [pull_request]

Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
path = third_party/chibios
url = https://github.com/berkeleyopenarms/ChibiOS.git
branch = stable_2.6.x
[submodule "proto/nanopb"]
path = third_party/nanopb
url = https://github.com/nanopb/nanopb.git
15 changes: 7 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
test: pytest lint

pytest: install_bd_tools
pytest: install_python_packages
python3 -m pytest tests/

lint: lint_python lint_cpp

lint_python:
isort --check tests bd_tools
black --check tests bd_tools
black --check bd_tools
black --check tests
pflake8 bd_tools tests

lint_cpp:
Expand All @@ -17,18 +18,16 @@ format: format_python format_bazel

format_python:
isort tests bd_tools
black tests bd_tools
black bd_tools
black tests

format_bazel:
bazel run //:buildifier

setup: init_submodules install_python_packages install_bd_tools
setup: init_submodules install_python_packages

init_submodules:
git submodule update --recursive --init

install_python_packages:
python3 -m pip install -r requirements.txt

install_bd_tools:
python3 -m pip install bd_tools/ --user
python3 -m pip install -r requirements.txt --user
55 changes: 25 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
cd ~
git clone https://github.com/BetzDrive/bldc-controller.git
cd bldc-controller
sudo apt-get install gcc-arm-none-eabi gdb-multiarch python3-pip
python3 -m pip install -r requirements.txt
sudo apt-get install gdb-multiarch python3-pip
make setup
```

Expand All @@ -13,39 +12,35 @@ A quick way of running the full test suite is to use `make test`. This will lint

### Compiling
```bash
cd ~/bldc_controller/<firmware or bootloader>
make
bazel build //<bootloader or firmware>
```

### Debugging via ST-LINK
To debug during execution, you can attach the debugger at any time using the following code. Keep in mind this is done with a direct connection to the hardware via an ST-Link.
### Uploading via ST-LINK
```bash
cd ~/bldc_controller/<firmware or bootloader>
openocd
----
cd ~/bldc_controller/<firmware or bootloader>/build
arm-none-eabi-gdb or gdb-multiarch <compiled file>.elf
(gdb) target extended-remote :3333
bazel run //<bootloader or firmware>:flash
```

This functionality is also built into the make files as `make debug`.

### Uploading via ST-LINK
### Debugging via ST-LINK
To debug during execution, you can attach the debugger at any time using the following code. Keep in mind this is done with a direct connection to the hardware via an ST-Link.
```bash
cd ~/bldc_controller/<firmware or bootloader>
make upload
# First terminal (flash latest and start gdb multiarch)
bazel run //<bootloader or firmware>:flash
bazel run //<bootloader or firmware>:gdb
# Second terminal (start openocd server)
bazel run //<bootloader or firmware>:debug
```

### Upload firmware via RS485
```bash
cd ~/bldc_controller/tools
python upload_firmware.py <serial_port> <board_ids> ~/bldc_controller/firmware/build/firmware.bin
bazel run //firmware:upload
```

### Upload bootloader via RS485
Use this with caution. If this fails and a power cycle or reboot occurs, the board will have to be programmed directly. In the case of a failed upload, disable the enumeration procedure and try again with the same board ID. To be safe, first upload and test on an easy-to-remove link such as the gripper or base which are not as difficult to access in case of a failure.

`upload_bootloader.py <serial_port> <board_id> <path_to_file>`
```bash
bazel run //bootloader:upload
```

### Serial Transmission Speed Limits
USB Serial device drivers only permit at most one transmission per millisecond. By default, this is set to 16 milliseconds on our usb to RS485 converters. To change this execute the following terminal commands:
Expand All @@ -59,40 +54,40 @@ cat /sys/bus/usb-serial/devices/ttyUSB<id_num>/latency_timer
(when running the arm, this is done automatically by the ROS control stack)

# Python Scripts
These scripts are made to remotely interface with the boards. They can debug, program, and run the boards. They interface using our custom communications protocol. Most scripts have been fitted with argparse which will give a description of the arguments when given the '-h' option. (i.e. `read_sensor.py -h`)
These scripts are made to remotely interface with the boards. They can debug, program, and run the boards. They interface using our custom communications protocol. The scripts are grouped under the base command `bdt`. Read more with `bdt -h`.

### Motor Calibration
To calibrate the motor board, a motor must first be attached without a load for optimal calibration. The results of calibration are based on the properties of the given motor.

* `calibrate_encoder.py <serial_port> <board_id> <duty_cycle>`
* `upload_calibration.py <serial_port> <board_id>`
* `bdt calibrate_encoder <serial_port> <board_id> <duty_cycle>`
* `bdt upload_calibration <serial_port> <board_id>`

1. Run _calibrate_encoder.py_: for 24V Power Supplies the duty cycle used in testing is 0.3 and for 48V or 50V supplies use half (0.15). This will generate a file called calibrations.json
2. Run _upload_calibration.py_ with the updated calibrations.json and the generated file will be loaded onto the board.
1. Run _calibrate_encoder_: for 24V Power Supplies the duty cycle used in testing is 0.3 and for 48V or 50V supplies use half (0.15). This will generate a file called calibrations.json
2. Run _upload_calibration_ with the updated calibrations.json and the generated file will be loaded onto the board.

After completing these steps, the motor should be controllable.

### Update Motor Calibration Constants (PID and Limits)
Run this script after there's a calibration on the board. Modify the contents of the script to adjust tunings!

`update_calibration.py <serial_port> <board_id>`
`bdt update_calibration <serial_port> <board_id>`

### Motor Control
Use this script to spin a motor with a given current command. A substantial starting point is 0.5 and increase above this with proper supervision! A negative command will reverse the direction. Description of what arguments each mode takes can be found with the '-h' option.

`control_motor.py <serial_port> <board_id> <mode> <command>`
`bdt control_motor <serial_port> <board_id> <mode> <command>`

To control multiple motors,

```
control_motor.py <serial_port> 1,2,3 torque 0.5,0.25,0
control_motor.py <serial_port> 1,2,3 current [0,0.5],[0,0.25],[0,0]
bdt control_motor <serial_port> 1,2,3 torque 0.5,0.25,0
bdt control_motor <serial_port> 1,2,3 current [0,0.5],[0,0.25],[0,0]
```

### Read Sensor
This script lets a user read from any of the sensors on-board the device.

`read_sensor.py <serial_port> <board_ids> <sensor>`
`bdt read_sensor <serial_port> <board_ids> <sensor>`

### View Control Loop
Plots the control target and the resulting quadrature/direct current commands. This is low frequency so use the recorder to get a better sense of the internal state of the motor driver.
Expand Down
21 changes: 21 additions & 0 deletions bd_tools/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Berkeley Open Arms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion bd_tools/bin/__init__.py

This file was deleted.

48 changes: 48 additions & 0 deletions bd_tools/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[build-system]
requires = [
"hatchling>=1.8.0",
]
build-backend = "hatchling.build"

[project]
name = "bd-tools"
description = "Python API for BetzDrive"
readme = "README.md"
license = "MIT"
requires-python = ">=3.7"
authors = [
{ name = "Greg Balke", email = "gbalke@berkeley.edu" },
]
classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
]
dependencies = [
"crcmod",
"matplotlib",
"numpy",
"pyserial",
"scipy",
]
dynamic = [
"version",
]

[tool.black]
line-length = 79

[project.scripts]
bdt = "bd_tools.__main__:main"

[project.urls]
Homepage = "http://github.com/betzdrive/bldc-controller"

[tool.hatch.version]
path = "src/bd_tools/__about__.py"

[tool.hatch.build.targets.sdist]
include = [
"/src/bd_tools",
]
26 changes: 2 additions & 24 deletions bd_tools/setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
from setuptools import find_packages, setup
from setuptools import setup

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name="bd_tools",
version="0.1",
description="Python API for BetzDrive",
long_description=long_description,
long_description_content_type="text/markdown",
url="http://github.com/betzdrive/bldc-controller",
author="Greg Balke",
author_email="gbalke@berkeley.edu",
license="BSD",
packages=find_packages(),
install_requires=["crcmod", "matplotlib", "numpy", "scipy", "pyserial"],
zip_safe=False,
classifiers=[
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
)
setup()
1 change: 1 addition & 0 deletions bd_tools/src/bd_tools/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1"
1 change: 1 addition & 0 deletions bd_tools/__init__.py → bd_tools/src/bd_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The betz drive tools package."""

import bd_tools.bin
import bd_tools.boards
import bd_tools.comms # noqa: F401
23 changes: 14 additions & 9 deletions bd_tools/__main__.py → bd_tools/src/bd_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
from pathlib import Path

import bd_tools

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


Expand All @@ -21,17 +23,16 @@ def parser_args(tools):

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())
# 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)
tool = getattr(bd_tools.bin, args.tool)
tool.action(tool.parser_args())


if __name__ == "__main__":
def 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
Expand Down Expand Up @@ -60,3 +61,7 @@ def action(args):
sys.argv.append("--help")

action(args)


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions bd_tools/src/bd_tools/bin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Executable tools for BetzDrive"""

import bd_tools.bin.calibrate_encoder
import bd_tools.bin.control_motor
import bd_tools.bin.read_sensor
import bd_tools.bin.update_calibration
import bd_tools.bin.upload_bootloader
import bd_tools.bin.upload_calibration
import bd_tools.bin.upload_firmware
import bd_tools.bin.view_control_loop # noqa: F401
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.
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.
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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 18 additions & 1 deletion bootloader/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("//third_party:variables.bzl", "project_inc_paths")
load("//toolchains:debug.bzl", "debug_gdb")
load("//toolchains:platforms.bzl", "bootloader_binary", "gen_binary")
load("//toolchains:os_config.bzl", "os_config")
load("//toolchains:flash.bzl", "flash_remote")
load("@bazel_embedded//tools/openocd:defs.bzl", "openocd_flash")
load("@bazel_embedded//tools/openocd:defs.bzl", "openocd_debug_server", "openocd_flash")

bootloader_binary(
name = "bootloader_raw",
Expand Down Expand Up @@ -80,3 +81,19 @@ openocd_flash(
"interface/stlink.cfg",
],
)

openocd_debug_server(
name = "debug",
device_configs = [
"target/stm32f4x.cfg",
],
interface_configs = [
"interface/stlink.cfg",
],
transport = "hla_swd",
)

debug_gdb(
name = "gdb",
elf = ":bootloader.elf",
)
Loading

0 comments on commit 9dce2d6

Please sign in to comment.