生活随笔
收集整理的這篇文章主要介紹了
gtsam 学习十一(ISAM2 实践)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
來自gtam/examples/
ISAM1模板
#include <gtsam/geometry/Point2.h>
#include <gtsam/geometry/SimpleCamera.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/slam/PriorFactor.h>
#include <gtsam/slam/ProjectionFactor.h>
#include <gtsam/nonlinear/NonlinearISAM.h>
#include <gtsam/nonlinear/NonlinearFactor.h>
#include <gtsam/nonlinear/Values.h>
#include <gtsam/geometry/Cal3DS2.h>
#include <vector>
std
::vector
<gtsam
::Point3
> createPoints() {std
::vector
<gtsam
::Point3
> points
;points
.push_back(gtsam
::Point3(10.0,10.0,10.0));points
.push_back(gtsam
::Point3(-10.0,10.0,10.0));points
.push_back(gtsam
::Point3(-10.0,-10.0,10.0));points
.push_back(gtsam
::Point3(10.0,-10.0,10.0));points
.push_back(gtsam
::Point3(10.0,10.0,-10.0));points
.push_back(gtsam
::Point3(-10.0,10.0,-10.0));points
.push_back(gtsam
::Point3(-10.0,-10.0,-10.0));points
.push_back(gtsam
::Point3(10.0,-10.0,-10.0));return points
;
}
std
::vector
<gtsam
::Pose3
> createPoses(const gtsam
::Pose3
& init
= gtsam
::Pose3(gtsam
::Rot3
::Ypr(M_PI
/2,0,-M_PI
/2), gtsam
::Point3(30, 0, 0)),const gtsam
::Pose3
& delta
= gtsam
::Pose3(gtsam
::Rot3
::Ypr(0,-M_PI
/4,0), gtsam
::Point3(sin(M_PI
/4)*30, 0, 30*(1-sin(M_PI
/4)))),int steps
= 8) {std
::vector
<gtsam
::Pose3
> poses
;int i
= 1;poses
.push_back(init
);for(; i
< steps
; ++i
) {poses
.push_back(poses
[i
-1].compose(delta
));}return poses
;
};
using namespace std
;
using namespace gtsam
;
int main(int argc
, const char** argv
) {Cal3_S2
::shared_ptr
K(new
Cal3_S2(50.0,50.0,0.0,50.0,50.0));noiseModel
::Isotropic
::shared_ptr noise
=noiseModel
::Isotropic
::Sigma(2,1.0);vector
<Pose3
> poses
=createPoses();vector
<Point3
> points
=createPoints();int relinearizeInterval
=3;NonlinearISAM
isam(relinearizeInterval
);NonlinearFactorGraph graph
;Values initialEstimate
;for(int i
=0;i
<poses
.size();i
++){for(int j
=0;j
<points
.size();j
++){SimpleCamera
camera(poses
[i
],*K
);Point2 measurement
=camera
.project(points
[j
]);graph
.emplace_shared
<GenericProjectionFactor
<Pose3
,Point3
,Cal3_S2
> >(measurement
,noise
,Symbol('x',i
),Symbol('l',j
),K
);}Pose3
noise(Rot3
::Rodrigues(-0.1, 0.2, 0.25), Point3(0.05, -0.10, 0.20));Pose3 initial_xi
=poses
[i
].compose(noise
);initialEstimate
.insert(symbol('x',i
),initial_xi
);if(i
==0){noiseModel
::Diagonal
::shared_ptr poseNoise
=noiseModel
::Diagonal
::Sigmas(Vector6(0.3,0.3,0.3,0.1,0.1,0.1));graph
.emplace_shared
<PriorFactor
<Pose3
> >(Symbol('x',0),poses
[0],poseNoise
);noiseModel
::Isotropic
::shared_ptr pointnoise
=noiseModel
::Isotropic
::Sigma(3,0.1);graph
.emplace_shared
<PriorFactor
<Point3
> >(Symbol('l',0),points
[0],pointnoise
);Point3
noise(-0.25, 0.20, 0.15);for(int j
=0;j
<points
.size();j
++){Point3 initial_lj
=points
[j
]+noise
;initialEstimate
.insert(Symbol('l',j
),initial_lj
);}}else{isam
.update(graph
,initialEstimate
);Values currentEsimate
=isam
.estimate();cout
<< "****************************************************" << endl
;cout
<< "Frame " << i
<< ": " << endl
;currentEsimate
.print("Current estimate: ");graph
.resize(0);initialEstimate
.clear();}}return 0;
}
ISAM2 基本使用
#include <gtsam/geometry/SimpleCamera.h>
#include <gtsam/nonlinear/ISAM2.h>
#include <gtsam/nonlinear/ISAM2Params.h>
#include <gtsam/slam/BetweenFactor.h>
#include <gtsam/slam/SmartProjectionPoseFactor.h>
#include <gtsam/inference/Key.h>
#include <iostream>
#include <gtsam/geometry/Pose3.h>
#include <gtsam/slam/PriorFactor.h>
#include <vector>using namespace std
;
using namespace gtsam
;
using symbol_shorthand
::X
;
using symbol_shorthand
::P
;typedef SmartProjectionPoseFactor
<Cal3_S2
> SmartFactor
;
int main(int argc
,char **argv
){Cal3_S2
::shared_ptr
K(new Cal3_S2(50.0,50.0,0,50,50));auto measurementNoise
=noiseModel
::Isotropic
::Sigma(2,1.0);Vector6 sigmas
;sigmas
<<0.3,0.3,0.3,0.1,0.1,0.1;auto noise
=noiseModel
::Diagonal
::Sigmas(sigmas
);ISAM2Params parameters
;parameters
.relinearizeThreshold
=0.01; parameters
.relinearizeSkip
=1; parameters
.enableRelinearization
=true; parameters
.evaluateNonlinearError
=false; parameters
.cacheLinearizedFactors
= false; parameters
.factorization
=ISAM2Params
::Factorization
::QR
;parameters
.keyFormatter
=DefaultKeyFormatter
;parameters
.enableDetailedResults
=true; parameters
.enablePartialRelinearizationCheck
=false; parameters
.findUnusedFactorSlots
=false;ISAM2
isam(parameters
);NonlinearFactorGraph graph
;Values initialEstimate
;Point3
point(0.0,0.0,1.0);Pose3
delta(Rot3
::Rodrigues(0.0,0.0,0.0),Point3(0.05,-0.10,0.20));Pose3
pose1(Rot3(), Point3(0.0, 0.0, 0.0));Pose3
pose2(Rot3(), Point3(0.0, 0.2, 0.0));Pose3
pose3(Rot3(), Point3(0.0, 0.4, 0.0));Pose3
pose4(Rot3(), Point3(0.0, 0.5, 0.0));Pose3
pose5(Rot3(), Point3(0.0, 0.6, 0.0));vector
<Pose3
> poses
= {pose1
, pose2
, pose3
, pose4
, pose5
};graph
.emplace_shared
<PriorFactor
<Pose3
>>(X(0), poses
[0], noise
);initialEstimate
.insert(X(0), poses
[0].compose(delta
));SmartFactor
::shared_ptr
smartFactor(new SmartFactor(measurementNoise
, K
));smartFactor
->add(PinholePose
<Cal3_S2
>(poses
[0], K
).project(point
), X(0));graph
.push_back(smartFactor
);for(size_t i
=1;i
<5;i
++){cout
<< "****************************************************" << endl
;cout
<< "i = " << i
<< endl
;graph
.emplace_shared
<PriorFactor
<Pose3
>>(X(i
), poses
[i
], noise
);initialEstimate
.insert(X(i
), poses
[i
].compose(delta
));PinholePose
<Cal3_S2
> camera(poses
[i
],K
); Point2 measurement
=camera
.project(point
); cout
<< "Measurement " << i
<< "" << measurement
<< endl
;smartFactor
->add(measurement
,X(i
));ISAM2Result result
=isam
.update(graph
,initialEstimate
);result
.print();cout
<< "Detailed results:" << endl
;for(auto keyedStatus
:result
.detail
->variableStatus
){const auto& status
=keyedStatus
.second
;PrintKey(keyedStatus
.first
);cout
<< " {" << endl
;cout
<<"變量是否被被重新限制(重新線性化、添加新值、或者在被更新的根目錄路徑上):"<<status
.isReeliminated
<<endl
;cout
<<"是否超過閾值(在平滑線性時超過是否閾值):"<<status
.isAboveRelinThreshold
<<endl
; cout
<<"是否被設計被重新線性化:"<<status
.isRelinearized
<<endl
;cout
<<"是否被觀測到(僅僅與添加的新元素相關):"<< status
.isObserved
<< endl
;cout
<<"新值:"<<status
.isNew
<<endl
;cout
<<"是否為根團:"<<status
.inRootClique
<<endl
;cout
<< " }" << endl
;}Values currentEstimate
=isam
.calculateBestEstimate();currentEstimate
.print("Current estimate:");boost
::optional
<Point3
> pointEstimate
=smartFactor
->point(currentEstimate
);if (pointEstimate
) {cout
<< *pointEstimate
<< endl
;}graph
.resize(0);initialEstimate
.clear();}return 0;
}
ISAM2模板
#include <gtsam/geometry/Point2.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/nonlinear/ISAM2.h>
#include <gtsam/nonlinear/Values.h>
#include <gtsam/nonlinear/NonlinearFactor.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/slam/PriorFactor.h>
#include <gtsam/geometry/SimpleCamera.h>
#include <gtsam/slam/ProjectionFactor.h>
#include <gtsam/geometry/Cal3_S2.h>
#include <gtsam/geometry/Pose3.h>
#include <gtsam/geometry/Point3.h>
#include <gtsam/nonlinear/ISAM2Params.h>
#include <vector>
std
::vector
<gtsam
::Point3
> createPoints() {std
::vector
<gtsam
::Point3
> points
;points
.push_back(gtsam
::Point3(10.0,10.0,10.0));points
.push_back(gtsam
::Point3(-10.0,10.0,10.0));points
.push_back(gtsam
::Point3(-10.0,-10.0,10.0));points
.push_back(gtsam
::Point3(10.0,-10.0,10.0));points
.push_back(gtsam
::Point3(10.0,10.0,-10.0));points
.push_back(gtsam
::Point3(-10.0,10.0,-10.0));points
.push_back(gtsam
::Point3(-10.0,-10.0,-10.0));points
.push_back(gtsam
::Point3(10.0,-10.0,-10.0));return points
;
}
std
::vector
<gtsam
::Pose3
> createPoses(const gtsam
::Pose3
& init
= gtsam
::Pose3(gtsam
::Rot3
::Ypr(M_PI
/2,0,-M_PI
/2), gtsam
::Point3(30, 0, 0)),const gtsam
::Pose3
& delta
= gtsam
::Pose3(gtsam
::Rot3
::Ypr(0,-M_PI
/4,0), gtsam
::Point3(sin(M_PI
/4)*30, 0, 30*(1-sin(M_PI
/4)))),int steps
= 8) {std
::vector
<gtsam
::Pose3
> poses
;int i
= 1;poses
.push_back(init
);for(; i
< steps
; ++i
) {poses
.push_back(poses
[i
-1].compose(delta
));}return poses
;
};
using namespace std
;
using namespace gtsam
;
int main(int argc
, const char** argv
) {Cal3_S2
::shared_ptr
K(new Cal3_S2(50.0,50.0,0,50.0,50.0));auto measurementnoise
=noiseModel
::Isotropic
::Sigma(2,1.0);vector
<Point3
> points
=createPoints();vector
<Pose3
> poses
=createPoses();ISAM2Params parameters
;parameters
.relinearizeThreshold
=0.01; parameters
.relinearizeSkip
=1;ISAM2
isam(parameters
);NonlinearFactorGraph graph
;Values initialEstimate
;for(size_t i
=0;i
<poses
.size();i
++){for(size_t j
=0;j
<points
.size();j
++){SimpleCamera
camera(poses
[i
],*K
);Point2 measurement
=camera
.project(points
[j
]);graph
.emplace_shared
<GenericProjectionFactor
<Pose3
,Point3
,Cal3_S2
> >(measurement
,measurementnoise
,Symbol('x',i
),Symbol('l',j
),K
);}Pose3
kDeltaPose(Rot3
::Rodrigues(-0.1,0.2,0.25),Point3(0.05,-0.10,0.20));initialEstimate
.insert(symbol('x',i
),poses
[i
]*kDeltaPose
);if(i
==0){auto kPosePrior
=noiseModel
::Diagonal
::Sigmas(Vector6(0.3,0.3,0.3,0.1,0.1,0.1));graph
.emplace_shared
<PriorFactor
<Pose3
> >(Symbol('x',0),poses
[0],kPosePrior
);noiseModel
::Isotropic
::shared_ptr kPointPrior
=noiseModel
::Isotropic
::Sigma(3,0.1);graph
.emplace_shared
<PriorFactor
<Point3
> >(Symbol('l',0),points
[0],kPointPrior
);Point3
kDeltaPoints(-0.25,0.20,0.15);for(size_t j
=0;j
<points
.size();j
++){initialEstimate
.insert
<Point3
>(Symbol('l',j
),points
[j
]+kDeltaPoints
);}}else{isam
.update(graph
,initialEstimate
);isam
.update();Values currentEstimate
=isam
.calculateBestEstimate();cout
<< "****************************************************" << endl
;cout
<< "Frame " << i
<< ": " << endl
;currentEstimate
.print("Current estimate: ");graph
.resize(0);initialEstimate
.clear();}}return 0;
}
IMU預計分與相機代矯正位姿
#include <gtsam/navigation/CombinedImuFactor.h>
#include <gtsam/nonlinear/ISAM2.h>
#include <gtsam_unstable/slam/SmartStereoProjectionPoseFactor.h>
#include <gtsam/slam/PriorFactor.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>using namespace std
;
using namespace gtsam
;
using symbol_shorthand
::X
;
using symbol_shorthand
::V
;
using symbol_shorthand
::B
;struct IMUhelper
{IMUhelper(){{auto gaussian
=noiseModel
::Diagonal
::Sigmas(Vector6(5.0e-2,5.0e-2,5.0e-2,5.0e-3,5.0e-3,5.0e-3));auto huber
=noiseModel
::Robust
::Create(noiseModel
::mEstimator
::Huber
::Create(1.345),gaussian
);biasNoiseModel
=huber
;}{auto gassian
=noiseModel
::Isotropic
::Sigma(3,0.01);auto huber
=noiseModel
::Robust
::Create(noiseModel
::mEstimator
::Huber
::Create(1.345),gassian
);velocityNoiseModel
= huber
;}auto p
= boost
::make_shared
<PreintegratedCombinedMeasurements
::Params
>(Vector3(0.0, 9.8, 0.0));p
->accelerometerCovariance
=I_3x3
*pow(0.0565,2.0);p
->integrationCovariance
=I_3x3
*1e-9;p
->gyroscopeCovariance
=I_3x3
*pow(4.0e-5,2.0);p
->biasAccCovariance
=I_3x3
*pow(0.00002,2.0);p
->biasAccOmegaInt
=Matrix
::Identity(6,6)*1e-5;Rot3
iRb(0.036129, -0.998727, 0.035207,0.045417, -0.033553, -0.998404,0.998315, 0.037670, 0.044147);Point3
iTb(0.03,-0.025,-0.06);p
->body_P_sensor
= Pose3(iRb
, iTb
);Rot3 prior_rotation
= Rot3(I_3x3
);Pose3
prior_pose(prior_rotation
, Point3(0, 0, 0));Vector3
acc_bias(0.0, -0.0942015, 0.0);Vector3
gyro_bias(-0.00527483, -0.00757152, -0.00469968);priorImuBias
= imuBias
::ConstantBias(acc_bias
, gyro_bias
);prevState
=NavState(prior_pose
,Vector3
::Zero());propState
=prevState
;preintegrated
=new PreintegratedCombinedMeasurements(p
,priorImuBias
);}imuBias
::ConstantBias priorImuBias
; noiseModel
::Robust
::shared_ptr velocityNoiseModel
; noiseModel
::Robust
::shared_ptr biasNoiseModel
; NavState prevState
; NavState propState
; imuBias
::ConstantBias prevBias
; PreintegratedCombinedMeasurements
* preintegrated
;
};int main(int argc
, const char** argv
) {string file
="/home/n1/notes/gtsam/ISAM_IMU_CAMERA/ISAM2_SmartFactorStereo_IMU.txt";ifstream
in(file
);double fx
= 822.37;double fy
= 822.37;double cx
= 538.73;double cy
= 579.10;double baseline
= 0.372; Cal3_S2Stereo
::shared_ptr
K(new Cal3_S2Stereo(fx
, fy
, 0.0, cx
, cy
, baseline
));ISAM2Params parameters
;parameters
.relinearizeThreshold
=0.1;ISAM2
isam(parameters
);std
::map
<size_t
,SmartStereoProjectionPoseFactor
::shared_ptr
>smartFactors
;NonlinearFactorGraph graph
;Values InitialEstimate
;IMUhelper imu
;auto priorPoseNoise
=noiseModel
::Diagonal
::Sigmas(Vector6(0.1,0.1,0.1,0.1,0.1,0.1));graph
.emplace_shared
<PriorFactor
<Pose3
> >(X(1), Pose3
::identity(),priorPoseNoise
);InitialEstimate
.insert(X(0),Pose3
::identity());graph
.add(PriorFactor
<imuBias
::ConstantBias
>(B(1),imu
.priorImuBias
,imu
.biasNoiseModel
));InitialEstimate
.insert(B(0),imu
.priorImuBias
);graph
.add(PriorFactor
<Vector3
>(V(1),Vector3(0,0,0),imu
.velocityNoiseModel
));InitialEstimate
.insert(V(0),Vector3(0, 0, 0));int lastframe
=1;int frame
;while(1){char line
[1024];in
.getline(line
,sizeof(line
));stringstream
ss(line
);char type
;ss
>> type
>> frame
;if(frame
!=lastframe
||in
.eof()){cout
<<"當前幀:"<<lastframe
<<endl
;InitialEstimate
.insert(X(lastframe
),Pose3
::identity());InitialEstimate
.insert(V(lastframe
),Vector3(0,0,0));InitialEstimate
.insert(B(lastframe
),imu
.priorImuBias
);CombinedImuFactor
imuFactor(X(lastframe
-1),V(lastframe
- 1),X(lastframe
),V(lastframe
),B(lastframe
-1),B(lastframe
),*imu
.preintegrated
);graph
.add(imuFactor
);isam
.update(graph
,InitialEstimate
);Values currentEstimate
=isam
.calculateEstimate();imu
.propState
=imu
.preintegrated
->predict(imu
.prevState
,imu
.prevBias
);imu
.prevState
=NavState(currentEstimate
.at
<Pose3
>(X(lastframe
)),currentEstimate
.at
<Vector3
>(V(lastframe
)));imu
.prevBias
=currentEstimate
.at
<imuBias
::ConstantBias
>(B(lastframe
));imu
.preintegrated
->resetIntegrationAndSetBias(imu
.prevBias
);graph
.resize(0);InitialEstimate
.print();InitialEstimate
.clear();if(in
.eof()){break;}}if(type
=='i'){double ax
,ay
,az
;double gx
,gy
,gz
;double dt
=1/800.0;ss
>>ax
>>ay
>>az
;ss
>>gx
>>gy
>>gz
;Vector3
acc(ax
,ay
,az
);Vector3
gyro(gx
,gy
,gz
);imu
.preintegrated
->integrateMeasurement(acc
,gyro
,dt
);}else if(type
=='s'){int landmark
;double xl
,xr
,y
;ss
>>landmark
>>xl
>>xr
>>y
;if (smartFactors
.count(landmark
) == 0) {auto gaussian
= noiseModel
::Isotropic
::Sigma(3, 1.0);SmartProjectionParams
params(HESSIAN
, ZERO_ON_DEGENERACY
);smartFactors
[landmark
] = SmartStereoProjectionPoseFactor
::shared_ptr(new SmartStereoProjectionPoseFactor(gaussian
, params
));graph
.push_back(smartFactors
[landmark
]);}smartFactors
[landmark
]->add(StereoPoint2(xl
,xr
,y
),X(frame
),K
);}else {throw runtime_error("讀取錯誤: " + string(1, type
));}lastframe
= frame
;}return 0;
};
總結
以上是生活随笔為你收集整理的gtsam 学习十一(ISAM2 实践)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。