forked from JBoggsy/CozmoSoar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (75 loc) · 2.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from time import sleep
from argparse import ArgumentParser
from pathlib import Path
import os
import cozmo
from cozmo_soar import CozmoSoar
import PySoarLib as psl
from c_soar_util import *
def cse_factory(agent_file: Path, auto_run=False, object_file=None, debugger=False):
"""Create the Cozmo program using the CLI arguments."""
def cozmo_soar_engine(robot: cozmo.robot):
agent_name = "cozmo"
agent = psl.SoarAgent(
agent_name=agent_name,
agent_source=str(agent_file.absolute()).replace("\\", "\\\\"),
watch_level=1,
write_to_stdout=True,
print_handler=lambda s: print(GREEN_STR + s + RESET_STR),
spawn_debugger=debugger
)
cozmo_robot = CozmoSoar(agent, robot, object_file)
for command in COZMO_COMMANDS:
cozmo_robot.add_output_command(command)
agent.add_connector("cozmo", cozmo_robot)
agent.connect()
if not auto_run:
while True:
agent.execute_command(input(">> "))
else:
agent.start()
while True:
sleep(60)
agent.stop()
return cozmo_soar_engine
def gen_cli_parser():
cli_parser = ArgumentParser(description="Run a Soar agent in a Cozmo robot.")
cli_parser.add_argument(
"-r",
"--run",
dest="autorun",
help="If present, the interface will run without prompting for input.",
action="store_true",
)
cli_parser.add_argument(
"-o",
"--objects",
dest="obj_file",
help="If present, points to an XML file defining custom objects for an environment."
)
cli_parser.add_argument(
"-d",
"--debugger",
dest="debugger",
help="If present, will launch the Java soar debugger as long as it is in your SOAR_HOME.",
action="store_true"
)
cli_parser.add_argument(
"--3d-view",
dest="debugger",
help="If present, open the 3D viewer.",
action="store_true"
)
cli_parser.add_argument("agent")
return cli_parser
if __name__ == "__main__":
cli_parser = gen_cli_parser()
args = cli_parser.parse_args()
agent_file_path = Path(args.agent)
if not agent_file_path.is_file():
raise FileNotFoundError("ERROR: Agent file doesn't exist!")
else:
print("Sourcing from file {}".format(agent_file_path.absolute()))
print(args.debugger)
cse = cse_factory(agent_file_path, args.autorun, args.obj_file, args.debugger)
cozmo.run_program(cse, use_3d_viewer=False, use_viewer=True)