Skip to content

Commit e15c752

Browse files
authored
Create robot.py
1 parent 05172c6 commit e15c752

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

robot.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# импортируем библиотеки
2+
import abc
3+
import serial
4+
5+
#создаём классы
6+
7+
class Robot(abc.ABC):
8+
@abc.abstractmethod
9+
def run_command(self, cmd: str, args: tuple): pass
10+
11+
@abc.abstractmethod
12+
def _connect(self, host: str, port: int): pass
13+
14+
@abc.abstractmethod
15+
def _disconnect(self): pass
16+
17+
@abc.abstractmethod
18+
def _send_message(self, message: str): pass
19+
20+
@abc.abstractmethod
21+
def _get_message(self, message: str = ''): pass
22+
23+
24+
class RobotSerial(Robot):
25+
def __init__(self):
26+
self._serial = None
27+
28+
@abc.abstractmethod
29+
def run_command(self, cmd: str, args: tuple):
30+
"""
31+
Convers RoboDK command to robot command and sends it to robot.
32+
:param str cmd: Command (Ex: MOVEJ, CONNECT)
33+
:param tuple args: Command arguments (Ex: Joint angles [0, 0, 0]
34+
"""
35+
36+
pass
37+
38+
def _connect(self, port: str, baud_rate: int) -> bool:
39+
"""
40+
Establishes a serial connection.
41+
:param str port: USB serial port address
42+
:param int baud_rate: Baud rate of communication channel
43+
:return: Whether connection attempt has succeeded
44+
:rtype: bool
45+
"""
46+
47+
try:
48+
self._serial = serial.Serial(port, baudrate=baud_rate, timeout=0.5)
49+
except serial.serialutil.SerialException:
50+
return False
51+
52+
return True
53+
54+
def _disconnect(self) -> bool:
55+
"""
56+
Ends serial connection.
57+
:return: Whether disconnection attempt has succeeded
58+
:rtype: bool
59+
"""
60+
61+
if not self._serial:
62+
return True
63+
64+
try:
65+
self._serial.close()
66+
except serial.serialutil.SerialException:
67+
return False
68+
69+
return True
70+
71+
def _send_message(self, message: str):
72+
"""
73+
Sends a line

0 commit comments

Comments
 (0)