-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuartSend.py
executable file
·267 lines (248 loc) · 8.61 KB
/
uartSend.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#! /usr/bin/python3
# from textwrap import indentsudo
import serial
import struct
import os
import time
# import serial_asyncio
# import asyncio
import binascii
from servo.Servo import *
from QRcode.QRcode import *
# from servo.Adafruit_PCA9685 import *
SERVO0_0m=262
SERVO0_0l=235
SERVO0_0r=290
SERVO0_1=70
SERVO0_2=20
SERVO0_3=118
SERVO1_0=5
SERVO1_1=300
SERVO1_2=150
SERVO2_0=260
SERVO2_1=90
SERVO2_2=180
SERVO2_3=0
SERVO3_0=195
SERVO3_1=220
SERVO3_2=120
SERVO0=4
SERVO1=1
SERVO2=2
SERVO3=3
SERVO0_SET=[0,0,0]
SERVO0_SET[0]=SERVO0_0l
SERVO0_SET[1]=SERVO0_0m
SERVO0_SET[2]=SERVO0_0r
SET_1=[0,0,0]
SET_1[0]=SERVO0_2
SET_1[1]=SERVO0_1
SET_1[2]=SERVO0_3
# class OutputProtocol(asyncio.Protocol):
# def __init__(self) -> None:
# super().__init__()
# # self.readin=""
# self.transport = None
# def connection_made(self, transport):
# self.transport = transport
# self.buf = bytes()
# print('port opened', transport)
# transport.serial.rts = False # You can manipulate Serial object via transport
# # transport.write(b'I,R123.32,T1235555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555\n') # Write serial data via transport
# # def data_received(self, data):
# # print('data received', repr(data),data.decode("ascii"))
# # # self.readin+=data.decode("utf-8")
# # if b'\n' in self.readin:
# # print(self.readin)
# # self.readin=""
# def data_received(self, data):
# """Store characters until a newline is received.
# """
# self.buf += data
# print(f'Reader received: {data.decode()}')
# if b'\n' in self.buf:
# lines = self.buf.split(b'\n')
# self.buf = lines[-1] # whatever was left over
# for line in lines[:-1]:
# print(f'Reader received: {line.decode()}')
# # self.msgs_recvd += 1
# # if self.msgs_recvd == 4:
# # self.transport.close()
# def data_decode(self):
# pass
# def data_write(self,data):
# self.transport.write(data) # Write serial data via
# print(data)
# def connection_lost(self, exc):
# print('port closed')
# self.transport.loop.stop()
# def pause_writing(self):
# print('pause writing')
# print(self.transport.get_write_buffer_size())
# def resume_writing(self):
# print(self.transport.get_write_buffer_size())
# print('resume writing')
class uartSerial():
def __init__(self,portx="/dev/ttyACM1",bps=115200) -> None:
self.buf = bytes()
try:
#端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等
# portx="/dev/ttyUSB0"
# #波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200
# bps=115200
#超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)
timex=1
# if not add this time, the pi will not contain the message doesn't be accepted by the arduino
# 打开串口,并得到串口对象
self.ser=serial.Serial(portx,bps,timeout=timex)
if self.ser.is_open:
print("open serial",self.ser.name)
else:
print("serial open failed")
exit(0)
except:
print("err when ini serial output")
self.currentReadinData=""
self.serialIn=""
self.NthInstruction=0
self.finishInstruction=0
pass
def send(self,value):
try:
# 写数据
result=self.ser.write(value)
# result=self.ser.write(struct.pack('>BB',id,value))
print("写总字节数:",result,"写入内容",value)
except Exception as e:
print("---err---",e)
def receive(self):
# data= self.ser.readlines()
indata=""
indata= self.ser.readline()
print(indata)
# if indata!=[]:
# indata=indata[0]
# else:
# indata=""
if indata!="":
self.currentReadinData= indata[:-1].decode()
print('in waiting',self.ser.in_waiting)
indata=self.ser.read(self.ser.in_waiting)
self.currentReadinData+=indata[:-1].decode()
print('readin',self.currentReadinData)
# print(self.currentReadinData)
return self.currentReadinData
else:
print("No readin from serial")
raise ""
# self.buf += data
# print(f'Reader received: {data.decode()}')
# if b'\n' in self.buf:
# lines = self.buf.split(b'\n')
# self.buf = lines[-1] # whatever was left over
# for line in lines[:-1]:
# print(f'Reader received: {line.decode()}')
# return data
def formatReadin(self):
self.serialInfos=[]
self.receive()
print(self.currentReadinData)
while(self.currentReadinData.find('!')==-1):
self.receive()
self.serialIn+=self.currentReadinData
lastidx=0
idx=self.serialIn.find('!')
print("idx",idx)
while(idx!=-1):
self.serialInfos+=[self.serialIn[lastidx:idx+1]]
lastidx=idx
self.serialIn=self.serialIn[idx:]
print(self.serialInfos)
# def sendBatch(self,id_num,values):
# try:
# # 写数据
# for i in range(id_num):
# result=self.ser.write(struct.pack('>BB',values[i*2],values[i*2+1]))
# print("写总字节数:",result,struct.pack('>BB',values[i*2],values[i*2+1]))
# except Exception as e:
# print("---err---",e)
# def sendBatch_V(self,id_num,values):
# try:
# # 写数据
# for i in range(id_num):
# result=self.ser.write(struct.pack('>BB',int(values[i*2]),int(values[i*2+1]/3.3*255)))
# print("写总字节数:",result,struct.pack('>BB',int(values[i*2]),int(values[i*2+1]/3.3*255)),int(values[i*2+1]/3.3*255))
# except Exception as e:
# print("---err---",e)
def close(self):
self.ser.close()#关闭串口
def sendInstruction(self,vx=-999,vy=-999,vw=-999,ms=-999,height=-999,mode=-999,order=-999):
self.finishInstruction=0
self.NthInstruction+=1
self.send(b'I%2.1f,X%2.1f,Y%2.1f,W%2.1f,T%2.1f,O%2.1f,N%2.1f,P%2.1f!'%(self.NthInstruction,vx,vy,vw,ms,height,order,mode))
while(self.receive().find('K')==-1):
pass
# if
# self.finishInstruction=1
self.receive()
if __name__=='__main__':
ServoControl(SERVO0_0l,SERVO0)
ServoControl(SERVO1_0,SERVO1)
ServoControl(SERVO2_0,SERVO2)
ServoControl(SERVO3_0,SERVO3)
ser=uartSerial(portx=os.popen("ls /dev/ttyACM*").readlines()[0][:-1])
for i in range(5):
# time.sleep(1)
print("wait for %2d s"%(i))
ser.receive()
# print(ser.receive())
print("----------------------------------")
ser.sendInstruction(height=10)
print("-------------start---------------------")
# ser.sendInstruction(0.2,0.2,0,3000)
# ser.sendInstruction(height=-10)
#ser.sendInstruction(order=123321)
# ser.sendInstruction(0,0,3.14,5000)
ser.sendInstruction(0,0.2,0,3000)
ser.sendInstruction(0.2,0,0,1800)
QRposTO(SERVO0_0l,0)
ser.sendInstruction(height=-75)#75
# ServoControl(SERVO0_0l,SERVO0)
# ServoControl(SERVO1_0,SERVO1)
# ServoControl(SERVO2_0,SERVO2)
# ServoControl(SERVO3_0,SERVO3)
catch_picture("0.jpg")
squ=QRScan("0.jpg")
print(squ)
squ=squ[:3]+squ[4:]
ser.sendInstruction(order=int(squ))
QRposRE(SERVO0_2,0)
for i in range(SERVO0_2,SERVO0_0m,1):
ServoControl(i,SERVO0)
time.sleep(0.02)
ser.sendInstruction(height=75)
ser.sendInstruction(0.2,0,0,7500)
ser.sendInstruction(0,0.2,0,2000)
ser.sendInstruction(0,-0.2,0,5000)
for i in range(SERVO0_0m,SERVO0_2,-1):
ServoControl(i,SERVO0)
time.sleep(0.02)
current_color=0
i=0
while i<3:
catch_picture("1.jpg")
current_color=classify_color()
print(current_color)
if current_color!=int(squ[i]):
pass
else:
setPlaceAngel0(SERVO0_SET[i],SERVO0_2)
setCatchAngel0(SERVO0_SET[i],SERVO0_2)
i+=1
# setPlaceAngel0(SERVO0_SET[i],SERVO0_2)
# if i!=2:
# setCatchAngel0(SERVO0_SET[i],SERVO0_2)
# else:
# setRecognize(SERVO0_0r,SERVO0_1)
# i+=1
ser.close()