PX4 Firmware
PX4 Autopilot Software http://px4.io
adis16477_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-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 "ADIS16477.hpp"
35 
36 #include <px4_platform_common/getopt.h>
37 
38 namespace adis16477
39 {
40 ADIS16477 *g_dev{nullptr};
41 
42 static int start(enum Rotation rotation)
43 {
44  if (g_dev != nullptr) {
45  PX4_WARN("already started");
46  return 0;
47  }
48 
49  // create the driver
50 #if defined(PX4_SPIDEV_ADIS16477)
51  g_dev = new ADIS16477(PX4_SPI_BUS_SENSOR1, PX4_SPIDEV_ADIS16477, rotation);
52 #elif defined(PX4_SPI_BUS_EXT)
53  g_dev = new ADIS16477(PX4_SPI_BUS_EXT, PX4_SPIDEV_EXT_MPU, rotation);
54 #elif defined(PX4_SPIDEV_EXTERNAL1_1)
55  g_dev = new ADIS16477(PX4_SPI_BUS_EXTERNAL1, PX4_SPIDEV_EXTERNAL1_1, rotation);
56 #else
57  PX4_ERR("External SPI not available");
58  return -1;
59 #endif
60 
61  if (g_dev == nullptr) {
62  PX4_ERR("driver start failed");
63  return -1;
64  }
65 
66  if (g_dev->init() != PX4_OK) {
67  PX4_ERR("driver init failed");
68  delete g_dev;
69  g_dev = nullptr;
70  return -1;
71  }
72 
73  return 0;
74 }
75 
76 static int stop()
77 {
78  if (g_dev == nullptr) {
79  PX4_WARN("driver not running");
80  return -1;
81  }
82 
83  delete g_dev;
84  g_dev = nullptr;
85 
86  return 0;
87 }
88 
89 static int status()
90 {
91  if (g_dev == nullptr) {
92  PX4_INFO("driver not running");
93  return 0;
94  }
95 
96  g_dev->print_info();
97 
98  return 0;
99 }
100 
101 static int usage()
102 {
103  PX4_INFO("missing command: try 'start', 'stop', 'status'");
104  PX4_INFO("options:");
105  PX4_INFO(" -R rotation");
106 
107  return 0;
108 }
109 
110 } // namespace adis16477
111 
112 extern "C" int adis16477_main(int argc, char *argv[])
113 {
114  enum Rotation rotation = ROTATION_NONE;
115  int myoptind = 1;
116  int ch = 0;
117  const char *myoptarg = nullptr;
118 
119  // start options
120  while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) {
121  switch (ch) {
122  case 'R':
123  rotation = (enum Rotation)atoi(myoptarg);
124  break;
125 
126  default:
127  return adis16477::usage();
128  }
129  }
130 
131  const char *verb = argv[myoptind];
132 
133  if (!strcmp(verb, "start")) {
134  return adis16477::start(rotation);
135 
136  } else if (!strcmp(verb, "stop")) {
137  return adis16477::stop();
138 
139  } else if (!strcmp(verb, "status")) {
140  return adis16477::status();
141  }
142 
143  return adis16477::usage();
144 }
static int start(enum Rotation rotation)
void print_info()
Definition: ADIS16477.cpp:389
int adis16477_main(int argc, char *argv[])
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
ADIS16477 * g_dev
static int stop()
static int usage()
static int status()
virtual int init()
Definition: ADIS16477.cpp:89