この装置 (2020.9.6) をつくるのに使用した
「ラズパイで使ったPythonプログラム」です
※ Hagiwara は、プログラミング独学の初心者です。温かい気持ちでみてください。
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 |
import RPi.GPIO as GPIO import cv2 import numpy as np GPIO.setmode(GPIO.BCM) GPIO.setup(20, GPIO.OUT) GPIO.setup(21, GPIO.OUT) GPIO.setup(22, GPIO.OUT) GPIO.setup(23, GPIO.OUT) GPIO.setup(24, GPIO.OUT) GPIO.setup(25, GPIO.OUT) #今回は5のみ使用 GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) cap = cv2.VideoCapture(0) while(1): _, frame = cap.read() # HSVに変換 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # HSV範囲 lower_red = np.array([0,60,0]) upper_red = np.array([10,255,255]) # マスク mask_red = cv2.inRange(hsv, lower_red, upper_red) res_red = cv2.bitwise_and(frame,frame, mask= mask_red) # 輪郭抽出 gray = cv2.cvtColor(res_red, cv2.COLOR_RGB2GRAY) ret, thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY) # 赤のピクセル数を数える redarea = cv2.countNonZero(thresh) # 赤が映っていたら if redarea > 30: imgEdge, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 一番大きい輪郭を抽出 contours.sort(key=cv2.contourArea, reverse=True) cnt = contours[0] # 最小外接円を描く (x,y), radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) img = cv2.circle(frame,center,radius,(0,255,0),2) img = cv2.circle(frame,(320, 240),70 ,(255,255,255),2) cv2.putText(img, 'X= '+str(int(x)), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2, cv2.LINE_AA) cv2.putText(img, 'Y= '+str(int(y)), (250,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2, cv2.LINE_AA) cv2.putText(img, 'R= '+str(int(radius)), (450,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA) if x > 360: GPIO.output(20, GPIO.LOW) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) elif x < 280: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.LOW) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) elif y < 200: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.LOW) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) elif y > 280: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.LOW) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) elif radius < 60: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.LOW) GPIO.output(25, GPIO.HIGH) elif radius > 80: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.LOW) else: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) # 赤が映っていなかったら else: if GPIO.input(5) == GPIO.LOW: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.LOW) else: GPIO.output(20, GPIO.HIGH) GPIO.output(21, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) img = cv2.circle(frame,(320, 240),70 ,(255,255,255),2) cv2.putText(img, 'Not Find', (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2, cv2.LINE_AA) # 再生 cv2.imshow('Tomato',img) k = cv2.waitKey(25) & 0xFF # 終了 if k == 27: break GPIO.cleanup() cv2.destroyAllWindows() |
前回
*******************************************
Arduinoでステッピングモーター3個を動かす(Arduino のプログラム)
・モーターに、8~13番ピン(8と9、10と11、12と13)
・スイッチ(上下左右前後)に、2~7番ピン
・Enableピンに、14~16番ピン(HIGHでドライバーOFF)
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 |
#include <Stepper.h> Stepper myStepper1(800, 8, 9); Stepper myStepper2(800, 10, 11); Stepper myStepper3(800, 12, 13); void setup(){ pinMode( 2, INPUT_PULLUP ); pinMode( 3, INPUT_PULLUP ); pinMode( 4, INPUT_PULLUP ); pinMode( 5, INPUT_PULLUP ); pinMode( 6, INPUT_PULLUP ); pinMode( 7, INPUT_PULLUP ); pinMode( 14, OUTPUT); pinMode( 15, OUTPUT); pinMode( 16, OUTPUT); digitalWrite( 14, HIGH); digitalWrite( 15, HIGH); digitalWrite( 16, HIGH); } void loop() { if(digitalRead(2) == LOW){ digitalWrite( 14, LOW); digitalWrite( 15, HIGH); digitalWrite( 16, HIGH); myStepper1.setSpeed(300); myStepper1.step(-20); } else if(digitalRead(3) == LOW){ digitalWrite( 14, LOW); digitalWrite( 15, HIGH); digitalWrite( 16, HIGH); myStepper1.setSpeed(300); myStepper1.step(20); } else if(digitalRead(4) == LOW){ digitalWrite( 14, HIGH); digitalWrite( 15, LOW); digitalWrite( 16, HIGH); myStepper2.setSpeed(300); myStepper2.step(-20); } else if(digitalRead(5) == LOW){ digitalWrite( 14, HIGH); digitalWrite( 15, LOW); digitalWrite( 16, HIGH); myStepper2.setSpeed(300); myStepper2.step(20); } else if(digitalRead(6) == LOW){ digitalWrite( 14, HIGH); digitalWrite( 15, HIGH); digitalWrite( 16, LOW); myStepper3.setSpeed(300); myStepper3.step(20); } else if(digitalRead(7) == LOW){ digitalWrite( 14, HIGH); digitalWrite( 15, HIGH); digitalWrite( 16, LOW); myStepper3.setSpeed(300); myStepper3.step(-20); } else { digitalWrite( 14, HIGH); digitalWrite( 15, HIGH); digitalWrite( 16, HIGH); delay(20); } } |