Wiring - příkaz scale

Wiring, C++, C, Java, ...
Pravidla fóra
Toto subfórum slouží k řešení obecných otázek kolem programování (konstrukce, knihovny, alokace paměti, ...)
Odpovědět
Axamith
Příspěvky: 538
Registrován: 09 srp 2017, 08:17
Kontaktovat uživatele:

Wiring - příkaz scale

Příspěvek od Axamith » 04 led 2018, 18:34

Snažím se něco spatlat na poli vážení, AD převodník HX711, tenzometr ... Prošel jsem dost kalibračních sketchí, jako velmi podařená se mi jeví https://learn.sparkfun.com/tutorials/lo ... 1492100024

Kód: Vybrat vše

 Arduino pin 2 -> HX711 CLK
 3 -> DOUT
 5V -> VCC
 GND -> GND

*/

#include "HX711.h"

#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");

  scale.set_scale();
  scale.tare(); //Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" lbs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}

Narazil jsem zde na příkaz

Kód: Vybrat vše

scale
Je mi jasné, že jde o nastavení měřítka. V kódu je použitý v různých formách zadání (výběr příkazů z kódu):

Kód: Vybrat vše

HX711 scale(DOUT, CLK);
scale.set_scale();
scale.tare();
long zero_factor = scale.read_average();
scale.set_scale(calibration_factor);
Serial.print(scale.get_units(), 1);
Snažil jsem se funkci kódu pochopit a hledal jsem nějaké bližší informace o tomto příkazu, formu zadání, použití, parametry ... Nikde nic rozumného jsem nenašel. Máte někdo s ním zkušenost?

Uživatelský avatar
pavel1tu
Příspěvky: 2065
Registrován: 26 říj 2017, 08:28
Bydliště: Trutnov
Kontaktovat uživatele:

Re: Wiring - příkaz scale

Příspěvek od pavel1tu » 04 led 2018, 19:15

to patří ke knihovně, tam to na github není popsané ?
UNO, NANO, Mikro, PRO mini, DUE, ESP32S2, RPi PICO
Pavel1TU
"Správně napsaný kod lze číst jako knihu"

ohruska
Příspěvky: 235
Registrován: 08 pro 2017, 20:56

Re: Wiring - příkaz scale

Příspěvek od ohruska » 04 led 2018, 20:21

Mám použit tento kód:

Kód: Vybrat vše

#include "HX711.h"

// HX711.DOUT	- pin #A1
// HX711.PD_SCK	- pin #A0

HX711 scale(A1, A0);		// parameter "gain" is ommited; the default value 128 is used by the library

void setup() {
  Serial.begin(57600);
  Serial.println("HX711 Demo");

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());			// print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));  	// print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);	// print the average of 5 readings from the ADC minus tare weight (not set) divided 
						// by the SCALE parameter (not set yet)  

  scale.set_scale(2280.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();				        // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

 Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));		// print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided 
						// by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
//  Serial.print("one reading:\t");
//   Serial.print('H');
//     Serial.print(',');
     Serial.print(scale.get_units()/100, 4);
 // Serial.print("\t| average:\t");
//  Serial.println(scale.get_units(2), 1);
  Serial.println();

 // scale.power_down();			        // put the ADC in sleep mode
  delay(100);
 // scale.power_up();
}
Jediné co jsem udělal, tak jsem změnil hodnotu 2280 v kódu, tak aby odpovídala měřená hmotnost, podle použitého tenzometru.

Kód: Vybrat vše

scale.set_scale(2280.f); 

martinius96
Příspěvky: 585
Registrován: 01 srp 2017, 19:29
Bydliště: Poprad
Kontaktovat uživatele:

Re: Wiring - příkaz scale

Příspěvek od martinius96 » 04 led 2018, 22:12

Ahoj, toto som robil pre jeden obchod s Arduinom + Ethernet shieldom. Vytiahni si, čo potrebuješ:

Kód: Vybrat vše

#include "HX711.h"
#include <Ethernet.h>
#include <SPI.h>                    
#define Hostname "Arduino"            //Meno arduina v siti
byte mac[] = { 0xAA, 0xBB, 0xAA, 0xBB, 0xAA, 0xBB };            //MAC ADRESA ARDUINA --> VOLITELNA
char server[] = "www.web.cz";      //ADRESA WEBSERVERA (MOZE BYT AJ IP ADRESA)
IPAddress ip(10, 0, 0, 151);                               //IP ADRESA ZARIADENIA V SIETI V LOKALNEJ SIETI
EthernetClient client;                                          //SPUSTENIE ETHERNETU AKO CLIENTA
HX711 scale (A2, A3); //definovane piny pre cip HX711

