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 muorb_test_example.cpp
36  * Example for muorb
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 <string.h>
47 #include "uORB/topics/pwm_input.h"
48 #include "modules/uORB/uORB.h"
49 #include "px4_middleware.h"
50 #include "px4_defines.h"
51 #include <stdlib.h>
52 #include <drivers/drv_hrt.h>
53 
54 px4::AppState MuorbTestExample::appState;
55 
57 {
58  int rc;
59  appState.setRunning(true);
60  rc = PingPongTest();
61  rc = FileReadTest();
62  rc = uSleepTest();
63  appState.setRunning(false);
64  return rc;
65 }
66 
68 {
69  struct pwm_input_s pwm;
70  struct sensor_combined_s sc;
71 
72  memset(&pwm, 0, sizeof(pwm_input_s));
73  memset(&sc, 0, sizeof(sensor_combined_s));
74  PX4_WARN("Successful after memset... ");
75  orb_advert_t pub_fd = orb_advertise(ORB_ID(pwm_input), &pwm);
76 
77  if (pub_fd == nullptr) {
78  PX4_WARN("Error: advertizing pwm_input topic");
79  return -1;
80  }
81 
82  orb_advert_t pub_sc = orb_advertise(ORB_ID(sensor_combined), &sc);
83 
84  if (pub_sc == nullptr) {
85  PX4_WARN("Error: advertizing sensor_combined topic");
86  return -1;
87  }
88 
89  int i = 0;
90  pwm.error_count++;
91  /*sc.gyro_errcount[i]++;*/ // no member named 'gyro_errcount' in 'sensor_combined_s'
92 
93  while (!appState.exitRequested() && i < 10) {
94 
95  PX4_INFO(" Doing work...");
96  orb_publish(ORB_ID(pwm_input), pub_fd, &pwm);
97  orb_publish(ORB_ID(sensor_combined), pub_sc, &sc);
98 
99  usleep(1000000);
100  ++i;
101  }
102 
103  return 0;
104 }
105 
107 {
108  int i = 0;
109  orb_advert_t pub_id_esc_status = orb_advertise(ORB_ID(esc_status), & m_esc_status);
110 
111  if (pub_id_esc_status == 0) {
112  PX4_ERR("error publishing esc_status");
113  return -1;
114  }
115 
116  if (orb_publish(ORB_ID(esc_status), pub_id_esc_status, &m_esc_status) == PX4_ERROR) {
117  PX4_ERR("[%d]Error publishing the esc_status message", i);
118  return -1;
119  }
120 
121  int sub_vc = orb_subscribe(ORB_ID(vehicle_command));
122 
123  if (sub_vc == PX4_ERROR) {
124  PX4_ERR("Error subscribing to vehicle_command topic");
125  return -1;
126  }
127 
128  while (!appState.exitRequested()) {
129 
130  PX4_DEBUG("[%d] Doing work...", i);
131  bool updated = false;
132 
133  if (orb_check(sub_vc, &updated) == 0) {
134  if (updated) {
135  PX4_WARN("[%d]vechile command status is updated... reading new value", i);
136 
137  if (orb_copy(ORB_ID(vehicle_command), sub_vc, &m_vc) != 0) {
138  PX4_ERR("[%d]Error calling orb copy for vechicle... ", i);
139  break;
140  }
141 
142  if (orb_publish(ORB_ID(esc_status), pub_id_esc_status, &m_esc_status) == PX4_ERROR) {
143  PX4_ERR("[%d]Error publishing the esc_status message", i);
144  break;
145  }
146 
147  } else {
148  PX4_WARN("[%d] vechicle command topic is not updated", i);
149  }
150 
151  } else {
152  PX4_ERR("[%d]Error checking the updated status for vehicle command ", i);
153  break;
154  }
155 
156  // sleep for 1 sec.
157  usleep(1000000);
158 
159  ++i;
160  }
161 
162  return 0;
163 }
164 
166 {
167  PX4_WARN("before usleep for 1 sec [%" PRIu64 "]", hrt_absolute_time());
168  usleep(1000000);
169  PX4_INFO("After usleep for 1 sec [%" PRIu64 "]", hrt_absolute_time());
170 
171  for (int i = 0; i < 10; ++i) {
172  PX4_INFO("In While Loop: B4 Sleep for[%d] seconds [%" PRIu64 "]", i + 1, hrt_absolute_time());
173  usleep((i + 1) * 1000000);
174  PX4_INFO("In While Loop: After Sleep for[%d] seconds [%" PRIu64 "]", i + 1, hrt_absolute_time());
175  }
176 
177  PX4_INFO("exiting sleep test...");
178  return 0;
179 }
180 
182 {
183  int rc = OK;
184  static const char TEST_FILE_PATH[] = "./test.txt";
185  FILE *fp;
186  char *line = NULL;
187  /*size_t len = 0;
188  ssize_t read;*/
189 
190  fp = fopen(TEST_FILE_PATH, "r");
191 
192  if (fp == NULL) {
193  PX4_WARN("unable to open file[%s] for reading", TEST_FILE_PATH);
194  rc = PX4_ERROR;
195 
196  } else {
197  /*
198  int i = 0;
199  while( ( read = getline( &line, &len, fp ) ) != -1 )
200  {
201  ++i;
202  PX4_INFO( "LineNum[%d] LineLength[%d]", i, len );
203  PX4_INFO( "LineNum[%d] Line[%s]", i, line );
204  }
205  */
206  PX4_INFO("Successfully opened file [%s]", TEST_FILE_PATH);
207  fclose(fp);
208 
209  if (line != NULL) {
210  free(line);
211  }
212  }
213 
214  return rc;
215 }
struct vehicle_command_s m_vc
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
API for the uORB lightweight object broker.
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
Definition: uORB.cpp:43
High-resolution timer with callouts and timekeeping.
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
#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
uint64_t error_count
Definition: pwm_input.h:54
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
Definition: uORB.cpp:70
int orb_check(int handle, bool *updated)
Definition: uORB.cpp:95
#define OK
Definition: uavcan_main.cpp:71
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).