multithreading in airsim using the same drone
Hi,
So when I send commands to the drone I want to know it's position as it is moving between two points. I could not find a way not using multithreading to do this (if there is please let me know).
So using 1 drone the first thread just gives it commands and measures the points when completing a movement between two points and the other thread measures the points between the movements (for the time being it will only measure one point cause even that is not working)
here is the code: import setup_path import airsim
import numpy as np import os import tempfile import pprint import cv2 import math import time import threading
class Position: def init(self, pos): self.x = pos.x_val self.y = pos.y_val self.z = pos.z_val
class Position2: def init(self, pos2): self.x = pos2.x_val self.y = pos2.y_val self.z = pos2.z_val
pxtol = [] pytol = [] pztol = []
px2tol = [] py2tol = [] pz2tol = []
pivals = np.arange(0,2.01,0.01) client = airsim.MultirotorClient() client.confirmConnection() client.enableApiControl(True) client.armDisarm(True) client.takeoffAsync().join() client.moveToPositionAsync(-10, 10, -10, 5).join()
def tracking(): var = 0 pos2 = client.getMultirotorState().kinematics_estimated.position px2tol.append(pos2.x_val) py2tol.append(pos2.y_val) pz2tol.append(pos2.z_val) print ("pos2x",pos2.x_val) print ("pos2y",pos2.y_val) print ("pos2z",pos2.z_val) #print(var)
def moving(): Position = client.getMultirotorState().kinematics_estimated.position print(Position) pos = client.getMultirotorState().kinematics_estimated.position print(pos.z_val) client.moveToPositionAsync(0, 0, -20, 5).join() Position = client.getMultirotorState().kinematics_estimated.position print(Position)
for i in pivals:
client.moveToPositionAsync(10*math.sin(math.pi*i),10*math.cos(math.pi*i),-20, 5).join()
pos = client.getMultirotorState().kinematics_estimated.position
pxtol.append(pos.x_val)
pytol.append(pos.y_val)
pztol.append(pos.z_val)
print(pos)
time.sleep(2)
print("moved")
client.hoverAsync().join()
print("hover")
Position = client.getMultirotorState().kinematics_estimated.position
print(Position)
client.enableApiControl(False)
t1 = threading.Thread(target=moving) t2 = threading.Thread(target=tracking)
t1.start() t2.start()
print("pxtol") print(pxtol) print("pytol") print(pytol) print("pztol") print(pztol)
but I keep getting this error:
Connected! Client Ver:1 (Min Req: 1), Server Ver:1 (Min Req: 1)
pxtol
[]
pytol
[]
pztol
[]
I tried only running the second thread and in that case I get this:
WARNING:tornado.general:Connect error on fd 8: ECONNREFUSED
WARNING:tornado.general:Connect error on fd 8: ECONNREFUSED
WARNING:tornado.general:Connect error on fd 8: ECONNREFUSED
WARNING:tornado.general:Connect error on fd 8: ECONNREFUSED
WARNING:tornado.general:Connect error on fd 8: ECONNREFUSED
Traceback (most recent call last):
File "Edwin3.py", line 36, in
So can I not measure the data point of the same drone in two different threads at the same time?
Have you tried using separate clients for each thread?
I did not know I could do that. How would I do that?
Think just passing the clients as args to the functions might work
cl1 = airsim.VehicleClient()
cl2 = airsim.VehicleClient()
threading...args=(cl1))
threading...args=(cl2))
Thanks! That solved the issue!
Hi, if I want use Airsim's client in the main and image_process threads at the same time, it will be a problem cause the client is occupied, how should I solve this problem to speed up my program. Thank you!
Hi, if I want use Airsim's client in the main and image_process threads at the same time, it will be a problem cause the client is occupied, how should I solve this problem to speed up my program. Thank you!
Hi, @paobuliao , have you solved it? I met the same problem, thanks for your help!
I run a thread to control the uav fly, and the other thread to get image from the camera. The client is occupied.
Hi, if I want use Airsim's client in the main and image_process threads at the same time, it will be a problem cause the client is occupied, how should I solve this problem to speed up my program. Thank you!
Hi, @paobuliao , have you solved it? I met the same problem, thanks for your help!
I run a thread to control the uav fly, and the other thread to get image from the camera. The client is occupied.
Sorry, I haven't solved it. For a uav, it can only be accessed by a thread, so I transfer image by ros to another ros node for processing.
@paobuliao How fast you reply! Thanks!
Luckily, I fixed it a few minutes ago, and I made a simple test. It works! The two thread could run individually, take photo while flying to a position. Here is the code. Hope it is useful for you.
import airsim
import threading
global_camera = "GlobalCamera"
client.enableApiControl(True, global_camera)
client.takeoffAsync(vehicle_name=global_camera).join()
client.moveToPositionAsync(0, 0, -40, 10, vehicle_name=global_camera).join()
airsim.wait_key()
def fly():
client1 = airsim.MultirotorClient()
res = client1.moveToPositionAsync(20,20,20,2,vehicle_name="GlobalCamera")
res.join()
def photo():
client2 = airsim.MultirotorClient()
response = client2.simGetImage("bottom_center", airsim.ImageType.Scene, vehicle_name="GlobalCamera")
import os
current_file_path = os.path.abspath(__file__)
current_directory = os.path.dirname(current_file_path)
import datetime
time_str = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
vehicle_directory = os.path.join(current_directory, "data", "GlobalCamera")
filename = os.path.join(vehicle_directory, f"{time_str}.png")
if not os.path.exists(vehicle_directory):
os.makedirs(vehicle_directory)
with open(filename, "wb") as f:
f.write(response)
threads = []
threads.append(threading.Thread(target=fly, ))
threads.append(threading.Thread(target=photo, ))
# start all threads
for thread in threads:
thread.start()
# wait for all threads finish
for thread in threads:
thread.join()