08/13

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

Fork of Cansat_program4 by CanSat2015aizu

Committer:
s1200058
Date:
Wed Aug 12 12:14:30 2015 +0000
Revision:
16:a3a0e5654835
Parent:
15:dd7c2ab65a09
Child:
18:c86872baed44
change falling_mode

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 12:5724d4a57a4c 68 Timer compass_Timer;
s1200058 12:5724d4a57a4c 69 const int compass_Time = 500;
kityann 0:1fcc61be1dcf 70 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 71 //
s1210160 10:ce253d8a5f2c 72 //For Compass data
s1210160 10:ce253d8a5f2c 73 //
s1210160 10:ce253d8a5f2c 74 /////////////////////////////////////////
s1210160 10:ce253d8a5f2c 75 Ticker compass_interrupt;
s1210160 10:ce253d8a5f2c 76 double heading0 = 0.0;
s1210160 10:ce253d8a5f2c 77 double heading1 = 0.0;
s1210160 10:ce253d8a5f2c 78 double heading2 = 0.0;
s1210160 10:ce253d8a5f2c 79 double heading3 = 0.0;
s1210160 10:ce253d8a5f2c 80 double headingLPF = 0.0;
s1210160 10:ce253d8a5f2c 81 double initHeading;
s1210160 10:ce253d8a5f2c 82 double tgtHeading;
s1210160 10:ce253d8a5f2c 83 double preHeading = 0.0;
s1210160 10:ce253d8a5f2c 84
s1210160 10:ce253d8a5f2c 85 int maxX, minX, maxY, minY;
s1200058 13:4f3fd6c4ddc2 86 const int ofsX = 46; //calibration x
s1200058 13:4f3fd6c4ddc2 87 const int ofsY = -950; //calibration y
s1210160 10:ce253d8a5f2c 88
s1210160 10:ce253d8a5f2c 89 int16_t raw[3];
s1210160 10:ce253d8a5f2c 90
s1210160 10:ce253d8a5f2c 91 /////////////////////////////////////////
s1210160 10:ce253d8a5f2c 92 //
kityann 0:1fcc61be1dcf 93 //For Kalman data
kityann 0:1fcc61be1dcf 94 //
kityann 0:1fcc61be1dcf 95 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 96 #define FIRST_S2_1 1.0e-8
kityann 0:1fcc61be1dcf 97 #define FIRST_S2_2 1.0e-6
kityann 0:1fcc61be1dcf 98 #define COUNTER_MAX 10000
kityann 0:1fcc61be1dcf 99 #define ERROR_RANGE 0.001
kityann 0:1fcc61be1dcf 100
kityann 0:1fcc61be1dcf 101 double x_cur,x_prev,y_cur,y_prev;//緯度と経度の時刻tと時刻t-1での推定値
kityann 0:1fcc61be1dcf 102 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 103 double s2_R=FIRST_S2_2;//GPSセンサの分散
kityann 0:1fcc61be1dcf 104 double s2_Q=FIRST_S2_2;
kityann 0:1fcc61be1dcf 105 double Kx=0,Ky=0;//カルマンゲイン
kityann 0:1fcc61be1dcf 106 double zx,zy;//観測値
kityann 0:1fcc61be1dcf 107 void Kalman(double Latitude,double Longitude);
kityann 0:1fcc61be1dcf 108 int change = 0;
kityann 0:1fcc61be1dcf 109
s1200058 11:19091694455e 110 int mode = 2; //ロボットのモード
s1200058 15:dd7c2ab65a09 111 double target_x = 40.085592,target_y = 139.592383;
s1200058 5:ba883a4bddc3 112 double goal_Pressure, goal_Temperature,goal_Humidity; //地表地点の気圧、気温、湿度
kityann 0:1fcc61be1dcf 113
s1200058 7:db6b436c0baa 114 Timer sep_Timer;
s1200058 7:db6b436c0baa 115 const int sep_Time = 30000; //seperate time in ms
s1200058 7:db6b436c0baa 116 int fall_flag = 0;
s1200058 7:db6b436c0baa 117
s1200058 14:4dfeeca65308 118 int log_number = 0;
s1200058 14:4dfeeca65308 119
kityann 0:1fcc61be1dcf 120
kityann 0:1fcc61be1dcf 121 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 122 //
kityann 0:1fcc61be1dcf 123 //Kalman Processing
kityann 0:1fcc61be1dcf 124 //
kityann 0:1fcc61be1dcf 125 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 126 void calc_Kalman(){
kityann 0:1fcc61be1dcf 127 //calc Kalman gain
kityann 0:1fcc61be1dcf 128 Kx = (s2x_prev+s2_Q)/(s2x_prev+s2_R+s2_Q);
kityann 0:1fcc61be1dcf 129 Ky = (s2y_prev+s2_Q)/(s2y_prev+s2_R+s2_Q);
kityann 0:1fcc61be1dcf 130 //estimate
kityann 0:1fcc61be1dcf 131 x_cur = x_prev + Kx*(zx-x_prev);
kityann 0:1fcc61be1dcf 132 y_cur = y_prev + Ky*(zy-y_prev);
kityann 0:1fcc61be1dcf 133 //calc sigma
kityann 0:1fcc61be1dcf 134 s2x_cur = (1-Kx)*(s2x_prev+s2_Q);
kityann 0:1fcc61be1dcf 135 s2y_cur = (1-Ky)*(s2y_prev+s2_Q);
kityann 0:1fcc61be1dcf 136
kityann 0:1fcc61be1dcf 137 }
kityann 0:1fcc61be1dcf 138
kityann 0:1fcc61be1dcf 139 void Kalman(double Latitude,double Longitude){
kityann 0:1fcc61be1dcf 140
kityann 0:1fcc61be1dcf 141 zx = Latitude;
kityann 0:1fcc61be1dcf 142 zy = Longitude;
kityann 0:1fcc61be1dcf 143
kityann 0:1fcc61be1dcf 144 calc_Kalman();
kityann 0:1fcc61be1dcf 145
kityann 0:1fcc61be1dcf 146 //更新
kityann 0:1fcc61be1dcf 147 x_prev = x_cur;
kityann 0:1fcc61be1dcf 148 y_prev = y_cur;
kityann 0:1fcc61be1dcf 149 s2x_prev = s2x_cur;
kityann 0:1fcc61be1dcf 150 s2y_prev = s2y_cur;
kityann 0:1fcc61be1dcf 151
kityann 3:0bd9ad37f319 152 //robotK\x,robotK_yに格納する
kityann 3:0bd9ad37f319 153 cansat.set_robotKalman_x(x_cur);
kityann 3:0bd9ad37f319 154 cansat.set_robotKalman_y(y_cur);
kityann 0:1fcc61be1dcf 155 }
kityann 0:1fcc61be1dcf 156
kityann 0:1fcc61be1dcf 157 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 158 //
kityann 0:1fcc61be1dcf 159 //Get GPS function
kityann 0:1fcc61be1dcf 160 //
kityann 0:1fcc61be1dcf 161 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 162
kityann 0:1fcc61be1dcf 163 void Get_GPS(Adafruit_GPS *myGPS){
kityann 0:1fcc61be1dcf 164 static int flag = 0;
kityann 0:1fcc61be1dcf 165
kityann 0:1fcc61be1dcf 166 if (myGPS->fix) {
kityann 0:1fcc61be1dcf 167
kityann 3:0bd9ad37f319 168 cansat.nowStatus = GPS_AVAIL;
s1200058 15:dd7c2ab65a09 169 cansat.set_robot_x((double)myGPS->latitudeH + (double)(myGPS->latitudeL / 1000000.0));
s1200058 15:dd7c2ab65a09 170 cansat.set_robot_y((double)myGPS->longitudeH +(double)(myGPS->longitudeL / 1000000.0));
kityann 3:0bd9ad37f319 171
kityann 0:1fcc61be1dcf 172
kityann 0:1fcc61be1dcf 173 if(flag < COUNTER_MAX){
kityann 0:1fcc61be1dcf 174 flag++;
kityann 0:1fcc61be1dcf 175 }
kityann 0:1fcc61be1dcf 176 if(flag == 5){
kityann 3:0bd9ad37f319 177 x_prev = cansat.get_robot_x();
kityann 3:0bd9ad37f319 178 y_prev = cansat.get_robot_y();
kityann 0:1fcc61be1dcf 179 }
kityann 0:1fcc61be1dcf 180
kityann 0:1fcc61be1dcf 181 if(flag >= 6){
kityann 3:0bd9ad37f319 182 if(abs(x_prev - cansat.get_robot_x()) < ERROR_RANGE && abs(y_prev - cansat.get_robot_y()) < ERROR_RANGE){
kityann 3:0bd9ad37f319 183 Kalman(cansat.get_robot_x(), cansat.get_robot_y());
kityann 0:1fcc61be1dcf 184 change = 1;
kityann 0:1fcc61be1dcf 185 }
kityann 0:1fcc61be1dcf 186 else{
kityann 0:1fcc61be1dcf 187 change = 0;
kityann 0:1fcc61be1dcf 188 }
kityann 1:f1f7413ae6bd 189 //printf("%.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
kityann 1:f1f7413ae6bd 190 //agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 1:f1f7413ae6bd 191 //agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 1:f1f7413ae6bd 192 //agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 1:f1f7413ae6bd 193 }
kityann 0:1fcc61be1dcf 194 }
kityann 3:0bd9ad37f319 195 else cansat.nowStatus = GPS_UNAVAIL;
kityann 0:1fcc61be1dcf 196
kityann 0:1fcc61be1dcf 197 }
kityann 0:1fcc61be1dcf 198
aoki0731 4:0fc7221e2f79 199 //ロボットの動き方
s1200058 11:19091694455e 200 char robot_Action(int robot_angle, int target_angle) {
aoki0731 4:0fc7221e2f79 201
s1200058 11:19091694455e 202 int n, t, r;
aoki0731 4:0fc7221e2f79 203 t = target_angle;
aoki0731 4:0fc7221e2f79 204 r = robot_angle;
aoki0731 4:0fc7221e2f79 205 n = r - t;
aoki0731 4:0fc7221e2f79 206 if(n<0) n *= -1;
aoki0731 4:0fc7221e2f79 207
aoki0731 4:0fc7221e2f79 208
aoki0731 4:0fc7221e2f79 209 if(t==r) {
aoki0731 4:0fc7221e2f79 210 //前進
aoki0731 4:0fc7221e2f79 211 return 'f';
aoki0731 4:0fc7221e2f79 212 } else if(n < 4) {
aoki0731 4:0fc7221e2f79 213 if(t > r)
aoki0731 4:0fc7221e2f79 214 t -= 8;
aoki0731 4:0fc7221e2f79 215 else
aoki0731 4:0fc7221e2f79 216 r -= 8;
aoki0731 4:0fc7221e2f79 217
aoki0731 4:0fc7221e2f79 218 if(r-t > 0)
aoki0731 4:0fc7221e2f79 219 return 'l';
aoki0731 4:0fc7221e2f79 220 else
aoki0731 4:0fc7221e2f79 221 return 'r';
aoki0731 4:0fc7221e2f79 222 } else if(n >= 4) {
aoki0731 4:0fc7221e2f79 223 if(t > r)
aoki0731 4:0fc7221e2f79 224 t -= 8;
aoki0731 4:0fc7221e2f79 225 else
aoki0731 4:0fc7221e2f79 226 r -= 8;
aoki0731 4:0fc7221e2f79 227
aoki0731 4:0fc7221e2f79 228 if(r-t > 0)
aoki0731 4:0fc7221e2f79 229 return 'r';
aoki0731 4:0fc7221e2f79 230 else
aoki0731 4:0fc7221e2f79 231 return 'l';
aoki0731 4:0fc7221e2f79 232 }
aoki0731 4:0fc7221e2f79 233
aoki0731 4:0fc7221e2f79 234 return 'b';
aoki0731 4:0fc7221e2f79 235 }
aoki0731 4:0fc7221e2f79 236
s1200058 5:ba883a4bddc3 237 //対象とロボットの角度
s1200058 5:ba883a4bddc3 238 double robot_compass(double robot_x, double robot_y) {
s1200058 5:ba883a4bddc3 239 double angle = 0;
s1200058 5:ba883a4bddc3 240
s1200058 5:ba883a4bddc3 241 if(robot_x==0&&robot_y>0)
s1200058 5:ba883a4bddc3 242 return 0;
s1200058 5:ba883a4bddc3 243 else if(robot_x>0&&robot_y==0) //東
s1200058 5:ba883a4bddc3 244 return 90;
s1200058 5:ba883a4bddc3 245 else if(robot_x==0&&robot_y<0) //南
s1200058 5:ba883a4bddc3 246 return 180;
s1200058 5:ba883a4bddc3 247 else if(robot_x<0&&robot_y==0) //西
s1200058 5:ba883a4bddc3 248 return 270;
s1200058 5:ba883a4bddc3 249 else if(robot_x>=0&&robot_y>=0) { //北東
s1200058 5:ba883a4bddc3 250 if(robot_x<=robot_y)
s1200058 5:ba883a4bddc3 251 angle = atan2(robot_x, robot_y);
s1200058 5:ba883a4bddc3 252 else
s1200058 5:ba883a4bddc3 253 angle = (M_PI/2) - atan2(robot_y, robot_x);
s1200058 5:ba883a4bddc3 254 return angle * 180.0 / M_PI;
s1200058 5:ba883a4bddc3 255 } else if(robot_x>=0&&robot_y<0) { //南東
s1200058 5:ba883a4bddc3 256 if(robot_x>abs(robot_y)){
s1200058 5:ba883a4bddc3 257 angle = (M_PI/2) - atan2(abs(robot_y), robot_x);
s1200058 5:ba883a4bddc3 258 }
s1200058 5:ba883a4bddc3 259 else{
s1200058 5:ba883a4bddc3 260 angle = atan2(abs(robot_y), robot_x);
s1200058 5:ba883a4bddc3 261 }
s1200058 5:ba883a4bddc3 262 return angle * 180.0 / M_PI + 90;
s1200058 5:ba883a4bddc3 263 } else if(robot_x<0&&robot_y<0) { //南西
s1200058 5:ba883a4bddc3 264 if(abs(robot_x)<abs(robot_y)){
s1200058 5:ba883a4bddc3 265 angle = atan2(abs(robot_x), abs(robot_y));
s1200058 5:ba883a4bddc3 266 }
s1200058 5:ba883a4bddc3 267 else{
s1200058 5:ba883a4bddc3 268 angle = (M_PI/2) - atan2(abs(robot_y), abs(robot_x));
s1200058 5:ba883a4bddc3 269 }
s1200058 5:ba883a4bddc3 270 return angle * 180.0 / M_PI + 180;
s1200058 5:ba883a4bddc3 271 } else if(robot_x<0&&robot_y>=0) { //北西
s1200058 5:ba883a4bddc3 272 if(abs(robot_x)>robot_y){
s1200058 5:ba883a4bddc3 273 angle = (M_PI/2) - atan2(robot_y, abs(robot_x));
s1200058 5:ba883a4bddc3 274 }
s1200058 5:ba883a4bddc3 275 else{
s1200058 5:ba883a4bddc3 276 angle = atan2(abs(robot_x), robot_y);
s1200058 5:ba883a4bddc3 277 }
kityann 9:2741e17438d6 278 return 360 - angle * 180.0 / M_PI;
s1200058 5:ba883a4bddc3 279 }
s1200058 5:ba883a4bddc3 280
s1200058 5:ba883a4bddc3 281 return -1;
s1200058 5:ba883a4bddc3 282 }
s1200058 5:ba883a4bddc3 283
s1210160 10:ce253d8a5f2c 284 int calc_angle(double c){
s1200058 11:19091694455e 285 if(c > 337.5 ||(c >=0 && c < 22.5)) return 0;
s1210160 10:ce253d8a5f2c 286 else if(c >= 22.5 && c < 67.5) return 1;
s1210160 10:ce253d8a5f2c 287 else if(c >= 67.5 && c < 112.5) return 2;
s1210160 10:ce253d8a5f2c 288 else if(c >= 112.5 && c < 157.5) return 3;
s1210160 10:ce253d8a5f2c 289 else if(c >= 157.5 && c < 202.5) return 4;
s1210160 10:ce253d8a5f2c 290 else if(c >= 202.5 && c < 247.5) return 5;
s1210160 10:ce253d8a5f2c 291 else if(c >= 247.5 && c < 292.5) return 6;
s1210160 10:ce253d8a5f2c 292 else if(c >= 292.5 && c < 337.5) return 7;
s1210160 10:ce253d8a5f2c 293 else return 8;
s1210160 10:ce253d8a5f2c 294 }
s1200058 5:ba883a4bddc3 295
s1210160 10:ce253d8a5f2c 296 void Compass_intrpt(){
s1210160 10:ce253d8a5f2c 297
s1210160 10:ce253d8a5f2c 298 compass.getXYZ(raw);
s1210160 10:ce253d8a5f2c 299 double heading = atan2(static_cast<double>(raw[2]-ofsY), static_cast<double>(raw[0]-ofsX)); //y=raw[2]
s1210160 10:ce253d8a5f2c 300 if(heading < 0)heading += 2*M_PI;
s1210160 10:ce253d8a5f2c 301 if(heading > 2*M_PI)heading -= 2*M_PI;
s1210160 10:ce253d8a5f2c 302 heading3 = heading2;
s1210160 10:ce253d8a5f2c 303 heading2 = heading1;
s1210160 10:ce253d8a5f2c 304 heading1 = heading0;
s1210160 10:ce253d8a5f2c 305 heading0 = heading;
s1210160 10:ce253d8a5f2c 306 headingLPF = (heading0 + heading1 + heading2 + heading3)/4; //low pass filter
s1210160 10:ce253d8a5f2c 307
s1210160 10:ce253d8a5f2c 308 headingLPF = headingLPF * 180.0 / M_PI;
s1210160 10:ce253d8a5f2c 309 // pc.printf("heading=%f\r\n",headingLPF);
s1210160 10:ce253d8a5f2c 310
s1210160 10:ce253d8a5f2c 311 cansat.set_compass(raw[0], raw[2], raw[1], headingLPF);
s1210160 10:ce253d8a5f2c 312 cansat.set_robot_angle(calc_angle(headingLPF));
s1210160 10:ce253d8a5f2c 313 }
s1210160 10:ce253d8a5f2c 314
s1210160 10:ce253d8a5f2c 315
kityann 0:1fcc61be1dcf 316 /******************************
kityann 0:1fcc61be1dcf 317 スタンバイモード
kityann 0:1fcc61be1dcf 318 ******************************/
kityann 0:1fcc61be1dcf 319 void standby(){
s1200058 5:ba883a4bddc3 320
s1200058 6:23bb3b018f44 321 cansat.control_Motor(1, cansat.get_speed());
s1200058 5:ba883a4bddc3 322 if(short_in == 0){
s1200058 5:ba883a4bddc3 323 mode = 1;
s1200058 14:4dfeeca65308 324 xbee.printf("change mode: falling\n");
s1200058 5:ba883a4bddc3 325 }
s1200058 5:ba883a4bddc3 326
kityann 0:1fcc61be1dcf 327 }
kityann 0:1fcc61be1dcf 328
kityann 0:1fcc61be1dcf 329 /******************************
kityann 0:1fcc61be1dcf 330 落下モード
kityann 0:1fcc61be1dcf 331 ******************************/
s1200058 15:dd7c2ab65a09 332 void falling_print(){
s1200058 15:dd7c2ab65a09 333
s1200058 15:dd7c2ab65a09 334 xbee.printf("%d times, x:%f, y:%f, speed:%d\n", log_number, cansat.get_robot_x(), cansat.get_robot_y(), cansat.get_speed());
s1200058 15:dd7c2ab65a09 335 xbee.printf("Pressure:%04.2f hPa\n", cansat.get_pressure());
s1200058 15:dd7c2ab65a09 336
s1200058 15:dd7c2ab65a09 337 log_number++;
s1200058 15:dd7c2ab65a09 338
s1200058 15:dd7c2ab65a09 339 }
s1200058 15:dd7c2ab65a09 340
kityann 0:1fcc61be1dcf 341 void falling(){
s1200058 7:db6b436c0baa 342
s1200058 7:db6b436c0baa 343 cansat.set_temperature(sensor.getTemperature());
s1200058 7:db6b436c0baa 344 cansat.set_pressure(sensor.getPressure());
s1200058 7:db6b436c0baa 345 cansat.set_humidity(sensor.getHumidity());
s1200058 7:db6b436c0baa 346
s1200058 5:ba883a4bddc3 347 if(cansat.get_pressure() >= goal_Pressure){
s1200058 7:db6b436c0baa 348 if(fall_flag == 0){
s1200058 7:db6b436c0baa 349 nic = 1;
s1200058 7:db6b436c0baa 350 fall_flag = 1;
s1200058 7:db6b436c0baa 351 sep_Timer.reset();
s1200058 7:db6b436c0baa 352 }
s1200058 16:a3a0e5654835 353 if(fall_flag == 1 && sep_Timer.read_ms() >= sep_Time){
s1200058 7:db6b436c0baa 354 mode = 2;
s1200058 7:db6b436c0baa 355 nic = 0;
s1200058 14:4dfeeca65308 356 xbee.printf("change mode: running\n");
s1200058 7:db6b436c0baa 357 }
s1200058 7:db6b436c0baa 358
s1200058 5:ba883a4bddc3 359 }
s1200058 15:dd7c2ab65a09 360
s1200058 15:dd7c2ab65a09 361 falling_print();
s1200058 15:dd7c2ab65a09 362
kityann 0:1fcc61be1dcf 363 }
kityann 0:1fcc61be1dcf 364
kityann 0:1fcc61be1dcf 365 /******************************
kityann 0:1fcc61be1dcf 366 走行モード
kityann 0:1fcc61be1dcf 367 ******************************/
s1200058 14:4dfeeca65308 368
s1200058 14:4dfeeca65308 369 void running_print(){
s1200058 14:4dfeeca65308 370
s1200058 14:4dfeeca65308 371 xbee.printf("%d times, x:%f, y:%f, speed:%d\n", log_number, cansat.get_robot_x(), cansat.get_robot_y(), cansat.get_speed());
s1200058 14:4dfeeca65308 372 xbee.printf("moter command: %c\n", cansat.motor_command);
s1200058 15:dd7c2ab65a09 373 xbee.printf("robot_angle:%d, target_angle:%d, robot_compass:%f, prressure:%04.2f hPa\n",cansat.get_robot_angle(), cansat.get_target_angle(), cansat.get_compass_angle(), cansat.get_pressure());
s1200058 14:4dfeeca65308 374
s1200058 14:4dfeeca65308 375 log_number++;
s1200058 14:4dfeeca65308 376
s1200058 14:4dfeeca65308 377 }
s1200058 14:4dfeeca65308 378
kityann 0:1fcc61be1dcf 379 void running(){
s1200058 14:4dfeeca65308 380
aoki0731 4:0fc7221e2f79 381 double r = 6378.137;
s1200058 5:ba883a4bddc3 382 double y1 = cansat.get_target_y();
s1200058 5:ba883a4bddc3 383 double y2 = cansat.get_robot_y();
s1200058 5:ba883a4bddc3 384 double x1 = cansat.get_target_x();
s1200058 5:ba883a4bddc3 385 double x2 = cansat.get_robot_x();
s1200058 5:ba883a4bddc3 386 double dx = x2 - x1;
s1200058 14:4dfeeca65308 387
s1200058 7:db6b436c0baa 388 cansat.set_target_distance(r*acos(sin(y1)*sin(y2)+cos(y1)*cos(y2)*cos(dx)));
s1200058 13:4f3fd6c4ddc2 389 cansat.set_target_angle(calc_angle(robot_compass(x1-x2, y1-y2)));
s1200058 5:ba883a4bddc3 390
s1200058 5:ba883a4bddc3 391 if(cansat.get_target_distance() < 10) cansat.set_speed(32);
s1200058 5:ba883a4bddc3 392 else if(cansat.get_target_distance() < 20 && cansat.get_target_distance() > 10) cansat.set_speed(64);
s1200058 5:ba883a4bddc3 393 else cansat.set_speed(128);
s1200058 5:ba883a4bddc3 394
s1200058 12:5724d4a57a4c 395 if(compass_Timer.read_ms() >= compass_Time){
s1200058 12:5724d4a57a4c 396 compass_Timer.reset();
s1200058 12:5724d4a57a4c 397 if(cansat.get_compass_z() < 0) {
s1200058 12:5724d4a57a4c 398 //ひっくり返っている
s1200058 12:5724d4a57a4c 399 cansat.control_Motor(0, cansat.get_speed());
s1200058 12:5724d4a57a4c 400 } else {
s1200058 14:4dfeeca65308 401 cansat.motor_command = robot_Action(cansat.get_robot_angle(), cansat.get_target_angle());
s1200058 14:4dfeeca65308 402 switch(cansat.motor_command) {
s1200058 12:5724d4a57a4c 403 case 'f': //前進
s1200058 12:5724d4a57a4c 404 cansat.control_Motor(0, cansat.get_speed());
s1200058 12:5724d4a57a4c 405 break;
s1200058 12:5724d4a57a4c 406 case 'l':
s1200058 12:5724d4a57a4c 407 cansat.control_Motor(2, cansat.get_speed());
s1200058 12:5724d4a57a4c 408 break;
s1200058 12:5724d4a57a4c 409 case 'r':
s1200058 12:5724d4a57a4c 410 cansat.control_Motor(3, cansat.get_speed());
s1200058 12:5724d4a57a4c 411 break;
s1200058 13:4f3fd6c4ddc2 412 case 'b':
s1200058 13:4f3fd6c4ddc2 413 cansat.control_Motor(4, cansat.get_speed());
s1200058 13:4f3fd6c4ddc2 414 break;
s1200058 12:5724d4a57a4c 415 }
s1200058 5:ba883a4bddc3 416 }
s1200058 5:ba883a4bddc3 417 }
s1200058 7:db6b436c0baa 418 if(cansat.get_target_distance() <= 1){
s1200058 6:23bb3b018f44 419 mode = 100;
s1200058 14:4dfeeca65308 420 xbee.printf("change mode: stopping\n");
s1200058 14:4dfeeca65308 421 xbee.printf("it is goal point.\n");
s1200058 6:23bb3b018f44 422 }
s1200058 14:4dfeeca65308 423
s1200058 14:4dfeeca65308 424 running_print();
kityann 0:1fcc61be1dcf 425 }
kityann 0:1fcc61be1dcf 426
kityann 0:1fcc61be1dcf 427 /******************************
kityann 0:1fcc61be1dcf 428 停止モード
kityann 0:1fcc61be1dcf 429 ******************************/
kityann 0:1fcc61be1dcf 430 void stopping(){
kityann 0:1fcc61be1dcf 431
s1200058 13:4f3fd6c4ddc2 432 cansat.control_Motor(1, cansat.get_speed());
kityann 0:1fcc61be1dcf 433
kityann 0:1fcc61be1dcf 434 }
kityann 0:1fcc61be1dcf 435
kityann 0:1fcc61be1dcf 436 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 437 //
kityann 0:1fcc61be1dcf 438 //Main Processing
kityann 0:1fcc61be1dcf 439 //
kityann 0:1fcc61be1dcf 440 /////////////////////////////////////////
kityann 0:1fcc61be1dcf 441 int main() {
kityann 0:1fcc61be1dcf 442 //start up time
kityann 0:1fcc61be1dcf 443 wait(3);
kityann 0:1fcc61be1dcf 444 //set pc frequency to 57600bps
kityann 0:1fcc61be1dcf 445 pc.baud(PC_BAUD_RATE);
kityann 0:1fcc61be1dcf 446 //set xbee frequency to 57600bps
s1200058 12:5724d4a57a4c 447 //xbee.begin(XBEE_BAUD_RATE);
s1200058 12:5724d4a57a4c 448 xbee.baud(9600);
s1210160 10:ce253d8a5f2c 449 //Compass setting
s1210160 10:ce253d8a5f2c 450 compass.init();
kityann 0:1fcc61be1dcf 451
kityann 0:1fcc61be1dcf 452 //GPS setting
kityann 0:1fcc61be1dcf 453 gps_Serial = new Serial(p28,p27);
kityann 0:1fcc61be1dcf 454 Adafruit_GPS myGPS(gps_Serial);
kityann 0:1fcc61be1dcf 455 Timer refresh_Timer;
kityann 0:1fcc61be1dcf 456 const int refresh_Time = 1000; //refresh time in ms
s1200058 14:4dfeeca65308 457 Timer running_Timer;
s1200058 14:4dfeeca65308 458 const int running_Time = 500;
kityann 0:1fcc61be1dcf 459 int count = 0;
kityann 0:1fcc61be1dcf 460
kityann 0:1fcc61be1dcf 461 myGPS.begin(GPS_BAUD_RATE);
kityann 0:1fcc61be1dcf 462
kityann 0:1fcc61be1dcf 463 //GPS Send Command
kityann 0:1fcc61be1dcf 464 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:1fcc61be1dcf 465 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:1fcc61be1dcf 466 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:1fcc61be1dcf 467
kityann 0:1fcc61be1dcf 468 wait_ms(2000);
kityann 0:1fcc61be1dcf 469
kityann 0:1fcc61be1dcf 470 //interrupt start
kityann 0:1fcc61be1dcf 471 refresh_Timer.start();
s1200058 13:4f3fd6c4ddc2 472 compass_Timer.start();
s1200058 14:4dfeeca65308 473 running_Timer.start();
s1200058 11:19091694455e 474 cansat.set_target(target_x, target_y);
s1200058 12:5724d4a57a4c 475 // wait_ms(10000);
s1200058 11:19091694455e 476
s1200058 11:19091694455e 477 compass_interrupt.attach(&Compass_intrpt, 0.5);
kityann 0:1fcc61be1dcf 478
kityann 0:1fcc61be1dcf 479 printf("start\n");
kityann 0:1fcc61be1dcf 480
s1200058 12:5724d4a57a4c 481 //int mode = -1;
s1200058 5:ba883a4bddc3 482 short_out = 1; //ショートピンの出力:high
s1200058 5:ba883a4bddc3 483 nic.output();
s1200058 5:ba883a4bddc3 484 nic = 0;
kityann 0:1fcc61be1dcf 485
kityann 0:1fcc61be1dcf 486 while (true) {
s1200058 14:4dfeeca65308 487
kityann 0:1fcc61be1dcf 488 switch(mode){
kityann 0:1fcc61be1dcf 489 //スタートモード:パラシュートが開くまではこのモードを実行
kityann 0:1fcc61be1dcf 490 case -1:
kityann 0:1fcc61be1dcf 491 standby();
kityann 0:1fcc61be1dcf 492 break;
kityann 0:1fcc61be1dcf 493 //落下モード:落下時はこのモード。気圧計または、時間でロボットとパラシュートを分離する
kityann 0:1fcc61be1dcf 494 case 1:
kityann 0:1fcc61be1dcf 495 falling();
kityann 0:1fcc61be1dcf 496 break;
kityann 0:1fcc61be1dcf 497 //走行モード:ターゲットにむかって走行を行う
kityann 0:1fcc61be1dcf 498 case 2:
s1200058 14:4dfeeca65308 499 if (running_Timer.read_ms() >= running_Time) {
s1200058 14:4dfeeca65308 500 running_Timer.reset();
s1200058 14:4dfeeca65308 501 running();
s1200058 14:4dfeeca65308 502 }
kityann 0:1fcc61be1dcf 503 break;
kityann 0:1fcc61be1dcf 504 //停止モード:ターゲット
kityann 0:1fcc61be1dcf 505 case 100:
kityann 0:1fcc61be1dcf 506 stopping();
kityann 0:1fcc61be1dcf 507 break;
kityann 0:1fcc61be1dcf 508 }
s1210160 10:ce253d8a5f2c 509
s1200058 11:19091694455e 510 /* while(1){
s1210160 10:ce253d8a5f2c 511
s1210160 10:ce253d8a5f2c 512 printf("compass x : %i, compass y : %i, compass z : %i\n", raw[0], raw[1], raw[2]);
s1210160 10:ce253d8a5f2c 513 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 514 printf("compass angle : %f\n", headingLPF);
s1210160 10:ce253d8a5f2c 515 printf("set compass angle : %f\n", cansat.get_compass_angle());
s1210160 10:ce253d8a5f2c 516 printf("robot angle : %d\n", calc_angle(headingLPF));
s1210160 10:ce253d8a5f2c 517 printf("set robot angle : %d\n", cansat.get_robot_angle());
s1210160 10:ce253d8a5f2c 518 }
s1200058 11:19091694455e 519 */
kityann 0:1fcc61be1dcf 520 myGPS.read();
kityann 0:1fcc61be1dcf 521 //recive gps module
kityann 0:1fcc61be1dcf 522 //check if we recieved a new message from GPS, if so, attempt to parse it,
kityann 0:1fcc61be1dcf 523 if ( myGPS.newNMEAreceived() ) {
kityann 0:1fcc61be1dcf 524 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kityann 0:1fcc61be1dcf 525 continue;
kityann 0:1fcc61be1dcf 526 }
kityann 0:1fcc61be1dcf 527 else{
kityann 0:1fcc61be1dcf 528 count++;
kityann 0:1fcc61be1dcf 529 }
kityann 0:1fcc61be1dcf 530 }
kityann 0:1fcc61be1dcf 531 //一定時間ごとに自分のGPSデータを取得し、AigamozuControlPacketsないのagzPointとagzPointKalmanに格納する
kityann 0:1fcc61be1dcf 532 if (refresh_Timer.read_ms() >= refresh_Time) {
kityann 0:1fcc61be1dcf 533 refresh_Timer.reset();
kityann 0:1fcc61be1dcf 534 //print_gps(count);
kityann 0:1fcc61be1dcf 535 Get_GPS(&myGPS);
s1200058 14:4dfeeca65308 536 //log++;
s1200058 14:4dfeeca65308 537 /* xbee.printf("%d times, x:%f, y:%f, speed:%d\n", log, cansat.get_robot_x(), cansat.get_robot_y(), cansat.get_speed());
s1200058 12:5724d4a57a4c 538 xbee.printf("moter command: %c\n", cansat.motor_command);
s1200058 12:5724d4a57a4c 539 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 14:4dfeeca65308 540 */
kityann 0:1fcc61be1dcf 541 }
kityann 0:1fcc61be1dcf 542 }
kityann 0:1fcc61be1dcf 543
kityann 0:1fcc61be1dcf 544 }