質問編集履歴
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,4 +5,123 @@
|
|
5
5
|
Arduinoが受信です
|
6
6
|
###実現したいこと
|
7
7
|
lを送信してtを送信するとltと受信するのを、lを送信してtを送信するとlを受信したあとtを受信するようにしたい。
|
8
|
-
説明下手ですいません。
|
8
|
+
説明下手ですいません。
|
9
|
+
### ソースコード
|
10
|
+
シリアル通信は通信確認用です。
|
11
|
+
```C++
|
12
|
+
#include <Wire.h>
|
13
|
+
|
14
|
+
const int RaspiAdd = 0x04;
|
15
|
+
|
16
|
+
void setup() {
|
17
|
+
Serial.begin(9600);
|
18
|
+
Wire.begin(RaspiAdd);
|
19
|
+
Wire.onReceive(readprocess);
|
20
|
+
Wire.onRequest(sendprocess);
|
21
|
+
}
|
22
|
+
|
23
|
+
void loop() {
|
24
|
+
}
|
25
|
+
|
26
|
+
void readprocess(){
|
27
|
+
char input = (char)Wire.read();
|
28
|
+
if(input[0] == 'n'){
|
29
|
+
|
30
|
+
}else{
|
31
|
+
if(input[0] == 'l'){
|
32
|
+
Serial.print('l');
|
33
|
+
}
|
34
|
+
else if(input[0] == 'r'){
|
35
|
+
Serial.print('r');
|
36
|
+
}
|
37
|
+
}
|
38
|
+
if(input[1] == 't'){
|
39
|
+
Serial.println('t');
|
40
|
+
}
|
41
|
+
else if(input[1] == 'b'){
|
42
|
+
Serial.println('b');
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
void sendprocess(){
|
47
|
+
|
48
|
+
}
|
49
|
+
```
|
50
|
+
画像認識した結果をRaspberry PIが送信しています
|
51
|
+
```Python
|
52
|
+
#!/usr/bin/python
|
53
|
+
# -*- coding: utf-8 -*-
|
54
|
+
|
55
|
+
from picamera import PiCamera
|
56
|
+
from picamera.array import PiRGBArray
|
57
|
+
import numpy as np
|
58
|
+
import smbus
|
59
|
+
import time
|
60
|
+
import cv2
|
61
|
+
import threading
|
62
|
+
|
63
|
+
#import threading
|
64
|
+
bus = smbus.SMBus(1)
|
65
|
+
|
66
|
+
SLAVE_ADDRESS = 0x04
|
67
|
+
CAMERA_WIDTH = 688
|
68
|
+
CAMERA_HEIGHT = 480
|
69
|
+
CAMERA_centorX = CAMERA_WIDTH / 2
|
70
|
+
CAMERA_centorY = CAMERA_HEIGHT / 2
|
71
|
+
Xaxis = 10
|
72
|
+
Yaxis = 10
|
73
|
+
def centor(input): #二値化した画像の面積の一番大きい範囲の重心を表示
|
74
|
+
if np.count_nonzero(input) <= 0:
|
75
|
+
return(-20,-20)
|
76
|
+
label = cv2.connectedComponentsWithStats(input)
|
77
|
+
n = label[0] -1
|
78
|
+
data = np.delete(label[2], 0, 0)
|
79
|
+
center = np.delete(label[3], 0, 0)
|
80
|
+
max_index = np.argmax(data[:,4])
|
81
|
+
return center[max_index]
|
82
|
+
|
83
|
+
camera = PiCamera()
|
84
|
+
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
|
85
|
+
camera.framerate = 30
|
86
|
+
rawCapture = PiRGBArray(camera, size=(CAMERA_WIDTH, CAMERA_HEIGHT))
|
87
|
+
time.sleep(0.1)
|
88
|
+
for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
|
89
|
+
stream = image.array
|
90
|
+
stream = cv2.flip(stream, -1)
|
91
|
+
#data = np.fromstring(stream.getvalue(), dtype=np.uint8)
|
92
|
+
#image = cv2.imdecode(data, 1)
|
93
|
+
hsv = cv2.cvtColor(stream,cv2.COLOR_BGR2HSV)
|
94
|
+
lower_blue = np.array([160,100,100])
|
95
|
+
upper_blue = np.array([180,200,200])
|
96
|
+
mask = cv2.inRange(hsv,lower_blue, upper_blue)
|
97
|
+
denoize = cv2.medianBlur(mask,5)
|
98
|
+
Xaxis,Yaxis = centor(mask)
|
99
|
+
print(Xaxis,Yaxis)
|
100
|
+
cv2.circle(mask, (int(Xaxis), int(Yaxis)), 10, (0, 0, 255), 5)
|
101
|
+
cv2.circle(stream, (int(Xaxis), int(Yaxis)), 20, (255, 255, 0), 10)
|
102
|
+
cv2.imshow('image',mask)
|
103
|
+
cv2.imshow('Mix',stream)
|
104
|
+
if Xaxis == -20 and Yaxis == -20:
|
105
|
+
print('nosuch')
|
106
|
+
bus.write_byte(SLAVE_ADDRESS, ord('n'))
|
107
|
+
else:
|
108
|
+
if Xaxis < CAMERA_centorX:
|
109
|
+
print('left')
|
110
|
+
bus.write_byte(SLAVE_ADDRESS, ord('l'))
|
111
|
+
elif Xaxis > CAMERA_centorX:
|
112
|
+
print('right')
|
113
|
+
bus.write_byte(SLAVE_ADDRESS, ord('r'))
|
114
|
+
if Yaxis < CAMERA_centorY:
|
115
|
+
print('top')
|
116
|
+
bus.write_byte(SLAVE_ADDRESS, ord('t'))
|
117
|
+
elif Yaxis > CAMERA_centorY:
|
118
|
+
print('bottom')
|
119
|
+
bus.write_byte(SLAVE_ADDRESS, ord('b'))
|
120
|
+
key = cv2.waitKey(1) & 0xFF
|
121
|
+
|
122
|
+
rawCapture.truncate(0)
|
123
|
+
|
124
|
+
if key == ord("q"):
|
125
|
+
break
|
126
|
+
cv2.destroyAllWindows()
|
127
|
+
```
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
右か左かをl or r上か下かをt or bとして送信しているのですが。
|
3
3
|
ltなどのように繋がって受信してしまいます。
|
4
4
|
lを受信した後にrを受信するということは可能でしょうか。
|
5
|
+
Arduinoが受信です
|
5
6
|
###実現したいこと
|
6
7
|
lを送信してtを送信するとltと受信するのを、lを送信してtを送信するとlを受信したあとtを受信するようにしたい。
|
7
8
|
説明下手ですいません。
|