Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Baro/AP_Baro_AUAV.cpp
9576 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
#include "AP_Baro_AUAV.h"
16
17
#if AP_BARO_AUAV_ENABLED
18
19
extern const AP_HAL::HAL &hal;
20
21
AP_Baro_AUAV::AP_Baro_AUAV(AP_Baro &baro, AP_HAL::Device *_dev)
22
: AP_Baro_Backend(baro)
23
, dev(_dev)
24
{
25
}
26
27
AP_Baro_Backend *AP_Baro_AUAV::probe(AP_Baro &baro, AP_HAL::Device &_dev)
28
{
29
AP_Baro_AUAV *sensor = NEW_NOTHROW AP_Baro_AUAV(baro, &_dev);
30
if (!sensor || !sensor->init()) {
31
delete sensor;
32
return nullptr;
33
}
34
return sensor;
35
}
36
37
bool AP_Baro_AUAV::init()
38
{
39
if (!dev) {
40
return false;
41
}
42
43
{
44
// Take semaphore for i2c functions
45
WITH_SEMAPHORE(dev->get_semaphore());
46
dev->set_retries(10);
47
48
// Request a measurement
49
if (!sensor.measure()) {
50
return false;
51
}
52
hal.scheduler->delay(40);
53
54
// Test read and discard result as the compensation coefficients are not configured
55
float PComp, temperature;
56
if (sensor.collect(PComp, temperature) != AUAV_Pressure_sensor::Status::Normal) {
57
return false;
58
}
59
}
60
61
// Register sensor and set dev-id
62
instance = _frontend.register_sensor();
63
dev->set_device_type(DEVTYPE_BARO_AUAV);
64
set_bus_id(instance, dev->get_bus_id());
65
66
dev->register_periodic_callback(40000,
67
FUNCTOR_BIND_MEMBER(&AP_Baro_AUAV::timer, void));
68
69
return true;
70
}
71
72
// accumulate a new sensor reading
73
void AP_Baro_AUAV::timer(void)
74
{
75
if (sensor.stage != AUAV_Pressure_sensor::CoefficientStage::Done) {
76
sensor.read_coefficients();
77
return;
78
}
79
80
if (measurement_requested) {
81
// Read in result of last measurement
82
float Pcomp, temp_C;
83
switch (sensor.collect(Pcomp, temp_C)) {
84
case AUAV_Pressure_sensor::Status::Normal: {
85
// Convert to correct units
86
const float pressure_mbar = 250 + (1.25 * ((Pcomp-1677722)/16777216.0) * 1000.0);
87
{
88
WITH_SEMAPHORE(_sem);
89
pressure_sum += pressure_mbar * 100;
90
temperature_sum += temp_C;
91
count++;
92
}
93
break;
94
}
95
case AUAV_Pressure_sensor::Status::Busy:
96
// Don't request a new measurement
97
return;
98
99
case AUAV_Pressure_sensor::Status::Fault:
100
break;
101
}
102
}
103
104
// Request a new measurement
105
measurement_requested = sensor.measure();
106
}
107
108
// transfer data to the frontend
109
void AP_Baro_AUAV::update(void)
110
{
111
if (count == 0) {
112
return;
113
}
114
115
WITH_SEMAPHORE(_sem);
116
117
_copy_to_frontend(instance, pressure_sum/count, temperature_sum/count);
118
119
pressure_sum = 0;
120
temperature_sum = 0;
121
count = 0;
122
}
123
124
#endif // AP_BARO_AUAV_ENABLED
125
126