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