Skip to content

Commit

Permalink
Preliminary work to pause/resume missions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaldwin committed Jan 27, 2020
1 parent ac23196 commit df69072
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 6 deletions.
12 changes: 11 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def launch_mission():
mission.parse_mission(mission_code)
return ""

@app.route('/pause_mission')
def pause_mission():
mission.pause_mission()
return ""

@app.route('/resume_mission')
def resume_mission():
mission.resume_mission()
return ""

# So that we can load DroneBlocks in an iframe
static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'droneblocks/dist')
@app.route('/droneblocks/<path:path>', methods=['GET'])
Expand All @@ -89,7 +99,7 @@ def droneblocks(path):
udp.start_listening()

# Create the mission handler
mission = Mission(udp, camera)
mission = Mission(udp, camera, drone)

# Handle Tello's state information
telemetry = Telemetry(drone)
Expand Down
1 change: 1 addition & 0 deletions lib/drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, is_aruco_tracking_enabled=False):
self.is_aruco_tracking_enabled = is_aruco_tracking_enabled
self.is_recording_video = False
self.is_running_droneblocks_mission = False
self.is_paused = False
self.pitch = None
self.roll = None
self.yaw = None
Expand Down
50 changes: 46 additions & 4 deletions lib/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

class Mission():

def __init__(self, udp, camera):
def __init__(self, udp, camera, drone):
self.udp = udp
self.camera = camera
self.drone = drone
self.test_command_string = "takeoff|fly_forward,20,in|yaw_right,90|land"
self.current_command_index = 0
self.is_mission_paused = False
self.resumed_mission = ""# This will contain the commands to resume the mission


def parse_mission(self, mission_code):

# Clear out any previous commands
self.udp.clear_response()

# Reset the paused mission varables
self.current_command_index = 0
self.is_mission_paused = False
self.resumed_mission = ""

# Split the mission into a command list
commands = mission_code.split("|")

# Used so we can update pause/resume UI button
self.drone.is_running_droneblocks_mission = True
self.drone.is_paused = False

# Remove any empty strings from the list. This can happen in cases where there is a mission without a takeoff block
while ("" in commands):
commands.remove("")
Expand Down Expand Up @@ -79,8 +92,9 @@ def parse_mission(self, mission_code):

delay = command.split(",")[1]

print("Delaying for ", delay, " seconds")
print("Hovering for ", delay, " seconds")
sleep(int(delay))
print("Done hovering")
command_to_execute = "command"

elif "yaw_right" in command:
Expand Down Expand Up @@ -136,14 +150,42 @@ def parse_mission(self, mission_code):
response = self.udp.get_response()

if response == "ok":
print("finished executing command: " + command_to_execute + " with response: " + response)
print("Finished executing command: " + command_to_execute + " with response: " + response)

# Increment the command counter (used for pausing and resuming missions)
self.current_command_index = self.current_command_index + 1

# Delay 1 second before issuing the next command
sleep(1)

# If mission is paused let's break out of the command loop
if self.is_mission_paused:

# Used to update the status so we can change the UI button to "Resume"
self.drone.is_paused = True

# Store the resumed mission in the string format so we can resume later
self.resumed_mission = "|".join(commands[self.current_command_index:])
print("Mission is paused. We'll resume mission with: " + self.resumed_mission)
break

else:
print("Here we could implement some retry logic or maybe even land. Response was " + response)

# Let's not trigger this code in cases where the mission is paused
if self.is_mission_paused is False:
print("Mission is complete!")
self.drone.is_running_droneblocks_mission = False

# Allow the user to pause the mission
# The mission will complete the current command being executed and then pause
def pause_mission(self):
self.is_mission_paused = True

# Pick up where we left off
def resume_mission(self):
self.parse_mission(self.resumed_mission)

print("Mission is complete")


# Tello only flies in units of cm so we need to convert from in to cm
Expand Down
1 change: 1 addition & 0 deletions lib/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def recv():
try:
self.tello_response, _ = self.sock.recvfrom(128)
self.tello_response = self.tello_response.decode(encoding="utf-8")
print("udp receive got a response: " + self.tello_response)
except Exception as e:
self.tello_response = None
print("Error receiving: " + str(e))
Expand Down
7 changes: 7 additions & 0 deletions static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@

.border {
border: 1px solid #ff0000;
}

#pause {
position: absolute;
bottom: 50px;
left: 50%;
visibility: hidden;
}
26 changes: 26 additions & 0 deletions static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ $(document).ready(function() {
$("#roll").text(response.roll + "°");
$("#pitch").text(response.pitch + "°");
$("#yaw").text(response.yaw + "°");

// Display pause button
if (response.is_running_droneblocks_mission) {

if (response.is_paused) {
$("#pause").html("Resume Mission");
} else {
$("#pause").html("Pause Mission");
$("#pause").css("visibility", "visible");
}

} else {
$("#pause").css("visibility", "hidden");
}
}
});
}, 500);
Expand All @@ -173,6 +187,18 @@ $(document).ready(function() {
}
});

// Handle pausing mission
$("#pause").click(function() {
let button_text = $(this).text();

if (button_text == "Pause Mission") {
$.get("/pause_mission", function(data){});
} else {
$.get("/resume_mission", function(data){});
}

});

});

// Called when a control command is being issued
Expand Down
3 changes: 2 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@

</div>

<div class="uk-width-3-5">
<div class="uk-width-3-5" style="position: relative">
<iframe src="/droneblocks/tello.html" width="100%" height="100%" id="droneblocks_iframe"></iframe>
<button class="uk-button uk-button-danger uk-button-small" type="button" id="pause">Pause Mission</button>
</div>
</div>
<script src="../static/js/app.js"></script>
Expand Down

0 comments on commit df69072

Please sign in to comment.