PX4 Firmware
PX4 Autopilot Software http://px4.io
tests_common.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 Todd Stellanova. 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 of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software 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 #include <stdio.h>
35 #include "tests_common.h"
36 
37 
38 void insert_values_around_mean(DataValidator *validator, const float mean, uint32_t count, float *rms_err,
39  uint64_t *timestamp_io)
40 {
41  uint64_t timestamp = *timestamp_io;
42  uint64_t timestamp_incr = 5;
43  const uint64_t error_count = 0;
44  const int priority = 50;
45  const float swing = 1E-2f;
46  double sum_dev_squares = 0.0f;
47 
48  //insert a series of values that swing around the mean
49  for (uint32_t i = 0; i < count; i++) {
50  float iter_swing = (0 == (i % 2)) ? swing : -swing;
51  float iter_val = mean + iter_swing;
52  float iter_dev = iter_val - mean;
53  sum_dev_squares += (iter_dev * iter_dev);
54  timestamp += timestamp_incr;
55  validator->put(timestamp, iter_val, error_count, priority);
56  }
57 
58  double rms = sqrt(sum_dev_squares / (double)count);
59  //note: this should be approximately equal to "swing"
60  *rms_err = (float)rms;
61  *timestamp_io = timestamp;
62 }
63 
65 {
66  uint32_t state = validator->state();
67  printf("state: 0x%x no_data: %d stale: %d timeout:%d\n",
68  validator->state(),
72  );
73  validator->print();
74  printf("\n");
75 }
76 
78  const float incr_value,
79  float *value_io,
80  uint64_t *timestamp_io)
81 {
82  uint64_t timestamp = *timestamp_io;
83  const uint64_t timestamp_incr = 5; //usec
84  const uint32_t timeout_usec = 2000;//derived from class-private value
85 
86  float val = *value_io;
87  const uint64_t error_count = 0;
88  const int priority = 50; //"medium" priority
89  const int equal_value_count = 100; //default is private VALUE_EQUAL_COUNT_DEFAULT
90 
91  validator->set_equal_value_threshold(equal_value_count);
92  validator->set_timeout(timeout_usec);
93 
94  //put a bunch of values that are all different
95  for (int i = 0; i < equal_value_count; i++, val += incr_value) {
96  timestamp += timestamp_incr;
97  validator->put(timestamp, val, error_count, priority);
98  }
99 
100  *timestamp_io = timestamp;
101  *value_io = val;
102 
103 }
static enum @74 state
static constexpr uint32_t ERROR_FLAG_TIMEOUT
void dump_validator_state(DataValidator *validator)
Print out the state of a DataValidator.
void insert_values_around_mean(DataValidator *validator, const float mean, uint32_t count, float *rms_err, uint64_t *timestamp_io)
Insert a series of samples around a mean value.
const int equal_value_count
static constexpr uint32_t ERROR_FLAG_STALE_DATA
void print()
Print the validator value.
void set_timeout(uint32_t timeout_interval_us)
Set the timeout value.
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
uint32_t state() const
Get the error state of this validator.
static constexpr uint32_t ERROR_FLAG_NO_DATA
void set_equal_value_threshold(uint32_t threshold)
Set the equal count threshold.
void put(uint64_t timestamp, float val, uint64_t error_count, int priority)
Put an item into the validator.
void fill_validator_with_samples(DataValidator *validator, const float incr_value, float *value_io, uint64_t *timestamp_io)
Insert a time series of samples into the validator.
Dual< Scalar, N > sqrt(const Dual< Scalar, N > &a)
Definition: Dual.hpp:188