CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Airspeed/examples/Airspeed/Airspeed.cpp
Views: 1800
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
* Airspeed.cpp - airspeed example sketch
18
*
19
*/
20
21
#include <AP_AHRS/AP_AHRS.h>
22
#include <AP_Airspeed/AP_Airspeed.h>
23
#include <AP_HAL/AP_HAL.h>
24
#include <AP_BoardConfig/AP_BoardConfig.h>
25
#include <GCS_MAVLink/GCS_Dummy.h>
26
27
void setup();
28
void loop();
29
30
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
31
32
float temperature;
33
34
// create an AHRS object for get_airspeed_max
35
AP_AHRS ahrs;
36
37
// create airspeed object
38
AP_Airspeed airspeed;
39
40
static AP_BoardConfig board_config;
41
42
namespace {
43
// try to set the object value but provide diagnostic if it failed
44
void set_object_value(const void *object_pointer,
45
const struct AP_Param::GroupInfo *group_info,
46
const char *name, float value)
47
{
48
if (!AP_Param::set_object_value(object_pointer, group_info, name, value)) {
49
hal.console->printf("WARNING: AP_Param::set object value \"%s::%s\" Failed.\n",
50
group_info->name, name);
51
}
52
}
53
}
54
55
// to be called only once on boot for initializing objects
56
void setup()
57
{
58
hal.console->printf("ArduPilot Airspeed library test\n");
59
60
// set airspeed pin to 65, enable and use to true
61
set_object_value(&airspeed, airspeed.var_info, "PIN", 65);
62
set_object_value(&airspeed, airspeed.var_info, "ENABLE", 1);
63
set_object_value(&airspeed, airspeed.var_info, "USE", 1);
64
65
board_config.init();
66
67
// initialize airspeed
68
// Note airspeed.set_log_bit(LOG_BIT) would need to be called in order to enable logging
69
airspeed.init();
70
71
airspeed.calibrate(false);
72
}
73
74
// loop
75
void loop(void)
76
{
77
static uint32_t timer;
78
79
// run read() and get_temperature() in 10Hz
80
if ((AP_HAL::millis() - timer) > 100) {
81
82
// current system time in milliseconds
83
timer = AP_HAL::millis();
84
airspeed.update();
85
airspeed.get_temperature(temperature);
86
87
// print temperature and airspeed to console
88
hal.console->printf("airspeed %5.2f temperature %6.2f healthy = %u\n",
89
(double)airspeed.get_airspeed(), (double)temperature, airspeed.healthy());
90
}
91
hal.scheduler->delay(1);
92
}
93
94
const struct AP_Param::GroupInfo GCS_MAVLINK_Parameters::var_info[] = {
95
AP_GROUPEND
96
};
97
GCS_Dummy _gcs;
98
99
AP_HAL_MAIN();
100
101