![](/img/trans.png)
[英]How do I implement a multi-threaded variable that I can safely increment using Java?
[英]How can I implement a multi-threaded PID controller in Java?
我对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
接口:
class PIDController implements Runnable {
....
}
现在,您可以通过调用以下命令启动PIDController异步:
pidControllerThread = new Thread( pidController );
pidControllerThread.start();
对于同步(如果需要),您应该查看sun 并发指南 。
到目前为止,将线程附加到任何对象的最佳机制是将完成工作的对象与线程对象分开。 Runnable接口可能很有吸引力,因为它允许人们将对象传递给Thread构造函数或Executor并运行它。 但是,如果您对对象的生命周期管理要求超出了“运行至完成”范围,例如暂停,那么您将发现在大多数情况下管理对象中的线程更为合适,这样您就可以知道哪个线程正在运行(是的,您可以在运行时将实例对象设置为Thread.currentThread(),但是...)。
因此,我认为您拥有一个好的开始。 您需要添加一些锁定功能,以帮助自己管理pause()和其他线程控制。
public class PIDController { private final Object myLock = new Object(); private final ControllerMethods ctrl; private volatile Thread thread; private volatile Runner runner; private int pidInterval = 700; private final double setPoint, kp, ki, kd; public PIDController( double setPoint, double kp, double ki, double kd, ControllerMethods inst ) { this.ctrl = inst; this.setPoint = setPoint; this.kp = kp; this.ki = ki; this.kd = kd; } public void pause() { synchronized( myLock ) { if( runner.paused ) { throw new IllegalOperationException(this+": already paused"); } runner.paused = true; } } public void resume() { synchronized( myLock ) { if( !runner.paused ) { throw new IllegalOperationException(this+": already resumed"); } runner.paused = false; } } public bool isRunning() { return running; } public void start() { if( thread != null ) { throw new IllegalOperationException( this+": already running"); } myThread = new Thread( runner = new Runner() ); myThread.start(); } public void stop() { if( runner == null ) { throw new IllegalOperationException( this+": PID is not running"); } runner.running = false; if( runner.paused ) resume(); runner = null; } // It is important, anytime that you implement a stoppable Runnable, that // you include the "running" flag as a member of an innner instance class like // this so that when you ask this instance to stop, you can immediately restart // another instance and not have the two threads observing the same "running" flag private class Runner implements Runnable { volatile bool running = false, bool paused; public void run() { running = true; while( running ) { // do this at the top of the loop so that a wake from // pause will check running, before recomputing. reComputePID(); // Use the double check idiom to if( paused ) { synchronized( myLock ) { while( paused ) { myLock.wait(); } } } Thread.sleep( pidInterval ); } } } public void reComputePID() { ... } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.