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