写一下gazebo差速驱动插件源码
时间: 2023-03-12 21:04:39 浏览: 117
驱动源代码
4星 · 用户满意度95%
我可以为您提供一份用于 Gazebo 差速驱动插件的源码:#include <gazebo/common/Plugin.hh>
#include <gazebo/physics/physics.hh>namespace gazebo
{
class DiffDrivePlugin : public ModelPlugin
{
public: void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
{
// 获取模型和世界指针
this->model = _parent;
this->world = this->model->GetWorld(); // 读取插件参数
this->robotNamespace = "";
if (_sdf->HasElement("robotNamespace"))
this->robotNamespace = _sdf->GetElement("robotNamespace")->Get<std::string>(); // 设置差速器参数
this->leftJointName = "left_wheel_joint";
if (_sdf->HasElement("left_joint"))
this->leftJointName = _sdf->GetElement("left_joint")->Get<std::string>();
this->rightJointName = "right_wheel_joint";
if (_sdf->HasElement("right_joint"))
this->rightJointName = _sdf->GetElement("right_joint")->Get<std::string>();
// 初始化差速器
this->leftJoint = this->model->GetJoint(this->leftJointName);
this->rightJoint = this->model->GetJoint(this->rightJointName); // 初始化控制器
this->pid = common::PID(0.1, 0.01, 0.1); // 订阅广播状态
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
std::bind(&DiffDrivePlugin::OnUpdate, this));
} // 每一步更新
public: void OnUpdate()
{
// 计算差速器控制力
double leftSpeed = this->pid.Update(this->leftJoint->GetVelocity(0), this->leftSpeedTarget);
double rightSpeed = this->pid.Update(this->rightJoint->GetVelocity(0), this->rightSpeedTarget);
this->leftJoint->SetForce(0, leftSpeed);
this->rightJoint->SetForce(0, rightSpeed);
} // 读取模型指针
private: physics::ModelPtr model; // 读取世界指针
private: physics::WorldPtr world; // 读取命名空间
private: std::string robotNamespace; // 读取左右引擎的名字
private: std::string leftJointName;
private: std::string rightJointName; // 读取左右引擎的指针
private: physics::JointPtr leftJoint;
private: physics::JointPtr rightJoint; // 读取PID控制器
private: common::PID pid; // 读取左右引擎的目标速度
private: double leftSpeedTarget = 0;
private: double rightSpeedTarget = 0; // 读取更新连接
private: event::ConnectionPtr updateConnection;
}; // 注册插件
GZ_REGISTER_MODEL_PLUGIN(DiffDrivePlugin)
}
阅读全文