armCtl/robot.py

92 lines
2.7 KiB
Python
Raw Normal View History

2019-07-30 00:12:23 -04:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""robot.py: class up 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 logging
import configparser
import serial
import threading
logger = logging.getLogger('__main__')
class Robot:
def __init__(self):
self.serial_port = None
self.config = None
2019-07-30 11:06:17 -04:00
self.preset = None
2019-07-30 00:12:23 -04:00
self.serial_read_thread = None
self.angle = []
self.target = []
2019-07-30 10:13:11 -04:00
self.buffer = bytes()
2019-07-30 00:12:23 -04:00
2019-07-30 11:05:45 -04:00
def move(self):
2019-07-30 10:38:09 -04:00
hexval = [hex(dec).lstrip("0x").rstrip("L") for dec in self.target]
str = '{'+','.join(hexval)+'}'
2019-07-30 10:38:22 -04:00
str = str.lower().encode()
2019-07-30 00:12:23 -04:00
try:
self.serial_port.write(str)
2019-07-30 10:37:39 -04:00
except serial.serialutil.SerialTimeoutException:
2019-07-30 00:12:23 -04:00
pass
def isMoving(self):
return self.angle != self.target
2019-07-30 11:05:08 -04:00
@staticmethod
def read_preset(str):
if str.startswith('{') and str.endswith('}'):
str = str.lstrip('{').rstrip('}')
else:
return None # malformed packet
data = str.split(',')
return [int(val) for val in data]
2019-07-30 00:12:23 -04:00
def serial_read_handler(self, data):
data = data.strip()
2019-07-30 08:52:30 -04:00
if data.startswith(b'{') and data.endswith(b'}'):
2019-07-30 09:34:00 -04:00
data = data.lstrip(b'{').rstrip(b'}')
2019-07-30 00:12:23 -04:00
else:
2019-07-30 09:33:33 -04:00
return # malformed packet
2019-07-30 09:34:00 -04:00
data = data.split(b',')
2019-07-30 09:33:33 -04:00
try:
self.angle = [int(hex, 16) for hex in data]
except ValueError:
return # malformed packet
2019-07-30 00:12:23 -04:00
2019-07-30 08:49:20 -04:00
def serial_read_worker(self):
2019-07-30 00:12:23 -04:00
while True:
2019-07-30 10:13:11 -04:00
self.buffer = self.buffer + self.serial_port.readline()
if b'\n' in self.buffer:
list = self.buffer.split(b'\n', 1)
self.buffer = list[1]
self.serial_read_handler(list[0])
2019-07-30 00:12:23 -04:00
def start(self):
# Start a serial read thread, if the serial port can be opened.
try:
self.serial_port = serial.Serial(self.config.get("port", None),
self.config.get("baud", 9600),
timeout=0)
self.serial_read_thread = threading.Thread(
2019-07-30 10:11:18 -04:00
target=self.serial_read_worker,
daemon=True)
2019-07-30 00:12:23 -04:00
self.serial_read_thread.start()
except NameError:
logger.error("ERROR: configuration hasn't been set!")
raise
except serial.serialutil.SerialException:
logger.error("ERROR: Unable to start serial port.")