3 #include <mujoco/mujoco.h>
13 mjp_defaultPlugin(&plugin);
15 plugin.name =
"MujocoRosUtils::ActuatorCommand";
17 plugin.capabilityflags |= mjPLUGIN_ACTUATOR;
18 plugin.capabilityflags |= mjPLUGIN_PASSIVE;
20 const char * attributes[] = {
"actuator_name",
"topic_name"};
22 plugin.nattribute =
sizeof(attributes) /
sizeof(attributes[0]);
23 plugin.attributes = attributes;
25 plugin.nstate = +[](
const mjModel *,
29 plugin.nsensordata = +[](
const mjModel *,
34 plugin.needstage = mjSTAGE_VEL;
36 plugin.init = +[](
const mjModel * m, mjData * d,
int plugin_id)
43 d->plugin_data[plugin_id] =
reinterpret_cast<uintptr_t
>(plugin_instance);
47 plugin.destroy = +[](mjData * d,
int plugin_id)
50 d->plugin_data[plugin_id] = 0;
53 plugin.
reset = +[](
const mjModel * m,
double *,
54 void * plugin_data,
int plugin_id)
56 auto * plugin_instance =
reinterpret_cast<class
ActuatorCommand *
>(plugin_data);
57 plugin_instance->
reset(m, plugin_id);
60 plugin.compute = +[](
const mjModel * m, mjData * d,
int plugin_id,
int
63 auto * plugin_instance =
reinterpret_cast<class
ActuatorCommand *
>(d->plugin_data[plugin_id]);
64 plugin_instance->
compute(m, d, plugin_id);
67 mjp_registerPlugin(&plugin);
73 const char * actuator_name_char = mj_getPluginConfig(m, plugin_id,
"actuator_name");
74 if(strlen(actuator_name_char) == 0)
76 mju_error(
"[ActuatorCommand] `actuator_name` is missing.");
80 for(; actuator_id < m->nu; actuator_id++)
82 if(strcmp(actuator_name_char, mj_id2name(m, mjOBJ_ACTUATOR, actuator_id)) == 0)
87 if(actuator_id == m->nu)
89 mju_error(
"[ActuatorCommand] The actuator with the specified name not found.");
94 const char * topic_name_char = mj_getPluginConfig(m, plugin_id,
"topic_name");
95 std::string topic_name =
"";
96 if(strlen(topic_name_char) > 0)
98 topic_name = std::string(topic_name_char);
101 std::cout <<
"[ActuatorCommand] Create." << std::endl;
109 std::string topic_name)
110 : actuator_id_(actuator_id)
112 if(topic_name.empty())
114 std::string actuator_name = std::string(mj_id2name(m, mjOBJ_ACTUATOR, actuator_id));
115 topic_name =
"mujoco/" + actuator_name;
119 char ** argv =
nullptr;
120 if(!ros::isInitialized())
122 ros::init(argc, argv,
"mujoco_ros", ros::init_options::NoSigintHandler);
125 nh_ = std::make_shared<ros::NodeHandle>();
146 if(!std::isnan(
ctrl_))
149 ctrl_ = std::numeric_limits<mjtNum>::quiet_NaN();