Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MatrixMath LPS25HB_I2C LSM9DS1 Matrix2 PIDcontroller LoopTicker SBUS_without_mainfile UsaPack solaESKF_wind Vector3 CalibrateMagneto FastPWM
run.cpp
- Committer:
- NaotoMorita
- Date:
- 2022-03-09
- Revision:
- 109:27ae949bc38e
- Parent:
- 108:e582f8bd4729
- Child:
- 114:ba221936d53a
File content as of revision 109:27ae949bc38e:
#include "global.hpp"
void run()
{
preflightCalibration();
//ESKFの共分散設定
eskf.setGravity(0.0f,0.0f,9.8f);
eskf.setPhatPosition(0.1f);
eskf.setPhatVelocity(0.1f);
eskf.setPhatAngleError(0.5f);
eskf.setPhatAccBias(0.001f);
eskf.setPhatGyroBias(0.001f);
eskf.setPhatGravity(0.0000001f);
eskf.setQVelocity(0.001f);
eskf.setQAngleError(0.0000025f);
eskf.setQAccBias(0.000001f);
eskf.setQGyroBias(0.000001f);
Matrix Rgps(5,5);
setDiag(Rgps,0.05f);
Rgps(4,4) = 0.1f;
Rgps(5,5) = 0.1f;
float dynAccCriteria = 0.4f;
Matrix Racc(3,3);
setDiag(Racc,100.0f);
Matrix RaccDyn(3,3);
setDiag(RaccDyn,5000.0f);
Matrix Rheading(1,1);
Rheading(1,1) = 0.01f;
Matrix Ropt(2,2);
setDiag(Ropt,10.0f);
wait(0.5);
_t.start();
tail.Subscribe(tail_address[pos_tail], &(updateValues));
preflightCheck();
LoopTicker PIDtick;
PIDtick.attach(calcServoOut,PID_dt);
while(1)
{
tstart = _t.read();
//センサの値を取得
getIMUval();
calcOpticalVel();
//ekfの更新
eskf.updateNominal(MatrixMath::Vector2mat(acc),MatrixMath::Vector2mat(gyro),att_dt);
eskf.updateErrState(MatrixMath::Vector2mat(acc),MatrixMath::Vector2mat(gyro),att_dt);
//キャリブレーション中
if(updateValues.calibrationFlag == 1111){
mainloopCalibration();
}else{
gpsUpdateFlag = false;
//pc.printf("%d %d \r\n",updateValues.gps_itow,updateValues.gps_fix );
if((gpsitow != updateValues.gps_itow) && (updateValues.gps_fix == 0x03)){
gpsUpdateFlag = true;
gpsitow = updateValues.gps_itow;
tgps = _t.read();
}
sensorUpdateFlag = false;
if(tstart-tsensors>0.05f){
sensorUpdateFlag = true;
tsensors = _t.read();
}
if(gpsUpdateFlag==true){
Vector3 pi(updateValues.pi[0], updateValues.pi[1], updateValues.pi[2]);
Vector3 vi(updateValues.vi[0], updateValues.vi[1], updateValues.vi[2]);
Rgps(1, 1) = (updateValues.gps_acc * 0.849f) * (updateValues.gps_acc * 0.849f);
Rgps(2, 2) = (updateValues.gps_acc * 0.849f) * (updateValues.gps_acc * 0.849f);
eskf.updateGPS(MatrixMath::Vector2mat(pi),palt,MatrixMath::Vector2mat(vi),Rgps);
//pc.printf("gps\r\n");
}else if(sensorUpdateFlag == true){
if(sensorUpdateID==1){
Matrix euler = eskf.computeAngles();
if(fabsf(euler(2,1))<30.0f*M_PI/180.0f){
float heading = atan2f(-mag.y,mag.x);
eskf.updateHeading(heading,Rheading);
//pc.printf("heading\r\n");
}
sensorUpdateID += 1;
}else{
//float vnorm_opt2 = updateValues.vx_opt*updateValues.vx_opt+updateValues.vy_opt*updateValues.vy_opt;
//if(updateValues.dist_ir<1.0f && vnorm_opt2 < 400.0f){
//if(vnorm_opt2 < 0.1f){
//Matrix vbOpt(2,1);
//vbOpt(1,1) = updateValues.vx_opt;
//vbOpt(2,1) = updateValues.vy_opt;
//eskf.updateVb(vbOpt,Ropt*0.1f);
//}else{
//Matrix vbOpt(2,1);
//vbOpt(1,1) = updateValues.vx_opt;
//vbOpt(2,1) = updateValues.vy_opt;
//eskf.updateVb(vbOpt,Ropt);
//}
//pc.printf("opt\r\n");
//}
sensorUpdateID=1;
}
}else{
Matrix dynacc = eskf.calcDynAcc(MatrixMath::Vector2mat(acc));
dynaccnorm2 = dynacc(1,1)*dynacc(1,1)+dynacc(2,1)*dynacc(2,1)+dynacc(3,1)*dynacc(3,1);
if(dynaccnorm2 > dynAccCriteria*dynAccCriteria){
eskf.updateAcc(MatrixMath::Vector2mat(acc),RaccDyn);
//pc.printf("dyn acc\r\n");
}else{
eskf.updateAcc(MatrixMath::Vector2mat(acc),Racc);
//pc.printf("static acc\r\n");
}
}
}
PIDtick.loop();
//制御時間を計測
float tend = _t.read();
att_dt = (tend-tstart);
}
}