首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >控制工具基础案例

控制工具基础案例

作者头像
zhangrelay
发布于 2022-05-01 09:58:18
发布于 2022-05-01 09:58:18
4030
举报

1. 阻尼振荡器

代码语言:javascript
复制
#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. 一个受摩擦力影响的简单质点

代码语言:javascript
复制
#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_;
};
代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档