ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Declaring moveit robot model in header file

asked 2019-07-08 12:55:01 -0600

Kolohe113 gravatar image

Hello everyone, I am new to C++ with ROS and would love to get some help. Currently, I am trying to write a C++ class object. In the class, I want to share a moveit robot model so I can use it among different function. Like this:

robot.cpp

#include <iostream>
#include <moveit/robot_model_loader/robot_model_loader.h>
#include <moveit/robot_model/robot_model.h>
#include <moveit/robot_state/robot_state.h>
#include "robot.h"

LoadRobot::LoadRobot()
{
    // Load robot model
  robot_model_loader::RobotModelLoader robot_model_loader("robot_description");
  robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel();

}

void LoadRobot::create_group(){
 const robot_state::JointModelGroup* joint_model_group = kinematic_model->getJointModelGroup("panda_arm");

//do something with joint_model_group ...
}

robot.h

#ifndef _ROBOT
#define _ROBOT

class LoadRobot{
public:
    LoadRobot();
    void create_group();
};
#endif

However, the LoadRobot constructor cannot pass kinematic_model to create_group(). What should I do instead? Should I put what's in the constructor to the header file and make them private?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-07-08 15:26:00 -0600

gvdhoorn gravatar image

This is purely a C++ question. In the future it would be best if you'd post these on a forum dedicated to C++ questions.

In this particular case:

LoadRobot::LoadRobot()
{
    // Load robot model
  robot_model_loader::RobotModelLoader robot_model_loader("robot_description");
  robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel();
}

the problem here is that kinematic_model is a variable defined and declared in the local scope of LoadRobot.

As soon as that scope exits (ie: ceases to exist), it will destroy all variables declared in it, including kinematic_model.

kinematic_model simply doesn't exist any more in create_group().

Should I put what's in the constructor to the header file and make them private?

I'm not sure I entirely understand what you mean by this, but one option could be to make kinematic_model a member variable indeed, and then initialise it in the constructor. At that point it would be part of a scope other than the local one of LoadRobot and persist to be used by create_group().

But again: this is purely a C++ related question. The fact that ROS classes are being used (or more precisely: MoveIt) does not factor in here.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-08 12:55:01 -0600

Seen: 518 times

Last updated: Jul 08 '19