使用PID控制器以恒定速度行驶精确距离。

3

我正在尝试通过发送带有线性速度的消息,在模拟中使机器人移动一个精确的距离。目前我的实现并不能让机器人移动一个精确的距离。以下是一些示例代码:

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?等等。
谢谢。
1个回答

4
针对您的问题,设定值将是(x,y)。过程反馈将是(px,py)。输出将是您需要行驶的速度。 Kp,Ki和Kd是您可以调整以获得所需行为的参数。例如,如果Kd太低,则可能会因未在接近目标时减速而超过目标。
PID控制器考虑三个因素:
误差:您希望到达的位置与您实际所在位置之间的差异
这当然是一个重要因素。如果您在点A处,您的目标在点B处,则从A到B的向量告诉您很多关于如何转向,但它并不是唯一的因素。
导数:您接近目标的速度有多快
如果您快速接近目标并且距离目标很近,则实际上需要减速。导数有助于考虑这一点。
积分:对齐误差
您的机器人可能实际上并不完全按照您的指示执行。积分有助于确定您需要补偿多少。

非常感谢您的回复。那么,我需要在我的代码中添加测量时间的代码才能得到dt吗? - Jigglypuff
1
@Jigglypuff:是的,如果你不知道自己采样位置的频率,那么你需要找到一种确定它的方法。 - Vaughn Cato
对于所有的 K 值,我应该在设置时进行试错,还是有一般的经验法则? - Jigglypuff
1
@Jigglypuff:试错确实是一种方法。可能有一些理论可以估算好的值,但那超出了我的知识范围。 - Vaughn Cato
如果可以的话,我还有一个问题。退出循环的条件是什么?我最初使用的是(error < 0),但显然这不起作用,因为错误总是为正数。 - Jigglypuff
1
@Jigglypuff:也许最好的事情是当你的输出幅度低于一定容差时,你不会再取得太多进展。 - Vaughn Cato

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接