Java Pi4j风速计-每间隔计数器
我正试图用一个数字风速仪来测量我的树莓皮和pi4j 我的想法是添加Java Pi4j风速计-每间隔计数器,java,raspberry-pi,pi4j,Java,Raspberry Pi,Pi4j,我正试图用一个数字风速仪来测量我的树莓皮和pi4j 我的想法是添加GpioPinListenerDigital,当一个管脚变高(意味着一个完整的风速计旋转)时进行监测,但我无法让它工作。。。我想设置一个间隔来观察中断,但没有成功 这是我的主课 public class Main { public static void main(String[] args) throws InterruptedException { Anemometer anemometer; int r
GpioPinListenerDigital
,当一个管脚变高(意味着一个完整的风速计旋转)时进行监测,但我无法让它工作。。。我想设置一个间隔来观察中断,但没有成功
这是我的主课
public class Main {
public static void main(String[] args) throws InterruptedException {
Anemometer anemometer;
int rotations = 0;
System.out.println("Start");
while(true){
rotations = Anemometer.countPulse();
System.out.println("Rotations "+ rotations);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
它简单地称为风速计,即
public class Anemometer {
final static int short_interval = 3;
static int final_counter = 0;
public static int countPulse() {
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN);
input.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
long start = System.currentTimeMillis();
long end = 0L;
int counter = 0;
while (end < short_interval * 1000) {
System.out.println("Inizio while");
if (event.getState().isHigh()) {
System.out.println("Pin state: " + event.getState());
counter++;
}
end = (new Date()).getTime() - start;
}
final_counter = counter;
System.out.println("final counter: "+final_counter);
}
});
gpio.unprovisionPin(input);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return final_counter;
}
}
公共级风速计{
最终静态int短_间隔=3;
静态int final_计数器=0;
公共静态int countPulse(){
GpioController gpio=GpioFactory.getInstance();
GPIOPIIndigitalInput输入=gpio.provisionDigitalInputPin(RaspiPin.gpio_01,PinPullResistance.PULL_DOWN);
addListener(新的GpioPinListenerDigital(){
@凌驾
public void handlePiopIndicatalStateChangeEvent(GPIOPiIndigitalStateChangeEvent){
长启动=System.currentTimeMillis();
长端=0升;
int计数器=0;
同时(结束<短间隔*1000){
System.out.println(“Inalizio while”);
if(event.getState().isHigh()){
System.out.println(“Pin状态:+event.getState());
计数器++;
}
end=(新日期()).getTime()-start;
}
最终计数器=计数器;
System.out.println(“最终计数器:+最终计数器”);
}
});
gpio.unprovisionPin(输入);
试一试{
睡眠(200);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回最终计数器;
}
}
看起来它从来没有进入while循环。。。有什么想法吗?我不熟悉这个API,但很明显,您没有给侦听器足够的时间来解雇它。以下是代码:
public void unposisionpin(GpioPin…pin){
if(pin==null | | pin.length==0){
抛出新的IllegalArgumentException(“缺少pin参数”);
}
对于(int index=(pin.length-1);index>=0;index--){
GpioPin p=针[指数];
//确保已设置请求的pin
如果(!pins.contains(p)){
抛出新的GpioPinNotProvisionedException(p.getPin());
}
//删除所有侦听器和触发器
if(GpioPinInput的p实例){
((GpioPinInput)p).removeAllListeners();
((GpioPinInput)p).移除所有触发器();
}
//从托管集合中删除此pin实例
销。移除(p);
}
}
注意removeAllListeners
调用。因此,本质上,您添加了侦听器,然后在代码中立即删除它。尝试调用gpio.unprovisionPin(输入)代码>等待200毫秒后
试试看{
睡眠(200);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
gpio.unprovisionPin(输入);
我不能100%确定到底是什么问题,但我假设间隔设置错误。在进入循环之前设置“start”变量。你也面临同样的问题吗?是的,我改变了,但什么都没发生。。。我将编辑这些问题,谢谢