-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.py
73 lines (55 loc) · 1.79 KB
/
robot.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
# импортируем библиотеки
import abc
import serial
#создаём классы
class Robot(abc.ABC):
@abc.abstractmethod
def run_command(self, cmd: str, args: tuple): pass
@abc.abstractmethod
def _connect(self, host: str, port: int): pass
@abc.abstractmethod
def _disconnect(self): pass
@abc.abstractmethod
def _send_message(self, message: str): pass
@abc.abstractmethod
def _get_message(self, message: str = ''): pass
class RobotSerial(Robot):
def __init__(self):
self._serial = None
@abc.abstractmethod
def run_command(self, cmd: str, args: tuple):
"""
Convers RoboDK command to robot command and sends it to robot.
:param str cmd: Command (Ex: MOVEJ, CONNECT)
:param tuple args: Command arguments (Ex: Joint angles [0, 0, 0]
"""
pass
def _connect(self, port: str, baud_rate: int) -> bool:
"""
Establishes a serial connection.
:param str port: USB serial port address
:param int baud_rate: Baud rate of communication channel
:return: Whether connection attempt has succeeded
:rtype: bool
"""
try:
self._serial = serial.Serial(port, baudrate=baud_rate, timeout=0.5)
except serial.serialutil.SerialException:
return False
return True
def _disconnect(self) -> bool:
"""
Ends serial connection.
:return: Whether disconnection attempt has succeeded
:rtype: bool
"""
if not self._serial:
return True
try:
self._serial.close()
except serial.serialutil.SerialException:
return False
return True
def _send_message(self, message: str):
"""
Sends a line