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

@@ -6,7 +6,8 @@ brain = CloudChatBrain()
while True: while True:
try: try:
brain.listen() brain.listen()
brain.understand() if not brain.is_emergency(brain.cmd_prompt):
brain.command() brain.understand()
brain.command()
except Exception as e: except Exception as e:
print(f"##### SOMETHING WENT WRONG ###### \n {e}") print(f"##### SOMETHING WENT WRONG ###### \n {e}")

32
brain/brain_base.py Normal file
View File

@@ -0,0 +1,32 @@
import os
from commander.commands import CommandHandler
class BaseBrain:
def __init__(self):
self.command_handler = CommandHandler()
@property
def sys_prompt(self):
return self._read_prompt()
def _read_prompt(self):
prompt_file_name = "prompt.txt"
for root, dirs, files in os.walk("./"):
if prompt_file_name in files:
prompt_filepath = os.path.join(root, prompt_file_name)
with open(prompt_filepath, "r") as f:
return f.read()
def is_emergency(self, input):
if input == "q":
print("##### BASE BRAIN: EMERGENCY STOP DETECTED!!! #####")
self.command_handler.handle({"command": "emergency"})
return True
else:
return False
def listen(self):
self.cmd_prompt = input("\n\nwhat should I do now?\n(enter q for emergency)\n\t")

View File

