Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Baro/examples/BARO_generic/BARO_generic.cpp
9592 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
16
/*
17
generic Baro driver test
18
*/
19
20
#include <AP_Baro/AP_Baro.h>
21
#include <AP_BoardConfig/AP_BoardConfig.h>
22
#include <AP_HAL/AP_HAL.h>
23
#include <GCS_MAVLink/GCS_Dummy.h>
24
#include <AP_ExternalAHRS/AP_ExternalAHRS.h>
25
#include <AP_Logger/AP_Logger.h>
26
#include <AP_AHRS/AP_AHRS.h>
27
28
const AP_HAL::HAL &hal = AP_HAL::get_HAL();
29
30
// create barometer object
31
static AP_Baro barometer;
32
33
// creating other objects
34
#if HAL_LOGGING_ENABLED
35
static AP_Logger logger;
36
AP_Int32 logger_bitmask;
37
static const struct LogStructure log_structure[] = {
38
LOG_COMMON_STRUCTURES
39
};
40
#endif // HAL_LOGGING_ENABLED
41
42
static AP_AHRS ahrs;
43
44
#if AP_EXTERNAL_AHRS_ENABLED
45
static AP_ExternalAHRS eAHRS;
46
#endif // AP_EXTERNAL_AHRS_ENABLED
47
48
static uint32_t timer;
49
static AP_BoardConfig board_config;
50
51
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
52
#include <SITL/SITL.h>
53
SITL::SIM sitl;
54
#endif
55
56
void setup();
57
void loop();
58
59
// to be called only once on boot for initializing objects
60
void setup()
61
{
62
hal.console->printf("Barometer library test\n");
63
64
board_config.init();
65
#if AP_SIM_ENABLED
66
sitl.init();
67
#endif // AP_SIM_ENABLED
68
#if HAL_LOGGING_ENABLED
69
logger.init(logger_bitmask, log_structure, ARRAY_SIZE(log_structure));
70
#endif // HAL_LOGGING_ENABLED
71
72
hal.scheduler->delay(1000);
73
74
// initialize the barometer
75
barometer.init();
76
barometer.calibrate();
77
78
// set up timer to count time in microseconds
79
timer = AP_HAL::micros();
80
}
81
82
// loop
83
void loop()
84
{
85
// terminate program if console fails to initialize
86
if (!hal.console->is_initialized()) {
87
return;
88
}
89
90
// run update() at 10Hz
91
if ((AP_HAL::micros() - timer) > 100 * 1000UL) {
92
timer = AP_HAL::micros();
93
barometer.update();
94
95
//calculate time taken for barometer readings to update
96
uint32_t read_time = AP_HAL::micros() - timer;
97
if (!barometer.healthy()) {
98
hal.console->printf("not healthy\n");
99
return;
100
}
101
102
//output barometer readings to console
103
hal.console->printf(" Pressure: %.2f Pa\n"
104
" Temperature: %.2f degC\n"
105
" Relative Altitude: %.2f m\n"
106
" climb=%.2f m/s\n"
107
" Read + update time: %u usec\n"
108
"\n",
109
(double)barometer.get_pressure(),
110
(double)barometer.get_temperature(),
111
(double)barometer.get_altitude(),
112
(double)barometer.get_climb_rate(),
113
(unsigned)read_time);
114
} else {
115
// if stipulated time has not passed between two distinct readings, delay the program for a millisecond
116
hal.scheduler->delay(1);
117
}
118
}
119
120
GCS_Dummy _gcs;
121
122
123
AP_HAL_MAIN();
124
125