How to programatically set joint friction torque?

I have a car model in Gazebo. Each of the 4 wheels has a continuous joint with friction torque in the urdf file. I want to set the friction torque value dynamically in the source code (instead of hardcoded in the urdf) to simulate the brake pedal. How can I do this? Can I use some kind of Gazebo plugin?

P.S I cannot manually apply a reverse torque to the joint because the car will never truly stop if I push the brake pedal. Instead it will shake forward and backward with a low speed forever.

The Load method is given a physics::ModelPtr. Call GetJoint(name) where name is an std::string with the name of the joint you would like to set friction on. This will return a physics::JointPtr. On this pointer call SetParam("friction", 0, 1.0). The second argument is the joint axis. 0 says use the first axis; revolute joints only have one. The last parameter is the minimum torque required to move the joint in newton-meters.

Note, SetParam is special in that the third parameter has to be exactly the right type. The compiler can't automatically cast it for you. In this case "friction" is a double. If the third parameter is 1.0f or 1 instead of 1.0 the call will fail.

Comments

Thanks! One follow-up question: any ideas on how to pass argument from source code to this plugin? Different cars will apply brake torque on different set of joints, but I'd like to use a same plugin. Also the required friction torque is computed in the source code. Can I use ros parameter server to set the joint names and use a subscriber in the plugin to get the required friction?

Sure, make the arguments part of the SDF for each instance of the plugin. SDF allows aribtrary XML tags to be passed to a plugin`<plugin name="my_fancy_plugin_instance" filename="libMyFancyPlugin.so"><max_brake_torque>12345</max_brake_torque></plugin>`. See the random velocity plugin for an example http://gazebosim.org/tutorials?tut=plugins_random_velocity

Ah, sneaky edit about ROS parameters! I believe you can initialize a ROS node inside the plugin, and from there do whatever you like through the ROS API. The plugins will need a way of identifying which which ROS param belongs to them, but the SDF arguments gives a way to name the instances.