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 10:56:24 -03:00
|
|
|
* @file px4io.cpp
|
|
|
|
* Driver for the PX4IO board.
|
2012-08-04 19:12:36 -03:00
|
|
|
*
|
|
|
|
* PX4IO is connected via serial (or possibly some other interface at a later
|
|
|
|
* point).
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <queue.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <stdlib.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
|
|
|
#include <drivers/device/device.h>
|
|
|
|
#include <drivers/drv_rc_input.h>
|
|
|
|
#include <drivers/drv_pwm_output.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <systemlib/mixer/mixer.h>
|
|
|
|
#include <drivers/drv_mixer.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
#include <systemlib/perf_counter.h>
|
|
|
|
#include <systemlib/hx_stream.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <systemlib/err.h>
|
|
|
|
#include <systemlib/systemlib.h>
|
|
|
|
|
|
|
|
#include <uORB/topics/actuator_controls.h>
|
|
|
|
#include <uORB/topics/actuator_outputs.h>
|
|
|
|
#include <uORB/topics/rc_channels.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-02 03:42:36 -03:00
|
|
|
#include <px4io/protocol.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include "uploader.h"
|
|
|
|
|
|
|
|
|
|
|
|
class PX4IO : public device::CDev
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PX4IO();
|
|
|
|
~PX4IO();
|
|
|
|
|
|
|
|
virtual int init();
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
virtual int ioctl(file *filp, int cmd, unsigned long arg);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
private:
|
2012-10-20 20:53:52 -03:00
|
|
|
static const unsigned _max_actuators = PX4IO_OUTPUT_CHANNELS;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
int _serial_fd; ///< serial interface to PX4IO
|
|
|
|
hx_stream_t _io_stream; ///< HX protocol stream
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
int _task; ///< worker task
|
2012-08-04 19:12:36 -03:00
|
|
|
volatile bool _task_should_exit;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
int _t_actuators; ///< actuator output topic
|
|
|
|
actuator_controls_s _controls; ///< actuator outputs
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
int _t_armed; ///< system armed control topic
|
|
|
|
actuator_armed_s _armed; ///< system armed state
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
orb_advert_t _t_outputs; ///< mixed outputs topic
|
|
|
|
actuator_outputs_s _outputs; ///< mixed outputs
|
|
|
|
|
|
|
|
MixerGroup *_mixers; ///< loaded mixers
|
|
|
|
|
|
|
|
bool _primary_pwm_device; ///< true if we are the default PWM output
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
volatile bool _switch_armed; ///< PX4IO switch armed state
|
2012-10-24 03:38:45 -03:00
|
|
|
// XXX how should this work?
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
bool _send_needed; ///< If true, we need to send a packet to IO
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trampoline to the worker task
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
static void task_main_trampoline(int argc, char *argv[]);
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* worker task
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
void task_main();
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/**
|
|
|
|
* Handle receiving bytes from PX4IO
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
void io_recv();
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/**
|
2012-10-20 20:53:52 -03:00
|
|
|
* HX protocol callback trampoline.
|
2012-10-20 02:10:12 -03:00
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
static void rx_callback_trampoline(void *arg, const void *buffer, size_t bytes_received);
|
2012-10-20 20:53:52 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when we receive a whole packet from PX4IO
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
void rx_callback(const uint8_t *buffer, size_t bytes_received);
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/**
|
|
|
|
* Send an update packet to PX4IO
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
void io_send();
|
2012-10-20 20:53:52 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mixer control callback; invoked to fetch a control from a specific
|
|
|
|
* group/index during mixing.
|
|
|
|
*/
|
2012-10-20 02:10:12 -03:00
|
|
|
static int control_callback(uintptr_t handle,
|
2012-10-24 03:38:45 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input);
|
2012-08-04 19:12:36 -03:00
|
|
|
};
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2012-10-24 03:38:45 -03:00
|
|
|
namespace
|
2012-10-20 02:10:12 -03:00
|
|
|
{
|
|
|
|
|
|
|
|
PX4IO *g_dev;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
PX4IO::PX4IO() :
|
|
|
|
CDev("px4io", "/dev/px4io"),
|
2012-10-20 02:10:12 -03:00
|
|
|
_serial_fd(-1),
|
|
|
|
_io_stream(nullptr),
|
2012-08-04 19:12:36 -03:00
|
|
|
_task(-1),
|
|
|
|
_task_should_exit(false),
|
2012-10-20 02:10:12 -03:00
|
|
|
_t_actuators(-1),
|
|
|
|
_t_armed(-1),
|
|
|
|
_t_outputs(-1),
|
|
|
|
_mixers(nullptr),
|
2012-10-20 20:53:52 -03:00
|
|
|
_primary_pwm_device(false),
|
2012-10-20 02:10:12 -03:00
|
|
|
_switch_armed(false),
|
|
|
|
_send_needed(false)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-10-20 02:10:12 -03:00
|
|
|
/* we need this potentially before it could be set in task_main */
|
2012-08-04 19:12:36 -03:00
|
|
|
g_dev = this;
|
|
|
|
|
|
|
|
_debug_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PX4IO::~PX4IO()
|
|
|
|
{
|
|
|
|
if (_task != -1) {
|
2012-10-20 20:53:52 -03:00
|
|
|
/* tell the task we want it to go away */
|
2012-08-04 19:12:36 -03:00
|
|
|
_task_should_exit = true;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* spin waiting for the thread to stop */
|
|
|
|
unsigned i = 10;
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
do {
|
2012-10-20 20:53:52 -03:00
|
|
|
/* wait 50ms - it should wake every 100ms or so worst-case */
|
|
|
|
usleep(50000);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* if we have given up, kill it */
|
2012-10-20 20:53:52 -03:00
|
|
|
if (--i == 0) {
|
2012-08-04 19:12:36 -03:00
|
|
|
task_delete(_task);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (_task != -1);
|
|
|
|
}
|
2012-10-20 20:53:52 -03:00
|
|
|
|
|
|
|
/* clean up the alternate device node */
|
|
|
|
if (_primary_pwm_device)
|
|
|
|
unregister_driver(PWM_OUTPUT_DEVICE_PATH);
|
|
|
|
|
|
|
|
/* kill the HX stream */
|
2012-10-20 02:10:12 -03:00
|
|
|
if (_io_stream != nullptr)
|
|
|
|
hx_stream_free(_io_stream);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
g_dev = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::init()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ASSERT(_task == -1);
|
|
|
|
|
|
|
|
/* do regular cdev init */
|
|
|
|
ret = CDev::init();
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* try to claim the generic PWM output device node as well - it's OK if we fail at this */
|
|
|
|
ret = register_driver(PWM_OUTPUT_DEVICE_PATH, &fops, 0666, (void *)this);
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
if (ret == OK) {
|
|
|
|
log("default PWM output device");
|
|
|
|
_primary_pwm_device = true;
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* start the IO interface task */
|
2012-10-21 03:11:04 -03:00
|
|
|
_task = task_create("px4io", SCHED_PRIORITY_DEFAULT, 4096, (main_t)&PX4IO::task_main_trampoline, nullptr);
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (_task < 0) {
|
|
|
|
debug("task start failed: %d", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::task_main_trampoline(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_dev->task_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::task_main()
|
|
|
|
{
|
2012-10-20 02:10:12 -03:00
|
|
|
log("starting");
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* open the serial port */
|
2012-10-21 03:11:04 -03:00
|
|
|
_serial_fd = ::open("/dev/ttyS2", O_RDWR);
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
if (_serial_fd < 0) {
|
2012-08-04 19:12:36 -03:00
|
|
|
debug("failed to open serial port for IO: %d", errno);
|
|
|
|
_task = -1;
|
|
|
|
_exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protocol stream */
|
2012-10-20 02:10:12 -03:00
|
|
|
_io_stream = hx_stream_init(_serial_fd, &PX4IO::rx_callback_trampoline, this);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
perf_counter_t pc_tx_frames = perf_alloc(PC_COUNT, "PX4IO frames transmitted");
|
|
|
|
perf_counter_t pc_rx_frames = perf_alloc(PC_COUNT, "PX4IO frames received");
|
2012-08-04 19:12:36 -03:00
|
|
|
perf_counter_t pc_rx_errors = perf_alloc(PC_COUNT, "PX4IO receive errors");
|
2012-10-20 02:10:12 -03:00
|
|
|
hx_stream_set_counters(_io_stream, pc_tx_frames, pc_rx_frames, pc_rx_errors);
|
|
|
|
|
|
|
|
/* XXX send a who-are-you request */
|
|
|
|
|
|
|
|
/* XXX verify firmware/protocol version */
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/*
|
|
|
|
* Subscribe to the appropriate PWM output topic based on whether we are the
|
|
|
|
* primary PWM output or not.
|
|
|
|
*/
|
|
|
|
_t_actuators = orb_subscribe(_primary_pwm_device ? ORB_ID_VEHICLE_ATTITUDE_CONTROLS :
|
|
|
|
ORB_ID(actuator_controls_1));
|
2012-10-20 02:10:12 -03:00
|
|
|
/* convert the update rate in hz to milliseconds, rounding down if necessary */
|
|
|
|
//int update_rate_in_ms = int(1000 / _update_rate);
|
|
|
|
orb_set_interval(_t_actuators, 20); /* XXX 50Hz hardcoded for now */
|
|
|
|
|
|
|
|
_t_armed = orb_subscribe(ORB_ID(actuator_armed));
|
|
|
|
orb_set_interval(_t_armed, 200); /* 5Hz update rate */
|
|
|
|
|
|
|
|
/* advertise the mixed control outputs */
|
2012-10-20 20:53:52 -03:00
|
|
|
_t_outputs = orb_advertise(_primary_pwm_device ? ORB_ID_VEHICLE_CONTROLS : ORB_ID(actuator_outputs_1),
|
|
|
|
&_outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* poll descriptor */
|
|
|
|
pollfd fds[3];
|
2012-10-20 02:10:12 -03:00
|
|
|
fds[0].fd = _serial_fd;
|
2012-08-04 19:12:36 -03:00
|
|
|
fds[0].events = POLLIN;
|
2012-10-20 02:10:12 -03:00
|
|
|
fds[1].fd = _t_actuators;
|
|
|
|
fds[1].events = POLLIN;
|
|
|
|
fds[2].fd = _t_armed;
|
|
|
|
fds[2].events = POLLIN;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-21 03:11:04 -03:00
|
|
|
log("ready");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* loop handling received serial bytes */
|
|
|
|
while (!_task_should_exit) {
|
|
|
|
|
|
|
|
/* sleep waiting for data, but no more than 100ms */
|
2012-10-21 03:11:04 -03:00
|
|
|
int ret = ::poll(&fds[0], sizeof(fds) / sizeof(fds[0]), 1000);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* this would be bad... */
|
|
|
|
if (ret < 0) {
|
|
|
|
log("poll error %d", errno);
|
|
|
|
usleep(1000000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we timed out waiting, we should send an update */
|
|
|
|
if (ret == 0)
|
|
|
|
_send_needed = true;
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
if (ret > 0) {
|
|
|
|
/* if we have new data from IO, go handle it */
|
|
|
|
if (fds[0].revents & POLLIN)
|
|
|
|
io_recv();
|
|
|
|
|
|
|
|
/* if we have new data from the ORB, go handle it */
|
|
|
|
if (fds[1].revents & POLLIN) {
|
|
|
|
|
|
|
|
/* get controls */
|
|
|
|
orb_copy(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, _t_actuators, &_controls);
|
|
|
|
|
|
|
|
/* mix */
|
|
|
|
if (_mixers != nullptr) {
|
|
|
|
/* XXX is this the right count? */
|
|
|
|
_mixers->mix(&_outputs.output[0], _max_actuators);
|
|
|
|
|
|
|
|
/* convert to PWM values */
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
_outputs.output[i] = 1500 + (600 * _outputs.output[i]);
|
|
|
|
|
|
|
|
/* and flag for update */
|
|
|
|
_send_needed = true;
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
if (fds[2].revents & POLLIN) {
|
2012-10-21 03:11:04 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
orb_copy(ORB_ID(actuator_armed), _t_armed, &_controls);
|
|
|
|
_send_needed = true;
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* send an update to IO if required */
|
|
|
|
if (_send_needed) {
|
|
|
|
_send_needed = false;
|
|
|
|
io_send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tell the dtor that we are exiting */
|
|
|
|
_task = -1;
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
int
|
|
|
|
PX4IO::control_callback(uintptr_t handle,
|
2012-10-24 03:38:45 -03:00
|
|
|
uint8_t control_group,
|
|
|
|
uint8_t control_index,
|
|
|
|
float &input)
|
2012-10-20 02:10:12 -03:00
|
|
|
{
|
|
|
|
const actuator_controls_s *controls = (actuator_controls_s *)handle;
|
|
|
|
|
|
|
|
input = controls->control[control_index];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
void
|
|
|
|
PX4IO::io_recv()
|
|
|
|
{
|
2012-10-21 03:11:04 -03:00
|
|
|
uint8_t buf[32];
|
|
|
|
int count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are here because poll says there is some data, so this
|
|
|
|
* won't block even on a blocking device. If more bytes are
|
|
|
|
* available, we'll go back to poll() again...
|
|
|
|
*/
|
|
|
|
count = ::read(_serial_fd, buf, sizeof(buf));
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-21 03:11:04 -03:00
|
|
|
/* pass received bytes to the packet decoder */
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
hx_stream_rx(_io_stream, buf[i]);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::rx_callback_trampoline(void *arg, const void *buffer, size_t bytes_received)
|
|
|
|
{
|
|
|
|
g_dev->rx_callback((const uint8_t *)buffer, bytes_received);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::rx_callback(const uint8_t *buffer, size_t bytes_received)
|
|
|
|
{
|
2012-10-20 20:53:52 -03:00
|
|
|
const px4io_report *rep = (const px4io_report *)buffer;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* sanity-check the received frame size */
|
2012-10-20 20:53:52 -03:00
|
|
|
if (bytes_received != sizeof(px4io_report))
|
2012-08-04 19:12:36 -03:00
|
|
|
return;
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/* XXX handle R/C inputs here ... needs code sharing/library */
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/* remember the latched arming switch state */
|
|
|
|
_switch_armed = rep->armed;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::io_send()
|
|
|
|
{
|
2012-10-20 02:10:12 -03:00
|
|
|
px4io_command cmd;
|
2012-11-03 02:46:52 -03:00
|
|
|
int ret;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
cmd.f2i_magic = F2I_MAGIC;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/* set outputs */
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
cmd.servo_command[i] = _outputs.output[i];
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/* publish as we send */
|
|
|
|
orb_publish(ORB_ID_VEHICLE_CONTROLS, _t_outputs, &_outputs);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
// XXX relays
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
cmd.arm_ok = _armed.armed;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-03 02:46:52 -03:00
|
|
|
ret = hx_stream_send(_io_stream, &cmd, sizeof(cmd));
|
|
|
|
if (ret)
|
|
|
|
debug("send error %d", ret);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-10-20 20:53:52 -03:00
|
|
|
PX4IO::ioctl(file *filep, int cmd, unsigned long arg)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-10-20 02:10:12 -03:00
|
|
|
int ret = OK;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
/* regular ioctl? */
|
|
|
|
switch (cmd) {
|
|
|
|
case PWM_SERVO_ARM:
|
2012-10-20 02:10:12 -03:00
|
|
|
/* fake an armed transition */
|
|
|
|
_armed.armed = true;
|
|
|
|
_send_needed = true;
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_DISARM:
|
2012-10-20 02:10:12 -03:00
|
|
|
/* fake a disarmed transition */
|
|
|
|
_armed.armed = true;
|
|
|
|
_send_needed = true;
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case PWM_SERVO_SET(0) ... PWM_SERVO_SET(_max_actuators - 1):
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* fake an update to the selected servo channel */
|
2012-10-20 02:10:12 -03:00
|
|
|
if ((arg >= 900) && (arg <= 2100)) {
|
|
|
|
_outputs.output[cmd - PWM_SERVO_SET(0)] = arg;
|
|
|
|
_send_needed = true;
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_GET(0) ... PWM_SERVO_GET(_max_actuators - 1):
|
2012-10-20 20:53:52 -03:00
|
|
|
/* copy the current output value from the channel */
|
2012-10-20 02:10:12 -03:00
|
|
|
*(servo_position_t *)arg = _outputs.output[cmd - PWM_SERVO_GET(0)];
|
|
|
|
break;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case MIXERIOCGETOUTPUTCOUNT:
|
|
|
|
*(unsigned *)arg = _max_actuators;
|
|
|
|
break;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case MIXERIOCRESET:
|
|
|
|
if (_mixers != nullptr) {
|
|
|
|
delete _mixers;
|
|
|
|
_mixers = nullptr;
|
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIXERIOCADDSIMPLE: {
|
|
|
|
mixer_simple_s *mixinfo = (mixer_simple_s *)arg;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* build the new mixer from the supplied argument */
|
2012-10-20 02:10:12 -03:00
|
|
|
SimpleMixer *mixer = new SimpleMixer(control_callback,
|
|
|
|
(uintptr_t)&_controls, mixinfo);
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* validate the new mixer */
|
2012-10-20 02:10:12 -03:00
|
|
|
if (mixer->check()) {
|
|
|
|
delete mixer;
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
} else {
|
2012-10-20 20:53:52 -03:00
|
|
|
/* if we don't have a group yet, allocate one */
|
2012-10-20 02:10:12 -03:00
|
|
|
if (_mixers == nullptr)
|
|
|
|
_mixers = new MixerGroup(control_callback,
|
|
|
|
(uintptr_t)&_controls);
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* add the new mixer to the group */
|
2012-10-20 02:10:12 -03:00
|
|
|
_mixers->add_mixer(mixer);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIXERIOCADDMULTIROTOR:
|
|
|
|
/* XXX not yet supported */
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case MIXERIOCLOADFILE: {
|
2012-10-20 20:53:52 -03:00
|
|
|
MixerGroup *newmixers;
|
2012-10-20 02:10:12 -03:00
|
|
|
const char *path = (const char *)arg;
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* allocate a new mixer group and load it from the file */
|
|
|
|
newmixers = new MixerGroup(control_callback, (uintptr_t)&_controls);
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
if (newmixers->load_from_file(path) != 0) {
|
|
|
|
delete newmixers;
|
|
|
|
ret = -EINVAL;
|
2012-10-20 02:10:12 -03:00
|
|
|
}
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* swap the new mixers in for the old */
|
|
|
|
if (_mixers != nullptr) {
|
2012-10-20 02:10:12 -03:00
|
|
|
delete _mixers;
|
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
_mixers = newmixers;
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-08-04 19:12:36 -03:00
|
|
|
/* not a recognised value */
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __EXPORT int px4io_main(int argc, char *argv[]);
|
|
|
|
|
|
|
|
int
|
|
|
|
px4io_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "start")) {
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
if (g_dev != nullptr)
|
|
|
|
errx(1, "already loaded");
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* create the driver - it will set g_dev */
|
|
|
|
(void)new PX4IO;
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
if (g_dev == nullptr)
|
|
|
|
errx(1, "driver alloc failed");
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
if (OK != g_dev->init()) {
|
|
|
|
delete g_dev;
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "driver init failed");
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
exit(0);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/* note, stop not currently implemented */
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (!strcmp(argv[1], "update")) {
|
|
|
|
PX4IO_Uploader *up;
|
|
|
|
const char *fn[3];
|
|
|
|
|
|
|
|
/* work out what we're uploading... */
|
|
|
|
if (argc > 2) {
|
|
|
|
fn[0] = argv[2];
|
|
|
|
fn[1] = nullptr;
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
|
|
|
fn[0] = "/fs/microsd/px4io.bin";
|
|
|
|
fn[1] = "/etc/px4io.bin";
|
|
|
|
fn[2] = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
up = new PX4IO_Uploader;
|
|
|
|
int ret = up->upload(&fn[0]);
|
|
|
|
delete up;
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case OK:
|
|
|
|
break;
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case -ENOENT:
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "PX4IO firmware file not found");
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case -EEXIST:
|
|
|
|
case -EIO:
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "error updating PX4IO - check that bootloader mode is enabled");
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case -EINVAL:
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "verify failed - retry the update");
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
case -ETIMEDOUT:
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "timed out waiting for bootloader - power-cycle and try again");
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
default:
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "unexpected error %d", ret);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
errx(1, "need a verb, only support 'start' and 'update'");
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|