2016_05_19 Auto mode: 10sec forward, 2sec stop, 2sec turn right Please change test Auto pwm

Dependencies:   ADXL345 AigamozuControlPackets_2016 HMC5843 ITG3200 MBed_Adafruit-GPS-Library XBee agzIDLIST_2016 mbed

Fork of Aigamozu_Robot_ver4 by CanSat2015aizu

Committer:
s1210160
Date:
Wed May 25 10:49:29 2016 +0000
Revision:
39:ff6819c7a066
Parent:
38:e2aa1165d28d
Child:
40:aa600a5ba02c
2016/5/25;

Who changed what in which revision?

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