質問編集履歴

2

修正

2018/06/30 13:16

投稿

UDON3
UDON3

スコア80

test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,241 @@
13
13
  lを送信してtを送信するとltと受信するのを、lを送信してtを送信するとlを受信したあとtを受信するようにしたい。
14
14
 
15
15
  説明下手ですいません。
16
+
17
+ ### ソースコード
18
+
19
+ シリアル通信は通信確認用です。
20
+
21
+ ```C++
22
+
23
+ #include <Wire.h>
24
+
25
+
26
+
27
+ const int RaspiAdd = 0x04;
28
+
29
+
30
+
31
+ void setup() {
32
+
33
+ Serial.begin(9600);
34
+
35
+ Wire.begin(RaspiAdd);
36
+
37
+ Wire.onReceive(readprocess);
38
+
39
+ Wire.onRequest(sendprocess);
40
+
41
+ }
42
+
43
+
44
+
45
+ void loop() {
46
+
47
+ }
48
+
49
+
50
+
51
+ void readprocess(){
52
+
53
+ char input = (char)Wire.read();
54
+
55
+ if(input[0] == 'n'){
56
+
57
+
58
+
59
+ }else{
60
+
61
+ if(input[0] == 'l'){
62
+
63
+ Serial.print('l');
64
+
65
+ }
66
+
67
+ else if(input[0] == 'r'){
68
+
69
+ Serial.print('r');
70
+
71
+ }
72
+
73
+ }
74
+
75
+ if(input[1] == 't'){
76
+
77
+ Serial.println('t');
78
+
79
+ }
80
+
81
+ else if(input[1] == 'b'){
82
+
83
+ Serial.println('b');
84
+
85
+ }
86
+
87
+ }
88
+
89
+
90
+
91
+ void sendprocess(){
92
+
93
+
94
+
95
+ }
96
+
97
+ ```
98
+
99
+ 画像認識した結果をRaspberry PIが送信しています
100
+
101
+ ```Python
102
+
103
+ #!/usr/bin/python
104
+
105
+ # -*- coding: utf-8 -*-
106
+
107
+
108
+
109
+ from picamera import PiCamera
110
+
111
+ from picamera.array import PiRGBArray
112
+
113
+ import numpy as np
114
+
115
+ import smbus
116
+
117
+ import time
118
+
119
+ import cv2
120
+
121
+ import threading
122
+
123
+
124
+
125
+ #import threading
126
+
127
+ bus = smbus.SMBus(1)
128
+
129
+
130
+
131
+ SLAVE_ADDRESS = 0x04
132
+
133
+ CAMERA_WIDTH = 688
134
+
135
+ CAMERA_HEIGHT = 480
136
+
137
+ CAMERA_centorX = CAMERA_WIDTH / 2
138
+
139
+ CAMERA_centorY = CAMERA_HEIGHT / 2
140
+
141
+ Xaxis = 10
142
+
143
+ Yaxis = 10
144
+
145
+ def centor(input): #二値化した画像の面積の一番大きい範囲の重心を表示
146
+
147
+ if np.count_nonzero(input) <= 0:
148
+
149
+ return(-20,-20)
150
+
151
+ label = cv2.connectedComponentsWithStats(input)
152
+
153
+ n = label[0] -1
154
+
155
+ data = np.delete(label[2], 0, 0)
156
+
157
+ center = np.delete(label[3], 0, 0)
158
+
159
+ max_index = np.argmax(data[:,4])
160
+
161
+ return center[max_index]
162
+
163
+
164
+
165
+ camera = PiCamera()
166
+
167
+ camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
168
+
169
+ camera.framerate = 30
170
+
171
+ rawCapture = PiRGBArray(camera, size=(CAMERA_WIDTH, CAMERA_HEIGHT))
172
+
173
+ time.sleep(0.1)
174
+
175
+ for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
176
+
177
+ stream = image.array
178
+
179
+ stream = cv2.flip(stream, -1)
180
+
181
+ #data = np.fromstring(stream.getvalue(), dtype=np.uint8)
182
+
183
+ #image = cv2.imdecode(data, 1)
184
+
185
+ hsv = cv2.cvtColor(stream,cv2.COLOR_BGR2HSV)
186
+
187
+ lower_blue = np.array([160,100,100])
188
+
189
+ upper_blue = np.array([180,200,200])
190
+
191
+ mask = cv2.inRange(hsv,lower_blue, upper_blue)
192
+
193
+ denoize = cv2.medianBlur(mask,5)
194
+
195
+ Xaxis,Yaxis = centor(mask)
196
+
197
+ print(Xaxis,Yaxis)
198
+
199
+ cv2.circle(mask, (int(Xaxis), int(Yaxis)), 10, (0, 0, 255), 5)
200
+
201
+ cv2.circle(stream, (int(Xaxis), int(Yaxis)), 20, (255, 255, 0), 10)
202
+
203
+ cv2.imshow('image',mask)
204
+
205
+ cv2.imshow('Mix',stream)
206
+
207
+ if Xaxis == -20 and Yaxis == -20:
208
+
209
+ print('nosuch')
210
+
211
+ bus.write_byte(SLAVE_ADDRESS, ord('n'))
212
+
213
+ else:
214
+
215
+ if Xaxis < CAMERA_centorX:
216
+
217
+ print('left')
218
+
219
+ bus.write_byte(SLAVE_ADDRESS, ord('l'))
220
+
221
+ elif Xaxis > CAMERA_centorX:
222
+
223
+ print('right')
224
+
225
+ bus.write_byte(SLAVE_ADDRESS, ord('r'))
226
+
227
+ if Yaxis < CAMERA_centorY:
228
+
229
+ print('top')
230
+
231
+ bus.write_byte(SLAVE_ADDRESS, ord('t'))
232
+
233
+ elif Yaxis > CAMERA_centorY:
234
+
235
+ print('bottom')
236
+
237
+ bus.write_byte(SLAVE_ADDRESS, ord('b'))
238
+
239
+ key = cv2.waitKey(1) & 0xFF
240
+
241
+
242
+
243
+ rawCapture.truncate(0)
244
+
245
+
246
+
247
+ if key == ord("q"):
248
+
249
+ break
250
+
251
+ cv2.destroyAllWindows()
252
+
253
+ ```

1

修正

2018/06/30 13:16

投稿

UDON3
UDON3

スコア80

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  lを受信した後にrを受信するということは可能でしょうか。
8
8
 
9
+ Arduinoが受信です
10
+
9
11
  ###実現したいこと
10
12
 
11
13
  lを送信してtを送信するとltと受信するのを、lを送信してtを送信するとlを受信したあとtを受信するようにしたい。