4 from typing
import Optional
12 from rosmon_msgs.srv
import StartStop, StartStopRequest
14 from .utils
import is_tool, write_flush, query_yes_no, prompt_options
15 from .board
import BoardType, determine_board, check_firmware_version
19 print(
"--> Disabling read/write protections and erasing flash")
20 subprocess.check_call(
"stm32loader -c rpi -f F4 -R -u -W", shell=
True)
22 print(
"--> Erasing flash")
23 subprocess.check_call(
"stm32loader -c rpi -f F4 -R -e", shell=
True)
25 print(
"--> Flashing bootloader")
26 subprocess.check_call(
27 f
"stm32loader -c rpi -f F4 -R -w -v {bootloader_path}", shell=
True
30 print(
"--> Flashing firmware")
31 subprocess.check_call(
32 f
"stm32loader -c rpi -f F4 -R -a 0x08010000 -w -v {firmware_path}", shell=
True
35 print(
"--> Flashing completed!")
39 print(
"--> Disabling flash read/write protections")
40 subprocess.check_call(
"stm32loader -c rpi -f F4 -u -W", shell=
True)
42 print(
"--> Erasing flash")
43 subprocess.check_call(
"stm32loader -c rpi -f F4 -e", shell=
True)
45 print(
"--> Flashing firmware")
46 subprocess.check_call(f
"stm32loader -c rpi -f F4 -w -v {firmware_path}", shell=
True)
48 print(
"--> Flashing completed!")
53 firmware_path: Optional[str] =
None,
54 board_type: Optional[BoardType] =
None,
55 check_version: bool =
True,
57 write_flush(
"--> Checking if stm32loader is installed.. ")
64 "ERROR: Cannot find the stm32loader tool. "
65 "Make sure the python3-stm32loader package is installed."
71 write_flush(
"--> Checking if ROS Master is online.. ")
73 if rosgraph.is_master_online():
81 "ROS Master is not running. "
82 "Will not be able to check the current firmware version."
84 if not query_yes_no(
"Are you sure you want to continue?", default=
"no"):
91 rospy.init_node(
"firmware_flasher", anonymous=
True)
97 write_flush(
"--> Checking if rosserial node is active.. ")
99 if rospy.resolve_name(
"serial_node")
in rosnode.get_node_names():
101 serial_node_active =
True
104 serial_node_active =
False
107 "Rosserial node is not active. "
108 "Will not be able to check the current firmware version."
110 if not query_yes_no(
"Are you sure you want to continue?", default=
"no"):
115 if master_online
and serial_node_active
and board_type
is None:
116 write_flush(
"--> Trying to determine board type.. ")
120 if board_type
is not None:
127 current_firmware_version =
"<unknown>"
129 if check_version
and master_online
and serial_node_active:
130 write_flush(
"--> Trying to check the current firmware version.. ")
134 if current_firmware_version !=
"<unknown>":
141 if master_online
and serial_node_active:
142 write_flush(
"--> Checking if rosmon service is available.. ")
144 if rospy.resolve_name(
"rosmon/start_stop")
in rosservice.get_service_list():
145 start_stop = rospy.ServiceProxy(
"rosmon/start_stop", StartStop)
147 rosmon_available =
True
150 rosmon_available =
False
154 if board_type
is None:
155 print(
"Was not able to determine the board type. Choose the board manually: ")
159 (
"LeoCore", BoardType.LEOCORE),
160 (
"Husarion CORE2", BoardType.CORE2),
166 if firmware_path
is not None:
167 firmware_version =
"<unknown>"
169 if board_type == BoardType.CORE2:
170 firmware_version =
"2.1.1"
171 elif board_type == BoardType.LEOCORE:
172 firmware_version =
"1.2.1"
174 print(f
"Current firmware version: {current_firmware_version}")
175 print(f
"Version of the firmware to flash: {firmware_version}")
182 if master_online
and serial_node_active:
186 "serial_node", rospy.get_namespace().rstrip(
"/"), StartStopRequest.STOP
192 "WARNING: rosserial node is active, but rosmon service "
193 "is not available. You have to manually stop rosserial node "
194 "before flashing the firmware."
201 if board_type == BoardType.CORE2:
202 bootloader_path = os.path.join(
203 rospkg.RosPack().get_path(
"leo_fw"),
204 "firmware/bootloader_1_0_0_core2.bin",
207 if firmware_path
is None:
208 firmware_path = os.path.join(
209 rospkg.RosPack().get_path(
"leo_fw"),
210 "firmware/core2_firmware.bin",
215 elif board_type == BoardType.LEOCORE:
216 if firmware_path
is None:
217 firmware_path = os.path.join(
218 rospkg.RosPack().get_path(
"leo_fw"),
219 "firmware/leocore_firmware.bin",
226 if master_online
and serial_node_active:
230 "serial_node", rospy.get_namespace().rstrip(
"/"), StartStopRequest.START
234 print(
"You can now start the rosserial node.")