Implemented main basically
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-04-14 17:41:48 +02:00
parent f8bd18c307
commit 97986d78e5
8 changed files with 152 additions and 44 deletions

View File

@@ -1,14 +1,16 @@
#!/usr/bin/env python3
import os
import sys
from config import Config
from plugins import SleepPlugin, SyncPlugin, WaitPlugin, URRTDEPlugin
from plugin_repository import PluginRepository
from program_executor import ProgramExecutor
from program_executor import ProgramExecutor, ProgramExecutorStates
from http_server import ControllerHTTPServer
import logging
import signal
from compiler import compile_program
from program_loader import load_program
class HTTPControl:
@@ -22,43 +24,54 @@ class HTTPControl:
return 201, "Will do sir!"
def main():
# init
def main() -> int:
# init instance
logging.basicConfig(
stream=sys.stdout,
format="%(asctime)s [%(levelname)s]: %(name)s: %(message)s",
level=logging.DEBUG
level=logging.DEBUG if Config.DEBUG else logging.INFO
)
http_server = ControllerHTTPServer(HTTPControl())
http_server.start()
compiler_repo = PluginRepository()
compiler_repo.register_plugin(SleepPlugin)
compiler_repo.register_plugin(SyncPlugin)
compiler_repo.register_plugin(WaitPlugin)
compiler_repo.register_plugin(URRTDEPlugin)
logging.info("Registering available plugins...")
# Register all available plugins
plugin_repo = PluginRepository()
plugin_repo.register_plugin(SleepPlugin)
plugin_repo.register_plugin(SyncPlugin)
plugin_repo.register_plugin(WaitPlugin)
plugin_repo.register_plugin(URRTDEPlugin)
# Example code:
compiler_repo.load_plugin("sleep")
compiler_repo.load_plugin("sync")
compiler_repo.load_plugin("wait")
compiler_repo.load_plugin("ur_rtde")
program = []
program.append(compiler_repo.get_compiler("sleep").compile(secs=2))
program.append(compiler_repo.get_compiler("moveJ").compile([5.7386425805573555, -0.536165146212658, 1.6278685933351111, -2.661452576366153, -1.5683528658421044, 1.0096729722787197], 1.0, 4.0))
program.append(compiler_repo.get_compiler("moveL").compile([-0.4, 0.1, -0.31, 3.142, 0, 0], 0.05, 0.75))
program.append(compiler_repo.get_compiler("moveL").compile([-0.4, 0.1, -0.24, 3.142, 0, 0], 0.05, 0.75))
program.append(compiler_repo.get_compiler("moveJ").compile([5.923472948343555, 0.032637657012293965, 0.2590068609959585, -0.2935643801854462, -2.7157323161031766, 4.71238898038469], 1.0, 4.0))
program.append(compiler_repo.get_compiler("moveJ").compile([4.982042349817814, -0.5256931707006921, 1.620887276327134, -1.0993828958312282, -3.660653573132907, 5.271592472723674], 1.0, 4.0))
program.append(compiler_repo.get_compiler("wait").compile())
program.append(compiler_repo.get_compiler("sleep").compile(secs=3))
program.append(compiler_repo.get_compiler("sync").compile(nodes=2, name="test"))
program.append(compiler_repo.get_compiler("sleep").compile(secs=10))
program.append(compiler_repo.get_compiler("sleep").compile(secs=10))
# Download the program
logging.info("Downloading program...")
try:
program_source = load_program(Config.PROGRAM_URL)
except Exception as e:
logging.error(f"Failed to download program: {e}! Exiting...")
logging.exception(e)
return 1
# prepare execution
executor = ProgramExecutor(program, loop=True)
# Load required plugins
logging.info("Loading required plugins...")
try:
plugin_repo.load_plugins(program_source['load_plugins'])
except Exception as e:
logging.error(f"Error during plugin loading: {e}! Exiting...")
logging.exception(e)
return 2
# Compile the program
logging.info("Compiling program...")
try:
program = compile_program(plugin_repo, program_source['program'])
except Exception as e:
logging.error(f"Error during compilation: {e}! Exiting...")
logging.exception(e)
return 3
# prepare the executor
logging.info("Preparing for execution...")
executor = ProgramExecutor(program, loop=False)
# Setup signal handler
def handle_stop_signal(signum, frame):
logging.warning(f"Signal {signum} received. Aborting execution!")
executor.abort()
@@ -70,14 +83,28 @@ def main():
signal.signal(signal.SIGINT, handle_stop_signal)
signal.signal(signal.SIGTERM, handle_stop_signal)
# start execution
executor.start()
# Actually execute
execution_success = True
if Config.DRY_RUN:
logging.info("DRY_RUN enabled. Dumping command descriptions and exiting!")
for i, command in enumerate(program):
logging.info(f"{i:04d}: {command.describe()}")
else:
logging.info("Starting execution...")
executor.start()
executor.join()
# End of execution
executor.join()
compiler_repo.close()
http_server.shutdown()
if executor.state == ProgramExecutorStates.DONE:
logging.info("Program executed successfully!")
else:
logging.error(f"Could not finish execution! Executor state: {executor.state.name}")
execution_success = False
# Close all resources
logging.info("Cleaning up...")
plugin_repo.close()
return 0 if execution_success else 5
if __name__ == '__main__':
main()
sys.exit(main())