Coder Social home page Coder Social logo

hx711's People

Contributors

endail avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hx711's Issues

Scale functionality should be separated

This repo should focus solely on communicating with a HX711. Functionality built on top, such as a scale for measuring weight, is out of scope and should be removed/moved into its own repo.

Calibration Exception

Try run the HX711 Calibration and occurs the exception

========================================
HX711 Calibration

Find an object you know the weight of. If you can't find anything, try searching Google for your phone's specifications to find its weight. You can then use your phone to calibrate your scale.

  1. Enter the unit you want to measure the object in (eg. g, kg, lb, oz): kg

  2. Enter the weight of the object in the unit you chose (eg. if you chose 'g', enter the weight of the object in grams): 1

  3. Enter the number of samples to take from the HX711 module (eg. 15): 30

  4. Remove all objects from the scale and then press enter.

Working...terminate called after throwing an instance of 'HX711::TimeoutException'
what(): timed out while trying to read bytes from HX711
Aborted

Investigate whether a HX711 can be detected

Reading from an unconnected GPIO could lead to the process hanging (or another other unpredictable result). It may be of some use to have a method of the HX711 class to "test" whether a HX711 could be detected by using a simple read which times-out after some predefined time. Conversely, it may even be better to implement a configurable timeout within the isReady and readValue methods.

std::chrono::high_resolution_clock use is ambiguous

https://en.cppreference.com/w/cpp/chrono/high_resolution_clock

The high_resolution_clock is not implemented consistently across different standard library implementations, and its use should be avoided. It is often just an alias for std::chrono::steady_clock or std::chrono::system_clock, but which one it is depends on the library or configuration. When it is a system_clock, it is not monotonic (e.g., the time can go backwards). For example, for gcc's libstdc++ it is system_clock, for MSVC it is steady_clock, and for clang's libc++ it depends on configuration.

Generally one should just use std::chrono::steady_clock or std::chrono::system_clock directly instead of std::chrono::high_resolution_clock: use steady_clock for duration measurements, and system_clock for wall-clock time.

https://github.com/endail/hx711/search?q=high_resolution_clock shows it being used in contexts where a steady clock (ie. std::chrono::steady_clock) should explicitly be used.

Rename repo

For the sake of consistency and to avoid confusion with Pico/RP2040 HX711 repos, rename this repo to hx711-rpi-cxx.

Note the breaking autobuild notification changes to hx711-rpi-py.

HX711::powerUp should not block to sleep for settling time

hx711/src/HX711.cpp

Lines 367 to 387 in 42d6305

void HX711::powerUp() {
std::lock_guard<std::mutex> lock(this->_commLock);
/**
* "When PD_SCK returns to low,
* chip will reset and enter normal operation mode"
* Datasheet pg. 5
*/
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::LOW);
/**
* "Settling time refers to the time from power up, reset,
* input channel change and gain change to valid stable output data."
* Datasheet pg. 3
*/
if(this->_rate != Rate::OTHER) {
Utility::sleep(_SETTLING_TIMES.at(this->_rate));
}
}

Sleeping for settling time should be separate to powering up.

HX711::powerDown should not block by sleeping for timeout duration

hx711/src/HX711.cpp

Lines 344 to 365 in 42d6305

void HX711::powerDown() {
std::lock_guard<std::mutex> lock(this->_commLock);
/**
* The delay between low to high is probably not necessary, but it
* should help to keep the underlying code from optimising it away -
* if does at all.
*/
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::LOW);
Utility::delay(std::chrono::microseconds(1));
Utility::writeGpio(this->_gpioHandle, this->_clockPin, GpioLevel::HIGH);
/**
* "When PD_SCK pin changes from low to high
* and stays at high for longer than 60µs, HX711
* enters power down mode (Fig.3)."
* Datasheet pg. 5
*/
Utility::sleep(_POWER_DOWN_TIMEOUT);
}

Timeout should be separate from powering down to allow other/concurrent code to run.

Investigate adapting code to interface with multiple HX711 ICs

