Skip to content

Commit 3a2871d

Browse files
committed
Added network communication and distance detection functions for m5 and seed versions
1 parent 9438305 commit 3a2871d

File tree

8 files changed

+91
-25
lines changed

8 files changed

+91
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test_test.py
1515
*.bat
1616
.VSCodeCounter
1717
.vscode/
18+
*.sh
1819

1920
*.pyc
2021
__pycache__/port.cpython-39.pyc

demo/Client.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env python3
2-
# coding:utf-8
3-
41
from pymycobot import MyCobotSocket
52

63
m = MyCobotSocket("192.168.10.10", "9000")

demo/Server.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@ def get_logger(name):
2020
# console = logging.StreamHandler()
2121
# console.setFormatter(formatter)
2222

23-
save = logging.handlers.RotatingFileHandler("/home/ubuntu/mycobot_server.log", maxBytes=10485760, backupCount=1)
23+
save = logging.handlers.RotatingFileHandler(
24+
"/home/ubuntu/mycobot_server.log", maxBytes=10485760, backupCount=1)
2425
save.setFormatter(formatter)
2526

2627
logger.addHandler(save)
2728
# logger.addHandler(console)
2829
return logger
2930

31+
3032
class MycobotServer(object):
3133

3234
def __init__(self, host, port):
3335
GPIO.setwarnings(False)
3436
self.logger = get_logger("AS")
3537
self.mc = None
3638
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37-
self.s.bind((host,port))
39+
self.s.bind((host, port))
3840
print "Binding succeeded!"
3941
self.s.listen(1)
4042
self.connect()
4143

4244
def connect(self):
43-
try:
44-
while True:
45+
while True:
46+
try:
4547
print "waiting connect!------------------"
4648
conn, addr = self.s.accept()
4749
port_baud = []
@@ -54,12 +56,13 @@ def connect(self):
5456
print("close dsiconnect!")
5557
break
5658
res = b'1'
57-
command = command.replace(" ","")
58-
if len(port_baud)<3:
59-
59+
command = command.replace(" ", "")
60+
if len(port_baud) < 3:
61+
6062
port_baud.append(command)
61-
if len(port_baud)==3:
62-
self.mc = serial.Serial(port_baud[0],port_baud[1],timeout=float(port_baud[1]))
63+
if len(port_baud) == 3:
64+
self.mc = serial.Serial(
65+
port_baud[0], port_baud[1], timeout=float(port_baud[1]))
6366
port_baud.append(1)
6467
else:
6568
self.logger.info(command)
@@ -71,12 +74,12 @@ def connect(self):
7174
GPIO.setmode(GPIO.BOARD)
7275
elif command[3] == 171:
7376
if command[5]:
74-
GPIO.setup(command[4],GPIO.OUT)
77+
GPIO.setup(command[4], GPIO.OUT)
7578
else:
76-
GPIO.setup(command[4],GPIO.IN)
79+
GPIO.setup(command[4], GPIO.IN)
7780

7881
elif command[3] == 172:
79-
GPIO.output(command[4],command[5])
82+
GPIO.output(command[4], command[5])
8083

8184
elif command[3] == 173:
8285
res = bytes(GPIO.input(command[4]))
@@ -89,19 +92,19 @@ def connect(self):
8992
except Exception as e:
9093
self.logger.error(str(e))
9194
conn.sendall(str.encode("ERROR:"+str(e)))
92-
break
93-
except Exception as e:
94-
self.logger.error(str(e))
95-
conn.close()
95+
break
96+
except Exception as e:
97+
self.logger.error(str(e))
98+
conn.close()
9699

97-
def write(self,command):
100+
def write(self, command):
98101
self.mc.write(command)
99102
self.mc.flush()
100103
time.sleep(0.05)
101104

102105
def read(self):
103106
data = None
104-
if self.mc.inWaiting()>0:
107+
if self.mc.inWaiting() > 0:
105108
data = self.mc.read(self.mc.inWaiting())
106109
return data
107110

@@ -112,7 +115,8 @@ def re_data_2(self, command):
112115
data_list = [int(i) for i in data_list]
113116
return data_list
114117

118+
115119
if __name__ == "__main__":
116120
HOST = socket.gethostbyname(socket.gethostname())
117121
PORT = 9000
118-
MycobotServer(HOST,PORT)
122+
MycobotServer(HOST, PORT)

pymycobot/common.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class ProtocolCode(object):
8686
SET_GPIO_OUTPUT = 0xAC
8787
GET_GPIO_IN = 0xAD
8888

89+
# set WIFI
90+
SET_SSID_PWD = 0xB0
91+
GET_SSID_PWD = 0xB1
92+
SET_SERVER_PORT = 0xB2
93+
94+
# Get the measured distance
95+
GET_TOF_DISTANCE = 0xC0
96+
8997

9098
class DataProcessor(object):
9199
# Functional approach
@@ -138,9 +146,11 @@ def _is_frame_header(self, data, pos1, pos2):
138146
return data[pos1] == ProtocolCode.HEADER and data[pos2] == ProtocolCode.HEADER
139147

140148
def _process_received(self, data, genre):
149+
if genre == 177:
150+
data = str(data)[2:-1].split(": ")
151+
return data[1][0:-9], data[-1]
141152
if not data:
142153
return []
143-
# print(data)
144154
data = bytearray(data)
145155
data_len = len(data)
146156
# Get valid header: 0xfe0xfe
@@ -186,8 +196,23 @@ def _process_single(self, data):
186196

187197
def write(self, command, method=None):
188198
if method == "socket":
199+
if command[3] == 176 and len(command) > 5:
200+
command = "'"+command[4]+"'"+"("+command[5]+")"
201+
command = command.encode()
189202
self.sock.sendall(bytes(command))
190-
data = self.sock.recv(1024)
203+
if command[-2] == 177:
204+
while True:
205+
data = self.sock.recv(1024)
206+
if b'password' in data:
207+
break
208+
if command[-2] == 192:
209+
data = b''
210+
while True:
211+
data += self.sock.recv(1024)
212+
if len(data) == 6:
213+
break
214+
else:
215+
data = self.sock.recv(1024)
191216
return data
192217
else:
193218
self.log.debug("_write: {}".format(command))

pymycobot/generate.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def _mesg(self, genre, *args, **kwargs):
105105
"""
106106
command_data = self._process_data_command(args)
107107

108+
if genre == 178:
109+
# 修改wifi端口
110+
command_data = self._encode_int16(command_data)
111+
108112
LEN = len(command_data) + 2
109113
command = [
110114
ProtocolCode.HEADER,
@@ -117,7 +121,6 @@ def _mesg(self, genre, *args, **kwargs):
117121

118122
real_command = self._flatten(command)
119123
has_reply = kwargs.get("has_reply", False)
120-
121124
return real_command, has_reply
122125

123126
# System status
@@ -525,3 +528,36 @@ def get_basic_input(self, pin_no):
525528
pin_no: pin port number.
526529
"""
527530
return self._mesg(ProtocolCode.GET_BASIC_INPUT, pin_no, has_reply=True)
531+
532+
def set_ssid_pwd(self, account, password):
533+
"""Set connected wifi account and password. (Apply to m5 or seeed)
534+
535+
Args:
536+
return: (account, password)
537+
"""
538+
self._mesg(ProtocolCode.SET_SSID_PWD)
539+
return self._mesg(ProtocolCode.SET_SSID_PWD, account, password)
540+
541+
def get_ssid_pwd(self):
542+
"""Get connected wifi account and password. (Apply to m5 or seeed)
543+
544+
Args:
545+
return: (account, password)
546+
"""
547+
return self._mesg(ProtocolCode.GET_SSID_PWD, has_reply=True)
548+
549+
def set_server_port(self, port):
550+
"""Set server port. (Apply to m5 or seeed)
551+
552+
Args:
553+
port: Set server port.
554+
"""
555+
return self._mesg(ProtocolCode.SET_SERVER_PORT, port)
556+
557+
def get_tof_distance(self):
558+
"""Get the measured distance.
559+
560+
Args:
561+
return: The unit is mm
562+
"""
563+
return self._mesg(ProtocolCode.GET_TOF_DISTANCE)

pymycobot/mycobot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def _mesg(self, genre, *args, **kwargs):
9595
ProtocolCode.GET_SPEED,
9696
ProtocolCode.GET_ENCODER,
9797
ProtocolCode.GET_BASIC_INPUT,
98+
ProtocolCode.GET_TOF_DISTANCE
9899
]:
99100
return self._process_single(res)
100101
elif genre in [ProtocolCode.GET_ANGLES]:

pymycobot/mycobotsocket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def _mesg(self, genre, *args, **kwargs):
103103
ProtocolCode.GET_SPEED,
104104
ProtocolCode.GET_ENCODER,
105105
ProtocolCode.GET_BASIC_INPUT,
106+
ProtocolCode.GET_TOF_DISTANCE
106107
]:
107108
return self._process_single(res)
108109
elif genre in [ProtocolCode.GET_ANGLES]:

pymycobot/mypalletizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def _mesg(self, genre, *args, **kwargs):
137137
ProtocolCode.GET_SPEED,
138138
ProtocolCode.GET_ENCODER,
139139
ProtocolCode.GET_BASIC_INPUT,
140+
ProtocolCode.GET_TOF_DISTANCE
140141
]:
141142
return self._process_single(res)
142143
elif genre in [ProtocolCode.GET_ANGLES]:

0 commit comments

Comments
 (0)