QQQ

Dependencies:   mbed-rtos mbed

Fork of BX-car_s by Tony Lin

Committer:
TonyLin
Date:
Sun Jun 29 14:02:37 2014 +0000
Revision:
19:eb0552a0ddae
Parent:
18:88b083db7491
Child:
20:4ed21397e775
new;

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