PX4 Firmware
PX4 Autopilot Software http://px4.io
ParameterTest.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 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 #include <px4_platform_common/module_params.h>
35 #include <uORB/Subscription.hpp>
37 #include <uORB/uORBManager.hpp>
38 
39 #include <gtest/gtest.h>
40 
41 class ParameterTest : public ::testing::Test
42 {
43 public:
44  void SetUp() override
45  {
47  }
48 };
49 
50 
51 TEST_F(ParameterTest, testParamReadWrite)
52 {
53  // GIVEN a parameter handle
54  param_t param = param_handle(px4::params::CP_DIST);
55 
56  // WHEN: we get the parameter
57  float value = -999.f;
58  int status = param_get(param, &value);
59 
60  // THEN it should be successful and have the default value
61  EXPECT_EQ(0, status);
62  EXPECT_EQ(-1, value);
63 
64  // WHEN: we set the parameter
65  value = 42.f;
66  status = param_set(param, &value);
67 
68  // THEN: it should be successful
69  EXPECT_EQ(0, status);
70 
71  // WHEN: we get the parameter again
72  float value2 = -1999.f;
73  status = param_get(param, &value2);
74 
75  // THEN: it should be exactly the value we set
76  EXPECT_EQ(0, status);
77  EXPECT_EQ(42.f, value2);
78 }
79 
80 
81 TEST_F(ParameterTest, testUorbSendReceive)
82 {
83  // GIVEN: a uOrb message
84  obstacle_distance_s message;
85  memset(&message, 0xDEAD, sizeof(message));
86  message.min_distance = 1.f;
87  message.max_distance = 10.f;
88 
89  // AND: a subscriber
90  uORB::SubscriptionData<obstacle_distance_s> sub_obstacle_distance{ORB_ID(obstacle_distance)};
91 
92  // WHEN we send the message
93  orb_advert_t obstacle_distance_pub = orb_advertise(ORB_ID(obstacle_distance), &message);
94  ASSERT_TRUE(obstacle_distance_pub != nullptr);
95 
96  // THEN: the subscriber should receive the message
97  sub_obstacle_distance.update();
98  const obstacle_distance_s &obstacle_distance = sub_obstacle_distance.get();
99 
100  // AND: the values we got should be the same
101  EXPECT_EQ(message.timestamp, obstacle_distance.timestamp);
102  EXPECT_EQ(message.min_distance, obstacle_distance.min_distance);
103  EXPECT_EQ(message.max_distance, obstacle_distance.max_distance);
104 
105  // AND: all the bytes should be equal
106  EXPECT_EQ(0, memcmp(&message, &obstacle_distance, sizeof(message)));
107 }
static struct vehicle_status_s status
Definition: Commander.cpp:138
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
Definition: parameters.cpp:814
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
Definition: uORB.cpp:43
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
TEST_F(ParameterTest, testParamReadWrite)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
void SetUp() override
__EXPORT void param_reset_all(void)
Reset all parameters to their default values.
Definition: parameters.cpp:910
uint32_t param_t
Parameter handle.
Definition: param.h:98