in resources/drone_orbit.py [0:0]
def start(self):
print("arming the drone...")
self.client.armDisarm(True)
# AirSim uses NED coordinates so negative axis is up.
start = self.client.getMultirotorState().kinematics_estimated.position
landed = self.client.getMultirotorState().landed_state
if not self.takeoff and landed == airsim.LandedState.Landed:
self.takeoff = True
print("taking off...")
self.client.takeoffAsync().join()
start = self.client.getMultirotorState().kinematics_estimated.position
z = -self.altitude + self.home.z_val
else:
print("already flying so we will orbit at current altitude {}".format(
start.z_val))
z = start.z_val # use current altitude then
print("climbing to position: {},{},{}".format(
start.x_val, start.y_val, z))
self.client.moveToPositionAsync(
start.x_val, start.y_val, z, self.speed).join()
self.z = z
print("ramping up to speed...")
count = 0
self.start_angle = None
self.next_snapshot = None
# ramp up time
ramptime = self.radius / 10
self.start_time = time.time()
while count < self.iterations and self.snapshot_index < self.snapshots:
# ramp up to full speed in smooth increments so we don't start too aggressively.
now = time.time()
speed = self.speed
diff = now - self.start_time
if diff < ramptime:
speed = self.speed * diff / ramptime
elif ramptime > 0:
print("reached full speed...")
ramptime = 0
lookahead_angle = speed / self.radius
# compute current angle
pos = self.client.getMultirotorState().kinematics_estimated.position
dx = pos.x_val - self.center.x_val
dy = pos.y_val - self.center.y_val
actual_radius = math.sqrt((dx*dx) + (dy*dy))
angle_to_center = math.atan2(dy, dx)
camera_heading = (angle_to_center - math.pi) * 180 / math.pi
# compute lookahead
lookahead_x = self.center.x_val + self.radius * \
math.cos(angle_to_center + lookahead_angle)
lookahead_y = self.center.y_val + self.radius * \
math.sin(angle_to_center + lookahead_angle)
vx = lookahead_x - pos.x_val
vy = lookahead_y - pos.y_val
if self.track_orbits(angle_to_center * 180 / math.pi):
count += 1
print("completed {} orbits".format(count))
self.camera_heading = camera_heading
self.client.moveByVelocityZAsync(
vx, vy, z, 1, airsim.DrivetrainType.MaxDegreeOfFreedom, airsim.YawMode(False, camera_heading))
self.client.moveToPositionAsync(start.x_val, start.y_val, z, 2).join()