2016_05_19 change manager

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:
Sun May 24 08:10:08 2015 +0000
Revision:
13:82f4f00f11f6
Parent:
12:83be8a3c212d
Child:
14:3aa6d735a2fa
change a fuction that calculate 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 13:82f4f00f11f6 42 const char MyID = 'b';
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);
kityann 0:eee5e3d906ce 88
kityann 0:eee5e3d906ce 89
kityann 0:eee5e3d906ce 90 /////////////////////////////////////////
kityann 0:eee5e3d906ce 91 //
kityann 0:eee5e3d906ce 92 //Send_Status
kityann 0:eee5e3d906ce 93 //
kityann 0:eee5e3d906ce 94 //リクエストがきたとき、自分の位置情報などを返信する
kityann 0:eee5e3d906ce 95 /////////////////////////////////////////
kityann 0:eee5e3d906ce 96 void Send_Status(char SenderIDc){
kityann 0:eee5e3d906ce 97 XBeeAddress64 send_Address;
kityann 0:eee5e3d906ce 98 if(SenderIDc == '0'){
kityann 0:eee5e3d906ce 99 send_Address = manager_Address;
kityann 0:eee5e3d906ce 100 }
kityann 0:eee5e3d906ce 101 if(SenderIDc >= 'A' && SenderIDc <= 'Z'){
kityann 0:eee5e3d906ce 102 send_Address = robot_Address[SenderIDc - 'A'];
kityann 0:eee5e3d906ce 103 }
kityann 0:eee5e3d906ce 104 if(SenderIDc >= 'a' && SenderIDc <= 'z'){
kityann 0:eee5e3d906ce 105 send_Address = base_Address[SenderIDc - 'a'];
kityann 0:eee5e3d906ce 106 }
kityann 0:eee5e3d906ce 107 //send normal data
kityann 0:eee5e3d906ce 108 //Create GPS Infomation Packet
kityann 1:ee2713435312 109 agz.createReceiveStatusCommand(MyID,SenderIDc,
kityann 1:ee2713435312 110 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 0:eee5e3d906ce 111 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 0:eee5e3d906ce 112 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 1:ee2713435312 113
s1200058 13:82f4f00f11f6 114 /* //debug***************************************************
kityann 1:ee2713435312 115 printf("latitude:%f,longitude:%f\nlatitudeK:%f,longitudeK:%f\nCovlat:%f,Covlongi:%f\n",
kityann 1:ee2713435312 116 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 1:ee2713435312 117 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 1:ee2713435312 118 agz.get_agzCov_lati(),agz.get_agzCov_longi()
kityann 1:ee2713435312 119 );
kityann 1:ee2713435312 120 for(int i = 0; i < RECEIVE_STATUS_COMMNAD_LENGTH; ++i) printf("%d ",agz.packetData[i]);
kityann 1:ee2713435312 121 printf("\n");
kityann 1:ee2713435312 122 //debug end***************************************************
s1200058 13:82f4f00f11f6 123 */
kityann 0:eee5e3d906ce 124 //Select Destination
kityann 0:eee5e3d906ce 125 ZBTxRequest tx64request(send_Address,agz.packetData,agz.getPacketLength());
kityann 0:eee5e3d906ce 126 //Send -> Base
kityann 0:eee5e3d906ce 127 xbee.send(tx64request);
kityann 0:eee5e3d906ce 128 }
kityann 0:eee5e3d906ce 129
kityann 0:eee5e3d906ce 130 /////////////////////////////////////////
kityann 0:eee5e3d906ce 131 //
kityann 0:eee5e3d906ce 132 //Get GPS function
kityann 0:eee5e3d906ce 133 //
kityann 0:eee5e3d906ce 134 /////////////////////////////////////////
kityann 0:eee5e3d906ce 135
kityann 0:eee5e3d906ce 136 void Get_GPS(Adafruit_GPS *myGPS){
s1200058 13:82f4f00f11f6 137 static int flag = 0;
kityann 0:eee5e3d906ce 138
kityann 0:eee5e3d906ce 139 if (myGPS->fix) {
kityann 0:eee5e3d906ce 140 agz.nowStatus = GPS_AVAIL;
s1200058 13:82f4f00f11f6 141 agz.reNewRobotPoint(myGPS->latitudeH,myGPS->latitudeL,myGPS->longitudeH,myGPS->longitudeL);
kityann 0:eee5e3d906ce 142
s1200058 13:82f4f00f11f6 143 if(flag < COUNTER_MAX){
s1200058 13:82f4f00f11f6 144 flag++;
s1200058 13:82f4f00f11f6 145 }
s1200058 13:82f4f00f11f6 146 if(flag == 15){
s1200058 13:82f4f00f11f6 147 x_prev = agz.get_agzPoint_lati();
s1200058 13:82f4f00f11f6 148 y_prev = agz.get_agzPoint_longi();
kityann 0:eee5e3d906ce 149 }
s1200058 13:82f4f00f11f6 150
s1200058 13:82f4f00f11f6 151 if(flag >= 16){
s1200058 13:82f4f00f11f6 152 Kalman(agz.get_agzPoint_lati(), agz.get_agzPoint_longi());
s1200058 13:82f4f00f11f6 153 }
s1200058 13:82f4f00f11f6 154
s1200058 13:82f4f00f11f6 155 printf("%.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
s1200058 13:82f4f00f11f6 156 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
s1200058 13:82f4f00f11f6 157 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
s1200058 13:82f4f00f11f6 158 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:eee5e3d906ce 159 }
kityann 0:eee5e3d906ce 160 else agz.nowStatus = GPS_UNAVAIL;
kityann 0:eee5e3d906ce 161
kityann 0:eee5e3d906ce 162 }
kityann 0:eee5e3d906ce 163
kityann 0:eee5e3d906ce 164
kityann 0:eee5e3d906ce 165 /////////////////////////////////////////
kityann 0:eee5e3d906ce 166 //
kityann 0:eee5e3d906ce 167 //Kalman Processing
kityann 0:eee5e3d906ce 168 //
kityann 0:eee5e3d906ce 169 /////////////////////////////////////////
s1200058 13:82f4f00f11f6 170 void calc_Kalman(){
s1200058 13:82f4f00f11f6 171 //calc Kalman gain
s1200058 13:82f4f00f11f6 172 Kx = s2x_prev/(s2x_prev+s2_R);
s1200058 13:82f4f00f11f6 173 Ky = s2y_prev/(s2y_prev+s2_R);
s1200058 13:82f4f00f11f6 174 //estimate
s1200058 13:82f4f00f11f6 175 x_cur = x_prev + Kx*(zx-x_prev);
s1200058 13:82f4f00f11f6 176 y_cur = y_prev + Ky*(zy-y_prev);
s1200058 13:82f4f00f11f6 177 //calc sigma
s1200058 13:82f4f00f11f6 178 s2x_cur = s2x_prev-Kx*s2x_prev;
s1200058 13:82f4f00f11f6 179 s2y_cur = s2y_prev-Ky*s2y_prev;
kityann 0:eee5e3d906ce 180
kityann 0:eee5e3d906ce 181 }
kityann 0:eee5e3d906ce 182
s1200058 13:82f4f00f11f6 183 void Kalman(double Latitude,double Longitude){
kityann 0:eee5e3d906ce 184
s1200058 13:82f4f00f11f6 185 zx = Latitude;
s1200058 13:82f4f00f11f6 186 zy = Longitude;
kityann 0:eee5e3d906ce 187
s1200058 13:82f4f00f11f6 188 calc_Kalman();
kityann 0:eee5e3d906ce 189
s1200058 13:82f4f00f11f6 190 //更新
s1200058 13:82f4f00f11f6 191 x_prev = x_cur;
s1200058 13:82f4f00f11f6 192 y_prev = y_cur;
s1200058 13:82f4f00f11f6 193 s2x_prev = s2x_cur;
s1200058 13:82f4f00f11f6 194 s2y_prev = s2y_cur;
kityann 2:d97700414618 195
s1200058 13:82f4f00f11f6 196 //agzPontKalmanとagzCovに格納する
s1200058 13:82f4f00f11f6 197 agz.set_agzPointKalman_lati(x_cur);
s1200058 13:82f4f00f11f6 198 agz.set_agzPointKalman_longi(y_cur);
s1200058 13:82f4f00f11f6 199 agz.set_agzCov(s2x_cur,s2y_cur);
kityann 0:eee5e3d906ce 200
kityann 0:eee5e3d906ce 201 }
kityann 0:eee5e3d906ce 202
kityann 0:eee5e3d906ce 203
kityann 0:eee5e3d906ce 204 /////////////////////////////////////////
kityann 0:eee5e3d906ce 205 //
kityann 0:eee5e3d906ce 206 //Main Processing
kityann 0:eee5e3d906ce 207 //
kityann 0:eee5e3d906ce 208 /////////////////////////////////////////
kityann 0:eee5e3d906ce 209 int main() {
kityann 0:eee5e3d906ce 210 //start up time
kityann 0:eee5e3d906ce 211 wait(3);
kityann 0:eee5e3d906ce 212 //set pc frequency to 57600bps
kityann 0:eee5e3d906ce 213 pc.baud(PC_BAUD_RATE);
kityann 0:eee5e3d906ce 214 //set xbee frequency to 57600bps
kityann 0:eee5e3d906ce 215 xbee.begin(XBEE_BAUD_RATE);
kityann 0:eee5e3d906ce 216
kityann 0:eee5e3d906ce 217
kityann 0:eee5e3d906ce 218 //GPS setting
kityann 0:eee5e3d906ce 219 gps_Serial = new Serial(p28,p27);
kityann 0:eee5e3d906ce 220 Adafruit_GPS myGPS(gps_Serial);
kityann 0:eee5e3d906ce 221 Timer refresh_Timer;
s1200058 3:60bad3679b4b 222 const int refresh_Time = 1000; //refresh time in ms
kityann 0:eee5e3d906ce 223 myGPS.begin(GPS_BAUD_RATE);
kityann 0:eee5e3d906ce 224
kityann 0:eee5e3d906ce 225 char SenderIDc;
kityann 0:eee5e3d906ce 226 //GPS Send Command
kityann 0:eee5e3d906ce 227 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:eee5e3d906ce 228 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:eee5e3d906ce 229 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:eee5e3d906ce 230
kityann 0:eee5e3d906ce 231 wait(2);
kityann 0:eee5e3d906ce 232
kityann 0:eee5e3d906ce 233 //interrupt start
kityann 0:eee5e3d906ce 234 refresh_Timer.start();
kityann 0:eee5e3d906ce 235
kityann 0:eee5e3d906ce 236 printf("start\n");
kityann 0:eee5e3d906ce 237
kityann 0:eee5e3d906ce 238 while (true) {
kityann 0:eee5e3d906ce 239
kityann 0:eee5e3d906ce 240 //Check Xbee Buffer Available
kityann 0:eee5e3d906ce 241 xbee.readPacket();
kityann 0:eee5e3d906ce 242
kityann 0:eee5e3d906ce 243 if (xbee.getResponse().isAvailable()) {
kityann 0:eee5e3d906ce 244 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 245 uint8_t *buf = zbRx.getFrameData();
kityann 0:eee5e3d906ce 246
kityann 0:eee5e3d906ce 247 if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
kityann 0:eee5e3d906ce 248 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 249 uint8_t *buf = zbRx.getFrameData();//フレームデータを格納する
kityann 0:eee5e3d906ce 250 uint8_t *buf1 = &buf[11];//データの部分のみを格納する
kityann 0:eee5e3d906ce 251 SenderIDc = buf1[5];//送信元のIDを取得する
kityann 0:eee5e3d906ce 252 char Command_type =agz.checkCommnadType(buf);//コマンドタイプを取得する
kityann 0:eee5e3d906ce 253
kityann 0:eee5e3d906ce 254 //Check Command Type
kityann 0:eee5e3d906ce 255 switch(Command_type){
kityann 0:eee5e3d906ce 256 //Get Request command
kityann 0:eee5e3d906ce 257 case STATUS_REQUEST:{
kityann 1:ee2713435312 258 Send_Status(SenderIDc);
kityann 0:eee5e3d906ce 259 break;
kityann 0:eee5e3d906ce 260 }
kityann 0:eee5e3d906ce 261 default:{
kityann 0:eee5e3d906ce 262 break;
kityann 0:eee5e3d906ce 263 }
kityann 0:eee5e3d906ce 264 }//endswitch
kityann 0:eee5e3d906ce 265 }//endifZB_RX_RESPONSE
kityann 0:eee5e3d906ce 266 }//endifisAvailable
kityann 0:eee5e3d906ce 267
kityann 0:eee5e3d906ce 268 myGPS.read();
kityann 0:eee5e3d906ce 269 //recive gps module
kityann 0:eee5e3d906ce 270 //check if we recieved a new message from GPS, if so, attempt to parse it,
kityann 0:eee5e3d906ce 271 if ( myGPS.newNMEAreceived() ) {
kityann 0:eee5e3d906ce 272 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kityann 0:eee5e3d906ce 273 continue;
kityann 0:eee5e3d906ce 274 }
kityann 0:eee5e3d906ce 275 }
kityann 0:eee5e3d906ce 276
kityann 0:eee5e3d906ce 277
kityann 0:eee5e3d906ce 278 if (refresh_Timer.read_ms() >= refresh_Time) {
kityann 0:eee5e3d906ce 279 refresh_Timer.reset();
kityann 0:eee5e3d906ce 280 Get_GPS(&myGPS);
kityann 1:ee2713435312 281
kityann 0:eee5e3d906ce 282 }
kityann 0:eee5e3d906ce 283 }
kityann 0:eee5e3d906ce 284 }