dji-sdk / Tello-Python

This is a collection of python modules that interact with the Ryze Tello drone.
Other
1.35k stars 645 forks source link

AirSim Support with unity or unreal engine python control #32

Open adamdarko opened 5 years ago

adamdarko commented 5 years ago

so I am having trouble with using airsim with tellopy. I am am getting an error Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/fahre/Desktop/dronecontrol.py', wdir='C:/Users/fahre/Desktop')

File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 95, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/fahre/Desktop/dronecontrol.py", line 73, in tellodrone = tellopy.Tello()

File "C:\ProgramData\Anaconda2\lib\site-packages\tellopy_internal\tello.py", line 91, in init self.sock.bind(('', self.port))

File "C:\ProgramData\Anaconda2\lib\socket.py", line 228, in meth return getattr(self._sock,name)(*args)

error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

adamdarko commented 5 years ago

`#import setup_path

import airsim from time import sleep import tellopy

import numpy as np

import os

import tempfile

import pprint

import cv2

def handler(event, sender, data, **args): tellodrone = sender if event is tellodrone.EVENT_FLIGHT_DATA: print(data)

connect to the AirSim simulator

client = airsim.MultirotorClient()

client.confirmConnection()

client.enableApiControl(True)

client.armDisarm(True)

state = client.getMultirotorState()

s = pprint.pformat(state)

print("state: %s" % s)

imu_data = client.getImuData()

s = pprint.pformat(imu_data)

print("imu_data: %s" % s)

barometer_data = client.getBarometerData()

s = pprint.pformat(barometer_data)

print("barometer_data: %s" % s)

magnetometer_data = client.getMagnetometerData()

s = pprint.pformat(magnetometer_data)

print("magnetometer_data: %s" % s)

gps_data = client.getGpsData()

s = pprint.pformat(gps_data)

print("gps_data: %s" % s)

tellodrone = tellopy.Tello() tellodrone.subscribe(tellodrone.EVENT_FLIGHT_DATA, handler) tellodrone.connect() tellodrone.wait_for_connection(60.0)

airsim.wait_key('Press any key to takeoff')

client.takeoffAsync().join() tellodrone.takeoff()

state = client.getMultirotorState()

print("state: %s" % pprint.pformat(state))

airsim.wait_key('Press any key to move vehicle to (-10, 10, -10) at 5 m/s')

negative z is positive real world z

client.moveToPositionAsync(10, 0, -10, 5).join() tellodrone.forward(10) tellodrone.up(5) sleep(20)

client.hoverAsync().join()

state = client.getMultirotorState()

print("state: %s" % pprint.pformat(state))

airsim.wait_key('Press any key to take images')

get camera images from the car

responses = client.simGetImages([

airsim.ImageRequest("0", airsim.ImageType.DepthVis),  #depth visualization image

airsim.ImageRequest("1", airsim.ImageType.DepthPerspective, True), #depth in perspective projection

airsim.ImageRequest("1", airsim.ImageType.Scene), #scene vision image in png format

airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)])  #scene vision image in uncompressed RGBA array

print('Retrieved images: %d' % len(responses))

tmp_dir = os.path.join(tempfile.gettempdir(), "airsim_drone")

print ("Saving images to %s" % tmp_dir)

try:

os.makedirs(tmp_dir)

except OSError:

if not os.path.isdir(tmp_dir):

    raise

for idx, response in enumerate(responses):

filename = os.path.join(tmp_dir, str(idx))

if response.pixels_as_float:

    print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))

    airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))

elif response.compress: #png format

    print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))

    airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)

else: #uncompressed array

    print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))

    img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) # get numpy array

    img_rgb = img1d.reshape(response.height, response.width, 3) # reshape array to 4 channel image array H X W X 3

    cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

airsim.wait_key('Press any key to reset to original state')

client.armDisarm(False) tellodrone.land() sleep(10) client.reset() drone.quit()

that's enough fun for now. let's quit cleanly

client.enableApiControl(False)`