-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilament_Sensor.cpp
157 lines (148 loc) · 4.03 KB
/
Filament_Sensor.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Filament_Sensor.h"
Filament_Sensor::Filament_Sensor(byte PIN){
_pin = PIN;
}
void Filament_Sensor::Init(){
ds = new OneWire(_pin);
readout_to_mean = READOUT_MEAN_N;
integration_time = INT_TIME;
measurement_time = M_TIME;
slope_coef = SLOPE;
intercept = INTERCEPT;
ADDR = OW_ADDR;
upload_config();
}
void Filament_Sensor::set_measurement_time(int time){
measurement_time = time;
}
void Filament_Sensor::set_integration_time(int time){
integration_time = time;
}
void Filament_Sensor::set_readout_to_mean(byte n){
readout_to_mean = n;
}
void Filament_Sensor::set_addr(byte addr){
ADDR = addr;
}
void Filament_Sensor::set_calibration_parameters(float _slope,byte _intercept){
slope_coef = _slope;
intercept = _intercept;
}
int Filament_Sensor::readSpeed_X(bool raw){
if(data[8] != 1){
int16_t raw_1 = (data[2] << 8) | data[0];
if(raw) raw_1 = (data[4] << 8) | data[5];
//if(raw_1 > 1000) raw_1 = -1*(4095-raw_1);
return(slope_coef*raw_1*60+intercept);
}else{
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Comm error");
#endif
return(-999);
}
}
int Filament_Sensor::readSpeed_Y(bool raw){
if(data[8] != 1){
int16_t raw_1 = (data[3] << 8) | data[1];
if(raw) raw_1 = (data[6] << 8) | data[7];
//if(raw_1 > 1000) raw_1 = -1*(4095-raw_1);
return(slope_coef*raw_1*60+intercept);
}else{
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Comm error");
#endif
return(-999);
}
}
bool Filament_Sensor::readData(){
if ( !ds->search(addr)) {
ds->reset_search();
delay(50);
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("No sensor detected!");
#endif
return(false);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("CRC is not valid!");
#endif
ds->reset_search();
delay(50);
return(false);
}
if(addr[0] != ADDR){
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Comm error no sensor detected!");
#endif
ds->reset_search();
delay(50);
return(false);
}
delay(50);
ds->reset();
ds->select(addr);
ds->write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds->read();
}
ds->reset_search();
return(true);
}
bool Filament_Sensor::upload_config(){
if ( !ds->search(addr)) {
ds->reset_search();
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Config uplaod fail");
#endif
return(false);
}
if(addr[0] == ADDR){
ds->reset();
ds->select(addr);
ds->write(0x4E);
ds->write(uint8_t(measurement_time>>8));
ds->write(uint8_t(measurement_time));
ds->write(uint8_t(integration_time>>8));
ds->write(uint8_t(integration_time));
ds->write(readout_to_mean);
ds->reset_search();
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Config uploaded!");
#endif
return(true);
}else{
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Comm error no sensor detected!");
#endif
return(false);
}
}
bool Filament_Sensor::set_resolution(byte X_res,byte Y_res){
if ( !ds->search(addr)) {
ds->reset_search();
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Resolution uplaod fail");
#endif
return(false);
}
if(addr[0] == ADDR){
ds->reset();
ds->select(addr);
ds->write(0xAA);
ds->write(X_res);
ds->write(Y_res);
ds->reset_search();
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Resolution Set!");
#endif
return(true);
}else{
#ifdef DEBUG_FIL_SENSOR
#ifdef SERIAL_FILAMENT_DEBUG
Serial.println("Comm error no sensor detected!");
#endif
#endif
return(false);
}
}