控制工具基础案例
【摘要】
1. 阻尼振荡器
#include <ct/core/core.h>int main(int argc, char** argv){ // a damped oscillator has two states, position and velocity const size_t state_dim ...
1. 阻尼振荡器
#include <ct/core/core.h>
int main(int argc, char** argv)
{
// a damped oscillator has two states, position and velocity
const size_t state_dim = ct::core::SecondOrderSystem::STATE_DIM; // = 2
// create a state
ct::core::StateVector<state_dim> x;
// we initialize it at a point with unit deflection and zero velocity
x(0) = 1.0;
x(1) = 0.0;
// create an oscillator, which is a predefined system in ct_core
double w_n = 10;
std::shared_ptr<ct::core::SecondOrderSystem> oscillator(new ct::core::SecondOrderSystem(w_n));
// create an integrator
ct::core::Integrator<state_dim> integrator(oscillator);
// simulate 1000 steps
double dt = 0.001;
ct::core::Time t0 = 0.0;
size_t nSteps = 1000;
integrator.integrate_n_steps(x, t0, nSteps, dt);
// print the new state
std::cout << "state after integration: " << x.transpose() << std::endl;
return 0;
}
2. 一个受摩擦力影响的简单质点
#include <ct/core/core.h> // as usual, include CT
// create a class that derives from ct::core::System
class Masspoint : public ct::core::System<2>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
static const size_t STATE_DIM = 2;
// constructor
Masspoint(double mass, double d) : mass_(mass), d_(d) {}
// copy constructor
Masspoint(const Masspoint& other) : mass_(other.mass_), d_(other.d_) {}
// destructor
~Masspoint() = default;
// clone method for deep copying
Masspoint* clone() const override
{
return new Masspoint(*this); // calls copy constructor
}
// The system dynamics. We override this method which gets called by e.g. the Integrator
void computeDynamics(const ct::core::StateVector<STATE_DIM>& x,
const ct::core::Time& t,
ct::core::StateVector<STATE_DIM>& derivative) override
{
// first part of state derivative is the velocity
derivative(0) = x(1);
// second part is the acceleration which is caused by damper forces
derivative(1) = -d_ / mass_ * x(1);
}
private:
double mass_;
double d_;
};
#include <ct/core/core.h>
#include <ct/core/examples/Masspoint.h>
int main(int argc, char** argv)
{
// a damped oscillator has two states, position and velocity
const size_t state_dim = Masspoint::STATE_DIM; // = 2
// create a state
ct::core::StateVector<state_dim> x;
// we initialize it at 0
x.setZero();
// create our mass point instance
double mass = 1.0;
double d = 0.01;
std::shared_ptr<Masspoint> masspoint(new Masspoint(mass, d));
// create an integrator
ct::core::Integrator<state_dim> integrator(masspoint);
// simulate 1000 steps
double dt = 0.001;
ct::core::Time t0 = 0.0;
size_t nSteps = 1000;
integrator.integrate_n_steps(x, t0, nSteps, dt);
// print the new state
std::cout << "state after integration: " << x.transpose() << std::endl;
return 0;
}
文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。
原文链接:zhangrelay.blog.csdn.net/article/details/123435687
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)