PX4 Firmware
PX4 Autopilot Software http://px4.io
hrt_test.cpp
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * Copyright (C) 2015 Mark Charlebois. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 /**
36  * @file hrt_test.cpp
37  * Test High Resolution Timers in Linux
38  *
39  * @author Mark Charlebois <charlebm@gmail.com>
40  */
41 
42 #include "hrt_test.h"
43 
44 #include <drivers/drv_hrt.h>
45 #include <px4_platform_common/log.h>
46 #include <px4_platform_common/time.h>
47 
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <cstring>
51 
52 px4::AppState HRTTest::appState;
53 
54 static struct hrt_call t1;
55 static int update_interval = 1;
56 
57 static void timer_expired(void *arg)
58 {
59  static int i = 0;
60  PX4_INFO("Test\n");
61 
62  if (i < 5) {
63  i++;
64  hrt_call_after(&t1, update_interval, timer_expired, (void *)nullptr);
65  }
66 }
67 
69 {
70  appState.setRunning(true);
71 
73  px4_usleep(1000000);
75  PX4_INFO("Elapsed time %llu in 1 sec (usleep)\n", (unsigned long long)elt);
76  PX4_INFO("Start time %llu\n", (unsigned long long)t);
77 
78  t = hrt_absolute_time();
79  px4_sleep(1);
80  elt = hrt_elapsed_time_atomic(&t);
81  PX4_INFO("Elapsed time %llu in 1 sec (sleep)\n", (unsigned long long)elt);
82  PX4_INFO("Start time %llu\n", (unsigned long long)t);
83 
84  memset(&t1, 0, sizeof(t1));
85 
86  PX4_INFO("HRT_CALL %d\n", hrt_called(&t1));
87 
88  hrt_call_after(&t1, update_interval, timer_expired, (void *)nullptr);
89  px4_sleep(2);
90  PX4_INFO("HRT_CALL - %d\n", hrt_called(&t1));
91  hrt_cancel(&t1);
92  PX4_INFO("HRT_CALL + %d\n", hrt_called(&t1));
93 
94  return 0;
95 }
int main()
Definition: hrt_test.cpp:68
static int update_interval
Definition: hrt_test.cpp:55
High-resolution timer with callouts and timekeeping.
static struct hrt_call t1
Definition: hrt_test.cpp:54
__EXPORT void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg)
Call callout(arg) after delay has elapsed.
void * arg
Definition: drv_hrt.h:78
__EXPORT bool hrt_called(struct hrt_call *entry)
If this returns true, the entry has been invoked and removed from the callout list, or it has never been entered.
Example app for Linux.
static px4::AppState appState
Definition: hrt_test.h:53
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
static void timer_expired(void *arg)
Definition: hrt_test.cpp:57
Callout record.
Definition: drv_hrt.h:72
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
__EXPORT void hrt_cancel(struct hrt_call *entry)
Remove the entry from the callout list.
__EXPORT hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.