插件配置
一、准备工作
二、创建一个API
三、测试消息传递API
1、val.cc
编辑cmake文件
我们已经对转速进行了硬编码,但是每次修改参数就需要重新编译,不能实现动态地调节插件。因此,在本节中,我们将修改插件以读取自定义SDF参数,该参数是Velodyne的目标速度。
首先添加一个新标签。新标签可以是任何东西,只要它是有效的XML。我们的插件将可以访问Load函数中的值。首先打开world文件编辑
一、准备工作
gedit ~/velodyne_plugin/velodyne.world
添加,里面包含一个标签。
25
现在,让我们在插件的Load函数中读取此值。
gedit ~/velodyne_plugin/velodyne_plugin.cc
修改Load函数的末尾,使用 sdf::ElementPtr参数来读取。
// Default to zero velocity
double velocity = 0;
// Check that the velocity element exists, then read the value
if (_sdf->HasElement("velocity"))
velocity = _sdf->Get("velocity");
// Set the joint's target velocity. This target velocity is just
// for demonstration purposes.
this->model->GetJointController()->SetVelocityTarget(
this->joint->GetScopedName(), velocity);
重新编译运行看下效果:
cd ~/velodyne_plugin/build
cmake ../
make
gazebo --verbose ../velodyne.world
二、创建一个API
通过SDF调整目标速度非常方便,但支持动态调整会更好。此更改将需要添加其他程序可以用来更改速度值的API。
我们可以使用两种API类型:
消息传递和函数。消息传递依赖于gazebo的传输机制,并且涉及创建一个命名主题,发布者可以在该主题上发送双精度值(double)。插件会收到这些消息,并适当设置速度。消息传递对于进程间通信很方便。
函数方法将创建一个新的公共函数来调整速度。为此,新插件将从我们当前的插件继承。子插件将由Gazebo而不是我们当前的插件实例化,并将通过调用我们的函数来控制速度。将gazebo连接到ROS时最常使用这种方法。
由于我们插件的简单性,可以轻松同时实现两者。
1、打开velodyne_plugin.cc文件,添加以下函数:
/// \brief Set the velocity of the Velodyne
/// \param[in] _vel New target velocity
public: void SetVelocity(const double &_vel)
{
// Set the joint's target velocit