- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我正在开发一个程序,在我进入我的房间时打开或关闭我的 hue 灯。在我的房间门口安装了 2 个 HCSr04,用于检测来人的方向。该程序大部分工作正常,但在短时间后它只是卡住并且没有任何反应。它在带有 Raspbian 的 Rapsberry Pi 3 上运行。
代码如下:
public class Distance {
//GPIO Pins
private static GpioPinDigitalOutput sensorTriggerPin;
private static GpioPinDigitalInput sensorEchoPin;
private static GpioPinDigitalOutput sensorTriggerPin1;
private static GpioPinDigitalInput sensorEchoPin1;
int f = 0;
ZoneId id;
public static double distance2;
public static double distance3;
public static boolean left = false;
public static boolean right = false;
public static long timeleft;
public static long timeright;
public int counter;
final static GpioController gpio = GpioFactory.getInstance();
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting...");
ZoneId id = ZoneId.of("Europe/Berlin");
int hour = 22; //LocalDateTime.now(id).getHour();
if (hour > 18 || hour < 7) {
} else {
System.out.println("Hour not in estimated working time");
System.out.println("Hour:" + hour);
}
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00); // Trigger pin as OUTPUT // rechts von Schrank aus
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN); // Echo pin as INPUT
sensorTriggerPin1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_27); // links von Schrank aus
sensorEchoPin1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_25, PinPullResistance.PULL_DOWN);
new UltraHue();
UltraHue.hue();
}
public void run() throws InterruptedException {
do {
Thread.sleep(20);
Double Distance = getdistance(sensorEchoPin, sensorTriggerPin);
//int hour =22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (Distance < 70 && distance2 < 70) { //rechts vom Schrank
if (right == false) {
right = true;
}
if (right == true && left == true) {
UltraHue.lightson(PHHueSDK.getInstance().getSelectedBridge());
right = false;
left = false;
counter = 0;
}
}
distance2 = Distance;
Thread.sleep(30);
double Distance1 = getdistance(sensorEchoPin1, sensorTriggerPin1);
int hour1 = 22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (Distance1 < 70 && distance3 < 70) { //links vom Schrank
if (left == false) {
left = true;
}
if (right == true && left == true) {
UltraHue.lightsoff(PHHueSDK.getInstance().getSelectedBridge());
right = false;
left = false;
counter = 0;
}
}
distance3 = Distance1;
if (left == true || right == true) {
counter = counter + 1;
System.out.println(counter);
if (counter == 70) {
System.out.println("resetting counter");
left = false;
right = false;
counter = 0;
}
} else {
counter = 0;
}
} while (true);
}
public static double getdistance(GpioPinDigitalInput sensorEchoPin3, GpioPinDigitalOutput sensorTriggerPin3) throws InterruptedException {
sensorTriggerPin3.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01); // Delay for 10 microseconds
sensorTriggerPin3.low(); //Make trigger pin LOW
while (sensorEchoPin3.isLow()) { //Wait until the ECHO pin gets HIGH
}
long startTime = System.nanoTime(); // Store the current time to calculate ECHO pin HIGH time.
while (sensorEchoPin3.isHigh()) { //Wait until the ECHO pin gets LOW
}
long endTime = System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
double distance = (((endTime - startTime) / 1e3) / 2) / 29.1;
return distance;
}
}
最佳答案
我认为你的代码可以简化很多。例如。为什么要使用 distance1 和 distance3 之类的变量,我不清楚它们的用途。我已将您的代码重写为更简单易读的版本。尝试运行代码,如果失败,用 try catch block 将其包围并检查堆栈跟踪。小心 while 循环,例如
`while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH
}
while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW
}`
您确定引脚最终会返回低电平或高电平状态吗?也尽量不要在线程运行方法内创建变量,在循环外创建并引用它们。它们的内存很重。
public void run() throws InterruptedException {
while(true){
sleep(20);
//int hour =22; //LocalDateTime.now(id).getHour();
// && hour>18 || hour<7
if (getdistance(sensorEchoPin, sensorTriggerPin) < 70) { //rechts vom Schrank
right = true;
if (left) {
UltraHue.lightson(PHHueSDK.getInstance().getSelectedBridge());
reset();
}
}
sleep(30);
if (getdistance(sensorEchoPin1, sensorTriggerPin1) < 70) { //links vom Schrank
left = true;
if (right) {
UltraHue.lightsoff(PHHueSDK.getInstance().getSelectedBridge());
reset();
}
}
if (left || right) {
//System.out.println(counter);
if (counter++ >= 70) {
//System.out.println("resetting counter");
reset();
}
}
}
}
private void reset(){
counter = 0;
left = false;
right = false;
}
private static long endTime;
private static long startTime;
public static double getdistance(GpioPinDigitalInput sensorEchoPin3, GpioPinDigitalOutput sensorTriggerPin3) throws InterruptedException{
sensorTriggerPin3.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01);// Delay for 10 microseconds
sensorTriggerPin3.low(); //Make trigger pin LOW
while(sensorEchoPin3.isLow()){ //Wait until the ECHO pin gets HIGH
}
startTime= System.nanoTime(); // Store the current time to calculate ECHO pin HIGH time.
while(sensorEchoPin3.isHigh()){ //Wait until the ECHO pin gets LOW
}
endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
return(((endTime-startTime)/1e3)/2) / 29.1;
}
关于Java 程序在 raspberry pi 上短时间后无错误地卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910926/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在使用带有Grove Pi +(1.2.2固件)的Raspberry Pi 3 B模型和用于Robots Image的Raspbian。 我在I2C-1端口中插入了多 channel 气体传感器,
这看起来非常简单,但我似乎无法弄清楚如何将 -Pi 和 Pi 之间的角度映射到 0 到 2Pi 的范围内。我尝试使用 np.select 但由于某种原因它卡住了我的程序。我需要这个范围内的角度,因为它
在使用 SciPy 和 NumPy 的项目中,我应该使用 scipy.pi , numpy.pi , 或 math.pi ? 最佳答案 >>> import math >>> import numpy
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我有一个运行 Raspbian 的 Raspberry Pi 1。我尝试在 Raspberry Pi 3 上运行 SD 卡,但它没有启动。 我已经阅读了有关升级 Raspberry Pi 2 安装以在
#include using namespace std; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #d
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我目前正在尝试RadiusNetworks发布的Raspberry Pi iBeacon教程,网址为 http://developer.radiusnetworks.com/2013/10/09/ho
我无法在运行Raspbian的Raspberry Pi 3上安装我创建的 Electron 应用程序。我已经使用了electronic-packager来创建软件包,然后创建了一个debian安装程序
我想在Linux上为Raspberry Pi 1设置交叉编译环境。 特别是我想尝试最新版本,即Raspbian测试+ Qt5开发分支。 这个问题: How can I create a modern
我想要从我的 Raspberry Pi Zero 到手机的低延迟流式传输。据我了解,移动浏览器不支持 RTMP 流式传输,HLS 流式传输具有高延迟,而 webRTC 是我最好的选择。 有谁知道从零开
我的公司使用 Raspberry Pi 3 作为产品中的嵌入式 Controller 。用户不会优雅地关闭它,他们只是扳动一个开关。为避免损坏,/boot 和/root 文件系统是只读的。这似乎是防弹
如何使用 Raspberry Pi 作为 b/w USB Tethered 手机和路由器的桥接器,使用“以太网电缆 b/w Raspberry Pi 和路由器”和“USB 电缆 b/w 手机和 Ras
我正在尝试在Raspberry Pi 3上安装Rakudo Star 2018.04。 我做: sudo perl Configure.pl --gen-moar --gen-nqp --prefix
我正在寻找一些可以有效完成的不错的 C 代码: while (deltaPhase >= M_PI) deltaPhase -= M_TWOPI; while (deltaPhase T Mod(T
我正在尝试为 raspberry Pi 构建跨环境以在 Eclipse CDT for windows 上构建二进制文件。 我得到了用于访问 GPIO 的 Wiring Pi,我需要使用“Window
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我正在寻找一些可以有效完成的不错的 C 代码: while (deltaPhase >= M_PI) deltaPhase -= M_TWOPI; while (deltaPhase T Mod(T
这个问题在这里已经有了答案: C: How to wrap a float to the interval [-pi, pi) (15 个答案) 关闭 9 年前。 我想知道是否可以定义一个只能取 -
我是一名优秀的程序员,十分优秀!