Skip to content

Commit

Permalink
Merge pull request #425 from antodld/topic/guiRobotMsg
Browse files Browse the repository at this point in the history
[mc_rtc_gui] change the robot type to handle more informations of mc_rbdyn::Robot
  • Loading branch information
gergondet authored Jan 30, 2024
2 parents be8887c + bf72892 commit 585c5db
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/mc_control/ControllerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ struct MC_CONTROL_CLIENT_DLLAPI ControllerClient
default_impl("Robot", id);
}

virtual void robot_msg(const ElementId & id, const mc_rtc::gui::RobotMsgData & /*msg*/)
{
default_impl("RobotMsg", id);
}

/** Should display the visual element \p visual at the position \p pose */
virtual void visual(const ElementId & id,
[[maybe_unused]] const rbd::parsers::Visual & visual,
Expand Down
79 changes: 79 additions & 0 deletions include/mc_rtc/gui/RobotMsg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
*/

#pragma once

#include <mc_rtc/gui/details/traits.h>
#include <mc_rtc/gui/elements.h>
#include <mc_rtc/gui/types.h>

#include <mc_rbdyn/Robot.h>

namespace mc_rtc::gui
{

namespace details
{

/** Robot should display a robot model in the environment
*
* The element provides the following data to the client:
* - the parameters passed to RobotLoader to get the RobotModule (vector<string>)
* - the current robot configuration (vector<vector<double>>)
*
* \tparam GetT Should return an mc_rbdyn::Robot
*/
template<typename GetT>
struct RobotMsgImpl : public Element
{
static constexpr auto type = Elements::RobotMsg;

RobotMsgImpl(const std::string & name, GetT get_fn) : Element(name), get_fn_(get_fn)
{
static_assert(CheckReturnType<GetT, mc_rbdyn::Robot>::value, "Robot element must return an mc_rbdyn::Robot");
}

static constexpr size_t write_size() { return Element::write_size() + 7; }

void write(mc_rtc::MessagePackBuilder & builder)
{
const mc_rbdyn::Robot & robot = get_fn_();
update(robot);
Element::write(builder);
builder.write(robot.module().parameters());
builder.write(msg_.q);
builder.write(msg_.alpha);
builder.write(msg_.alphaD);
builder.write(msg_.tau);
builder.write(robot.mbc().force);
builder.write(robot.posW());
}

private:
GetT get_fn_;
RobotMsgData msg_;

void update(const mc_rbdyn::Robot & robot)
{
msg_.q.resize(robot.mb().nrParams());
rbd::paramToVector(robot.mbc().q, msg_.q);
msg_.alpha.resize(robot.mb().nrDof());
rbd::paramToVector(robot.mbc().alpha, msg_.alpha);
msg_.alphaD.resize(msg_.alpha.size());
rbd::paramToVector(robot.mbc().alphaD, msg_.alphaD);
msg_.tau.resize(msg_.alpha.size());
rbd::paramToVector(robot.mbc().jointTorque, msg_.tau);
}
};

} // namespace details

/** Helper function to create a RobotImpl */
template<typename GetT>
auto RobotMsg(const std::string & name, GetT get_fn)
{
return details::RobotMsgImpl(name, get_fn);
}

} // namespace mc_rtc::gui
3 changes: 2 additions & 1 deletion include/mc_rtc/gui/elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ enum class Elements
PolyhedronTrianglesList,
PolyhedronVerticesTriangles,
GenericArray,
OneOf
OneOf,
RobotMsg
};

/** Element is the common class for every element's type available in the
Expand Down
25 changes: 25 additions & 0 deletions include/mc_rtc/gui/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,31 @@ struct MC_RTC_GUI_DLLAPI PolyhedronConfig
///< passed along with the triangles
bool fixed_vertices_color = true;
};

/** Provides full information about a robot kinematic and dynamic state */
struct MC_RTC_GUI_DLLAPI RobotMsgData
{
RobotMsgData() = default;
RobotMsgData(const std::vector<std::string> & params,
const Eigen::VectorXd & q,
const Eigen::VectorXd & alpha,
const Eigen::VectorXd & alphaD,
const Eigen::VectorXd & tau,
const std::vector<sva::ForceVecd> & forces,
const sva::PTransformd & posW)
: parameters(params), q(q), alpha(alpha), alphaD(alphaD), tau(tau), forces(forces), posW(posW)
{
}

std::vector<std::string> parameters;
Eigen::VectorXd q;
Eigen::VectorXd alpha;
Eigen::VectorXd alphaD;
Eigen::VectorXd tau;
std::vector<sva::ForceVecd> forces;
sva::PTransformd posW;
};

} // namespace gui

template<>
Expand Down
6 changes: 6 additions & 0 deletions src/mc_control/ControllerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ void ControllerClient::handle_widget(const ElementId & id, const mc_rtc::Configu
case Elements::Robot:
robot(id, data[3], data[4], data[5]);
break;
case Elements::RobotMsg:
{
mc_rtc::gui::RobotMsgData msg(data[3], data[4], data[5], data[6], data[7], data[8], data[9]);
robot_msg(id, msg);
break;
}
case Elements::Visual:
if(data[4].size() == 3)
{
Expand Down

0 comments on commit 585c5db

Please sign in to comment.