Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahBarrett98 committed Mar 7, 2021
1 parent 8aee1e0 commit a873f0b
Show file tree
Hide file tree
Showing 61 changed files with 53 additions and 134 deletions.
38 changes: 33 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed RealDilemma
Empty file.
10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from src.main_node.Communication.Communication import *
from src.MainNode.Communication.communication import *
import settings
import socket
import threading
from src.Social_Study import questions
from src.Pi.PiBot.PiBot2 import PiBot2
from src.SocialStudy import questions
from src.Pi.PiBot.pibot2 import PiBot2
"""
determine if main or pi
"""
Expand All @@ -15,8 +15,8 @@
from src.DRL.train import train
elif hostname == settings.PC:
IS_PI = False
# from src.main_node.Utils.OverHead import OverHead
# from src.main_node.Utils.stream_oh_inference import run
# from src.MainNode.Utils.OverHead import OverHead
# from src.MainNode.Utils.stream_oh_inference import run
else:
raise Exception("Must configure this device in settings.py")

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 5 additions & 6 deletions settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import socket
import os
"""
To set some things straight
"""
Expand All @@ -13,13 +12,13 @@
HOSTNAME = socket.gethostname()
if HOSTNAME == PC:
# ensure that mosquitto is in the path
ACTION_FILE = r".\actions.txt"
RESPONSE_FILE = r".\responses.json"
ACTION_FILE = r".\outputs\actions.txt"
RESPONSE_FILE = r".\outputs\responses.json"
else:
ACTION_FILE = "/home/pi/RealDilemma/actions.txt"
RESPONSE_FILE = r"/home/pi/RealDilemma/responses.json"
ACTION_FILE = "/home/pi/RealDilemma/outputs/actions.txt"
RESPONSE_FILE = r"/home/pi/RealDilemma/outputs/responses.json"

QUESTIONS_FILE = "./Questions.json"
QUESTIONS_FILE = "./outputs/Questions.json"
RPIS = {
"Jimi": "192.168.137.63",
"Frank": "192.168.137.118",
Expand Down
Binary file removed src/DRL/__pycache__/run_session.cpython-37.pyc
Binary file not shown.
10 changes: 2 additions & 8 deletions src/DRL/gym-pibot/gym_pibot/envs/pibot_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from src.Pi.PiBot.PiBot import PiBot
from src.Pi.PiBot.pibot import PiBot
import numpy as np

ENERGY_THRES = 4000# thres for total amount of different motors used in robo
Expand All @@ -25,12 +25,8 @@ def __init__(self):
3: self.PiBot.turn_ccw,
4: self.PiBot.turn_servo
}

self.reward_range = (0, MAX_REWARD)
# Actions: [[0:Forwards, 1:Backwards, 2:turn_cw, 3:turn_ccw, 4:Turn Servo], power]
#.action_space = spaces.Box(
# low=np.array([0, 0]), high=np.array([5, 10]))
#self.action_space = spaces.Box(low=np.array([0, 0]), high=np.array([4, 10]), dtype=np.int32)
# Actions: [[0:Forwards, 1:Backwards, 2:turn_cw, 3:turn_ccw, 4:Turn Servo]]
self.action_space = spaces.Discrete(5)
# Initial observation will just be the ultrasound sensor and amount of distance travelled (either forward or backward)
self.observation_space = spaces.Box(low=np.array([0, 0]), high=np.array([4, 1000]), dtype=np.int32)
Expand All @@ -41,7 +37,6 @@ def reset(self):
:return:
"""
# initial condition

self.PiBot.reset()
return self.PiBot.get_state()

Expand All @@ -65,7 +60,6 @@ def do_action(self, action):
""" Converts the action space into PiBot action"""
control = self.CONTROL_LOOKUP[action[0]]
# perform action with a default of 1s
#control(action[1])
control(1)

def _get_reward(self):
Expand Down
5 changes: 1 addition & 4 deletions src/DRL/gym-pibot/gym_pibot/envs/pibot_env2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from src.Pi.PiBot.PiBot2 import PiBot2
import numpy as np

ENERGY_THRES = 4000# thres for total amount of different motors used in robo
Expand All @@ -17,7 +15,7 @@ class PiBotEnv2(gym.Env):

def __init__(self, PiBot, servo=False):
super(PiBotEnv2, self).__init__()
self.PiBot = PiBot # we are using the newer pibot
self.PiBot = PiBot
self.SERVO = servo
self.CONTROL_LOOKUP = {
0: self.PiBot.forward,
Expand Down Expand Up @@ -48,7 +46,6 @@ def reset(self):
:return:
"""
# initial condition

self.PiBot.reset()
state = self._get_state()

