PX4 Firmware
PX4 Autopilot Software http://px4.io
lsm303d_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-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 /**
35  * @file lsm303d_main.cpp
36  * Driver for the ST LSM303D MEMS accelerometer / magnetometer connected via SPI.
37  */
38 
39 #include "LSM303D.hpp"
40 
41 #include <px4_platform_common/getopt.h>
42 
43 /**
44  * Local functions in support of the shell command.
45  */
46 namespace lsm303d
47 {
48 
50 
51 void start(bool external_bus, enum Rotation rotation, unsigned range);
52 void info();
53 void usage();
54 
55 /**
56  * Start the driver.
57  *
58  * This function call only returns once the driver is
59  * up and running or failed to detect the sensor.
60  */
61 void
62 start(bool external_bus, enum Rotation rotation, unsigned range)
63 {
64  if (g_dev != nullptr) {
65  errx(0, "already started");
66  }
67 
68  /* create the driver */
69  if (external_bus) {
70 #if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_ACCEL_MAG)
71  g_dev = new LSM303D(PX4_SPI_BUS_EXT, PX4_SPIDEV_EXT_ACCEL_MAG, rotation);
72 #else
73  errx(0, "External SPI not available");
74 #endif
75 
76  } else {
77  g_dev = new LSM303D(PX4_SPI_BUS_SENSORS, PX4_SPIDEV_ACCEL_MAG, rotation);
78  }
79 
80  if (g_dev == nullptr) {
81  PX4_ERR("failed instantiating LSM303D obj");
82  goto fail;
83  }
84 
85  if (OK != g_dev->init()) {
86  goto fail;
87  }
88 
89  exit(0);
90 fail:
91 
92  if (g_dev != nullptr) {
93  delete g_dev;
94  g_dev = nullptr;
95  }
96 
97  errx(1, "driver start failed");
98 }
99 
100 /**
101  * Print a little info about the driver.
102  */
103 void
105 {
106  if (g_dev == nullptr) {
107  errx(1, "driver not running\n");
108  }
109 
110  printf("state @ %p\n", g_dev);
111  g_dev->print_info();
112 
113  exit(0);
114 }
115 
116 void
118 {
119  PX4_INFO("missing command: try 'start', 'info'");
120  PX4_INFO("options:");
121  PX4_INFO(" -X (external bus)");
122  PX4_INFO(" -R rotation");
123 }
124 
125 } // namespace
126 
127 int
128 lsm303d_main(int argc, char *argv[])
129 {
130  bool external_bus = false;
131  enum Rotation rotation = ROTATION_NONE;
132  int accel_range = 8;
133 
134  int myoptind = 1;
135  int ch;
136  const char *myoptarg = nullptr;
137 
138  /* jump over start/off/etc and look at options first */
139  while ((ch = px4_getopt(argc, argv, "XR:a:", &myoptind, &myoptarg)) != EOF) {
140  switch (ch) {
141  case 'X':
142  external_bus = true;
143  break;
144 
145  case 'R':
146  rotation = (enum Rotation)atoi(myoptarg);
147  break;
148 
149  case 'a':
150  accel_range = atoi(myoptarg);
151  break;
152 
153  default:
154  lsm303d::usage();
155  exit(0);
156  }
157  }
158 
159  if (myoptind >= argc) {
160  lsm303d::usage();
161  exit(0);
162  }
163 
164  const char *verb = argv[myoptind];
165 
166  /*
167  * Start/load the driver.
168 
169  */
170  if (!strcmp(verb, "start")) {
171  lsm303d::start(external_bus, rotation, accel_range);
172  }
173 
174  /*
175  * Print driver information.
176  */
177  if (!strcmp(verb, "info")) {
178  lsm303d::info();
179  }
180 
181  errx(1, "unrecognized command, try 'start', info'");
182 }
void start(bool external_bus, enum Rotation rotation, unsigned range)
Start the driver.
Local functions in support of the shell command.
int init() override
Definition: LSM303D.cpp:86
void info()
Print a little info about the driver.
int lsm303d_main(int argc, char *argv[])
void print_info()
Diagnostics - print some basic information about the driver.
Definition: LSM303D.cpp:585
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
void usage()
Prints info about the driver argument usage.
#define errx(eval,...)
Definition: err.h:89
Driver for the ST LSM303D MEMS accelerometer / magnetometer connected via SPI.
#define OK
Definition: uavcan_main.cpp:71
LSM303D * g_dev