2015/06/09

Dependencies:   ADXL345 AigamozuControlPackets HMC5843 ITG3200 MBed_Adafruit-GPS-Library XBee agzIDLIST agz_common mbed

Fork of Aigamozu_Base_ver3_1 by aigamozu

Committer:
s1200058
Date:
Mon May 25 11:38:09 2015 +0000
Revision:
14:3aa6d735a2fa
Parent:
13:82f4f00f11f6
Child:
15:35e3917fcbf5
change kalman

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kityann 0:eee5e3d906ce 1 /**********************************************/
kityann 0:eee5e3d906ce 2 //
kityann 0:eee5e3d906ce 3 //
kityann 0:eee5e3d906ce 4 //
kityann 0:eee5e3d906ce 5 // Program name: Aigamozu BASE
kityann 0:eee5e3d906ce 6 // Author: Mineta Kizuku
kityann 0:eee5e3d906ce 7 //
kityann 0:eee5e3d906ce 8 //
kityann 0:eee5e3d906ce 9 /**********************************************/
kityann 0:eee5e3d906ce 10
kityann 0:eee5e3d906ce 11 /**********************************************/
kityann 0:eee5e3d906ce 12 //更新情報
kityann 0:eee5e3d906ce 13 //2015/05/11
kityann 0:eee5e3d906ce 14 //ベースプログラムの作成
kityann 0:eee5e3d906ce 15 //
kityann 2:d97700414618 16 //2015/05/13
kityann 2:d97700414618 17 //カルマンフィルタの共分散の値を0.0001以下にならないようにした
kityann 2:d97700414618 18 //共分散の値を10進数に変換するようにした
s1200058 12:83be8a3c212d 19 //
s1200058 12:83be8a3c212d 20 //2015/05/17
s1200058 12:83be8a3c212d 21 //Get_GPS()の中身longitudeの範囲138〜140に変更
s1200058 12:83be8a3c212d 22 //
s1200058 13:82f4f00f11f6 23 //2015/05/24
s1200058 13:82f4f00f11f6 24 //Kalmanフィルターを十進数で計算するようにした。
s1200058 13:82f4f00f11f6 25 //Kalmanフィルターの計算式を変更した。
s1200058 13:82f4f00f11f6 26 //set_kalmanを追加した。
kityann 0:eee5e3d906ce 27 /**********************************************/
kityann 0:eee5e3d906ce 28
kityann 0:eee5e3d906ce 29 #include "mbed.h"
kityann 0:eee5e3d906ce 30 #include "XBee.h"
kityann 0:eee5e3d906ce 31 #include "MBed_Adafruit_GPS.h"
kityann 0:eee5e3d906ce 32 #include "AigamozuControlPackets.h"
kityann 0:eee5e3d906ce 33 #include "agzIDLIST.h"
kityann 0:eee5e3d906ce 34 #include "aigamozuSetting.h"
kityann 0:eee5e3d906ce 35 #include "agz_common.h"
kityann 0:eee5e3d906ce 36 #include "Kalman.h"
kityann 0:eee5e3d906ce 37
kityann 2:d97700414618 38
kityann 2:d97700414618 39 #define SIGMA_MIN 0.0001
kityann 2:d97700414618 40
kityann 0:eee5e3d906ce 41 //************ID Number*****************
s1200058 14:3aa6d735a2fa 42 const char MyID = 'd';
kityann 0:eee5e3d906ce 43 //************ID Number*****************
kityann 0:eee5e3d906ce 44
kityann 0:eee5e3d906ce 45 /////////////////////////////////////////
kityann 0:eee5e3d906ce 46 //
kityann 0:eee5e3d906ce 47 //Pin Setting
kityann 0:eee5e3d906ce 48 //
kityann 0:eee5e3d906ce 49 /////////////////////////////////////////
kityann 0:eee5e3d906ce 50 VNH5019 agz_motorShield(p21,p22,p23,p24,p25,p26);
kityann 0:eee5e3d906ce 51
kityann 0:eee5e3d906ce 52
kityann 0:eee5e3d906ce 53 /////////////////////////////////////////
kityann 0:eee5e3d906ce 54 //
kityann 0:eee5e3d906ce 55 //Connection Setting
kityann 0:eee5e3d906ce 56 //
kityann 0:eee5e3d906ce 57 /////////////////////////////////////////
kityann 0:eee5e3d906ce 58
kityann 0:eee5e3d906ce 59 //Serial Connect Setting: PC <--> mbed
kityann 0:eee5e3d906ce 60 Serial pc(USBTX, USBRX);
kityann 0:eee5e3d906ce 61
kityann 0:eee5e3d906ce 62 //Serial Connect Setting: GPS <--> mbed
kityann 0:eee5e3d906ce 63 Serial * gps_Serial;
kityann 0:eee5e3d906ce 64
kityann 0:eee5e3d906ce 65 //Serial Connect Setting: XBEE <--> mbed
kityann 0:eee5e3d906ce 66 XBee xbee(p13,p14);
kityann 0:eee5e3d906ce 67 ZBRxResponse zbRx = ZBRxResponse();
kityann 0:eee5e3d906ce 68
kityann 0:eee5e3d906ce 69 //set up GPS module
kityann 0:eee5e3d906ce 70
kityann 0:eee5e3d906ce 71 //set up AigamozuControlPackets library
kityann 0:eee5e3d906ce 72 AigamozuControlPackets agz(agz_motorShield);
kityann 0:eee5e3d906ce 73
kityann 0:eee5e3d906ce 74
kityann 0:eee5e3d906ce 75 /////////////////////////////////////////
kityann 0:eee5e3d906ce 76 //
kityann 0:eee5e3d906ce 77 //For Kalman data
kityann 0:eee5e3d906ce 78 //
kityann 0:eee5e3d906ce 79 /////////////////////////////////////////
s1200058 13:82f4f00f11f6 80 #define FIRST_S2 0.000001
s1200058 13:82f4f00f11f6 81 #define COUNTER_MAX 10000
s1200058 13:82f4f00f11f6 82 double x_cur,x_prev,y_cur,y_prev;//緯度と経度の時刻tと時刻t-1での推定値
s1200058 13:82f4f00f11f6 83 double s2x_cur=FIRST_S2,s2x_prev=FIRST_S2,s2y_cur=FIRST_S2,s2y_prev=FIRST_S2;//緯度経度のの時刻tと時刻t-1での共分散
s1200058 13:82f4f00f11f6 84 double s2_R=FIRST_S2;//GPSセンサの分散
s1200058 13:82f4f00f11f6 85 double Kx=0,Ky=0;//カルマンゲイン
s1200058 13:82f4f00f11f6 86 double zx,zy;//観測値
s1200058 13:82f4f00f11f6 87 void Kalman(double Latitude,double Longitude);
s1200058 14:3aa6d735a2fa 88 int change = 0;
kityann 0:eee5e3d906ce 89
s1200058 14:3aa6d735a2fa 90 /*
s1200058 14:3aa6d735a2fa 91 LocalFileSystem local("local"); // マウントポイントを定義(ディレクトリパスになる)
s1200058 14:3aa6d735a2fa 92 FILE *fp;
s1200058 14:3aa6d735a2fa 93 char filename[16] = "/local/out0.txt";
s1200058 14:3aa6d735a2fa 94 */
kityann 0:eee5e3d906ce 95 /////////////////////////////////////////
kityann 0:eee5e3d906ce 96 //
kityann 0:eee5e3d906ce 97 //Send_Status
kityann 0:eee5e3d906ce 98 //
kityann 0:eee5e3d906ce 99 //リクエストがきたとき、自分の位置情報などを返信する
kityann 0:eee5e3d906ce 100 /////////////////////////////////////////
kityann 0:eee5e3d906ce 101 void Send_Status(char SenderIDc){
kityann 0:eee5e3d906ce 102 XBeeAddress64 send_Address;
kityann 0:eee5e3d906ce 103 if(SenderIDc == '0'){
kityann 0:eee5e3d906ce 104 send_Address = manager_Address;
kityann 0:eee5e3d906ce 105 }
kityann 0:eee5e3d906ce 106 if(SenderIDc >= 'A' && SenderIDc <= 'Z'){
kityann 0:eee5e3d906ce 107 send_Address = robot_Address[SenderIDc - 'A'];
kityann 0:eee5e3d906ce 108 }
kityann 0:eee5e3d906ce 109 if(SenderIDc >= 'a' && SenderIDc <= 'z'){
kityann 0:eee5e3d906ce 110 send_Address = base_Address[SenderIDc - 'a'];
kityann 0:eee5e3d906ce 111 }
kityann 0:eee5e3d906ce 112 //send normal data
kityann 0:eee5e3d906ce 113 //Create GPS Infomation Packet
kityann 1:ee2713435312 114 agz.createReceiveStatusCommand(MyID,SenderIDc,
kityann 1:ee2713435312 115 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 0:eee5e3d906ce 116 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 0:eee5e3d906ce 117 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 1:ee2713435312 118
s1200058 13:82f4f00f11f6 119 /* //debug***************************************************
kityann 1:ee2713435312 120 printf("latitude:%f,longitude:%f\nlatitudeK:%f,longitudeK:%f\nCovlat:%f,Covlongi:%f\n",
kityann 1:ee2713435312 121 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 1:ee2713435312 122 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 1:ee2713435312 123 agz.get_agzCov_lati(),agz.get_agzCov_longi()
kityann 1:ee2713435312 124 );
kityann 1:ee2713435312 125 for(int i = 0; i < RECEIVE_STATUS_COMMNAD_LENGTH; ++i) printf("%d ",agz.packetData[i]);
kityann 1:ee2713435312 126 printf("\n");
kityann 1:ee2713435312 127 //debug end***************************************************
s1200058 13:82f4f00f11f6 128 */
kityann 0:eee5e3d906ce 129 //Select Destination
kityann 0:eee5e3d906ce 130 ZBTxRequest tx64request(send_Address,agz.packetData,agz.getPacketLength());
kityann 0:eee5e3d906ce 131 //Send -> Base
kityann 0:eee5e3d906ce 132 xbee.send(tx64request);
kityann 0:eee5e3d906ce 133 }
kityann 0:eee5e3d906ce 134
kityann 0:eee5e3d906ce 135 /////////////////////////////////////////
kityann 0:eee5e3d906ce 136 //
kityann 0:eee5e3d906ce 137 //Get GPS function
kityann 0:eee5e3d906ce 138 //
kityann 0:eee5e3d906ce 139 /////////////////////////////////////////
kityann 0:eee5e3d906ce 140
kityann 0:eee5e3d906ce 141 void Get_GPS(Adafruit_GPS *myGPS){
s1200058 14:3aa6d735a2fa 142 static int flag = 0;
s1200058 14:3aa6d735a2fa 143 // static int save_counter = 0;
kityann 0:eee5e3d906ce 144
kityann 0:eee5e3d906ce 145 if (myGPS->fix) {
kityann 0:eee5e3d906ce 146 agz.nowStatus = GPS_AVAIL;
s1200058 13:82f4f00f11f6 147 agz.reNewRobotPoint(myGPS->latitudeH,myGPS->latitudeL,myGPS->longitudeH,myGPS->longitudeL);
kityann 0:eee5e3d906ce 148
s1200058 13:82f4f00f11f6 149 if(flag < COUNTER_MAX){
s1200058 13:82f4f00f11f6 150 flag++;
s1200058 13:82f4f00f11f6 151 }
s1200058 13:82f4f00f11f6 152 if(flag == 15){
s1200058 13:82f4f00f11f6 153 x_prev = agz.get_agzPoint_lati();
s1200058 13:82f4f00f11f6 154 y_prev = agz.get_agzPoint_longi();
kityann 0:eee5e3d906ce 155 }
s1200058 13:82f4f00f11f6 156
s1200058 13:82f4f00f11f6 157 if(flag >= 16){
s1200058 14:3aa6d735a2fa 158 // if(abs(x_prev - agz.get_agzPoint_lati()) < 0.001 && abs(y_prev - agz.get_agzPoint_longi()) < 0.001){
s1200058 14:3aa6d735a2fa 159 Kalman(agz.get_agzPoint_lati(), agz.get_agzPoint_longi());
s1200058 14:3aa6d735a2fa 160 /* change = 1;
s1200058 14:3aa6d735a2fa 161 }
s1200058 14:3aa6d735a2fa 162 else{
s1200058 14:3aa6d735a2fa 163 change = 0;
s1200058 14:3aa6d735a2fa 164 }
s1200058 14:3aa6d735a2fa 165
s1200058 14:3aa6d735a2fa 166 if(save_counter < 10){
s1200058 14:3aa6d735a2fa 167 fp = fopen(filename, "a");
s1200058 14:3aa6d735a2fa 168 fprintf(fp, "%d %.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
s1200058 14:3aa6d735a2fa 169 change, agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
s1200058 14:3aa6d735a2fa 170 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
s1200058 14:3aa6d735a2fa 171 agz.get_agzCov_lati(),agz.get_agzCov_longi());
s1200058 14:3aa6d735a2fa 172 fclose(fp);
s1200058 14:3aa6d735a2fa 173
s1200058 14:3aa6d735a2fa 174 if((flag - 16) % 500 == 0){
s1200058 14:3aa6d735a2fa 175 filename[10]++;
s1200058 14:3aa6d735a2fa 176 save_counter++;
s1200058 14:3aa6d735a2fa 177 }
s1200058 14:3aa6d735a2fa 178 }
s1200058 14:3aa6d735a2fa 179 */
s1200058 13:82f4f00f11f6 180 }
s1200058 13:82f4f00f11f6 181
s1200058 13:82f4f00f11f6 182 printf("%.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
s1200058 13:82f4f00f11f6 183 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
s1200058 13:82f4f00f11f6 184 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
s1200058 13:82f4f00f11f6 185 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:eee5e3d906ce 186 }
kityann 0:eee5e3d906ce 187 else agz.nowStatus = GPS_UNAVAIL;
kityann 0:eee5e3d906ce 188
kityann 0:eee5e3d906ce 189 }
kityann 0:eee5e3d906ce 190
kityann 0:eee5e3d906ce 191
kityann 0:eee5e3d906ce 192 /////////////////////////////////////////
kityann 0:eee5e3d906ce 193 //
kityann 0:eee5e3d906ce 194 //Kalman Processing
kityann 0:eee5e3d906ce 195 //
kityann 0:eee5e3d906ce 196 /////////////////////////////////////////
s1200058 13:82f4f00f11f6 197 void calc_Kalman(){
s1200058 13:82f4f00f11f6 198 //calc Kalman gain
s1200058 13:82f4f00f11f6 199 Kx = s2x_prev/(s2x_prev+s2_R);
s1200058 13:82f4f00f11f6 200 Ky = s2y_prev/(s2y_prev+s2_R);
s1200058 13:82f4f00f11f6 201 //estimate
s1200058 13:82f4f00f11f6 202 x_cur = x_prev + Kx*(zx-x_prev);
s1200058 13:82f4f00f11f6 203 y_cur = y_prev + Ky*(zy-y_prev);
s1200058 13:82f4f00f11f6 204 //calc sigma
s1200058 13:82f4f00f11f6 205 s2x_cur = s2x_prev-Kx*s2x_prev;
s1200058 13:82f4f00f11f6 206 s2y_cur = s2y_prev-Ky*s2y_prev;
kityann 0:eee5e3d906ce 207
kityann 0:eee5e3d906ce 208 }
kityann 0:eee5e3d906ce 209
s1200058 13:82f4f00f11f6 210 void Kalman(double Latitude,double Longitude){
kityann 0:eee5e3d906ce 211
s1200058 13:82f4f00f11f6 212 zx = Latitude;
s1200058 13:82f4f00f11f6 213 zy = Longitude;
kityann 0:eee5e3d906ce 214
s1200058 13:82f4f00f11f6 215 calc_Kalman();
kityann 0:eee5e3d906ce 216
s1200058 13:82f4f00f11f6 217 //更新
s1200058 13:82f4f00f11f6 218 x_prev = x_cur;
s1200058 13:82f4f00f11f6 219 y_prev = y_cur;
s1200058 13:82f4f00f11f6 220 s2x_prev = s2x_cur;
s1200058 13:82f4f00f11f6 221 s2y_prev = s2y_cur;
kityann 2:d97700414618 222
s1200058 13:82f4f00f11f6 223 //agzPontKalmanとagzCovに格納する
s1200058 13:82f4f00f11f6 224 agz.set_agzPointKalman_lati(x_cur);
s1200058 13:82f4f00f11f6 225 agz.set_agzPointKalman_longi(y_cur);
s1200058 13:82f4f00f11f6 226 agz.set_agzCov(s2x_cur,s2y_cur);
kityann 0:eee5e3d906ce 227
kityann 0:eee5e3d906ce 228 }
kityann 0:eee5e3d906ce 229
kityann 0:eee5e3d906ce 230
kityann 0:eee5e3d906ce 231 /////////////////////////////////////////
kityann 0:eee5e3d906ce 232 //
kityann 0:eee5e3d906ce 233 //Main Processing
kityann 0:eee5e3d906ce 234 //
kityann 0:eee5e3d906ce 235 /////////////////////////////////////////
kityann 0:eee5e3d906ce 236 int main() {
kityann 0:eee5e3d906ce 237 //start up time
kityann 0:eee5e3d906ce 238 wait(3);
kityann 0:eee5e3d906ce 239 //set pc frequency to 57600bps
kityann 0:eee5e3d906ce 240 pc.baud(PC_BAUD_RATE);
kityann 0:eee5e3d906ce 241 //set xbee frequency to 57600bps
kityann 0:eee5e3d906ce 242 xbee.begin(XBEE_BAUD_RATE);
kityann 0:eee5e3d906ce 243
kityann 0:eee5e3d906ce 244
kityann 0:eee5e3d906ce 245 //GPS setting
kityann 0:eee5e3d906ce 246 gps_Serial = new Serial(p28,p27);
kityann 0:eee5e3d906ce 247 Adafruit_GPS myGPS(gps_Serial);
kityann 0:eee5e3d906ce 248 Timer refresh_Timer;
s1200058 3:60bad3679b4b 249 const int refresh_Time = 1000; //refresh time in ms
kityann 0:eee5e3d906ce 250 myGPS.begin(GPS_BAUD_RATE);
kityann 0:eee5e3d906ce 251
kityann 0:eee5e3d906ce 252 char SenderIDc;
kityann 0:eee5e3d906ce 253 //GPS Send Command
kityann 0:eee5e3d906ce 254 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:eee5e3d906ce 255 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:eee5e3d906ce 256 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:eee5e3d906ce 257
kityann 0:eee5e3d906ce 258 wait(2);
kityann 0:eee5e3d906ce 259
kityann 0:eee5e3d906ce 260 //interrupt start
kityann 0:eee5e3d906ce 261 refresh_Timer.start();
kityann 0:eee5e3d906ce 262
kityann 0:eee5e3d906ce 263 printf("start\n");
kityann 0:eee5e3d906ce 264
kityann 0:eee5e3d906ce 265 while (true) {
kityann 0:eee5e3d906ce 266
kityann 0:eee5e3d906ce 267 //Check Xbee Buffer Available
kityann 0:eee5e3d906ce 268 xbee.readPacket();
kityann 0:eee5e3d906ce 269
kityann 0:eee5e3d906ce 270 if (xbee.getResponse().isAvailable()) {
kityann 0:eee5e3d906ce 271 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 272 uint8_t *buf = zbRx.getFrameData();
kityann 0:eee5e3d906ce 273
kityann 0:eee5e3d906ce 274 if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
kityann 0:eee5e3d906ce 275 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 276 uint8_t *buf = zbRx.getFrameData();//フレームデータを格納する
kityann 0:eee5e3d906ce 277 uint8_t *buf1 = &buf[11];//データの部分のみを格納する
kityann 0:eee5e3d906ce 278 SenderIDc = buf1[5];//送信元のIDを取得する
kityann 0:eee5e3d906ce 279 char Command_type =agz.checkCommnadType(buf);//コマンドタイプを取得する
kityann 0:eee5e3d906ce 280
kityann 0:eee5e3d906ce 281 //Check Command Type
kityann 0:eee5e3d906ce 282 switch(Command_type){
kityann 0:eee5e3d906ce 283 //Get Request command
kityann 0:eee5e3d906ce 284 case STATUS_REQUEST:{
kityann 1:ee2713435312 285 Send_Status(SenderIDc);
kityann 0:eee5e3d906ce 286 break;
kityann 0:eee5e3d906ce 287 }
kityann 0:eee5e3d906ce 288 default:{
kityann 0:eee5e3d906ce 289 break;
kityann 0:eee5e3d906ce 290 }
kityann 0:eee5e3d906ce 291 }//endswitch
kityann 0:eee5e3d906ce 292 }//endifZB_RX_RESPONSE
kityann 0:eee5e3d906ce 293 }//endifisAvailable
kityann 0:eee5e3d906ce 294
kityann 0:eee5e3d906ce 295 myGPS.read();
kityann 0:eee5e3d906ce 296 //recive gps module
kityann 0:eee5e3d906ce 297 //check if we recieved a new message from GPS, if so, attempt to parse it,
kityann 0:eee5e3d906ce 298 if ( myGPS.newNMEAreceived() ) {
kityann 0:eee5e3d906ce 299 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kityann 0:eee5e3d906ce 300 continue;
kityann 0:eee5e3d906ce 301 }
kityann 0:eee5e3d906ce 302 }
kityann 0:eee5e3d906ce 303
kityann 0:eee5e3d906ce 304
kityann 0:eee5e3d906ce 305 if (refresh_Timer.read_ms() >= refresh_Time) {
kityann 0:eee5e3d906ce 306 refresh_Timer.reset();
kityann 0:eee5e3d906ce 307 Get_GPS(&myGPS);
kityann 1:ee2713435312 308
kityann 0:eee5e3d906ce 309 }
kityann 0:eee5e3d906ce 310 }
kityann 0:eee5e3d906ce 311 }