pythonroboticspx4airsim

FailSafe mode is activated [unexpectedly] when PX4Multirotor takeoff


I'm working with AirSim and PX4. Wanna try some CV features using drone's camera in AirSim. I've properly installed all of software and it works well with examples from AirSim's repo.

So when I start demo path script AirSim\PythonClient\multirotor\path.py the drone is working good and accurately do what the Python script ask without any errors (I've try to modify its path and still no problems).

When I've added some logic to process frames from camera, the drone started to freeze after takeoff with message "Failsafe activated". I've tried to set 0 to all parameters connected to drone's safety. I've also tried to do the same from QGC.

How to fix this wrong failsafe activation?

My settings.json for AirSim:

{
  "SeeDocsAt": "https://github.com/Microsoft/AirSim/blob/main/docs/settings.md",
  "SimMode": "Multirotor",
  "ClockType": "SteppableClock",
  "SettingsVersion": 1.2,
  "CameraDefaults": {
    "CaptureSettings": [
      {
        "ImageType": 0,
        "Width": 640,
        "Height": 480
      },
      {
        "ImageType": 3,
        "Width": 640,
        "Height": 480
      },
      {
        "ImageType": 5,
        "Width": 640,
        "Height": 480
      }
    ]
  },
  "Recording": {
    "RecordOnMove": false,
    "RecordInterval": 0.033,
    "Folder": "",
    "Enabled": false,
    "Cameras": [
      {
        "CameraName": "high_res",
        "ImageType": 0,
        "PixelsAsFloat": false,
        "VehicleName": "PX4",
        "Compress": true
      }
    ]
  },
  "Vehicles": {
    "PX4": {
      "VehicleType": "PX4Multirotor",
      "UseSerial": false,
      "UseTcp": true,
      "LockStep": true,
      "QgcHostIp": "",
      "TcpPort": 4560,
      "ControlIp": "***",
      "ControlPort": 14580,
      "LocalHostIp": "***",
      "Parameters": {
        "SYS_MC_EST_GROUP": 2,
        "MPC_XY_VEL_MAX": 20,
        "MPC_XY_CRUISE": 5,
        "COM_OBL_RC_ACT": 5,
        "COM_RCL_EXCEPT": 4,
        "NAV_RCL_ACT": 0,
        "NAV_DLL_ACT": 0,
        "GF_ACTION": 0
      },
      "Sensors": {
        "barometer": {
          "SensorType": 1,
          "Enabled": true,
          "pressure_factor_sigma": 0.0001815
        }
      },
      "Cameras": {
        "track": {
          "CaptureSettings": [
            {
              "ImageType": 0,
              "Width": 640,
              "Height": 480
            }
          ],
          "X": 0.50,
          "Y": 0.0,
          "Z": 0.10,
          "Pitch": 0.0,
          "Roll": 0.0,
          "Yaw": 0.0
        }
      },
      "Logs": "C:\\<bla bla bla>\\Documents\\AirSimLogs"
    }
  }
}

My Python script fragment based on demo path.py:

/* imports */

def get_image_from_drone_as_np_array(_client: MultirotorClient, image_as_np: bool = True):
    images: List[ImageResponse] = client.simGetImages(
        [airsim.ImageRequest("0", airsim.ImageType.Scene, False, False)])

    image: ImageResponse = images[0]

    p_image = Image.frombytes(mode="RGB",
                              size=SHAPE,
                              data=image.image_data_uint8)
    if image_as_np:
        return np.array(p_image)
    else:
        return p_image

client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)

print("arming the drone...")
client.armDisarm(True)

state = client.getMultirotorState()
if state.landed_state == airsim.LandedState.Landed:
    print("taking off...")
    client.takeoffAsync().join()
else:
    client.hoverAsync().join()

time.sleep(1)

state = client.getMultirotorState()
if state.landed_state == airsim.LandedState.Landed:
    print("take off failed...")
    sys.exit(1)

z = -5
print("make sure we are hovering at {} meters...".format(-z))
client.moveToZAsync(z, 1).join() # while debugging message "Failsafe activated" appears here

tracker = ObjectTracker(det_classes=["car"])

for i in range(TRACKING_STEPS):
    print(f"Tracking step {i}")
    image_p = get_image_from_drone_as_np_array(client, image_as_np=False)
    // some CV stuff

/* etc */

PX4 logs:

...
______  __   __    ___
| ___ \ \ \ / /   /   |
| |_/ /  \ V /   / /| |
|  __/   /   \  / /_| |
| |     / /^\ \ \___  |
\_|     \/   \/     |_/

px4 starting.

INFO  [px4] startup script: /bin/sh etc/init.d-posix/rcS 0
env SYS_AUTOSTART: 10016
INFO  [param] selected parameter default file parameters.bson
INFO  [param] importing from 'parameters.bson'
INFO  [parameters] BSON document size 729 bytes, decoded 729 bytes (INT32:25, FLOAT:11)
INFO  [param] selected parameter backup file parameters_backup.bson
INFO  [dataman] data manager file './dataman' size is 7872608 bytes
INFO  [init] PX4_SIM_HOSTNAME: 172.19.208.1
INFO  [simulator_mavlink] using TCP on remote host 172.19.208.1 port 4560
WARN  [simulator_mavlink] Please ensure port 4560 is not blocked by a firewall.
INFO  [simulator_mavlink] Waiting for simulator to accept connection on TCP port 4560
INFO  [simulator_mavlink] Simulator connected on TCP port 4560.
INFO  [lockstep_scheduler] setting initial absolute time to 1707899797857182 us
WARN  [vehicle_angular_velocity] no gyro selected, using sensor_gyro_fifo:0 1310988
INFO  [commander] LED: open /dev/led0 failed (22)
WARN  [health_and_arming_checks] Preflight Fail: ekf2 missing data
INFO  [mavlink] mode: Normal, data rate: 4000000 B/s on udp port 18570 remote port 14550
INFO  [mavlink] mode: Onboard, data rate: 4000000 B/s on udp port 14580 remote port 14540
INFO  [mavlink] mode: Onboard, data rate: 4000 B/s on udp port 14280 remote port 14030
INFO  [mavlink] mode: Gimbal, data rate: 400000 B/s on udp port 13030 remote port 13280
INFO  [logger] logger started (mode=all)
INFO  [logger] Start file log (type: full)
INFO  [logger] [logger] ./log/2024-02-14/08_36_37.ulg
INFO  [logger] Opened full log file: ./log/2024-02-14/08_36_37.ulg
INFO  [mavlink] MAVLink only on localhost (set param MAV_{i}_BROADCAST = 1 to enable network)
INFO  [mavlink] MAVLink only on localhost (set param MAV_{i}_BROADCAST = 1 to enable network)
INFO  [px4] Startup script returned successfully
pxh> INFO  [tone_alarm] home set
INFO  [mavlink] partner IP: 172.19.208.1
INFO  [commander] Ready for takeoff!
INFO  [commander] Armed by external command
INFO  [tone_alarm] arming warning
INFO  [commander] Takeoff detected
WARN  [failsafe] Failsafe activated
INFO  [tone_alarm] battery warning (fast)
...

I'm using following setup:
Windows 11 (i7 14 gen + RTX 4070) + WSL2 (Ubuntu 22) for PX4
UE v4.27
AirSim v1.7.0
QGroundControl v4.3.0
PX-Autopilot v1.14.0


Solution

  • The reason of Failsafe was connection loss. Default time is 1 sec so using 1 sec pause (sleep) in script or CV algorithms broke the connection. I set parameter COM_OF_LOSS_T to 10-20, there have been no failsafe now. Closing the question.