Skip to content

Commit f542702

Browse files
authored
Swarm flash utility (#456)
* add flash as executable * manage flashing with simple zipfile * flash both zip as bins * works with absolute path * add flash to cfmultitool * flash target based only on file name * add missing variable
1 parent ff3f673 commit f542702

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

crazyflie/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ install(PROGRAMS
111111
scripts/simple_mapper_multiranger.py
112112
scripts/aideck_streamer.py
113113
scripts/gui.py
114+
scripts/flash.py
114115
DESTINATION lib/${PROJECT_NAME}
115116
)
116117

crazyflie/scripts/cfmult.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"version",
2626
"listLogVariables",
2727
"listParams",
28-
"listMemories"
28+
"listMemories",
29+
"flash",
2930
}
3031

3132

@@ -46,6 +47,9 @@ def main():
4647
yaml_parser.add_argument("-C", "--configpath",
4748
default=Path(__file__).parent.parent.resolve() / "config",
4849
help="Path to the configuration *.yaml files")
50+
51+
parser.add_argument("--file_name", help="File name for flashing the crazyflie",
52+
type=str, default="")
4953

5054
args = parser.parse_args()
5155

@@ -63,13 +67,25 @@ def main():
6367

6468
# Run for all URI's determined.
6569
for uri in uris:
66-
cmd = [
70+
if args.subcommand[0] == "flash":
71+
SUBPROC_TIMEOUT = 20
72+
cmd = [
6773
"ros2",
6874
"run",
6975
"crazyflie",
70-
*args.subcommand,
76+
"flash.py",
7177
f"--uri={uri}",
78+
f"--file_name={args.file_name}"
7279
]
80+
else:
81+
SUBPROC_TIMEOUT = 1
82+
cmd = [
83+
"ros2",
84+
"run",
85+
"crazyflie",
86+
*args.subcommand,
87+
f"--uri={uri}",
88+
]
7389
print(f"{' '.join(cmd)}")
7490
subprocess.run(cmd, timeout=SUBPROC_TIMEOUT)
7591

crazyflie/scripts/flash.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
A flash utility script to flash crazyflies with the latest or custom firmware
5+
6+
2024 - K. N. McGuire (Bitcraze AB)
7+
"""
8+
9+
import rclpy
10+
from rclpy.node import Node
11+
import cflib.crtp # noqa
12+
from cflib.bootloader import Bootloader, Target
13+
from cflib.bootloader.boottypes import BootVersion
14+
import argparse
15+
import os
16+
17+
class Flash(Node):
18+
def __init__(self, uri, file_name):
19+
super().__init__('flash')
20+
21+
self.get_logger().info(f"Flashing {uri} with {file_name}")
22+
23+
base_file_name = os.path.basename(file_name)
24+
25+
if base_file_name.endswith("zip") and base_file_name.startswith("firmware-cf2"):
26+
targets = []
27+
elif base_file_name.endswith("bin") and base_file_name.startswith("cf2"):
28+
targets = [Target("cf2", 'stm32', 'fw', [], [])]
29+
else:
30+
self.get_logger().error(f"Unsupported file type or name. Only cf2*.bin or firmware-cf2*.zip supported")
31+
return
32+
33+
bl = Bootloader(uri)
34+
try:
35+
bl.flash_full(None, file_name, True, targets)
36+
except Exception as e:
37+
self.get_logger().error(f"Failed to flash, Error {str(e)}")
38+
finally:
39+
if bl:
40+
bl.close()
41+
42+
return
43+
44+
def main(args=None):
45+
cflib.crtp.init_drivers()
46+
rclpy.init(args=args)
47+
parser = argparse.ArgumentParser(description="This is a sample script")
48+
parser.add_argument('--uri', type=str, default="radio://0/80/2M/E7E7E7E7E7", help='unique resource identifier')
49+
parser.add_argument('--file_name', type=str, default="cf2.bin", help='')
50+
51+
# Parse the arguments
52+
args = parser.parse_args()
53+
54+
print("URI: ", args.uri)
55+
flash_node = Flash(args.uri, args.file_name)
56+
57+
flash_node.destroy_node()
58+
rclpy.shutdown()
59+
60+
if __name__ == '__main__':
61+
main()

0 commit comments

Comments
 (0)