PX4 Firmware
PX4 Autopilot Software http://px4.io
muorb_test_example.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2015 Mark Charlebois. 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 hello_example.cpp
36  * Example for Linux
37  *
38  * @author Mark Charlebois <charlebm@gmail.com>
39  */
40 
41 #include "muorb_test_example.h"
42 #include <px4_platform_common/log.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <px4_platform_common/defines.h>
46 #include <uORB/Publication.hpp>
48 #include <uORB/Subscription.hpp>
49 
50 px4::AppState MuorbTestExample::appState;
51 
53 {
54  int rc;
55  appState.setRunning(true);
56  rc = PingPongTest();
57  appState.setRunning(false);
58  return rc;
59 }
60 
62 {
63  int i = 0;
64 
65  uORB::Subscription sub_vc{ORB_ID(vehicle_command)};
66  uORB::PublicationQueued<vehicle_command_s> vcmd_pub{ORB_ID(vehicle_command)};
67  uORB::Publication<esc_status_s> pub_id{ORB_ID(esc_status)};
68  pub_id.publish(m_esc_status);
69 
70  while (!appState.exitRequested() && i < 100) {
71 
72  PX4_DEBUG("[%d] Doing work...", i);
73 
74  if (!pub_id.publish(m_esc_status)) {
75  PX4_ERR("[%d]Error publishing the esc status message for iter", i);
76  break;
77  }
78 
79  if (sub_vc.updated()) {
80  PX4_DEBUG("[%d]Vechicle Status is updated... reading new value", i);
81 
82  if (!sub_vc.copy(&m_vc)) {
83  PX4_ERR("[%d]Error calling orb copy for vechivle status... ", i);
84  break;
85  }
86 
87  if (!vcmd_pub.publish(m_vc)) {
88  PX4_ERR("[%d]Error publishing the vechile command message", i);
89  break;
90  }
91 
92  } else {
93  PX4_ERR("[%d]Error checking the updated status for vechile command... ", i);
94  PX4_DEBUG("[%d] VC topic is not updated", i);
95  break;
96  }
97 
98  ++i;
99  }
100 
101  return 0;
102 }
103 
105 {
106  int i = 0;
107  uORB::PublicationQueued<vehicle_command_s> vcmd_pub{ORB_ID(vehicle_command)};
108  uORB::Subscription sub_esc_status{ORB_ID(esc_status)};
109 
110  while (!appState.exitRequested()) {
111 
112  PX4_INFO("[%d] Doing work...", i);
113 
114  if (sub_esc_status.updated()) {
115  PX4_INFO("[%d]ESC status is updated... reading new value", i);
116 
117  if (!sub_esc_status.copy(&m_esc_status)) {
118  PX4_ERR("[%d]Error calling orb copy for esc status... ", i);
119  break;
120  }
121 
122  if (!vcmd_pub.publish(m_vc)) {
123  PX4_ERR("[%d]Error publishing the vechile command message", i);
124  break;
125  }
126 
127  } else {
128  PX4_INFO("[%d] esc status topic is not updated", i);
129  }
130 
131  // sleep for 1 sec.
132  usleep(1000000);
133 
134  ++i;
135  }
136 
137  return 0;
138 }
struct vehicle_command_s m_vc
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
struct esc_status_s m_esc_status
static px4::AppState appState