Skip to content

Commit a3186fb

Browse files
committed
Optimize tcp/ip control gpio
1 parent 703ab62 commit a3186fb

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

pymycobot/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class ProtocolCode(object):
8080
SET_BASIC_OUTPUT = 0xA0
8181
GET_BASIC_INPUT = 0xA1
8282

83+
# Linux GPIO, mode: GPIO.BCM
84+
SET_GPIO_MODE = 0xAA
85+
SET_GPIO_UP = 0xAB
86+
SET_GPIO_OUTPUT = 0xAC
87+
GET_GPIO_IN = 0xAD
88+
8389

8490
class DataProcessor(object):
8591
# Functional approach

pymycobot/error.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,12 @@ def calibration_parameters(**kwargs):
7171
for i, v in enumerate(kwargs["rgb"]):
7272
if not (0 <= v <= 255):
7373
raise MyCobotDataException(
74-
"The RGB value needs be 0 ~ 255, but the %s is %s" % (rgb_str[i], v)
74+
"The RGB value needs be 0 ~ 255, but the %s is %s" % (
75+
rgb_str[i], v)
7576
)
77+
78+
if kwargs.get("gpiomode", None) is not None:
79+
mode = kwargs["gpiomode"]
80+
if mode not in ["BCM", "BOARD"]:
81+
raise MyCobotDataException(
82+
"'module' object has no attribute '%s',Available values are: 'BCM' or 'BOARD'" % (mode))

pymycobot/mycobotsocket.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def __init__(self, ip, netport, serialport="/dev/ttyAMA0", baudrate="1000000", t
5959
super(MyCobotSocket, self).__init__()
6060
self.calibration_parameters = calibration_parameters
6161
self.SERVER_IP = ip
62-
# 端口号
6362
self.SERVER_PORT = 9000
6463
self.sock = self.connect()
6564
self._write(serialport, "socket")
@@ -68,7 +67,7 @@ def __init__(self, ip, netport, serialport="/dev/ttyAMA0", baudrate="1000000", t
6867

6968
def connect(self):
7069
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
71-
sock.connect((self.SERVER_IP, self.SERVER_PORT)) # 打开链接
70+
sock.connect((self.SERVER_IP, self.SERVER_PORT))
7271
return sock
7372

7473
def _mesg(self, genre, *args, **kwargs):
@@ -87,7 +86,6 @@ def _mesg(self, genre, *args, **kwargs):
8786
MyCobotSocket, self)._mesg(genre, *args, **kwargs)
8887
# [254,...,255]
8988
data = self._write(self._flatten(real_command), "socket")
90-
9189
if data != b'None':
9290
res = self._process_received(data, genre)
9391
if genre in [
@@ -168,12 +166,42 @@ def sync_send_coords(self, coords, speed, mode, timeout=7):
168166
time.sleep(0.1)
169167
return self
170168

171-
def pub_pump(self, v):
172-
"""Set GPIO output value.
169+
def set_gpio_mode(self, mode):
170+
"""Set GPIO mode
171+
Args:
172+
mode: BCM or BOARD (str)
173+
"""
174+
self.calibration_parameters(gpiomode=mode)
175+
if mode == "BCM":
176+
return self._mesg(ProtocolCode.SET_GPIO_MODE, 0)
177+
else:
178+
return self._mesg(ProtocolCode.SET_GPIO_MODE, 1)
179+
180+
def set_gpio_out(self, pin_no, mode):
181+
"""Equivalent to: GPIO.setup(pin_no, mode)
182+
Args:
183+
pin_no: (int)
184+
mode: (str) in --> GPIO.IN out --> GPIO.OUT
185+
"""
186+
if mode == "in":
187+
return self._mesg(ProtocolCode.SET_GPIO_UP, pin_no, 0)
188+
else:
189+
return self._mesg(ProtocolCode.SET_GPIO_UP, pin_no, 1)
190+
191+
def set_gpio_output(self, pin_no, state):
192+
"""Set state of GPIO pin
193+
Args:
194+
pin_no: (int)
195+
state: (int) 0 --> GPIO..HIGH 1 --> GPIO.LOW
196+
"""
197+
return self._mesg(ProtocolCode.SET_GPIO_OUTPUT, pin_no, state)
198+
199+
def get_gpio_in(self, pin_no):
200+
"""Detect state of GPIO pin
173201
Args:
174-
v: Output value(int), 1 - pump close, 0 - pump open
202+
pin_no: (int)
175203
"""
176-
self._write(v, "socket")
204+
return self._mesg(ProtocolCode.GET_GPIO_IN, pin_no)
177205

178206
# Other
179207
def wait(self, t):

0 commit comments

Comments
 (0)