我试图通过发送线速度的消息使模拟中的机器人移动一段精确的距离。现在我的实施并没有让机器人移动一个确切的距离。这是一些示例代码:
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?文? KD? e.t.c。
感谢。
答案 0 :(得分:5)
对于您的问题,设定值将为(x,y)。 process_feedback将是(px,py)。输出将是您需要行进的速度。 Kp,Ki和Kd是您可以调整以获得所需行为的参数。例如,如果Kd太低,你可以通过在接近目标时不会减速来射击目标。
PID控制器需要考虑三个因素:
这当然是一个重要因素。如果你在A点并且你的目标位于B点,那么从A到B的向量会告诉你很多关于你需要如何操纵的东西,但它不是唯一的因素。
如果你快速接近目标并且接近它,你实际上需要放慢速度。衍生工具有助于将其考虑在内。
您的机器人可能实际上并没有完全按照您的要求去做。积分有助于确定需要多少补偿。