Expand Down
23 changes: 0 additions & 23 deletions src/DRL/run_session.py

This file was deleted.

57 changes: 0 additions & 57 deletions src/DRL/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,3 @@ def train(steps, pibot, model, model_name) -> str:
del pibot
del env
return fpath

def train_PPO2_w_servo(steps, pibot) -> str:
# If the environment don't follow the interface, an error will be thrown
env = PiBotEnv2(pibot)
check_env(env, warn=True)
# The algorithms require a vectorized environment to run
env = DummyVecEnv([partial(PiBotEnv2, PiBot=pibot, servo=True)])
model = PPO2(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=steps)
fpath = os.path.join(TRAIN_DIR, HOSTNAME+"_PPO2_W_SERVO")
model.save(fpath)
with open(POLICYF, "r") as f:
policies = json.load(f)
policies["PPO2_W_SERVO"] = fpath
with open(POLICYF, "w") as f:
json.dump(policies, f)
del pibot
del env
return fpath

def train_A2C(steps, pibot) -> str:
# If the environment don't follow the interface, an error will be thrown
env = PiBotEnv2(pibot)
check_env(env, warn=True)
# The algorithms require a vectorized environment to run
env = DummyVecEnv([partial(PiBotEnv2, PiBot=pibot)])
model = A2C(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=steps)
fpath = os.path.join(TRAIN_DIR, HOSTNAME+"_A2C")
model.save(fpath)
with open(POLICYF, "r") as f:
policies = json.load(f)
policies["A2C"] = fpath
with open(POLICYF, "w") as f:
json.dump(policies, f)
del pibot
del env
return fpath

def train_A2C_SERVO(steps, pibot) -> str:
# If the environment don't follow the interface, an error will be thrown
env = PiBotEnv2(pibot, servo=False)
check_env(env, warn=True)
# The algorithms require a vectorized environment to run
env = DummyVecEnv([partial(PiBotEnv2, PiBot=pibot)])
model = A2C(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=steps)
fpath = os.path.join(TRAIN_DIR, HOSTNAME+"_A2C")
model.save(fpath)
with open(POLICYF, "r") as f:
policies = json.load(f)
policies["A2C"] = fpath
with open(POLICYF, "w") as f:
json.dump(policies, f)
del pibot
del env
return fpath
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import socket
import json
from settings import RPIS, MAIN_NODE_HOST_NAME, H_TITLE, ACTION_FILE
from settings import MAIN_NODE_HOST_NAME, H_TITLE, ACTION_FILE
import os
"""
connecting main node to pis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
script to perform inference on trained models:
credit to https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_checkpoint.html
"""
from src.main_node.Communication import main_node
from src.MainNode.Communication import main_node
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # need to supress GPU due to high memory usage
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
Expand Down
6 changes: 3 additions & 3 deletions src/main_node/README.md → src/MainNode/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This directory contains the scripts that will be run by the main node in the system
All of the overhead camera percepts will be generated by the main node as it is more
computationally expensive and therefore will be more efficiently carried out on a computer
This directory contains the scripts that will be run by the main node in the system
All of the overhead camera percepts will be generated by the main node as it is more
computationally expensive and therefore will be more efficiently carried out on a computer
more powerful than the pis.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.main_node.ObjectDetection.training.inference import run_inference
from src.MainNode.ObjectDetection.training.inference import run_inference
LABELMAP: str = r"src/ObjectDetection/data/training/annotations/label_map.pbtxt" # to be used for model inference
IMG_DIR: str = r"src/ObjectDetection/data/inference/*"

Expand Down
19 changes: 1 addition & 18 deletions src/Pi/PiBot/PiBot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
import numpy as np
from threading import Thread
from src.Pi.PiBot.PiBot import PiBot
from src.Pi.PiBot.pibot import PiBot

class PiBot2(PiBot):
"""
Expand Down Expand Up @@ -72,24 +72,7 @@ def _update_us(self):
if self.stop_us:
return
self._ultrasound = self.read_ultrasound()
"""
def _start_comm(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self

def _update_comm(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
data, addr = self.s.recvfrom(1024)
data = data.decode('utf-8')
print("Message from: " + str(addr))
print("From connected user: " + data)
data = data.upper()
print("Sending: " + data)
self.s.sendto(data.encode('utf-8'), addr)
"""
def forward(self, duty, n):
"""
move forward: action = 0
Expand Down
Binary file removed src/Pi/PiBot/__pycache__/PiBot.cpython-37.pyc
Binary file not shown.
Binary file removed src/Pi/PiBot/__pycache__/PiBot2.cpython-37.pyc
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit a873f0b

Please sign in to comment.