控制工具基础案例

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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangrelay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值