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