implement emergency stop & better exception handling

This commit is contained in:
Alihan
2023-05-18 19:31:09 +03:00
parent 6dd62d6c9b
commit 6116fa37e4
7 changed files with 89 additions and 48 deletions

View File

@@ -42,6 +42,10 @@ class CommandHandler:
def _end_session(self):
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/end")
print(response.json())
def _emergency(self):
response = requests.get(f"{self.COMMANDER_COMMANDS_URL}/emergency")
print(response.json())
def handle(self, cmd: dict):
#print(f"commanding for: {cmd}")
@@ -59,5 +63,7 @@ class CommandHandler:
self._takeoff()
elif cmd["command"] == "land":
self._land()
elif cmd["command"] == "emergency":
self._emergency()
else:
raise ValueError(f"Uknown command object: {cmd}")

View File

@@ -45,14 +45,17 @@ def takeoff():
try:
tello.connect()
except Exception as e:
return {"msg": "failed to connect"}
return {"msg": "command failed", "reason": "failed to connect"}
if tello.is_flying:
return {"msg": "already flying"}
tello.takeoff()
stats_collector.start_collecting()
try:
tello.takeoff()
stats_collector.start_collecting()
except Exception as e:
return { "msg": "command failed", "reason": e }
if tello.is_flying:
return {"msg": "ok"}
else:
@@ -63,8 +66,11 @@ def land():
if not tello.is_flying:
return {"msg": "already on land"}
tello.land()
stats_collector.stop_collecting()
try:
tello.land()
stats_collector.stop_collecting()
except Exception as e:
return { "msg": "command failed", "reason": e }
if not tello.is_flying:
return {"msg": "ok"}
@@ -76,10 +82,13 @@ def turn(direction: str, degree: int):
if degree < 1 or degree > 360:
return {"degree must be between 1 and 360"}
if direction == "right":
tello.rotate_clockwise(degree)
elif direction == "left":
tello.rotate_counter_clockwise(degree)
try:
if direction == "right":
tello.rotate_clockwise(degree)
elif direction == "left":
tello.rotate_counter_clockwise(degree)
except Exception as e:
return { "msg": "command failed", "reason": e }
return {"msg": "ok"}
@@ -93,14 +102,22 @@ def turn(direction: str, distance: int):
try:
tello.move(direction, distance)
except Exception as e:
return {
"msg": "command failed",
"reason": e
}
return { "msg": "command failed", "reason": e }
return {"msg": "ok"}
@router.get("/emergency")
def emercengy_stop():
try:
tello.emergency()
return {"msg": "ok"}
except Exception as e:
return { "msg": "command failed", "reason": e }
@router.get("/end")
def end_flight_session():
tello.end()
return {"msg": "ok"}
try:
tello.end()
return {"msg": "ok"}
except Exception as e:
return { "msg": "command failed", "reason": e }