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
6356 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
static AP_Int32 log_bitmask;
35
static AP_Logger logger;
36
static AP_AHRS ahrs;
37
38
#if AP_EXTERNAL_AHRS_ENABLED
39
static AP_ExternalAHRS eAHRS;
40
#endif // AP_EXTERNAL_AHRS_ENABLED
41
42
static uint32_t timer;
43
static AP_BoardConfig board_config;
44
45
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
46
#include <SITL/SITL.h>
47
SITL::SIM sitl;
48
#endif
49
50
void setup();
51
void loop();
52
53
// to be called only once on boot for initializing objects
54
void setup()
55
{
56
hal.console->printf("Barometer library test\n");
57
58
board_config.init();
59
60
hal.scheduler->delay(1000);
61
62
// initialize the barometer
63
barometer.init();
64
barometer.calibrate();
65
66
// set up timer to count time in microseconds
67
timer = AP_HAL::micros();
68
}
69
70
// loop
71
void loop()
72
{
73
// terminate program if console fails to initialize
74
if (!hal.console->is_initialized()) {
75
return;
76
}
77
78
// run update() at 10Hz
79
if ((AP_HAL::micros() - timer) > 100 * 1000UL) {
80
timer = AP_HAL::micros();
81
barometer.update();
82
83
//calculate time taken for barometer readings to update
84
uint32_t read_time = AP_HAL::micros() - timer;
85
if (!barometer.healthy()) {
86
hal.console->printf("not healthy\n");
87
return;
88
}
89
90
//output barometer readings to console
91
hal.console->printf(" Pressure: %.2f Pa\n"
92
" Temperature: %.2f degC\n"
93
" Relative Altitude: %.2f m\n"
94
" climb=%.2f m/s\n"
95
" Read + update time: %u usec\n"
96
"\n",
97
(double)barometer.get_pressure(),
98
(double)barometer.get_temperature(),
99
(double)barometer.get_altitude(),
100
(double)barometer.get_climb_rate(),
101
(unsigned)read_time);
102
} else {
103
// if stipulated time has not passed between two distinct readings, delay the program for a millisecond
104
hal.scheduler->delay(1);
105
}
106
}
107
108
GCS_Dummy _gcs;
109
110
111
AP_HAL_MAIN();
112
113