termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.22k stars 443 forks source link

Duplicate Sensor Values with SensorAPI #289

Open Muny opened 5 years ago

Muny commented 5 years ago

Problem description When subscribing to continuous sensor updates, SensorOutputWriter sleeps for a fixed amount of time, then sends out the most recent sensor data. This sensor data may be exactly the same as was sent out previously.

Steps to reproduce

$ termux-sensor -s Pressure -d 10
{
  "BMP280 Pressure": {
    "values": [
      1004.2263793945312,
      0,
      0
    ]
  }
}
{
  "BMP280 Pressure": {
    "values": [
      1004.2263793945312,
      0,
      0
    ]
  }
}
{
  "BMP280 Pressure": {
    "values": [
      1004.2263793945312,
      0,
      0
    ]
  }
}
{
  "BMP280 Pressure": {
    "values": [
      1004.230224609375,
      0,
      0
    ]
  }
}
{
  "BMP280 Pressure": {
    "values": [
      1004.230224609375,
      0,
      0
    ]
  }
}
{
  "BMP280 Pressure": {
    "values": [
      1004.230224609375,
      0,
      0
    ]
  }
}

Notice the duplicate values.

Expected behavior No duplicate values.

I'm not sure what the intention was with the -d flag.

Android allows you to configure a sensor's update rate, and it notifies you when a sensor's value changes.

I think SensorAPI should:

I think this would be a much more useful behavior. I'm currently trying to automate logging of sensor values, and I want to squeeze the maximum performance out of my device's sensors. However, the updates termux-sensors produces are not in-sync with the actual sensor updates.

Additional information

$ termux-info
Updatable packages:
All packages up to date
Subscribed repositories:
https://dl.bintray.com/xeffyr/x11-packages-24 x11/main
https://dl.bintray.com/xeffyr/unstable-packages-24 unstable/main
https://dl.bintray.com/grimler/science-packages-24 science/stable
https://dl.bintray.com/grimler/termux-root-packages-24 root/stable
https://dl.bintray.com/grimler/game-packages-24 games/stable
https://dl.bintray.com/termux/termux-packages-24 stable/main
System information:
Linux localhost 3.18.31-10376601-eng #1 SMP PREEMPT Thu Feb 2 19:51:12 KST 2017 aarch64 Android
Termux-packages arch:
aarch64
Android version:
7.0
Device manufacturer:
samsung
Device model:
SM-G930V
ghost commented 5 years ago

All sensors have their own minimal supported update rate. The option -d 10 means to update sensor value each 10 milliseconds which is really fast and may not be supported.

Try to increase value to 50 or even 100.

ghost commented 5 years ago

Termux:API execution takes longer time, so 10 ms delay is pointless.

Muny commented 5 years ago

I understand this, but the problem I'm having is that the sensor updates are not synchronized with the timer that runs the SensorOutputWriter.

Even if my sensor updates at 10Hz, and I set my delay to 100ms, I can get duplicate values because of how the values are being sent.

And for the execution time, when doing continuous updates the command is only executed once. I am getting what seem to be 10ms updates with -d 10.