Está en la página 1de 12

UNIVERSIDAD NACIONAL DE SAN ANTONIO ABAD DEL CUSCO

FACULTAD DE INGENIERA ELCTRICA, ELECTRNICA,


INFORMTICA Y MECNICA

ESCUELA PROFESIONAL DE INGENIERA


INFORMTICA Y DE SISTEMAS

ROBOTICA Y PROCESAMIENTO DE SEAL

SOLUCIN DEL PROBLEMA DE LA ESQUINA Y DEL CALLEJN

RECONOCIMIENTO DE COLORES

Docente: Lic. Jose Muro Pillco Quispe

Alumnos:

Bernal Rios, Paul Theo 113549

Farfan Escobedo, Jeanfranco David 120885

Farfan Lazo, Josue 113552

Quispe Condori, Edgar Rodolfo 120682

CUSCO PER

2016
1. SOLUCIN DEL PROBLEMA DE LA ESQUINA Y DEL CALLEJN
a. Diagrama.
Servomotor

Trig

Sensor de
Proximidad

Pulso

Echo
Arduino nano

Gnd Vcc
Puente H

Motor
b. Codigo Fuente
2. #include <Servo.h>
3. #define left 7
4. #define right 8
5. #define fordward 9
6. #define backward 10
7.
8. #define Pecho 6
9. #define Ptrig 5
10. #define pinServo 4
11. int angle=0, dir = -5 ;
12. long duracion, distancia;
13. int DELAY = 10;
14. int maxangulo, maxdist;
15. Servo servo;
16.
17. void setup() {
18. // put your setup code here, to run once:
19. pinMode(Pecho,INPUT);
20. pinMode(Ptrig,OUTPUT);
21. pinMode(left, OUTPUT);
22. pinMode(right, OUTPUT);
23. pinMode(fordward, OUTPUT);
24. pinMode(backward, OUTPUT);
25. Serial.begin(9600);
26. servo.attach(pinServo,600,1500);
27.
28. digitalWrite(left, LOW);
29. digitalWrite(right, LOW);
30. digitalWrite(fordward, LOW);
31. digitalWrite(backward, LOW);
32.
33. servo.write(90);
34. }
35. int getBestDistance()
36. {
37. maxangulo = 0;
38. maxdist = 6000;
39. //Serial.println("----------------------------");
40. for(int i=0;i<=180;i+=10)
41. {
42. servo.write(i);
43. if(i==90) {delay(100); continue;}
44. int tmp = med();
45. /*Serial.print(i );
46. Serial.print(" : ");
47. Serial.println(tmp);
48. Serial.print(maxdist);
49. Serial.print(" ::> ");
50. Serial.print(tmp);*/
51. if(tmp<maxdist)
52. {
53. maxdist = tmp;
54. maxangulo = i;
55. /*Serial.print(maxdist);
56. Serial.print(" ---> ");
57. Serial.println(maxangulo);*/
58.
59. }
60. delay(100);
61. }
62. /*Serial.print(maxdist);
63. Serial.println("----------------------------");*/
64. servo.write(90);
65. return maxangulo;
66. }
67. int med(){
68. int dis=0;
69. digitalWrite(Ptrig, LOW);
70. delayMicroseconds(2);
71. digitalWrite(Ptrig, HIGH); // genera el pulso
de triger por 10ms
72. delayMicroseconds(10);
73. digitalWrite(Ptrig, LOW);
74.
75. duracion = pulseIn(Pecho, HIGH);
76. distancia = (duracion/2) / 29; //
calcula la distancia en centimetros seria
77. //Serial.print("Distancia: ");
78. //Serial.println(distancia);
79. //delay(DELAY);
80. dis=distancia;
81. //if(dis>5000 ) dis = 0;
82. return dis;
83. }
84. void runCar(char in)
85. {
86. if(in == 'a') //left
87. {
88. Serial.println("left");
89. digitalWrite(left, HIGH);
90. digitalWrite(right, LOW);
91. digitalWrite(fordward, LOW);
92. digitalWrite(backward, LOW);
93. }
94. if(in == 'd') //right
95. {
96. Serial.println("right");
97. digitalWrite(left, LOW);
98. digitalWrite(right, HIGH);
99. digitalWrite(fordward, LOW);
100. digitalWrite(backward, LOW);
101.
102. }
103. if(in == 'w') //fordward
104. {
105. Serial.println("fordward");
106. digitalWrite(left, LOW);
107. digitalWrite(right, LOW);
108. digitalWrite(fordward, HIGH);
109. digitalWrite(backward, LOW);
110. }
111. if(in == 's') //backward
112. {
113. Serial.println("backward");
114. digitalWrite(left, LOW);
115. digitalWrite(right, LOW);
116. digitalWrite(fordward, LOW);
117. digitalWrite(backward, HIGH);
118. }
119.
120. if(in == 'e') // fordward right
121. {
122. Serial.println("fordward right");
123. digitalWrite(left, LOW);
124. digitalWrite(right, HIGH);
125. digitalWrite(fordward, HIGH);
126. digitalWrite(backward, LOW);
127. }
128. if(in == 'q') // fordward left
129. {
130. Serial.println("fordward left");
131. digitalWrite(left, HIGH);
132. digitalWrite(right, LOW);
133. digitalWrite(fordward, HIGH);
134. digitalWrite(backward, LOW);
135. }
136. if(in == 'c') //backward right
137. {
138. Serial.println("backward right");
139. digitalWrite(left, LOW);
140. digitalWrite(right, HIGH);
141. digitalWrite(fordward, LOW);
142. digitalWrite(backward, HIGH);
143. }
144. if(in == 'z') //backward left
145. {
146. Serial.println("backward left");
147. digitalWrite(left, HIGH);
148. digitalWrite(right, LOW);
149. digitalWrite(fordward, LOW);
150. digitalWrite(backward, HIGH);
151. }
152. if(in == 'o') //backward left
153. {
154. Serial.println("backward left");
155. digitalWrite(left, LOW);
156. digitalWrite(right, LOW);
157. digitalWrite(fordward, LOW);
158. digitalWrite(backward, LOW);
159. }
160. }
161. void makeForce()
162. {
163. for(int i=0;i<4;i++)
164. {
165. runCar('d');
166. delay(100);
167. runCar('a');
168. delay(100);
169. }
170. }
171. void algobruto(){
172. digitalWrite(Ptrig, LOW);
173. delayMicroseconds(2);
174. digitalWrite(Ptrig, HIGH); // genera el pulso
de triger por 10ms
175. delayMicroseconds(10);
176. digitalWrite(Ptrig, LOW);
177.
178. duracion = pulseIn(Pecho, HIGH);
179. distancia = (duracion/2) / 29; //
calcula la distancia en centimetros seria
180. delay(DELAY);
181. if(distancia>=5000) distancia = 0;
182. if( distancia > 40){
183. runCar('w');
184. }
185. else{
186. runCar('s');
187. delay(300);
188. runCar('o');
189. maxangulo = getBestDistance();
190. makeForce();
191. if(!(maxangulo<=90))
192. {
193. runCar('z');
194. delay(1100);
195. runCar('e');
196. delay(1000);
197. }
198. else
199. {
200. runCar('c');
201. delay(1100);
202. runCar('q');
203. delay(1000);
204.
205. }
206. }
207. }
208. void loop() {
209. algobruto();
210.
211. }
212.
2. RECONOCIMIENTO DE COLORES
a. Diagrama
Servomotor

