55class PX4TakeoffStateMachine :
66 """PX4TakeoffStateMachine."""
77
8- def __init__ (self , node , px4_comm ):
8+ # pylint: disable=too-many-instance-attributes
9+
10+ def __init__ (self , node , px4_comm , config = None ):
911 """
1012 Initialize the state machine.
1113
1214 Args:
1315 node: The ROS2 node
1416 px4_comm: The PX4 communication handler
17+ config: Configuration dictionary loaded from YAML file
1518 """
1619 self .node = node
1720 self .px4_comm = px4_comm
21+ self .config = config or {}
1822
1923
2024 # Define the state sequence for the complete flight operation
@@ -97,7 +101,7 @@ def _handle_waiting_for_status(self, current_time_sec):
97101 self .state_variables ['enter_offboard_mode' ] = {
98102 'start_time' : 0 ,
99103 'last_offboard_set_time' : 0 ,
100- 'offboard_set_interval' : 0.2
104+ 'offboard_set_interval' : self . config [ 'offboard_set_interval' ]
101105 }
102106 # Set the start time for the offboard transition in the next state
103107 offboard_vars = self .state_variables ['enter_offboard_mode' ]
@@ -117,7 +121,7 @@ def _handle_enter_offboard_mode(self, current_time_sec):
117121 self .state_variables [current_state ] = {
118122 'start_time' : 0 ,
119123 'last_offboard_set_time' : 0 ,
120- 'offboard_set_interval' : 0.2 # seconds (200ms) to periodically try switching to offboard mode
124+ 'offboard_set_interval' : self . config [ 'offboard_set_interval' ] # seconds to periodically try switching to offboard mode
121125 }
122126
123127 offboard_vars = self .state_variables [current_state ]
@@ -127,20 +131,14 @@ def _handle_enter_offboard_mode(self, current_time_sec):
127131 offboard_vars ['start_time' ] = current_time_sec
128132 offboard_vars ['last_offboard_set_time' ] = current_time_sec
129133
130- last_offboard_set_time = offboard_vars ['last_offboard_set_time' ]
131-
132134 # Continue sending zero velocity commands while waiting for mode switch
133135 self .px4_comm .send_zero_velocity_setpoint ()
134136 self .px4_comm .set_offboard_mode () # Continue publishing offboard mode
135137
136138 # Re-send VEHICLE_CMD_DO_SET_MODE command periodically
137- offboard_set_interval = offboard_vars ['offboard_set_interval' ] # Get interval from state variables
138- if current_time_sec - last_offboard_set_time > offboard_set_interval :
139+ if current_time_sec - offboard_vars ['last_offboard_set_time' ] > offboard_vars ['offboard_set_interval' ]:
139140 self .px4_comm .send_offboard_mode_command ()
140- last_offboard_set_time = current_time_sec # Update the time when command is sent
141-
142- # Update the stored time
143- offboard_vars ['last_offboard_set_time' ] = last_offboard_set_time
141+ offboard_vars ['last_offboard_set_time' ] = current_time_sec # Update the time when command is sent
144142
145143 # Check if the vehicle has switched to offboard mode
146144 success = (self .px4_comm .vehicle_status
@@ -166,9 +164,9 @@ def _handle_arming_vehicle(self, current_time_sec):
166164 if self .px4_comm .vehicle_status .arming_state == VehicleStatus .ARMING_STATE_ARMED :
167165 return True
168166
169- # Re-send arm command periodically with proper throttling
167+ # Re-send arm command periodically with proper throttling using config
170168 last_arm_time = self .state_variables ['arming_vehicle' ]['last_arm_command_time' ]
171- if last_arm_time is None or current_time_sec - last_arm_time >= 1.0 : # Every second
169+ if last_arm_time is None or current_time_sec - last_arm_time >= self . config [ 'arm_command_interval' ]:
172170 self .px4_comm .arm_vehicle ()
173171 self .state_variables ['arming_vehicle' ]['last_arm_command_time' ] = current_time_sec # Update the time when command is sent
174172
@@ -186,8 +184,8 @@ def _handle_sending_takeoff(self, current_time_sec): # pylint: disable=unused-a
186184 return True
187185
188186 if self .px4_comm .vehicle_status .nav_state != VehicleStatus .NAVIGATION_STATE_AUTO_TAKEOFF :
189- # Send takeoff command
190- self .px4_comm .send_takeoff_command (10.0 )
187+ # Send takeoff command with altitude from config
188+ self .px4_comm .send_takeoff_command (self . config [ 'takeoff_altitude' ] )
191189
192190 return False
193191
@@ -210,15 +208,11 @@ def _handle_performing_velocity_forward(self, current_time_sec):
210208 vel_vars ['start_time' ] = current_time_sec
211209 vel_vars ['phase_start_time' ] = current_time_sec
212210
213- # Define parameters for the velocity pattern
214- forward_duration = 4.0 # seconds for forward velocity
215- forward_velocity = [2.0 , 0.0 , 0.0 ] # m/s in x, y, z directions
216-
217211 # Send forward velocity command
218- self .px4_comm .send_velocity_setpoint (forward_velocity )
212+ self .px4_comm .send_velocity_setpoint (self . config [ 'velocity_commands' ][ 'forward' ][ 'velocity' ] )
219213
220- # Print position every second while executing velocity commands
221- if self .px4_comm .should_print_position (current_time_sec ):
214+ # Print position based on interval from config while executing velocity commands
215+ if self .px4_comm .should_print_position (current_time_sec , self . config [ 'position_print_interval' ] ):
222216 position = self .px4_comm .get_current_position ()
223217 if position :
224218 x , y , z = position
@@ -227,7 +221,7 @@ def _handle_performing_velocity_forward(self, current_time_sec):
227221 self .node .get_logger ().info ('Position unavailable' )
228222
229223 elapsed_time = current_time_sec - vel_vars ['phase_start_time' ]
230- if elapsed_time >= forward_duration :
224+ if elapsed_time >= self . config [ 'velocity_commands' ][ 'forward' ][ 'duration' ] :
231225 return True
232226
233227 return False
@@ -251,15 +245,11 @@ def _handle_performing_velocity_zero(self, current_time_sec):
251245 vel_vars ['start_time' ] = current_time_sec
252246 vel_vars ['phase_start_time' ] = current_time_sec
253247
254- # Define parameters for the velocity pattern
255- zero_duration = 2.0 # seconds for zero velocity
256- zero_velocity = [0.0 , 0.0 , 0.0 ] # m/s
257-
258248 # Send zero velocity command
259- self .px4_comm .send_velocity_setpoint (zero_velocity )
249+ self .px4_comm .send_velocity_setpoint (self . config [ 'velocity_commands' ][ 'zero' ][ 'velocity' ] )
260250
261- # Print position every second while executing velocity commands
262- if self .px4_comm .should_print_position (current_time_sec ):
251+ # Print position based on interval from config while executing velocity commands
252+ if self .px4_comm .should_print_position (current_time_sec , self . config [ 'position_print_interval' ] ):
263253 position = self .px4_comm .get_current_position ()
264254 if position :
265255 x , y , z = position
@@ -268,7 +258,7 @@ def _handle_performing_velocity_zero(self, current_time_sec):
268258 self .node .get_logger ().info ('Position unavailable' )
269259
270260 elapsed_time = current_time_sec - vel_vars ['phase_start_time' ]
271- if elapsed_time >= zero_duration :
261+ if elapsed_time >= self . config [ 'velocity_commands' ][ 'zero' ][ 'duration' ] :
272262 return True
273263
274264 return False
@@ -292,15 +282,11 @@ def _handle_performing_velocity_backward(self, current_time_sec):
292282 vel_vars ['start_time' ] = current_time_sec
293283 vel_vars ['phase_start_time' ] = current_time_sec
294284
295- # Define parameters for the velocity pattern
296- backward_duration = 4.0 # seconds for backward velocity
297- backward_velocity = [- 2.0 , 0.0 , 0.0 ] # m/s in x, y, z directions
298-
299285 # Send backward velocity command
300- self .px4_comm .send_velocity_setpoint (backward_velocity )
286+ self .px4_comm .send_velocity_setpoint (self . config [ 'velocity_commands' ][ 'backward' ][ 'velocity' ] )
301287
302- # Print position every second while executing velocity commands
303- if self .px4_comm .should_print_position (current_time_sec ):
288+ # Print position based on interval from config while executing velocity commands
289+ if self .px4_comm .should_print_position (current_time_sec , self . config [ 'position_print_interval' ] ):
304290 position = self .px4_comm .get_current_position ()
305291 if position :
306292 x , y , z = position
@@ -309,7 +295,7 @@ def _handle_performing_velocity_backward(self, current_time_sec):
309295 self .node .get_logger ().info ('Position unavailable' )
310296
311297 elapsed_time = current_time_sec - vel_vars ['phase_start_time' ]
312- if elapsed_time >= backward_duration :
298+ if elapsed_time >= self . config [ 'velocity_commands' ][ 'backward' ][ 'duration' ] :
313299 return True
314300
315301 return False
0 commit comments