PX4 Firmware
PX4 Autopilot Software http://px4.io
test_hrt.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_hrt.c
36  * Tests the high resolution timer.
37  */
38 
39 #include <drivers/drv_hrt.h>
40 #include <px4_platform_common/posix.h>
41 #include <sys/time.h>
42 
43 #include "tests_main.h"
44 
45 int test_hrt(int argc, char *argv[])
46 {
47  struct hrt_call call;
48  hrt_abstime prev, now;
49  int i;
50  struct timeval tv1, tv2;
51 
52  printf("start-time (hrt, sec/usec), end-time (hrt, sec/usec), microseconds per 1/10 second\n");
53 
54  for (i = 0; i < 10; i++) {
55  prev = hrt_absolute_time();
56  gettimeofday(&tv1, nullptr);
57  px4_usleep(100000);
58  now = hrt_absolute_time();
59  gettimeofday(&tv2, nullptr);
60  printf("%lu (%lu/%lu), %lu (%lu/%lu), %lu\n",
61  (unsigned long)prev, (unsigned long)tv1.tv_sec, (unsigned long)tv1.tv_usec,
62  (unsigned long)now, (unsigned long)tv2.tv_sec, (unsigned long)tv2.tv_usec,
63  (unsigned long)(hrt_absolute_time() - prev));
64  fflush(stdout);
65  }
66 
67  px4_usleep(1000000);
68 
69  printf("one-second ticks\n");
70 
71  for (i = 0; i < 3; i++) {
72  hrt_call_after(&call, 1000000, nullptr, nullptr);
73 
74  while (!hrt_called(&call)) {
75  px4_usleep(1000);
76  }
77 
78  printf("tick\n");
79  fflush(stdout);
80  }
81 
82  return 0;
83 }
High-resolution timer with callouts and timekeeping.
__EXPORT void hrt_call_after(struct hrt_call *entry, hrt_abstime delay, hrt_callout callout, void *arg)
Call callout(arg) after delay has elapsed.
__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.
Tests declaration file.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
int test_hrt(int argc, char *argv[])
Definition: test_hrt.cpp:45
Callout record.
Definition: drv_hrt.h:72
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).