计步器算法使用加速度计数据正确计数步骤不正确

发布于 2025-02-06 12:48:45 字数 2084 浏览 2 评论 0原文

我正在使用React-Native,我想计算用户的步骤,例如三星健康步骤指示器。

我正在使用 react-native-sensensors 库来访问加速度计数据。

我跟随该教程用于计步器算法,在反应本中实现。

import {View, Text} from 'react-native';
import {
  accelerometer,
  SensorTypes,
  setUpdateIntervalForType,
} from 'react-native-sensors';

setUpdateIntervalForType(SensorTypes.accelerometer, 400);

const App = () => {
  const [xAcceleration, setXAcceleration] = useState(0);
  const [yAcceleration, setYAcceleration] = useState(0);
  const [zAcceleration, setZAcceleration] = useState(0);
  const [magnitudePrevious, setMagnitudePrevious] = useState(0);
  const [steps, setSteps] = useState(0);

  useEffect(() => {
    const subscription = accelerometer
      .pipe(data => data)
      .subscribe(speed => {
        setXAcceleration(speed.x);
        setYAcceleration(speed.y);
        setZAcceleration(speed.z);
      });

    return () => {
      subscription.unsubscribe();
    };
  }, []);

  useEffect(() => {
    const magnitude = Math.sqrt(
      Math.pow(xAcceleration, 2) +
        Math.pow(yAcceleration, 2) +
        Math.pow(zAcceleration, 2),
    );

    const magnitudeDelta = magnitude - magnitudePrevious;

    setMagnitudePrevious(() => magnitude);

    // I tried magnitudeDelta > 6, magnitudeDelta > 4, 
    // magnitudeDelta > 2, magnitudeDelta > 10 but didn't work properly
    if (magnitudeDelta > 2) setSteps(prevSteps => prevSteps + 1);
  }, [xAcceleration, yAcceleration, zAcceleration]);

  return (
    <View>
      <Text>{steps}</Text>
    </View>
  )
}

如果向上或侧面摇动我的手机,它会增加步骤,但我认为可以,因为三星Health在Samsung Health中会发生齐射。但是主要的问题是走路时它的准确性不如三星健康。

例如:如果一个踩20次,则仅计算其中8个。我希望它接近实际价值。

I am using react-native and I want to count the steps of the user like Samsung Health step indicator.

I am using react-native-sensors library to access accelerometer data.

I followed this tutorial for pedometer algorithm, implemented in react-native.

import {View, Text} from 'react-native';
import {
  accelerometer,
  SensorTypes,
  setUpdateIntervalForType,
} from 'react-native-sensors';

setUpdateIntervalForType(SensorTypes.accelerometer, 400);

const App = () => {
  const [xAcceleration, setXAcceleration] = useState(0);
  const [yAcceleration, setYAcceleration] = useState(0);
  const [zAcceleration, setZAcceleration] = useState(0);
  const [magnitudePrevious, setMagnitudePrevious] = useState(0);
  const [steps, setSteps] = useState(0);

  useEffect(() => {
    const subscription = accelerometer
      .pipe(data => data)
      .subscribe(speed => {
        setXAcceleration(speed.x);
        setYAcceleration(speed.y);
        setZAcceleration(speed.z);
      });

    return () => {
      subscription.unsubscribe();
    };
  }, []);

  useEffect(() => {
    const magnitude = Math.sqrt(
      Math.pow(xAcceleration, 2) +
        Math.pow(yAcceleration, 2) +
        Math.pow(zAcceleration, 2),
    );

    const magnitudeDelta = magnitude - magnitudePrevious;

    setMagnitudePrevious(() => magnitude);

    // I tried magnitudeDelta > 6, magnitudeDelta > 4, 
    // magnitudeDelta > 2, magnitudeDelta > 10 but didn't work properly
    if (magnitudeDelta > 2) setSteps(prevSteps => prevSteps + 1);
  }, [xAcceleration, yAcceleration, zAcceleration]);

  return (
    <View>
      <Text>{steps}</Text>
    </View>
  )
}

If a shake my phone upwards or sideways it increments the steps but I think it's ok because samething happens in Samsung Health. But the main problem is it is not as accurate as Samsung Health when you walk.

For example: If a stepped 20 times it only counts 8 of them. I want it to be close to actual value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文