Considerations:

  • A HX711Collection class?
  • While the current code for interfacing a single HX711 pulses the clock pin then reads the data pin, a collection of HX711s may not function properly if this is applied to a collection. Too much time may elapse and cause an individual - or all - HX711 chips to power down.
  • A collection will need to pulse the clock pin of each HX711 simultaneously. This may not be an issue if each is connected to the same GPIO pin. But what if multiple HX711's clock pins are connected to different GPIO pins? And are there any other ramifications of using a single GPIO pin connected to multiple HX711 clock pins?
  • What should be returned from reading a collection of HX711s? An std::vector< HX711::Value >? Perhaps a new HX711::ValueCollection class?
  • lg may also provide some useful group GPIO functions in this context: lgGroupClaimInput, lgGroupClaimOutput, and lgGroupFree.
  • How will current implementation be adapted? e.g an overriden HX711::read? etc...

Investigate use of lgTxPulse for pulsing clock pin

Currently, the clock pin is pulsed with two GPIO write calls through the lg library:

https://github.com/endail/hx711/blob/master/src/HX711.cpp#L88
https://github.com/endail/hx711/blob/master/src/HX711.cpp#L97

Both call lgGpioWrite through the Utility class. But lg also has a lgTxPulse function.

I am wondering if using lgTxPulse to provide a single pulse to the HX711's clock pin may be more efficient than the two lgGpioWrite calls. If it is, it may also help to avoid the 60us power down issue.

Remove HX711::Format

HX711 has a predefined most significant bit format. HX711::Format is therefore redundant.

Replace with a virtual waitReady method

hx711/src/HX711.cpp

Lines 257 to 265 in de87c5b

/**
* A read must take place to set the gain at the
* hardware level. See datasheet pg. 4 "Serial
* Interface".
*
* TODO: this is an inefficient busy-wait. Solutions?
*/
while(!this->isReady());
this->readValue();

The waitReady method should busy-wait for a predefined maximum amount of time. If the amount of time is exceeded, a TimeoutException should be thrown. The try-catch in setConfig can then handle accordingly.

Inheriting classes like AdvancedHX711 can then override with something else as needed.

This will also handle the case where DOUT is never "ready" for some unforeseen reason.

high_resolution_clock should be changed to steady_clock

hx711/src/HX711.cpp

Lines 310 to 328 in 42d6305

bool HX711::waitReady(const std::chrono::nanoseconds timeout) const {
using namespace std::chrono;
const auto maxEnd = high_resolution_clock::now();
while(true) {
if(this->isReady()) {
return true;
}
if(high_resolution_clock::now() >= maxEnd) {
return false;
}
}
}

The high_resolution_clock is not implemented consistently across different standard library implementations, and its use should be avoided. It is often just an alias for std::chrono::steady_clock or std::chrono::system_clock, but which one it is depends on the library or configuration. When it is a system_clock, it is not monotonic (e.g., the time can go backwards). For example, for gcc's libstdc++ it is system_clock, for MSVC it is steady_clock, and for clang's libc++ it depends on configuration.

Generally one should just use std::chrono::steady_clock or std::chrono::system_clock directly instead of std::chrono::high_resolution_clock: use steady_clock for duration measurements, and system_clock for wall-clock time.

https://en.cppreference.com/w/cpp/chrono/high_resolution_clock

Generic HX711 breakout board should be noted

hx711/README.md

Line 139 in b1cb63a

### Wiring and Pins

eg. https://www.ebay.com/sch/i.html?_nkw=hx711

Note:

  • (typically) green board, as opposed to Sparkfun's red board
  • not all HX711 pins are broken-out on the generic board compared to the Sparkfun version
  • there is no jumper on the rear of the generic board (or anywhere) for changing/selecting the sample rate
  • changing the sample rate on the generic board requires delicate modification of the hardware, specifically the HX711 IC

Typedef for raw values

static std::int32_t _convertFromTwosComplement(const std::int32_t val) noexcept;

hx711/src/HX711.cpp

Lines 65 to 67 in de87c5b

std::int32_t HX711::_convertFromTwosComplement(const std::int32_t val) noexcept {
return -(val & 0x800000) + (val & 0x7fffff);
}

void HX711::_readBits(std::int32_t* const v) {

std::int32_t v = 0;

HX711 uses 24 bits, but in code it's unclear why a 32 bit int is used.

Suggest typedef'ing std::int32_t (or one of the other std ints) with HX711::rawval_t or similar.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.