2012-08-04 19:12:36 -03:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2012-08-05 17:43:16 -03:00
|
|
|
* @file fmu.cpp
|
|
|
|
*
|
|
|
|
* Driver/configurator for the PX4 FMU multi-purpose port.
|
2012-08-04 19:12:36 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2012-08-05 00:05:47 -03:00
|
|
|
#include <stdlib.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
|
|
|
#include <drivers/device/device.h>
|
|
|
|
#include <drivers/drv_pwm_output.h>
|
|
|
|
#include <drivers/drv_gpio.h>
|
2012-08-10 04:30:40 -03:00
|
|
|
|
|
|
|
#include <systemlib/mixer/mixer.h>
|
2012-08-05 00:05:47 -03:00
|
|
|
#include <drivers/drv_mixer.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
#include <arch/board/up_pwm_servo.h>
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <uORB/topics/actuator_controls.h>
|
2012-08-22 00:14:43 -03:00
|
|
|
#include <uORB/topics/actuator_outputs.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
|
|
|
|
class FMUServo : public device::CDev
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
MODE_2PWM,
|
|
|
|
MODE_4PWM,
|
|
|
|
MODE_NONE
|
|
|
|
};
|
|
|
|
FMUServo(Mode mode);
|
|
|
|
~FMUServo();
|
|
|
|
|
|
|
|
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
|
|
|
|
|
|
|
|
virtual int init();
|
|
|
|
|
|
|
|
private:
|
2012-08-05 00:05:47 -03:00
|
|
|
static const unsigned _max_actuators = 4;
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
Mode _mode;
|
|
|
|
int _task;
|
|
|
|
int _t_actuators;
|
|
|
|
int _t_armed;
|
2012-08-22 03:44:22 -03:00
|
|
|
orb_advert_t _t_outputs;
|
2012-08-04 19:12:36 -03:00
|
|
|
unsigned _num_outputs;
|
|
|
|
|
|
|
|
volatile bool _task_should_exit;
|
|
|
|
bool _armed;
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
MixerGroup *_mixers;
|
|
|
|
|
|
|
|
actuator_controls_s _controls;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
static void task_main_trampoline(int argc, char *argv[]);
|
2012-08-22 00:14:43 -03:00
|
|
|
void task_main() __attribute__((noreturn));
|
2012-08-10 04:30:40 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
static int control_callback(uintptr_t handle,
|
2012-08-14 12:47:04 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input);
|
2012-08-04 19:12:36 -03:00
|
|
|
};
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
namespace
|
|
|
|
{
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
FMUServo *g_servo;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
FMUServo::FMUServo(Mode mode) :
|
|
|
|
CDev("fmuservo", PWM_OUTPUT_DEVICE_PATH),
|
|
|
|
_mode(mode),
|
|
|
|
_task(-1),
|
|
|
|
_t_actuators(-1),
|
|
|
|
_t_armed(-1),
|
|
|
|
_task_should_exit(false),
|
2012-08-10 04:30:40 -03:00
|
|
|
_armed(false),
|
|
|
|
_mixers(nullptr)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FMUServo::~FMUServo()
|
|
|
|
{
|
|
|
|
if (_task != -1) {
|
2012-08-05 06:09:11 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* task should wake up every 100ms or so at least */
|
|
|
|
_task_should_exit = true;
|
|
|
|
|
|
|
|
unsigned i = 0;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
do {
|
|
|
|
/* wait 20ms */
|
|
|
|
usleep(20000);
|
|
|
|
|
|
|
|
/* if we have given up, kill it */
|
|
|
|
if (++i > 10) {
|
|
|
|
task_delete(_task);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (_task != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_servo = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FMUServo::init()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ASSERT(_task == -1);
|
|
|
|
|
|
|
|
/* do regular cdev init */
|
|
|
|
ret = CDev::init();
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* start the IO interface task */
|
|
|
|
_task = task_create("fmuservo", SCHED_PRIORITY_DEFAULT, 1024, (main_t)&FMUServo::task_main_trampoline, nullptr);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (_task < 0) {
|
|
|
|
debug("task start failed: %d", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FMUServo::task_main_trampoline(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_servo->task_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FMUServo::task_main()
|
|
|
|
{
|
|
|
|
/* configure for PWM output */
|
|
|
|
switch (_mode) {
|
|
|
|
case MODE_2PWM:
|
|
|
|
/* multi-port with flow control lines as PWM */
|
|
|
|
/* XXX magic numbers */
|
|
|
|
up_pwm_servo_init(0x3);
|
|
|
|
break;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case MODE_4PWM:
|
|
|
|
/* multi-port as 4 PWM outs */
|
|
|
|
/* XXX magic numbers */
|
|
|
|
up_pwm_servo_init(0xf);
|
|
|
|
break;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case MODE_NONE:
|
|
|
|
/* we should never get here... */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* subscribe to objects that we are interested in watching */
|
2012-08-05 17:43:16 -03:00
|
|
|
_t_actuators = orb_subscribe(ORB_ID_VEHICLE_ATTITUDE_CONTROLS);
|
2012-08-04 19:12:36 -03:00
|
|
|
orb_set_interval(_t_actuators, 20); /* 50Hz update rate */
|
|
|
|
|
|
|
|
_t_armed = orb_subscribe(ORB_ID(actuator_armed));
|
|
|
|
orb_set_interval(_t_armed, 100); /* 10Hz update rate */
|
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
/* advertise the mixed control outputs */
|
|
|
|
struct actuator_output_s outputs;
|
|
|
|
memset(&outputs, 0, sizeof(outputs));
|
|
|
|
_t_outputs = orb_advertise(ORB_ID_VEHICLE_CONTROLS, &outputs);
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
struct pollfd fds[2];
|
|
|
|
fds[0].fd = _t_actuators;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
fds[1].fd = _t_armed;
|
|
|
|
fds[1].events = POLLIN;
|
|
|
|
|
|
|
|
unsigned num_outputs = (_mode == MODE_2PWM) ? 2 : 4;
|
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
log("starting");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* loop until killed */
|
|
|
|
while (!_task_should_exit) {
|
|
|
|
|
|
|
|
/* sleep waiting for data, but no more than 100ms */
|
2012-08-11 14:34:02 -03:00
|
|
|
int ret = ::poll(&fds[0], 2, 1000);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* this would be bad... */
|
|
|
|
if (ret < 0) {
|
|
|
|
log("poll error %d", errno);
|
|
|
|
usleep(1000000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do we have a control update? */
|
|
|
|
if (fds[0].revents & POLLIN) {
|
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* get controls - must always do this to avoid spinning */
|
2012-08-10 04:30:40 -03:00
|
|
|
orb_copy(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, _t_actuators, &_controls);
|
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* can we mix? */
|
|
|
|
if (_mixers != nullptr) {
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* do mixing */
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers->mix(&outputs.output[0], num_outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-11 14:34:02 -03:00
|
|
|
/* iterate actuators */
|
|
|
|
for (unsigned i = 0; i < num_outputs; i++) {
|
|
|
|
|
|
|
|
/* scale for PWM output 900 - 2100us */
|
2012-08-22 00:14:43 -03:00
|
|
|
outputs.output[i] = 1500 + (600 * outputs.output[i]);
|
|
|
|
|
|
|
|
/* output to the servo */
|
|
|
|
up_pwm_servo_set(i, outputs.output[i]);
|
2012-08-11 14:34:02 -03:00
|
|
|
}
|
2012-08-22 00:14:43 -03:00
|
|
|
|
|
|
|
/* and publish for anyone that cares to see */
|
|
|
|
orb_publish(ORB_ID_VEHICLE_CONTROLS, _t_outputs, &outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how about an arming update? */
|
|
|
|
if (fds[1].revents & POLLIN) {
|
2012-08-05 06:09:11 -03:00
|
|
|
struct actuator_armed_s aa;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* get new value */
|
|
|
|
orb_copy(ORB_ID(actuator_armed), _t_armed, &aa);
|
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
/* update PWM servo armed status */
|
2012-08-04 19:12:36 -03:00
|
|
|
up_pwm_servo_arm(aa.armed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::close(_t_actuators);
|
|
|
|
::close(_t_armed);
|
2012-08-22 00:14:43 -03:00
|
|
|
::close(_t_outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* make sure servos are off */
|
2012-08-05 17:43:16 -03:00
|
|
|
up_pwm_servo_deinit();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
log("stopping");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* note - someone else is responsible for restoring the GPIO config */
|
|
|
|
|
|
|
|
/* tell the dtor that we are exiting */
|
|
|
|
_task = -1;
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
int
|
2012-08-22 00:14:43 -03:00
|
|
|
FMUServo::control_callback(uintptr_t handle,
|
2012-08-10 04:30:40 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input)
|
|
|
|
{
|
2012-08-22 00:14:43 -03:00
|
|
|
const actuator_controls_s *controls = (actuator_controls_s *)handle;
|
2012-08-10 04:30:40 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
input = controls->control[control_index];
|
2012-08-10 04:30:40 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
int
|
|
|
|
FMUServo::ioctl(struct file *filp, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
2012-08-05 00:05:47 -03:00
|
|
|
int channel;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case PWM_SERVO_ARM:
|
|
|
|
up_pwm_servo_arm(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_DISARM:
|
|
|
|
up_pwm_servo_arm(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_SET(2):
|
|
|
|
case PWM_SERVO_SET(3):
|
|
|
|
if (_mode != MODE_4PWM) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PWM_SERVO_SET(0):
|
|
|
|
case PWM_SERVO_SET(1):
|
|
|
|
if (arg < 2100) {
|
2012-08-05 00:05:47 -03:00
|
|
|
channel = cmd - PWM_SERVO_SET(0);
|
2012-08-04 19:12:36 -03:00
|
|
|
up_pwm_servo_set(channel, arg);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_GET(2):
|
|
|
|
case PWM_SERVO_GET(3):
|
|
|
|
if (_mode != MODE_4PWM) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case PWM_SERVO_GET(0):
|
|
|
|
case PWM_SERVO_GET(1): {
|
2012-08-05 17:43:16 -03:00
|
|
|
channel = cmd - PWM_SERVO_SET(0);
|
|
|
|
*(servo_position_t *)arg = up_pwm_servo_get(channel);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCGETOUTPUTCOUNT:
|
2012-08-05 00:05:47 -03:00
|
|
|
if (_mode == MODE_4PWM) {
|
|
|
|
*(unsigned *)arg = 4;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
} else {
|
|
|
|
*(unsigned *)arg = 2;
|
|
|
|
}
|
2012-08-14 12:47:04 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
break;
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCRESET:
|
|
|
|
if (_mixers != nullptr) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
2012-08-05 06:09:11 -03:00
|
|
|
}
|
2012-08-14 12:47:04 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
break;
|
2012-08-05 06:09:11 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCADDSIMPLE: {
|
2012-08-14 12:47:04 -03:00
|
|
|
mixer_simple_s *mixinfo = (mixer_simple_s *)arg;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
SimpleMixer *mixer = new SimpleMixer(control_callback,
|
|
|
|
(uintptr_t)&_controls, mixinfo);
|
2012-08-14 12:47:04 -03:00
|
|
|
|
|
|
|
if (mixer->check()) {
|
|
|
|
delete mixer;
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (_mixers == nullptr)
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers = new MixerGroup(control_callback,
|
|
|
|
(uintptr_t)&_controls);
|
2012-08-14 12:47:04 -03:00
|
|
|
|
|
|
|
_mixers->add_mixer(mixer);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCADDMULTIROTOR:
|
|
|
|
/* XXX not yet supported */
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
2012-08-05 23:46:55 -03:00
|
|
|
|
2012-08-10 04:30:40 -03:00
|
|
|
case MIXERIOCLOADFILE: {
|
2012-08-14 12:47:04 -03:00
|
|
|
const char *path = (const char *)arg;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-14 12:47:04 -03:00
|
|
|
if (_mixers != nullptr) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-08-22 00:14:43 -03:00
|
|
|
_mixers = new MixerGroup(control_callback, (uintptr_t)&_controls);
|
2012-08-14 12:47:04 -03:00
|
|
|
|
|
|
|
if (_mixers->load_from_file(path) != 0) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
namespace
|
|
|
|
{
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
enum PortMode {
|
2012-08-05 06:09:11 -03:00
|
|
|
PORT_MODE_UNSET = 0,
|
2012-08-04 19:12:36 -03:00
|
|
|
PORT_FULL_GPIO,
|
|
|
|
PORT_FULL_SERIAL,
|
|
|
|
PORT_FULL_PWM,
|
|
|
|
PORT_GPIO_AND_SERIAL,
|
|
|
|
PORT_PWM_AND_SERIAL,
|
|
|
|
PORT_PWM_AND_GPIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
PortMode g_port_mode;
|
|
|
|
|
|
|
|
int
|
2012-08-05 17:43:16 -03:00
|
|
|
fmu_new_mode(PortMode new_mode)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = OK;
|
|
|
|
uint32_t gpio_bits;
|
|
|
|
FMUServo::Mode servo_mode;
|
|
|
|
|
|
|
|
/* get hold of the GPIO configuration descriptor */
|
|
|
|
fd = open(GPIO_DEVICE_PATH, 0);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
/* start by tearing down any existing state and revert to all-GPIO-inputs */
|
|
|
|
if (g_servo != nullptr) {
|
|
|
|
delete g_servo;
|
|
|
|
g_servo = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset to all-inputs */
|
|
|
|
ioctl(fd, GPIO_RESET, 0);
|
|
|
|
|
|
|
|
gpio_bits = 0;
|
|
|
|
servo_mode = FMUServo::MODE_NONE;
|
|
|
|
|
|
|
|
switch (new_mode) {
|
|
|
|
case PORT_FULL_GPIO:
|
|
|
|
case PORT_MODE_UNSET:
|
|
|
|
/* nothing more to do here */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_FULL_SERIAL:
|
|
|
|
/* set all multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_1 | GPIO_MULTI_2 | GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_FULL_PWM:
|
|
|
|
/* select 4-pin PWM mode */
|
|
|
|
servo_mode = FMUServo::MODE_4PWM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_GPIO_AND_SERIAL:
|
|
|
|
/* set RX/TX multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_PWM_AND_SERIAL:
|
|
|
|
/* select 2-pin PWM mode */
|
|
|
|
servo_mode = FMUServo::MODE_2PWM;
|
|
|
|
/* set RX/TX multi-GPIOs to serial mode */
|
|
|
|
gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PORT_PWM_AND_GPIO:
|
|
|
|
/* select 2-pin PWM mode */
|
|
|
|
servo_mode = FMUServo::MODE_2PWM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust GPIO config for serial mode(s) */
|
|
|
|
if (gpio_bits != 0)
|
|
|
|
ioctl(fd, GPIO_SET_ALT_1, gpio_bits);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* create new PWM driver if required */
|
|
|
|
if (servo_mode != FMUServo::MODE_NONE) {
|
|
|
|
g_servo = new FMUServo(servo_mode);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (g_servo == nullptr) {
|
|
|
|
ret = -ENOMEM;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
|
|
|
ret = g_servo->init();
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK) {
|
|
|
|
delete g_servo;
|
|
|
|
g_servo = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
void
|
|
|
|
test(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(PWM_OUTPUT_DEVICE_PATH, 0);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
puts("open fail");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ioctl(fd, PWM_SERVO_ARM, 0);
|
|
|
|
ioctl(fd, PWM_SERVO_SET(0), 1000);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fake(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 5) {
|
|
|
|
puts("fmu fake <roll> <pitch> <yaw> <thrust> (values -100 - 100)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct actuator_controls_s ac;
|
|
|
|
|
|
|
|
ac.control[0] = strtol(argv[1], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[1] = strtol(argv[2], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[2] = strtol(argv[3], 0, 0) / 100.0f;
|
|
|
|
|
|
|
|
ac.control[3] = strtol(argv[4], 0, 0) / 100.0f;
|
|
|
|
|
2012-08-22 03:44:22 -03:00
|
|
|
orb_advert_t handle = orb_advertise(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, &ac);
|
2012-08-05 23:46:55 -03:00
|
|
|
|
|
|
|
if (handle < 0) {
|
|
|
|
puts("advertise failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} // namespace
|
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
extern "C" __EXPORT int fmu_main(int argc, char *argv[]);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
int
|
|
|
|
fmu_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
PortMode new_mode = PORT_MODE_UNSET;
|
|
|
|
|
2012-08-05 23:46:55 -03:00
|
|
|
if (!strcmp(argv[1], "test"))
|
|
|
|
test();
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "fake"))
|
|
|
|
fake(argc - 1, argv + 1);
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/*
|
|
|
|
* Mode switches.
|
|
|
|
*
|
2012-08-05 23:46:55 -03:00
|
|
|
* XXX use getopt?
|
2012-08-04 19:12:36 -03:00
|
|
|
*/
|
|
|
|
if (!strcmp(argv[1], "mode_gpio")) {
|
|
|
|
new_mode = PORT_FULL_GPIO;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (!strcmp(argv[1], "mode_serial")) {
|
|
|
|
new_mode = PORT_FULL_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (!strcmp(argv[1], "mode_pwm")) {
|
|
|
|
new_mode = PORT_FULL_PWM;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (!strcmp(argv[1], "mode_gpio_serial")) {
|
|
|
|
new_mode = PORT_GPIO_AND_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (!strcmp(argv[1], "mode_pwm_serial")) {
|
|
|
|
new_mode = PORT_PWM_AND_SERIAL;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (!strcmp(argv[1], "mode_pwm_gpio")) {
|
|
|
|
new_mode = PORT_PWM_AND_GPIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* was a new mode set? */
|
|
|
|
if (new_mode != PORT_MODE_UNSET) {
|
|
|
|
|
|
|
|
/* yes but it's the same mode */
|
|
|
|
if (new_mode == g_port_mode)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* switch modes */
|
|
|
|
return fmu_new_mode(new_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test, etc. here */
|
|
|
|
|
|
|
|
fprintf(stderr, "FMU: unrecognised command, try:\n");
|
|
|
|
fprintf(stderr, " mode_gpio, mode_serial, mode_pwm, mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|