int led1 = 5;
int led2 = 4;
int led3 = 3;
int led4 = 2;
int PRETIZENI = 4000;
int PRAZDNY = 10;
double momentalnahodnota=0;
double predchadzajucahodnota=0;
int rozdiel=0; 
unsigned long pocitadlo= 0;
void setup()
{
Serial.begin(9600);

scale.set_scale(300.f); 
scale.tare(); // NASTAVI VAHU NA 0
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);



  }
  void resetdiod(){ 
  digitalWrite (led1, LOW);
  digitalWrite (led2, LOW);
  digitalWrite (led3, LOW);
  digitalWrite (led4, LOW);
  }
void loop()
{  
  if(pocitadlo < 1){
 momentalnahodnota = scale.get_units(); 
 if(0<= momentalnahodnota && momentalnahodnota <= 5 || 0>= momentalnahodnota && momentalnahodnota >= -5){
scale.tare();
 momentalnahodnota=0;}
  predchadzajucahodnota = scale.get_units();
 
  pocitadlo++;
  delay(3000);
  }else{
    if (Ethernet.begin(mac) == 0) {                 
    Serial.println("Chyba konfiguracie cez DHCP"); //SERIOVY VYPIS CHYBY KONFIGURACIE DHCP
    Ethernet.begin(mac, ip);                       //NASTAVENIE IP A MAC ADRESY PRE ETHERNET MODUL
  }
momentalnahodnota = scale.get_units(); 
if(0<= momentalnahodnota && momentalnahodnota <= 5 || 0>= momentalnahodnota && momentalnahodnota >= -5){
 scale.tare();
 momentalnahodnota=0;}

 
rozdiel =  (momentalnahodnota - predchadzajucahodnota);
   if (rozdiel > 30){
if (rozdiel > 50){
  Serial.println("Pridani vice polozek");
    if (client.connect(server, 80)) {
    client.print("GET /pridanivicepolozek.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
    client.print("&predchadzajucahodnota=");      
    client.print(predchadzajucahodnota); 
    client.print("&rozdiel=");              
    client.print(rozdiel); 
  client.print("&stav=Pridani%20vice%20polozek");                             
    client.println(" HTTP/1.1");               
    client.println("Host: www.web.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }
  else{
  Serial.println("Pridani polozky");
   if (client.connect(server, 80)) {
    client.print("GET /pridanipolozky.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
    client.print("&predchadzajucahodnota=");      
    client.print(predchadzajucahodnota); 
    client.print("&rozdiel=");              
    client.print(rozdiel); 
     client.print("&stav=Pridani%20polozky");                                   
    client.println(" HTTP/1.1");               
    client.println("Host: www.web.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }}
  if (rozdiel < -30){
if (rozdiel < -50){
   digitalWrite (led4, HIGH);
  Serial.println("Pozor - odebrání více položek");
    if (client.connect(server, 80)) {
    client.print("GET /odebranivicepolozek.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
    client.print("&predchadzajucahodnota=");      
    client.print(predchadzajucahodnota); 
    client.print("&rozdiel=");              
    client.print(rozdiel); 
    client.print("&stav=Pozor%20odebrani%20vice%20polozek");                                
    client.println(" HTTP/1.1");               
    client.println("Host: www.webs.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }else{
  Serial.println("Odebrani polozky");
  digitalWrite (led3, HIGH);
    if (client.connect(server, 80)) {
    client.print("GET /odebranipolozky.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
    client.print("&predchadzajucahodnota=");      
    client.print(predchadzajucahodnota); 
    client.print("&rozdiel=");              
    client.print(rozdiel); 
      client.print("&stav=Odebrani%20polozky");                                 
    client.println(" HTTP/1.1");               
    client.println("Host: www.dsfsdf.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  }} 
  }
  
   
   
if (momentalnahodnota < PRAZDNY) {
  Serial.println("Prazdny regal");

  digitalWrite (led1, HIGH);
  delay(350);
  digitalWrite (led1, LOW);
  delay(350);
      if (client.connect(server, 80)) {
    client.print("GET /prazdnyregal.php?momentalnahodnota=");  
    client.print(momentalnahodnota);
    client.print("&predchadzajucahodnota="); 
   client.print(predchadzajucahodnota);
   client.print("&rozdiel="); 
   client.print(rozdiel);
    client.print("&stav=Prazdny%20regal");                                   
    client.println(" HTTP/1.1");               
    client.println("Host: www.shelfgdssdfdsfuard.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  }
}
if (momentalnahodnota > PRETIZENI) {
  Serial.println("Pretizeny regal");
  digitalWrite (led2, HIGH);
        if (client.connect(server, 80)) {
    client.print("GET /pretizenyregal.php?momentalnahodnota=");  
   client.print(momentalnahodnota);
   client.print("&predchadzajucahodnota="); 
   client.print(predchadzajucahodnota);
   client.print("&rozdiel="); 
   client.print(rozdiel);
    client.print("&stav=Pretizeny%20regal");                               
    client.println(" HTTP/1.1");               
    client.println("Host: www.dsfdsf.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } else {                                    
    Serial.println("Pripojenie zlyhalo");            //SERIOVY VYPIS O NEUSPESNOSTI PRIPOJENIA
  }
}

  if(pocitadlo>=150){
if (momentalnahodnota > PRETIZENI) {

       if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
   client.print(momentalnahodnota);
    client.print("&stav=Pretizeny%20regal");                               
    client.println(" HTTP/1.1");               
    client.println("Host: www.fdsf.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  }

  
  
  }
 if (momentalnahodnota < PRAZDNY) {
  Serial.println("Prazdny regal");

  digitalWrite (led1, HIGH);
  delay(350);
  digitalWrite (led1, LOW);
  delay(350);
      if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
    client.print(momentalnahodnota);
    client.print("&stav=Prazdny%20regal");                                   
    client.println(" HTTP/1.1");               
    client.println("Host: www.sdfdfdf.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  }
}
  
if (rozdiel < -30){
if (rozdiel < -50){
   digitalWrite (led4, HIGH);
  Serial.println("Pozor - odebrání více položek");
    if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
    client.print("&stav=Pozor%20odebrani%20vice%20polozek");                                
    client.println(" HTTP/1.1");               
    client.println("Host: www.dsfdsfds.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }else{
  Serial.println("Odebrani polozky");
  digitalWrite (led3, HIGH);
    if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
      client.print("&stav=Odebrani%20polozky");                                 
    client.println(" HTTP/1.1");               
    client.println("Host: www.aaaa.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  }} 
  }
    if (rozdiel > 30){
if (rozdiel > 50){
  Serial.println("Pridani vice polozek");
    if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
  client.print("&stav=Pridani%20vice%20polozek");                             
    client.println(" HTTP/1.1");               
    client.println("Host: www.abc.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }
  else{
  Serial.println("Pridani polozky");
   if (client.connect(server, 80)) {
    client.print("GET /reference.php?momentalnahodnota=");  
    client.print(momentalnahodnota);  
     client.print("&stav=Pridani%20polozky");                                   
    client.println(" HTTP/1.1");               
    client.println("Host: www.host.php5.cz");
    client.println("Connection: close");         
    client.println();                           
    client.stop();                                 

  } 
  }}
      
    pocitadlo=1;
    }
   
predchadzajucahodnota = scale.get_units();
if(0<= predchadzajucahodnota && predchadzajucahodnota <= 5 || 0>= predchadzajucahodnota && predchadzajucahodnota >= -5){
 scale.tare();
 predchadzajucahodnota=0;}
delay(4000); //pockame 6 sekund pred novym meranim vahy
resetdiod(); //vypneme vsetky ledky pred novym meranim, aby bol ich stav aktualny
  pocitadlo++;
 
  }
}}

Axamith
Příspěvky: 538
Registrován: 09 srp 2017, 08:17
Kontaktovat uživatele:

Re: Wiring - příkaz scale

Příspěvek od Axamith » 05 led 2018, 08:13

pavel1tu píše:
04 led 2018, 19:15
to patří ke knihovně, tam to na github není popsané ?
Mohl bych poprosit o ještě větší nakopnutí? Jaké konkrétní knihovny? Asi na Githubu neumím hledat, nic konkrétního jsem nenašel.

Proč to vlastně potřebuji, napadlo udělal autokalibrační sketch.
Mám vstupní veličiny:
  • základní kalibrační konstantu (Kt)
  • krok kalibrační konstanty (Kx)
  • aktuálně naměřenou hmotnost (X)
  • požadovanou kalibrační hmotnost (0)
Podmínkou by šlo nechat dopočítávat kalibrační konstatntu.

Je-li měření větší než nula, Kt=Kt+Kx, následně proveď další porovnání výpočtu ve smyčce. Směr výpočtu (+ nebo -) by určila předešlá podmínka, která by analyzovala kladnou nebo zápornou hodnotu měření. Už jsem se díval na nějaké podmínky a vzorce, mělo by to fungovat. A výsledek uložit do EEprom přes knihovnu EEproMex, takže kalibrační koeficient by byl dostupný bez zásahu do sketche.

Možná je to blbost, ale chtěl jsem to zkusit, tak jsem si našel nějakou funkční kalibraci a procházel detaily kódu, nějak jsem ale nepochopil práci s příkazem scale, respektice rozumím, k čemu ve sketchi je, ale nevím, jaké má možnosti, detaily práce s ním. Rád věci chápu, nechci se jen spokojit s tím, že něco funguje.

jankop
Příspěvky: 1057
Registrován: 06 zář 2017, 20:04
Bydliště: Brno
Kontaktovat uživatele:

Re: Wiring - příkaz scale

Příspěvek od jankop » 12 led 2018, 16:52

Nejsem expertem přes knihovny, ale ten tvůj scale je podle mě prostě funkcí (metodou?) knihovny HX711.h, kterou vkládáš. Tak se do ní podívej, prostuduj případné examples a projdi si její soubory *.h a *.cpp to by ti mělo poskytnout dostatečný obraz.

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 5 hostů