RobotMsg.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
8 #include <mc_rtc/gui/elements.h>
9 #include <mc_rtc/gui/types.h>
10 
11 #include <mc_rbdyn/Robot.h>
12 
13 namespace mc_rtc::gui
14 {
15 
16 namespace details
17 {
18 
27 template<typename GetT>
28 struct RobotMsgImpl : public Element
29 {
30  static constexpr auto type = Elements::RobotMsg;
31 
32  RobotMsgImpl(const std::string & name, GetT get_fn) : Element(name), get_fn_(get_fn)
33  {
34  static_assert(CheckReturnType<GetT, mc_rbdyn::Robot>::value, "Robot element must return an mc_rbdyn::Robot");
35  }
36 
37  static constexpr size_t write_size() { return Element::write_size() + 7; }
38 
40  {
41  const mc_rbdyn::Robot & robot = get_fn_();
42  update(robot);
43  Element::write(builder);
44  builder.write(robot.module().parameters());
45  builder.write(msg_.q);
46  builder.write(msg_.alpha);
47  builder.write(msg_.alphaD);
48  builder.write(msg_.tau);
49  builder.write(robot.mbc().force);
50  builder.write(robot.posW());
51  }
52 
53 private:
54  GetT get_fn_;
55  RobotMsgData msg_;
56 
57  void update(const mc_rbdyn::Robot & robot)
58  {
59  msg_.q.resize(robot.mb().nrParams());
60  rbd::paramToVector(robot.mbc().q, msg_.q);
61  msg_.alpha.resize(robot.mb().nrDof());
62  rbd::paramToVector(robot.mbc().alpha, msg_.alpha);
63  msg_.alphaD.resize(msg_.alpha.size());
64  rbd::paramToVector(robot.mbc().alphaD, msg_.alphaD);
65  msg_.tau.resize(msg_.alpha.size());
66  rbd::paramToVector(robot.mbc().jointTorque, msg_.tau);
67  }
68 };
69 
70 } // namespace details
71 
73 template<typename GetT>
74 auto RobotMsg(const std::string & name, GetT get_fn)
75 {
76  return details::RobotMsgImpl(name, get_fn);
77 }
78 
79 } // namespace mc_rtc::gui
mc_rtc::gui::RobotMsgData::alphaD
Eigen::VectorXd alphaD
Definition: types.h:498
mc_rtc::gui::Element
Definition: elements.h:58
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::details::RobotMsgImpl::type
static constexpr auto type
Definition: RobotMsg.h:30
types.h
mc_rbdyn::Robot::mb
rbd::MultiBody & mb()
mc_rtc::gui::RobotMsgData::tau
Eigen::VectorXd tau
Definition: types.h:499
mc_rtc::gui::Element::write
void write(mc_rtc::MessagePackBuilder &)
Definition: elements.h:83
mc_rbdyn::Robot
Definition: Robot.h:62
mc_rtc::gui::RobotMsgData::alpha
Eigen::VectorXd alpha
Definition: types.h:497
mc_rtc::gui::details::RobotMsgImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: RobotMsg.h:39
mc_rbdyn::RobotModule::parameters
const std::vector< std::string > & parameters() const
Definition: RobotModule.h:511
mc_rbdyn::Robot::module
const RobotModule & module() const
mc_rtc::gui::Elements::RobotMsg
@ RobotMsg
mc_rtc::gui::RobotMsgData::q
Eigen::VectorXd q
Definition: types.h:496
elements.h
mc_rbdyn::Robot::posW
const sva::PTransformd & posW() const
mc_rtc::gui::RobotMsg
auto RobotMsg(const std::string &name, GetT get_fn)
Definition: RobotMsg.h:74
mc_rtc::gui::details::RobotMsgImpl::write_size
static constexpr size_t write_size()
Definition: RobotMsg.h:37
Robot.h
mc_rtc::MessagePackBuilder::write
void write()
mc_rbdyn::Robot::mbc
rbd::MultiBodyConfig & mbc()
mc_rtc::gui::Element::write_size
static constexpr size_t write_size()
Definition: elements.h:76
mc_rtc::gui::details::RobotMsgImpl::RobotMsgImpl
RobotMsgImpl(const std::string &name, GetT get_fn)
Definition: RobotMsg.h:32
traits.h
mc_rtc::gui::RobotMsgData
Definition: types.h:481
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui::details::RobotMsgImpl
Definition: RobotMsg.h:28
mc_rtc::gui
Definition: Observer.h:15
mc_rtc::gui::Element::name
const std::string & name() const
Definition: elements.h:61