@@ -6,7 +6,7 @@ import ast
import openai import openai
from commander.commands import CommandHandler from brain_base import BaseBrain
from settings.config import settings from settings.config import settings
@@ -16,22 +16,11 @@ class CloudSTTBrain:
print("not implemented") print("not implemented")
class CloudChatBrain: class CloudChatBrain(BaseBrain):
def __init__(self): def __init__(self):
super().__init__()
openai.api_key = settings.OPENAI_API_KEY openai.api_key = settings.OPENAI_API_KEY
self.command_handler = CommandHandler()
@property
def sys_prompt(self):
return self._read_prompt()
def _read_prompt(self):
prompt_file_name = "prompt.txt"
for root, dirs, files in os.walk("./"):
if prompt_file_name in files:
prompt_filepath = os.path.join(root, prompt_file_name)
with open(prompt_filepath, "r") as f:
return f.read()
def _is_valid_json(self, answer): def _is_valid_json(self, answer):
try: try:
@@ -45,9 +34,6 @@ class CloudChatBrain:
self.cmd_prompt = None self.cmd_prompt = None
self.response = None self.response = None
def listen(self):
self.cmd_prompt = input("\n\nwhat should I do now?\n\t")
def understand(self): def understand(self):
self.response = openai.ChatCompletion.create( self.response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",

View File

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

View File

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

View File

@@ -9,7 +9,7 @@ DRONE_INTERFACE=$(yq '.commander.drone_interface' < settings/admin.yml)
DRONE_WPA_SUPP_CONF=$(yq '.commander.drone_wpa_supp' < settings/admin.yml) DRONE_WPA_SUPP_CONF=$(yq '.commander.drone_wpa_supp' < settings/admin.yml)
NET_INTERFACE=$(yq '.commander.net_interface' < settings/admin.yml) NET_INTERFACE=$(yq '.commander.net_interface' < settings/admin.yml)
NET_WPA_SUPP_CONF=$(yq '.commander.net_wpa_supp' < settings/admin.yml) NET_WPA_SUPP_CONF=$(yq '.commander.net_wpa_supp' < settings/admin.yml)
ENV_FOR_DYNACONF=$(yq '.commander.env_for_dynaconf' < settings/admin.yml) #ENV_FOR_DYNACONF=$(yq '.commander.env_for_dynaconf' < settings/admin.yml)
pids_dir='./pids' pids_dir='./pids'
if [[ ! -d "$pids_dir" ]]; then if [[ ! -d "$pids_dir" ]]; then
@@ -70,11 +70,11 @@ start_commander_service() {
} }
talk_to_drone() { talk_to_drone() {
ENV_FOR_DYNACONF=$ENV_FOR_DYNACONF python brain/brain.py ENV_FOR_DYNACONF=$1 python brain/brain.py
} }
kill_everything() { kill_everything() {
for p in $pids_dir/*.txt; do echo "killing $p"; sudo pkill $(cat "$p"); done for p in $pids_dir/*.txt; do echo "killing $p"; sudo pkill -15 $(cat "$p"); done
} }
remote_shutdown() { remote_shutdown() {
@@ -102,7 +102,7 @@ elif [ "$1" == "connect-drone" ]; then
elif [ "$1" == "disconnect-drone" ]; then elif [ "$1" == "disconnect-drone" ]; then
wpa_supp_pid_file="$pids_dir/wpa_supp_pid.txt" wpa_supp_pid_file="$pids_dir/wpa_supp_pid.txt"
if [ -f "$wpa_supp_pid_file" ]; then if [ -f "$wpa_supp_pid_file" ]; then
sudo pkill -P $(cat $wpa_supp_pid_file) sudo pkill -15 -P $(cat $wpa_supp_pid_file)
echo "stopped drone connection via wpa_supp" echo "stopped drone connection via wpa_supp"
fi fi
@@ -115,7 +115,7 @@ elif [ "$1" == "get-dhcp" ]; then
elif [ "$1" == "kill-dhcp" ]; then elif [ "$1" == "kill-dhcp" ]; then
dhcp_ip_pid_file="$pids_dir/dhcp_ip_pid.txt" dhcp_ip_pid_file="$pids_dir/dhcp_ip_pid.txt"
if [ -f "$dhcp_ip_pid_file" ]; then if [ -f "$dhcp_ip_pid_file" ]; then
sudo pkill -P $(cat $dhcp_ip_pid_file) sudo pkill -15 -P $(cat $dhcp_ip_pid_file)
echo "killed dhcp client" echo "killed dhcp client"
fi fi
@@ -132,7 +132,7 @@ elif [ "$1" == "start-jupyter" ]; then
elif [ "$1" == "stop-jupyter" ]; then elif [ "$1" == "stop-jupyter" ]; then
jupyter_pid_file="$pids_dir/jupyter_pid.txt" jupyter_pid_file="$pids_dir/jupyter_pid.txt"
if [ -f "$jupyter_pid_file" ]; then if [ -f "$jupyter_pid_file" ]; then
pkill -P $(cat $jupyter_pid_file) pkill -15 -P $(cat $jupyter_pid_file)
echo "stopped jupyter" echo "stopped jupyter"
fi fi
@@ -146,7 +146,8 @@ elif [ "$1" == "start-cs" ]; then
elif [ "$1" == "stop-cs" ]; then elif [ "$1" == "stop-cs" ]; then
codeserver_pid_file="$pids_dir/codeserver_pid.txt" codeserver_pid_file="$pids_dir/codeserver_pid.txt"
if [ -f "$codeserver_pid_file" ]; then if [ -f "$codeserver_pid_file" ]; then
pkill -P $(cat $codeserver_pid_file) sudo pkill -15 -P $(cat $codeserver_pid_file)
sudo killport 8888
echo "stopped code server" echo "stopped code server"
fi fi
@@ -168,7 +169,7 @@ elif [ "$1" == "stop-commander" ]; then
commander_pid_file="$pids_dir/commander_pid.txt" commander_pid_file="$pids_dir/commander_pid.txt"
if [ -f "$commander_pid_file" ]; then if [ -f "$commander_pid_file" ]; then
sudo killport 8889 sudo killport 8889
pkill -P $(cat $commander_pid_file) sudo pkill -15 -P $(cat $commander_pid_file)
echo "stopped commander" echo "stopped commander"
fi fi
@@ -185,7 +186,7 @@ elif [ "$1" == "finish-flight" ]; then
kill_everything kill_everything
echo "flight finished" echo "flight finished"
elif [ "$1" == "start-talking" ]; then elif [ "$1" == "start-talking" ]; then
talk_to_drone talk_to_drone $2
###################### ######################
## INFO ## INFO
@@ -199,7 +200,7 @@ else
- start-/ stop-jupyter - start-/ stop-jupyter
- start-/ stop-cs - start-/ stop-cs
- start-/ stop-commander [port] - start-/ stop-commander [port]
- start-talking - start-talking tuncel / commander
- turn-off - turn-off
- prepare-/ finish-flight" - prepare-/ finish-flight"
fi fi

View File

@@ -7,5 +7,3 @@ commander:
wpa_supp_djituad0_plus.conf wpa_supp_djituad0_plus.conf
net_wpa_supp: net_wpa_supp:
wpa_supp_uadis.conf wpa_supp_uadis.conf
env_for_dynaconf:
commander