36 #include <px4_platform_common/tasks.h> 37 #include <px4_platform_common/getopt.h> 38 #include <px4_platform_common/posix.h> 50 #include <lib/mixer/mixer_multirotor_normalized.generated.h> 64 #define MAX_LEN_DEV_PATH 32 68 #define UART_ESC_MAX_MOTORS 4 109 static void task_main(
int argc,
char *argv[]);
114 static int mixer_control_callback(uintptr_t handle, uint8_t control_group, uint8_t control_index,
float &input);
141 sprintf(nbuf,
"UART_ESC_MOTOR%d", i + 1);
150 PX4_WARN(
"uart_esc_main parameters_update");
160 PX4_WARN(
"UART_ESC_BAUD %d",
_parameters.baudrate);
166 PX4_WARN(
"UART_ESC_MOTOR%d %d", i + 1,
_parameters.px4_motor_mapping[i]);
172 uint8_t control_group,
173 uint8_t control_index,
178 input = controls[control_group].
control[control_index];
199 int mixer_initialized = -1;
202 unsigned int buflen =
sizeof(buf);
204 PX4_INFO(
"Initializing mixer from config file in %s", mixer_filename);
206 int fd_load = open(mixer_filename, O_RDONLY);
209 int nRead =
read(fd_load, buf, buflen);
215 if (mixer !=
nullptr) {
216 PX4_INFO(
"Successfully initialized mixer from config file");
217 mixer_initialized = 0;
220 PX4_WARN(
"Unable to parse from mixer config file");
224 PX4_WARN(
"Unable to read from mixer config file");
228 PX4_WARN(
"Unable to open mixer config file");
233 if (mixer_initialized < 0) {
234 float roll_scale = 1;
235 float pitch_scale = 1;
240 MultirotorGeometry::QUAD_X,
241 roll_scale, pitch_scale, yaw_scale, deadband);
243 if (mixer ==
nullptr) {
244 PX4_ERR(
"mixer initialization failed");
245 mixer_initialized = -1;
246 return mixer_initialized;
249 PX4_WARN(
"mixer config file not found, successfully initialized default quad x mixer");
250 mixer_initialized = 0;
253 return mixer_initialized;
266 for (i = 0; i < num_rotors; i++) {
267 motor_rpm_copy[i] = motor_rpm[i];
270 for (i = 0; i < num_rotors; i++) {
271 motor_rpm[
_parameters.px4_motor_mapping[i] - 1] = motor_rpm_copy[i];
277 PX4_INFO(
"enter uart_esc task_main");
279 _outputs_pub =
nullptr;
283 esc = UartEsc::get_instance();
286 PX4_ERR(
"failed to new UartEsc instance");
288 }
else if (esc->initialize((
enum esc_model_t)
_parameters.model,
290 PX4_ERR(
"failed to initialize UartEsc");
299 memset(&_outputs, 0,
sizeof(_outputs));
302 px4_pollfd_struct_t fds[1];
304 fds[0].events = POLLIN;
309 PX4_ERR(
"Mixer initialization failed.");
310 _task_should_exit =
true;
314 while (!_task_should_exit) {
315 int pret =
px4_poll(&fds[0], (
sizeof(fds) /
sizeof(fds[0])), 100);
324 PX4_WARN(
"poll error %d, %d", pret, errno);
331 if (fds[0].revents & POLLIN) {
340 _outputs.
noutputs = mixer->
mix(&_outputs.
output[0], actuator_controls_s::NUM_ACTUATOR_CONTROLS);
344 PX4_ERR(
"Unsupported motors %d, up to %d motors supported",
350 for (
unsigned outIdx = 0; outIdx < _outputs.
noutputs; outIdx++) {
352 motor_rpms[outIdx] = (int16_t)(((_outputs.
output[outIdx] + 1.0) / 2.0) *
353 (esc->max_rpm() - esc->min_rpm()) + esc->min_rpm());
361 for (
unsigned outIdx = 0; outIdx < _outputs.
noutputs; outIdx++) {
362 motor_rpms[outIdx] = 0;
363 _outputs.
output[outIdx] = -1.0;
367 esc->send_rpms(&motor_rpms[0], _outputs.
noutputs);
383 if (_outputs_pub !=
nullptr) {
392 bool updated =
false;
397 PX4_DEBUG(
"arming status updated, _armed.armed: %d", _armed.
armed);
411 PX4_WARN(
"closing uart_esc");
419 PX4_WARN(
"task_main_trampoline");
425 ASSERT(_task_handle == -1);
428 _task_handle = px4_task_spawn_cmd(
"uart_esc_main",
435 if (_task_handle < 0) {
436 warn(
"task start failed");
453 PX4_WARN(
"missing command: try 'start', 'stop', 'status'");
454 PX4_WARN(
"options:");
455 PX4_WARN(
" -D device");
462 const char *
device = NULL;
465 const char *myoptarg = NULL;
467 while ((ch = px4_getopt(argc, argv,
"D:", &myoptind, &myoptarg)) != EOF) {
480 if (device == NULL || strlen(device) == 0) {
488 const char *verb = argv[myoptind];
493 if (!strcmp(verb,
"start")) {
495 PX4_WARN(
"uart_esc already running");
502 else if (!strcmp(verb,
"stop")) {
504 PX4_WARN(
"uart_esc is not running");
511 else if (!strcmp(verb,
"status")) {
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
struct uart_esc::@19 _parameter_handles
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
API for the uORB lightweight object broker.
static int initialize_mixer(const char *mixer_filename)
static MultirotorMixer * from_text(Mixer::ControlCallback control_cb, uintptr_t cb_handle, const char *buf, unsigned &buflen)
Factory method.
static void stop()
uart_esc stop
static char _device[MAX_LEN_DEV_PATH]
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
Namespace encapsulating all device framework classes, functions and data.
High-resolution timer with callouts and timekeeping.
Global flash based parameter store.
static MultirotorMixer * mixer
mixer initialization
static void read(bootloader_app_shared_t *pshared)
int orb_subscribe(const struct orb_metadata *meta)
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
static void start(const char *device) __attribute__((unused))
uart_esc start
static void task_main_trampoline(int argc, char *argv[])
task main trampoline function
actuator_outputs_s _outputs
static const char * MIXER_FILENAME
void uart_esc_rotate_motors(int16_t *motor_rpm, int num_rotors)
Rotate the motor rpm values based on the motor mappings configuration stored in motor_mapping.
static px4_task_t _task_handle
static void usage()
Print out the usage information.
struct __attribute__((__packed__)) reading_msg
__EXPORT int uart_esc_main(int argc, char *argv[])
driver 'main' command
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
orb_advert_t _outputs_pub
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
int px4_motor_mapping[UART_ESC_MAX_MOTORS]
actuator_controls_s _controls
#define UART_ESC_MAX_MOTORS
struct uart_esc::@18 _parameters
unsigned mix(float *outputs, unsigned space) override
Perform the mixing function.
static void parameters_init()
int orb_check(int handle, bool *updated)
volatile bool _task_should_exit
Multi-rotor mixer for pre-defined vehicle geometries.
static int mixer_control_callback(uintptr_t handle, uint8_t control_group, uint8_t control_index, float &input)
static void parameters_update()
static void task_main(int argc, char *argv[])
uart_esc thread primary entry point
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
parameter_update_s _param_update