gpt4 book ai didi

java - 如何用Java实现多线程PID Controller ?

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:19 25 4
gpt4 key购买 nike

我对 Java 还不太熟悉,所以还没有完全掌握多线程的概念。我想创建一个 PIDController 类来允许我执行此操作:

ControllerMethods methods = new ControllerMethods()
{
public long getError(long setpoint)
{
//get an input
}
public void setOutput(long value)
{
//do something
}
public void isComplete(long setpoint)
{
return getError() == 0;
}
};

PIDController motorPID = new PIDController(setpoint, kp, ki, kd, methods);

motorPID.run();
//runs the PID controller to completion (methods.isComplete() == true)

motorPID.run(false);
//starts the PID controller in a separate thread, allowing
//continual monitoring in the current thread

while(motorPID.isRunning())
{
//do something else
if(condition1)
motorPID.pause();
//pause the PID controller, preventing the integral from increasing
else if(condition2)
motorPID.stop();
}

我已经弄清楚如何计算标准 PID 算法,但我无法弄清楚如何提供异步功能。

谁能告诉我如何实现类似的 API?

最佳答案

您已经为 PIDController 实现了 run() 方法,因此您还应该实现 Runnable 接口(interface):

class PIDController implements Runnable {
....
}

现在您可以通过调用异步启动 PIDController:

pidControllerThread = new Thread( pidController );
pidControllerThread.start();

为了同步(如果需要),你应该看看太阳 concurrency guide .

关于java - 如何用Java实现多线程PID Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1620343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com