Clark Lin
/
BX-car_s
QQQ
Fork of BX-car_s by
camera_api.cpp@21:5f7efc1ca8ad, 2014-06-30 (annotated)
- Committer:
- physicsgood
- Date:
- Mon Jun 30 08:19:43 2014 +0000
- Revision:
- 21:5f7efc1ca8ad
- Parent:
- 20:4ed21397e775
- Child:
- 22:1464a3f0a290
140630
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" |
TonyLin | 20:4ed21397e775 | 3 | |
backman | 1:82bc25a7b68b | 4 | #define clk 2 //ms |
TonyLin | 20:4ed21397e775 | 5 | |
TonyLin | 20:4ed21397e775 | 6 | BX_camera::BX_camera(int p) |
backman | 6:5a39bde2e016 | 7 | { |
TonyLin | 20:4ed21397e775 | 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); |
TonyLin | 20:4ed21397e775 | 13 | padding = p; |
backman | 6:5a39bde2e016 | 14 | } |
physicsgood | 21:5f7efc1ca8ad | 15 | int BX_camera::black_center(void) |
physicsgood | 21:5f7efc1ca8ad | 16 | { |
physicsgood | 21:5f7efc1ca8ad | 17 | int black_L_right = 96 , black_R_left = 32; |
physicsgood | 21:5f7efc1ca8ad | 18 | |
physicsgood | 21:5f7efc1ca8ad | 19 | for(int i = 10; i <118; i++){ |
physicsgood | 21:5f7efc1ca8ad | 20 | if(sign_line_imageR[i] == 'O' && sign_line_imageR[i+1] == ' ' && sign_line_imageR[i+2] == ' '){ |
physicsgood | 21:5f7efc1ca8ad | 21 | black_R_left = i; |
physicsgood | 21:5f7efc1ca8ad | 22 | break; |
physicsgood | 21:5f7efc1ca8ad | 23 | } |
physicsgood | 21:5f7efc1ca8ad | 24 | } |
physicsgood | 21:5f7efc1ca8ad | 25 | |
physicsgood | 21:5f7efc1ca8ad | 26 | for(int i = 118; i >= 10; i--){ |
physicsgood | 21:5f7efc1ca8ad | 27 | if(sign_line_imageR[i] == 'O' && sign_line_imageR[i-1] == ' ' && sign_line_imageR[i-2] == ' '){ |
physicsgood | 21:5f7efc1ca8ad | 28 | black_L_right = i; |
physicsgood | 21:5f7efc1ca8ad | 29 | break; |
physicsgood | 21:5f7efc1ca8ad | 30 | } |
physicsgood | 21:5f7efc1ca8ad | 31 | } |
physicsgood | 21:5f7efc1ca8ad | 32 | |
physicsgood | 21:5f7efc1ca8ad | 33 | return (black_R_left + black_L_right) / 2; |
physicsgood | 21:5f7efc1ca8ad | 34 | } |
TonyLin | 20:4ed21397e775 | 35 | |
backman | 6:5a39bde2e016 | 36 | int BX_camera::black_centerR(void) |
backman | 6:5a39bde2e016 | 37 | { |
TonyLin | 20:4ed21397e775 | 38 | |
backman | 7:fd976e1ced33 | 39 | int l_care=10; |
backman | 7:fd976e1ced33 | 40 | int r_care=118; |
TonyLin | 20:4ed21397e775 | 41 | int b_start=0; |
TonyLin | 20:4ed21397e775 | 42 | int b_end=0; |
TonyLin | 20:4ed21397e775 | 43 | bool l_f1=false; |
TonyLin | 20:4ed21397e775 | 44 | bool l_f2=false; |
TonyLin | 20:4ed21397e775 | 45 | bool find=false; |
TonyLin | 20:4ed21397e775 | 46 | int b_thr_up=32; |
TonyLin | 20:4ed21397e775 | 47 | int b_thr_dn=5; |
backman | 6:5a39bde2e016 | 48 | int b_w=0; |
TonyLin | 19:eb0552a0ddae | 49 | |
TonyLin | 20:4ed21397e775 | 50 | for(int i=r_care;i>l_care;i--){ |
TonyLin | 20:4ed21397e775 | 51 | |
TonyLin | 20:4ed21397e775 | 52 | |
TonyLin | 20:4ed21397e775 | 53 | if(l_f1==false&&sign_line_imageR[i]==' '){ |
TonyLin | 20:4ed21397e775 | 54 | |
TonyLin | 20:4ed21397e775 | 55 | b_start=i; |
TonyLin | 20:4ed21397e775 | 56 | l_f1=true; |
TonyLin | 20:4ed21397e775 | 57 | } |
TonyLin | 20:4ed21397e775 | 58 | if(l_f1==true && sign_line_imageR[i]=='O'){ |
TonyLin | 20:4ed21397e775 | 59 | b_end=i-1; |
TonyLin | 20:4ed21397e775 | 60 | l_f2=true; |
TonyLin | 20:4ed21397e775 | 61 | } |
TonyLin | 20:4ed21397e775 | 62 | |
TonyLin | 20:4ed21397e775 | 63 | if(l_f1==true && l_f2== true){ |
TonyLin | 20:4ed21397e775 | 64 | b_w=b_start-b_end; |
TonyLin | 20:4ed21397e775 | 65 | if( b_thr_up>b_w&&b_w> b_thr_dn){ |
TonyLin | 20:4ed21397e775 | 66 | |
TonyLin | 20:4ed21397e775 | 67 | find=true; |
TonyLin | 20:4ed21397e775 | 68 | break; |
backman | 6:5a39bde2e016 | 69 | } |
TonyLin | 20:4ed21397e775 | 70 | else{ |
TonyLin | 20:4ed21397e775 | 71 | l_f1=false; |
TonyLin | 20:4ed21397e775 | 72 | l_f2=false; |
TonyLin | 20:4ed21397e775 | 73 | |
TonyLin | 20:4ed21397e775 | 74 | } |
TonyLin | 17:af867c7512bb | 75 | |
TonyLin | 20:4ed21397e775 | 76 | |
TonyLin | 20:4ed21397e775 | 77 | } |
TonyLin | 20:4ed21397e775 | 78 | |
TonyLin | 20:4ed21397e775 | 79 | |
backman | 6:5a39bde2e016 | 80 | } |
TonyLin | 20:4ed21397e775 | 81 | |
TonyLin | 20:4ed21397e775 | 82 | |
TonyLin | 20:4ed21397e775 | 83 | |
TonyLin | 20:4ed21397e775 | 84 | |
TonyLin | 20:4ed21397e775 | 85 | |
TonyLin | 20:4ed21397e775 | 86 | if(find) |
TonyLin | 20:4ed21397e775 | 87 | return (b_start+b_end)/2; |
TonyLin | 20:4ed21397e775 | 88 | else |
TonyLin | 20:4ed21397e775 | 89 | return -1; |
TonyLin | 20:4ed21397e775 | 90 | } |
TonyLin | 20:4ed21397e775 | 91 | |
TonyLin | 20:4ed21397e775 | 92 | |
TonyLin | 20:4ed21397e775 | 93 | |
TonyLin | 20:4ed21397e775 | 94 | |
TonyLin | 20:4ed21397e775 | 95 | |
backman | 6:5a39bde2e016 | 96 | void BX_camera::read(void) |
backman | 6:5a39bde2e016 | 97 | { |
TonyLin | 20:4ed21397e775 | 98 | |
backman | 6:5a39bde2e016 | 99 | w_f_vL=0x0000; |
backman | 6:5a39bde2e016 | 100 | b_f_vL=0xffff; |
TonyLin | 20:4ed21397e775 | 101 | |
backman | 6:5a39bde2e016 | 102 | w_f_vR=0x0000; |
backman | 6:5a39bde2e016 | 103 | b_f_vR=0xffff; |
TonyLin | 20:4ed21397e775 | 104 | |
TonyLin | 20:4ed21397e775 | 105 | |
TonyLin | 20:4ed21397e775 | 106 | |
TonyLin | 20:4ed21397e775 | 107 | |
backman | 6:5a39bde2e016 | 108 | *si=1; |
backman | 6:5a39bde2e016 | 109 | *cam_clk=1; |
TonyLin | 20:4ed21397e775 | 110 | |
backman | 6:5a39bde2e016 | 111 | wait_us(30); // tune here |
backman | 6:5a39bde2e016 | 112 | *si=0; |
backman | 6:5a39bde2e016 | 113 | *cam_clk=0; |
TonyLin | 20:4ed21397e775 | 114 | |
TonyLin | 20:4ed21397e775 | 115 | |
TonyLin | 20:4ed21397e775 | 116 | |
backman | 6:5a39bde2e016 | 117 | line_CamR->enable(); |
backman | 6:5a39bde2e016 | 118 | line_CamL->enable(); |
TonyLin | 20:4ed21397e775 | 119 | |
TonyLin | 20:4ed21397e775 | 120 | |
backman | 6:5a39bde2e016 | 121 | //input 128 //both |
TonyLin | 20:4ed21397e775 | 122 | |
backman | 6:5a39bde2e016 | 123 | for(int i=0; i<128; i++) { |
backman | 6:5a39bde2e016 | 124 | *cam_clk=1; |
backman | 6:5a39bde2e016 | 125 | wait_us(5); |
TonyLin | 20:4ed21397e775 | 126 | |
TonyLin | 20:4ed21397e775 | 127 | |
backman | 6:5a39bde2e016 | 128 | line_imageR[i]=line_CamR->read_u16(); |
backman | 6:5a39bde2e016 | 129 | line_imageL[i]=line_CamL->read_u16(); |
TonyLin | 20:4ed21397e775 | 130 | |
backman | 6:5a39bde2e016 | 131 | // big small |
backman | 6:5a39bde2e016 | 132 | if(line_imageR[i] > w_f_vR) |
backman | 6:5a39bde2e016 | 133 | w_f_vR=line_imageR[i]; |
backman | 6:5a39bde2e016 | 134 | else if(line_imageR[i] < b_f_vR ) |
backman | 6:5a39bde2e016 | 135 | b_f_vR = line_imageR[i]; |
TonyLin | 20:4ed21397e775 | 136 | |
TonyLin | 20:4ed21397e775 | 137 | |
backman | 6:5a39bde2e016 | 138 | if(line_imageL[i] > w_f_vL) |
backman | 6:5a39bde2e016 | 139 | w_f_vL=line_imageL[i]; |
backman | 6:5a39bde2e016 | 140 | else if(line_imageL[i] < b_f_vL ) |
backman | 6:5a39bde2e016 | 141 | b_f_vL = line_imageL[i]; |
TonyLin | 20:4ed21397e775 | 142 | |
TonyLin | 20:4ed21397e775 | 143 | |
TonyLin | 20:4ed21397e775 | 144 | |
TonyLin | 20:4ed21397e775 | 145 | |
backman | 6:5a39bde2e016 | 146 | *cam_clk=0; |
backman | 6:5a39bde2e016 | 147 | wait_us(5); |
TonyLin | 20:4ed21397e775 | 148 | |
TonyLin | 20:4ed21397e775 | 149 | |
backman | 6:5a39bde2e016 | 150 | } |
TonyLin | 20:4ed21397e775 | 151 | |
TonyLin | 20:4ed21397e775 | 152 | |
backman | 6:5a39bde2e016 | 153 | line_CamR->enable(); |
backman | 6:5a39bde2e016 | 154 | line_CamL->enable(); |
TonyLin | 20:4ed21397e775 | 155 | |
TonyLin | 20:4ed21397e775 | 156 | |
backman | 6:5a39bde2e016 | 157 | //filter L R //may change |
TonyLin | 20:4ed21397e775 | 158 | |
backman | 6:5a39bde2e016 | 159 | for(int i=0; i<128; i++) { |
TonyLin | 20:4ed21397e775 | 160 | |
TonyLin | 20:4ed21397e775 | 161 | |
backman | 6:5a39bde2e016 | 162 | if( (line_imageR[i]-b_f_vR) < (w_f_vR - line_imageR[i] ) ) |
backman | 6:5a39bde2e016 | 163 | sign_line_imageR[i]=' '; |
backman | 6:5a39bde2e016 | 164 | else |
backman | 6:5a39bde2e016 | 165 | sign_line_imageR[i]='O'; |
TonyLin | 20:4ed21397e775 | 166 | |
TonyLin | 20:4ed21397e775 | 167 | |
backman | 6:5a39bde2e016 | 168 | if( (line_imageL[i]-b_f_vL) < (w_f_vL - line_imageL[i] ) ) |
backman | 6:5a39bde2e016 | 169 | sign_line_imageL[i]=' '; |
backman | 6:5a39bde2e016 | 170 | else |
backman | 6:5a39bde2e016 | 171 | sign_line_imageL[i]='O'; |
TonyLin | 20:4ed21397e775 | 172 | |
TonyLin | 20:4ed21397e775 | 173 | |
TonyLin | 20:4ed21397e775 | 174 | |
TonyLin | 20:4ed21397e775 | 175 | |
TonyLin | 20:4ed21397e775 | 176 | |
backman | 6:5a39bde2e016 | 177 | if(i==0) { |
backman | 6:5a39bde2e016 | 178 | sign_line_imageR[i]='X'; |
backman | 6:5a39bde2e016 | 179 | sign_line_imageL[i]='X'; |
backman | 6:5a39bde2e016 | 180 | } |
TonyLin | 20:4ed21397e775 | 181 | |
TonyLin | 20:4ed21397e775 | 182 | |
backman | 6:5a39bde2e016 | 183 | } |
TonyLin | 20:4ed21397e775 | 184 | |
TonyLin | 20:4ed21397e775 | 185 | |
TonyLin | 20:4ed21397e775 | 186 | |
backman | 6:5a39bde2e016 | 187 | } |