rcb4 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rcb4/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ # flake8: noqa
2
+
3
+ import sys
4
+ import pkg_resources
5
+
6
+
7
+ if sys.version_info[0] < 3:
8
+ print("\033[91mThis package is not supported in Python 2.\033[0m")
9
+ sys.exit(1)
10
+
11
+ __version__ = pkg_resources.get_distribution("rcb4").version
rcb4/apps/__init__.py ADDED
File without changes
@@ -0,0 +1,175 @@
1
+ #!/usr/bin/env python
2
+
3
+ import argparse
4
+
5
+ import pandas as pd
6
+ from tabulate import tabulate
7
+ import yaml
8
+
9
+ from rcb4.armh7interface import ARMH7Interface
10
+
11
+
12
+ def calibrate_worms(interface, file_path, output_path, update=False, inplace=False):
13
+ with open(file_path) as file:
14
+ data = yaml.safe_load(file)
15
+
16
+ output_str = ""
17
+ for entry in data:
18
+ print(entry)
19
+ worm_id = entry["worm_id"]
20
+ servo_id = entry["servo_id"]
21
+ sensor_id = entry["sensor_id"]
22
+ magenc_present = entry.get("magenc_init", None)
23
+ if update is True:
24
+ magenc_present = None
25
+ calibrated_value = interface.calibrate_worm(
26
+ worm_id, servo_id, sensor_id, magenc_present
27
+ )
28
+ entry["magenc_init"] = calibrated_value
29
+ output_str += f" - {{worm_id: {worm_id}, servo_id: {servo_id}, sensor_id: {sensor_id}, magenc_init: {calibrated_value}}}\n"
30
+ interface.databssram_to_dataflash()
31
+ if inplace:
32
+ output_path = file_path
33
+ print(output_str, end="")
34
+ if output_path:
35
+ with open(output_path, "w") as file:
36
+ file.write(output_str)
37
+ print(f"Worm calibration file is saved to {output_path}")
38
+
39
+
40
+ def read_calib_sensors(interface, output_path):
41
+ output_str = ""
42
+ for worm_id in interface.search_worm_ids():
43
+ worm = interface.read_worm_calib_data(worm_id)
44
+ output_str += f" - {{worm_id: {worm_id}, servo_id: {worm.servo_id}, sensor_id: {worm.sensor_id}, magenc_init: {worm.magenc_init}}}\n"
45
+ print(output_str, end="")
46
+ with open(output_path, "w") as file:
47
+ file.write(output_str)
48
+ print(f"Worm calibration file is saved to {output_path}")
49
+
50
+
51
+ def print_sensor_values(interface):
52
+ try:
53
+ while True:
54
+ sensors = interface.all_jointbase_sensors()
55
+ data = {
56
+ "ID": [sensor.id for sensor in sensors],
57
+ "Magenc": [sensor.magenc for sensor in sensors],
58
+ "Proximity1": [sensor.ps[0] for sensor in sensors],
59
+ "Proximity2": [sensor.ps[1] for sensor in sensors],
60
+ "Proximity3": [sensor.ps[2] for sensor in sensors],
61
+ "Proximity4": [sensor.ps[3] for sensor in sensors],
62
+ "Force1": [sensor.adc[0] for sensor in sensors],
63
+ "Force2": [sensor.adc[1] for sensor in sensors],
64
+ "Force3": [sensor.adc[2] for sensor in sensors],
65
+ "Force4": [sensor.adc[3] for sensor in sensors],
66
+ }
67
+ df = pd.DataFrame(data)
68
+
69
+ print(tabulate(df, headers="keys", tablefmt="psql", showindex=False))
70
+ print("\033[H\033[J", end="")
71
+ except KeyboardInterrupt:
72
+ pass
73
+
74
+
75
+ def print_worm_values(interface):
76
+ attributes = [
77
+ "servo_id",
78
+ "sensor_id",
79
+ "magenc_init",
80
+ "magenc_present",
81
+ "present_angle",
82
+ ]
83
+ try:
84
+ while True:
85
+ sensors = [
86
+ interface.read_worm_calib_data(worm_idx)
87
+ for worm_idx in interface.search_worm_ids()
88
+ ]
89
+ data = {attr: [] for attr in attributes}
90
+ for sensor in sensors:
91
+ for attr in attributes:
92
+ data[attr].append(getattr(sensor, attr, "N/A"))
93
+ df = pd.DataFrame(data)
94
+ print(tabulate(df, headers="keys", tablefmt="psql", showindex=False))
95
+ print("\033[H\033[J", end="")
96
+ except KeyboardInterrupt:
97
+ pass
98
+
99
+
100
+ def main():
101
+ parser = argparse.ArgumentParser()
102
+ subparsers = parser.add_subparsers()
103
+
104
+ parser_calibrate = subparsers.add_parser(
105
+ "calibrate", help="Calibrate worm gear module."
106
+ )
107
+ parser_calibrate.add_argument(
108
+ "--device", "-d", default=None, help="Input device port."
109
+ )
110
+ parser_calibrate.add_argument("file_path", type=str, help="Path to yaml file.")
111
+ parser_calibrate.add_argument(
112
+ "--inplace", "-i", action="store_true", help="Overwrite file_path."
113
+ )
114
+ parser_calibrate.add_argument(
115
+ "--update",
116
+ "-u",
117
+ action="store_true",
118
+ help="Overwrite input yaml's current magenc_init.",
119
+ )
120
+ parser_calibrate.add_argument(
121
+ "--output", "-o", type=str, help="Output file path for calibrated data."
122
+ )
123
+
124
+ parser_calibrate.set_defaults(
125
+ func=lambda args: calibrate_worms(
126
+ ARMH7Interface.from_port(args.device),
127
+ args.file_path,
128
+ output_path=args.output,
129
+ update=args.update,
130
+ inplace=args.inplace,
131
+ )
132
+ )
133
+
134
+ parser_read_calib = subparsers.add_parser(
135
+ "read-calib", help="Read worm gear module calib data from armh7"
136
+ )
137
+ parser_read_calib.add_argument(
138
+ "--device", "-d", default=None, help="Input device port."
139
+ )
140
+ parser_read_calib.add_argument(
141
+ "output_file_path", type=str, help="Output path to yaml file."
142
+ )
143
+ parser_read_calib.set_defaults(
144
+ func=lambda args: read_calib_sensors(
145
+ ARMH7Interface.from_port(args.device), args.output_file_path
146
+ )
147
+ )
148
+
149
+ parser_print_sensor = subparsers.add_parser(
150
+ "print-sensor", help="Print sensor values."
151
+ )
152
+ parser_print_sensor.add_argument(
153
+ "--device", "-d", default=None, help="Input device port."
154
+ )
155
+ parser_print_sensor.set_defaults(
156
+ func=lambda args: print_sensor_values(ARMH7Interface.from_port(args.device))
157
+ )
158
+
159
+ parser_print_worm = subparsers.add_parser("print-worm", help="Print worm values.")
160
+ parser_print_worm.add_argument(
161
+ "--device", "-d", default=None, help="Input device port."
162
+ )
163
+ parser_print_worm.set_defaults(
164
+ func=lambda args: print_worm_values(ARMH7Interface.from_port(args.device))
165
+ )
166
+
167
+ args = parser.parse_args()
168
+ if hasattr(args, "func"):
169
+ args.func(args)
170
+ else:
171
+ parser.print_help()
172
+
173
+
174
+ if __name__ == "__main__":
175
+ main()
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env python
2
+
3
+ from pathlib import Path
4
+ import shutil
5
+ import subprocess
6
+ import sys
7
+
8
+ from rcb4.data import kondoh7_elf
9
+ from rcb4.data import stlink
10
+
11
+
12
+ def check_dependencies():
13
+ objcopy = shutil.which("arm-none-eabi-objcopy")
14
+ if objcopy is None:
15
+ print("Please install arm-none-eabi-objcopy.")
16
+ print("sudo apt install -y binutils-arm-none-eabi")
17
+ sys.exit(1)
18
+
19
+
20
+ def convert_elf_to_bin(elf_path, bin_path):
21
+ cmd = ["arm-none-eabi-objcopy", "-O", "binary", elf_path, bin_path]
22
+ result = subprocess.run(cmd, capture_output=True, text=True)
23
+ if result.returncode != 0:
24
+ print(f"Error converting ELF to BIN: {result.stderr}")
25
+ sys.exit(2)
26
+
27
+
28
+ def flash_bin_to_device(st_flash_path, bin_path):
29
+ cmd = [st_flash_path, "write", bin_path, "0x08000000"]
30
+ result = subprocess.run(cmd, capture_output=True, text=True)
31
+ if result.returncode != 0:
32
+ print(f"Error flashing BIN to device: {result.stderr}")
33
+ sys.exit(3)
34
+
35
+
36
+ def main():
37
+ check_dependencies()
38
+
39
+ st_flash_path = stlink()
40
+ elf_path = kondoh7_elf()
41
+ bin_path = Path(elf_path).with_suffix(".bin")
42
+
43
+ convert_elf_to_bin(elf_path, bin_path)
44
+ flash_bin_to_device(st_flash_path, bin_path)
45
+
46
+
47
+ if __name__ == "__main__":
48
+ main()