1
0
Fork 0
armCtl/armCtl.py

97 lines
3.0 KiB
Python
Raw Normal View History

2019-07-30 00:13:27 -04:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""armcCtl.py: control your robot arm."""
__author__ = "Kevin Matz"
__copyright__ = "Copyright 2019, Company 235, LLC"
__credits__ = ["Kevin Matz"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Kevin Matz"
__email__ = "kevin@company235.com"
__status__ = "Prototype"
import configparser
import logging
import sys
from firmware.robot import Robot
2019-07-30 00:13:27 -04:00
from antlr4 import CommonTokenStream
from antlr4 import InputStream
from antlr4 import ParseTreeWalker
from antlr4.error.ErrorListener import ErrorListener
2019-08-05 10:54:19 -04:00
from language.ArmControlLexer import ArmControlLexer
from language.ArmControlParser import ArmControlParser
2019-07-30 00:13:27 -04:00
from ArmCtlListener import ArmCtlListener
# define an error listener that raises SyntaxError exceptions
class SyntaxErrorListener(ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg)
# instantiate a robot
r = Robot()
# setup logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler() # create console handler and
ch.setLevel(logging.INFO) # set console log level to INFO
logger.addHandler(ch) # add console log handler
# load configuration
config = configparser.ConfigParser(allow_no_value=True)
config.read('robot.cfg') # open config file
2019-07-30 11:06:17 -04:00
r.config = config['robot'] # robot section of config file
r.preset = config['presets'] # presets section of config file
2019-07-30 11:06:34 -04:00
r.target = r.read_preset(r.preset.get('home', None)) # get default position
2019-07-30 00:13:27 -04:00
r.start() # open port, start read thread
script = ArmCtlListener(r)
walker = ParseTreeWalker()
def command(text):
input_stream = InputStream(text)
lexer = ArmControlLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = ArmControlParser(stream)
parser._listeners.append(SyntaxErrorListener())
try:
tree = parser.prog()
walker.walk(script, tree)
except SyntaxError as e:
logger.debug(e) # antlr internal listener prints the error
# # log it to the debug logger anyway
if __name__ == '__main__':
if len(sys.argv) > 1:
logger.debug("found macro at argv[1]")
2019-07-31 08:41:34 -04:00
with open(sys.argv[1]) as f:
lines = f.readlines()
for line in lines:
2019-07-31 10:34:27 -04:00
try:
print("command#", line.strip())
command(line)
except (KeyboardInterrupt):
break
print("command#", "exit")
2019-07-30 00:13:27 -04:00
else:
import readline # for input history and line editing
while True:
try:
text = input("command# ")
except (KeyboardInterrupt, EOFError):
text = 'exit'
print(text)
if text == 'exit' or text == 'quit':
break
else:
command(text)