-
Notifications
You must be signed in to change notification settings - Fork 9
/
ads8339.h
66 lines (59 loc) · 1.77 KB
/
ads8339.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include "torque_sensor.h"
#include "logger.h"
class ADS8339 : public TorqueSensorBase {
public:
ADS8339(SPIDMA &spi_dma, uint8_t decimation=0) :
TorqueSensorBase(), spi_dma_(spi_dma), decimation_(decimation) {
}
bool init() {
return true;
}
void trigger() {
if (++count_ <= decimation_) {
return;
}
count_ = 0;
spi_dma_.start_readwrite_isr(data_out_, data_in_, length_);
}
float read() {
if (count_ == 0) {
spi_dma_.finish_readwrite_isr();
raw_value_ = data_in_[0] << 8 | data_in_[1];
signed_value_ = raw_value_ - 0x7FFF;
torque_ = signed_value_ * gain_;
// stale values are currently the only fault mechanism
if (last_new_signed_value_ != signed_value_) {
last_new_signed_value_ = signed_value_;
stale_value_count_ = 0;
} else {
stale_value_count_++;
if (stale_value_count_ > 3) {
timeout_error_++;
stale_value_count_ = 0;
}
}
if (raw_value_ == 0 || raw_value_ == 0xFFFF) {
read_error_++;
}
}
return torque_;
}
void clear_faults() {
timeout_error_ = 0;
stale_value_count_ = 0;
read_error_ = 0;
}
uint8_t count_ = 0;
int32_t signed_value_ = 0;
int32_t last_new_signed_value_ = 0;
uint8_t stale_value_count_ = 0;
uint32_t timeout_error_ = 0;
uint32_t read_error_ = 0;
uint32_t raw_value_ = 0;
SPIDMA &spi_dma_;
static const uint8_t length_ = 2;
uint8_t data_out_[length_] = {};
uint8_t data_in_[length_] = {};
uint8_t decimation_ = 0;
};