PX4 Firmware
PX4 Autopilot Software http://px4.io
tfmini_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017-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 "TFMINI.hpp"
35 
36 #include <px4_platform_common/getopt.h>
37 
38 /**
39  * Local functions in support of the shell command.
40  */
41 namespace tfmini
42 {
43 
44 TFMINI *g_dev{nullptr};
45 
46 int start(const char *port, uint8_t rotation);
47 int status();
48 int stop();
49 int usage();
50 
51 int
52 start(const char *port, uint8_t rotation)
53 {
54  if (g_dev != nullptr) {
55  PX4_ERR("already started");
56  return PX4_OK;
57  }
58 
59  // Instantiate the driver.
60  g_dev = new TFMINI(port, rotation);
61 
62  if (g_dev == nullptr) {
63  PX4_ERR("driver start failed");
64  return PX4_ERROR;
65  }
66 
67  if (OK != g_dev->init()) {
68  PX4_ERR("driver start failed");
69  delete g_dev;
70  g_dev = nullptr;
71  return PX4_ERROR;
72  }
73 
74  return PX4_OK;
75 }
76 
77 int
79 {
80  if (g_dev == nullptr) {
81  PX4_ERR("driver not running");
82  return 1;
83  }
84 
85  printf("state @ %p\n", g_dev);
86  g_dev->print_info();
87 
88  return 0;
89 }
90 
91 int stop()
92 {
93  if (g_dev != nullptr) {
94  PX4_INFO("stopping driver");
95  delete g_dev;
96  g_dev = nullptr;
97  PX4_INFO("driver stopped");
98 
99  } else {
100  PX4_ERR("driver not running");
101  return 1;
102  }
103 
104  return PX4_OK;
105 }
106 
107 int
109 {
110  PRINT_MODULE_DESCRIPTION(
111  R"DESCR_STR(
112 ### Description
113 
114 Serial bus driver for the Benewake TFmini LiDAR.
115 
116 Most boards are configured to enable/start the driver on a specified UART using the SENS_TFMINI_CFG parameter.
117 
118 Setup/usage information: https://docs.px4.io/en/sensor/tfmini.html
119 
120 ### Examples
121 
122 Attempt to start driver on a specified serial device.
123 $ tfmini start -d /dev/ttyS1
124 Stop driver
125 $ tfmini stop
126 )DESCR_STR");
127 
128  PRINT_MODULE_USAGE_NAME("tfmini", "driver");
129  PRINT_MODULE_USAGE_SUBCATEGORY("distance_sensor");
130  PRINT_MODULE_USAGE_COMMAND_DESCR("start","Start driver");
131  PRINT_MODULE_USAGE_PARAM_STRING('d', nullptr, nullptr, "Serial device", false);
132  PRINT_MODULE_USAGE_PARAM_INT('R', 25, 1, 25, "Sensor rotation - downward facing by default", true);
133  PRINT_MODULE_USAGE_COMMAND_DESCR("status","Driver status");
134  PRINT_MODULE_USAGE_COMMAND_DESCR("stop","Stop driver");
135  PRINT_MODULE_USAGE_COMMAND_DESCR("test","Test driver (basic functional tests)");
136  PRINT_MODULE_USAGE_COMMAND_DESCR("status","Print driver status");
137  return PX4_OK;
138 }
139 
140 } // namespace
141 
142 extern "C" __EXPORT int tfmini_main(int argc, char *argv[])
143 {
144  int ch = 0;
145  uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING;
146  const char *device_path = TFMINI_DEFAULT_PORT;
147  int myoptind = 1;
148  const char *myoptarg = nullptr;
149 
150  while ((ch = px4_getopt(argc, argv, "R:d:", &myoptind, &myoptarg)) != EOF) {
151  switch (ch) {
152  case 'R':
153  rotation = (uint8_t)atoi(myoptarg);
154  break;
155 
156  case 'd':
157  device_path = myoptarg;
158  break;
159 
160  default:
161  PX4_WARN("Unknown option!");
162  return PX4_ERROR;
163  }
164  }
165 
166  if (myoptind >= argc) {
167  PX4_ERR("unrecognized command");
168  return tfmini::usage();
169  }
170 
171  if (!strcmp(argv[myoptind], "start")) {
172  if (strcmp(device_path, "") != 0) {
173  return tfmini::start(device_path, rotation);
174 
175  } else {
176  PX4_WARN("Please specify device path!");
177  return tfmini::usage();
178  }
179 
180  } else if (!strcmp(argv[myoptind], "stop")) {
181  return tfmini::stop();
182 
183  } else if (!strcmp(argv[myoptind], "status")) {
184  return tfmini::status();
185  }
186 
187  return tfmini::usage();
188 }
int usage()
Prints info about the driver argument usage.
Definition: I2C.hpp:51
int init()
Definition: TFMINI.cpp:57
#define TFMINI_DEFAULT_PORT
Definition: TFMINI.hpp:59
int status()
Definition: tfmini_main.cpp:78
__EXPORT int tfmini_main(int argc, char *argv[])
Local functions in support of the shell command.
Definition: tfmini_main.cpp:41
TFMINI * g_dev
Definition: tfmini_main.cpp:44
int stop()
Stop the driver.
Definition: tfmini_main.cpp:91
#define OK
Definition: uavcan_main.cpp:71
int start(const char *port, uint8_t rotation)
Definition: tfmini_main.cpp:52
void print_info()
Definition: TFMINI.cpp:255