ggurov / hx711

raspberry pi hx711 weight scale interface
56 stars 33 forks source link

Why polling on DT_R? #15

Open Tosa95 opened 7 years ago

Tosa95 commented 7 years ago

I saw you poll on DT_R like following:

while( DT_R );

That's so bad for CPU usage.

You should at least insert a wait instruction inside the cycle in order to let the CPU free for other tasks.

But there's a better way: using WiringPi library you can use interrupts on data pins, so you can register a function for being called when the pin DT_R changes state with likely no CPU overhead.

I think i'll have to rewrite this library using WiringPi. That's a pity :(

Kriechi commented 6 years ago

@Tosa95 did you do a rewrite? Is so, please share a link if it's available online! Thanks!

ggurov commented 6 years ago

it's absolutely terrible for CPU, which is why i've abandoned this and went with i2c 16 bit ADC in differential mode that works amazingly well for load cells like this. there's one on adafruit that works many times better than hx711.

main issue is that hx711 was meant to be bitbanged from a controller that doesn't abstract GPIO pins with a kernel, and the timing was pretty critical.

there's not a rewrite of this, the 16 bit ADC works way better.

-gurov

On Mon, Feb 26, 2018 at 3:18 PM, Thomas Kriechbaumer < notifications@github.com> wrote:

@Tosa95 https://github.com/tosa95 did you do a rewrite? Is so, please share a link if it's available online! Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ggurov/hx711/issues/15#issuecomment-368633120, or mute the thread https://github.com/notifications/unsubscribe-auth/ACA6rQIcUvbyHBkEkil5Ys3srO1mrWl4ks5tYw84gaJpZM4NZkxe .

Tosa95 commented 6 years ago

Hi,

Yes I tried a different approach but it has some problems with slower versions of RPIs ( B1 and zero / zero W). My version uses a sort of interrupt that is provided by wiring pi but unfortunately it is not working well on single core RPIs. Since I had to use the Pi zero I abandoned the code, however it works pretty well on Pi 2 and 3.

The problem is probably due to the fact that the wiring pi interrupt is a low priority one and consequently OS interrupts gets executed before that one causing data loss.

The amount of loss tends to zero on Pi 2 and 3 but is predominant on other models. I also wrote a filter to throw away bad data so if you do not need to make a real time control application (as I had to do) it could work well. The impact on the CPU is near to 0.

I leave you the link of the repo: https://github.com/Tosa95/hx711

For my project I switched to Arduino connected to Raspberry at the end, since I couldn't admit any data loss.

Feel free to use the code as you want, to ask for clarifications and please let me know if you found it useful or if you could make it better.

(sorry for my bad english but I'm italian)

Regards, Tosatto Davide

On Feb 26, 2018 21:20, "Thomas Kriechbaumer" notifications@github.com wrote:

@Tosa95 https://github.com/tosa95 did you do a rewrite? Is so, please share a link if it's available online! Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ggurov/hx711/issues/15#issuecomment-368633120, or mute the thread https://github.com/notifications/unsubscribe-auth/AJlH1F4zGfTsYxwgFZbIcTVjmpO4YrkXks5tYw85gaJpZM4NZkxe .