Skip to content

Commit

Permalink
clean demo
Browse files Browse the repository at this point in the history
  • Loading branch information
tcdanielh committed Apr 13, 2024
1 parent d1e5b9a commit 344bee1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 108 deletions.
31 changes: 2 additions & 29 deletions examples/airsim/demoDrone.scenic
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class AdversaryDrone(Drone):
# Find the adversary. drone1 is the adversary target
# https://scenic-lang.readthedocs.io/en/latest/reference/visibility.html
behavior FindAdversary(speed = 5):
# Gets the airsim client to call airsim's apis
client = simulation().client

# Randomly select point/region to look at given the weights
# https://scenic-lang.readthedocs.io/en/latest/reference/distributions.html
# Discrete({value: weight, … })
Expand All @@ -58,47 +55,23 @@ behavior FindAdversary(speed = 5):
# https://scenic-lang.readthedocs.io/en/latest/reference/statements.html#try-interrupt-statement
try:
# if doing online path solution then do api call for that here, and remove the patrolPoints/patrolPointsProb
do FlyToPosition(selectedPoint, speed) for 10 seconds
do MoveToPosition(selectedPoint, speed) for 10 seconds
# resample point since didn't find adversary at that position
selectedPoint = Discrete({drone1.patrolPoints[0]:drone1.patrolPointsProb[0],
drone1.patrolPoints[1]:drone1.patrolPointsProb[1],
drone1.patrolPoints[2]:drone1.patrolPointsProb[2],
drone1.patrolPoints[3]:drone1.patrolPointsProb[3]})
interrupt when self can see drone1:
# when I can see adversary, follow it
do Follow(drone1)
# interrupt when distance from self to drone1 < 10:
# # when I can see adversary (i'm within 10 meters of it), follow it
# do Follow(drone1)
do MoveToPosition(drone1.position, speed)
interrupt when distance from self to drone1 < 2:
# when I get within 2 meters of adversary, terminate scenario
terminate

behavior Follow(target, speed = 5,tolerance = 2, offset = (0,0,1)):
client = simulation().client

while True:
targetPosition = target.position + offset

velocity = targetPosition-self.position
distance = magnitude(velocity)
velocity = (velocity / distance) * speed
if distance > tolerance:
take SetVelocity(velocity)
wait
# Note: SetVelocity is an action in actions.py. Actions are 1 time events that a scenic object takes
# Behaviors are continuous events that a scenic object takes, that change depending on conditions met
# Generic behaviors can be found in behaviors.scenic

# Adversary behavior. Continuously patrol to each of these positions.
behavior Adversary(speed):
client = simulation().client

do Patrol(self.patrolPoints, loop=True, speed=speed)




ego = new Drone at (Range(-10,10),Range(-10,10),Range(0,10)),
with behavior FindAdversary(),
with viewAngles (180 deg, 180 deg), # https://scenic-lang.readthedocs.io/en/latest/reference/visibility.html
Expand Down
2 changes: 2 additions & 0 deletions src/scenic/simulators/airsim/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def applyTo(self, obj, sim): # The inputs for the applyTo() function is FIXED. A

class SetVelocity(Action):
def __init__(self, velocity):
# velocity = (vx, vy, vz) where vx := speed in x axis
# Note that we need use the following helper function "scenicToAirsimVector" to convert Scenic's position to AirSim's position
self.newVelocity = scenicToAirsimVector(toVector(velocity))

def applyTo(self, obj, sim):
Expand Down
158 changes: 79 additions & 79 deletions src/scenic/simulators/airsim/behaviors.scenic
Original file line number Diff line number Diff line change
Expand Up @@ -16,107 +16,107 @@ from .utils import (
)
from scenic.simulators.airsim.actions import *

behavior MoveToPosition(position, speed):
while ((distance from position to self.position) > 1):
take MoveToPositionAsync(position, speed)

def magnitude(v):
return math.hypot(v.x, v.y, v.z)
behavior Patrol(positions, loop=True, speed = 5):
while True:
for pos in positions:
do MoveToPosition(pos,speed=speed)

if not loop:
return

# creates a promise from an msgpackrpc future (the futures that are used in airsim)
def createPromise(future):
def promFunc(resolve, reject):

def joinAsync():
future.join()
resolve(True)

def waitAsync():
while not future._set_flag:
time.sleep(.01)
resolve(True)
behavior SetVelocityBehavior(velocity):
# velocity = (vx, vy, vz) where vx := speed in x axis
while True:
take SetVelocity(velocity)

if not future._loop:
threading.Thread(target=joinAsync).start()
else:
threading.Thread(target=waitAsync).start()

prom = Promise(promFunc)
# def magnitude(v):
# return math.hypot(v.x, v.y, v.z)

return prom
# # creates a promise from an msgpackrpc future (the futures that are used in airsim)
# def createPromise(future):
# def promFunc(resolve, reject):

# def joinAsync():
# future.join()
# resolve(True)

# def waitAsync():
# while not future._set_flag:
# time.sleep(.01)
# resolve(True)

# waits for a promise to be fulfilled
behavior waitForPromise(promise):
while not promise.is_fulfilled:
wait
# if not future._loop:
# threading.Thread(target=joinAsync).start()
# else:
# threading.Thread(target=waitAsync).start()

behavior MoveToPosition(position, speed):
while ((distance from position to self.position) > 1):
take MoveToPositionAsync(position, speed)
# prom = Promise(promFunc)

behavior SetVelocityBehavior(direction):
while True:
take SetVelocity(direction)
# return prom

# # waits for a promise to be fulfilled
# behavior waitForPromise(promise):
# while not promise.is_fulfilled:
# wait

# Flies the drone to a position
behavior FlyToPosition(newPos, speed = 5,tolerance = 1,pidMode = True):
# pidMode is true if we want the drone to slow down as it reaches its destination
# # Flies the drone to a position
# behavior FlyToPosition(newPos, speed = 5,tolerance = 1,pidMode = True):
# # pidMode is true if we want the drone to slow down as it reaches its destination

client = simulation().client

if pidMode:
newPos = scenicToAirsimVector(toVector(newPos))
do waitForPromise(createPromise(
client.moveToPositionAsync(
newPos.x_val,
newPos.y_val,
newPos.z_val,
velocity=speed,
vehicle_name=self.realObjName,
)
))
else:
while True:
direction = newPos -self.position
distance = magnitude(direction)
# client = simulation().client

# if pidMode:
# newPos = scenicToAirsimVector(toVector(newPos))
# do waitForPromise(createPromise(
# client.moveToPositionAsync(
# newPos.x_val,
# newPos.y_val,
# newPos.z_val,
# velocity=speed,
# vehicle_name=self.realObjName,
# )
# ))
# else:
# while True:
# direction = newPos -self.position
# distance = magnitude(direction)

if distance < tolerance:
break
direction= (direction/distance)*speed
take SetVelocity(direction)
wait
return

# if distance < tolerance:
# break
# direction= (direction/distance)*speed
# take SetVelocity(direction)
# wait
# return



behavior Patrol(positions, loop=True, smooth = False, speed = 5,tolerance = 2):
while True:
for pos in positions:
do FlyToPosition(pos,speed=speed,pidMode= not smooth,tolerance=tolerance)

if not loop:
return




behavior MoveByVelocity(velocity,seconds):
client = simulation().client
# behavior MoveByVelocity(velocity,seconds):
# client = simulation().client

newVelocity = scenicToAirsimVector(toVector(velocity))
# newVelocity = scenicToAirsimVector(toVector(velocity))

do waitForPromise(createPromise(
client.moveByVelocityAsync(
newVelocity.x_val,
newVelocity.y_val,
newVelocity.z_val,
duration=seconds,
vehicle_name=self.realObjName,
)
))
# do waitForPromise(createPromise(
# client.moveByVelocityAsync(
# newVelocity.x_val,
# newVelocity.y_val,
# newVelocity.z_val,
# duration=seconds,
# vehicle_name=self.realObjName,
# )
# ))




behavior FlyToStart():
do FlyToPosition(self._startPos)
# behavior FlyToStart():
# do FlyToPosition(self._startPos)


0 comments on commit 344bee1

Please sign in to comment.