我试图通过发送线性速度的消息来使仿真中的机器人移动精确的距离。现在,我的实现并不能使机器人移动精确的距离。这是一些示例代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我四处询问,有人建议将PID控制器用于反馈回路可以解决此问题,但是在阅读Wikipedia页面后,我并没有真正了解如何在这种情况下使用它。维基百科页面上有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
Run Code Online (Sandbox Code Playgroud)
在速度和距离的背景下,我将如何实现呢?距离错误吗?还是速度?有人可以帮忙吗?什么是积分?衍生物?Kp?淇?d 等等
谢谢。
对于您的问题,设定点将为(x,y)。process_feedback将为(px,py)。输出将是您需要行驶的速度。Kp,Ki和Kd是可以调整以获得所需行为的参数。例如,如果Kd太低,则在接近目标时可能不会放慢速度,从而越过目标。
PID控制器要考虑三件事:
这当然是一个很大的因素。如果您在点A处并且目标在点B处,那么从A到B的向量会告诉您很多有关如何转向的信息,但这不是唯一的因素。
如果您正在快速接近目标并接近目标,则实际上需要放慢速度。导数有助于将其考虑在内。
您的机器人实际上可能并没有完全按照您的指示去做。积分可帮助您确定需要多少补偿。