检测加速度计的运动

时间:2018-03-01 19:26:47

标签: c 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);
    }
}

2 个答案:

答案 0 :(得分:0)

如果加速度的大小没有改变,则设备无论如何都可以移动,因此您需要存储所有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);
    }
}

答案 1 :(得分:0)

我最终通过添加一个阈值来解决它:

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);
    }
相关问题