Dependencies: mbed
main.cpp@7:fae874e898b3, 2019-12-04 (annotated)
- Committer:
- YUPPY
- Date:
- Wed Dec 04 11:49:44 2019 +0000
- Revision:
- 7:fae874e898b3
- Parent:
- 6:db1a62608047
- Child:
- 8:765a73e21907
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YUPPY | 0:6212b283430c | 1 | #define cansatB |
YUPPY | 7:fae874e898b3 | 2 | #include "mbed.h" |
YUPPY | 7:fae874e898b3 | 3 | //mbed |
YUPPY | 7:fae874e898b3 | 4 | #include "getGPS.h" |
YUPPY | 7:fae874e898b3 | 5 | #include "math.h" |
YUPPY | 7:fae874e898b3 | 6 | //GPS |
YUPPY | 7:fae874e898b3 | 7 | #include "TB6612.h" |
YUPPY | 7:fae874e898b3 | 8 | //motorDriver |
YUPPY | 7:fae874e898b3 | 9 | #include "JPEGCamera.h" |
YUPPY | 7:fae874e898b3 | 10 | //カメラ |
YUPPY | 0:6212b283430c | 11 | #include <stdio.h> |
YUPPY | 7:fae874e898b3 | 12 | #include <base64.h> |
YUPPY | 7:fae874e898b3 | 13 | //XBee |
YUPPY | 7:fae874e898b3 | 14 | #include "us015.h" |
YUPPY | 7:fae874e898b3 | 15 | // 超音波センサ |
YUPPY | 7:fae874e898b3 | 16 | |
YUPPY | 7:fae874e898b3 | 17 | |
YUPPY | 2:e2b803e3bcbc | 18 | GPS gps (p28,p27); //GPS |
YUPPY | 4:1354e56c7dd3 | 19 | |
YUPPY | 2:e2b803e3bcbc | 20 | DigitalOut FET(p21); //FET |
YUPPY | 7:fae874e898b3 | 21 | DigitalOut myled(LED1); |
YUPPY | 7:fae874e898b3 | 22 | US015 hs(p12,p11); |
YUPPY | 7:fae874e898b3 | 23 | DigitalIn thermo(p20); |
YUPPY | 7:fae874e898b3 | 24 | DigitalOut Sb612switch(p15); //焦電スイッチ |
YUPPY | 4:1354e56c7dd3 | 25 | DigitalOut Ultra(p12); |
YUPPY | 7:fae874e898b3 | 26 | Serial pc(USBTX,USBRX); // tx, rx |
YUPPY | 7:fae874e898b3 | 27 | JPEGCamera camera(p9, p10); // TX, RX |
YUPPY | 7:fae874e898b3 | 28 | TB6612 left1(p25,p17,p16); //モーターピン |
YUPPY | 7:fae874e898b3 | 29 | TB6612 right1(p26,p19,p18); //モーターピン |
YUPPY | 7:fae874e898b3 | 30 | Serial xbee(p13,p14); //xbee |
YUPPY | 2:e2b803e3bcbc | 31 | |
YUPPY | 7:fae874e898b3 | 32 | |
YUPPY | 7:fae874e898b3 | 33 | |
YUPPY | 5:5aa7223226df | 34 | |
YUPPY | 7:fae874e898b3 | 35 | int main() |
YUPPY | 7:fae874e898b3 | 36 | { |
YUPPY | 7:fae874e898b3 | 37 | |
YUPPY | 7:fae874e898b3 | 38 | |
YUPPY | 7:fae874e898b3 | 39 | Sb612switch=0; //焦電off |
YUPPY | 7:fae874e898b3 | 40 | wait(1); |
YUPPY | 7:fae874e898b3 | 41 | Ultra=0;//超音波off |
YUPPY | 7:fae874e898b3 | 42 | |
YUPPY | 5:5aa7223226df | 43 | printf("CanSat-B_Start!\r\n"); |
YUPPY | 5:5aa7223226df | 44 | |
YUPPY | 7:fae874e898b3 | 45 | //FET |
YUPPY | 7:fae874e898b3 | 46 | |
YUPPY | 7:fae874e898b3 | 47 | FET=1; |
YUPPY | 7:fae874e898b3 | 48 | wait(10); |
YUPPY | 7:fae874e898b3 | 49 | FET=0; |
YUPPY | 7:fae874e898b3 | 50 | wait(10); |
YUPPY | 7:fae874e898b3 | 51 | |
YUPPY | 7:fae874e898b3 | 52 | |
YUPPY | 7:fae874e898b3 | 53 | |
YUPPY | 5:5aa7223226df | 54 | //以下GPS |
YUPPY | 2:e2b803e3bcbc | 55 | double a; |
YUPPY | 2:e2b803e3bcbc | 56 | double b; |
YUPPY | 2:e2b803e3bcbc | 57 | double distance; |
YUPPY | 2:e2b803e3bcbc | 58 | |
YUPPY | 2:e2b803e3bcbc | 59 | pc.printf("GPS Start\r\n"); |
YUPPY | 2:e2b803e3bcbc | 60 | |
YUPPY | 7:fae874e898b3 | 61 | while(1) |
YUPPY | 7:fae874e898b3 | 62 | { |
YUPPY | 7:fae874e898b3 | 63 | if(gps.getgps()) |
YUPPY | 7:fae874e898b3 | 64 | { |
YUPPY | 2:e2b803e3bcbc | 65 | a = gps.latitude; |
YUPPY | 2:e2b803e3bcbc | 66 | b = gps.longitude; |
YUPPY | 2:e2b803e3bcbc | 67 | |
YUPPY | 2:e2b803e3bcbc | 68 | pc.printf("(%lf,%lf)\r\n",gps.latitude,gps.longitude);//緯度と経度を表示 |
YUPPY | 2:e2b803e3bcbc | 69 | |
YUPPY | 2:e2b803e3bcbc | 70 | break; |
YUPPY | 2:e2b803e3bcbc | 71 | |
YUPPY | 2:e2b803e3bcbc | 72 | }else{ |
YUPPY | 7:fae874e898b3 | 73 | pc.printf("NO DATA\r\n");//データ取得失敗 |
YUPPY | 7:fae874e898b3 | 74 | wait(1); |
YUPPY | 7:fae874e898b3 | 75 | } |
YUPPY | 7:fae874e898b3 | 76 | } |
YUPPY | 7:fae874e898b3 | 77 | while(1) |
YUPPY | 7:fae874e898b3 | 78 | { |
YUPPY | 2:e2b803e3bcbc | 79 | if(gps.getgps()) { |
YUPPY | 2:e2b803e3bcbc | 80 | |
YUPPY | 2:e2b803e3bcbc | 81 | pc.printf("(%lf,%lf)\n\r",gps.latitude,gps.longitude);//緯度と経度を表示 |
YUPPY | 2:e2b803e3bcbc | 82 | |
YUPPY | 7:fae874e898b3 | 83 | // 球面三角法により、大円距離(メートル)を求める |
YUPPY | 7:fae874e898b3 | 84 | double c; |
YUPPY | 7:fae874e898b3 | 85 | double d; |
YUPPY | 7:fae874e898b3 | 86 | c = gps.latitude; |
YUPPY | 7:fae874e898b3 | 87 | d = gps.longitude; |
YUPPY | 2:e2b803e3bcbc | 88 | |
YUPPY | 7:fae874e898b3 | 89 | const double pi = 3.14159265359; // 円周率 |
YUPPY | 2:e2b803e3bcbc | 90 | |
YUPPY | 7:fae874e898b3 | 91 | double ra = a * pi / 180; |
YUPPY | 7:fae874e898b3 | 92 | double rb = b * pi / 180; // 緯度経度をラジアンに変換 |
YUPPY | 7:fae874e898b3 | 93 | double rc = c * pi / 180; |
YUPPY | 7:fae874e898b3 | 94 | double rd = d * pi / 180; |
YUPPY | 2:e2b803e3bcbc | 95 | |
YUPPY | 7:fae874e898b3 | 96 | double e = sin(ra) * sin(rc) + cos(ra) * cos(rc) * cos(rb - rd); // 2点の中心角(ラジアン)を求める |
YUPPY | 7:fae874e898b3 | 97 | double rr = acos(e); |
YUPPY | 2:e2b803e3bcbc | 98 | |
YUPPY | 7:fae874e898b3 | 99 | const double earth_radius = 6378140; // 地球赤道半径(m) |
YUPPY | 2:e2b803e3bcbc | 100 | |
YUPPY | 7:fae874e898b3 | 101 | distance = earth_radius * rr; // 2点間の距離(m) |
YUPPY | 2:e2b803e3bcbc | 102 | |
YUPPY | 2:e2b803e3bcbc | 103 | |
YUPPY | 2:e2b803e3bcbc | 104 | |
YUPPY | 2:e2b803e3bcbc | 105 | |
YUPPY | 2:e2b803e3bcbc | 106 | if (distance<5){ |
YUPPY | 2:e2b803e3bcbc | 107 | printf("%lf\r\n",distance); |
YUPPY | 7:fae874e898b3 | 108 | right1=100; |
YUPPY | 7:fae874e898b3 | 109 | left1=100; |
YUPPY | 2:e2b803e3bcbc | 110 | }else{ |
YUPPY | 7:fae874e898b3 | 111 | right1=0; |
YUPPY | 7:fae874e898b3 | 112 | left1=0; |
YUPPY | 2:e2b803e3bcbc | 113 | pc.printf("5m clear!"); |
YUPPY | 2:e2b803e3bcbc | 114 | break; |
YUPPY | 7:fae874e898b3 | 115 | } |
YUPPY | 2:e2b803e3bcbc | 116 | |
YUPPY | 7:fae874e898b3 | 117 | }else{ |
YUPPY | 2:e2b803e3bcbc | 118 | pc.printf("NO DATA\r\n");//データ取得失敗 |
YUPPY | 2:e2b803e3bcbc | 119 | wait(1); |
YUPPY | 2:e2b803e3bcbc | 120 | } |
YUPPY | 2:e2b803e3bcbc | 121 | } |
YUPPY | 5:5aa7223226df | 122 | //GPS End |
YUPPY | 5:5aa7223226df | 123 | |
YUPPY | 7:fae874e898b3 | 124 | int i=1; |
YUPPY | 4:1354e56c7dd3 | 125 | float th; |
YUPPY | 4:1354e56c7dd3 | 126 | Timer tm; |
YUPPY | 7:fae874e898b3 | 127 | for(i=0;i<3;i++){ |
YUPPY | 4:1354e56c7dd3 | 128 | pc.printf("start\r\n"); |
YUPPY | 6:db1a62608047 | 129 | |
YUPPY | 7:fae874e898b3 | 130 | left1 = 100; //左モーター100% |
YUPPY | 7:fae874e898b3 | 131 | right1 = 100;//右モーター100% |
YUPPY | 7:fae874e898b3 | 132 | printf("Restart\r\n" ); |
YUPPY | 7:fae874e898b3 | 133 | wait(4); |
YUPPY | 7:fae874e898b3 | 134 | left1=50; |
YUPPY | 7:fae874e898b3 | 135 | right1=50; |
YUPPY | 7:fae874e898b3 | 136 | wait(1); |
YUPPY | 7:fae874e898b3 | 137 | left1=0; |
YUPPY | 7:fae874e898b3 | 138 | right1=0; |
YUPPY | 7:fae874e898b3 | 139 | wait(1); |
YUPPY | 7:fae874e898b3 | 140 | printf("停止\n\r"); |
YUPPY | 7:fae874e898b3 | 141 | Sb612switch=0; //焦電off |
YUPPY | 7:fae874e898b3 | 142 | wait(1); |
YUPPY | 4:1354e56c7dd3 | 143 | Ultra=1;//超音波on |
YUPPY | 7:fae874e898b3 | 144 | wait(1); |
YUPPY | 5:5aa7223226df | 145 | |
YUPPY | 7:fae874e898b3 | 146 | while(1) { |
YUPPY | 7:fae874e898b3 | 147 | printf("超音波on\r\n 焦電off\r\n" ) ; |
YUPPY | 7:fae874e898b3 | 148 | hs.TrigerOut(); |
YUPPY | 5:5aa7223226df | 149 | wait(1); |
YUPPY | 5:5aa7223226df | 150 | int distance; |
YUPPY | 5:5aa7223226df | 151 | distance = hs.GetDistance(); |
YUPPY | 7:fae874e898b3 | 152 | printf("distance=%dmm\r\n",distance);//距離出力 |
YUPPY | 7:fae874e898b3 | 153 | |
YUPPY | 7:fae874e898b3 | 154 | if(distance<=2000){//超音波反応 |
YUPPY | 7:fae874e898b3 | 155 | |
YUPPY | 7:fae874e898b3 | 156 | Ultra=0;//超音波off |
YUPPY | 7:fae874e898b3 | 157 | wait(1); |
YUPPY | 7:fae874e898b3 | 158 | Sb612switch=1; //焦電on |
YUPPY | 7:fae874e898b3 | 159 | wait(1); |
YUPPY | 7:fae874e898b3 | 160 | printf("焦電On!\r\n "); |
YUPPY | 7:fae874e898b3 | 161 | bool detected=false; |
YUPPY | 7:fae874e898b3 | 162 | th = thermo; |
YUPPY | 7:fae874e898b3 | 163 | if(th==1 && !detected) {//焦電反応ありの場合 |
YUPPY | 7:fae874e898b3 | 164 | i++; |
YUPPY | 7:fae874e898b3 | 165 | detected=true; |
YUPPY | 7:fae874e898b3 | 166 | pc.printf("human\r\n"); |
YUPPY | 7:fae874e898b3 | 167 | tm.reset(); |
YUPPY | 7:fae874e898b3 | 168 | tm.start(); |
YUPPY | 7:fae874e898b3 | 169 | |
YUPPY | 7:fae874e898b3 | 170 | LocalFileSystem local("local"); |
YUPPY | 7:fae874e898b3 | 171 | Timer timer; |
YUPPY | 7:fae874e898b3 | 172 | timer.start(); |
YUPPY | 7:fae874e898b3 | 173 | camera.setPictureSize(JPEGCamera::SIZE320x240); |
YUPPY | 7:fae874e898b3 | 174 | |
YUPPY | 7:fae874e898b3 | 175 | FILE *fp; |
YUPPY | 7:fae874e898b3 | 176 | base64 *bs; |
YUPPY | 7:fae874e898b3 | 177 | int c; |
YUPPY | 7:fae874e898b3 | 178 | |
YUPPY | 7:fae874e898b3 | 179 | for (int r = 0; r < 1; r++) { |
YUPPY | 7:fae874e898b3 | 180 | if (camera.isReady()) { |
YUPPY | 7:fae874e898b3 | 181 | char filename[32]; |
YUPPY | 7:fae874e898b3 | 182 | sprintf(filename, "/local/pict%03d.jpg",r); |
YUPPY | 7:fae874e898b3 | 183 | printf("Picture: %s ", filename); |
YUPPY | 7:fae874e898b3 | 184 | if (camera.takePicture(filename)) { |
YUPPY | 7:fae874e898b3 | 185 | while (camera.isProcessing()) { |
YUPPY | 7:fae874e898b3 | 186 | camera.processPicture(); |
YUPPY | 7:fae874e898b3 | 187 | printf("take pictuer!"); |
YUPPY | 7:fae874e898b3 | 188 | } |
YUPPY | 7:fae874e898b3 | 189 | }else{ |
YUPPY | 7:fae874e898b3 | 190 | printf("take picture failed\r\n"); |
YUPPY | 7:fae874e898b3 | 191 | } |
YUPPY | 7:fae874e898b3 | 192 | }else{ |
YUPPY | 7:fae874e898b3 | 193 | printf("camera is not ready\r\n"); |
YUPPY | 7:fae874e898b3 | 194 | } |
YUPPY | 7:fae874e898b3 | 195 | |
YUPPY | 7:fae874e898b3 | 196 | printf("time = %f\n", timer.read()); |
YUPPY | 7:fae874e898b3 | 197 | |
YUPPY | 7:fae874e898b3 | 198 | |
YUPPY | 7:fae874e898b3 | 199 | |
YUPPY | 7:fae874e898b3 | 200 | xbee.printf("xbee connected!\r\n"); |
YUPPY | 7:fae874e898b3 | 201 | bs = new base64(); |
YUPPY | 7:fae874e898b3 | 202 | bs->Encode("/local/pict000.jpg","/local/data000.txt"); |
YUPPY | 7:fae874e898b3 | 203 | |
YUPPY | 7:fae874e898b3 | 204 | if((fp=fopen("/local/data000.txt","r"))!=NULL) |
YUPPY | 7:fae874e898b3 | 205 | { |
YUPPY | 7:fae874e898b3 | 206 | pc.printf("ok\r\n"); |
YUPPY | 7:fae874e898b3 | 207 | while((c=fgetc(fp))!=EOF){ |
YUPPY | 7:fae874e898b3 | 208 | xbee.printf("%c",c); |
YUPPY | 7:fae874e898b3 | 209 | } |
YUPPY | 7:fae874e898b3 | 210 | fclose(fp); |
YUPPY | 7:fae874e898b3 | 211 | } |
YUPPY | 7:fae874e898b3 | 212 | } |
YUPPY | 7:fae874e898b3 | 213 | while(1){ |
YUPPY | 7:fae874e898b3 | 214 | |
YUPPY | 7:fae874e898b3 | 215 | int received_data = xbee.getc(); |
YUPPY | 7:fae874e898b3 | 216 | |
YUPPY | 7:fae874e898b3 | 217 | if (received_data == 82 || received_data == 114){ //Rまたはr |
YUPPY | 7:fae874e898b3 | 218 | xbee.printf("_________________________________________________________________________________________________________________________________\r\n"); |
YUPPY | 7:fae874e898b3 | 219 | if((fp=fopen("/local/data000.txt","r"))!=NULL) |
YUPPY | 7:fae874e898b3 | 220 | { |
YUPPY | 7:fae874e898b3 | 221 | while ((c=fgetc(fp))!=EOF){ |
YUPPY | 7:fae874e898b3 | 222 | xbee.printf("%c",c); //再送 |
YUPPY | 7:fae874e898b3 | 223 | } |
YUPPY | 7:fae874e898b3 | 224 | fclose(fp); |
YUPPY | 7:fae874e898b3 | 225 | } |
YUPPY | 7:fae874e898b3 | 226 | } |
YUPPY | 7:fae874e898b3 | 227 | else{ |
YUPPY | 7:fae874e898b3 | 228 | break; |
YUPPY | 7:fae874e898b3 | 229 | } |
YUPPY | 7:fae874e898b3 | 230 | } |
YUPPY | 7:fae874e898b3 | 231 | |
YUPPY | 7:fae874e898b3 | 232 | Sb612switch=0; //焦電off |
YUPPY | 7:fae874e898b3 | 233 | wait(1); |
YUPPY | 7:fae874e898b3 | 234 | }else{//焦電反応なしの場合 |
YUPPY | 7:fae874e898b3 | 235 | printf("not found!\r\n"); |
YUPPY | 7:fae874e898b3 | 236 | |
YUPPY | 7:fae874e898b3 | 237 | |
YUPPY | 7:fae874e898b3 | 238 | Sb612switch=0; |
YUPPY | 7:fae874e898b3 | 239 | wait(1); |
YUPPY | 7:fae874e898b3 | 240 | Ultra=0; |
YUPPY | 7:fae874e898b3 | 241 | wait(1); |
YUPPY | 7:fae874e898b3 | 242 | detected=false; |
YUPPY | 7:fae874e898b3 | 243 | printf("後退\r\n"); |
YUPPY | 7:fae874e898b3 | 244 | left1 = -100; //左モーター-50% |
YUPPY | 7:fae874e898b3 | 245 | right1 = -100;//右モーター-50% |
YUPPY | 7:fae874e898b3 | 246 | wait(2.0); |
YUPPY | 7:fae874e898b3 | 247 | left1=-50; |
YUPPY | 7:fae874e898b3 | 248 | right1=-50; |
YUPPY | 7:fae874e898b3 | 249 | wait(1); |
YUPPY | 7:fae874e898b3 | 250 | left1=0; |
YUPPY | 7:fae874e898b3 | 251 | right1=0; |
YUPPY | 7:fae874e898b3 | 252 | wait(1); |
YUPPY | 7:fae874e898b3 | 253 | |
YUPPY | 7:fae874e898b3 | 254 | printf("右折\n\r"); |
YUPPY | 7:fae874e898b3 | 255 | |
YUPPY | 7:fae874e898b3 | 256 | left1 = 60; //左モーター100% |
YUPPY | 7:fae874e898b3 | 257 | right1 = 100;//右モーター100% |
YUPPY | 7:fae874e898b3 | 258 | wait(2.0); |
YUPPY | 7:fae874e898b3 | 259 | } |
YUPPY | 7:fae874e898b3 | 260 | }else{//超音波distance>2000 |
YUPPY | 7:fae874e898b3 | 261 | printf("safety zone\r\n"); |
YUPPY | 7:fae874e898b3 | 262 | Ultra=0; |
YUPPY | 7:fae874e898b3 | 263 | wait(1); |
YUPPY | 7:fae874e898b3 | 264 | left1 = 60; //左モーター50% |
YUPPY | 7:fae874e898b3 | 265 | right1 = 100;//右モーター50% |
YUPPY | 7:fae874e898b3 | 266 | printf("右折\r\n"); |
YUPPY | 5:5aa7223226df | 267 | } |
YUPPY | 4:1354e56c7dd3 | 268 | |
YUPPY | 5:5aa7223226df | 269 | } |
YUPPY | 7:fae874e898b3 | 270 | |
YUPPY | 7:fae874e898b3 | 271 | } |
YUPPY | 7:fae874e898b3 | 272 | } |