2015/06/09

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

Fork of Aigamozu_Robot_ver4_4 by aigamozu

Committer:
s1200058
Date:
Sun May 24 08:06:17 2015 +0000
Revision:
33:3025b16bccd2
Parent:
32:6ba2e5402f00
Child:
34:9c3192c00e41
change kalman

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kityann 0:daab5accfd83 1 /**********************************************/
kityann 0:daab5accfd83 2 //
kityann 0:daab5accfd83 3 //
kityann 0:daab5accfd83 4 //
kityann 0:daab5accfd83 5 // Program name: Aigamozu ROBOT
kityann 0:daab5accfd83 6 // Author: Mineta Kizuku
kityann 0:daab5accfd83 7 //
s1200058 21:76e7f36df4a9 8 //
kityann 0:daab5accfd83 9 //
kityann 0:daab5accfd83 10 /**********************************************/
kityann 0:daab5accfd83 11
kityann 0:daab5accfd83 12 /**********************************************/
kityann 0:daab5accfd83 13 //更新情報
kityann 0:daab5accfd83 14 //2015/05/11
kityann 0:daab5accfd83 15 //ロボットプログラムの作成
kityann 0:daab5accfd83 16 //
kityann 4:f36986ceb73d 17 //2015/05/13
kityann 4:f36986ceb73d 18 //カルマンフィルタの共分散の値を0.0001以下にならないようにした
kityann 4:f36986ceb73d 19 //共分散の値を10進数に変換するようにした
kityann 0:daab5accfd83 20 //
s1200058 21:76e7f36df4a9 21 //2015/05/13 Yokokawa
s1200058 6:13f212b75e71 22 //何回目のGPSでとられたGPSか確認するようにしました。
s1200058 6:13f212b75e71 23 //
s1200058 17:e563cf8e6bcf 24 //2015/05/15
s1200058 17:e563cf8e6bcf 25 //プログラムcreateReceiveStatusCommand()にて
s1200058 17:e563cf8e6bcf 26 //Aigamozu_collect_dataにinかoutかを送るためにstate関連をいじったので必要に応じて直してください。
s1200058 17:e563cf8e6bcf 27 //
kityann 23:c5071bf93db1 28 //2015/05//17
kityann 23:c5071bf93db1 29 //Send_Status関数内を変更:基地局への送信データのみ現在のモードを入れるパケットの部分に内外判定の結果を入れるようにした
kityann 23:c5071bf93db1 30 //基地局以外には現在のモードを送信するようにしてある
kityann 23:c5071bf93db1 31 //要確認!!!!
s1200058 28:4ccd7cfc6b1b 32 //
s1200058 28:4ccd7cfc6b1b 33 //2015/05/17
s1200058 28:4ccd7cfc6b1b 34 //Get_GPS()の中身longitudeの範囲138〜140に変更
s1200058 28:4ccd7cfc6b1b 35 //
s1200058 33:3025b16bccd2 36 //2015/05/19
s1200058 33:3025b16bccd2 37 //autoモードのとき、三十秒前進・三秒右に転回に変更した。
s1200058 33:3025b16bccd2 38 //
s1200058 32:6ba2e5402f00 39 //2015/05/24
s1200058 32:6ba2e5402f00 40 //Kalmanフィルターを十進数で計算するようにした。
s1200058 32:6ba2e5402f00 41 //Kalmanフィルターの計算式を変更した。
s1200058 32:6ba2e5402f00 42 //set_kalmanを追加した。
s1200058 32:6ba2e5402f00 43 //
kityann 0:daab5accfd83 44 /**********************************************/
kityann 0:daab5accfd83 45
kityann 0:daab5accfd83 46 #include "mbed.h"
kityann 0:daab5accfd83 47 #include "XBee.h"
kityann 0:daab5accfd83 48 #include "MBed_Adafruit_GPS.h"
kityann 0:daab5accfd83 49 #include "AigamozuControlPackets.h"
kityann 0:daab5accfd83 50 #include "agzIDLIST.h"
kityann 0:daab5accfd83 51 #include "aigamozuSetting.h"
kityann 0:daab5accfd83 52 #include "Kalman.h"
kityann 0:daab5accfd83 53
kityann 4:f36986ceb73d 54 #define SIGMA_MIN 0.0001
kityann 4:f36986ceb73d 55
kityann 0:daab5accfd83 56 //************ID Number*****************
kityann 0:daab5accfd83 57 //Robot ID: 'A' ~ 'Z'
kityann 0:daab5accfd83 58 //Base ID: 'a' ~ 'a'
kityann 0:daab5accfd83 59 //manager ID: '0'(数字のゼロ)
kityann 0:daab5accfd83 60 //
s1200058 29:524684a1198f 61 const char MyID = 'D';
kityann 0:daab5accfd83 62 //************ID Number*****************
kityann 0:daab5accfd83 63
kityann 0:daab5accfd83 64 /////////////////////////////////////////
kityann 0:daab5accfd83 65 //
kityann 0:daab5accfd83 66 //Pin Setting
kityann 0:daab5accfd83 67 //
kityann 0:daab5accfd83 68 /////////////////////////////////////////
kityann 0:daab5accfd83 69 VNH5019 agz_motorShield(p21,p22,p23,p24,p25,p26);
kityann 0:daab5accfd83 70
kityann 0:daab5accfd83 71
kityann 0:daab5accfd83 72 /////////////////////////////////////////
kityann 0:daab5accfd83 73 //
kityann 0:daab5accfd83 74 //Connection Setting
kityann 0:daab5accfd83 75 //
kityann 0:daab5accfd83 76 /////////////////////////////////////////
kityann 0:daab5accfd83 77
kityann 0:daab5accfd83 78 //Serial Connect Setting: PC <--> mbed
kityann 0:daab5accfd83 79 Serial pc(USBTX, USBRX);
kityann 0:daab5accfd83 80
kityann 0:daab5accfd83 81 //Serial Connect Setting: GPS <--> mbed
kityann 0:daab5accfd83 82 Serial * gps_Serial;
kityann 0:daab5accfd83 83
kityann 0:daab5accfd83 84 //Serial Connect Setting: XBEE <--> mbed
kityann 0:daab5accfd83 85 XBee xbee(p13,p14);
kityann 0:daab5accfd83 86 ZBRxResponse zbRx = ZBRxResponse();
kityann 0:daab5accfd83 87
kityann 0:daab5accfd83 88 //set up GPS module
kityann 0:daab5accfd83 89
kityann 0:daab5accfd83 90 //set up AigamozuControlPackets library
kityann 0:daab5accfd83 91 AigamozuControlPackets agz(agz_motorShield);
kityann 0:daab5accfd83 92
kityann 0:daab5accfd83 93
kityann 0:daab5accfd83 94 /////////////////////////////////////////
kityann 0:daab5accfd83 95 //
kityann 0:daab5accfd83 96 //For Kalman data
kityann 0:daab5accfd83 97 //
kityann 0:daab5accfd83 98 /////////////////////////////////////////
s1200058 32:6ba2e5402f00 99 #define FIRST_S2 0.000001
s1200058 32:6ba2e5402f00 100 #define COUNTER_MAX 10000
s1200058 32:6ba2e5402f00 101 double x_cur,x_prev,y_cur,y_prev;//緯度と経度の時刻tと時刻t-1での推定値
s1200058 32:6ba2e5402f00 102 double s2x_cur=FIRST_S2,s2x_prev=FIRST_S2,s2y_cur=FIRST_S2,s2y_prev=FIRST_S2;//緯度経度のの時刻tと時刻t-1での共分散
s1200058 32:6ba2e5402f00 103 double s2_R=FIRST_S2;//GPSセンサの分散
s1200058 32:6ba2e5402f00 104 double Kx=0,Ky=0;//カルマンゲイン
s1200058 32:6ba2e5402f00 105 double zx,zy;//観測値
s1200058 32:6ba2e5402f00 106 void Kalman(double Latitude,double Longitude);
kityann 0:daab5accfd83 107
s1200058 2:886fac7f4399 108 /////////////////////////////////////////
s1200058 2:886fac7f4399 109 //
s1200058 2:886fac7f4399 110 //Plus Speed
s1200058 2:886fac7f4399 111 //
s1200058 2:886fac7f4399 112 //MNUAL_MODEの時にスピードを変える
s1200058 2:886fac7f4399 113 /////////////////////////////////////////
s1200058 2:886fac7f4399 114 void Plus_Speed(uint8_t *packetdata){
s1200058 2:886fac7f4399 115
s1200058 2:886fac7f4399 116 if(agz.nowMode == MANUAL_MODE){
s1200058 2:886fac7f4399 117 agz.changeSpeed(packetdata);
s1200058 2:886fac7f4399 118 }
s1200058 2:886fac7f4399 119
s1200058 2:886fac7f4399 120 }
kityann 0:daab5accfd83 121
kityann 0:daab5accfd83 122 /////////////////////////////////////////
kityann 0:daab5accfd83 123 //
s1200058 2:886fac7f4399 124 //Send Status
kityann 0:daab5accfd83 125 //
kityann 0:daab5accfd83 126 //リクエストがきたとき、自分の位置情報などを返信する
kityann 0:daab5accfd83 127 /////////////////////////////////////////
kityann 0:daab5accfd83 128 void Send_Status(char SenderIDc){
kityann 0:daab5accfd83 129 XBeeAddress64 send_Address;
s1200058 22:af860680417b 130 if(SenderIDc == '0'){
kityann 0:daab5accfd83 131 send_Address = manager_Address;
kityann 23:c5071bf93db1 132 agz.createReceiveStatusCommand(MyID,SenderIDc, agz.nowMode,
kityann 23:c5071bf93db1 133 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 23:c5071bf93db1 134 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 23:c5071bf93db1 135 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:daab5accfd83 136 }
s1200058 25:ae5fab0946f2 137 if(SenderIDc >= 'A' && SenderIDc <= 'Z'){
kityann 0:daab5accfd83 138 send_Address = robot_Address[SenderIDc - 'A'];
s1200058 25:ae5fab0946f2 139 //Create GPS Infomation Packet
kityann 24:698d4e920d33 140 agz.createReceiveStatusCommand(MyID,SenderIDc, agz.nowMode,
kityann 23:c5071bf93db1 141 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 23:c5071bf93db1 142 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 23:c5071bf93db1 143 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:daab5accfd83 144 }
kityann 0:daab5accfd83 145 if(SenderIDc >= 'a' && SenderIDc <= 'z'){
kityann 0:daab5accfd83 146 send_Address = base_Address[SenderIDc - 'a'];
kityann 24:698d4e920d33 147
kityann 24:698d4e920d33 148 agz.createReceiveStatusCommand(MyID,SenderIDc, (int)agz.gpsAuto(),
kityann 23:c5071bf93db1 149 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 23:c5071bf93db1 150 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 24:698d4e920d33 151 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:daab5accfd83 152 }
kityann 0:daab5accfd83 153 //send normal data
kityann 23:c5071bf93db1 154
s1200058 32:6ba2e5402f00 155 /*
kityann 0:daab5accfd83 156 //debug***************************************************
kityann 0:daab5accfd83 157 printf("latitude:%f,longitude:%f\nlatitudeK:%f,longitudeK:%f\nCovlat:%f,Covlongi:%f\n",
kityann 0:daab5accfd83 158 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
kityann 0:daab5accfd83 159 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
kityann 0:daab5accfd83 160 agz.get_agzCov_lati(),agz.get_agzCov_longi()
kityann 0:daab5accfd83 161 );
kityann 0:daab5accfd83 162 for(int i = 0; i < RECEIVE_STATUS_COMMNAD_LENGTH; ++i) printf("%d ",agz.packetData[i]);
kityann 0:daab5accfd83 163 printf("\n");
kityann 0:daab5accfd83 164 //debug end***************************************************
s1200058 32:6ba2e5402f00 165 */
kityann 0:daab5accfd83 166 //Select Destination
kityann 0:daab5accfd83 167 ZBTxRequest tx64request(send_Address,agz.packetData,agz.getPacketLength());
kityann 0:daab5accfd83 168 //Send -> Base
kityann 0:daab5accfd83 169 xbee.send(tx64request);
s1200058 20:eee8ac8d1788 170
kityann 0:daab5accfd83 171 }
kityann 0:daab5accfd83 172
kityann 0:daab5accfd83 173 /////////////////////////////////////////
kityann 0:daab5accfd83 174 //
kityann 0:daab5accfd83 175 //Get GPS function
kityann 0:daab5accfd83 176 //
kityann 0:daab5accfd83 177 /////////////////////////////////////////
kityann 0:daab5accfd83 178
kityann 0:daab5accfd83 179 void Get_GPS(Adafruit_GPS *myGPS){
s1200058 32:6ba2e5402f00 180 static int flag = 0;
kityann 0:daab5accfd83 181
kityann 0:daab5accfd83 182 if (myGPS->fix) {
s1200058 32:6ba2e5402f00 183
kityann 0:daab5accfd83 184 agz.nowStatus = GPS_AVAIL;
s1200058 32:6ba2e5402f00 185 agz.reNewRobotPoint(myGPS->latitudeH,myGPS->latitudeL,myGPS->longitudeH,myGPS->longitudeL);
kityann 0:daab5accfd83 186
s1200058 32:6ba2e5402f00 187 if(flag < COUNTER_MAX){
s1200058 32:6ba2e5402f00 188 flag++;
s1200058 32:6ba2e5402f00 189 }
s1200058 33:3025b16bccd2 190 if(flag == 5){
s1200058 32:6ba2e5402f00 191 x_prev = agz.get_agzPoint_lati();
s1200058 32:6ba2e5402f00 192 y_prev = agz.get_agzPoint_longi();
kityann 0:daab5accfd83 193 }
s1200058 32:6ba2e5402f00 194
s1200058 32:6ba2e5402f00 195 if(flag >= 6){
s1200058 32:6ba2e5402f00 196 Kalman(agz.get_agzPoint_lati(), agz.get_agzPoint_longi());
s1200058 32:6ba2e5402f00 197 }
s1200058 33:3025b16bccd2 198
s1200058 33:3025b16bccd2 199 printf("%.14lf %.14lf %.14lf %.14lf %.14le %.14le \n",
s1200058 33:3025b16bccd2 200 agz.get_agzPoint_lati(),agz.get_agzPoint_longi(),
s1200058 33:3025b16bccd2 201 agz.get_agzPointKalman_lati(),agz.get_agzPointKalman_longi(),
s1200058 33:3025b16bccd2 202 agz.get_agzCov_lati(),agz.get_agzCov_longi());
kityann 0:daab5accfd83 203 }
kityann 0:daab5accfd83 204 else agz.nowStatus = GPS_UNAVAIL;
kityann 0:daab5accfd83 205
kityann 0:daab5accfd83 206 }
s1200058 2:886fac7f4399 207
s1200058 2:886fac7f4399 208 /////////////////////////////////////////
s1200058 2:886fac7f4399 209 //
s1200058 2:886fac7f4399 210 //New Mode
s1200058 2:886fac7f4399 211 //
s1200058 2:886fac7f4399 212 /////////////////////////////////////////
s1200058 2:886fac7f4399 213
s1200058 2:886fac7f4399 214 void New_Mode(uint8_t *packetdata){
s1200058 2:886fac7f4399 215
s1200058 19:a806105ba365 216 //bool result;
s1200058 20:eee8ac8d1788 217 agz.changeMode(packetdata);
s1200058 2:886fac7f4399 218
s1200058 2:886fac7f4399 219 }
kityann 1:b2b950b916ce 220
kityann 1:b2b950b916ce 221 /////////////////////////////////////////
kityann 1:b2b950b916ce 222 //
kityann 1:b2b950b916ce 223 //Get Status
kityann 1:b2b950b916ce 224 //
kityann 1:b2b950b916ce 225 /////////////////////////////////////////
kityann 1:b2b950b916ce 226 void Get_Status(char SenderIDc,uint8_t *packetdata){
kityann 1:b2b950b916ce 227
kityann 1:b2b950b916ce 228 //マネージャからデータが来たとき
kityann 1:b2b950b916ce 229 if(SenderIDc == '0'){
kityann 1:b2b950b916ce 230 printf("get manager Status\n");
kityann 1:b2b950b916ce 231 }
kityann 1:b2b950b916ce 232 //他のロボットからデータが来たとき
kityann 1:b2b950b916ce 233 if(SenderIDc >= 'A' && SenderIDc <= 'Z'){
kityann 1:b2b950b916ce 234 printf("get other robots Status\n");
kityann 1:b2b950b916ce 235 }
kityann 1:b2b950b916ce 236 //基地局からデータが来たとき
kityann 1:b2b950b916ce 237 if(SenderIDc >= 'a' && SenderIDc <= 'z'){
kityann 1:b2b950b916ce 238 printf("Get Base data\n");
kityann 1:b2b950b916ce 239 int id = SenderIDc - 'a';
kityann 1:b2b950b916ce 240 agz.reNewBasePoint(id,&packetdata[13],&packetdata[21]);
kityann 1:b2b950b916ce 241 agz.reNewBasePointKalman(id,&packetdata[29],&packetdata[37]);
kityann 1:b2b950b916ce 242
kityann 1:b2b950b916ce 243 //debug
kityann 1:b2b950b916ce 244 for(int i = 0;i < 4;i++){
kityann 1:b2b950b916ce 245 printf("BASE:%d\n",i);
kityann 1:b2b950b916ce 246 printf("latitude:%f,longitude:%f\nlatitudeK:%f,longitudeK:%f\n",
kityann 1:b2b950b916ce 247 agz.get_basePoint_lati(i),agz.get_basePoint_longi(i),
kityann 1:b2b950b916ce 248 agz.get_basePointKalman_lati(i),agz.get_basePointKalman_longi(i)
kityann 1:b2b950b916ce 249 );
kityann 1:b2b950b916ce 250 }
kityann 1:b2b950b916ce 251 }
kityann 1:b2b950b916ce 252 }
kityann 1:b2b950b916ce 253
s1200058 2:886fac7f4399 254 void Get_Status_Kalman(char SenderIDc,uint8_t *packetdata){
s1200058 2:886fac7f4399 255
s1200058 2:886fac7f4399 256 //マネージャからデータが来たとき
s1200058 2:886fac7f4399 257 if(SenderIDc == '0'){
s1200058 2:886fac7f4399 258 printf("get manager Status Kalman\n");
s1200058 2:886fac7f4399 259 }
s1200058 2:886fac7f4399 260 //他のロボットからデータが来たとき
s1200058 2:886fac7f4399 261 if(SenderIDc >= 'A' && SenderIDc <= 'Z'){
s1200058 2:886fac7f4399 262 printf("get other robots Status Kalman\n");
s1200058 2:886fac7f4399 263 }
s1200058 2:886fac7f4399 264 //基地局からデータが来たとき
s1200058 2:886fac7f4399 265 if(SenderIDc >= 'a' && SenderIDc <= 'z'){
s1200058 2:886fac7f4399 266 printf("Get Base data Kalman\n");
s1200058 2:886fac7f4399 267 int id = SenderIDc - 'a';
s1200058 2:886fac7f4399 268 agz.reNewBasePointKalman(id,&packetdata[29],&packetdata[37]);
s1200058 2:886fac7f4399 269
s1200058 2:886fac7f4399 270 //debug
s1200058 2:886fac7f4399 271 for(int i = 0;i < 4;i++){
s1200058 2:886fac7f4399 272 printf("BASE:%d\n",i);
s1200058 2:886fac7f4399 273 printf("latitudeK:%f,longitudeK:%f\n",
s1200058 2:886fac7f4399 274 agz.get_basePointKalman_lati(i),agz.get_basePointKalman_longi(i));
s1200058 2:886fac7f4399 275 }
s1200058 2:886fac7f4399 276 }
s1200058 2:886fac7f4399 277 }
s1200058 2:886fac7f4399 278
kityann 1:b2b950b916ce 279 /////////////////////////////////////////
kityann 1:b2b950b916ce 280 //
kityann 1:b2b950b916ce 281 //Send_Request_to_base
kityann 1:b2b950b916ce 282 //
kityann 1:b2b950b916ce 283 /////////////////////////////////////////
kityann 1:b2b950b916ce 284 void Send_Request_Base(int basenumber){
kityann 1:b2b950b916ce 285 printf("send\n");
kityann 1:b2b950b916ce 286 agz.createRequestCommand(MyID, basenumber);
kityann 1:b2b950b916ce 287 //Select Destination
kityann 1:b2b950b916ce 288 ZBTxRequest tx64request(base_Address[basenumber],agz.packetData,agz.getPacketLength());
kityann 1:b2b950b916ce 289 //Send -> Base
kityann 1:b2b950b916ce 290 xbee.send(tx64request);
kityann 1:b2b950b916ce 291 }
kityann 0:daab5accfd83 292
s1200058 3:1ac506a96fd6 293 /////////////////////////////////////////
s1200058 3:1ac506a96fd6 294 //
s1200058 3:1ac506a96fd6 295 //auto_Move
s1200058 3:1ac506a96fd6 296 //
s1200058 3:1ac506a96fd6 297 //InAreaかOutAreaの判定
s1200058 3:1ac506a96fd6 298 //Kalmanを通した値を出力(Baseと自分)
s1200058 3:1ac506a96fd6 299 /////////////////////////////////////////
s1200058 3:1ac506a96fd6 300
s1200058 3:1ac506a96fd6 301 void auto_Move(){
s1200058 3:1ac506a96fd6 302
s1200058 3:1ac506a96fd6 303 bool result;
s1200058 3:1ac506a96fd6 304 int i;
s1200058 3:1ac506a96fd6 305
s1200058 3:1ac506a96fd6 306 result = agz.gpsAuto();
s1200058 20:eee8ac8d1788 307 //agz.set_agzAutoGPS();
s1200058 20:eee8ac8d1788 308 //agz.set_agzKalmanGPS();
s1200058 3:1ac506a96fd6 309
s1200058 3:1ac506a96fd6 310 if(result == true){
s1200058 3:1ac506a96fd6 311 printf("Out Area\n");
s1200058 3:1ac506a96fd6 312 }
s1200058 3:1ac506a96fd6 313 else if(result == false){
s1200058 3:1ac506a96fd6 314 printf("In Area\n");
s1200058 3:1ac506a96fd6 315 }
s1200058 3:1ac506a96fd6 316 for(i = 0; i < 4; i++){
s1200058 3:1ac506a96fd6 317 printf("%d: %f, %f\n", i, agz.get_basePointKalman_lati(i), agz.get_basePointKalman_longi(i));
s1200058 3:1ac506a96fd6 318 }
s1200058 3:1ac506a96fd6 319 printf("robot: %f, %f\n", agz.get_agzPointKalman_lati(), agz.get_agzPointKalman_longi());
s1200058 3:1ac506a96fd6 320
s1200058 3:1ac506a96fd6 321 }
s1200058 3:1ac506a96fd6 322
s1200058 5:522c47c78401 323 void print_gps(int count){
s1200058 5:522c47c78401 324
s1200058 5:522c47c78401 325 printf("%d times:\n", count);
s1200058 5:522c47c78401 326 printf("%f, %f\n", agz.get_agzPointKalman_lati(), agz.get_agzPointKalman_longi());
s1200058 5:522c47c78401 327
s1200058 5:522c47c78401 328 }
kityann 0:daab5accfd83 329
s1200058 32:6ba2e5402f00 330
kityann 0:daab5accfd83 331 /////////////////////////////////////////
kityann 0:daab5accfd83 332 //
kityann 0:daab5accfd83 333 //Kalman Processing
kityann 0:daab5accfd83 334 //
kityann 0:daab5accfd83 335 /////////////////////////////////////////
s1200058 32:6ba2e5402f00 336 void calc_Kalman(){
s1200058 32:6ba2e5402f00 337 //calc Kalman gain
s1200058 32:6ba2e5402f00 338 Kx = s2x_prev/(s2x_prev+s2_R);
s1200058 32:6ba2e5402f00 339 Ky = s2y_prev/(s2y_prev+s2_R);
s1200058 32:6ba2e5402f00 340 //estimate
s1200058 32:6ba2e5402f00 341 x_cur = x_prev + Kx*(zx-x_prev);
s1200058 32:6ba2e5402f00 342 y_cur = y_prev + Ky*(zy-y_prev);
s1200058 32:6ba2e5402f00 343 //calc sigma
s1200058 32:6ba2e5402f00 344 s2x_cur = s2x_prev-Kx*s2x_prev;
s1200058 32:6ba2e5402f00 345 s2y_cur = s2y_prev-Ky*s2y_prev;
kityann 0:daab5accfd83 346
kityann 0:daab5accfd83 347 }
kityann 0:daab5accfd83 348
s1200058 32:6ba2e5402f00 349 void Kalman(double Latitude,double Longitude){
s1200058 32:6ba2e5402f00 350
s1200058 32:6ba2e5402f00 351 zx = Latitude;
s1200058 32:6ba2e5402f00 352 zy = Longitude;
s1200058 32:6ba2e5402f00 353
s1200058 32:6ba2e5402f00 354 calc_Kalman();
kityann 0:daab5accfd83 355
s1200058 32:6ba2e5402f00 356 //更新
s1200058 32:6ba2e5402f00 357 x_prev = x_cur;
s1200058 32:6ba2e5402f00 358 y_prev = y_cur;
s1200058 32:6ba2e5402f00 359 s2x_prev = s2x_cur;
s1200058 32:6ba2e5402f00 360 s2y_prev = s2y_cur;
s1200058 25:ae5fab0946f2 361
s1200058 32:6ba2e5402f00 362 //agzPontKalmanとagzCovに格納する
s1200058 32:6ba2e5402f00 363 agz.set_agzPointKalman_lati(x_cur);
s1200058 32:6ba2e5402f00 364 agz.set_agzPointKalman_longi(y_cur);
s1200058 32:6ba2e5402f00 365 agz.set_agzCov(s2x_cur,s2y_cur);
kityann 0:daab5accfd83 366
kityann 0:daab5accfd83 367 }
kityann 0:daab5accfd83 368
kityann 0:daab5accfd83 369 /////////////////////////////////////////
kityann 0:daab5accfd83 370 //
kityann 0:daab5accfd83 371 //Main Processing
kityann 0:daab5accfd83 372 //
kityann 0:daab5accfd83 373 /////////////////////////////////////////
kityann 0:daab5accfd83 374 int main() {
kityann 0:daab5accfd83 375 //start up time
kityann 0:daab5accfd83 376 wait(3);
kityann 0:daab5accfd83 377 //set pc frequency to 57600bps
kityann 0:daab5accfd83 378 pc.baud(PC_BAUD_RATE);
kityann 0:daab5accfd83 379 //set xbee frequency to 57600bps
kityann 0:daab5accfd83 380 xbee.begin(XBEE_BAUD_RATE);
kityann 0:daab5accfd83 381
kityann 0:daab5accfd83 382
kityann 0:daab5accfd83 383 //GPS setting
kityann 0:daab5accfd83 384 gps_Serial = new Serial(p28,p27);
kityann 0:daab5accfd83 385 Adafruit_GPS myGPS(gps_Serial);
kityann 0:daab5accfd83 386 Timer refresh_Timer;
s1200058 7:a9a1acc7673b 387 const int refresh_Time = 1000; //refresh time in ms
s1200058 3:1ac506a96fd6 388 Timer auto_Timer;
s1200058 3:1ac506a96fd6 389 const int auto_Time = 2000; //refresh time in ms
s1200058 5:522c47c78401 390 int count = 0;
s1200058 29:524684a1198f 391
s1200058 29:524684a1198f 392 const int straight = 30000, turning = 34000, wait = 31000;
s1200058 29:524684a1198f 393
kityann 0:daab5accfd83 394 myGPS.begin(GPS_BAUD_RATE);
kityann 0:daab5accfd83 395
s1200058 14:5deb7a4f1cd4 396 Timer collect_Timer;
s1200058 14:5deb7a4f1cd4 397 const int collect_Time = 2000; //when we collect 4 GPS point, we use
s1200058 14:5deb7a4f1cd4 398 int collect_flag = 0;
s1200058 14:5deb7a4f1cd4 399
kityann 0:daab5accfd83 400 char SenderIDc;
kityann 0:daab5accfd83 401 //GPS Send Command
kityann 0:daab5accfd83 402 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
kityann 0:daab5accfd83 403 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kityann 0:daab5accfd83 404 myGPS.sendCommand(PGCMD_ANTENNA);
kityann 0:daab5accfd83 405
s1200058 29:524684a1198f 406 wait_ms(2000);
kityann 0:daab5accfd83 407
kityann 0:daab5accfd83 408 //interrupt start
kityann 0:daab5accfd83 409 refresh_Timer.start();
s1200058 3:1ac506a96fd6 410 auto_Timer.start();
s1200058 30:7f6ebe2121d9 411 agz.Move_Timer.start();
s1200058 16:a07350b3eb64 412 collect_Timer.start();
s1200058 29:524684a1198f 413 printf("start\n");
kityann 1:b2b950b916ce 414
kityann 0:daab5accfd83 415
kityann 0:daab5accfd83 416 while (true) {
kityann 0:daab5accfd83 417
kityann 0:daab5accfd83 418 //Check Xbee Buffer Available
kityann 0:daab5accfd83 419 xbee.readPacket();
kityann 0:daab5accfd83 420
kityann 0:daab5accfd83 421 if (xbee.getResponse().isAvailable()) {
kityann 0:daab5accfd83 422 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:daab5accfd83 423 uint8_t *buf = zbRx.getFrameData();
kityann 0:daab5accfd83 424
kityann 0:daab5accfd83 425 if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
kityann 0:daab5accfd83 426 xbee.getResponse().getZBRxResponse(zbRx);
kityann 0:daab5accfd83 427 uint8_t *buf = zbRx.getFrameData();//フレームデータを格納する
kityann 0:daab5accfd83 428 uint8_t *buf1 = &buf[11];//データの部分のみを格納する
kityann 0:daab5accfd83 429 SenderIDc = buf1[5];//送信元のIDを取得する
s1200058 21:76e7f36df4a9 430 char Command_type = agz.checkCommnadType(buf);//コマンドタイプを取得する
kityann 0:daab5accfd83 431
kityann 0:daab5accfd83 432 //Check Command Type
kityann 0:daab5accfd83 433 switch(Command_type){
kityann 0:daab5accfd83 434 //Get Request command
s1200058 2:886fac7f4399 435 case MANUAL:{
s1200058 12:48ef69b4f0e4 436 Plus_Speed(buf);
s1200058 2:886fac7f4399 437 break;
s1200058 2:886fac7f4399 438 }
kityann 0:daab5accfd83 439 case STATUS_REQUEST:{
s1200058 21:76e7f36df4a9 440 Send_Status(SenderIDc);
s1200058 21:76e7f36df4a9 441 printf("%c\n", SenderIDc);
kityann 0:daab5accfd83 442 break;
kityann 0:daab5accfd83 443 }
s1200058 2:886fac7f4399 444 case CHANGE_MODE:{
s1200058 12:48ef69b4f0e4 445 New_Mode(buf);
s1200058 2:886fac7f4399 446 break;
s1200058 2:886fac7f4399 447 }
kityann 1:b2b950b916ce 448 case RECEIVE_STATUS:{
kityann 1:b2b950b916ce 449 Get_Status(SenderIDc,buf1);
kityann 1:b2b950b916ce 450 break;
kityann 1:b2b950b916ce 451 }
kityann 0:daab5accfd83 452 default:{
kityann 0:daab5accfd83 453 break;
kityann 0:daab5accfd83 454 }
kityann 0:daab5accfd83 455 }//endswitch
kityann 0:daab5accfd83 456 }//endifZB_RX_RESPONSE
kityann 0:daab5accfd83 457 }//endifisAvailable
kityann 0:daab5accfd83 458
kityann 0:daab5accfd83 459 myGPS.read();
kityann 0:daab5accfd83 460 //recive gps module
kityann 0:daab5accfd83 461 //check if we recieved a new message from GPS, if so, attempt to parse it,
kityann 0:daab5accfd83 462 if ( myGPS.newNMEAreceived() ) {
kityann 0:daab5accfd83 463 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
s1200058 8:994c73f6fad9 464 continue;
s1200058 8:994c73f6fad9 465 }
s1200058 8:994c73f6fad9 466 else{
s1200058 5:522c47c78401 467 count++;
s1200058 8:994c73f6fad9 468 }
kityann 0:daab5accfd83 469 }
kityann 1:b2b950b916ce 470 //一定時間ごとに自分のGPSデータを取得し、AigamozuControlPacketsないのagzPointとagzPointKalmanに格納する
kityann 0:daab5accfd83 471 if (refresh_Timer.read_ms() >= refresh_Time) {
kityann 0:daab5accfd83 472 refresh_Timer.reset();
s1200058 12:48ef69b4f0e4 473 //print_gps(count);
kityann 0:daab5accfd83 474 Get_GPS(&myGPS);
kityann 0:daab5accfd83 475
kityann 0:daab5accfd83 476 }
s1200058 20:eee8ac8d1788 477
s1200058 20:eee8ac8d1788 478 //get base GPS
s1200058 14:5deb7a4f1cd4 479 if( collect_Timer.read_ms() >= collect_Time){
s1200058 15:f3d01f37f00d 480 collect_Timer.reset();
s1200058 15:f3d01f37f00d 481
s1200058 15:f3d01f37f00d 482 Send_Request_Base(collect_flag);
s1200058 14:5deb7a4f1cd4 483
s1200058 14:5deb7a4f1cd4 484 collect_flag++;
s1200058 14:5deb7a4f1cd4 485
s1200058 14:5deb7a4f1cd4 486 if(collect_flag == 4){
s1200058 14:5deb7a4f1cd4 487 collect_flag = 0;
s1200058 14:5deb7a4f1cd4 488 }
s1200058 14:5deb7a4f1cd4 489 }
s1200058 20:eee8ac8d1788 490
s1200058 14:5deb7a4f1cd4 491 if(agz.nowMode == AUTO_GPS_MODE && auto_Timer.read_ms() >= auto_Time){
s1200058 29:524684a1198f 492 auto_Timer.reset();
s1200058 29:524684a1198f 493 auto_Move();
s1200058 29:524684a1198f 494 }
s1200058 29:524684a1198f 495
s1200058 29:524684a1198f 496 if(agz.nowMode == AUTO_GPS_MODE){
s1200058 30:7f6ebe2121d9 497 if(agz.Move_Timer.read_ms() < straight){
s1200058 29:524684a1198f 498 agz.test_Auto(0); //straight
s1200058 29:524684a1198f 499 }
s1200058 30:7f6ebe2121d9 500 if(agz.Move_Timer.read_ms() > straight && agz.Move_Timer.read_ms() < wait){
s1200058 29:524684a1198f 501 agz.test_Auto(1);
s1200058 29:524684a1198f 502 }
s1200058 30:7f6ebe2121d9 503 if(agz.Move_Timer.read_ms() > wait && agz.Move_Timer.read_ms() < turning){
s1200058 29:524684a1198f 504 agz.test_Auto(2); //Turn Right
s1200058 29:524684a1198f 505 }
s1200058 30:7f6ebe2121d9 506 if(agz.Move_Timer.read_ms() > turning){
s1200058 29:524684a1198f 507 agz.test_Auto(3);
s1200058 29:524684a1198f 508 wait_ms(200);
s1200058 30:7f6ebe2121d9 509 agz.Move_Timer.reset();
kityann 1:b2b950b916ce 510 }
kityann 0:daab5accfd83 511 }
s1200058 29:524684a1198f 512 }
kityann 0:daab5accfd83 513 }