Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality, code structure and performance improvements to ADS1256. #87

Merged
merged 15 commits into from
Dec 21, 2024

Conversation

cjritola
Copy link
Contributor

Why This Change*
My project requires speed reliability that the example didn't quite provide, but provided a good starting point to improve upon and after the modifications I decided to polish it up and do a fork/PR.

What has changed

  • VotingADS125x.java wrapper class was created to enable voting/median based error correction for measurements. It can be handy in critical systems where a bad reading can cause big problems.
  • ADS1256x.java interface created to facilitate wrapper classes like VotingADS125x.java and variants like ADS1255.java.
  • Most of ADS1256.java logic has been moved to AbstractADS125x.java. Then there are final class declarations for ADS1255.java and ADS1256.java in the package as well, such that they are GPIO and analog pin-count aware.
  • enums in ADS1256.java carry additional metadata and are moved to ADS1256x. Names were shortened to eliminate redundancy.
  • All constants from ADS1256_Declares.java were merged with ADS125x.java next to the enums. ADS1256_Decleares.java was then deleted. Some methods were made static and moved to ADS125x.java interface as well.
  • Busy loops in ADS1256.java were replaced with Thread.sleep(). Busy loops can be great on a microcontroller but can cause problems on a multithreaded kernel environment. These loops take away from other operations. Particularly, it was busy-looping on a property which was itself set by an event-based state-change dispatch system on another thread. If this were on a single-core system, the busy loop could run far longer than intended, as the event dispatcher would have to wait until the kernel performs a context change off the busy loop to dispatch the event necessary to free the busy loop itself. On top of this, the DigitalInput dispatch system is PRIORITY_NORMAL; alas it won't be expedited over a busy-wait thread.
  • WaitforDrdy busy loop replaced by the InputTransitionBarrier, which keeps track of pin toggles using thread wait()s and synchronization. It is also a debugging tool to catch unexpected toggles but the way it is used in AS125x.java is emphasis on conflict recovery.
  • On a raspberry pi 3, stable operation (>6hrs) at 50SPS constant polling on all pins using input-dump mode on ADS1256App. It will take higher SPS values but they haven't been thoroughly tested and it's not clear it is even keeping up with that sampling rate in the JVM. A bottleneck is latency of the DigitalInput event dispatch when waiting for DRDY.
  • Added GPIOChaser.java to cycle the GPIO outputs on and off for more testing.
  • In AbstractADS125x.java moved console debug print behind if() conditional which itself is controlled by a the public static boolean DEBUG variable such that it is compiled out if DEBUG=false. This is because even with the logger silent, copious numbers of strings are being assembled and discarded with the various pin toggles and register writes and caused significant CPU overhead when the chip is pushed to high poll rates.
  • Big update to ADS1256 README.md with additional features documented.
  • Added read/write helper methods and replaced a few existing ones in AbstractADS125x.java
  • Added gpio chase, data (sample) rate, enable input buffer, dump input states, dump repeatedly, use 3-way voting/median, to ADS1256App.
  • Changed default values for various pins from 42 (wouldn't work since they were all set to that) to the pins on the Waveshare High Precision AD/DA board in ADS1256App.java.
  • Fleshed out javadoc comments in general.
  • Shortened sleeps in AbstractADS125x.java.

Known issues

  • AD1256App sometimes exits with chip ID = 0. Retrying usually resolves.
  • Setting the PGA to gains other than 1 results in voltage readings that scale nonlinearly when turning the knob on the Waveshare High Precision AD/DA board. May have something to do with input impedance interaction with the potentiometer.

@taartspi taartspi merged commit 6e66a5e into Pi4J:master Dec 21, 2024
1 check passed
@taartspi
Copy link
Collaborator

Thanks very much for moving your code improvements into the repo. Tom

@taartspi
Copy link
Collaborator

And a further comment. Your explanation of your design points provides education

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants