good

Dependencies:   mbed

Fork of BX-car by Clark Lin

Committer:
even
Date:
Wed Jun 25 05:59:29 2014 +0000
Revision:
11:ffd762ae141b
Parent:
10:d2401a243e8d
1st PID complete

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"
physicsgood 10:d2401a243e8d 3 #define clk 2 //ms
backman 1:82bc25a7b68b 4
physicsgood 10:d2401a243e8d 5
physicsgood 10:d2401a243e8d 6
backman 6:5a39bde2e016 7
backman 6:5a39bde2e016 8 BX_camera::BX_camera(void)
backman 6:5a39bde2e016 9 {
backman 6:5a39bde2e016 10
backman 6:5a39bde2e016 11 line_CamR = new FastAnalogIn(PTD5);
backman 6:5a39bde2e016 12 line_CamL= new FastAnalogIn(PTD6,0);
backman 6:5a39bde2e016 13 cam_clk=new DigitalOut(PTE1);
backman 6:5a39bde2e016 14 si=new DigitalOut(PTD7);
backman 6:5a39bde2e016 15
backman 6:5a39bde2e016 16 }
backman 6:5a39bde2e016 17
backman 6:5a39bde2e016 18 int BX_camera::black_centerR(void)
backman 6:5a39bde2e016 19 {
backman 6:5a39bde2e016 20
physicsgood 10:d2401a243e8d 21 int black_left , black_right;
physicsgood 10:d2401a243e8d 22
physicsgood 10:d2401a243e8d 23 for(int i = 10; i < 115; i++){
physicsgood 10:d2401a243e8d 24 if(sign_line_imageR[i] == 'O' && sign_line_imageR[i+1] == ' ' && sign_line_imageR[i+2] == ' '){
physicsgood 10:d2401a243e8d 25 black_left = i;
physicsgood 10:d2401a243e8d 26 break;
physicsgood 10:d2401a243e8d 27 }
physicsgood 10:d2401a243e8d 28 }
physicsgood 10:d2401a243e8d 29
physicsgood 10:d2401a243e8d 30 for(int i = 115; i > 10; i--){
physicsgood 10:d2401a243e8d 31 if(sign_line_imageR[i] == ' ' && sign_line_imageR[i+1] == ' ' && sign_line_imageR[i+2] == ' '){
physicsgood 10:d2401a243e8d 32 black_right = i+2;
physicsgood 10:d2401a243e8d 33 break;
physicsgood 10:d2401a243e8d 34 }
physicsgood 10:d2401a243e8d 35 }
physicsgood 10:d2401a243e8d 36
physicsgood 10:d2401a243e8d 37 return (black_left + black_right) / 2;
backman 6:5a39bde2e016 38
backman 6:5a39bde2e016 39
backman 7:fd976e1ced33 40
backman 6:5a39bde2e016 41 }
backman 6:5a39bde2e016 42
backman 6:5a39bde2e016 43
backman 3:c5f2281b3ed2 44
backman 3:c5f2281b3ed2 45
backman 6:5a39bde2e016 46 int BX_camera::black_centerL(void)
backman 6:5a39bde2e016 47 {
backman 6:5a39bde2e016 48
backman 6:5a39bde2e016 49 // find center
backman 6:5a39bde2e016 50 // case 1 // | //
backman 6:5a39bde2e016 51 //case 2 / | /
backman 6:5a39bde2e016 52
backman 6:5a39bde2e016 53
backman 6:5a39bde2e016 54 char find_type=0x00;
backman 6:5a39bde2e016 55
backman 6:5a39bde2e016 56 int b_end=0;
backman 6:5a39bde2e016 57 int b_start=0;
backman 6:5a39bde2e016 58 int b2_end=0;
backman 6:5a39bde2e016 59 int b2_start=0;
backman 6:5a39bde2e016 60
backman 6:5a39bde2e016 61
backman 6:5a39bde2e016 62 int center;
backman 6:5a39bde2e016 63 int j=64;
backman 6:5a39bde2e016 64 bool f1=false;
backman 6:5a39bde2e016 65 bool f2=false;
backman 6:5a39bde2e016 66 bool f3=false;
backman 6:5a39bde2e016 67 bool f4=false;
backman 6:5a39bde2e016 68
backman 6:5a39bde2e016 69 if(sign_line_imageL[64]==' ')
backman 6:5a39bde2e016 70 find_type=0x02;
backman 6:5a39bde2e016 71 else
backman 6:5a39bde2e016 72 find_type=0x01;
backman 6:5a39bde2e016 73
backman 6:5a39bde2e016 74
backman 6:5a39bde2e016 75 for(int i=64; i<128; i++,j--) {
backman 6:5a39bde2e016 76
backman 6:5a39bde2e016 77
backman 6:5a39bde2e016 78 switch(find_type) {
backman 6:5a39bde2e016 79
backman 6:5a39bde2e016 80
backman 6:5a39bde2e016 81 case 0x01:
backman 6:5a39bde2e016 82
backman 6:5a39bde2e016 83 if(f1==false&&sign_line_imageL[i]==' ') {
backman 6:5a39bde2e016 84 if(f1==false) {
backman 6:5a39bde2e016 85 b_start=i;
backman 6:5a39bde2e016 86 f1=true;
backman 6:5a39bde2e016 87 }
backman 6:5a39bde2e016 88
backman 6:5a39bde2e016 89 }
backman 6:5a39bde2e016 90 if(f1== true&& f2==false&&sign_line_imageL[i]=='O') {
backman 6:5a39bde2e016 91 if(f2==false) {
backman 6:5a39bde2e016 92 b_end=i-1;
backman 6:5a39bde2e016 93 f2=true;
backman 6:5a39bde2e016 94 }
backman 6:5a39bde2e016 95
backman 6:5a39bde2e016 96 }
backman 4:05b21dbca0c7 97
backman 6:5a39bde2e016 98 if(f3==false&&sign_line_imageL[j]==' ') {
backman 6:5a39bde2e016 99 if(f3==false) {
backman 6:5a39bde2e016 100 b2_end=j;
backman 6:5a39bde2e016 101 f3=true;
backman 6:5a39bde2e016 102 }
backman 6:5a39bde2e016 103
backman 6:5a39bde2e016 104 }
backman 6:5a39bde2e016 105 if(f3==true&&f4==false&&sign_line_imageL[j]=='O') {
backman 6:5a39bde2e016 106 if(f4==false) {
backman 6:5a39bde2e016 107 b2_start=j-1;
backman 6:5a39bde2e016 108 f4=true;
backman 6:5a39bde2e016 109 }
backman 6:5a39bde2e016 110
backman 6:5a39bde2e016 111 }
backman 6:5a39bde2e016 112
backman 6:5a39bde2e016 113 break;
backman 6:5a39bde2e016 114
backman 6:5a39bde2e016 115
backman 6:5a39bde2e016 116 case 0x02:
backman 6:5a39bde2e016 117
backman 6:5a39bde2e016 118 if(sign_line_imageL[i]=='O') {
backman 6:5a39bde2e016 119
backman 6:5a39bde2e016 120 if(f1==false) {
backman 6:5a39bde2e016 121 b_end=i;
backman 6:5a39bde2e016 122 f1=true;
backman 6:5a39bde2e016 123 }
backman 6:5a39bde2e016 124 }
backman 6:5a39bde2e016 125
backman 6:5a39bde2e016 126 if(sign_line_imageL[j]=='O') {
backman 6:5a39bde2e016 127
backman 6:5a39bde2e016 128 if(f2==false) {
backman 6:5a39bde2e016 129 b_start=j;
backman 6:5a39bde2e016 130 f2=true;
backman 6:5a39bde2e016 131 }
backman 6:5a39bde2e016 132
backman 6:5a39bde2e016 133 }
backman 6:5a39bde2e016 134
backman 6:5a39bde2e016 135
backman 6:5a39bde2e016 136 break;
backman 6:5a39bde2e016 137
backman 6:5a39bde2e016 138 }
backman 6:5a39bde2e016 139
backman 6:5a39bde2e016 140 }
backman 6:5a39bde2e016 141
backman 6:5a39bde2e016 142
backman 6:5a39bde2e016 143 switch(find_type) {
backman 6:5a39bde2e016 144
backman 6:5a39bde2e016 145 case 0x01:
backman 6:5a39bde2e016 146
backman 6:5a39bde2e016 147 if((b_end-b_start)>(b2_end-b2_start) )
backman 6:5a39bde2e016 148 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 149
backman 3:c5f2281b3ed2 150 else
backman 6:5a39bde2e016 151 center=(b2_end+b2_start)/2;
backman 6:5a39bde2e016 152 break;
backman 6:5a39bde2e016 153
backman 6:5a39bde2e016 154 case 0x02:
backman 6:5a39bde2e016 155 center=(b_end+b_start)/2;
backman 6:5a39bde2e016 156
backman 6:5a39bde2e016 157 break;
backman 6:5a39bde2e016 158 }
backman 6:5a39bde2e016 159
backman 3:c5f2281b3ed2 160 return center;
backman 6:5a39bde2e016 161
backman 6:5a39bde2e016 162 }
backman 6:5a39bde2e016 163
backman 6:5a39bde2e016 164
backman 6:5a39bde2e016 165
backman 6:5a39bde2e016 166 void BX_camera::read(void)
backman 6:5a39bde2e016 167 {
backman 6:5a39bde2e016 168
backman 6:5a39bde2e016 169 w_f_vL=0x0000;
backman 6:5a39bde2e016 170 b_f_vL=0xffff;
backman 6:5a39bde2e016 171
backman 6:5a39bde2e016 172 w_f_vR=0x0000;
backman 6:5a39bde2e016 173 b_f_vR=0xffff;
backman 6:5a39bde2e016 174
backman 6:5a39bde2e016 175
backman 6:5a39bde2e016 176
backman 6:5a39bde2e016 177
backman 6:5a39bde2e016 178 *si=1;
backman 6:5a39bde2e016 179 *cam_clk=1;
backman 6:5a39bde2e016 180
backman 6:5a39bde2e016 181 wait_us(30); // tune here
backman 6:5a39bde2e016 182 *si=0;
backman 6:5a39bde2e016 183 *cam_clk=0;
backman 6:5a39bde2e016 184
backman 6:5a39bde2e016 185
backman 6:5a39bde2e016 186
backman 6:5a39bde2e016 187 line_CamR->enable();
backman 6:5a39bde2e016 188 line_CamL->enable();
backman 6:5a39bde2e016 189
backman 6:5a39bde2e016 190
backman 6:5a39bde2e016 191 //input 128 //both
backman 6:5a39bde2e016 192
physicsgood 10:d2401a243e8d 193 for(int i=127; i>=0; i--) {
backman 6:5a39bde2e016 194 *cam_clk=1;
backman 6:5a39bde2e016 195 wait_us(5);
backman 6:5a39bde2e016 196
backman 6:5a39bde2e016 197
backman 6:5a39bde2e016 198 line_imageR[i]=line_CamR->read_u16();
backman 6:5a39bde2e016 199 line_imageL[i]=line_CamL->read_u16();
backman 4:05b21dbca0c7 200
backman 6:5a39bde2e016 201 // big small
backman 6:5a39bde2e016 202 if(line_imageR[i] > w_f_vR)
backman 6:5a39bde2e016 203 w_f_vR=line_imageR[i];
backman 6:5a39bde2e016 204 else if(line_imageR[i] < b_f_vR )
backman 6:5a39bde2e016 205 b_f_vR = line_imageR[i];
backman 6:5a39bde2e016 206
backman 6:5a39bde2e016 207
backman 6:5a39bde2e016 208 if(line_imageL[i] > w_f_vL)
backman 6:5a39bde2e016 209 w_f_vL=line_imageL[i];
backman 6:5a39bde2e016 210 else if(line_imageL[i] < b_f_vL )
backman 6:5a39bde2e016 211 b_f_vL = line_imageL[i];
backman 6:5a39bde2e016 212
backman 6:5a39bde2e016 213
backman 6:5a39bde2e016 214
backman 6:5a39bde2e016 215
backman 6:5a39bde2e016 216 *cam_clk=0;
backman 6:5a39bde2e016 217 wait_us(5);
backman 6:5a39bde2e016 218
backman 6:5a39bde2e016 219
backman 6:5a39bde2e016 220 }
backman 6:5a39bde2e016 221
backman 6:5a39bde2e016 222
backman 6:5a39bde2e016 223 line_CamR->enable();
backman 6:5a39bde2e016 224 line_CamL->enable();
backman 6:5a39bde2e016 225
backman 6:5a39bde2e016 226
backman 6:5a39bde2e016 227 //filter L R //may change
backman 6:5a39bde2e016 228
physicsgood 10:d2401a243e8d 229 for(int i=127; i>=0; i--) {
backman 6:5a39bde2e016 230
backman 6:5a39bde2e016 231
backman 6:5a39bde2e016 232 if( (line_imageR[i]-b_f_vR) < (w_f_vR - line_imageR[i] ) )
backman 6:5a39bde2e016 233 sign_line_imageR[i]=' ';
backman 6:5a39bde2e016 234 else
backman 6:5a39bde2e016 235 sign_line_imageR[i]='O';
backman 6:5a39bde2e016 236
backman 6:5a39bde2e016 237
backman 6:5a39bde2e016 238 if( (line_imageL[i]-b_f_vL) < (w_f_vL - line_imageL[i] ) )
backman 6:5a39bde2e016 239 sign_line_imageL[i]=' ';
backman 6:5a39bde2e016 240 else
backman 6:5a39bde2e016 241 sign_line_imageL[i]='O';
backman 6:5a39bde2e016 242
backman 6:5a39bde2e016 243
backman 6:5a39bde2e016 244
backman 6:5a39bde2e016 245
backman 6:5a39bde2e016 246
backman 6:5a39bde2e016 247 if(i==0) {
backman 6:5a39bde2e016 248 sign_line_imageR[i]='X';
backman 6:5a39bde2e016 249 sign_line_imageL[i]='X';
backman 6:5a39bde2e016 250 }
backman 6:5a39bde2e016 251
backman 6:5a39bde2e016 252
backman 6:5a39bde2e016 253 }
backman 6:5a39bde2e016 254
backman 6:5a39bde2e016 255
backman 6:5a39bde2e016 256
backman 6:5a39bde2e016 257 }