From d8a21a32cea3975551ff8c9e2c532fbcc2804ee1 Mon Sep 17 00:00:00 2001 From: dominikn Date: Thu, 7 Dec 2023 18:48:54 +0100 Subject: [PATCH] firmware flashing scripts embedded into rosbot_ros package --- Dockerfile.hardware | 46 +++------------ flash-firmware-usb.py | 109 ------------------------------------ flash-firmware.py | 0 microros_localhost_only.xml | 26 --------- 4 files changed, 7 insertions(+), 174 deletions(-) delete mode 100755 flash-firmware-usb.py mode change 100755 => 100644 flash-firmware.py delete mode 100644 microros_localhost_only.xml diff --git a/Dockerfile.hardware b/Dockerfile.hardware index c07482c..c48ed4b 100644 --- a/Dockerfile.hardware +++ b/Dockerfile.hardware @@ -1,29 +1,6 @@ ARG ROS_DISTRO=humble ARG PREFIX= ARG ROSBOT_FW_RELEASE=0.8.0 -## ============================ STM32FLASH ================================= -# stm32flash needs an older version of glibc (2.28), which is why ubuntu 18.04 was used -FROM ubuntu:18.04 AS stm32flash_builder - -ARG ROS_DISTRO -ARG ROSBOT_FW_RELEASE -ARG TARGETARCH - -SHELL ["/bin/bash", "-c"] - -# official releases are only for intel archs, so we need to build stm32flash from sources -RUN apt-get update && apt-get install -y \ - curl - -RUN echo ros_distro=$ROS_DISTRO firmware_release=$ROSBOT_FW_RELEASE - -# Copy firmware binaries -RUN curl -L https://github.com/husarion/stm32flash/releases/download/2023-12-05/stm32flash-$TARGETARCH -o /stm32flash && \ - chmod +x /stm32flash - -# Copy firmware binaries -RUN curl -L https://github.com/husarion/rosbot_ros2_firmware/releases/download/$ROSBOT_FW_RELEASE/firmware.bin -o /firmware.bin && \ - curl -L https://github.com/husarion/rosbot_ros2_firmware/releases/download/$ROSBOT_FW_RELEASE/firmware.hex -o /firmware.hex ## =========================== Firmware CPU ID ================================ @@ -91,13 +68,10 @@ RUN mkdir src COPY ./healthcheck.cpp / RUN apt-get update && apt-get install -y \ - git \ python3-pip \ - python3-sh \ - python3-periphery && \ - pip3 install pyserial + python3-sh -RUN git clone --depth 1 https://github.com/husarion/rosbot_ros.git src -b humble && \ +RUN git clone --depth 1 https://github.com/husarion/rosbot_ros.git src -b ros2-rosbot-utils && \ vcs import src < src/rosbot/rosbot_hardware.repos && \ cp -r src/ros2_controllers/diff_drive_controller src/ && \ cp -r src/ros2_controllers/imu_sensor_broadcaster src/ && \ @@ -130,9 +104,9 @@ COPY --from=healthcheck_builder /ros2_ws /ros2_ws_healthcheck RUN apt-get update && apt-get install -y \ python3-pip \ python3-rosdep \ - usbutils \ + stm32flash \ ros-$ROS_DISTRO-teleop-twist-keyboard && \ - pip3 install pyftdi sh && \ + pip3 install pyftdi && \ rm -rf /etc/ros/rosdep/sources.list.d/20-default.list && \ rosdep init && \ rosdep update --rosdistro $ROS_DISTRO && \ @@ -140,15 +114,13 @@ RUN apt-get update && apt-get install -y \ # Save version echo $(cat /ros2_ws/src/rosbot/package.xml | grep '' | sed -r 's/.*([0-9]+.[0-9]+.[0-9]+)<\/version>/\1/g') >> /version.txt && \ # Size optimalization + apt-get remove -y \ + python3-rosdep \ + python3-pip && \ apt-get clean && \ rm -rf src && \ rm -rf /var/lib/apt/lists/* -# copy firmware built in previous stage and downloaded repository -COPY --from=stm32flash_builder /firmware.bin /root/firmware.bin -COPY --from=stm32flash_builder /firmware.hex /root/firmware.hex -COPY --from=stm32flash_builder /stm32flash /usr/bin/stm32flash - COPY --from=cpu_id_builder /read_cpu_id/.pio/build/olimex_e407/firmware.bin /firmware_read_cpu_id.bin COPY ros_entrypoint.sh / @@ -158,10 +130,6 @@ COPY healthcheck.sh / HEALTHCHECK --interval=5s --timeout=2s --start-period=5s --retries=4 \ CMD ["/healthcheck.sh"] -COPY microros_localhost_only.xml / - # copy scripts -COPY flash-firmware.py / COPY flash-firmware.py /usr/bin/ -COPY flash-firmware-usb.py /usr/bin/ COPY print-serial-number.py /usr/bin/ diff --git a/flash-firmware-usb.py b/flash-firmware-usb.py deleted file mode 100755 index 75a606c..0000000 --- a/flash-firmware-usb.py +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/python3 - -import sh -import time -import sys -import argparse -from pyftdi.ftdi import Ftdi - -# CBUS0 - BOOT0 -# CBUS1 - RST - -class FirmwareFlasher: - def __init__(self, binary_file, port): - - # ftdi.show_devices() - # self.ftdi = Ftdi.create_from_url('ftdi://ftdi:ft-x:DK0AM0V0/1') - self.device = 'ftdi://ftdi:ft-x:/1' - self.ftdi = Ftdi() - - self.binary_file = binary_file - self.max_approach_no = 3 - self.port = port - - def enter_bootloader_mode(self): - - self.ftdi.open_from_url(url=self.device) - self.ftdi.set_cbus_direction(0b11,0b11) # set CBUS0 and CBUS1 to output - time.sleep(0.1) - self.ftdi.set_cbus_gpio(0b11) #set CBUS0 to 1 and RST to 1 - time.sleep(0.1) - self.ftdi.set_cbus_gpio(0b01) #set CBUS0 to 1 and RST to 0 - time.sleep(0.1) - # self.ftdi.set_cbus_direction(0b11,0b00) # set CBUS0 and CBUS1 to input - time.sleep(0.1) - self.ftdi.close() - - def exit_bootloader_mode(self): - - self.ftdi.open_from_url(url=self.device) - self.ftdi.set_cbus_direction(0b11,0b11) # set CBUS0 and CBUS1 to output - time.sleep(0.1) - self.ftdi.set_cbus_gpio(0b10) #set CBUS0 to 1 and RST to 1 - time.sleep(0.1) - self.ftdi.set_cbus_gpio(0b00) #set CBUS0 to 1 and RST to 0 - time.sleep(0.1) - # self.ftdi.set_cbus_direction(0b11,0b00) # set CBUS0 and CBUS1 to input - time.sleep(0.1) - self.ftdi.close() - - def try_flash_operation(self, operation_name, flash_command, flash_args): - for i in range(self.max_approach_no): - try: - self.enter_bootloader_mode() - sh.usbreset("0403:6015") - flash_command(self.port, *flash_args, _out=sys.stdout) - self.exit_bootloader_mode() - time.sleep(0.2) - break - except Exception as e: - print(f"{operation_name} error! Trying again.") - print(f"Error: {e}") - print("---------------------------------------") - else: - print(f"WARNING! {operation_name} went wrong.") - - def flash_firmware(self): - # Disable the flash write-protection - self.try_flash_operation("Write-UnProtection", sh.stm32flash, ["-u"]) - - # Disable the flash read-protection - self.try_flash_operation("Read-UnProtection", sh.stm32flash, ["-k"]) - - # Flashing the firmware - # /usr/bin/stm32flash /dev/ttyUSB0 -v -w /root/firmware.bin -b 115200 - flash_args = ["-v", "-w", self.binary_file, "-b", "115200"] - self.try_flash_operation("Flashing", sh.stm32flash, flash_args) - - - sh.usbreset("0403:6015") - - -def main(): - - parser = argparse.ArgumentParser( - description='Flashing the firmware on STM32 microcontroller in ROSbot XL') - - parser.add_argument( - "-f", - "--file", - nargs='?', - default="/root/firmware.bin", - help="Path to a firmware file. Default: /root/firmware.bin") - parser.add_argument( - "-p", - "--port", - nargs='?', - default="/dev/ttyUSB0", - help="Path to serial connection. Default: /dev/ttyUSB0") - - binary_file = parser.parse_args().file - port = parser.parse_args().port - - flasher = FirmwareFlasher(binary_file, port) - flasher.flash_firmware() - print("Done.") - - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/flash-firmware.py b/flash-firmware.py old mode 100755 new mode 100644 diff --git a/microros_localhost_only.xml b/microros_localhost_only.xml deleted file mode 100644 index d433142..0000000 --- a/microros_localhost_only.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - CustomUdpTransport - UDPv4 - -
127.0.0.1
-
-
-
- - - - - CustomUdpTransport - - - false - - -
-
\ No newline at end of file