Gazebo | Ignition | Community
Ask Your Question

Why this->model->SetLinearVel doesn't work?

asked 2019-06-15 18:01:31 -0500

rahul gravatar image

updated 2019-06-15 18:43:28 -0500

I am trying to give velocity to a model by using SetLinearVel for the model and apparently it doesn't seem to work. I looked up this tutorial: and they are doing model->GetLink("white_link_0")->SetLinearVel({0, 1, 0});. However, I am attempting to do this->model->SetLinearVel(ignition::math::Vector3d(2.0, 0.0, 0.0)); and it doesn't seem to work. I don't see my model moving. What is it that I am doing wrong?

The full code is given below:

namespace gazebo

    void Sparkle::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
        this->model = _parent; /*Store the pointer to the model*/
        this->world = _parent->GetWorld();
        this->robotNamespace = "";
        this->physicsEngine = (this->world)->Physics(); 

        /*Get the update rate from sdf*/
        if (_sdf->HasElement("updateRate"))
            this->updateRate = _sdf->GetElement("updateRate")->Get<double>();
        if (_sdf->HasElement("robotNamespace"))
            this->robotNamespace = _sdf->GetElement("robotNamespace")->Get<std::string>();

        this->velTopic = this->robotNamespace + "/vel";

        //Start up ros_node
        int argc = 0;
        char** argv = NULL;
        ros::init(argc, argv, "sparkle_sim", ros::init_options::NoSigintHandler | ros::init_options::AnonymousName);
        rosnode_ = new ros::NodeHandle(this->robotNamespace);

        this->ros_spinner_thread_ = boost::thread( boost::bind( &Sparkle::SimROSThread, this ) );


    void Sparkle::SimROSThread()
        ros::NodeHandle nodehandle;
        ros::Subscriber velsub = nodehandle.subscribe(this->velTopic, 1, &Sparkle::changeVel, this);
        ros::Rate loop_rate(10);

        while (this->rosnode_->ok())
            loop_rate.sleep( );

    void Sparkle::changeVel(const geometry_msgs::Twist::ConstPtr& msg)
        double x = msg->linear.x;

        ROS_INFO("Velocity to set is %f\n", x);

        this->model->SetLinearVel(ignition::math::Vector3d(x, 0.0, 0.0));

    void Sparkle::changePos(const gazebo_msgs::ModelState::ConstPtr& msg)
        gazebo_msgs::ModelState newMsgs;
        newMsgs.model_name = this->robotNamespace;
        newMsgs.pose.position = msg->pose.position;
        newMsgs.pose.orientation = msg->pose.orientation;

Apart from the fact that tutorial is using old math library, what is the difference between these two methods and why my method doesn't seem to work?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-06-20 08:14:03 -0500

kumpakri gravatar image

updated 2019-06-20 08:14:56 -0500

Few questions:

  1. Did you compile your plugin? Does it compile without errors?
  2. How do you know the plugin is actually loaded?
  3. Which version of Gazebo are you using?
  4. What does the ROS_INFO("Velocity to set is %f\n", x); line actually print in the terminal?
  5. What does the robot look like? What are its physical properties? Is it even able to move in the x direction? Wouldn't, for example, friction prevent the robot from moving?
  6. Can you show us the header file?
edit flag offensive delete link more



Thanks for the suggestions. I took your steps into consideration. I am using Gazebo 9.0. The problem I found was in Point no. 5. My Inertial for robot model was not correct and upon fixing, it worked just fine.

rahul gravatar imagerahul ( 2019-06-20 13:48:36 -0500 )edit

Question Tools

1 follower


Asked: 2019-06-15 18:01:31 -0500

Seen: 606 times

Last updated: Jun 20 '19