2012-08-04 19:12:36 -03:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
2013-01-15 04:41:47 -04:00
|
|
|
* Copyright (C) 2012,2013 PX4 Development Team. All rights reserved.
|
2012-08-04 19:12:36 -03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
2013-01-15 04:41:47 -04:00
|
|
|
* PX4IO is connected via I2C.
|
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>
|
2012-12-13 05:23:02 -04:00
|
|
|
#include <math.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
#include <arch/board/board.h>
|
|
|
|
|
|
|
|
#include <drivers/device/device.h>
|
2013-01-16 03:01:04 -04:00
|
|
|
#include <drivers/device/i2c.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <drivers/drv_rc_input.h>
|
|
|
|
#include <drivers/drv_pwm_output.h>
|
2012-12-21 01:31:02 -04:00
|
|
|
#include <drivers/drv_gpio.h>
|
2012-11-05 11:04:45 -04:00
|
|
|
#include <drivers/drv_hrt.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <drivers/drv_mixer.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-05 11:04:45 -04:00
|
|
|
#include <systemlib/mixer/mixer.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include <systemlib/perf_counter.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <systemlib/err.h>
|
|
|
|
#include <systemlib/systemlib.h>
|
2012-12-27 14:01:00 -04:00
|
|
|
#include <systemlib/scheduling_priorities.h>
|
2013-01-05 17:13:12 -04:00
|
|
|
#include <systemlib/param/param.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
#include <uORB/topics/actuator_controls.h>
|
2012-11-27 06:53:50 -04:00
|
|
|
#include <uORB/topics/actuator_controls_effective.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <uORB/topics/actuator_outputs.h>
|
2012-12-13 05:23:02 -04:00
|
|
|
#include <uORB/topics/vehicle_status.h>
|
2013-02-17 11:06:33 -04:00
|
|
|
#include <uORB/topics/vehicle_command.h>
|
2012-10-20 02:10:12 -03:00
|
|
|
#include <uORB/topics/rc_channels.h>
|
2013-01-01 08:30:24 -04:00
|
|
|
#include <uORB/topics/battery_status.h>
|
2013-01-16 17:02:49 -04:00
|
|
|
#include <uORB/topics/parameter_update.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-02 03:42:36 -03:00
|
|
|
#include <px4io/protocol.h>
|
2013-03-07 15:51:33 -04:00
|
|
|
#include <mavlink/mavlink_log.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
#include "uploader.h"
|
2013-03-06 17:52:19 -04:00
|
|
|
#include <debug.h>
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
class PX4IO : public device::I2C
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
PX4IO();
|
2013-03-09 16:07:29 -04:00
|
|
|
virtual ~PX4IO();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
virtual int init();
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
virtual int ioctl(file *filp, int cmd, unsigned long arg);
|
2013-01-15 04:41:47 -04:00
|
|
|
virtual ssize_t write(file *filp, const char *buffer, size_t len);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2013-02-24 15:24:21 -04:00
|
|
|
void print_status();
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
private:
|
2012-12-30 05:49:27 -04:00
|
|
|
// XXX
|
2013-01-16 03:01:04 -04:00
|
|
|
unsigned _max_actuators;
|
2013-02-24 19:31:01 -04:00
|
|
|
unsigned _max_controls;
|
2013-01-16 03:01:04 -04:00
|
|
|
unsigned _max_rc_input;
|
|
|
|
unsigned _max_relays;
|
|
|
|
unsigned _max_transfer;
|
2013-01-15 04:41:47 -04:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
unsigned _update_interval; ///< subscription interval limiting send rate
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
volatile int _task; ///< worker task
|
2012-08-04 19:12:36 -03:00
|
|
|
volatile bool _task_should_exit;
|
|
|
|
|
2013-03-07 15:51:33 -04:00
|
|
|
int _mavlink_fd;
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
perf_counter_t _perf_update;
|
|
|
|
|
|
|
|
/* cached IO state */
|
|
|
|
uint16_t _status;
|
|
|
|
uint16_t _alarms;
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
/* subscribed topics */
|
2013-03-10 18:01:13 -03:00
|
|
|
int _t_actuators; ///< actuator controls topic
|
2012-10-20 20:53:52 -03:00
|
|
|
int _t_armed; ///< system armed control topic
|
2012-12-13 05:23:02 -04:00
|
|
|
int _t_vstatus; ///< system / vehicle status
|
2013-01-16 17:02:49 -04:00
|
|
|
int _t_param; ///< parameter update topic
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
/* advertised topics */
|
2012-11-05 11:04:45 -04:00
|
|
|
orb_advert_t _to_input_rc; ///< rc inputs from io
|
2013-01-14 04:19:01 -04:00
|
|
|
orb_advert_t _to_actuators_effective; ///< effective actuator controls topic
|
|
|
|
orb_advert_t _to_outputs; ///< mixed servo outputs topic
|
2013-01-01 08:30:24 -04:00
|
|
|
orb_advert_t _to_battery; ///< battery status / voltage
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
actuator_outputs_s _outputs; ///< mixed outputs
|
2013-01-14 04:19:01 -04:00
|
|
|
actuator_controls_effective_s _controls_effective; ///< effective controls
|
2012-10-20 20:53:52 -03:00
|
|
|
|
|
|
|
bool _primary_pwm_device; ///< true if we are the default PWM output
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2012-12-21 01:31:02 -04:00
|
|
|
|
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
|
|
|
/**
|
2013-01-14 04:19:01 -04:00
|
|
|
* Send controls to IO
|
2012-10-20 20:53:52 -03:00
|
|
|
*/
|
2013-01-14 04:19:01 -04:00
|
|
|
int io_set_control_state();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
/**
|
2013-01-14 04:19:01 -04:00
|
|
|
* Update IO's arming-related state
|
2012-10-20 02:10:12 -03:00
|
|
|
*/
|
2013-01-14 04:19:01 -04:00
|
|
|
int io_set_arming_state();
|
2012-10-20 20:53:52 -03:00
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
/**
|
|
|
|
* Push RC channel configuration to IO.
|
|
|
|
*/
|
|
|
|
int io_set_rc_config();
|
|
|
|
|
2013-01-14 04:30:18 -04:00
|
|
|
/**
|
|
|
|
* Fetch status and alarms from IO
|
2013-01-16 03:01:04 -04:00
|
|
|
*
|
|
|
|
* Also publishes battery voltage/current.
|
2013-01-14 04:30:18 -04:00
|
|
|
*/
|
|
|
|
int io_get_status();
|
|
|
|
|
|
|
|
/**
|
2013-01-16 03:01:04 -04:00
|
|
|
* Fetch RC inputs from IO.
|
2013-01-15 04:41:47 -04:00
|
|
|
*
|
|
|
|
* @param input_rc Input structure to populate.
|
2013-01-16 03:01:04 -04:00
|
|
|
* @return OK if data was returned.
|
|
|
|
*/
|
|
|
|
int io_get_raw_rc_input(rc_input_values &input_rc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and publish raw RC input data.
|
|
|
|
*/
|
|
|
|
int io_publish_raw_rc();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and publish the mixed control values.
|
2013-01-14 04:30:18 -04:00
|
|
|
*/
|
2013-01-16 03:01:04 -04:00
|
|
|
int io_publish_mixed_controls();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and publish the PWM servo outputs.
|
|
|
|
*/
|
|
|
|
int io_publish_pwm_outputs();
|
2013-01-14 04:30:18 -04:00
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/**
|
2013-01-14 04:19:01 -04:00
|
|
|
* write register(s)
|
2013-01-15 04:41:47 -04:00
|
|
|
*
|
|
|
|
* @param page Register page to write to.
|
|
|
|
* @param offset Register offset to start writing at.
|
|
|
|
* @param values Pointer to array of values to write.
|
|
|
|
* @param num_values The number of values to write.
|
|
|
|
* @return Zero if all values were successfully written.
|
2012-10-20 20:53:52 -03:00
|
|
|
*/
|
2013-01-14 04:19:01 -04:00
|
|
|
int io_reg_set(uint8_t page, uint8_t offset, const uint16_t *values, unsigned num_values);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2013-01-15 04:41:47 -04:00
|
|
|
/**
|
|
|
|
* write a register
|
|
|
|
*
|
|
|
|
* @param page Register page to write to.
|
|
|
|
* @param offset Register offset to write to.
|
|
|
|
* @param value Value to write.
|
|
|
|
* @return Zero if the value was written successfully.
|
|
|
|
*/
|
|
|
|
int io_reg_set(uint8_t page, uint8_t offset, const uint16_t value);
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/**
|
2013-01-14 04:19:01 -04:00
|
|
|
* read register(s)
|
2013-01-15 04:41:47 -04:00
|
|
|
*
|
|
|
|
* @param page Register page to read from.
|
|
|
|
* @param offset Register offset to start reading from.
|
|
|
|
* @param values Pointer to array where values should be stored.
|
|
|
|
* @param num_values The number of values to read.
|
|
|
|
* @return Zero if all values were successfully read.
|
2012-10-20 20:53:52 -03:00
|
|
|
*/
|
2013-01-14 04:19:01 -04:00
|
|
|
int io_reg_get(uint8_t page, uint8_t offset, uint16_t *values, unsigned num_values);
|
2012-10-20 20:53:52 -03:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/**
|
|
|
|
* read a register
|
|
|
|
*
|
|
|
|
* @param page Register page to read from.
|
|
|
|
* @param offset Register offset to start reading from.
|
|
|
|
* @return Register value that was read, or _io_reg_get_error on error.
|
|
|
|
*/
|
|
|
|
uint32_t io_reg_get(uint8_t page, uint8_t offset);
|
|
|
|
static const uint32_t _io_reg_get_error = 0x80000000;
|
|
|
|
|
2012-11-05 04:55:22 -04:00
|
|
|
/**
|
2013-01-15 04:41:47 -04:00
|
|
|
* modify a register
|
|
|
|
*
|
|
|
|
* @param page Register page to modify.
|
|
|
|
* @param offset Register offset to modify.
|
|
|
|
* @param clearbits Bits to clear in the register.
|
|
|
|
* @param setbits Bits to set in the register.
|
2012-11-05 04:55:22 -04:00
|
|
|
*/
|
2013-01-14 04:19:01 -04:00
|
|
|
int io_reg_modify(uint8_t page, uint8_t offset, uint16_t clearbits, uint16_t setbits);
|
2012-11-05 04:55:22 -04:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/**
|
|
|
|
* Send mixer definition text to IO
|
|
|
|
*/
|
|
|
|
int mixer_send(const char *buf, unsigned buflen);
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
/**
|
|
|
|
* Handle a status update from IO.
|
|
|
|
*
|
|
|
|
* Publish IO status information if necessary.
|
|
|
|
*
|
|
|
|
* @param status The status register
|
|
|
|
*/
|
|
|
|
int io_handle_status(uint16_t status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an alarm update from IO.
|
|
|
|
*
|
|
|
|
* Publish IO alarm information if necessary.
|
|
|
|
*
|
|
|
|
* @param alarm The status register
|
|
|
|
*/
|
|
|
|
int io_handle_alarms(uint16_t alarms);
|
|
|
|
|
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() :
|
2013-01-16 03:01:04 -04:00
|
|
|
I2C("px4io", "/dev/px4io", PX4_I2C_BUS_ONBOARD, PX4_I2C_OBDEV_PX4IO, 320000),
|
2013-01-15 04:41:47 -04:00
|
|
|
_max_actuators(0),
|
2013-02-24 19:31:01 -04:00
|
|
|
_max_controls(0),
|
2013-01-16 03:01:04 -04:00
|
|
|
_max_rc_input(0),
|
|
|
|
_max_relays(0),
|
|
|
|
_max_transfer(16), /* sensible default */
|
|
|
|
_update_interval(0),
|
2012-08-04 19:12:36 -03:00
|
|
|
_task(-1),
|
|
|
|
_task_should_exit(false),
|
2013-03-07 15:51:33 -04:00
|
|
|
_mavlink_fd(-1),
|
2013-01-16 03:01:04 -04:00
|
|
|
_perf_update(perf_alloc(PC_ELAPSED, "px4io update")),
|
2013-02-24 17:40:46 -04:00
|
|
|
_status(0),
|
|
|
|
_alarms(0),
|
2012-10-20 02:10:12 -03:00
|
|
|
_t_actuators(-1),
|
|
|
|
_t_armed(-1),
|
2012-12-13 05:23:02 -04:00
|
|
|
_t_vstatus(-1),
|
2013-02-24 17:40:46 -04:00
|
|
|
_t_param(-1),
|
2013-01-14 04:19:01 -04:00
|
|
|
_to_input_rc(0),
|
|
|
|
_to_actuators_effective(0),
|
|
|
|
_to_outputs(0),
|
|
|
|
_to_battery(0),
|
2013-01-16 03:01:04 -04:00
|
|
|
_primary_pwm_device(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;
|
|
|
|
|
2013-03-07 15:51:33 -04:00
|
|
|
/* open MAVLink text channel */
|
|
|
|
_mavlink_fd = ::open(MAVLINK_LOG_DEVICE, 0);
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
_debug_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PX4IO::~PX4IO()
|
|
|
|
{
|
2012-11-30 04:02:47 -04:00
|
|
|
/* tell the task we want it to go away */
|
|
|
|
_task_should_exit = true;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
/* spin waiting for the task to stop */
|
|
|
|
for (unsigned i = 0; (i < 10) && (_task != -1); i++) {
|
|
|
|
/* give it another 100ms */
|
|
|
|
usleep(100000);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
2012-12-30 05:17:19 -04:00
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
/* well, kill it anyway, though this will probably crash */
|
|
|
|
if (_task != -1)
|
|
|
|
task_delete(_task);
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
g_dev = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::init()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ASSERT(_task == -1);
|
|
|
|
|
|
|
|
/* do regular cdev init */
|
2013-01-14 04:19:01 -04:00
|
|
|
ret = I2C::init();
|
2012-08-04 19:12:36 -03:00
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
2013-01-26 23:43:23 -04:00
|
|
|
/*
|
|
|
|
* Enable a couple of retries for operations to IO.
|
|
|
|
*
|
|
|
|
* Register read/write operations are intentionally idempotent
|
|
|
|
* so this is safe as designed.
|
|
|
|
*/
|
2013-01-24 02:19:33 -04:00
|
|
|
_retries = 2;
|
|
|
|
|
2013-01-15 04:41:47 -04:00
|
|
|
/* get some parameters */
|
2013-01-16 03:01:04 -04:00
|
|
|
_max_actuators = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_ACTUATOR_COUNT);
|
2013-02-24 19:31:01 -04:00
|
|
|
_max_controls = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_CONTROL_COUNT);
|
2013-01-16 03:01:04 -04:00
|
|
|
_max_relays = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_RELAY_COUNT);
|
2013-01-26 01:54:04 -04:00
|
|
|
_max_transfer = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_MAX_TRANSFER) - 2;
|
2013-01-16 03:01:04 -04:00
|
|
|
_max_rc_input = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_RC_INPUT_COUNT);
|
2013-01-23 22:56:03 -04:00
|
|
|
if ((_max_actuators < 1) || (_max_actuators > 255) ||
|
|
|
|
(_max_relays < 1) || (_max_relays > 255) ||
|
2013-01-24 00:18:18 -04:00
|
|
|
(_max_transfer < 16) || (_max_transfer > 255) ||
|
2013-01-23 22:56:03 -04:00
|
|
|
(_max_rc_input < 1) || (_max_rc_input > 255)) {
|
2013-01-15 04:41:47 -04:00
|
|
|
|
|
|
|
log("failed getting parameters from PX4IO");
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_emergency(_mavlink_fd, "[IO] param read fail, abort.");
|
2013-01-23 22:56:03 -04:00
|
|
|
return -1;
|
2013-01-15 04:41:47 -04:00
|
|
|
}
|
2013-01-16 03:01:04 -04:00
|
|
|
if (_max_rc_input > RC_INPUT_MAX_CHANNELS)
|
|
|
|
_max_rc_input = RC_INPUT_MAX_CHANNELS;
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
/*
|
|
|
|
* Check for IO flight state - if FMU was flagged to be in
|
|
|
|
* armed state, FMU is recovering from an in-air reset.
|
|
|
|
* Read back status and request the commander to arm
|
|
|
|
* in this case.
|
|
|
|
*/
|
2013-01-26 23:43:23 -04:00
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
uint16_t reg;
|
|
|
|
|
2013-02-17 12:47:26 -04:00
|
|
|
/* get IO's last seen FMU state */
|
|
|
|
ret = io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, ®, sizeof(reg));
|
2013-02-17 11:06:33 -04:00
|
|
|
if (ret != OK)
|
2013-01-16 17:02:49 -04:00
|
|
|
return ret;
|
2013-02-17 11:06:33 -04:00
|
|
|
|
2013-02-17 23:16:09 -04:00
|
|
|
/*
|
|
|
|
* in-air restart is only tried if the IO board reports it is
|
|
|
|
* already armed, and has been configured for in-air restart
|
|
|
|
*/
|
|
|
|
if ((reg & PX4IO_P_SETUP_ARMING_INAIR_RESTART_OK) &&
|
|
|
|
(reg & PX4IO_P_SETUP_ARMING_ARM_OK)) {
|
2013-02-17 11:06:33 -04:00
|
|
|
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_emergency(_mavlink_fd, "[IO] RECOVERING FROM FMU IN-AIR RESTART");
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
/* WARNING: COMMANDER app/vehicle status must be initialized.
|
|
|
|
* If this fails (or the app is not started), worst-case IO
|
|
|
|
* remains untouched (so manual override is still available).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int vstatus_sub = orb_subscribe(ORB_ID(vehicle_status));
|
|
|
|
/* fill with initial values, clear updated flag */
|
|
|
|
vehicle_status_s status;
|
|
|
|
uint64_t try_start_time = hrt_absolute_time();
|
|
|
|
bool updated = false;
|
|
|
|
|
|
|
|
/* keep checking for an update, ensure we got a recent state,
|
|
|
|
not something that was published a long time ago. */
|
|
|
|
do {
|
|
|
|
orb_check(vstatus_sub, &updated);
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
/* got data, copy and exit loop */
|
|
|
|
orb_copy(ORB_ID(vehicle_status), vstatus_sub, &status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait 10 ms */
|
|
|
|
usleep(10000);
|
|
|
|
|
2013-02-17 12:47:26 -04:00
|
|
|
/* abort after 5s */
|
|
|
|
if ((hrt_absolute_time() - try_start_time)/1000 > 50000) {
|
2013-02-17 11:06:33 -04:00
|
|
|
log("failed to recover from in-air restart (1), aborting IO driver init.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
/* send command to arm system via command API */
|
|
|
|
vehicle_command_s cmd;
|
|
|
|
/* request arming */
|
|
|
|
cmd.param1 = 1.0f;
|
|
|
|
cmd.param2 = 0;
|
|
|
|
cmd.param3 = 0;
|
|
|
|
cmd.param4 = 0;
|
|
|
|
cmd.param5 = 0;
|
|
|
|
cmd.param6 = 0;
|
|
|
|
cmd.param7 = 0;
|
|
|
|
cmd.command = VEHICLE_CMD_COMPONENT_ARM_DISARM;
|
|
|
|
cmd.target_system = status.system_id;
|
|
|
|
cmd.target_component = status.component_id;
|
|
|
|
cmd.source_system = status.system_id;
|
|
|
|
cmd.source_component = status.component_id;
|
|
|
|
/* ask to confirm command */
|
|
|
|
cmd.confirmation = 1;
|
|
|
|
|
|
|
|
/* send command once */
|
|
|
|
(void)orb_advertise(ORB_ID(vehicle_command), &cmd);
|
|
|
|
|
|
|
|
/* spin here until IO's state has propagated into the system */
|
|
|
|
do {
|
|
|
|
orb_check(vstatus_sub, &updated);
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
orb_copy(ORB_ID(vehicle_status), vstatus_sub, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait 10 ms */
|
|
|
|
usleep(10000);
|
|
|
|
|
2013-02-17 12:47:26 -04:00
|
|
|
/* abort after 5s */
|
|
|
|
if ((hrt_absolute_time() - try_start_time)/1000 > 50000) {
|
2013-02-17 11:06:33 -04:00
|
|
|
log("failed to recover from in-air restart (2), aborting IO driver init.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keep waiting for state change for 10 s */
|
|
|
|
} while (!status.flag_system_armed);
|
|
|
|
|
|
|
|
/* regular boot, no in-air restart, init IO */
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
/* dis-arm IO before touching anything */
|
|
|
|
io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING,
|
|
|
|
PX4IO_P_SETUP_ARMING_ARM_OK |
|
2013-02-17 23:16:09 -04:00
|
|
|
PX4IO_P_SETUP_ARMING_INAIR_RESTART_OK |
|
2013-02-17 11:06:33 -04:00
|
|
|
PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK |
|
|
|
|
PX4IO_P_SETUP_ARMING_VECTOR_FLIGHT_OK, 0);
|
|
|
|
|
2013-02-24 15:24:21 -04:00
|
|
|
/* publish RC config to IO */
|
2013-02-17 11:06:33 -04:00
|
|
|
ret = io_set_rc_config();
|
|
|
|
if (ret != OK) {
|
|
|
|
log("failed to update RC input config");
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_info(_mavlink_fd, "[IO] RC config upload fail");
|
2013-02-17 11:06:33 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
}
|
|
|
|
|
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-12-27 14:01:00 -04:00
|
|
|
_task = task_create("px4io", SCHED_PRIORITY_ACTUATOR_OUTPUTS, 4096, (main_t)&PX4IO::task_main_trampoline, nullptr);
|
2012-12-30 05:17:19 -04:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (_task < 0) {
|
|
|
|
debug("task start failed: %d", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_info(_mavlink_fd, "[IO] init ok");
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::task_main_trampoline(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_dev->task_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PX4IO::task_main()
|
|
|
|
{
|
2013-01-16 03:01:04 -04:00
|
|
|
hrt_abstime last_poll_time = 0;
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
log("starting");
|
2012-08-04 19:12:36 -03:00
|
|
|
|
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));
|
2013-01-16 03:01:04 -04:00
|
|
|
orb_set_interval(_t_actuators, 20); /* default to 50Hz */
|
2012-10-20 02:10:12 -03:00
|
|
|
|
|
|
|
_t_armed = orb_subscribe(ORB_ID(actuator_armed));
|
|
|
|
orb_set_interval(_t_armed, 200); /* 5Hz update rate */
|
|
|
|
|
2012-12-13 05:23:02 -04:00
|
|
|
_t_vstatus = orb_subscribe(ORB_ID(vehicle_status));
|
|
|
|
orb_set_interval(_t_vstatus, 200); /* 5Hz update rate max. */
|
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
_t_param = orb_subscribe(ORB_ID(parameter_update));
|
|
|
|
orb_set_interval(_t_param, 500); /* 2Hz update rate max. */
|
|
|
|
|
2012-10-20 20:53:52 -03:00
|
|
|
/* poll descriptor */
|
2013-01-16 17:02:49 -04:00
|
|
|
pollfd fds[4];
|
2013-01-14 04:19:01 -04:00
|
|
|
fds[0].fd = _t_actuators;
|
2012-08-04 19:12:36 -03:00
|
|
|
fds[0].events = POLLIN;
|
2013-01-14 04:19:01 -04:00
|
|
|
fds[1].fd = _t_armed;
|
2012-10-20 02:10:12 -03:00
|
|
|
fds[1].events = POLLIN;
|
2013-01-14 04:19:01 -04:00
|
|
|
fds[2].fd = _t_vstatus;
|
2012-10-20 02:10:12 -03:00
|
|
|
fds[2].events = POLLIN;
|
2013-01-16 17:02:49 -04:00
|
|
|
fds[3].fd = _t_param;
|
|
|
|
fds[3].events = POLLIN;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-12-30 05:17:19 -04:00
|
|
|
debug("ready");
|
|
|
|
|
|
|
|
/* lock against the ioctl handler */
|
|
|
|
lock();
|
2012-10-21 03:11:04 -03:00
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
/* loop talking to IO */
|
2012-08-04 19:12:36 -03:00
|
|
|
while (!_task_should_exit) {
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/* adjust update interval */
|
|
|
|
if (_update_interval != 0) {
|
|
|
|
if (_update_interval < 5)
|
|
|
|
_update_interval = 5;
|
|
|
|
if (_update_interval > 100)
|
|
|
|
_update_interval = 100;
|
|
|
|
orb_set_interval(_t_actuators, _update_interval);
|
|
|
|
_update_interval = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-26 04:01:25 -04:00
|
|
|
/* sleep waiting for topic updates, but no more than 20ms */
|
2012-12-30 05:17:19 -04:00
|
|
|
unlock();
|
2013-01-26 04:01:25 -04:00
|
|
|
int ret = ::poll(&fds[0], sizeof(fds) / sizeof(fds[0]), 20);
|
2012-12-30 05:17:19 -04:00
|
|
|
lock();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
|
|
|
/* this would be bad... */
|
|
|
|
if (ret < 0) {
|
|
|
|
log("poll error %d", errno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-26 04:01:25 -04:00
|
|
|
perf_begin(_perf_update);
|
|
|
|
hrt_abstime now = hrt_absolute_time();
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
/* if we have new control data from the ORB, handle it */
|
|
|
|
if (fds[0].revents & POLLIN)
|
|
|
|
io_set_control_state();
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
/* if we have an arming state update, handle it */
|
|
|
|
if ((fds[1].revents & POLLIN) || (fds[2].revents & POLLIN))
|
|
|
|
io_set_arming_state();
|
2012-10-21 03:11:04 -03:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/*
|
2013-01-26 04:01:25 -04:00
|
|
|
* If it's time for another tick of the polling status machine,
|
|
|
|
* try it now.
|
2013-01-16 03:01:04 -04:00
|
|
|
*/
|
2013-01-26 04:01:25 -04:00
|
|
|
if ((now - last_poll_time) >= 20000) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull status and alarms from IO.
|
|
|
|
*/
|
|
|
|
io_get_status();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get raw R/C input from IO.
|
|
|
|
*/
|
|
|
|
io_publish_raw_rc();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch mixed servo controls and PWM outputs from IO.
|
|
|
|
*
|
|
|
|
* XXX We could do this at a reduced rate in many/most cases.
|
|
|
|
*/
|
|
|
|
io_publish_mixed_controls();
|
|
|
|
io_publish_pwm_outputs();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If parameters have changed, re-send RC mappings to IO
|
|
|
|
*
|
|
|
|
* XXX this may be a bit spammy
|
|
|
|
*/
|
|
|
|
if (fds[3].revents & POLLIN) {
|
|
|
|
parameter_update_s pupdate;
|
|
|
|
|
|
|
|
/* copy to reset the notification */
|
|
|
|
orb_copy(ORB_ID(parameter_update), _t_param, &pupdate);
|
|
|
|
|
|
|
|
/* re-upload RC input config as it may have changed */
|
|
|
|
io_set_rc_config();
|
|
|
|
}
|
2013-01-16 17:02:49 -04:00
|
|
|
}
|
2013-01-26 04:01:25 -04:00
|
|
|
|
|
|
|
perf_end(_perf_update);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2012-12-30 05:17:19 -04:00
|
|
|
unlock();
|
|
|
|
|
2012-11-30 04:02:47 -04:00
|
|
|
debug("exiting");
|
|
|
|
|
|
|
|
/* clean up the alternate device node */
|
|
|
|
if (_primary_pwm_device)
|
|
|
|
unregister_driver(PWM_OUTPUT_DEVICE_PATH);
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/* tell the dtor that we are exiting */
|
|
|
|
_task = -1;
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
int
|
2013-01-14 04:19:01 -04:00
|
|
|
PX4IO::io_set_control_state()
|
2012-10-20 02:10:12 -03:00
|
|
|
{
|
2013-01-14 04:19:01 -04:00
|
|
|
actuator_controls_s controls; ///< actuator outputs
|
|
|
|
uint16_t regs[_max_actuators];
|
|
|
|
|
|
|
|
/* get controls */
|
|
|
|
orb_copy(_primary_pwm_device ? ORB_ID_VEHICLE_ATTITUDE_CONTROLS :
|
2013-01-16 03:01:04 -04:00
|
|
|
ORB_ID(actuator_controls_1), _t_actuators, &controls);
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2013-02-24 19:31:01 -04:00
|
|
|
for (unsigned i = 0; i < _max_controls; i++)
|
2013-01-16 03:01:04 -04:00
|
|
|
regs[i] = FLOAT_TO_REG(controls.control[i]);
|
2013-01-14 04:19:01 -04:00
|
|
|
|
|
|
|
/* copy values to registers in IO */
|
2013-02-24 19:31:01 -04:00
|
|
|
return io_reg_set(PX4IO_PAGE_CONTROLS, 0, regs, _max_controls);
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::io_set_arming_state()
|
|
|
|
{
|
|
|
|
actuator_armed_s armed; ///< system armed state
|
|
|
|
vehicle_status_s vstatus; ///< overall system state
|
|
|
|
|
|
|
|
orb_copy(ORB_ID(actuator_armed), _t_armed, &armed);
|
|
|
|
orb_copy(ORB_ID(vehicle_status), _t_vstatus, &vstatus);
|
|
|
|
|
|
|
|
uint16_t set = 0;
|
|
|
|
uint16_t clear = 0;
|
|
|
|
|
|
|
|
if (armed.armed) {
|
|
|
|
set |= PX4IO_P_SETUP_ARMING_ARM_OK;
|
|
|
|
} else {
|
|
|
|
clear |= PX4IO_P_SETUP_ARMING_ARM_OK;
|
|
|
|
}
|
|
|
|
if (vstatus.flag_vector_flight_mode_ok) {
|
|
|
|
set |= PX4IO_P_SETUP_ARMING_VECTOR_FLIGHT_OK;
|
|
|
|
} else {
|
|
|
|
clear |= PX4IO_P_SETUP_ARMING_VECTOR_FLIGHT_OK;
|
|
|
|
}
|
|
|
|
if (vstatus.flag_external_manual_override_ok) {
|
2013-02-13 02:43:51 -04:00
|
|
|
set |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
|
2013-01-14 04:19:01 -04:00
|
|
|
} else {
|
2013-02-13 02:43:51 -04:00
|
|
|
clear |= PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK;
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
return io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, clear, set);
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_set_rc_config()
|
|
|
|
{
|
|
|
|
unsigned offset = 0;
|
|
|
|
int input_map[_max_rc_input];
|
|
|
|
int32_t ichan;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate the input channel -> control channel mapping table;
|
|
|
|
* assign RC_MAP_ROLL/PITCH/YAW/THROTTLE to the canonical
|
|
|
|
* controls.
|
|
|
|
*/
|
|
|
|
for (unsigned i = 0; i < _max_rc_input; i++)
|
|
|
|
input_map[i] = -1;
|
|
|
|
|
2013-02-25 04:07:13 -04:00
|
|
|
/*
|
|
|
|
* NOTE: The indices for mapped channels are 1-based
|
|
|
|
* for compatibility reasons with existing
|
|
|
|
* autopilots / GCS'.
|
|
|
|
*/
|
2013-01-16 17:02:49 -04:00
|
|
|
param_get(param_find("RC_MAP_ROLL"), &ichan);
|
|
|
|
if ((ichan >= 0) && (ichan < (int)_max_rc_input))
|
2013-02-25 04:07:13 -04:00
|
|
|
input_map[ichan - 1] = 0;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
param_get(param_find("RC_MAP_PITCH"), &ichan);
|
|
|
|
if ((ichan >= 0) && (ichan < (int)_max_rc_input))
|
2013-02-25 04:07:13 -04:00
|
|
|
input_map[ichan - 1] = 1;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
param_get(param_find("RC_MAP_YAW"), &ichan);
|
|
|
|
if ((ichan >= 0) && (ichan < (int)_max_rc_input))
|
2013-02-25 04:07:13 -04:00
|
|
|
input_map[ichan - 1] = 2;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
param_get(param_find("RC_MAP_THROTTLE"), &ichan);
|
|
|
|
if ((ichan >= 0) && (ichan < (int)_max_rc_input))
|
2013-02-25 04:07:13 -04:00
|
|
|
input_map[ichan - 1] = 3;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
ichan = 4;
|
|
|
|
for (unsigned i = 0; i < _max_rc_input; i++)
|
|
|
|
if (input_map[i] == -1)
|
|
|
|
input_map[i] = ichan++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate all possible RC inputs.
|
|
|
|
*/
|
|
|
|
for (unsigned i = 0; i < _max_rc_input; i++) {
|
|
|
|
uint16_t regs[PX4IO_P_RC_CONFIG_STRIDE];
|
|
|
|
char pname[16];
|
|
|
|
float fval;
|
|
|
|
|
2013-02-12 04:33:52 -04:00
|
|
|
/*
|
|
|
|
* RC params are floats, but do only
|
|
|
|
* contain integer values. Do not scale
|
|
|
|
* or cast them, let the auto-typeconversion
|
|
|
|
* do its job here.
|
|
|
|
* Channels: 500 - 2500
|
|
|
|
* Inverted flag: -1 (inverted) or 1 (normal)
|
|
|
|
*/
|
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
sprintf(pname, "RC%d_MIN", i + 1);
|
|
|
|
param_get(param_find(pname), &fval);
|
2013-02-12 04:33:52 -04:00
|
|
|
regs[PX4IO_P_RC_CONFIG_MIN] = fval;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
sprintf(pname, "RC%d_TRIM", i + 1);
|
|
|
|
param_get(param_find(pname), &fval);
|
2013-02-12 04:33:52 -04:00
|
|
|
regs[PX4IO_P_RC_CONFIG_CENTER] = fval;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
sprintf(pname, "RC%d_MAX", i + 1);
|
|
|
|
param_get(param_find(pname), &fval);
|
2013-02-12 04:33:52 -04:00
|
|
|
regs[PX4IO_P_RC_CONFIG_MAX] = fval;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
sprintf(pname, "RC%d_DZ", i + 1);
|
|
|
|
param_get(param_find(pname), &fval);
|
2013-02-12 04:33:52 -04:00
|
|
|
regs[PX4IO_P_RC_CONFIG_DEADZONE] = fval;
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
regs[PX4IO_P_RC_CONFIG_ASSIGNMENT] = input_map[i];
|
|
|
|
|
|
|
|
regs[PX4IO_P_RC_CONFIG_OPTIONS] = PX4IO_P_RC_CONFIG_OPTIONS_ENABLED;
|
|
|
|
sprintf(pname, "RC%d_REV", i + 1);
|
|
|
|
param_get(param_find(pname), &fval);
|
2013-02-12 04:33:52 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This has been taken for the sake of compatibility
|
|
|
|
* with APM's setup / mission planner: normal: 1,
|
|
|
|
* inverted: -1
|
|
|
|
*/
|
|
|
|
if (fval < 0) {
|
2013-01-16 17:02:49 -04:00
|
|
|
regs[PX4IO_P_RC_CONFIG_OPTIONS] |= PX4IO_P_RC_CONFIG_OPTIONS_REVERSE;
|
2013-02-12 04:33:52 -04:00
|
|
|
}
|
2013-01-16 17:02:49 -04:00
|
|
|
|
|
|
|
/* send channel config to IO */
|
|
|
|
ret = io_reg_set(PX4IO_PAGE_RC_CONFIG, offset, regs, PX4IO_P_RC_CONFIG_STRIDE);
|
2013-02-12 04:33:52 -04:00
|
|
|
if (ret != OK) {
|
2013-03-06 17:52:19 -04:00
|
|
|
log("rc config upload failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the IO initialisation flag */
|
2013-03-06 20:03:38 -04:00
|
|
|
if (!(io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS) & PX4IO_P_STATUS_FLAGS_INIT_OK)) {
|
2013-03-06 17:52:19 -04:00
|
|
|
log("config for RC%d rejected by IO", i + 1);
|
2013-01-16 17:02:49 -04:00
|
|
|
break;
|
2013-02-12 04:33:52 -04:00
|
|
|
}
|
2013-03-06 17:52:19 -04:00
|
|
|
|
2013-01-16 17:02:49 -04:00
|
|
|
offset += PX4IO_P_RC_CONFIG_STRIDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_handle_status(uint16_t status)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
/**
|
|
|
|
* WARNING: This section handles in-air resets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* check for IO reset - force it back to armed if necessary */
|
2013-02-17 12:47:26 -04:00
|
|
|
if (_status & PX4IO_P_STATUS_FLAGS_ARMED && !(status & PX4IO_P_STATUS_FLAGS_ARMED)
|
|
|
|
&& !(status & PX4IO_P_STATUS_FLAGS_ARM_SYNC)) {
|
2013-02-17 11:06:33 -04:00
|
|
|
/* set the arming flag */
|
2013-02-17 12:47:26 -04:00
|
|
|
ret = io_reg_modify(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS, 0, PX4IO_P_STATUS_FLAGS_ARMED | PX4IO_P_STATUS_FLAGS_ARM_SYNC);
|
2013-02-17 11:06:33 -04:00
|
|
|
|
|
|
|
/* set new status */
|
|
|
|
_status = status;
|
|
|
|
_status &= PX4IO_P_STATUS_FLAGS_ARMED;
|
2013-02-17 12:47:26 -04:00
|
|
|
} else if (!(_status & PX4IO_P_STATUS_FLAGS_ARM_SYNC)) {
|
|
|
|
|
|
|
|
/* set the sync flag */
|
|
|
|
ret = io_reg_modify(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS, 0, PX4IO_P_STATUS_FLAGS_ARM_SYNC);
|
|
|
|
/* set new status */
|
|
|
|
_status = status;
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
/* set new status */
|
|
|
|
_status = status;
|
|
|
|
}
|
2013-02-17 12:47:26 -04:00
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::io_handle_alarms(uint16_t alarms)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* XXX handle alarms */
|
|
|
|
|
|
|
|
|
|
|
|
/* set new alarms state */
|
|
|
|
_alarms = alarms;
|
2013-02-24 17:40:46 -04:00
|
|
|
|
|
|
|
return 0;
|
2013-02-17 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_get_status()
|
|
|
|
{
|
2013-01-16 03:01:04 -04:00
|
|
|
uint16_t regs[4];
|
2013-01-14 04:19:01 -04:00
|
|
|
int ret;
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/* get STATUS_FLAGS, STATUS_ALARMS, STATUS_VBATT, STATUS_IBATT in that order */
|
2013-01-23 22:56:03 -04:00
|
|
|
ret = io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS, ®s[0], sizeof(regs) / sizeof(regs[0]));
|
2013-01-16 03:01:04 -04:00
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
2013-02-17 11:06:33 -04:00
|
|
|
io_handle_status(regs[0]);
|
|
|
|
io_handle_alarms(regs[1]);
|
2013-01-14 04:19:01 -04:00
|
|
|
|
|
|
|
/* only publish if battery has a valid minimum voltage */
|
2013-01-16 03:01:04 -04:00
|
|
|
if (regs[2] > 3300) {
|
2013-01-14 04:19:01 -04:00
|
|
|
battery_status_s battery_status;
|
|
|
|
|
|
|
|
battery_status.timestamp = hrt_absolute_time();
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/* voltage is scaled to mV */
|
|
|
|
battery_status.voltage_v = regs[2] / 1000.0f;
|
|
|
|
|
|
|
|
/* current scaling should be to cA in order to avoid limiting at 65A */
|
|
|
|
battery_status.current_a = regs[3] / 100.f;
|
|
|
|
|
|
|
|
/* this requires integration over time - not currently implemented */
|
2013-01-14 04:19:01 -04:00
|
|
|
battery_status.discharged_mah = -1.0f;
|
|
|
|
|
|
|
|
/* lazily publish the battery voltage */
|
|
|
|
if (_to_battery > 0) {
|
|
|
|
orb_publish(ORB_ID(battery_status), _to_battery, &battery_status);
|
|
|
|
} else {
|
|
|
|
_to_battery = orb_advertise(ORB_ID(battery_status), &battery_status);
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 03:01:04 -04:00
|
|
|
return ret;
|
2012-10-20 02:10:12 -03:00
|
|
|
}
|
|
|
|
|
2013-01-14 04:30:18 -04:00
|
|
|
int
|
2013-01-16 03:01:04 -04:00
|
|
|
PX4IO::io_get_raw_rc_input(rc_input_values &input_rc)
|
2013-01-14 04:30:18 -04:00
|
|
|
{
|
2013-01-23 22:56:03 -04:00
|
|
|
uint32_t channel_count;
|
2013-01-24 00:18:18 -04:00
|
|
|
int ret = OK;
|
2013-01-14 04:30:18 -04:00
|
|
|
|
|
|
|
/* we don't have the status bits, so input_source has to be set elsewhere */
|
|
|
|
input_rc.input_source = RC_INPUT_SOURCE_UNKNOWN;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Because the channel count and channel data are fetched
|
|
|
|
* separately, there is a risk of a race between the two
|
|
|
|
* that could leave us with channel data and a count that
|
|
|
|
* are out of sync.
|
|
|
|
* Fixing this would require a guarantee of atomicity from
|
|
|
|
* IO, and a single fetch for both count and channels.
|
|
|
|
*
|
|
|
|
* XXX Since IO has the input calibration info, we ought to be
|
|
|
|
* able to get the pre-fixed-up controls directly.
|
2013-01-16 03:01:04 -04:00
|
|
|
*
|
|
|
|
* XXX can we do this more cheaply? If we knew we had DMA, it would
|
|
|
|
* almost certainly be better to just get all the inputs...
|
2013-01-14 04:30:18 -04:00
|
|
|
*/
|
2013-01-23 22:56:03 -04:00
|
|
|
channel_count = io_reg_get(PX4IO_PAGE_RAW_RC_INPUT, PX4IO_P_RAW_RC_COUNT);
|
|
|
|
if (channel_count == _io_reg_get_error)
|
2013-01-24 00:18:18 -04:00
|
|
|
return -EIO;
|
2013-01-23 22:56:03 -04:00
|
|
|
if (channel_count > RC_INPUT_MAX_CHANNELS)
|
|
|
|
channel_count = RC_INPUT_MAX_CHANNELS;
|
2013-01-14 04:30:18 -04:00
|
|
|
input_rc.channel_count = channel_count;
|
|
|
|
|
2013-01-26 04:01:25 -04:00
|
|
|
if (channel_count > 0) {
|
2013-01-16 03:01:04 -04:00
|
|
|
ret = io_reg_get(PX4IO_PAGE_RAW_RC_INPUT, PX4IO_P_RAW_RC_BASE, input_rc.values, channel_count);
|
2013-01-26 04:01:25 -04:00
|
|
|
if (ret == OK)
|
|
|
|
input_rc.timestamp = hrt_absolute_time();
|
|
|
|
}
|
2013-01-14 04:30:18 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_publish_raw_rc()
|
|
|
|
{
|
2013-01-26 04:01:25 -04:00
|
|
|
/* if no raw RC, just don't publish */
|
2013-01-16 03:01:04 -04:00
|
|
|
if (!(_status & PX4IO_P_STATUS_FLAGS_RC_OK))
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* fetch values from IO */
|
|
|
|
rc_input_values rc_val;
|
|
|
|
rc_val.timestamp = hrt_absolute_time();
|
|
|
|
|
|
|
|
int ret = io_get_raw_rc_input(rc_val);
|
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* sort out the source of the values */
|
|
|
|
if (_status & PX4IO_P_STATUS_FLAGS_RC_PPM) {
|
|
|
|
rc_val.input_source = RC_INPUT_SOURCE_PX4IO_PPM;
|
|
|
|
} else if (_status & PX4IO_P_STATUS_FLAGS_RC_DSM) {
|
|
|
|
rc_val.input_source = RC_INPUT_SOURCE_PX4IO_SPEKTRUM;
|
|
|
|
} else if (_status & PX4IO_P_STATUS_FLAGS_RC_SBUS) {
|
|
|
|
rc_val.input_source = RC_INPUT_SOURCE_PX4IO_SBUS;
|
|
|
|
} else {
|
|
|
|
rc_val.input_source = RC_INPUT_SOURCE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lazily advertise on first publication */
|
|
|
|
if (_to_input_rc == 0) {
|
|
|
|
_to_input_rc = orb_advertise(ORB_ID(input_rc), &rc_val);
|
|
|
|
} else {
|
|
|
|
orb_publish(ORB_ID(input_rc), _to_input_rc, &rc_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::io_publish_mixed_controls()
|
|
|
|
{
|
|
|
|
/* if no FMU comms(!) just don't publish */
|
|
|
|
if (!(_status & PX4IO_P_STATUS_FLAGS_FMU_OK))
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* if not taking raw PPM from us, must be mixing */
|
2013-01-26 16:27:03 -04:00
|
|
|
if (_status & PX4IO_P_STATUS_FLAGS_RAW_PWM)
|
2013-01-16 03:01:04 -04:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* data we are going to fetch */
|
|
|
|
actuator_controls_effective_s controls_effective;
|
|
|
|
controls_effective.timestamp = hrt_absolute_time();
|
|
|
|
|
|
|
|
/* get actuator controls from IO */
|
|
|
|
uint16_t act[_max_actuators];
|
|
|
|
int ret = io_reg_get(PX4IO_PAGE_ACTUATORS, 0, act, _max_actuators);
|
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* convert from register format to float */
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
controls_effective.control_effective[i] = REG_TO_FLOAT(act[i]);
|
|
|
|
|
|
|
|
/* laxily advertise on first publication */
|
|
|
|
if (_to_actuators_effective == 0) {
|
|
|
|
_to_actuators_effective =
|
|
|
|
orb_advertise((_primary_pwm_device ?
|
|
|
|
ORB_ID_VEHICLE_ATTITUDE_CONTROLS_EFFECTIVE :
|
|
|
|
ORB_ID(actuator_controls_effective_1)),
|
|
|
|
&controls_effective);
|
|
|
|
} else {
|
|
|
|
orb_publish((_primary_pwm_device ?
|
|
|
|
ORB_ID_VEHICLE_ATTITUDE_CONTROLS_EFFECTIVE :
|
|
|
|
ORB_ID(actuator_controls_effective_1)),
|
|
|
|
_to_actuators_effective, &controls_effective);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PX4IO::io_publish_pwm_outputs()
|
|
|
|
{
|
|
|
|
/* if no FMU comms(!) just don't publish */
|
|
|
|
if (!(_status & PX4IO_P_STATUS_FLAGS_FMU_OK))
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* data we are going to fetch */
|
|
|
|
actuator_outputs_s outputs;
|
|
|
|
outputs.timestamp = hrt_absolute_time();
|
|
|
|
|
|
|
|
/* get servo values from IO */
|
|
|
|
uint16_t ctl[_max_actuators];
|
|
|
|
int ret = io_reg_get(PX4IO_PAGE_SERVOS, 0, ctl, _max_actuators);
|
|
|
|
if (ret != OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* convert from register format to float */
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
2013-03-09 19:16:55 -04:00
|
|
|
outputs.output[i] = ctl[i];
|
2013-01-16 03:01:04 -04:00
|
|
|
outputs.noutputs = _max_actuators;
|
|
|
|
|
|
|
|
/* lazily advertise on first publication */
|
|
|
|
if (_to_outputs == 0) {
|
|
|
|
_to_outputs = orb_advertise((_primary_pwm_device ?
|
|
|
|
ORB_ID_VEHICLE_CONTROLS :
|
|
|
|
ORB_ID(actuator_outputs_1)),
|
|
|
|
&outputs);
|
|
|
|
} else {
|
|
|
|
orb_publish((_primary_pwm_device ?
|
|
|
|
ORB_ID_VEHICLE_CONTROLS :
|
|
|
|
ORB_ID(actuator_outputs_1)),
|
|
|
|
_to_outputs,
|
|
|
|
&outputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_reg_set(uint8_t page, uint8_t offset, const uint16_t *values, unsigned num_values)
|
|
|
|
{
|
2013-01-26 17:15:57 -04:00
|
|
|
/* range check the transfer */
|
|
|
|
if (num_values > ((_max_transfer) / sizeof(*values))) {
|
|
|
|
debug("io_reg_set: too many registers (%u, max %u)", num_values, _max_transfer / 2);
|
2013-01-24 00:18:18 -04:00
|
|
|
return -EINVAL;
|
2013-01-26 17:15:57 -04:00
|
|
|
}
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2013-01-26 20:16:19 -04:00
|
|
|
/* set up the transfer */
|
|
|
|
uint8_t addr[2] = {
|
|
|
|
page,
|
|
|
|
offset
|
|
|
|
};
|
|
|
|
i2c_msg_s msgv[2];
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2013-01-26 20:16:19 -04:00
|
|
|
msgv[0].flags = 0;
|
|
|
|
msgv[0].buffer = addr;
|
|
|
|
msgv[0].length = 2;
|
|
|
|
msgv[1].flags = I2C_M_NORESTART;
|
|
|
|
msgv[1].buffer = (uint8_t *)values;
|
|
|
|
msgv[1].length = num_values * sizeof(*values);
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2013-01-26 17:15:57 -04:00
|
|
|
/* perform the transfer */
|
2013-01-26 20:16:19 -04:00
|
|
|
int ret = transfer(msgv, 2);
|
2013-01-23 22:56:03 -04:00
|
|
|
if (ret != OK)
|
2013-01-24 00:18:18 -04:00
|
|
|
debug("io_reg_set: error %d", ret);
|
2013-01-23 22:56:03 -04:00
|
|
|
return ret;
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_reg_set(uint8_t page, uint8_t offset, uint16_t value)
|
|
|
|
{
|
|
|
|
return io_reg_set(page, offset, &value, 1);
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_reg_get(uint8_t page, uint8_t offset, uint16_t *values, unsigned num_values)
|
|
|
|
{
|
2013-01-26 20:16:19 -04:00
|
|
|
/* set up the transfer */
|
|
|
|
uint8_t addr[2] = {
|
|
|
|
page,
|
|
|
|
offset
|
|
|
|
};
|
|
|
|
i2c_msg_s msgv[2];
|
|
|
|
|
|
|
|
msgv[0].flags = 0;
|
|
|
|
msgv[0].buffer = addr;
|
|
|
|
msgv[0].length = 2;
|
|
|
|
msgv[1].flags = I2C_M_READ;
|
|
|
|
msgv[1].buffer = (uint8_t *)values;
|
|
|
|
msgv[1].length = num_values * sizeof(*values);
|
2013-01-14 04:19:01 -04:00
|
|
|
|
2013-01-26 20:16:19 -04:00
|
|
|
/* perform the transfer */
|
|
|
|
int ret = transfer(msgv, 2);
|
2013-01-23 22:56:03 -04:00
|
|
|
if (ret != OK)
|
2013-01-24 00:18:18 -04:00
|
|
|
debug("io_reg_get: data error %d", ret);
|
2013-01-23 22:56:03 -04:00
|
|
|
return ret;
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
uint32_t
|
|
|
|
PX4IO::io_reg_get(uint8_t page, uint8_t offset)
|
|
|
|
{
|
|
|
|
uint16_t value;
|
|
|
|
|
|
|
|
if (io_reg_get(page, offset, &value, 1))
|
|
|
|
return _io_reg_get_error;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:19:01 -04:00
|
|
|
int
|
|
|
|
PX4IO::io_reg_modify(uint8_t page, uint8_t offset, uint16_t clearbits, uint16_t setbits)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint16_t value;
|
|
|
|
|
|
|
|
ret = io_reg_get(page, offset, &value, 1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
value &= ~clearbits;
|
|
|
|
value |= setbits;
|
|
|
|
|
2013-01-23 22:56:03 -04:00
|
|
|
return io_reg_set(page, offset, value);
|
2013-01-14 04:19:01 -04:00
|
|
|
}
|
|
|
|
|
2012-12-30 05:17:19 -04:00
|
|
|
int
|
|
|
|
PX4IO::mixer_send(const char *buf, unsigned buflen)
|
|
|
|
{
|
2013-01-16 03:01:04 -04:00
|
|
|
uint8_t frame[_max_transfer];
|
2012-12-30 05:17:19 -04:00
|
|
|
px4io_mixdata *msg = (px4io_mixdata *)&frame[0];
|
2013-01-16 03:01:04 -04:00
|
|
|
unsigned max_len = _max_transfer - sizeof(px4io_mixdata);
|
2012-12-30 05:17:19 -04:00
|
|
|
|
|
|
|
msg->f2i_mixer_magic = F2I_MIXER_MAGIC;
|
|
|
|
msg->action = F2I_MIXER_ACTION_RESET;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned count = buflen;
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
if (count > max_len)
|
|
|
|
count = max_len;
|
2012-12-30 05:17:19 -04:00
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
memcpy(&msg->text[0], buf, count);
|
|
|
|
buf += count;
|
|
|
|
buflen -= count;
|
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/*
|
|
|
|
* We have to send an even number of bytes. This
|
|
|
|
* will only happen on the very last transfer of a
|
|
|
|
* mixer, and we are guaranteed that there will be
|
|
|
|
* space left to round up as _max_transfer will be
|
|
|
|
* even.
|
|
|
|
*/
|
|
|
|
unsigned total_len = sizeof(px4io_mixdata) + count;
|
|
|
|
if (total_len % 1) {
|
|
|
|
msg->text[count] = '\0';
|
|
|
|
total_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = io_reg_set(PX4IO_PAGE_MIXERLOAD, 0, (uint16_t *)frame, total_len / 2);
|
2012-12-30 05:17:19 -04:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
log("mixer send error %d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->action = F2I_MIXER_ACTION_APPEND;
|
|
|
|
|
|
|
|
} while (buflen > 0);
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/* check for the mixer-OK flag */
|
2013-02-24 15:24:21 -04:00
|
|
|
if (io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS) & PX4IO_P_STATUS_FLAGS_MIXER_OK) {
|
|
|
|
debug("mixer upload OK");
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_info(_mavlink_fd, "[IO] mixer upload ok");
|
2013-01-16 03:01:04 -04:00
|
|
|
return 0;
|
2013-02-24 15:24:21 -04:00
|
|
|
} else {
|
|
|
|
debug("mixer rejected by IO");
|
2013-03-07 15:51:33 -04:00
|
|
|
mavlink_log_info(_mavlink_fd, "[IO] mixer upload fail");
|
2013-02-24 15:24:21 -04:00
|
|
|
}
|
2013-01-26 01:54:04 -04:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
/* load must have failed for some reason */
|
|
|
|
return -EINVAL;
|
2012-12-30 05:17:19 -04:00
|
|
|
}
|
|
|
|
|
2013-02-24 15:24:21 -04:00
|
|
|
void
|
|
|
|
PX4IO::print_status()
|
|
|
|
{
|
2013-02-24 17:40:46 -04:00
|
|
|
/* basic configuration */
|
|
|
|
printf("protocol %u software %u bootloader %u buffer %uB\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_PROTOCOL_VERSION),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_SOFTWARE_VERSION),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_BOOTLOADER_VERSION),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_MAX_TRANSFER));
|
|
|
|
printf("%u controls %u actuators %u R/C inputs %u analog inputs %u relays\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_CONTROL_COUNT),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_ACTUATOR_COUNT),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_RC_INPUT_COUNT),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_ADC_INPUT_COUNT),
|
|
|
|
io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_RELAY_COUNT));
|
|
|
|
|
|
|
|
/* status */
|
|
|
|
printf("%u bytes free\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FREEMEM));
|
|
|
|
uint16_t flags = io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS);
|
2013-03-06 20:03:38 -04:00
|
|
|
printf("status 0x%04x%s%s%s%s%s%s%s%s%s%s%s\n",
|
2013-02-24 17:40:46 -04:00
|
|
|
flags,
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_ARMED) ? " ARMED" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_OVERRIDE) ? " OVERRIDE" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_RC_OK) ? " RC_OK" : " RC_FAIL"),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_RC_PPM) ? " PPM" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_RC_DSM) ? " DSM" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_RC_SBUS) ? " SBUS" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_FMU_OK) ? " FMU_OK" : " FMU_FAIL"),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_RAW_PWM) ? " RAW_PPM" : ""),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_MIXER_OK) ? " MIXER_OK" : " MIXER_FAIL"),
|
2013-03-06 20:03:38 -04:00
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_ARM_SYNC) ? " ARM_SYNC" : " ARM_NO_SYNC"),
|
|
|
|
((flags & PX4IO_P_STATUS_FLAGS_INIT_OK) ? " INIT_OK" : " INIT_FAIL"));
|
2013-02-24 17:40:46 -04:00
|
|
|
uint16_t alarms = io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_ALARMS);
|
|
|
|
printf("alarms 0x%04x%s%s%s%s%s%s\n",
|
|
|
|
alarms,
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_VBATT_LOW) ? " VBATT_LOW" : ""),
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_TEMPERATURE) ? " TEMPERATURE" : ""),
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_SERVO_CURRENT) ? " SERVO_CURRENT" : ""),
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_ACC_CURRENT) ? " ACC_CURRENT" : ""),
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_FMU_LOST) ? " FMU_LOST" : ""),
|
|
|
|
((alarms & PX4IO_P_STATUS_ALARMS_RC_LOST) ? " RC_LOST" : ""));
|
|
|
|
printf("vbatt %u ibatt %u\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_VBATT),
|
|
|
|
io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_IBATT));
|
|
|
|
printf("actuators");
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_ACTUATORS, i));
|
|
|
|
printf("\n");
|
|
|
|
printf("servos");
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_SERVOS, i));
|
|
|
|
printf("\n");
|
|
|
|
uint16_t raw_inputs = io_reg_get(PX4IO_PAGE_RAW_RC_INPUT, PX4IO_P_RAW_RC_COUNT);
|
2013-02-24 17:58:52 -04:00
|
|
|
printf("%d raw R/C inputs", raw_inputs);
|
2013-02-24 17:40:46 -04:00
|
|
|
for (unsigned i = 0; i < raw_inputs; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_RAW_RC_INPUT, PX4IO_P_RAW_RC_BASE + i));
|
|
|
|
printf("\n");
|
2013-02-24 17:58:52 -04:00
|
|
|
uint16_t mapped_inputs = io_reg_get(PX4IO_PAGE_RC_INPUT, PX4IO_P_RC_VALID);
|
|
|
|
printf("mapped R/C inputs 0x%04x", mapped_inputs);
|
2013-02-24 17:40:46 -04:00
|
|
|
for (unsigned i = 0; i < _max_rc_input; i++) {
|
2013-02-24 18:06:28 -04:00
|
|
|
if (mapped_inputs & (1 << i))
|
2013-02-24 19:55:38 -04:00
|
|
|
printf(" %u:%d", i, REG_TO_SIGNED(io_reg_get(PX4IO_PAGE_RC_INPUT, PX4IO_P_RC_BASE + i)));
|
2013-02-24 15:24:21 -04:00
|
|
|
}
|
2013-02-24 17:40:46 -04:00
|
|
|
printf("\n");
|
|
|
|
uint16_t adc_inputs = io_reg_get(PX4IO_PAGE_CONFIG, PX4IO_P_CONFIG_ADC_INPUT_COUNT);
|
|
|
|
printf("ADC inputs");
|
|
|
|
for (unsigned i = 0; i < adc_inputs; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_RAW_ADC_INPUT, i));
|
|
|
|
printf("\n");
|
|
|
|
|
2013-02-24 18:30:56 -04:00
|
|
|
/* setup and state */
|
2013-02-24 17:40:46 -04:00
|
|
|
printf("features 0x%04x\n", io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_FEATURES));
|
|
|
|
uint16_t arming = io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING);
|
2013-02-24 17:58:52 -04:00
|
|
|
printf("arming 0x%04x%s%s%s%s\n",
|
|
|
|
arming,
|
|
|
|
((arming & PX4IO_P_SETUP_ARMING_ARM_OK) ? " ARM_OK" : ""),
|
|
|
|
((arming & PX4IO_P_SETUP_ARMING_MANUAL_OVERRIDE_OK) ? " MANUAL_OVERRIDE_OK" : ""),
|
|
|
|
((arming & PX4IO_P_SETUP_ARMING_VECTOR_FLIGHT_OK) ? " VECTOR_FLIGHT_OK" : ""),
|
|
|
|
((arming & PX4IO_P_SETUP_ARMING_INAIR_RESTART_OK) ? " INAIR_RESTART_OK" : ""));
|
2013-02-24 17:40:46 -04:00
|
|
|
printf("rates 0x%04x lowrate %u highrate %u relays 0x%04x\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_PWM_RATES),
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_PWM_LOWRATE),
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_PWM_HIGHRATE),
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_RELAYS));
|
|
|
|
printf("vbatt scale %u ibatt scale %u ibatt bias %u\n",
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_VBATT_SCALE),
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_IBATT_SCALE),
|
|
|
|
io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_IBATT_BIAS));
|
|
|
|
printf("debuglevel %u\n", io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_SET_DEBUG));
|
2013-02-24 19:31:01 -04:00
|
|
|
printf("controls");
|
|
|
|
for (unsigned i = 0; i < _max_controls; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_CONTROLS, i));
|
2013-02-24 17:40:46 -04:00
|
|
|
printf("\n");
|
2013-02-24 18:30:56 -04:00
|
|
|
for (unsigned i = 0; i < _max_rc_input; i++) {
|
|
|
|
unsigned base = PX4IO_P_RC_CONFIG_STRIDE * i;
|
|
|
|
uint16_t options = io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_OPTIONS);
|
|
|
|
printf("input %u min %u center %u max %u deadzone %u assigned %u options 0x%04x%s%s\n",
|
|
|
|
i,
|
|
|
|
io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_MIN),
|
|
|
|
io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_CENTER),
|
|
|
|
io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_MAX),
|
|
|
|
io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_DEADZONE),
|
|
|
|
io_reg_get(PX4IO_PAGE_RC_CONFIG, base + PX4IO_P_RC_CONFIG_ASSIGNMENT),
|
|
|
|
options,
|
|
|
|
((options & PX4IO_P_RC_CONFIG_OPTIONS_ENABLED) ? " ENABLED" : ""),
|
|
|
|
((options & PX4IO_P_RC_CONFIG_OPTIONS_REVERSE) ? " REVERSED" : ""));
|
|
|
|
}
|
2013-02-24 19:31:01 -04:00
|
|
|
printf("failsafe");
|
|
|
|
for (unsigned i = 0; i < _max_actuators; i++)
|
|
|
|
printf(" %u", io_reg_get(PX4IO_PAGE_FAILSAFE_PWM, i));
|
|
|
|
printf("\n");
|
2013-02-24 15:24:21 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* regular ioctl? */
|
|
|
|
switch (cmd) {
|
|
|
|
case PWM_SERVO_ARM:
|
2013-01-15 04:41:47 -04:00
|
|
|
/* set the 'armed' bit */
|
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, 0, PX4IO_P_SETUP_ARMING_ARM_OK);
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_DISARM:
|
2013-01-15 04:41:47 -04:00
|
|
|
/* clear the 'armed' bit */
|
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, PX4IO_P_SETUP_ARMING_ARM_OK, 0);
|
2012-08-04 19:12:36 -03:00
|
|
|
break;
|
|
|
|
|
2013-02-17 23:16:09 -04:00
|
|
|
case PWM_SERVO_INAIR_RESTART_ENABLE:
|
|
|
|
/* set the 'in-air restart' bit */
|
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, 0, PX4IO_P_SETUP_ARMING_INAIR_RESTART_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PWM_SERVO_INAIR_RESTART_DISABLE:
|
|
|
|
/* unset the 'in-air restart' bit */
|
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_ARMING, PX4IO_P_SETUP_ARMING_INAIR_RESTART_OK, 0);
|
|
|
|
break;
|
|
|
|
|
2013-01-05 15:31:00 -04:00
|
|
|
case PWM_SERVO_SET_UPDATE_RATE:
|
2013-01-15 04:41:47 -04:00
|
|
|
/* set the requested rate */
|
|
|
|
if ((arg >= 50) && (arg <= 400)) {
|
|
|
|
ret = io_reg_set(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_PWM_HIGHRATE, arg);
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2013-01-05 15:31:00 -04:00
|
|
|
break;
|
|
|
|
|
2013-01-26 16:27:42 -04:00
|
|
|
case PWM_SERVO_GET_COUNT:
|
|
|
|
*(unsigned *)arg = _max_actuators;
|
|
|
|
break;
|
|
|
|
|
2013-01-26 17:16:39 -04:00
|
|
|
case PWM_SERVO_SET_DEBUG:
|
|
|
|
/* set the debug level */
|
|
|
|
ret = io_reg_set(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_SET_DEBUG, arg);
|
|
|
|
break;
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
case PWM_SERVO_SET(0) ... PWM_SERVO_SET(PWM_OUTPUT_MAX_CHANNELS): {
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2013-01-15 04:41:47 -04:00
|
|
|
unsigned channel = cmd - PWM_SERVO_SET(0);
|
2013-01-16 03:01:04 -04:00
|
|
|
if ((channel >= _max_actuators) || (arg < 900) || (arg > 2100)) {
|
2012-10-20 02:10:12 -03:00
|
|
|
ret = -EINVAL;
|
2013-01-16 03:01:04 -04:00
|
|
|
} else {
|
|
|
|
/* send a direct PWM value */
|
2013-01-26 17:14:52 -04:00
|
|
|
ret = io_reg_set(PX4IO_PAGE_DIRECT_PWM, channel, arg);
|
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;
|
2013-01-15 04:41:47 -04:00
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
case PWM_SERVO_GET(0) ... PWM_SERVO_GET(PWM_OUTPUT_MAX_CHANNELS): {
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2013-01-15 04:41:47 -04:00
|
|
|
unsigned channel = cmd - PWM_SERVO_GET(0);
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
if (channel >= _max_actuators) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
} else {
|
|
|
|
/* fetch a current PWM value */
|
2013-01-26 17:14:52 -04:00
|
|
|
uint32_t value = io_reg_get(PX4IO_PAGE_SERVOS, channel);
|
2013-01-16 03:01:04 -04:00
|
|
|
if (value == _io_reg_get_error) {
|
|
|
|
ret = -EIO;
|
|
|
|
} else {
|
|
|
|
*(servo_position_t *)arg = value;
|
|
|
|
}
|
|
|
|
}
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
2013-01-15 04:41:47 -04:00
|
|
|
}
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
case GPIO_RESET:
|
|
|
|
ret = io_reg_set(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_RELAYS, 0);
|
2012-12-21 01:31:02 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_SET:
|
2013-01-15 04:41:47 -04:00
|
|
|
arg &= ((1 << _max_relays) - 1);
|
2013-01-16 03:01:04 -04:00
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_RELAYS, 0, arg);
|
2013-01-15 04:41:47 -04:00
|
|
|
break;
|
|
|
|
|
2012-12-21 01:31:02 -04:00
|
|
|
case GPIO_CLEAR:
|
2013-01-15 04:41:47 -04:00
|
|
|
arg &= ((1 << _max_relays) - 1);
|
2013-01-16 03:01:04 -04:00
|
|
|
ret = io_reg_modify(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_RELAYS, arg, 0);
|
2012-12-21 01:31:02 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_GET:
|
2013-01-16 03:01:04 -04:00
|
|
|
*(uint32_t *)arg = io_reg_get(PX4IO_PAGE_SETUP, PX4IO_P_SETUP_RELAYS);
|
|
|
|
if (*(uint32_t *)arg == _io_reg_get_error)
|
|
|
|
ret = -EIO;
|
2012-12-21 01:31:02 -04:00
|
|
|
break;
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case MIXERIOCGETOUTPUTCOUNT:
|
2013-01-16 03:01:04 -04:00
|
|
|
*(unsigned *)arg = _max_actuators;
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
case MIXERIOCRESET:
|
2012-12-30 05:17:19 -04:00
|
|
|
ret = 0; /* load always resets */
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
|
|
|
|
2013-01-24 19:15:41 -04:00
|
|
|
case MIXERIOCLOADBUF: {
|
|
|
|
const char *buf = (const char *)arg;
|
|
|
|
ret = mixer_send(buf, strnlen(buf, 1024));
|
2012-10-20 02:10:12 -03:00
|
|
|
break;
|
2013-01-24 19:15:41 -04:00
|
|
|
}
|
2012-10-20 02:10:12 -03:00
|
|
|
|
2013-01-24 02:19:33 -04:00
|
|
|
case RC_INPUT_GET: {
|
|
|
|
uint16_t status;
|
|
|
|
rc_input_values *rc_val = (rc_input_values *)arg;
|
|
|
|
|
|
|
|
ret = io_reg_get(PX4IO_PAGE_STATUS, PX4IO_P_STATUS_FLAGS, &status, 1);
|
|
|
|
if (ret != OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if no R/C input, don't try to fetch anything */
|
|
|
|
if (!(status & PX4IO_P_STATUS_FLAGS_RC_OK)) {
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort out the source of the values */
|
|
|
|
if (status & PX4IO_P_STATUS_FLAGS_RC_PPM) {
|
|
|
|
rc_val->input_source = RC_INPUT_SOURCE_PX4IO_PPM;
|
|
|
|
} else if (status & PX4IO_P_STATUS_FLAGS_RC_DSM) {
|
|
|
|
rc_val->input_source = RC_INPUT_SOURCE_PX4IO_SPEKTRUM;
|
|
|
|
} else if (status & PX4IO_P_STATUS_FLAGS_RC_SBUS) {
|
|
|
|
rc_val->input_source = RC_INPUT_SOURCE_PX4IO_SBUS;
|
|
|
|
} else {
|
|
|
|
rc_val->input_source = RC_INPUT_SOURCE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read raw R/C input values */
|
|
|
|
ret = io_reg_get(PX4IO_PAGE_RAW_RC_INPUT, PX4IO_P_RAW_RC_BASE, &(rc_val->values[0]), _max_rc_input);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
default:
|
2013-02-24 15:24:21 -04:00
|
|
|
/* not a recognized value */
|
2012-08-04 19:12:36 -03:00
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
2012-10-24 03:38:45 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:41:47 -04:00
|
|
|
ssize_t
|
2013-01-16 03:01:04 -04:00
|
|
|
PX4IO::write(file *filp, const char *buffer, size_t len)
|
2013-01-15 04:41:47 -04:00
|
|
|
{
|
|
|
|
unsigned count = len / 2;
|
|
|
|
|
2013-01-26 20:16:19 -04:00
|
|
|
if (count > _max_actuators)
|
|
|
|
count = _max_actuators;
|
2013-01-26 17:15:27 -04:00
|
|
|
if (count > 0) {
|
|
|
|
int ret = io_reg_set(PX4IO_PAGE_DIRECT_PWM, 0, (uint16_t *)buffer, count);
|
|
|
|
if (ret != OK)
|
2013-01-26 20:16:19 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2013-01-26 17:15:27 -04:00
|
|
|
return count * 2;
|
|
|
|
}
|
2013-01-15 04:41:47 -04:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
extern "C" __EXPORT int px4io_main(int argc, char *argv[]);
|
|
|
|
|
2012-11-03 05:12:01 -03:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
void
|
|
|
|
test(void)
|
|
|
|
{
|
2013-01-26 20:16:19 -04:00
|
|
|
int fd;
|
2013-01-26 18:36:31 -04:00
|
|
|
unsigned servo_count = 0;
|
|
|
|
unsigned pwm_value = 1000;
|
|
|
|
int direction = 1;
|
|
|
|
int ret;
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
fd = open("/dev/px4io", O_WRONLY);
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
if (fd < 0)
|
|
|
|
err(1, "failed to open device");
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
if (ioctl(fd, PWM_SERVO_GET_COUNT, (unsigned long)&servo_count))
|
|
|
|
err(1, "failed to get servo count");
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
if (ioctl(fd, PWM_SERVO_ARM, 0))
|
|
|
|
err(1, "failed to arm servos");
|
2012-11-03 05:12:01 -03:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
for (;;) {
|
2012-12-30 05:28:07 -04:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
/* sweep all servos between 1000..2000 */
|
|
|
|
servo_position_t servos[servo_count];
|
|
|
|
for (unsigned i = 0; i < servo_count; i++)
|
|
|
|
servos[i] = pwm_value;
|
2012-12-30 05:28:07 -04:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
ret = write(fd, servos, sizeof(servos));
|
2013-03-07 15:51:33 -04:00
|
|
|
if (ret != (int)sizeof(servos))
|
2013-01-26 18:36:31 -04:00
|
|
|
err(1, "error writing PWM servo data, wrote %u got %d", sizeof(servos), ret);
|
2012-12-30 05:28:07 -04:00
|
|
|
|
2013-01-26 18:36:31 -04:00
|
|
|
if (direction > 0) {
|
|
|
|
if (pwm_value < 2000) {
|
|
|
|
pwm_value++;
|
|
|
|
} else {
|
|
|
|
direction = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pwm_value > 1000) {
|
|
|
|
pwm_value--;
|
|
|
|
} else {
|
|
|
|
direction = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readback servo values */
|
|
|
|
for (unsigned i = 0; i < servo_count; i++) {
|
|
|
|
servo_position_t value;
|
|
|
|
|
|
|
|
if (ioctl(fd, PWM_SERVO_GET(i), (unsigned long)&value))
|
|
|
|
err(1, "error reading PWM servo %d", i);
|
|
|
|
if (value != servos[i])
|
|
|
|
errx(1, "servo %d readback error, got %u expected %u", i, value, servos[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-11-03 05:12:01 -03:00
|
|
|
}
|
|
|
|
|
2012-12-04 03:19:12 -04:00
|
|
|
void
|
|
|
|
monitor(void)
|
|
|
|
{
|
2012-12-13 05:23:02 -04:00
|
|
|
unsigned cancels = 3;
|
2012-12-04 03:19:12 -04:00
|
|
|
printf("Hit <enter> three times to exit monitor mode\n");
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pollfd fds[1];
|
|
|
|
|
|
|
|
fds[0].fd = 0;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
poll(fds, 1, 500);
|
|
|
|
|
|
|
|
if (fds[0].revents == POLLIN) {
|
|
|
|
int c;
|
|
|
|
read(0, &c, 1);
|
2012-12-30 05:17:19 -04:00
|
|
|
|
2012-12-04 03:19:12 -04:00
|
|
|
if (cancels-- == 0)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-01-16 03:01:04 -04:00
|
|
|
#warning implement this
|
|
|
|
|
|
|
|
// if (g_dev != nullptr)
|
|
|
|
// g_dev->dump_one = true;
|
2012-12-04 03:19:12 -04:00
|
|
|
}
|
|
|
|
}
|
2012-11-03 05:12:01 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
int
|
|
|
|
px4io_main(int argc, char *argv[])
|
|
|
|
{
|
2013-02-05 13:11:59 -04:00
|
|
|
/* check for sufficient number of arguments */
|
|
|
|
if (argc < 2)
|
|
|
|
goto out;
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
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 */
|
2013-03-07 14:47:43 -04:00
|
|
|
(void)new PX4IO();
|
2012-08-04 19:12:36 -03:00
|
|
|
|
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-12-13 05:23:02 -04:00
|
|
|
/* look for the optional pwm update rate for the supported modes */
|
2013-03-07 15:51:33 -04:00
|
|
|
if ((argc > 2) && (strcmp(argv[2], "-u") == 0 || strcmp(argv[2], "--update-rate") == 0)) {
|
2012-12-13 05:23:02 -04:00
|
|
|
if (argc > 2 + 1) {
|
2013-01-16 03:01:04 -04:00
|
|
|
#warning implement this
|
2012-12-13 05:23:02 -04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "missing argument for pwm update rate (-u)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-20 02:10:12 -03:00
|
|
|
exit(0);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
2013-03-07 14:47:43 -04:00
|
|
|
if (!strcmp(argv[1], "recovery")) {
|
|
|
|
|
|
|
|
if (g_dev != nullptr) {
|
|
|
|
/*
|
|
|
|
* Enable in-air restart support.
|
|
|
|
* We can cheat and call the driver directly, as it
|
|
|
|
* doesn't reference filp in ioctl()
|
|
|
|
*/
|
|
|
|
g_dev->ioctl(NULL, PWM_SERVO_INAIR_RESTART_ENABLE, 0);
|
|
|
|
} else {
|
|
|
|
errx(1, "not loaded");
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-12-23 17:23:28 -04:00
|
|
|
if (!strcmp(argv[1], "stop")) {
|
|
|
|
|
2013-03-07 14:47:43 -04:00
|
|
|
if (g_dev != nullptr) {
|
|
|
|
/* stop the driver */
|
|
|
|
delete g_dev;
|
|
|
|
} else {
|
|
|
|
errx(1, "not loaded");
|
2012-12-23 17:23:28 -04:00
|
|
|
}
|
2013-03-07 14:47:43 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
2012-12-23 17:23:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "status")) {
|
|
|
|
|
2013-02-24 15:24:21 -04:00
|
|
|
if (g_dev != nullptr) {
|
2012-12-23 17:23:28 -04:00
|
|
|
printf("[px4io] loaded\n");
|
2013-02-24 15:24:21 -04:00
|
|
|
g_dev->print_status();
|
|
|
|
} else {
|
2012-12-23 17:23:28 -04:00
|
|
|
printf("[px4io] not loaded\n");
|
2013-02-24 15:24:21 -04:00
|
|
|
}
|
2012-12-23 17:23:28 -04:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-01-26 17:16:39 -04:00
|
|
|
if (!strcmp(argv[1], "debug")) {
|
|
|
|
if (argc <= 2) {
|
|
|
|
printf("usage: px4io debug LEVEL\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (g_dev == nullptr) {
|
|
|
|
printf("px4io is not started\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
uint8_t level = atoi(argv[2]);
|
2013-03-07 14:47:43 -04:00
|
|
|
/* we can cheat and call the driver directly, as it
|
|
|
|
* doesn't reference filp in ioctl()
|
|
|
|
*/
|
2013-01-26 17:16:39 -04:00
|
|
|
int ret = g_dev->ioctl(NULL, PWM_SERVO_SET_DEBUG, level);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("SET_DEBUG failed - %d\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("SET_DEBUG %u OK\n", (unsigned)level);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
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")) {
|
2012-12-23 17:23:28 -04:00
|
|
|
|
|
|
|
if (g_dev != nullptr) {
|
|
|
|
printf("[px4io] loaded, detaching first\n");
|
|
|
|
/* stop the driver */
|
|
|
|
delete g_dev;
|
|
|
|
}
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
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-11-29 04:35:21 -04:00
|
|
|
if (!strcmp(argv[1], "rx_dsm") ||
|
|
|
|
!strcmp(argv[1], "rx_dsm_10bit") ||
|
2012-11-30 04:02:47 -04:00
|
|
|
!strcmp(argv[1], "rx_dsm_11bit") ||
|
|
|
|
!strcmp(argv[1], "rx_sbus") ||
|
|
|
|
!strcmp(argv[1], "rx_ppm"))
|
|
|
|
errx(0, "receiver type is automatically detected, option '%s' is deprecated", argv[1]);
|
2012-12-04 03:19:12 -04:00
|
|
|
|
2012-11-05 04:55:22 -04:00
|
|
|
if (!strcmp(argv[1], "test"))
|
|
|
|
test();
|
2012-12-30 05:17:19 -04:00
|
|
|
|
2012-12-04 03:19:12 -04:00
|
|
|
if (!strcmp(argv[1], "monitor"))
|
|
|
|
monitor();
|
2012-11-05 04:55:22 -04:00
|
|
|
|
2013-02-05 13:11:59 -04:00
|
|
|
out:
|
2013-01-26 17:16:39 -04:00
|
|
|
errx(1, "need a command, try 'start', 'stop', 'status', 'test', 'monitor', 'debug' or 'update'");
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|