QQQ

Dependencies:   mbed-rtos mbed

Fork of BX-car_s by Tony Lin

Committer:
TonyLin
Date:
Sat Jun 28 18:59:41 2014 +0000
Revision:
17:af867c7512bb
Parent:
12:418e39749f48
Child:
18:88b083db7491
6/29

Who changed what in which revision?

UserRevisionLine numberNew contents of line
backman 1:82bc25a7b68b 1 #include "mbed.h"
backman 1:82bc25a7b68b 2 #include "camera_api.h"
backman 1:82bc25a7b68b 3
backman 1:82bc25a7b68b 4 #define clk 2 //ms
backman 6:5a39bde2e016 5
TonyLin 17:af867c7512bb 6
backman 6:5a39bde2e016 7 BX_camera::BX_camera(void)
backman 6:5a39bde2e016 8 {
backman 6:5a39bde2e016 9
backman 6:5a39bde2e016 10 line_CamR = new FastAnalogIn(PTD5);
backman 6:5a39bde2e016 11 line_CamL= new FastAnalogIn(PTD6,0);
backman 6:5a39bde2e016 12 cam_clk=new DigitalOut(PTE1);
backman 6:5a39bde2e016 13 si=new DigitalOut(PTD7);
backman 6:5a39bde2e016 14
backman 6:5a39bde2e016 15 }
backman 6:5a39bde2e016 16
backman 6:5a39bde2e016 17 int BX_camera::black_centerR(void)
backman 6:5a39bde2e016 18 {
backman 6:5a39bde2e016 19
backman 7:fd976e1ced33 20 int l_care=10;
backman 7:fd976e1ced33 21 int r_care=118;
backman 7:fd976e1ced33 22
backman 7:fd976e1ced33 23
backman 6:5a39bde2e016 24 // find center
backman 6:5a39bde2e016 25 // case 1 // | //
backman 6:5a39bde2e016 26 // case 2 / | /
backman 6:5a39bde2e016 27
backman 6:5a39bde2e016 28 char find_type=0x00;
backman 6:5a39bde2e016 29
TonyLin 17:af867c7512bb 30 int b_end=64;
TonyLin 17:af867c7512bb 31 int b_start=64;
backman 6:5a39bde2e016 32 int b_w=0;
TonyLin 17:af867c7512bb 33 int b_center=64;
TonyLin 17:af867c7512bb 34 int b2_end=64;
TonyLin 17:af867c7512bb 35 int b2_start=64;
TonyLin 17:af867c7512bb 36 int b2_center=64;
backman 6:5a39bde2e016 37 int b2_w=0;
backman 7:fd976e1ced33 38 int center=30;
backman 6:5a39bde2e016 39 int j=64;
TonyLin 17:af867c7512bb 40 int offset=64;
backman 6:5a39bde2e016 41 bool f1=false;
backman 6:5a39bde2e016 42 bool f2=false;
backman 6:5a39bde2e016 43 bool f3=false;
backman 6:5a39bde2e016 44 bool f4=false;
backman 6:5a39bde2e016 45 int w_thr_up=32;
backman 6:5a39bde2e016 46 int w_thr_dn=0;
backman 6:5a39bde2e016 47
TonyLin 17:af867c7512bb 48 b_start=b_center=b_end=b2_start=b2_center=b2_end=offset;
TonyLin 17:af867c7512bb 49
backman 6:5a39bde2e016 50 if(sign_line_imageR[64]==' ')
backman 6:5a39bde2e016 51 find_type=0x02;
backman 6:5a39bde2e016 52 else
backman 6:5a39bde2e016 53 find_type=0x01;
backman 6:5a39bde2e016 54
backman 6:5a39bde2e016 55
backman 7:fd976e1ced33 56 for(int i=64; i<r_care; i++,j--) {
TonyLin 17:af867c7512bb 57
backman 6:5a39bde2e016 58 switch(find_type) {
backman 6:5a39bde2e016 59 case 0x01:
backman 6:5a39bde2e016 60 if(f1==false&&sign_line_imageR[i]==' ') {
backman 6:5a39bde2e016 61 if(f1==false) {
backman 6:5a39bde2e016 62 b_start=i;
backman 6:5a39bde2e016 63 f1=true;
backman 6:5a39bde2e016 64 }
backman 6:5a39bde2e016 65 }
backman 6:5a39bde2e016 66 if(f1== true&& f2==false&&sign_line_imageR[i]=='O') {
backman 6:5a39bde2e016 67 if(f2==false) {
backman 6:5a39bde2e016 68 b_end=i-1;
backman 6:5a39bde2e016 69 f2=true;
backman 6:5a39bde2e016 70 }
backman 6:5a39bde2e016 71 }
backman 6:5a39bde2e016 72 if(f3==false&&sign_line_imageR[j]==' ') {
backman 6:5a39bde2e016 73 if(f3==false) {
backman 6:5a39bde2e016 74 b2_end=j;
backman 6:5a39bde2e016 75 f3=true;
backman 6:5a39bde2e016 76 }
backman 6:5a39bde2e016 77 }
backman 6:5a39bde2e016 78 if(f3==true&&f4==false&&sign_line_imageR[j]=='O') {
backman 6:5a39bde2e016 79 if(f4==false) {
TonyLin 17:af867c7512bb 80 b2_start=j+1;
backman 6:5a39bde2e016 81 f4=true;
backman 6:5a39bde2e016 82 }
backman 6:5a39bde2e016 83 }
backman 6:5a39bde2e016 84 break;
backman 6:5a39bde2e016 85
backman 6:5a39bde2e016 86 case 0x02:
backman 6:5a39bde2e016 87 if(sign_line_imageR[i]=='O') {
backman 6:5a39bde2e016 88 if(f1==false) {
backman 6:5a39bde2e016 89 b_end=i;
backman 6:5a39bde2e016 90 f1=true;
backman 6:5a39bde2e016 91 }
backman 6:5a39bde2e016 92 }
backman 6:5a39bde2e016 93 if(sign_line_imageR[j]=='O') {
backman 6:5a39bde2e016 94 if(f2==false) {
backman 6:5a39bde2e016 95 b_start=j;
backman 6:5a39bde2e016 96 f2=true;
backman 6:5a39bde2e016 97 }
backman 6:5a39bde2e016 98 }
backman 6:5a39bde2e016 99 break;
backman 6:5a39bde2e016 100 }
backman 6:5a39bde2e016 101 }
backman 6:5a39bde2e016 102
backman 6:5a39bde2e016 103 b_w=b_start-b_end;
backman 6:5a39bde2e016 104 b2_w=b2_start-b2_end;
TonyLin 17:af867c7512bb 105
backman 11:03d5aa2511c4 106 de_v=b_start;
TonyLin 17:af867c7512bb 107 de_v2=b_end;
backman 6:5a39bde2e016 108
backman 6:5a39bde2e016 109 switch(find_type) {
TonyLin 17:af867c7512bb 110 case 0x01:
TonyLin 17:af867c7512bb 111 b_center=(b_end+b_start)/2;
TonyLin 17:af867c7512bb 112 b2_center=(b2_end+b2_start)/2;
TonyLin 17:af867c7512bb 113 if(w_thr_up>b_w && (b_center!=offset) && (b_center-offset)<(offset-b2_center))
TonyLin 17:af867c7512bb 114 center=b_center;
TonyLin 17:af867c7512bb 115 else
TonyLin 17:af867c7512bb 116 center=b2_center;
backman 6:5a39bde2e016 117
TonyLin 17:af867c7512bb 118 /* if( ( w_thr_up- (b_w))>0 &&( ( w_thr_up- (b_w)) < (w_thr_up-(b2_w)) ) ) {
TonyLin 17:af867c7512bb 119 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 120
backman 6:5a39bde2e016 121
backman 6:5a39bde2e016 122
TonyLin 17:af867c7512bb 123 // } else if( ( w_thr_up- (b2_w) )>0 ) {
TonyLin 17:af867c7512bb 124 center=(b2_end+b2_start)/2;
TonyLin 17:af867c7512bb 125 // } else {
backman 6:5a39bde2e016 126
TonyLin 17:af867c7512bb 127 center=65;
backman 6:5a39bde2e016 128
TonyLin 17:af867c7512bb 129 //????????????????
backman 6:5a39bde2e016 130
TonyLin 17:af867c7512bb 131 }
backman 6:5a39bde2e016 132
TonyLin 17:af867c7512bb 133 */
backman 6:5a39bde2e016 134 break;
backman 6:5a39bde2e016 135
backman 6:5a39bde2e016 136 case 0x02:
backman 6:5a39bde2e016 137 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 138 break;
backman 3:c5f2281b3ed2 139 }
backman 6:5a39bde2e016 140 return center;
backman 6:5a39bde2e016 141 }
backman 6:5a39bde2e016 142
backman 6:5a39bde2e016 143
backman 3:c5f2281b3ed2 144
backman 6:5a39bde2e016 145 int BX_camera::black_centerL(void)
backman 6:5a39bde2e016 146 {
backman 6:5a39bde2e016 147
backman 12:418e39749f48 148 int l_care=10;
backman 12:418e39749f48 149 int r_care=118;
backman 6:5a39bde2e016 150 // find center
backman 6:5a39bde2e016 151 // case 1 // | //
backman 12:418e39749f48 152 // case 2 / | /
backman 6:5a39bde2e016 153 char find_type=0x00;
backman 12:418e39749f48 154 int b_end=118;
backman 12:418e39749f48 155 int b_start=118;
backman 12:418e39749f48 156 int b_w=0;
backman 12:418e39749f48 157 int b_center=0;
backman 6:5a39bde2e016 158 int b2_end=0;
backman 6:5a39bde2e016 159 int b2_start=0;
backman 12:418e39749f48 160 int b2_center=0;
backman 12:418e39749f48 161 int b2_w=0;
backman 12:418e39749f48 162 int center=30;
backman 6:5a39bde2e016 163 int j=64;
backman 6:5a39bde2e016 164 bool f1=false;
backman 6:5a39bde2e016 165 bool f2=false;
backman 6:5a39bde2e016 166 bool f3=false;
backman 6:5a39bde2e016 167 bool f4=false;
backman 12:418e39749f48 168 int w_thr_up=32;
backman 12:418e39749f48 169 int w_thr_dn=0;
backman 6:5a39bde2e016 170
backman 12:418e39749f48 171 if(sign_line_imageR[64]==' ')
backman 6:5a39bde2e016 172 find_type=0x02;
backman 6:5a39bde2e016 173 else
backman 6:5a39bde2e016 174 find_type=0x01;
backman 6:5a39bde2e016 175
backman 6:5a39bde2e016 176
backman 12:418e39749f48 177 for(int i=64; i<r_care; i++,j--) {
TonyLin 17:af867c7512bb 178
backman 6:5a39bde2e016 179 switch(find_type) {
backman 6:5a39bde2e016 180 case 0x01:
backman 12:418e39749f48 181 if(f1==false&&sign_line_imageR[i]==' ') {
backman 6:5a39bde2e016 182 if(f1==false) {
backman 6:5a39bde2e016 183 b_start=i;
backman 6:5a39bde2e016 184 f1=true;
backman 6:5a39bde2e016 185 }
backman 6:5a39bde2e016 186
backman 6:5a39bde2e016 187 }
backman 12:418e39749f48 188 if(f1== true&& f2==false&&sign_line_imageR[i]=='O') {
backman 6:5a39bde2e016 189 if(f2==false) {
backman 6:5a39bde2e016 190 b_end=i-1;
backman 6:5a39bde2e016 191 f2=true;
backman 6:5a39bde2e016 192 }
backman 6:5a39bde2e016 193
backman 6:5a39bde2e016 194 }
backman 12:418e39749f48 195 if(f3==false&&sign_line_imageR[j]==' ') {
backman 6:5a39bde2e016 196 if(f3==false) {
backman 6:5a39bde2e016 197 b2_end=j;
backman 6:5a39bde2e016 198 f3=true;
backman 6:5a39bde2e016 199 }
backman 6:5a39bde2e016 200
backman 6:5a39bde2e016 201 }
backman 12:418e39749f48 202 if(f3==true&&f4==false&&sign_line_imageR[j]=='O') {
backman 6:5a39bde2e016 203 if(f4==false) {
backman 6:5a39bde2e016 204 b2_start=j-1;
backman 6:5a39bde2e016 205 f4=true;
backman 6:5a39bde2e016 206 }
backman 6:5a39bde2e016 207
backman 6:5a39bde2e016 208 }
backman 6:5a39bde2e016 209 break;
TonyLin 17:af867c7512bb 210
backman 6:5a39bde2e016 211 case 0x02:
backman 12:418e39749f48 212 if(sign_line_imageR[i]=='O') {
backman 6:5a39bde2e016 213 if(f1==false) {
backman 6:5a39bde2e016 214 b_end=i;
backman 6:5a39bde2e016 215 f1=true;
backman 6:5a39bde2e016 216 }
backman 6:5a39bde2e016 217 }
backman 12:418e39749f48 218 if(sign_line_imageR[j]=='O') {
backman 6:5a39bde2e016 219 if(f2==false) {
backman 6:5a39bde2e016 220 b_start=j;
backman 6:5a39bde2e016 221 f2=true;
backman 6:5a39bde2e016 222 }
backman 6:5a39bde2e016 223 }
backman 6:5a39bde2e016 224 break;
backman 6:5a39bde2e016 225 }
backman 6:5a39bde2e016 226 }
backman 6:5a39bde2e016 227
backman 12:418e39749f48 228 b_w=b_start-b_end;
backman 12:418e39749f48 229 b2_w=b2_start-b2_end;
TonyLin 17:af867c7512bb 230
backman 12:418e39749f48 231 de_v=b_start;
TonyLin 17:af867c7512bb 232 de_v2=b_end;
backman 6:5a39bde2e016 233
backman 6:5a39bde2e016 234 switch(find_type) {
backman 6:5a39bde2e016 235 case 0x01:
TonyLin 17:af867c7512bb 236 b_center=(b_end+b_start)/2;
TonyLin 17:af867c7512bb 237 b2_center=(b2_end+b2_start)/2;
TonyLin 17:af867c7512bb 238 if(w_thr_up>b_w&&(b_center!=0)&&(b_center-64)<(64-b2_center))
TonyLin 17:af867c7512bb 239 center=b_center;
TonyLin 17:af867c7512bb 240 else
TonyLin 17:af867c7512bb 241 center=b2_center;
TonyLin 17:af867c7512bb 242 /* if( ( w_thr_up- (b_w))>0 &&( ( w_thr_up- (b_w)) < (w_thr_up-(b2_w)) ) ) {
TonyLin 17:af867c7512bb 243 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 244
backman 12:418e39749f48 245
backman 12:418e39749f48 246
TonyLin 17:af867c7512bb 247 // } else if( ( w_thr_up- (b2_w) )>0 ) {
TonyLin 17:af867c7512bb 248 center=(b2_end+b2_start)/2;
TonyLin 17:af867c7512bb 249 // } else {
backman 12:418e39749f48 250
TonyLin 17:af867c7512bb 251 center=65;
backman 12:418e39749f48 252
TonyLin 17:af867c7512bb 253 //????????????????
backman 12:418e39749f48 254
TonyLin 17:af867c7512bb 255 }
backman 12:418e39749f48 256
TonyLin 17:af867c7512bb 257 */
backman 6:5a39bde2e016 258 break;
backman 6:5a39bde2e016 259
backman 6:5a39bde2e016 260 case 0x02:
backman 6:5a39bde2e016 261 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 262 break;
backman 6:5a39bde2e016 263 }
backman 3:c5f2281b3ed2 264 return center;
backman 12:418e39749f48 265 }
backman 6:5a39bde2e016 266
backman 6:5a39bde2e016 267 void BX_camera::read(void)
backman 6:5a39bde2e016 268 {
backman 6:5a39bde2e016 269
backman 6:5a39bde2e016 270 w_f_vL=0x0000;
backman 6:5a39bde2e016 271 b_f_vL=0xffff;
backman 6:5a39bde2e016 272 w_f_vR=0x0000;
backman 6:5a39bde2e016 273 b_f_vR=0xffff;
backman 6:5a39bde2e016 274
backman 6:5a39bde2e016 275 *si=1;
backman 6:5a39bde2e016 276 *cam_clk=1;
backman 6:5a39bde2e016 277
backman 6:5a39bde2e016 278 wait_us(30); // tune here
backman 6:5a39bde2e016 279 *si=0;
backman 6:5a39bde2e016 280 *cam_clk=0;
backman 6:5a39bde2e016 281
backman 6:5a39bde2e016 282 line_CamR->enable();
backman 6:5a39bde2e016 283 line_CamL->enable();
TonyLin 17:af867c7512bb 284
backman 6:5a39bde2e016 285 //input 128 //both
backman 6:5a39bde2e016 286 for(int i=0; i<128; i++) {
backman 6:5a39bde2e016 287 *cam_clk=1;
backman 6:5a39bde2e016 288 wait_us(5);
backman 6:5a39bde2e016 289
backman 6:5a39bde2e016 290 line_imageR[i]=line_CamR->read_u16();
backman 6:5a39bde2e016 291 line_imageL[i]=line_CamL->read_u16();
backman 4:05b21dbca0c7 292
backman 6:5a39bde2e016 293 // big small
backman 6:5a39bde2e016 294 if(line_imageR[i] > w_f_vR)
backman 6:5a39bde2e016 295 w_f_vR=line_imageR[i];
backman 6:5a39bde2e016 296 else if(line_imageR[i] < b_f_vR )
backman 6:5a39bde2e016 297 b_f_vR = line_imageR[i];
backman 6:5a39bde2e016 298
backman 6:5a39bde2e016 299
backman 6:5a39bde2e016 300 if(line_imageL[i] > w_f_vL)
backman 6:5a39bde2e016 301 w_f_vL=line_imageL[i];
backman 6:5a39bde2e016 302 else if(line_imageL[i] < b_f_vL )
backman 6:5a39bde2e016 303 b_f_vL = line_imageL[i];
backman 6:5a39bde2e016 304
backman 6:5a39bde2e016 305 *cam_clk=0;
backman 6:5a39bde2e016 306 wait_us(5);
backman 6:5a39bde2e016 307 }
backman 6:5a39bde2e016 308
backman 6:5a39bde2e016 309 line_CamR->enable();
backman 6:5a39bde2e016 310 line_CamL->enable();
backman 6:5a39bde2e016 311
backman 6:5a39bde2e016 312 //filter L R //may change
backman 6:5a39bde2e016 313 for(int i=0; i<128; i++) {
backman 6:5a39bde2e016 314 if( (line_imageR[i]-b_f_vR) < (w_f_vR - line_imageR[i] ) )
backman 6:5a39bde2e016 315 sign_line_imageR[i]=' ';
backman 6:5a39bde2e016 316 else
backman 6:5a39bde2e016 317 sign_line_imageR[i]='O';
backman 6:5a39bde2e016 318
backman 6:5a39bde2e016 319 if( (line_imageL[i]-b_f_vL) < (w_f_vL - line_imageL[i] ) )
backman 6:5a39bde2e016 320 sign_line_imageL[i]=' ';
backman 6:5a39bde2e016 321 else
backman 6:5a39bde2e016 322 sign_line_imageL[i]='O';
backman 6:5a39bde2e016 323
backman 6:5a39bde2e016 324 if(i==0) {
backman 6:5a39bde2e016 325 sign_line_imageR[i]='X';
backman 6:5a39bde2e016 326 sign_line_imageL[i]='X';
backman 6:5a39bde2e016 327 }
backman 6:5a39bde2e016 328 }
backman 6:5a39bde2e016 329 }