-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_cell.h
47 lines (37 loc) · 1.12 KB
/
load_cell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef COG_ESTIMATOR_LOAD_CELL_H_
#define COG_ESTIMATOR_LOAD_CELL_H_
namespace measure {
class Load_cell {
Load_cell(const Load_cell&) = delete;
public:
Load_cell(uint_fast8_t dat, uint_fast8_t clk) : dat_pin(dat), clk_pin(clk), offset(0) {
pinMode(this->dat_pin, INPUT);
pinMode(this->clk_pin, OUTPUT);
this->offset = this->read();
}
float read() const {
uint_fast32_t data = 0;
while (digitalRead(this->dat_pin) != 0);
for (uint_fast8_t i = 0; i < 24; i++) {
digitalWrite(this->clk_pin, 1);
delayMicroseconds(1);
digitalWrite(this->clk_pin, 0);
delayMicroseconds(1);
data = (data << 1) | digitalRead(this->dat_pin);
}
digitalWrite(this->clk_pin, 1); //gain=128
delayMicroseconds(1);
digitalWrite(this->clk_pin, 0);
delayMicroseconds(1);
data = data ^ 0x800000;
const auto volt = data * (4.2987 / 16777216.0 / 128); //Serial.println(volt,10);
const auto gram = volt / (0.000669 * 4.2987 / 200.0); //Serial.println(gram,4);
return gram - this->offset;
}
private:
uint_fast8_t dat_pin;
uint_fast8_t clk_pin;
float offset;
};
};
#endif