main.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import os
4 import sys
5 import subprocess
6 
7 import rospy
8 import rospkg
9 import rosgraph
10 import rosnode
11 import rosservice
12 
13 from std_srvs.srv import Trigger
14 from rosmon_msgs.srv import StartStop, StartStopRequest
15 
16 from .utils import is_tool, write_flush, query_yes_no
17 
18 
19 def flash_firmware(
20  bootloader_path, firmware_path, firmware_version="<unknown>", check_version=True
21 ):
22  write_flush("--> Checking if stm32loader is installed.. ")
23 
24  if is_tool("stm32loader"):
25  print("YES")
26  else:
27  print("NO")
28  print(
29  "ERROR: Cannot find the stm32loader tool. "
30  "Make sure the python3-stm32loader package is installed."
31  )
32  return
33 
34  write_flush("--> Checking if ROS Master is online.. ")
35 
36  if rosgraph.is_master_online():
37  print("YES")
38  master_online = True
39  else:
40  print("NO")
41  master_online = False
42  if check_version:
43  print(
44  "ROS Master is not running. "
45  "Will not be able to check the current firmware version."
46  )
47  if not query_yes_no("Are you sure you want to continue?", default="no"):
48  return
49 
50  if master_online:
51  write_flush("--> Initializing ROS node.. ")
52  rospy.init_node("firmware_flasher", anonymous=True)
53  print("DONE")
54 
55  if master_online:
56  write_flush("--> Checking if rosserial node is active.. ")
57 
58  if "/serial_node" in rosnode.get_node_names():
59  print("YES")
60  serial_node_active = True
61  else:
62  print("NO")
63  serial_node_active = False
64  if check_version:
65  print(
66  "Rosserial node is not active. "
67  "Will not be able to check the current firmware version."
68  )
69  if not query_yes_no("Are you sure you want to continue?", default="no"):
70  return
71 
72  current_firmware_version = "<unknown>"
73 
74  if check_version and master_online and serial_node_active:
75  write_flush("--> Checking the current firmware version.. ")
76 
77  if "/core2/get_firmware_version" in rosservice.get_service_list():
78  get_firmware_version = rospy.ServiceProxy(
79  "/core2/get_firmware_version", Trigger
80  )
81  current_firmware_version = get_firmware_version().message
82  print("OK")
83  else:
84  print("FAIL")
85  print(
86  "WARNING: Could not get the current firmware version: "
87  "/core2/get_firmware_version service is not available."
88  )
89 
90  print("Current firmware version: {}".format(current_firmware_version))
91  print("Version of the firmware to flash: {}".format(firmware_version))
92 
93  if master_online and serial_node_active:
94  write_flush("--> Checking if rosmon service is available.. ")
95 
96  if "/rosmon/start_stop" in rosservice.get_service_list():
97  start_stop = rospy.ServiceProxy("/rosmon/start_stop", StartStop)
98  print("YES")
99  rosmon_available = True
100  else:
101  print("NO")
102  rosmon_available = False
103 
104  if not query_yes_no("Flash the firmware?"):
105  return
106 
107  if master_online and serial_node_active:
108  if rosmon_available:
109  write_flush("--> Stopping the rosserial node.. ")
110  start_stop("serial_node", "", StartStopRequest.STOP)
111  rospy.sleep(1)
112  print("DONE")
113  else:
114  print(
115  "WARNING: rosserial node is active, but rosmon service "
116  "is not available. You have to manually stop rosserial node "
117  "before flashing the firmware."
118  )
119  if not query_yes_no("Continue?", default="no"):
120  return
121 
122  print("--> Disabling flash write protection")
123  subprocess.check_call("stm32loader -c rpi -f F4 -W", shell=True)
124 
125  print("--> Erasing flash and flashing bootloader")
126  subprocess.check_call(
127  "stm32loader -c rpi -f F4 -e -w -v {}".format(bootloader_path), shell=True
128  )
129 
130  print("--> Flashing firmware")
131  subprocess.check_call(
132  "stm32loader -c rpi -f F4 -a 0x08010000 -w -v {}".format(firmware_path),
133  shell=True,
134  )
135 
136  print("Flashing completed!")
137 
138  if master_online and serial_node_active:
139  if rosmon_available:
140  write_flush("--> Starting the rosserial node.. ")
141  start_stop("serial_node", "", StartStopRequest.START)
142  print("DONE")
143  else:
144  print("You can now start the rosserial node.")
def write_flush(msg)
Write a message to standard output and flush the buffer.
Definition: utils.py:46
def is_tool(name)
Check whether an executable exists on PATH.
Definition: utils.py:5
def flash_firmware(bootloader_path, firmware_path, firmware_version="<unknown>", check_version=True)
Definition: main.py:21
def query_yes_no(question, default="yes")
Ask a yes/no question via raw_input() and return their answer.
Definition: utils.py:16


leo_fw
Author(s): Błażej Sowa
autogenerated on Mon Feb 28 2022 22:43:59