dronekit / dronekit-python

DroneKit-Python library for communicating with Drones via MAVLink.
https://readthedocs.org/projects/dronekit-python/
Apache License 2.0
1.56k stars 1.43k forks source link

Airsim/Ardupilot Listeners for sensor data #1042

Open mgroov opened 4 years ago

mgroov commented 4 years ago

I am currently working on a windows wsl 18.04 with dronekit and ardupilot looking at an air sim sitl. I can use the script to have the drone take off and land with proper altitude readings. However, I cannot get the * message listener to print anything at all as well as a rangefinder despite one being defined in the settings.json.

Dronekit version:2.9.2

The script I am using:

import dronekit
import pymavlink
from dronekit import connect,VehicleMode
import time

# Connect to the Vehicle (in this case a simulator running the same computer)
vehicle = connect('127.0.0.1:14550', wait_ready=True)

@vehicle.on_message('*')
def listener(self, name, message):
    print ('message: %s' % message)
    print ('potato')

print ("Mode: %s" % vehicle.mode.name )   # settable

#Create a message listener using the decorator.
@vehicle.on_message('LIDAR')
def listener(self, name, message):
    print ( 'message: %s' % message)
    print ('have fun at boss fight')

def arm_and_takeoff(aTargetAltitude):
    """
    Arms vehicle and fly to aTargetAltitude.
    """

    print ("Basic pre-arm checks")
    # Don't try to arm until autopilot is ready
    while not vehicle.is_armable:
        print (" Waiting for vehicle to initialise...")
        time.sleep(1)

    print ("Arming motors")
    # Copter should arm in GUIDED mode
    vehicle.mode    = VehicleMode("GUIDED")
    vehicle.armed   = True

    # Confirm vehicle armed before attempting to take off
    while not vehicle.armed:
        print (" Waiting for arming...")
        time.sleep(1)

    print ("Taking off!")
    vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude

    # Wait until the vehicle reaches a safe height before processing the goto (otherwise the command
    #  after Vehicle.simple_takeoff will execute immediately).
    while True:
        print (" Altitude: ", vehicle.location.global_relative_frame.alt)
        #Break and return from function just below target altitude.
        if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:
            print ("Reached target altitude")
            break
        time.sleep(1)

arm_and_takeoff(20)
time.sleep(10)
vehicle.mode    = VehicleMode("LAND")
time.sleep(5)
arm_and_takeoff(10)
time.sleep(1)
vehicle.mode = VehicleMode("LAND")

vehicle.remove_message_listener();

The settings file:


{
  "SettingsVersion": 1.2,
  "SimMode": "Multirotor",
  "OriginGeopoint": {
    "Latitude": -35.363261,
    "Longitude": 149.165230,
    "Altitude": 583
  },
  "Vehicles": {
    "Copter": {
      "VehicleType": "ArduCopter",
      "UseSerial": false,
      "LocalHostIp": "127.0.0.1",
      "UdpIp": "127.0.0.1",
      "UdpPort": 9003,
      "ControlPort": 9002,
      "AutoCreate": true,
      "Sensors": {
        "Imu": {
          "SensorType": 2,
          "Enabled": true
        },
        "Gps": {
          "SensorType": 3,
          "Enabled": true
        },
        "Lidar1": {
          "SensorType": 6,
          "Enabled": true,
          "NumberOfChannels": 1,
          "PointsPerSecond": 5000,
          "DrawDebugPoints": false,
          "RotationsPerSecond": 10,
          "VerticalFOVUpper": 0,
          "VerticalFOVLower": 0,
          "HorizontalFOVStart": 0,
          "HorizontalFOVEnd": 0,
          "DataFrame": "SensorLocalFrame"
        },
        "Distance": {
          "SensorType": 5,
          "Enabled": true
        }
      }
    }
  }
}

The command I use to launch ardupilot:

sim_vehicle.py -v ArduCopter -f airsim-copter --add-param-file=libraries/SITL/examples/Airsim/lidar.parm  --console --map
cepoya commented 3 years ago

how to connect for real equipment?