Bluetooth

Trig

Sensor de
Proximidad

Pulso

Echo
Arduino nano

Gnd Vcc
Puente H

Motor
b. Codigo Fuente
package com.kangkamal.remoteandtouchfollow;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
public class ColorBlobDetector {
// Lower and Upper bounds for range checking in HSV color space
private Scalar mLowerBound = new Scalar(0);
private Scalar mUpperBound = new Scalar(0);
// Minimum contour area in percent for contours filtering
private static double mMinContourArea = 0.1;
// Color radius for range checking in HSV color space
private Scalar mColorRadius = new Scalar(25,50,50,0);
private Mat mSpectrum = new Mat();
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
private Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);;

// Cache
Mat mPyrDownMat = new Mat();
Mat mHsvMat = new Mat();
Mat mMask = new Mat();
Mat mDilatedMask = new Mat();
Mat mHierarchy = new Mat();
public void setColorRadius(Scalar radius) {
mColorRadius = radius;
}
public void setHsvColor(Scalar hsvColor) {
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-
mColorRadius.val[0] : 0;
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ?
hsvColor.val[0]+mColorRadius.val[0] : 255;
mLowerBound.val[0] = minH;
mUpperBound.val[0] = maxH;
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];
mLowerBound.val[3] = 0;
mUpperBound.val[3] = 255;
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);
for (int j = 0; j < maxH-minH; j++) {
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
spectrumHsv.put(0, j, tmp);
}
Imgproc.cvtColor(spectrumHsv, mSpectrum,
Imgproc.COLOR_HSV2RGB_FULL, 4);
}
public Mat getSpectrum() {
return mSpectrum;
}

