Last active
November 15, 2019 00:45
-
-
Save madratman/e617b53ec20c5f38a7d10633ba3a42c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import airsimneurips | |
import threading | |
import time | |
import random | |
class ReproduceResetRaceCondition(): | |
def __init__(self, drone_name = "drone_1"): | |
self.airsim_client = airsimneurips.MultirotorClient() | |
self.airsim_client_2 = airsimneurips.MultirotorClient() | |
self.airsim_client_3 = airsimneurips.MultirotorClient() | |
self.drone_name = drone_name | |
self.is_thread_active = False | |
self.min_episode_duration = 4.0 | |
self.max_episode_duration = 10.0 | |
self.thread_reset_episode = threading.Thread(target=self.repeat_timer, args=(self.reset_episode, self.min_episode_duration, self.max_episode_duration)) | |
self.is_thread_active = False | |
def repeat_timer(self, callback, period_min, period_max): | |
while self.is_thread_active: | |
callback() | |
time.sleep(random.uniform(period_min, period_max)) | |
def load_level(self, level_name, sleep_sec = 2.0): | |
self.level_name = level_name | |
self.airsim_client.simLoadLevel(self.level_name) | |
self.airsim_client.confirmConnection() # failsafe | |
time.sleep(sleep_sec) # let the environment load completely | |
def start_race(self, tier): | |
print(time.time(), 'called start race') | |
self.airsim_client.simStartRace(tier) | |
def reset_episode(self): | |
print(time.time(), 'called reset, followed by simResetRace') | |
self.airsim_client.reset() | |
time.sleep(0.1) | |
self.airsim_client.simResetRace() | |
time.sleep(0.1) | |
self.airsim_client.enableApiControl(vehicle_name=self.drone_name) | |
self.airsim_client.arm(vehicle_name=self.drone_name) | |
self.airsim_client.simStartRace(tier=1) | |
def initialize_drone(self): | |
self.airsim_client.enableApiControl(vehicle_name=self.drone_name) | |
self.airsim_client.arm(vehicle_name=self.drone_name) | |
# set default values for trajectory tracker gains | |
traj_tracker_gains = airsimneurips.TrajectoryTrackerGains(kp_cross_track = 5.0, kd_cross_track = 0.0, | |
kp_vel_cross_track = 3.0, kd_vel_cross_track = 0.0, | |
kp_along_track = 0.4, kd_along_track = 0.0, | |
kp_vel_along_track = 0.04, kd_vel_along_track = 0.0, | |
kp_z_track = 2.0, kd_z_track = 0.0, | |
kp_vel_z = 0.4, kd_vel_z = 0.0, | |
kp_yaw = 3.0, kd_yaw = 0.1) | |
self.airsim_client.setTrajectoryTrackerGains(traj_tracker_gains, vehicle_name=self.drone_name) | |
time.sleep(0.2) | |
def start_threads(self): | |
if not self.is_thread_active: | |
self.is_thread_active = True | |
self.thread_reset_episode.start() | |
print("Started threads") | |
def stop_threads(self): | |
if self.is_thread_active: | |
self.is_thread_active = False | |
self.thread_reset_episode.start() | |
print("Stopped threads.") | |
if __name__ == "__main__": | |
reproducer = ReproduceResetRaceCondition('drone_1') | |
reproducer.load_level('ZhangJiaJie_Medium') | |
reproducer.initialize_drone() | |
reproducer.start_race(3) | |
time.sleep(5) | |
reproducer.start_threads() | |
time.sleep(3600) | |
reproducer.stop_threads() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment