Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swarm flash utility #456

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crazyflie/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ install(PROGRAMS
scripts/simple_mapper_multiranger.py
scripts/aideck_streamer.py
scripts/gui.py
scripts/flash.py
DESTINATION lib/${PROJECT_NAME}
)

Expand Down
22 changes: 19 additions & 3 deletions crazyflie/scripts/cfmult.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"version",
"listLogVariables",
"listParams",
"listMemories"
"listMemories",
"flash",
}


Expand All @@ -46,6 +47,9 @@ def main():
yaml_parser.add_argument("-C", "--configpath",
default=Path(__file__).parent.parent.resolve() / "config",
help="Path to the configuration *.yaml files")

parser.add_argument("--file_name", help="File name for flashing the crazyflie",
type=str, default="")

args = parser.parse_args()

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

# Run for all URI's determined.
for uri in uris:
cmd = [
if args.subcommand[0] == "flash":
SUBPROC_TIMEOUT = 20
cmd = [
"ros2",
"run",
"crazyflie",
*args.subcommand,
"flash.py",
f"--uri={uri}",
f"--file_name={args.file_name}"
]
else:
SUBPROC_TIMEOUT = 1
cmd = [
"ros2",
"run",
"crazyflie",
*args.subcommand,
f"--uri={uri}",
]
print(f"{' '.join(cmd)}")
subprocess.run(cmd, timeout=SUBPROC_TIMEOUT)

Expand Down
61 changes: 61 additions & 0 deletions crazyflie/scripts/flash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3

"""
A flash utility script to flash crazyflies with the latest or custom firmware

2024 - K. N. McGuire (Bitcraze AB)
"""

import rclpy
from rclpy.node import Node
import cflib.crtp # noqa
from cflib.bootloader import Bootloader, Target
from cflib.bootloader.boottypes import BootVersion
import argparse
import os

class Flash(Node):
def __init__(self, uri, file_name):
super().__init__('flash')

self.get_logger().info(f"Flashing {uri} with {file_name}")

base_file_name = os.path.basename(file_name)

if base_file_name.endswith("zip") and base_file_name.startswith("firmware-cf2"):
targets = []
elif base_file_name.endswith("bin") and base_file_name.startswith("cf2"):
targets = [Target("cf2", 'stm32', 'fw', [], [])]
else:
self.get_logger().error(f"Unsupported file type or name. Only cf2*.bin or firmware-cf2*.zip supported")
return

bl = Bootloader(uri)
try:
bl.flash_full(None, file_name, True, targets)
except Exception as e:
self.get_logger().error(f"Failed to flash, Error {str(e)}")
finally:
if bl:
bl.close()

return

def main(args=None):
cflib.crtp.init_drivers()
rclpy.init(args=args)
parser = argparse.ArgumentParser(description="This is a sample script")
parser.add_argument('--uri', type=str, default="radio://0/80/2M/E7E7E7E7E7", help='unique resource identifier')
parser.add_argument('--file_name', type=str, default="cf2.bin", help='')

# Parse the arguments
args = parser.parse_args()

print("URI: ", args.uri)
flash_node = Flash(args.uri, args.file_name)

flash_node.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()
Loading