PX4 Firmware
PX4 Autopilot Software http://px4.io
roboclaw_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013 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 
36 /**
37  * @file roboclaw_main.cpp
38  *
39  * RoboClaw Motor Driver
40  *
41  * references:
42  * http://downloads.ionmc.com/docs/roboclaw_user_manual.pdf
43  *
44  */
45 
46 #include <px4_platform_common/px4_config.h>
47 #include <px4_platform_common/log.h>
48 #include <px4_platform_common/module.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <math.h>
54 
55 #include <parameters/param.h>
56 
57 #include <arch/board/board.h>
58 #include "RoboClaw.hpp"
59 
60 static bool thread_running = false; /**< Deamon status flag */
61 px4_task_t deamon_task;
62 
63 /**
64  * Deamon management function.
65  */
66 extern "C" __EXPORT int roboclaw_main(int argc, char *argv[]);
67 
68 /**
69  * Mainloop of deamon.
70  */
71 int roboclaw_thread_main(int argc, char *argv[]);
72 
73 /**
74  * Print the correct usage.
75  */
76 static void usage();
77 
78 static void usage()
79 {
80  PRINT_MODULE_USAGE_NAME("roboclaw", "driver");
81 
82  PRINT_MODULE_DESCRIPTION(R"DESCR_STR(
83 ### Description
84 
85 This driver communicates over UART with the [Roboclaw motor driver](http://downloads.ionmc.com/docs/roboclaw_user_manual.pdf).
86 It performs two tasks:
87 
88  - Control the motors based on the `actuator_controls_0` UOrb topic.
89  - Read the wheel encoders and publish the raw data in the `wheel_encoders` UOrb topic
90 
91 In order to use this driver, the Roboclaw should be put into Packet Serial mode (see the linked documentation), and
92 your flight controller's UART port should be connected to the Roboclaw as shown in the documentation. For Pixhawk 4,
93 use the `UART & I2C B` port, which corresponds to `/dev/ttyS3`.
94 
95 ### Implementation
96 
97 The main loop of this module (Located in `RoboClaw.cpp::task_main()`) performs 2 tasks:
98 
99  1. Write `actuator_controls_0` messages to the Roboclaw as they become available
100  2. Read encoder data from the Roboclaw at a constant, fixed rate.
101 
102 Because of the latency of UART, this driver does not write every single `actuator_controls_0` message to the Roboclaw
103 immediately. Instead, it is rate limited based on the parameter `RBCLW_WRITE_PER`.
104 
105 On startup, this driver will attempt to read the status of the Roboclaw to verify that it is connected. If this fails,
106 the driver terminates immediately.
107 
108 ### Examples
109 
110 The command to start this driver is:
111 
112  $ roboclaw start <device> <baud>
113 
114 `<device>` is the name of the UART port. On the Pixhawk 4, this is `/dev/ttyS3`.
115 `<baud>` is te baud rate.
116 
117 All available commands are:
118 
119  - `$ roboclaw start <device> <baud>`
120  - `$ roboclaw status`
121  - `$ roboclaw stop`
122  )DESCR_STR");
123 }
124 
125 /**
126  * The deamon app only briefly exists to start
127  * the background job. The stack size assigned in the
128  * Makefile does only apply to this management task.
129  *
130  * The actual stack size should be set in the call
131  * to task_create().
132  */
133 int roboclaw_main(int argc, char *argv[])
134 {
135 
136  if (argc < 4) {
137  usage();
138  }
139 
140  if (!strcmp(argv[1], "start")) {
141 
142  if (thread_running) {
143  printf("roboclaw already running\n");
144  /* this is not an error */
145  return 0;
146  }
147 
148  RoboClaw::taskShouldExit = false;
149  deamon_task = px4_task_spawn_cmd("roboclaw",
150  SCHED_DEFAULT,
151  SCHED_PRIORITY_MAX - 10,
152  2000,
154  (char *const *)argv);
155  return 0;
156 
157  } else if (!strcmp(argv[1], "stop")) {
158 
160  return 0;
161 
162  } else if (!strcmp(argv[1], "status")) {
163 
164  if (thread_running) {
165  printf("\troboclaw app is running\n");
166 
167  } else {
168  printf("\troboclaw app not started\n");
169  }
170 
171  return 0;
172  }
173 
174  usage();
175  return 1;
176 }
177 
178 int roboclaw_thread_main(int argc, char *argv[])
179 {
180  printf("[roboclaw] starting\n");
181 
182  // skip parent process args
183  argc -= 2;
184  argv += 2;
185 
186  if (argc < 2) {
187  printf("usage: roboclaw start <device> <baud>\n");
188  return -1;
189  }
190 
191  const char *deviceName = argv[1];
192  const char *baudRate = argv[2];
193 
194  // start
195  RoboClaw roboclaw(deviceName, baudRate);
196 
197  thread_running = true;
198 
199  roboclaw.taskMain();
200 
201  // exit
202  printf("[roboclaw] exiting.\n");
203  thread_running = false;
204  return 0;
205 }
int roboclaw_thread_main(int argc, char *argv[])
Mainloop of deamon.
static bool taskShouldExit
Definition: RoboClaw.hpp:67
Definition: I2C.hpp:51
px4_task_t deamon_task
static bool thread_running
Deamon status flag.
Global flash based parameter store.
static void usage()
Print the correct usage.
__EXPORT int roboclaw_main(int argc, char *argv[])
Deamon management function.
This is a driver for the RoboClaw motor controller.
Definition: RoboClaw.hpp:62
void taskMain()
Definition: RoboClaw.cpp:135