elixir-circuits / circuits_gpio

Use GPIOs from Elixir
129 stars 23 forks source link

strange timing results from gpio #6

Closed tcoopman closed 5 years ago

tcoopman commented 6 years ago

I'm going to do my best to include the necessary information here after a discussion with @fhunleth on slack.

So I have a setup with an ultrasonic range sensor (a HC-SR04). Before I've used elixir_ale to measure the time with this kind of code:

  defp read(echo_pin) do
    case @gpio.read(echo_pin) do
      0 -> read(echo_pin)
      1 -> :timer.tc(UltraSonic, :time_result, [echo_pin])
    end
  end

  def time_result(echo_pin) do
    case @gpio.read(echo_pin) do
      1 -> time_result(echo_pin)
      0 -> :ok
    end
  end

This works, but was not very accurate. Now I've also tried the elixir_ale 2.0-fah branch a couple of weeks ago and the accuracy was much improved. So now I tried to use the gpio code and I've noticed very strange things:

  1. for every short distances (17cm or less) the measurement is farely ok.
  2. but as soon as I go over that distance, the measurement is not accurate at all and the measurements iterate between about 70cm and 1200cm:

nothing close to the sensor:

00:01:22.787 [info]  received {:distance, 1197.46445} in state off - doing nothing
00:01:23.863 [info]  received {:distance, 72.59595} in state off - doing nothing
00:01:24.944 [info]  received {:distance, 1335.69345} in state off - doing nothing
00:01:26.028 [info]  received {:distance, 72.59595} in state off - doing nothing
00:01:27.109 [info]  received {:distance, 1335.642} in state off - doing nothing
00:01:28.185 [info]  received {:distance, 72.6474} in state off - doing nothing
00:01:29.267 [info]  received {:distance, 1336.03645} in state off - doing nothing

hand close to the sensor

00:01:43.238 [info]  received {:distance, 72.59595} in state off - doing nothing
# moving hand in front
00:01:44.311 [info]  received {:distance, 1195.5265} in state off - doing nothing
00:01:45.384 [info]  received {:distance, 9.0895} in state off - doing nothing
00:01:46.457 [info]  received {:distance, 8.0262} in state off - doing nothing
00:01:47.530 [info]  received {:distance, 7.80325} in state off - doing nothing
00:01:48.603 [info]  received {:distance, 7.90615} in state off - doing nothing
00:01:49.676 [info]  received {:distance, 12.36515} in state off - doing nothing
# moving hand away
00:01:50.752 [info]  received {:distance, 72.3044} in state off - doing nothing
00:01:51.825 [info]  received {:distance, 1198.442} in state off - doing nothing
00:01:52.901 [info]  received {:distance, 72.59595} in state off - doing nothing

Some observations:

Resources:

Hope this helps to fix this.

fhunleth commented 5 years ago

Closing since I suspect that this may have been resolved a while back, but we were never sure enough that the timing was good enough to close this. I'm getting about 20 uS accuracy on a Raspberry Pi Zero. This isn't great, but probably the best we can do until the gpio char interface code gets integrated.