public void setMinContourArea(double area) {


mMinContourArea = area;
}
public void process(Mat rgbaImage) {
Imgproc.pyrDown(rgbaImage, mPyrDownMat);
Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);
Imgproc.cvtColor(mPyrDownMat, mHsvMat,
Imgproc.COLOR_RGB2HSV_FULL);
Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
Imgproc.dilate(mMask, mDilatedMask, new Mat());
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
try {
Imgproc.drawContours(rgbaImage, contours, -1, CONTOUR_COLOR, -1);
//Warna full merah smw >> Terblock
Imgproc.findContours(mDilatedMask, contours, mHierarchy,
Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
} catch (Exception e) {
// TODO: handle exception
}
// Find max contour area
double maxArea = 1000;
Iterator<MatOfPoint> each = contours.iterator();
while (each.hasNext()) {
MatOfPoint wrapper = each.next();
double area = Imgproc.contourArea(wrapper);
if (area > maxArea){
maxArea = area;
}
}
mContours.clear();
each = contours.iterator();
while (each.hasNext()) {
MatOfPoint contour = each.next();
if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
Core.multiply(contour, new Scalar(4,4), contour);
mContours.add(contour);
}
}
}

public List<MatOfPoint> getContours() {


return mContours;
}
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {


mRgba = inputFrame.rgba();
if (mIsColorSelected) {
mDetector.process(mRgba);
List<MatOfPoint> contours = mDetector.getContours();
Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);
btnReset.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sendData("Z");
mIsColorSelected = false;
sendData("Z"); //Memastikan kalau robot harus benar2 stop
}});
for(int i=0; i< contours.size();i++){
System.out.println(Imgproc.contourArea(contours.get(i)));
if (Imgproc.contourArea(contours.get(i)) > 50 ){
Rect rect = Imgproc.boundingRect(contours.get(i));
//System.out.println(rect.height);
if (rect.height > 28){
sizeObject = rect.height + rect.width; if
(contours.size() == 0){
Core.putText(mRgba, "No Object", new Point(10, 100), 2, 1, new Scalar(255, 0, 0,
255), 3);
sendData("1cs");
}
if (contours.size() == 1){
Core.putText(mRgba, "Following", new Point(10, 250), 2, 1, mBlobColorRgba, 3);

Core.putText(mRgba, "x: " + rect.x + " y: " + rect.y, new Point(10, 100), 2, 1, new
Scalar(255, 255, 0, 255), 3);
Core.putText(mRgba, "Size: " + sizeObject, new Point(10, 150), 2, 1, new Scalar(0,
255, 0, 255), 3);
Core.putText(mRgba, "Total: " + contours.size(), new Point(10, 200), 2, 1, new
Scalar(255, 0, 0, 255), 3);
Core.rectangle(mRgba, new Point(rect.x,rect.y), new
Point(rect.x+rect.width,rect.y+rect.height),new Scalar(255,0,255));

sendData(rect.x + "as");
sendData(sizeObject + "bs");
)
if (contours.size() > 1){
Core.putText(mRgba, "I'm Dizzy, Just One Please", new Point(10, 100), 2, 1, new
Scalar(255, 0, 0, 255), 3);
sendData("Z");
}
}
}
Mat colorLabel = mRgba.submat(4, 68, 4, 68);
colorLabel.setTo(mBlobColorRgba);

Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 +


mSpectrum.cols());
mSpectrum.copyTo(spectrumLabel);
}
return mRgba
}

También podría gustarte