从加速度计检测运动
Detect movement from accelerometer
我有一个Micro:Bit。它有一个加速度计,所以我可以测量 x、y、z 轴上的加速度。
想法是将它戴在手臂上,并在检测到手臂上有动作时通过蓝牙发送。
所以,我想检查加速度并在它超过某种阈值时生成一个事件,但我不知道该怎么做。
这将是这样的:
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i",x,y,z);
uart->send(ManagedString(buffer));
}
int main() {
while (1) {
x = uBit.accelerometer.getX();
y = uBit.accelerometer.getY();
z = uBit.accelerometer.getZ();
// Check if device is getting moved
if (accel > 1) onAwake(x,y,z); // Some kind of threshold
sleep(200);
}
}
如果加速度的大小没有改变,设备可能仍然在移动,因此您需要存储所有 3 个值并进行比较。
像这样
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i", x, y, z);
uart->send(ManagedString(buffer));
}
int main() {
int x;
int y;
int z;
while (1) {
int nx = uBit.accelerometer.getX();
int ny = uBit.accelerometer.getY();
int nz = uBit.accelerometer.getZ();
// Check if device is getting moved
if ((x != nx) || (y != ny) || (z != nz))
onAwake(x, y, z); // Some kind of threshold
sleep(200);
}
}
我终于通过添加阈值解决了它:
MicroBit uBit;
char buffer[20];
int x, nx, y, ny, z, nz;
int threshold = 100;
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i", x, y, z);
uart->send(ManagedString(buffer));
}
int main() {
uBit.init();
x = uBit.accelerometer.getX();
y = uBit.accelerometer.getY();
z = uBit.accelerometer.getZ();
while (1) {
nx = uBit.accelerometer.getX();
ny = uBit.accelerometer.getY();
nz = uBit.accelerometer.getZ();
if ((x != nx && abs(x-nx)>threshold) || (y != ny && abs(y-ny)>threshold) || (z != nz && abs(z-nz)>threshold)) {
onAwake(x,y,z);
}
x = nx; y = ny; z = nz;
sleep(200);
}
我有一个Micro:Bit。它有一个加速度计,所以我可以测量 x、y、z 轴上的加速度。
想法是将它戴在手臂上,并在检测到手臂上有动作时通过蓝牙发送。
所以,我想检查加速度并在它超过某种阈值时生成一个事件,但我不知道该怎么做。
这将是这样的:
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i",x,y,z);
uart->send(ManagedString(buffer));
}
int main() {
while (1) {
x = uBit.accelerometer.getX();
y = uBit.accelerometer.getY();
z = uBit.accelerometer.getZ();
// Check if device is getting moved
if (accel > 1) onAwake(x,y,z); // Some kind of threshold
sleep(200);
}
}
如果加速度的大小没有改变,设备可能仍然在移动,因此您需要存储所有 3 个值并进行比较。
像这样
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i", x, y, z);
uart->send(ManagedString(buffer));
}
int main() {
int x;
int y;
int z;
while (1) {
int nx = uBit.accelerometer.getX();
int ny = uBit.accelerometer.getY();
int nz = uBit.accelerometer.getZ();
// Check if device is getting moved
if ((x != nx) || (y != ny) || (z != nz))
onAwake(x, y, z); // Some kind of threshold
sleep(200);
}
}
我终于通过添加阈值解决了它:
MicroBit uBit;
char buffer[20];
int x, nx, y, ny, z, nz;
int threshold = 100;
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i", x, y, z);
uart->send(ManagedString(buffer));
}
int main() {
uBit.init();
x = uBit.accelerometer.getX();
y = uBit.accelerometer.getY();
z = uBit.accelerometer.getZ();
while (1) {
nx = uBit.accelerometer.getX();
ny = uBit.accelerometer.getY();
nz = uBit.accelerometer.getZ();
if ((x != nx && abs(x-nx)>threshold) || (y != ny && abs(y-ny)>threshold) || (z != nz && abs(z-nz)>threshold)) {
onAwake(x,y,z);
}
x = nx; y = ny; z = nz;
sleep(200);
}