我正在尝试通过发送带有线性速度的消息,在模拟中使机器人移动一个精确的距离。目前我的实现并不能让机器人移动一个精确的距离。以下是一些示例代码:
void Robot::travel(double x, double y)
{
// px and py are the current positions (constantly gets updated as the robot moves in the simulation)
// x and y is the target position that I want to go to
double startx = px;
double starty = py;
double distanceTravelled = 0;
align(x, y);
// This gets the distance from the robot's current position to the target position
double distance = calculateDistance(px, py, x, y);
// Message with velocity
geometry_msgs::Twist msg;
msg.linear.x = 1;
msg.angular.z = 0;
ros::Rate loop_rate(10);
while (distanceTravelled < distance)
{
distanceTravelled = calculateDistance(startx, starty, px, py);
// Publishes message telling the robot to move with the linear.x velocity
RobotVelocity_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
我询问了周围的人,有人建议使用PID控制器作为反馈环路来解决这个问题,但是在阅读维基百科页面后,我不太明白如何在这种情况下使用它。维基百科页面上有PID算法的伪代码,但我不知道哪个对应哪个。
previous_error = setpoint - process_feedback
integral = 0
start:
wait(dt)
error = setpoint - process_feedback
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*error) + (Ki*integral) + (Kd*derivative)
previous_error = error
goto start
如何在速度和距离的背景下实现这个?是距离误差还是速度?能否请有经验的人帮助一下?积分是什么?导数?Kp?Ki?Kd?等等。
谢谢。