Skip to content

Commit

Permalink
Merge pull request #265 from MelbourneHighSchoolRobotics/mindpile
Browse files Browse the repository at this point in the history
Mindpile integration
  • Loading branch information
glipR authored May 22, 2021
2 parents 2bd52e6 + 11f4cb7 commit aadceea
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9

- name: Install dependencies
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pypi_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.x
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9
- name: Install dependencies
run: |
sudo apt-get update
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9
- name: Install dependencies
run: |
sudo apt-get update
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/python_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ jobs:
test-package:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
python-version: 3.9
- name: Install dependencies
run: |
sudo apt-get update
Expand Down
Binary file added ev3sim/assets/ui/code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ev3sim/assets/ui/mindstorms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ev3sim/assets/ui/python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ev3sim/presets/default_mindstorms.ev3
Binary file not shown.
5 changes: 4 additions & 1 deletion ev3sim/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def initialise_bot(topLevelConfig, robotFolder, prefix, path_index):
robot._follow_collider_offset = config.get("follow_collider", [0, 0])
ScriptLoader.instance.robots[prefix] = robot
ScriptLoader.instance.outstanding_events[prefix] = []
scriptname = config.get("script", "code.py")
if config.get("type", "python") == "mindstorms":
scriptname = config.get("script", "program.ev3")
else:
scriptname = config.get("script", "code.py")
if scriptname is not None:
scriptname = find_abs(scriptname, code_locations(robotFolder))
ScriptLoader.instance.scriptnames[prefix] = scriptname
Expand Down
19 changes: 16 additions & 3 deletions ev3sim/simulation/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,23 @@ def startProcess(self, robot_id, kill_recent=True):
else:
raise ValueError("Did not expect an existing process!")
if self.scriptnames[robot_id] is not None:
from os.path import join, split
from os.path import join, split, dirname
from ev3sim.attach_bot import attach_bot

format_filename = join(self.scriptnames[robot_id])
if self.scriptnames[robot_id].endswith(".ev3"):
actual_script = join(dirname(self.scriptnames[robot_id]), ".compiled.py")
try:
from mindpile import from_ev3

with open(actual_script, "w") as f:
f.write(from_ev3(self.scriptnames[robot_id], ev3sim_support=True))
except Exception as e:
with open(actual_script, "w") as f:
f.write(f'print("Mindstorms compilation failed! {e}")')
else:
actual_script = self.scriptnames[robot_id]

format_filename = join(actual_script)
# This ensures that as long as the code sits in the bot directory, relative imports will work fine.
possible_locations = ["workspace/robots/", "workspace", "package/examples/robots"]
extra_dirs = []
Expand All @@ -84,7 +97,7 @@ def startProcess(self, robot_id, kill_recent=True):
target=attach_bot,
args=(
robot_id,
self.scriptnames[robot_id],
actual_script,
extra_dirs[::-1],
StateHandler.instance.shared_info["result_queue"],
StateHandler.instance.shared_info["result_queue"]._internal_size,
Expand Down
1 change: 1 addition & 0 deletions ev3sim/validation/bot_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BotValidator(Validator):
"follow_collider",
"hidden",
"hidden_edit",
"type",
]

@classmethod
Expand Down
128 changes: 97 additions & 31 deletions ev3sim/visual/menus/batch_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,32 +167,30 @@ def generateObjects(self):
)
self._all_objs.append(self.preview_image)

