cansat program1

Dependencies:   ADXL345 BME280 HMC5883L ITG3200 MBed_Adafruit-GPS-Library XBee agzIDLIST cansat mbed

Fork of Cansat_program4_1 by CanSat2015aizu

Committer:
s1200058
Date:
Thu Aug 13 07:11:57 2015 +0000
Revision:
19:cb3a4b4c3526
Parent:
18:c86872baed44
Child:
20:f92bdcda5a60
08/13_16:10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kityann 0:1fcc61be1dcf 1 /**********************************************/
kityann 0:1fcc61be1dcf 2 //更新情報
kityann 0:1fcc61be1dcf 3
kityann 0:1fcc61be1dcf 4 /**********************************************/
kityann 0:1fcc61be1dcf 5
kityann 0:1fcc61be1dcf 6 #include "mbed.h"
kityann 0:1fcc61be1dcf 7 #include "XBee.h"
kityann 0:1fcc61be1dcf 8 #include "MBed_Adafruit_GPS.h"
kityann 1:f1f7413ae6bd 9 //#include "AigamozuControlPackets.h"
kityann 0:1fcc61be1dcf 10 #include "agzIDLIST.h"
kityann 0:1fcc61be1dcf 11 #include "aigamozuSetting.h"
kityann 1:f1f7413ae6bd 12 #include "HMC5883L.h"
kityann 1:f1f7413ae6bd 13 #include "VNH5019.h"
kityann 3:0bd9ad37f319 14 #include "cansat.h"
kityann 0:1fcc61be1dcf 15 #include "math.h"
s1200058 5:ba883a4bddc3 16 #include "BME280.h"
kityann 0:1fcc61be1dcf 17
kityann 0:1fcc61be1dcf 18 #define SIGMA_MIN 0.0001
kityann 0:1fcc61be1dcf 19
s1210160 10:ce253d8a5f2c 20 #define STOP 0 //compass initial
s1210160 10:ce253d8a5f2c 21 #define CAL 1 //compass calibration
s1210160 10:ce253d8a5f2c 22 #define RUN 2 //compass run
s1210160 10:ce253d8a5f2c 23
kityann 0:1fcc61be1dcf 24 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 25 //
kityann 0:1fcc61be1dcf 26 //Pin Setting
kityann 0:1fcc61be1dcf 27 //
kityann 0:1fcc61be1dcf 28 /////////////////////////////////////////
s1200058 12:5724d4a57a4c 29 VNH5019 agz_motorShield(p23,p22,p25,p21,p24,p26);
s1200058 12:5724d4a57a4c 30 //VNH5019 agz_motorShield(p21,p22,p23,p24,p25,p26);
kityann 0:1fcc61be1dcf 31
kityann 0:1fcc61be1dcf 32 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 33 //
kityann 0:1fcc61be1dcf 34 //Connection Setting
kityann 0:1fcc61be1dcf 35 //
kityann 0:1fcc61be1dcf 36 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 37
kityann 0:1fcc61be1dcf 38 //Serial Connect Setting: PC <--> mbed
kityann 0:1fcc61be1dcf 39 Serial pc(USBTX, USBRX);
kityann 0:1fcc61be1dcf 40
kityann 0:1fcc61be1dcf 41 //Serial Connect Setting: GPS <--> mbed
kityann 0:1fcc61be1dcf 42 Serial * gps_Serial;
kityann 0:1fcc61be1dcf 43
kityann 0:1fcc61be1dcf 44 //Serial Connect Setting: XBEE <--> mbed
s1200058 12:5724d4a57a4c 45 Serial xbee(p13,p14);
kityann 0:1fcc61be1dcf 46 ZBRxResponse zbRx = ZBRxResponse();
kityann 0:1fcc61be1dcf 47
s1210160 10:ce253d8a5f2c 48 // compass
s1210160 10:ce253d8a5f2c 49 HMC5883L compass(p9, p10);
s1210160 10:ce253d8a5f2c 50
kityann 0:1fcc61be1dcf 51 //set up GPS module
kityann 0:1fcc61be1dcf 52
kityann 0:1fcc61be1dcf 53 //set up AigamozuControlPackets library
kityann 1:f1f7413ae6bd 54 //AigamozuControlPackets agz(agz_motorShield);
kityann 3:0bd9ad37f319 55 CanSat cansat(agz_motorShield);
kityann 0:1fcc61be1dcf 56
s1200058 5:ba883a4bddc3 57 //set up for tempratures...
s1200058 5:ba883a4bddc3 58 #if defined(TARGET_LPC1768)
s1200058 5:ba883a4bddc3 59 BME280 sensor(p9, p10);
s1200058 5:ba883a4bddc3 60 #else
s1200058 5:ba883a4bddc3 61 BME280 sensor(I2C_SDA, I2C_SCL);
s1200058 5:ba883a4bddc3 62 #endif
s1200058 5:ba883a4bddc3 63
s1200058 5:ba883a4bddc3 64 DigitalIn short_in(p29);
s1200058 5:ba883a4bddc3 65 DigitalOut short_out(p30);
s1200058 5:ba883a4bddc3 66 DigitalInOut nic(p5);
kityann 0:1fcc61be1dcf 67
s1200058 18:c86872baed44 68 int short_flag = 0;
s1200058 19:cb3a4b4c3526 69 int running_flag = 0;
s1200058 18:c86872baed44 70
s1200058 12:5724d4a57a4c 71 Timer compass_Timer;
s1200058 12:5724d4a57a4c 72 const int compass_Time = 500;
s1200058 18:c86872baed44 73 Timer running_Timer;
s1200058 18:c86872baed44 74 const int running_Time = 500;
s1200058 18:c86872baed44 75 Timer parachute_Timer;
s1200058 18:c86872baed44 76 const int parachute_Time = 30000;
kityann 0:1fcc61be1dcf 77 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 78 //
s1210160 10:ce253d8a5f2c 79 //For Compass data
s1210160 10:ce253d8a5f2c 80 //
s1210160 10:ce253d8a5f2c 81 /////////////////////////////////////////
s1210160 10:ce253d8a5f2c 82 Ticker compass_interrupt;
s1210160 10:ce253d8a5f2c 83 double heading0 = 0.0;
s1210160 10:ce253d8a5f2c 84 double heading1 = 0.0;
s1210160 10:ce253d8a5f2c 85 double heading2 = 0.0;
s1210160 10:ce253d8a5f2c 86 double heading3 = 0.0;
s1210160 10:ce253d8a5f2c 87 double headingLPF = 0.0;
s1210160 10:ce253d8a5f2c 88 double initHeading;
s1210160 10:ce253d8a5f2c 89 double tgtHeading;
s1210160 10:ce253d8a5f2c 90 double preHeading = 0.0;
s1210160 10:ce253d8a5f2c 91
s1210160 10:ce253d8a5f2c 92 int maxX, minX, maxY, minY;
s1200058 19:cb3a4b4c3526 93 const int ofsX = 36; //calibration x
s1200058 19:cb3a4b4c3526 94 const int ofsY = -425; //calibration y
s1210160 10:ce253d8a5f2c 95
s1210160 10:ce253d8a5f2c 96 int16_t raw[3];
s1210160 10:ce253d8a5f2c 97
s1210160 10:ce253d8a5f2c 98 /////////////////////////////////////////
s1210160 10:ce253d8a5f2c 99 //
kityann 0:1fcc61be1dcf 100 //For Kalman data
kityann 0:1fcc61be1dcf 101 //
kityann 0:1fcc61be1dcf 102 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 103 #define FIRST_S2_1 1.0e-8
kityann 0:1fcc61be1dcf 104 #define FIRST_S2_2 1.0e-6
kityann 0:1fcc61be1dcf 105 #define COUNTER_MAX 10000
kityann 0:1fcc61be1dcf 106 #define ERROR_RANGE 0.001
kityann 0:1fcc61be1dcf 107
kityann 0:1fcc61be1dcf 108 double x_cur,x_prev,y_cur,y_prev;//緯度と経度の時刻tと時刻t-1での推定値
kityann 0:1fcc61be1dcf 109 double s2x_cur=FIRST_S2_1,s2x_prev=FIRST_S2_1,s2y_cur=FIRST_S2_1,s2y_prev=FIRST_S2_1;//緯度経度のの時刻tと時刻t-1での共分散
kityann 0:1fcc61be1dcf 110 double s2_R=FIRST_S2_2;//GPSセンサの分散
kityann 0:1fcc61be1dcf 111 double s2_Q=FIRST_S2_2;
kityann 0:1fcc61be1dcf 112 double Kx=0,Ky=0;//カルマンゲイン
kityann 0:1fcc61be1dcf 113 double zx,zy;//観測値
kityann 0:1fcc61be1dcf 114 void Kalman(double Latitude,double Longitude);
kityann 0:1fcc61be1dcf 115 int change = 0;
kityann 0:1fcc61be1dcf 116
s1200058 18:c86872baed44 117 int mode = -1; //ロボットのモード
s1200058 19:cb3a4b4c3526 118 double target_x = 139.987305,target_y = 40.142655;
s1200058 19:cb3a4b4c3526 119 double goal_Pressure = 1002.20, goal_Temperature,goal_Humidity; //地表地点の気圧、気温、湿度
kityann 0:1fcc61be1dcf 120
s1200058 7:db6b436c0baa 121 Timer sep_Timer;
s1200058 18:c86872baed44 122 const int sep_Time = 3000; //seperate time in ms
s1200058 7:db6b436c0baa 123 int fall_flag = 0;
s1200058 7:db6b436c0baa 124
s1200058 14:4dfeeca65308 125 int log_number = 0;
s1200058 14:4dfeeca65308 126
kityann 0:1fcc61be1dcf 127
kityann 0:1fcc61be1dcf 128 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 129 //
kityann 0:1fcc61be1dcf 130 //Kalman Processing
kityann 0:1fcc61be1dcf 131 //
kityann 0:1fcc61be1dcf 132 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 133 void calc_Kalman(){
kityann 0:1fcc61be1dcf 134 //calc Kalman gain
kityann 0:1fcc61be1dcf 135 Kx = (s2x_prev+s2_Q)/(s2x_prev+s2_R+s2_Q);
kityann 0:1fcc61be1dcf 136 Ky = (s2y_prev+s2_Q)/(s2y_prev+s2_R+s2_Q);
kityann 0:1fcc61be1dcf 137 //estimate
kityann 0:1fcc61be1dcf 138 x_cur = x_prev + Kx*(zx-x_prev);
kityann 0:1fcc61be1dcf 139 y_cur = y_prev + Ky*(zy-y_prev);
kityann 0:1fcc61be1dcf 140 //calc sigma
kityann 0:1fcc61be1dcf 141 s2x_cur = (1-Kx)*(s2x_prev+s2_Q);
kityann 0:1fcc61be1dcf 142 s2y_cur = (1-Ky)*(s2y_prev+s2_Q);
kityann 0:1fcc61be1dcf 143
kityann 0:1fcc61be1dcf 144 }
kityann 0:1fcc61be1dcf 145
kityann 0:1fcc61be1dcf 146 void Kalman(double Latitude,double Longitude){
kityann 0:1fcc61be1dcf 147
kityann 0:1fcc61be1dcf 148 zx = Latitude;
kityann 0:1fcc61be1dcf 149 zy = Longitude;
kityann 0:1fcc61be1dcf 150
kityann 0:1fcc61be1dcf 151 calc_Kalman();
kityann 0:1fcc61be1dcf 152
kityann 0:1fcc61be1dcf 153 //更新
kityann 0:1fcc61be1dcf 154 x_prev = x_cur;
kityann 0:1fcc61be1dcf 155 y_prev = y_cur;
kityann 0:1fcc61be1dcf 156 s2x_prev = s2x_cur;
kityann 0:1fcc61be1dcf 157 s2y_prev = s2y_cur;
kityann 0:1fcc61be1dcf 158
kityann 3:0bd9ad37f319 159 //robotK\x,robotK_yに格納する
kityann 3:0bd9ad37f319 160 cansat.set_robotKalman_x(x_cur);
kityann 3:0bd9ad37f319 161 cansat.set_robotKalman_y(y_cur);
kityann 0:1fcc61be1dcf 162 }
kityann 0:1fcc61be1dcf 163
kityann 0:1fcc61be1dcf 164 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 165 //
kityann 0:1fcc61be1dcf 166 //Get GPS function
kityann 0:1fcc61be1dcf 167 //
kityann 0:1fcc61be1dcf 168 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 169
kityann 0:1fcc61be1dcf 170 void Get_GPS(Adafruit_GPS *myGPS){
kityann 0:1fcc61be1dcf 171 static int flag = 0;
kityann 0:1fcc61be1dcf 172
kityann 0:1fcc61be1dcf 173 if (myGPS->fix) {
kityann 0:1fcc61be1dcf 174
kityann 3:0bd9ad37f319 175 cansat.nowStatus = GPS_AVAIL;
s1200058 19:cb3a4b4c3526 176 cansat.set_robot_y((double)myGPS->latitudeH + (double)(myGPS->latitudeL / 10000.0 / 60.0));
s1200058 19:cb3a4b4c3526 177 cansat.set_robot_x((double)myGPS->longitudeH +(double)(myGPS->longitudeL / 10000.0 / 60.0));
kityann 3:0bd9ad37f319 178
kityann 0:1fcc61be1dcf 179
kityann 0:1fcc61be1dcf 180 if(flag < COUNTER_MAX){
kityann 0:1fcc61be1dcf 181 flag++;
kityann 0:1fcc61be1dcf 182 }
kityann 0:1fcc61be1dcf 183 if(flag == 5){
kityann 3:0bd9ad37f319 184 x_prev = cansat.get_robot_x();
kityann 3:0bd9ad37f319 185 y_prev = cansat.get_robot_y();
kityann 0:1fcc61be1dcf 186 }
kityann 0:1fcc61be1dcf 187
kityann 0:1fcc61be1dcf 188 if(flag >= 6){
kityann 3:0bd9ad37f319 189 if(abs(x_prev - cansat.get_robot_x()) < ERROR_RANGE && abs(y_prev - cansat.get_robot_y()) < ERROR_RANGE){
kityann 3:0bd9ad37f319 190 Kalman(cansat.get_robot_x(), cansat.get_robot_y());
kityann 0:1fcc61be1dcf 191 change = 1;
kityann 0:1fcc61be1dcf 192 }
kityann 0:1fcc61be1dcf 193 else{
kityann 0:1fcc61be1dcf 194 change = 0;
kityann 0:1fcc61be1dcf 195 }
kityann 1:f1f7413ae6bd 196 //printf("%.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
kityann 1:f1f7413ae6bd 197 //agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 1:f1f7413ae6bd 198 //agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 1:f1f7413ae6bd 199 //agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 1:f1f7413ae6bd 200 }
kityann 0:1fcc61be1dcf 201 }
kityann 3:0bd9ad37f319 202 else cansat.nowStatus = GPS_UNAVAIL;
kityann 0:1fcc61be1dcf 203
kityann 0:1fcc61be1dcf 204 }
kityann 0:1fcc61be1dcf 205
aoki0731 4:0fc7221e2f79 206 //ロボットの動き方
s1200058 11:19091694455e 207 char robot_Action(int robot_angle, int target_angle) {
aoki0731 4:0fc7221e2f79 208
s1200058 11:19091694455e 209 int n, t, r;
aoki0731 4:0fc7221e2f79 210 t = target_angle;
aoki0731 4:0fc7221e2f79 211 r = robot_angle;
aoki0731 4:0fc7221e2f79 212 n = r - t;
aoki0731 4:0fc7221e2f79 213 if(n<0) n *= -1;
aoki0731 4:0fc7221e2f79 214
aoki0731 4:0fc7221e2f79 215
aoki0731 4:0fc7221e2f79 216 if(t==r) {
aoki0731 4:0fc7221e2f79 217 //前進
aoki0731 4:0fc7221e2f79 218 return 'f';
aoki0731 4:0fc7221e2f79 219 } else if(n < 4) {
aoki0731 4:0fc7221e2f79 220 if(t > r)
aoki0731 4:0fc7221e2f79 221 t -= 8;
aoki0731 4:0fc7221e2f79 222 else
aoki0731 4:0fc7221e2f79 223 r -= 8;
aoki0731 4:0fc7221e2f79 224
aoki0731 4:0fc7221e2f79 225 if(r-t > 0)
aoki0731 4:0fc7221e2f79 226 return 'l';
aoki0731 4:0fc7221e2f79 227 else
aoki0731 4:0fc7221e2f79 228 return 'r';
aoki0731 4:0fc7221e2f79 229 } else if(n >= 4) {
aoki0731 4:0fc7221e2f79 230 if(t > r)
aoki0731 4:0fc7221e2f79 231 t -= 8;
aoki0731 4:0fc7221e2f79 232 else
aoki0731 4:0fc7221e2f79 233 r -= 8;
aoki0731 4:0fc7221e2f79 234
aoki0731 4:0fc7221e2f79 235 if(r-t > 0)
aoki0731 4:0fc7221e2f79 236 return 'r';
aoki0731 4:0fc7221e2f79 237 else
aoki0731 4:0fc7221e2f79 238 return 'l';
aoki0731 4:0fc7221e2f79 239 }
aoki0731 4:0fc7221e2f79 240
aoki0731 4:0fc7221e2f79 241 return 'b';
aoki0731 4:0fc7221e2f79 242 }
aoki0731 4:0fc7221e2f79 243
s1200058 5:ba883a4bddc3 244 //対象とロボットの角度
s1200058 5:ba883a4bddc3 245 double robot_compass(double robot_x, double robot_y) {
s1200058 5:ba883a4bddc3 246 double angle = 0;
s1200058 5:ba883a4bddc3 247
s1200058 5:ba883a4bddc3 248 if(robot_x==0&&robot_y>0)
s1200058 5:ba883a4bddc3 249 return 0;
s1200058 5:ba883a4bddc3 250 else if(robot_x>0&&robot_y==0) //東
s1200058 5:ba883a4bddc3 251 return 90;
s1200058 5:ba883a4bddc3 252 else if(robot_x==0&&robot_y<0) //南
s1200058 5:ba883a4bddc3 253 return 180;
s1200058 5:ba883a4bddc3 254 else if(robot_x<0&&robot_y==0) //西
s1200058 5:ba883a4bddc3 255 return 270;
s1200058 5:ba883a4bddc3 256 else if(robot_x>=0&&robot_y>=0) { //北東
s1200058 5:ba883a4bddc3 257 if(robot_x<=robot_y)
s1200058 5:ba883a4bddc3 258 angle = atan2(robot_x, robot_y);
s1200058 5:ba883a4bddc3 259 else
s1200058 5:ba883a4bddc3 260 angle = (M_PI/2) - atan2(robot_y, robot_x);
s1200058 5:ba883a4bddc3 261 return angle * 180.0 / M_PI;
s1200058 5:ba883a4bddc3 262 } else if(robot_x>=0&&robot_y<0) { //南東
s1200058 5:ba883a4bddc3 263 if(robot_x>abs(robot_y)){
s1200058 5:ba883a4bddc3 264 angle = (M_PI/2) - atan2(abs(robot_y), robot_x);
s1200058 5:ba883a4bddc3 265 }
s1200058 5:ba883a4bddc3 266 else{
s1200058 5:ba883a4bddc3 267 angle = atan2(abs(robot_y), robot_x);
s1200058 5:ba883a4bddc3 268 }
s1200058 5:ba883a4bddc3 269 return angle * 180.0 / M_PI + 90;
s1200058 5:ba883a4bddc3 270 } else if(robot_x<0&&robot_y<0) { //南西
s1200058 5:ba883a4bddc3 271 if(abs(robot_x)<abs(robot_y)){
s1200058 5:ba883a4bddc3 272 angle = atan2(abs(robot_x), abs(robot_y));
s1200058 5:ba883a4bddc3 273 }
s1200058 5:ba883a4bddc3 274 else{
s1200058 5:ba883a4bddc3 275 angle = (M_PI/2) - atan2(abs(robot_y), abs(robot_x));
s1200058 5:ba883a4bddc3 276 }
s1200058 5:ba883a4bddc3 277 return angle * 180.0 / M_PI + 180;
s1200058 5:ba883a4bddc3 278 } else if(robot_x<0&&robot_y>=0) { //北西
s1200058 5:ba883a4bddc3 279 if(abs(robot_x)>robot_y){
s1200058 5:ba883a4bddc3 280 angle = (M_PI/2) - atan2(robot_y, abs(robot_x));
s1200058 5:ba883a4bddc3 281 }
s1200058 5:ba883a4bddc3 282 else{
s1200058 5:ba883a4bddc3 283 angle = atan2(abs(robot_x), robot_y);
s1200058 5:ba883a4bddc3 284 }
kityann 9:2741e17438d6 285 return 360 - angle * 180.0 / M_PI;
s1200058 5:ba883a4bddc3 286 }
s1200058 5:ba883a4bddc3 287
s1200058 5:ba883a4bddc3 288 return -1;
s1200058 5:ba883a4bddc3 289 }
s1200058 5:ba883a4bddc3 290
s1210160 10:ce253d8a5f2c 291 int calc_angle(double c){
s1200058 11:19091694455e 292 if(c > 337.5 ||(c >=0 && c < 22.5)) return 0;
s1210160 10:ce253d8a5f2c 293 else if(c >= 22.5 && c < 67.5) return 1;
s1210160 10:ce253d8a5f2c 294 else if(c >= 67.5 && c < 112.5) return 2;
s1210160 10:ce253d8a5f2c 295 else if(c >= 112.5 && c < 157.5) return 3;
s1210160 10:ce253d8a5f2c 296 else if(c >= 157.5 && c < 202.5) return 4;
s1210160 10:ce253d8a5f2c 297 else if(c >= 202.5 && c < 247.5) return 5;
s1210160 10:ce253d8a5f2c 298 else if(c >= 247.5 && c < 292.5) return 6;
s1210160 10:ce253d8a5f2c 299 else if(c >= 292.5 && c < 337.5) return 7;
s1210160 10:ce253d8a5f2c 300 else return 8;
s1210160 10:ce253d8a5f2c 301 }
s1200058 5:ba883a4bddc3 302
s1210160 10:ce253d8a5f2c 303 void Compass_intrpt(){
s1210160 10:ce253d8a5f2c 304
s1210160 10:ce253d8a5f2c 305 compass.getXYZ(raw);
s1210160 10:ce253d8a5f2c 306 double heading = atan2(static_cast<double>(raw[2]-ofsY), static_cast<double>(raw[0]-ofsX)); //y=raw[2]
s1210160 10:ce253d8a5f2c 307 if(heading < 0)heading += 2*M_PI;
s1210160 10:ce253d8a5f2c 308 if(heading > 2*M_PI)heading -= 2*M_PI;
s1210160 10:ce253d8a5f2c 309 heading3 = heading2;
s1210160 10:ce253d8a5f2c 310 heading2 = heading1;
s1210160 10:ce253d8a5f2c 311 heading1 = heading0;
s1210160 10:ce253d8a5f2c 312 heading0 = heading;
s1200058 19:cb3a4b4c3526 313 headingLPF = (heading0 + heading1 + heading2 + heading3)/4.0; //low pass filter
s1210160 10:ce253d8a5f2c 314
s1210160 10:ce253d8a5f2c 315 headingLPF = headingLPF * 180.0 / M_PI;
s1210160 10:ce253d8a5f2c 316 // pc.printf("heading=%f\r\n",headingLPF);
s1210160 10:ce253d8a5f2c 317
s1210160 10:ce253d8a5f2c 318 cansat.set_compass(raw[0], raw[2], raw[1], headingLPF);
s1200058 18:c86872baed44 319 cansat.set_robot_angle(calc_angle(cansat.get_compass_angle()));
s1210160 10:ce253d8a5f2c 320 }
s1210160 10:ce253d8a5f2c 321
s1210160 10:ce253d8a5f2c 322
kityann 0:1fcc61be1dcf 323 /******************************
kityann 0:1fcc61be1dcf 324 スタンバイモード
kityann 0:1fcc61be1dcf 325 ******************************/
kityann 0:1fcc61be1dcf 326 void standby(){
s1200058 5:ba883a4bddc3 327
s1200058 6:23bb3b018f44 328 cansat.control_Motor(1, cansat.get_speed());
s1200058 18:c86872baed44 329 if(!short_in){
s1200058 18:c86872baed44 330 xbee.printf("change mode: falling\n");
s1200058 18:c86872baed44 331 short_flag++;
s1200058 18:c86872baed44 332 }
s1200058 18:c86872baed44 333 else{
s1200058 18:c86872baed44 334 if(running_Timer.read_ms() >= running_Time){
s1200058 18:c86872baed44 335 running_Timer.reset();
s1200058 18:c86872baed44 336 xbee.printf("stand by\n");
s1200058 18:c86872baed44 337 }
s1200058 18:c86872baed44 338 }
s1200058 18:c86872baed44 339 if(short_flag >= 5){
s1200058 5:ba883a4bddc3 340 mode = 1;
s1200058 19:cb3a4b4c3526 341 parachute_Timer.start();
s1200058 5:ba883a4bddc3 342 }
s1200058 5:ba883a4bddc3 343
kityann 0:1fcc61be1dcf 344 }
kityann 0:1fcc61be1dcf 345
kityann 0:1fcc61be1dcf 346 /******************************
kityann 0:1fcc61be1dcf 347 落下モード
kityann 0:1fcc61be1dcf 348 ******************************/
s1200058 15:dd7c2ab65a09 349 void falling_print(){
s1200058 15:dd7c2ab65a09 350
s1200058 19:cb3a4b4c3526 351 xbee.printf("%d: %04.2f hPa\n", log_number, cansat.get_pressure());
s1200058 15:dd7c2ab65a09 352
s1200058 15:dd7c2ab65a09 353 log_number++;
s1200058 15:dd7c2ab65a09 354
s1200058 15:dd7c2ab65a09 355 }
s1200058 15:dd7c2ab65a09 356
kityann 0:1fcc61be1dcf 357 void falling(){
s1200058 7:db6b436c0baa 358
s1200058 7:db6b436c0baa 359 cansat.set_temperature(sensor.getTemperature());
s1200058 7:db6b436c0baa 360 cansat.set_pressure(sensor.getPressure());
s1200058 7:db6b436c0baa 361 cansat.set_humidity(sensor.getHumidity());
s1200058 7:db6b436c0baa 362
s1200058 18:c86872baed44 363 falling_print();
s1200058 18:c86872baed44 364
s1200058 5:ba883a4bddc3 365 if(cansat.get_pressure() >= goal_Pressure){
s1200058 7:db6b436c0baa 366 if(fall_flag == 0){
s1200058 7:db6b436c0baa 367 nic = 1;
s1200058 7:db6b436c0baa 368 fall_flag = 1;
s1200058 7:db6b436c0baa 369 sep_Timer.reset();
s1200058 7:db6b436c0baa 370 mode = 2;
s1200058 18:c86872baed44 371 xbee.printf("my pressure is high!\n");
s1200058 18:c86872baed44 372 xbee.printf("remove the parachute\n");
s1200058 14:4dfeeca65308 373 xbee.printf("change mode: running\n");
s1200058 7:db6b436c0baa 374 }
s1200058 18:c86872baed44 375 }
s1200058 18:c86872baed44 376 if(fall_flag == 0 && parachute_Timer.read_ms() >= parachute_Time){
s1200058 18:c86872baed44 377 mode = 2;
s1200058 18:c86872baed44 378 nic = 1;
s1200058 18:c86872baed44 379 xbee.printf("Time out!\n");
s1200058 18:c86872baed44 380 xbee.printf("remove the parachute\n");
s1200058 18:c86872baed44 381 wait_ms(3000);
s1200058 18:c86872baed44 382 nic = 0;
s1200058 18:c86872baed44 383 xbee.printf("change mode: running\n");
s1200058 7:db6b436c0baa 384
s1200058 5:ba883a4bddc3 385 }
s1200058 15:dd7c2ab65a09 386
s1200058 15:dd7c2ab65a09 387
kityann 0:1fcc61be1dcf 388 }
kityann 0:1fcc61be1dcf 389
kityann 0:1fcc61be1dcf 390 /******************************
kityann 0:1fcc61be1dcf 391 走行モード
kityann 0:1fcc61be1dcf 392 ******************************/
s1200058 14:4dfeeca65308 393
s1200058 14:4dfeeca65308 394 void running_print(){
s1200058 14:4dfeeca65308 395
s1200058 19:cb3a4b4c3526 396 if(running_flag <= 20){
s1200058 19:cb3a4b4c3526 397 xbee.printf("%d: %f, %f, %d\n", log_number, cansat.get_robot_x(), cansat.get_robot_y(), cansat.get_speed());
s1200058 19:cb3a4b4c3526 398 }
s1200058 19:cb3a4b4c3526 399 else{
s1200058 19:cb3a4b4c3526 400 xbee.printf("%d: %f, %f, %d\n", log_number, cansat.get_robotKalman_x(), cansat.get_robotKalman_y(), cansat.get_speed());
s1200058 19:cb3a4b4c3526 401 }
s1200058 19:cb3a4b4c3526 402 xbee.printf("%c, %f\n", cansat.motor_command, cansat.get_compass_angle());
s1200058 19:cb3a4b4c3526 403 xbee.printf("%d, %d\n",cansat.get_robot_angle(), cansat.get_target_angle());
s1200058 19:cb3a4b4c3526 404 // double y1 = cansat.get_target_y();
s1200058 19:cb3a4b4c3526 405 // double y2 = cansat.get_robot_y();
s1200058 19:cb3a4b4c3526 406 // double x1 = cansat.get_target_x();
s1200058 19:cb3a4b4c3526 407 // double x2 = cansat.get_robot_x();
s1200058 19:cb3a4b4c3526 408 // xbee.printf("taret_angle:%f",robot_compass(x1-x2, y1-y2));
s1200058 14:4dfeeca65308 409
s1200058 14:4dfeeca65308 410 log_number++;
s1200058 14:4dfeeca65308 411
s1200058 14:4dfeeca65308 412 }
s1200058 14:4dfeeca65308 413
kityann 0:1fcc61be1dcf 414 void running(){
s1200058 14:4dfeeca65308 415
s1200058 18:c86872baed44 416 // double r = 6378.137;
s1200058 5:ba883a4bddc3 417 double y1 = cansat.get_target_y();
s1200058 5:ba883a4bddc3 418 double y2 = cansat.get_robot_y();
s1200058 5:ba883a4bddc3 419 double x1 = cansat.get_target_x();
s1200058 5:ba883a4bddc3 420 double x2 = cansat.get_robot_x();
s1200058 19:cb3a4b4c3526 421 double x_k2 = cansat.get_robotKalman_x();
s1200058 19:cb3a4b4c3526 422 double y_k2 = cansat.get_robotKalman_y();
s1200058 5:ba883a4bddc3 423 double dx = x2 - x1;
s1200058 18:c86872baed44 424 double dy = y2 - y1;
s1200058 19:cb3a4b4c3526 425 double dx_k = x_k2 - x1;
s1200058 19:cb3a4b4c3526 426 double dy_k = y_k2 - y1;
s1200058 19:cb3a4b4c3526 427 double y_sub;
s1200058 19:cb3a4b4c3526 428 double x_sub;
s1200058 19:cb3a4b4c3526 429
s1200058 19:cb3a4b4c3526 430 if(running_flag < 20){
s1200058 19:cb3a4b4c3526 431 y_sub = dy*111135.0;
s1200058 19:cb3a4b4c3526 432 x_sub = dx*91191.0;
s1200058 19:cb3a4b4c3526 433 cansat.set_target_distance(sqrt(y_sub*y_sub + x_sub*x_sub));
s1200058 19:cb3a4b4c3526 434 cansat.set_target_angle(calc_angle(robot_compass(x1-x2, y1-y2)));
s1200058 19:cb3a4b4c3526 435 running_flag++;
s1200058 19:cb3a4b4c3526 436 }
s1200058 19:cb3a4b4c3526 437 else{
s1200058 19:cb3a4b4c3526 438 y_sub = dy_k*111135.0;
s1200058 19:cb3a4b4c3526 439 x_sub = dx_k*91191.0;
s1200058 19:cb3a4b4c3526 440 cansat.set_target_distance(sqrt(y_sub*y_sub + x_sub*x_sub));
s1200058 19:cb3a4b4c3526 441 cansat.set_target_angle(calc_angle(robot_compass(x1-x_k2, y1-y_k2)));
s1200058 19:cb3a4b4c3526 442 }
s1200058 14:4dfeeca65308 443
s1200058 19:cb3a4b4c3526 444 if(cansat.get_target_distance() < 10) cansat.set_speed(64);
s1200058 19:cb3a4b4c3526 445 else if(cansat.get_target_distance() < 20 && cansat.get_target_distance() > 10) cansat.set_speed(128);
s1200058 19:cb3a4b4c3526 446 else cansat.set_speed(255);
s1200058 5:ba883a4bddc3 447
s1200058 18:c86872baed44 448 if(fall_flag == 1 && sep_Timer.read_ms() >= sep_Time){
s1200058 18:c86872baed44 449 nic = 0;
s1200058 18:c86872baed44 450 }
s1200058 12:5724d4a57a4c 451 if(compass_Timer.read_ms() >= compass_Time){
s1200058 12:5724d4a57a4c 452 compass_Timer.reset();
s1200058 12:5724d4a57a4c 453 if(cansat.get_compass_z() < 0) {
s1200058 12:5724d4a57a4c 454 //ひっくり返っている
s1200058 12:5724d4a57a4c 455 cansat.control_Motor(0, cansat.get_speed());
s1200058 12:5724d4a57a4c 456 } else {
s1200058 14:4dfeeca65308 457 cansat.motor_command = robot_Action(cansat.get_robot_angle(), cansat.get_target_angle());
s1200058 14:4dfeeca65308 458 switch(cansat.motor_command) {
s1200058 12:5724d4a57a4c 459 case 'f': //前進
s1200058 12:5724d4a57a4c 460 cansat.control_Motor(0, cansat.get_speed());
s1200058 12:5724d4a57a4c 461 break;
s1200058 12:5724d4a57a4c 462 case 'l':
s1200058 12:5724d4a57a4c 463 cansat.control_Motor(2, cansat.get_speed());
s1200058 12:5724d4a57a4c 464 break;
s1200058 12:5724d4a57a4c 465 case 'r':
s1200058 12:5724d4a57a4c 466 cansat.control_Motor(3, cansat.get_speed());
s1200058 12:5724d4a57a4c 467 break;
s1200058 13:4f3fd6c4ddc2 468 case 'b':
s1200058 13:4f3fd6c4ddc2 469 cansat.control_Motor(4, cansat.get_speed());
s1200058 13:4f3fd6c4ddc2 470 break;
s1200058 12:5724d4a57a4c 471 }
s1200058 5:ba883a4bddc3 472 }
s1200058 5:ba883a4bddc3 473 }
s1200058 18:c86872baed44 474
s1200058 18:c86872baed44 475 running_print();
s1200058 18:c86872baed44 476
s1200058 7:db6b436c0baa 477 if(cansat.get_target_distance() <= 1){
s1200058 6:23bb3b018f44 478 mode = 100;
s1200058 14:4dfeeca65308 479 xbee.printf("change mode: stopping\n");
s1200058 14:4dfeeca65308 480 xbee.printf("it is goal point.\n");
s1200058 6:23bb3b018f44 481 }
kityann 0:1fcc61be1dcf 482 }
kityann 0:1fcc61be1dcf 483
kityann 0:1fcc61be1dcf 484 /******************************
kityann 0:1fcc61be1dcf 485 停止モード
kityann 0:1fcc61be1dcf 486 ******************************/
kityann 0:1fcc61be1dcf 487 void stopping(){
kityann 0:1fcc61be1dcf 488
s1200058 13:4f3fd6c4ddc2 489 cansat.control_Motor(1, cansat.get_speed());
kityann 0:1fcc61be1dcf 490
kityann 0:1fcc61be1dcf 491 }
kityann 0:1fcc61be1dcf 492
kityann 0:1fcc61be1dcf 493 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 494 //
kityann 0:1fcc61be1dcf 495 //Main Processing
kityann 0:1fcc61be1dcf 496 //
kityann 0:1fcc61be1dcf 497 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 498 int main() {
kityann 0:1fcc61be1dcf 499 //start up time
kityann 0:1fcc61be1dcf 500 wait(3);
kityann 0:1fcc61be1dcf 501 //set pc frequency to 57600bps
kityann 0:1fcc61be1dcf 502 pc.baud(PC_BAUD_RATE);
kityann 0:1fcc61be1dcf 503 //set xbee frequency to 57600bps
s1200058 12:5724d4a57a4c 504 //xbee.begin(XBEE_BAUD_RATE);
s1200058 19:cb3a4b4c3526 505 xbee.baud(57600);
s1210160 10:ce253d8a5f2c 506 //Compass setting
s1210160 10:ce253d8a5f2c 507 compass.init();
kityann 0:1fcc61be1dcf 508
kityann 0:1fcc61be1dcf 509 //GPS setting
kityann 0:1fcc61be1dcf 510 gps_Serial = new Serial(p28,p27);
kityann 0:1fcc61be1dcf 511 Adafruit_GPS myGPS(gps_Serial);
kityann 0:1fcc61be1dcf 512 Timer refresh_Timer;
kityann 0:1fcc61be1dcf 513 const int refresh_Time = 1000; //refresh time in ms
kityann 0:1fcc61be1dcf 514 int count = 0;
s1200058 19:cb3a4b4c3526 515 Timer compass_refresh_Timer;
s1200058 19:cb3a4b4c3526 516 const int compass_refresh_Time = 500;
kityann 0:1fcc61be1dcf 517
kityann 0:1fcc61be1dcf 518 myGPS.begin(GPS_BAUD_RATE);
kityann 0:1fcc61be1dcf 519
kityann 0:1fcc61be1dcf 520 //GPS Send Command
kityann 0:1fcc61be1dcf 521 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:1fcc61be1dcf 522 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:1fcc61be1dcf 523 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:1fcc61be1dcf 524
kityann 0:1fcc61be1dcf 525 wait_ms(2000);
kityann 0:1fcc61be1dcf 526
kityann 0:1fcc61be1dcf 527 //interrupt start
kityann 0:1fcc61be1dcf 528 refresh_Timer.start();
s1200058 13:4f3fd6c4ddc2 529 compass_Timer.start();
s1200058 14:4dfeeca65308 530 running_Timer.start();
s1200058 18:c86872baed44 531 sep_Timer.start();
s1200058 19:cb3a4b4c3526 532 compass_refresh_Timer.start();
s1200058 11:19091694455e 533 cansat.set_target(target_x, target_y);
s1200058 12:5724d4a57a4c 534 // wait_ms(10000);
s1200058 11:19091694455e 535
s1200058 19:cb3a4b4c3526 536 //compass_interrupt.attach(&Compass_intrpt, 0.5);
s1200058 19:cb3a4b4c3526 537
kityann 0:1fcc61be1dcf 538
kityann 0:1fcc61be1dcf 539 printf("start\n");
s1200058 19:cb3a4b4c3526 540 xbee.printf("target: %f, %f\n",cansat.get_target_x(), cansat.get_target_y());
s1200058 12:5724d4a57a4c 541 //int mode = -1;
s1200058 5:ba883a4bddc3 542 nic.output();
s1200058 5:ba883a4bddc3 543 nic = 0;
kityann 0:1fcc61be1dcf 544
s1200058 18:c86872baed44 545 short_out = 1;
s1200058 18:c86872baed44 546 wait_ms(1000);
s1200058 19:cb3a4b4c3526 547 // if(short_out){
s1200058 19:cb3a4b4c3526 548 // xbee.printf("HIGH\n");
s1200058 19:cb3a4b4c3526 549 // }
s1200058 18:c86872baed44 550
kityann 0:1fcc61be1dcf 551 while (true) {
s1200058 19:cb3a4b4c3526 552
s1200058 19:cb3a4b4c3526 553 myGPS.read();
s1200058 19:cb3a4b4c3526 554 //recive gps module
s1200058 19:cb3a4b4c3526 555 //check if we recieved a new message from GPS, if so, attempt to parse it,
s1200058 19:cb3a4b4c3526 556 if ( myGPS.newNMEAreceived() ) {
s1200058 19:cb3a4b4c3526 557 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
s1200058 19:cb3a4b4c3526 558 continue;
s1200058 19:cb3a4b4c3526 559 }
s1200058 19:cb3a4b4c3526 560 else{
s1200058 19:cb3a4b4c3526 561 count++;
s1200058 19:cb3a4b4c3526 562 }
s1200058 19:cb3a4b4c3526 563 }
s1200058 19:cb3a4b4c3526 564 //一定時間ごとに自分のGPSデータを取得し、AigamozuControlPacketsないのagzPointとagzPointKalmanに格納する
s1200058 19:cb3a4b4c3526 565 if (refresh_Timer.read_ms() >= refresh_Time) {
s1200058 19:cb3a4b4c3526 566 refresh_Timer.reset();
s1200058 19:cb3a4b4c3526 567 //print_gps(count);
s1200058 19:cb3a4b4c3526 568 Get_GPS(&myGPS);
s1200058 19:cb3a4b4c3526 569 //log++;
s1200058 19:cb3a4b4c3526 570 /* xbee.printf("%d times, x:%f, y:%f, speed:%d\n", log, cansat.get_robot_x(), cansat.get_robot_y(), cansat.get_speed());
s1200058 19:cb3a4b4c3526 571 xbee.printf("moter command: %c\n", cansat.motor_command);
s1200058 19:cb3a4b4c3526 572 xbee.printf("robot_angle:%d, target_angle:%d, robot_compass:%f, %04.2f hPa\n",cansat.get_robot_angle(), cansat.get_target_angle(), cansat.get_compass_angle(), cansat.get_pressure());
s1200058 19:cb3a4b4c3526 573 */
s1200058 19:cb3a4b4c3526 574 }
s1200058 19:cb3a4b4c3526 575 if(compass_refresh_Timer.read_ms() >= compass_refresh_Time){
s1200058 19:cb3a4b4c3526 576 Compass_intrpt();
s1200058 19:cb3a4b4c3526 577 compass_refresh_Timer.reset();
s1200058 19:cb3a4b4c3526 578 }
s1200058 14:4dfeeca65308 579
kityann 0:1fcc61be1dcf 580 switch(mode){
kityann 0:1fcc61be1dcf 581 //スタートモード:パラシュートが開くまではこのモードを実行
kityann 0:1fcc61be1dcf 582 case -1:
kityann 0:1fcc61be1dcf 583 standby();
kityann 0:1fcc61be1dcf 584 break;
kityann 0:1fcc61be1dcf 585 //落下モード:落下時はこのモード。気圧計または、時間でロボットとパラシュートを分離する
kityann 0:1fcc61be1dcf 586 case 1:
kityann 0:1fcc61be1dcf 587 falling();
kityann 0:1fcc61be1dcf 588 break;
kityann 0:1fcc61be1dcf 589 //走行モード:ターゲットにむかって走行を行う
kityann 0:1fcc61be1dcf 590 case 2:
s1200058 14:4dfeeca65308 591 if (running_Timer.read_ms() >= running_Time) {
s1200058 14:4dfeeca65308 592 running_Timer.reset();
s1200058 14:4dfeeca65308 593 running();
s1200058 14:4dfeeca65308 594 }
kityann 0:1fcc61be1dcf 595 break;
kityann 0:1fcc61be1dcf 596 //停止モード:ターゲット
kityann 0:1fcc61be1dcf 597 case 100:
kityann 0:1fcc61be1dcf 598 stopping();
kityann 0:1fcc61be1dcf 599 break;
kityann 0:1fcc61be1dcf 600 }
s1210160 10:ce253d8a5f2c 601
s1200058 11:19091694455e 602 /* while(1){
s1210160 10:ce253d8a5f2c 603
s1210160 10:ce253d8a5f2c 604 printf("compass x : %i, compass y : %i, compass z : %i\n", raw[0], raw[1], raw[2]);
s1210160 10:ce253d8a5f2c 605 printf("set compass x : %i, set compass y : %i, set compass z : %i\n", cansat.get_compass_x(), cansat.get_compass_y(), cansat.get_compass_z());
s1210160 10:ce253d8a5f2c 606 printf("compass angle : %f\n", headingLPF);
s1210160 10:ce253d8a5f2c 607 printf("set compass angle : %f\n", cansat.get_compass_angle());
s1210160 10:ce253d8a5f2c 608 printf("robot angle : %d\n", calc_angle(headingLPF));
s1210160 10:ce253d8a5f2c 609 printf("set robot angle : %d\n", cansat.get_robot_angle());
s1210160 10:ce253d8a5f2c 610 }
s1200058 11:19091694455e 611 */
s1200058 19:cb3a4b4c3526 612
kityann 0:1fcc61be1dcf 613 }
kityann 0:1fcc61be1dcf 614
kityann 0:1fcc61be1dcf 615 }