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:
Wed May 13 12:58:27 2015 +0000
Revision:
3:60bad3679b4b
Parent:
2:d97700414618
Child:
4:a1faa62fb49b
2015/05/13

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進数に変換するようにした
kityann 0:eee5e3d906ce 19 /**********************************************/
kityann 0:eee5e3d906ce 20
kityann 0:eee5e3d906ce 21 #include "mbed.h"
kityann 0:eee5e3d906ce 22 #include "XBee.h"
kityann 0:eee5e3d906ce 23 #include "MBed_Adafruit_GPS.h"
kityann 0:eee5e3d906ce 24 #include "AigamozuControlPackets.h"
kityann 0:eee5e3d906ce 25 #include "agzIDLIST.h"
kityann 0:eee5e3d906ce 26 #include "aigamozuSetting.h"
kityann 0:eee5e3d906ce 27 #include "agz_common.h"
kityann 0:eee5e3d906ce 28 #include "Kalman.h"
kityann 0:eee5e3d906ce 29
kityann 2:d97700414618 30
kityann 2:d97700414618 31 #define SIGMA_MIN 0.0001
kityann 2:d97700414618 32
kityann 0:eee5e3d906ce 33 //************ID Number*****************
kityann 0:eee5e3d906ce 34 const char MyID = 'D';
kityann 0:eee5e3d906ce 35 //************ID Number*****************
kityann 0:eee5e3d906ce 36
kityann 0:eee5e3d906ce 37 /////////////////////////////////////////
kityann 0:eee5e3d906ce 38 //
kityann 0:eee5e3d906ce 39 //Pin Setting
kityann 0:eee5e3d906ce 40 //
kityann 0:eee5e3d906ce 41 /////////////////////////////////////////
kityann 0:eee5e3d906ce 42 VNH5019 agz_motorShield(p21,p22,p23,p24,p25,p26);
kityann 0:eee5e3d906ce 43
kityann 0:eee5e3d906ce 44
kityann 0:eee5e3d906ce 45 /////////////////////////////////////////
kityann 0:eee5e3d906ce 46 //
kityann 0:eee5e3d906ce 47 //Connection Setting
kityann 0:eee5e3d906ce 48 //
kityann 0:eee5e3d906ce 49 /////////////////////////////////////////
kityann 0:eee5e3d906ce 50
kityann 0:eee5e3d906ce 51 //Serial Connect Setting: PC <--> mbed
kityann 0:eee5e3d906ce 52 Serial pc(USBTX, USBRX);
kityann 0:eee5e3d906ce 53
kityann 0:eee5e3d906ce 54 //Serial Connect Setting: GPS <--> mbed
kityann 0:eee5e3d906ce 55 Serial * gps_Serial;
kityann 0:eee5e3d906ce 56
kityann 0:eee5e3d906ce 57 //Serial Connect Setting: XBEE <--> mbed
kityann 0:eee5e3d906ce 58 XBee xbee(p13,p14);
kityann 0:eee5e3d906ce 59 ZBRxResponse zbRx = ZBRxResponse();
kityann 0:eee5e3d906ce 60
kityann 0:eee5e3d906ce 61 //set up GPS module
kityann 0:eee5e3d906ce 62
kityann 0:eee5e3d906ce 63 //set up AigamozuControlPackets library
kityann 0:eee5e3d906ce 64 AigamozuControlPackets agz(agz_motorShield);
kityann 0:eee5e3d906ce 65
kityann 0:eee5e3d906ce 66
kityann 0:eee5e3d906ce 67 /////////////////////////////////////////
kityann 0:eee5e3d906ce 68 //
kityann 0:eee5e3d906ce 69 //For Kalman data
kityann 0:eee5e3d906ce 70 //
kityann 0:eee5e3d906ce 71 /////////////////////////////////////////
kityann 0:eee5e3d906ce 72 double sigmaGPS[2][2] = {{250,0},{0,250}};
kityann 0:eee5e3d906ce 73 double K[2][2][2]= {{{1,0},{0,1}},{{1,0},{0,1}}};
kityann 0:eee5e3d906ce 74 double sigma[2][2][2]= {{{250,0},{0,250}},{{250,0},{0,250}}};
kityann 0:eee5e3d906ce 75 double y[2],x[2][2]={0};
kityann 0:eee5e3d906ce 76 void Kalman(double Latitude,double Longitude,Adafruit_GPS *myGPS);
kityann 0:eee5e3d906ce 77
kityann 0:eee5e3d906ce 78 /////////////////////////////////////////
kityann 0:eee5e3d906ce 79 //
kityann 0:eee5e3d906ce 80 //Address List
kityann 0:eee5e3d906ce 81 //
kityann 0:eee5e3d906ce 82 /////////////////////////////////////////
kityann 0:eee5e3d906ce 83 XBeeAddress64 base_Address[BaseNumber] = {XBeeAddress64(BASE1_32H,BASE1_32L), XBeeAddress64(BASE2_32H,BASE2_32L),
kityann 0:eee5e3d906ce 84 XBeeAddress64(BASE3_32H,BASE3_32L), XBeeAddress64(BASE4_32H,BASE4_32L),
kityann 0:eee5e3d906ce 85 XBeeAddress64(BASE5_32H,BASE5_32L)};
kityann 0:eee5e3d906ce 86 XBeeAddress64 robot_Address[RobotNumber] = {XBeeAddress64(ROBOT1_32H,ROBOT1_32L), XBeeAddress64(ROBOT1_32H,ROBOT1_32L)};
kityann 0:eee5e3d906ce 87 XBeeAddress64 manager_Address = XBeeAddress64(BASE1_32H,BASE1_32L);
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
kityann 1:ee2713435312 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***************************************************
kityann 1:ee2713435312 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){
kityann 0:eee5e3d906ce 137 static bool flag = true;
kityann 0:eee5e3d906ce 138
kityann 0:eee5e3d906ce 139 if (myGPS->fix) {
kityann 0:eee5e3d906ce 140 agz.nowStatus = GPS_AVAIL;
kityann 0:eee5e3d906ce 141
kityann 0:eee5e3d906ce 142 if(flag){//初期値設定
kityann 1:ee2713435312 143 if(myGPS->latitudeH>=36 && myGPS->latitudeH<=38 && myGPS->longitudeH>=138 && myGPS->longitudeH<=141){
kityann 0:eee5e3d906ce 144 flag = false;
kityann 0:eee5e3d906ce 145 x[0][0]=(double)myGPS->latitudeL;
kityann 0:eee5e3d906ce 146 x[0][1]=(double)myGPS->longitudeL;
kityann 0:eee5e3d906ce 147 }
kityann 0:eee5e3d906ce 148 }
kityann 0:eee5e3d906ce 149
kityann 0:eee5e3d906ce 150 if(myGPS->longitudeH<138 || myGPS->longitudeH>141 || myGPS->latitudeH<36 || myGPS->latitudeH>38){
kityann 0:eee5e3d906ce 151 return;
kityann 0:eee5e3d906ce 152 }
kityann 0:eee5e3d906ce 153 //Kalman Filter
kityann 0:eee5e3d906ce 154 Kalman(myGPS->latitudeL,myGPS->longitudeL,myGPS);
kityann 0:eee5e3d906ce 155
kityann 1:ee2713435312 156 agz.reNewRobotPoint(myGPS->latitudeH,myGPS->latitudeL,myGPS->longitudeH,myGPS->longitudeL);
kityann 1:ee2713435312 157 agz.reNewRobotPointKalman(myGPS->latitudeKH,myGPS->latitudeKL,myGPS->longitudeKH,myGPS->longitudeKL);
kityann 0:eee5e3d906ce 158 }
kityann 0:eee5e3d906ce 159 else agz.nowStatus = GPS_UNAVAIL;
kityann 0:eee5e3d906ce 160
kityann 0:eee5e3d906ce 161 }
kityann 0:eee5e3d906ce 162
kityann 0:eee5e3d906ce 163
kityann 0:eee5e3d906ce 164 /////////////////////////////////////////
kityann 0:eee5e3d906ce 165 //
kityann 0:eee5e3d906ce 166 //Kalman Processing
kityann 0:eee5e3d906ce 167 //
kityann 0:eee5e3d906ce 168 /////////////////////////////////////////
kityann 0:eee5e3d906ce 169
kityann 0:eee5e3d906ce 170 void get_K(){
kityann 0:eee5e3d906ce 171 double temp[2][2]={
kityann 0:eee5e3d906ce 172 {sigma[0][0][0]+sigmaGPS[0][0],sigma[0][0][1]+sigmaGPS[0][1]},
kityann 0:eee5e3d906ce 173 {sigma[0][1][0]+sigmaGPS[1][0],sigma[0][1][1]+sigmaGPS[1][1]}
kityann 0:eee5e3d906ce 174 };
kityann 0:eee5e3d906ce 175 double ad_bc = temp[0][0]*temp[1][1]-temp[1][0]*temp[0][1];
kityann 0:eee5e3d906ce 176 K[1][0][0] = sigma[0][0][0]*(1/ad_bc)*(temp[1][1]);
kityann 0:eee5e3d906ce 177 K[1][1][1] = sigma[0][1][1]*(1/ad_bc)*(temp[0][0]);
kityann 0:eee5e3d906ce 178 }
kityann 0:eee5e3d906ce 179
kityann 0:eee5e3d906ce 180
kityann 0:eee5e3d906ce 181 void get_x(){
kityann 0:eee5e3d906ce 182 x[1][0] = x[0][0]+K[1][0][0]*(y[0]-x[0][0]);
kityann 0:eee5e3d906ce 183 x[1][1] = x[0][1]+K[1][1][1]*(y[1]-x[0][1]);
kityann 0:eee5e3d906ce 184 }
kityann 0:eee5e3d906ce 185
kityann 0:eee5e3d906ce 186
kityann 0:eee5e3d906ce 187 void get_sigma(){
kityann 0:eee5e3d906ce 188 double temp[2][2];
kityann 0:eee5e3d906ce 189 for(int i=0;i<2;i++) {
kityann 0:eee5e3d906ce 190 for(int j=0;j<2;j++) {
kityann 0:eee5e3d906ce 191 for(int k=0;k<2;k++) {
kityann 0:eee5e3d906ce 192 temp[i][j]+=K[1][i][k]*sigma[0][k][j];
kityann 0:eee5e3d906ce 193 }
kityann 0:eee5e3d906ce 194 }
kityann 0:eee5e3d906ce 195 }
kityann 0:eee5e3d906ce 196 for(int i = 0;i < 2;i++){
kityann 0:eee5e3d906ce 197 for(int j = 0;j < 2;j++){
kityann 0:eee5e3d906ce 198 sigma[1][i][j] = sigma[0][i][j]-temp[i][j];
kityann 0:eee5e3d906ce 199 }
kityann 0:eee5e3d906ce 200 }
kityann 0:eee5e3d906ce 201 }
kityann 0:eee5e3d906ce 202
kityann 0:eee5e3d906ce 203 void Kalman(double Latitude,double Longitude,Adafruit_GPS *myGPS){
kityann 0:eee5e3d906ce 204 y[0] = Latitude;
kityann 0:eee5e3d906ce 205 y[1] = Longitude;
kityann 0:eee5e3d906ce 206 //K[t+1] = sigma[t]*Inverse[sigma[t]+sigmaGPS];
kityann 0:eee5e3d906ce 207 get_K();
kityann 0:eee5e3d906ce 208 //x[t+1] = x[t]+K[t+1]*(y[t*1]-x[t]);
kityann 0:eee5e3d906ce 209 get_x();
kityann 0:eee5e3d906ce 210 //sigma[t+1] = sigma[t]-K[t+1]*sigma[t];
kityann 0:eee5e3d906ce 211 get_sigma();
kityann 0:eee5e3d906ce 212
kityann 0:eee5e3d906ce 213
kityann 0:eee5e3d906ce 214 //kousinn
kityann 0:eee5e3d906ce 215 for(int i = 0;i < 2;i++){
kityann 0:eee5e3d906ce 216 for(int j = 0;j < 2;j++){
kityann 0:eee5e3d906ce 217 K[0][i][j]=K[1][i][j];
kityann 0:eee5e3d906ce 218 x[0][i]=x[1][i];
kityann 0:eee5e3d906ce 219 sigma[0][i][j]=sigma[1][i][j];
kityann 0:eee5e3d906ce 220 }
kityann 0:eee5e3d906ce 221 }
kityann 2:d97700414618 222
kityann 2:d97700414618 223 if(sigma[0][0][0] < SIGMA_MIN)sigma[0][0][0]=SIGMA_MIN;
kityann 2:d97700414618 224 if(sigma[0][1][1] < SIGMA_MIN)sigma[0][1][1]=SIGMA_MIN;
kityann 0:eee5e3d906ce 225
kityann 0:eee5e3d906ce 226 myGPS->latitudeKH=myGPS->latitudeH;//latitude after filtering
kityann 0:eee5e3d906ce 227 myGPS->longitudeKH=myGPS->longitudeH;//longitude after filtering
kityann 0:eee5e3d906ce 228 myGPS->latitudeKL=(long)x[1][0];//latitude after filtering
kityann 0:eee5e3d906ce 229 myGPS->longitudeKL=(long)x[1][1];//longitude after filtering
kityann 0:eee5e3d906ce 230
kityann 0:eee5e3d906ce 231 agz.set_agzCov(sigma[0][0][0],sigma[0][1][1]);
kityann 0:eee5e3d906ce 232 }
kityann 0:eee5e3d906ce 233
kityann 0:eee5e3d906ce 234
kityann 0:eee5e3d906ce 235 /////////////////////////////////////////
kityann 0:eee5e3d906ce 236 //
kityann 0:eee5e3d906ce 237 //Main Processing
kityann 0:eee5e3d906ce 238 //
kityann 0:eee5e3d906ce 239 /////////////////////////////////////////
kityann 0:eee5e3d906ce 240 int main() {
kityann 0:eee5e3d906ce 241 //start up time
kityann 0:eee5e3d906ce 242 wait(3);
kityann 0:eee5e3d906ce 243 //set pc frequency to 57600bps
kityann 0:eee5e3d906ce 244 pc.baud(PC_BAUD_RATE);
kityann 0:eee5e3d906ce 245 //set xbee frequency to 57600bps
kityann 0:eee5e3d906ce 246 xbee.begin(XBEE_BAUD_RATE);
kityann 0:eee5e3d906ce 247
kityann 0:eee5e3d906ce 248
kityann 0:eee5e3d906ce 249 //GPS setting
kityann 0:eee5e3d906ce 250 gps_Serial = new Serial(p28,p27);
kityann 0:eee5e3d906ce 251 Adafruit_GPS myGPS(gps_Serial);
kityann 0:eee5e3d906ce 252 Timer refresh_Timer;
s1200058 3:60bad3679b4b 253 const int refresh_Time = 1000; //refresh time in ms
kityann 0:eee5e3d906ce 254 myGPS.begin(GPS_BAUD_RATE);
kityann 0:eee5e3d906ce 255
kityann 0:eee5e3d906ce 256 char SenderIDc;
kityann 0:eee5e3d906ce 257 //GPS Send Command
kityann 0:eee5e3d906ce 258 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:eee5e3d906ce 259 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:eee5e3d906ce 260 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:eee5e3d906ce 261
kityann 0:eee5e3d906ce 262 wait(2);
kityann 0:eee5e3d906ce 263
kityann 0:eee5e3d906ce 264 //interrupt start
kityann 0:eee5e3d906ce 265 refresh_Timer.start();
kityann 0:eee5e3d906ce 266
kityann 0:eee5e3d906ce 267 printf("start\n");
kityann 0:eee5e3d906ce 268
kityann 0:eee5e3d906ce 269 while (true) {
kityann 0:eee5e3d906ce 270
kityann 0:eee5e3d906ce 271 //Check Xbee Buffer Available
kityann 0:eee5e3d906ce 272 xbee.readPacket();
kityann 0:eee5e3d906ce 273
kityann 0:eee5e3d906ce 274 if (xbee.getResponse().isAvailable()) {
kityann 0:eee5e3d906ce 275 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 276 uint8_t *buf = zbRx.getFrameData();
kityann 0:eee5e3d906ce 277
kityann 0:eee5e3d906ce 278 if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
kityann 0:eee5e3d906ce 279 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:eee5e3d906ce 280 uint8_t *buf = zbRx.getFrameData();//フレームデータを格納する
kityann 0:eee5e3d906ce 281 uint8_t *buf1 = &buf[11];//データの部分のみを格納する
kityann 0:eee5e3d906ce 282 SenderIDc = buf1[5];//送信元のIDを取得する
kityann 0:eee5e3d906ce 283 char Command_type =agz.checkCommnadType(buf);//コマンドタイプを取得する
kityann 0:eee5e3d906ce 284
kityann 0:eee5e3d906ce 285 //Check Command Type
kityann 0:eee5e3d906ce 286 switch(Command_type){
kityann 0:eee5e3d906ce 287 //Get Request command
kityann 0:eee5e3d906ce 288 case STATUS_REQUEST:{
kityann 1:ee2713435312 289 Send_Status(SenderIDc);
kityann 0:eee5e3d906ce 290 break;
kityann 0:eee5e3d906ce 291 }
kityann 0:eee5e3d906ce 292 default:{
kityann 0:eee5e3d906ce 293 break;
kityann 0:eee5e3d906ce 294 }
kityann 0:eee5e3d906ce 295 }//endswitch
kityann 0:eee5e3d906ce 296 }//endifZB_RX_RESPONSE
kityann 0:eee5e3d906ce 297 }//endifisAvailable
kityann 0:eee5e3d906ce 298
kityann 0:eee5e3d906ce 299 myGPS.read();
kityann 0:eee5e3d906ce 300 //recive gps module
kityann 0:eee5e3d906ce 301 //check if we recieved a new message from GPS, if so, attempt to parse it,
kityann 0:eee5e3d906ce 302 if ( myGPS.newNMEAreceived() ) {
kityann 0:eee5e3d906ce 303 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kityann 0:eee5e3d906ce 304 continue;
kityann 0:eee5e3d906ce 305 }
kityann 0:eee5e3d906ce 306 }
kityann 0:eee5e3d906ce 307
kityann 0:eee5e3d906ce 308
kityann 0:eee5e3d906ce 309 if (refresh_Timer.read_ms() >= refresh_Time) {
kityann 0:eee5e3d906ce 310 refresh_Timer.reset();
kityann 0:eee5e3d906ce 311 Get_GPS(&myGPS);
kityann 1:ee2713435312 312
kityann 0:eee5e3d906ce 313 }
kityann 0:eee5e3d906ce 314 }
kityann 0:eee5e3d906ce 315 }