Raspberry PIとArduinoでデータ通信をしています。
右か左かをl or r上か下かをt or bとして送信しているのですが。
ltなどのように繋がって受信してしまいます。
lを受信した後にrを受信するということは可能でしょうか。
Arduinoが受信です
###実現したいこと
lを送信してtを送信するとltと受信するのを、lを送信してtを送信するとlを受信したあとtを受信するようにしたい。
説明下手ですいません。
ソースコード
シリアル通信は通信確認用です。
C++
1#include <Wire.h> 2 3const int RaspiAdd = 0x04; 4 5void setup() { 6 Serial.begin(9600); 7 Wire.begin(RaspiAdd); 8 Wire.onReceive(readprocess); 9 Wire.onRequest(sendprocess); 10} 11 12void loop() { 13} 14 15void readprocess(){ 16 char input = (char)Wire.read(); 17 if(input[0] == 'n'){ 18 19 }else{ 20 if(input[0] == 'l'){ 21 Serial.print('l'); 22 } 23 else if(input[0] == 'r'){ 24 Serial.print('r'); 25 } 26 } 27 if(input[1] == 't'){ 28 Serial.println('t'); 29 } 30 else if(input[1] == 'b'){ 31 Serial.println('b'); 32 } 33} 34 35void sendprocess(){ 36 37}
画像認識した結果をRaspberry PIが送信しています
Python
1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3 4from picamera import PiCamera 5from picamera.array import PiRGBArray 6import numpy as np 7import smbus 8import time 9import cv2 10import threading 11 12#import threading 13bus = smbus.SMBus(1) 14 15SLAVE_ADDRESS = 0x04 16CAMERA_WIDTH = 688 17CAMERA_HEIGHT = 480 18CAMERA_centorX = CAMERA_WIDTH / 2 19CAMERA_centorY = CAMERA_HEIGHT / 2 20Xaxis = 10 21Yaxis = 10 22def centor(input): #二値化した画像の面積の一番大きい範囲の重心を表示 23 if np.count_nonzero(input) <= 0: 24 return(-20,-20) 25 label = cv2.connectedComponentsWithStats(input) 26 n = label[0] -1 27 data = np.delete(label[2], 0, 0) 28 center = np.delete(label[3], 0, 0) 29 max_index = np.argmax(data[:,4]) 30 return center[max_index] 31 32camera = PiCamera() 33camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT) 34camera.framerate = 30 35rawCapture = PiRGBArray(camera, size=(CAMERA_WIDTH, CAMERA_HEIGHT)) 36time.sleep(0.1) 37for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): 38 stream = image.array 39 stream = cv2.flip(stream, -1) 40 #data = np.fromstring(stream.getvalue(), dtype=np.uint8) 41 #image = cv2.imdecode(data, 1) 42 hsv = cv2.cvtColor(stream,cv2.COLOR_BGR2HSV) 43 lower_blue = np.array([160,100,100]) 44 upper_blue = np.array([180,200,200]) 45 mask = cv2.inRange(hsv,lower_blue, upper_blue) 46 denoize = cv2.medianBlur(mask,5) 47 Xaxis,Yaxis = centor(mask) 48 print(Xaxis,Yaxis) 49 cv2.circle(mask, (int(Xaxis), int(Yaxis)), 10, (0, 0, 255), 5) 50 cv2.circle(stream, (int(Xaxis), int(Yaxis)), 20, (255, 255, 0), 10) 51 cv2.imshow('image',mask) 52 cv2.imshow('Mix',stream) 53 if Xaxis == -20 and Yaxis == -20: 54 print('nosuch') 55 bus.write_byte(SLAVE_ADDRESS, ord('n')) 56 else: 57 if Xaxis < CAMERA_centorX: 58 print('left') 59 bus.write_byte(SLAVE_ADDRESS, ord('l')) 60 elif Xaxis > CAMERA_centorX: 61 print('right') 62 bus.write_byte(SLAVE_ADDRESS, ord('r')) 63 if Yaxis < CAMERA_centorY: 64 print('top') 65 bus.write_byte(SLAVE_ADDRESS, ord('t')) 66 elif Yaxis > CAMERA_centorY: 67 print('bottom') 68 bus.write_byte(SLAVE_ADDRESS, ord('b')) 69 key = cv2.waitKey(1) & 0xFF 70 71 rawCapture.truncate(0) 72 73 if key == ord("q"): 74 break 75cv2.destroyAllWindows()
回答2件
あなたの回答
tips
プレビュー