folder_size = preview_size[0] * 0.4, preview_size[1] * 0.4
folder_button_pos = (
self._size[0] * 0.9 - folder_size[0] - 10,
code_size = preview_size[0] * 0.4, preview_size[1] * 0.4
code_button_pos = (
self._size[0] * 0.9 - code_size[0] - 10,
self._size[1] * 0.1 + preview_size[1] + 10,
)
folder_icon_size = folder_size[1] * 0.6, folder_size[1] * 0.6
self.folder_button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect(*folder_button_pos, *folder_size),
code_icon_size = code_size[1] * 0.6, code_size[1] * 0.6
self.code_button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect(*code_button_pos, *code_size),
text="",
manager=self,
object_id=pygame_gui.core.ObjectID("bot-folder", "settings_buttons"),
object_id=pygame_gui.core.ObjectID("bot-code", "settings_buttons"),
)
self.addButtonEvent("bot-folder", self.clickFolder)
if not self.folder_enable:
self.folder_button.disable()
folder_icon_path = find_abs("ui/folder_white.png", allowed_areas=asset_locations())
self.folder_icon = pygame_gui.elements.UIImage(
relative_rect=pygame.Rect(
*self.iconPos(folder_button_pos, folder_size, folder_icon_size), *folder_icon_size
),
image_surface=pygame.image.load(folder_icon_path),
self.addButtonEvent("bot-code", self.clickCode)
if not self.code_enable:
self.code_button.disable()
code_icon_path = find_abs("ui/code.png", allowed_areas=asset_locations())
self.code_icon = pygame_gui.elements.UIImage(
relative_rect=pygame.Rect(*self.iconPos(code_button_pos, code_size, code_icon_size), *code_icon_size),
image_surface=pygame.image.load(code_icon_path),
manager=self,
object_id=pygame_gui.core.ObjectID("settings-icon"),
object_id=pygame_gui.core.ObjectID("code-icon"),
)
self._all_objs.append(self.folder_button)
self._all_objs.append(self.folder_icon)
self._all_objs.append(self.code_button)
self._all_objs.append(self.code_icon)

start_size = self._size[0] / 4, min(self._size[1] / 4, 120)
start_icon_size = start_size[1] * 0.6, start_size[1] * 0.6
Expand Down Expand Up @@ -262,22 +260,90 @@ def clickRemove(self):
shutil.rmtree(self.available_batches[self.batch_index][2])
self.setBatchIndex(-1)

def clickFolder(self):
def clickCode(self):
# Shouldn't happen but lets be safe.
if self.batch_index == -1:
return
# No more renaming.
import platform
import subprocess
with open(os.path.join(self.available_batches[self.batch_index][2], "bot", "config.bot")) as f:
conf = yaml.safe_load(f)
if conf.get("type", "python") == "mindstorms":
script_location = conf.get("script", "program.ev3")
import platform
import subprocess

if platform.system() == "Windows":
subprocess.Popen(
["explorer", "/select,", os.path.join(self.available_batches[self.batch_index][2], "bot", "code.py")]
)
elif platform.system() == "Darwin":
subprocess.Popen(["open", os.path.join(self.available_batches[self.batch_index][2], "bot", "code.py")])
if platform.system() == "Windows":
# If Mindstorms is in the start menu, we can likely find it.
found = False
for path in [
os.path.join(os.environ["ALLUSERSPROFILE"], "Microsoft", "Windows", "Start Menu", "Programs"),
os.path.join(os.environ["APPDATA"], "Microsoft", "Windows", "Start Menu", "Programs"),
]:
if os.path.exists(path):
for folder in os.listdir(path):
# There are multiple versions of mindstorms.
if "MINDSTORMS" in folder:
f = os.path.join(path, folder)
for file in os.listdir(f):
found = True
subprocess.run(
f'start "{os.path.join(f, file)}" "{os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)}"',
shell=True,
)
if not found:
subprocess.Popen(
[
"explorer",
"/select,",
os.path.join(self.available_batches[self.batch_index][2], "bot", script_location),
]
)
elif platform.system() == "Darwin":
subprocess.Popen(
["open", os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)]
)
else:
subprocess.Popen(
["xdg-open", os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)]
)
else:
subprocess.Popen(["xdg-open", os.path.join(self.available_batches[self.batch_index][2], "bot", "code.py")])
script_location = conf.get("script", "code.py")
import platform
import subprocess

if platform.system() == "Windows":
# If Mindstorms is in the start menu, we can likely find it.
found = False
for path in [
os.path.join(os.environ["ALLUSERSPROFILE"], "Microsoft", "Windows", "Start Menu", "Programs"),
os.path.join(os.environ["APPDATA"], "Microsoft", "Windows", "Start Menu", "Programs"),
]:
if os.path.exists(path):
for folder in os.listdir(path):
# There are multiple versions of vscode.
if "Visual Studio Code" in folder:
f = os.path.join(path, folder)
for file in os.listdir(f):
found = True
subprocess.run(
f'start "code" "{os.path.join(f, file)}" ""{os.path.join(find_abs_directory("workspace"))}" --goto "{os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)}""',
shell=True,
)
if not found:
subprocess.Popen(
[
"explorer",
"/select,",
os.path.join(self.available_batches[self.batch_index][2], "bot", script_location),
]
)
elif platform.system() == "Darwin":
subprocess.Popen(
["open", os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)]
)
else:
subprocess.Popen(
["xdg-open", os.path.join(self.available_batches[self.batch_index][2], "bot", script_location)]
)

def handleEvent(self, event):
super().handleEvent(event)
Expand All @@ -295,7 +361,7 @@ def setBatchIndex(self, new_index):
self.batch_index = new_index
self.start_enable = new_index != -1
self.remove_enable = new_index != -1
self.folder_enable = new_index != -1
self.code_enable = new_index != -1
self.regenerateObjects()

def incrementBatchIndex(self, amount):
Expand Down
Loading

0 comments on commit aadceea

Please sign in to comment.