Home | Tutorials | Wiki | Issues
Ask Your Question
0

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

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

rahul gravatar image

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

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: http://gazebosim.org/tutorials?tut=se... 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
{
    Sparkle::Sparkle()
    {

    }
    void Sparkle::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
    {
        ROS_INFO_STREAM("Load");
        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_INFO_STREAM("SimROSThread,");
        ros::NodeHandle nodehandle;
        ros::Subscriber velsub = nodehandle.subscribe(this->velTopic, 1, &Sparkle::changeVel, this);
        ros::Rate loop_rate(10);

        while (this->rosnode_->ok())
        {
            ROS_INFO_STREAM("Looping");
            ros::spinOnce();
            loop_rate.sleep( );
        }
    }

    void Sparkle::changeVel(const geometry_msgs::Twist::ConstPtr& msg)
    {
        ROS_INFO_STREAM("ChangingVel");
        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;
        this->modelPub.publish(newMsgs);
    }
   GZ_REGISTER_MODEL_PLUGIN(Sparkle)
}

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
1

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

kumpakri gravatar image

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

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

Comments

1

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 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 91 times

Last updated: Jun 20