控制工具基础案例

举报
zhangrelay 发表于 2022/03/12 23:58:37 2022/03/12
【摘要】 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. 阻尼振荡器


  
  1. #include <ct/core/core.h>
  2. int main(int argc, char** argv)
  3. {
  4. // a damped oscillator has two states, position and velocity
  5. const size_t state_dim = ct::core::SecondOrderSystem::STATE_DIM; // = 2
  6. // create a state
  7. ct::core::StateVector<state_dim> x;
  8. // we initialize it at a point with unit deflection and zero velocity
  9. x(0) = 1.0;
  10. x(1) = 0.0;
  11. // create an oscillator, which is a predefined system in ct_core
  12. double w_n = 10;
  13. std::shared_ptr<ct::core::SecondOrderSystem> oscillator(new ct::core::SecondOrderSystem(w_n));
  14. // create an integrator
  15. ct::core::Integrator<state_dim> integrator(oscillator);
  16. // simulate 1000 steps
  17. double dt = 0.001;
  18. ct::core::Time t0 = 0.0;
  19. size_t nSteps = 1000;
  20. integrator.integrate_n_steps(x, t0, nSteps, dt);
  21. // print the new state
  22. std::cout << "state after integration: " << x.transpose() << std::endl;
  23. return 0;
  24. }

2. 一个受摩擦力影响的简单质点


  
  1. #include <ct/core/core.h> // as usual, include CT
  2. // create a class that derives from ct::core::System
  3. class Masspoint : public ct::core::System<2>
  4. {
  5. public:
  6. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  7. static const size_t STATE_DIM = 2;
  8. // constructor
  9. Masspoint(double mass, double d) : mass_(mass), d_(d) {}
  10. // copy constructor
  11. Masspoint(const Masspoint& other) : mass_(other.mass_), d_(other.d_) {}
  12. // destructor
  13. ~Masspoint() = default;
  14. // clone method for deep copying
  15. Masspoint* clone() const override
  16. {
  17. return new Masspoint(*this); // calls copy constructor
  18. }
  19. // The system dynamics. We override this method which gets called by e.g. the Integrator
  20. void computeDynamics(const ct::core::StateVector<STATE_DIM>& x,
  21. const ct::core::Time& t,
  22. ct::core::StateVector<STATE_DIM>& derivative) override
  23. {
  24. // first part of state derivative is the velocity
  25. derivative(0) = x(1);
  26. // second part is the acceleration which is caused by damper forces
  27. derivative(1) = -d_ / mass_ * x(1);
  28. }
  29. private:
  30. double mass_;
  31. double d_;
  32. };

  
  1. #include <ct/core/core.h>
  2. #include <ct/core/examples/Masspoint.h>
  3. int main(int argc, char** argv)
  4. {
  5. // a damped oscillator has two states, position and velocity
  6. const size_t state_dim = Masspoint::STATE_DIM; // = 2
  7. // create a state
  8. ct::core::StateVector<state_dim> x;
  9. // we initialize it at 0
  10. x.setZero();
  11. // create our mass point instance
  12. double mass = 1.0;
  13. double d = 0.01;
  14. std::shared_ptr<Masspoint> masspoint(new Masspoint(mass, d));
  15. // create an integrator
  16. ct::core::Integrator<state_dim> integrator(masspoint);
  17. // simulate 1000 steps
  18. double dt = 0.001;
  19. ct::core::Time t0 = 0.0;
  20. size_t nSteps = 1000;
  21. integrator.integrate_n_steps(x, t0, nSteps, dt);
  22. // print the new state
  23. std::cout << "state after integration: " << x.transpose() << std::endl;
  24. return 0;
  25. }

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/123435687

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。