feat: capteur_balance #35
80
IOT/lib/Balance/include/Balance.h
Normal file
80
IOT/lib/Balance/include/Balance.h
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#ifndef BALANCE_H
|
||||||
|
#define BALANCE_H
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <HX711.h>
|
||||||
|
|
||||||
|
class Balance {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief constructeur
|
||||||
|
*
|
||||||
|
* @param doutPin : pin DOUT du module HX711
|
||||||
|
*
|
||||||
|
* @param sckPin : pin horloge du module HX711
|
||||||
|
*
|
||||||
|
* @param poidsRef : valeur de poids posée sur la balance ref en grammes
|
||||||
|
*/
|
||||||
|
Balance(int doutPin, int sckPin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief determine le poids
|
||||||
|
*
|
||||||
|
* @return poids (double)
|
||||||
|
*/
|
||||||
|
double getValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief determine la moyenne poids
|
||||||
|
*
|
||||||
|
* @param nbMesure : nombre de mesure effectues pour la moyenne
|
||||||
|
*
|
||||||
|
* @return moyenne poids (double)
|
||||||
|
*/
|
||||||
|
double getAverage(int nbMesure);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief tare et scale a 0, et recuperation de la valeur brut du plateau
|
||||||
|
*/
|
||||||
|
boolean initCalibration();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief initialisation du calibrage
|
||||||
|
*
|
||||||
|
* @param[in] poidsRef : poid de reférence pour la calibration
|
||||||
|
* @param[in] moyenne_calibration : nombre de valeurs a lire pour une mesure
|
||||||
|
*
|
||||||
|
* @return renvoi un true si calibration bien effectue
|
||||||
|
*/
|
||||||
|
boolean calibration(int poidsRef,int moyenne_calibration);
|
||||||
|
|
||||||
|
//TODO: faire doc setCalibration fact
|
||||||
|
void setCalibrationFact(int caliFact);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief true = calibration et false = erreur
|
||||||
|
*/
|
||||||
|
boolean initialized;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Facteur de calibration obtenu par : (val brute poids ref - val brute poids plexiglas)/ poids ref
|
||||||
|
*/
|
||||||
|
int calibrationFact;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Variable issue de la librairie HX711 qui permet d'utiliser les fonctions de celle-ci
|
||||||
|
*/
|
||||||
|
HX711 scale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Valeur moyenne brute du poids sans rien sur la balance juste le plexiglas
|
||||||
|
*/
|
||||||
|
long initialVal;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
62
IOT/lib/Balance/src/Balance.cpp
Normal file
62
IOT/lib/Balance/src/Balance.cpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include "../include/Balance.h"
|
||||||
|
|
||||||
|
Balance::Balance(int doutPin, int sckPin) {
|
||||||
|
|
||||||
|
scale.begin(doutPin, sckPin);
|
||||||
|
this->initialized = false;
|
||||||
|
scale.set_scale();
|
||||||
|
scale.tare();
|
||||||
|
this->initialVal = 0;
|
||||||
|
this->calibrationFact = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean Balance::initCalibration() {
|
||||||
|
|
||||||
|
scale.set_scale();
|
||||||
|
scale.tare();
|
||||||
|
this->initialVal = scale.read_average(20);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean Balance::calibration(int poidsRef,int moyenne_calibration) {
|
||||||
|
|
||||||
|
int rawValref = 0; // Valeur brute de plexiglas et poids réf
|
||||||
|
int tempPoids = 0; // C'est la valeur du poids en grammes calculée grâce au facteur de calibration
|
||||||
|
|
||||||
|
rawValref = scale.read_average(moyenne_calibration);
|
||||||
|
this->calibrationFact = (rawValref - this->initialVal) / poidsRef;
|
||||||
|
do {
|
||||||
|
scale.set_scale(calibrationFact);
|
||||||
|
tempPoids = scale.get_units(5);
|
||||||
|
if (tempPoids < poidsRef) {
|
||||||
|
calibrationFact -= 1;
|
||||||
|
} else if (tempPoids > poidsRef) {
|
||||||
|
calibrationFact += 1;
|
||||||
|
}
|
||||||
|
} while (tempPoids != poidsRef);
|
||||||
|
return initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Balance::getAverage(int nbMesure) {
|
||||||
|
|
||||||
|
if (initialized == true) {
|
||||||
|
return scale.get_units(nbMesure);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double Balance::getValue() {
|
||||||
|
|
||||||
|
if (initialized == true) {
|
||||||
|
return scale.get_units();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Balance::setCalibrationFact(int caliFact){
|
||||||
|
this->initialized = true;
|
||||||
|
this->calibrationFact = caliFact;
|
||||||
|
scale.set_scale(caliFact);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user