我有一个Raspberry PI,我使用这个传感器MPU-9150来捕捉动作。
我使用本指南https://blogs.oracle.com/acaicedo/entry/beyond_beauty_javafx_i2c_parallax将传感器连接到PI,在接线后我根据指南中显示的类编写了一个类。
现在的问题是,考虑下面写的方法,运行到一个单独的线程中读取传感器数据。
正如你所看到的,这个方法使用一个循环while(true){}迭代读取数据的流...我认为这种方法会杀死CPU的资源,所以更好的方法是做到这一点?
我希望我能够最好地解释这个问题,所以提前感谢并祝你有个美好的一天。
private void readingSensorsV2() throws IOException {
int gyroX;
int gyroY;
int gyroZ;
int gyroX_last = 0;
int gyroY_last = 0;
int gyroZ_last = 0;
while (true) {
gyroData = new byte[6];
// You can read one registry at a time,
// or you can read multiple consecutive ones,
// in our case we are reading 6 consecutive registries
// from 0x43, meaning we are reading all the
// gyroscope measurements.
int r = device.read(0x43, gyroData, 0, 6);
if (r != 6) {
System.out.println("Error reading gyro data, < 6 bytes");
}
// Convert the values to integers, using the
// helper method asInt
gyroX = (asInt(gyroData[0]) * 256 + asInt(gyroData[1]));
gyroY = (asInt(gyroData[2]) * 256 + asInt(gyroData[3]));
gyroZ = (asInt(gyroData[4]) * 256 + asInt(gyroData[5]));
if( accuracy<Math.abs( gyroX_last-gyroX ) || accuracy<Math.abs( gyroY_last-gyroY ) || accuracy<Math.abs( gyroZ_last-gyroZ ) ){
for( GyroMovDet gyroListner : gyroListners ){
gyroListner.onMove( gyroX, gyroY, gyroZ);
}
}
gyroX_last = gyroX;
gyroY_last = gyroY;
gyroZ_last = gyroZ;
}
}