mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-22 11:23:34 +08:00
VOXL2 specific drivers, modules, and miscellaneous support files (#22588)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
############################################################################
|
||||
#
|
||||
# Copyright (c) 2015-2020 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
px4_add_module(
|
||||
MODULE drivers__voxl2_io
|
||||
MAIN voxl2_io
|
||||
SRCS
|
||||
voxl2_io_crc16.c
|
||||
voxl2_io_crc16.h
|
||||
voxl2_io_serial.cpp
|
||||
voxl2_io_serial.hpp
|
||||
voxl2_io_packet.c
|
||||
voxl2_io_packet.h
|
||||
voxl2_io_packet_types.h
|
||||
voxl2_io.cpp
|
||||
voxl2_io.hpp
|
||||
DEPENDS
|
||||
px4_work_queue
|
||||
mixer_module
|
||||
MODULE_CONFIG
|
||||
module.yaml
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
menuconfig DRIVERS_VOXL2_IO
|
||||
bool "voxl2_io"
|
||||
default n
|
||||
---help---
|
||||
Enable support for voxl2_io
|
||||
@@ -0,0 +1,13 @@
|
||||
module_name: VOXL2 IO Output
|
||||
actuator_output:
|
||||
config_parameters:
|
||||
- param: 'VOXL2_IO_MIN'
|
||||
label: 'PWM min value'
|
||||
- param: 'VOXL2_IO_MAX'
|
||||
label: 'PWM max value'
|
||||
output_groups:
|
||||
- param_prefix: VOXL2_IO
|
||||
group_label: 'PWMs'
|
||||
channel_label: 'PWM Channel'
|
||||
num_channels: 4
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,228 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2012-2022 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <board_config.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <drivers/drv_pwm_output.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <lib/rc/sbus.h>
|
||||
#include <lib/mathlib/mathlib.h>
|
||||
#include <lib/mixer_module/mixer_module.hpp>
|
||||
#include <lib/perf/perf_counter.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/getopt.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/PublicationMulti.hpp>
|
||||
#include <uORB/topics/parameter_update.h>
|
||||
#include <uORB/topics/input_rc.h>
|
||||
|
||||
#include "voxl2_io_serial.hpp"
|
||||
|
||||
#include "voxl2_io_packet.h"
|
||||
#include "voxl2_io_packet_types.h"
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
class Voxl2IO final : public ModuleBase<Voxl2IO>, public OutputModuleInterface
|
||||
{
|
||||
public:
|
||||
Voxl2IO();
|
||||
~Voxl2IO() override;
|
||||
|
||||
/** @see ModuleBase */
|
||||
static int task_spawn(int argc, char *argv[]);
|
||||
|
||||
/** @see ModuleBase */
|
||||
static int custom_command(int argc, char *argv[]);
|
||||
|
||||
/** @see ModuleBase */
|
||||
static int print_usage(const char *reason = nullptr);
|
||||
|
||||
/** @see ModuleBase::print_status() */
|
||||
int print_status() override;
|
||||
|
||||
/** @see OutputModuleInterface */
|
||||
bool updateOutputs(bool stop_motors, uint16_t outputs[input_rc_s::RC_INPUT_MAX_CHANNELS],
|
||||
unsigned num_outputs, unsigned num_control_groups_updated) override;
|
||||
|
||||
virtual int init();
|
||||
|
||||
void update_pwm_config();
|
||||
int get_version_info();
|
||||
|
||||
struct Command {
|
||||
uint16_t id = 0;
|
||||
uint8_t len = 0;
|
||||
uint16_t repeats = 0;
|
||||
uint16_t repeat_delay_us = 2000;
|
||||
uint8_t retries = 0;
|
||||
bool response = false;
|
||||
uint16_t resp_delay_us = 1000;
|
||||
bool print_feedback = false;
|
||||
|
||||
static const uint8_t BUF_SIZE = 128;
|
||||
uint8_t buf[BUF_SIZE];
|
||||
|
||||
bool valid() const { return len > 0; }
|
||||
void clear() { len = 0; }
|
||||
};
|
||||
|
||||
int send_cmd_thread_safe(Command *cmd);
|
||||
int receive_sbus();
|
||||
|
||||
void fill_rc_in(uint16_t raw_rc_count_local,
|
||||
uint16_t raw_rc_values_local[input_rc_s::RC_INPUT_MAX_CHANNELS],
|
||||
hrt_abstime now, bool frame_drop, bool failsafe,
|
||||
unsigned frame_drops, int rssi, input_rc_s &input_rc);
|
||||
private:
|
||||
void Run() override;
|
||||
bool stop_all_pwms();
|
||||
|
||||
/* PWM Parameters */
|
||||
static constexpr uint32_t VOXL2_IO_CONFIG = 0; // Default to off
|
||||
static constexpr uint32_t VOXL2_IO_BOARD_CONFIG_SIZE = 4; // PWM_MIN, PWM_MAX, 4 bytes
|
||||
static constexpr uint32_t VOXL2_IO_ESC_CAL_SIZE = 1;
|
||||
static constexpr uint32_t VOXL2_IO_DEFAULT_BAUD = 921600;
|
||||
static constexpr uint16_t VOXL2_IO_OUTPUT_CHANNELS = 4;
|
||||
static constexpr uint16_t VOXL2_IO_OUTPUT_DISABLED = 0;
|
||||
|
||||
static constexpr uint32_t VOXL2_IO_WRITE_WAIT_US = 200;
|
||||
static constexpr uint32_t VOXL2_IO_DISCONNECT_TIMEOUT_US = 500000;
|
||||
|
||||
static constexpr uint16_t DISARMED_VALUE = 0;
|
||||
|
||||
static constexpr uint16_t VOXL2_IO_MIXER_MIN = 0;
|
||||
static constexpr uint16_t VOXL2_IO_MIXER_MAX = 800;
|
||||
static constexpr uint16_t VOXL2_IO_MIXER_FAILSAFE = 0;
|
||||
static constexpr uint16_t VOXL2_IO_MIXER_DISARMED = 0;
|
||||
|
||||
static constexpr int32_t VOXL2_IO_DEFAULT_MIN = 1000;
|
||||
static constexpr int32_t VOXL2_IO_DEFAULT_MAX = 2000;
|
||||
static constexpr int32_t VOXL2_IO_DEFAULT_FAILSAFE = 900;
|
||||
static constexpr int32_t VOXL2_IO_TICS = 24; // 24 tics per us on M0065 timer clks
|
||||
|
||||
/* SBUS */
|
||||
static constexpr uint16_t VOXL2_IO_SBUS_FRAME_SIZE = 30;
|
||||
static constexpr uint16_t SBUS_PAYLOAD = 3;
|
||||
|
||||
/* M0065 version info */
|
||||
static constexpr uint16_t VOXL2_IO_VERSION_INFO_SIZE = 6;
|
||||
static constexpr uint16_t VOXL2_IO_SW_PROTOCOL_VERSION = 1;
|
||||
static constexpr uint16_t VOXL2_IO_HW_PROTOCOL_VERSION = 35;
|
||||
VOXL2_IO_VERSION_INFO _version_info;
|
||||
|
||||
/* Module update interval */
|
||||
static constexpr unsigned _current_update_interval{4000}; // 250 Hz
|
||||
|
||||
typedef struct {
|
||||
int32_t config{VOXL2_IO_CONFIG};
|
||||
int32_t baud_rate{VOXL2_IO_DEFAULT_BAUD};
|
||||
int32_t pwm_min{VOXL2_IO_DEFAULT_MIN};
|
||||
int32_t pwm_max{VOXL2_IO_DEFAULT_MAX};
|
||||
int32_t pwm_failsafe{VOXL2_IO_DEFAULT_FAILSAFE};
|
||||
int32_t param_rc_input_proto{0};
|
||||
int32_t param_rc_rssi_pwm_chan{0};
|
||||
int32_t function_map[VOXL2_IO_OUTPUT_CHANNELS] {0, 0, 0, 0};
|
||||
int32_t verbose_logging{0};
|
||||
} voxl2_io_params_t;
|
||||
voxl2_io_params_t _parameters;
|
||||
|
||||
typedef enum {
|
||||
PWM_MODE_START = 0,
|
||||
PWM_MODE_400,
|
||||
PWM_MODE_END
|
||||
} PWM_MODE;
|
||||
|
||||
enum RC_MODE {
|
||||
DISABLED = 0,
|
||||
SBUS,
|
||||
SPEKTRUM,
|
||||
EXTERNAL,
|
||||
SCAN
|
||||
} _rc_mode{RC_MODE::SCAN};
|
||||
|
||||
/* QUP7, VOXL2 J19, /dev/slpi-uart-7*/
|
||||
char _device[10] {VOXL2_IO_DEFAULT_PORT};
|
||||
Voxl2IoSerial *_uart_port;
|
||||
|
||||
/* Mixer output */
|
||||
MixingOutput _mixing_output;
|
||||
|
||||
/* RC input */
|
||||
VOXL2_IOPacket _sbus_packet;
|
||||
uint64_t _rc_last_valid;
|
||||
uint16_t _raw_rc_values[input_rc_s::RC_INPUT_MAX_CHANNELS] {UINT16_MAX};
|
||||
unsigned _sbus_frame_drops{0};
|
||||
uint16_t _sbus_total_frames{0};
|
||||
bool _new_packet{false};
|
||||
|
||||
/* Publications */
|
||||
uORB::PublicationMulti<input_rc_s> _rc_pub{ORB_ID(input_rc)};
|
||||
|
||||
/* Subscriptions */
|
||||
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)};
|
||||
|
||||
bool _pwm_on{false};
|
||||
int32_t _pwm_fullscale{0};
|
||||
int16_t _pwm_values[VOXL2_IO_OUTPUT_CHANNELS] = {0, 0, 0, 0};
|
||||
bool _outputs_disabled{false};
|
||||
|
||||
perf_counter_t _cycle_perf;
|
||||
perf_counter_t _output_update_perf;
|
||||
|
||||
bool _debug{false};
|
||||
uint16_t _cmd_id{0};
|
||||
Command _current_cmd;
|
||||
px4::atomic<Command *> _pending_cmd{nullptr};
|
||||
|
||||
static const uint8_t READ_BUF_SIZE = 128;
|
||||
uint8_t _read_buf[READ_BUF_SIZE];
|
||||
uint32_t _bytes_sent{0};
|
||||
uint32_t _bytes_received{0};
|
||||
uint32_t _packets_sent{0};
|
||||
uint32_t _packets_received{0};
|
||||
|
||||
int parse_response(uint8_t *buf, uint8_t len);
|
||||
int load_params(voxl2_io_params_t *params);
|
||||
int update_params();
|
||||
int flush_uart_rx();
|
||||
int calibrate_escs();
|
||||
};
|
||||
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
* Copyright (c) 2017 The Linux Foundation. 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 The Linux Foundation nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
* THIS LICENSE.
|
||||
* 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.
|
||||
* In addition Supplemental Terms apply. See the SUPPLEMENTAL file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "voxl2_io_crc16.h"
|
||||
|
||||
// Look-up table for fast CRC16 computations
|
||||
const uint16_t voxl2_io_crc16_table[256] = {
|
||||
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
|
||||
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
|
||||
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
|
||||
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
|
||||
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
|
||||
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
|
||||
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
|
||||
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
|
||||
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
|
||||
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
|
||||
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
|
||||
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
|
||||
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
|
||||
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
|
||||
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
|
||||
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
|
||||
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
|
||||
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
|
||||
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
|
||||
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
|
||||
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
|
||||
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
|
||||
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
|
||||
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
|
||||
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
|
||||
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
|
||||
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
|
||||
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
|
||||
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
|
||||
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
|
||||
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
|
||||
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
|
||||
};
|
||||
|
||||
uint16_t voxl2_io_crc16_init()
|
||||
{
|
||||
return 0xFFFF;
|
||||
}
|
||||
|
||||
uint16_t voxl2_io_crc16_byte(uint16_t prev_crc, const uint8_t new_byte)
|
||||
{
|
||||
uint8_t lut = (prev_crc ^ new_byte) & 0xFF;
|
||||
return (prev_crc >> 8) ^ voxl2_io_crc16_table[lut];
|
||||
}
|
||||
|
||||
uint16_t voxl2_io_crc16(uint16_t prev_crc, uint8_t const *input_buffer, uint16_t input_length)
|
||||
{
|
||||
uint16_t crc = prev_crc;
|
||||
|
||||
while (input_length--) {
|
||||
crc = voxl2_io_crc16_byte(crc, *input_buffer++);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
* Copyright (c) 2017 The Linux Foundation. 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 The Linux Foundation nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
* THIS LICENSE.
|
||||
* 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.
|
||||
* In addition Supplemental Terms apply. See the SUPPLEMENTAL file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/*
|
||||
* This file contains function prototypes for crc16 computations using polynomial 0x8005
|
||||
*/
|
||||
|
||||
#ifndef CRC16_H_
|
||||
#define CRC16_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Returns the seed of crc output, which should be used when computing crc16 of a byte sequence
|
||||
uint16_t voxl2_io_crc16_init(void);
|
||||
|
||||
// Process one byte by providing crc16 from previous step and new byte to consume.
|
||||
// Output is the new crc16 value
|
||||
uint16_t voxl2_io_crc16_byte(uint16_t prev_crc, const uint8_t new_byte);
|
||||
|
||||
// Process an array of bytes by providing crc16 from previous step (or seed), array of bytes and its length
|
||||
// Output is the new crc16 value
|
||||
uint16_t voxl2_io_crc16(uint16_t prev_crc, uint8_t const *input_buffer, uint16_t input_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //CRC16_H_
|
||||
@@ -0,0 +1,253 @@
|
||||
/****************************************************************************
|
||||
* Copyright (c) 2017 The Linux Foundation. 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 The Linux Foundation nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
* THIS LICENSE.
|
||||
* 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.
|
||||
* In addition Supplemental Terms apply. See the SUPPLEMENTAL file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include "voxl2_io_packet.h"
|
||||
#include "voxl2_io_packet_types.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int32_t voxl2_io_create_version_request_packet(uint8_t id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_VERSION_REQUEST, &id, 1, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_extended_version_request_packet(uint8_t id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_VERSION_EXT_REQUEST, &id, 1, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_reset_packet(uint8_t id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
char payload[] = "RESET0";
|
||||
payload[5] += id;
|
||||
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_RESET_CMD, (uint8_t *)payload, 6 /*sizeof(payload)*/, out, out_size);
|
||||
}
|
||||
|
||||
|
||||
int32_t voxl2_io_create_sound_packet(uint8_t frequency, uint8_t duration, uint8_t power, uint8_t mask, uint8_t *out,
|
||||
uint16_t out_size)
|
||||
{
|
||||
uint8_t data[4] = {frequency, duration, power, mask};
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_SOUND_CMD, (uint8_t *) & (data[0]), 4, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_led_control_packet(uint8_t led_byte_1, uint8_t led_byte_2, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
uint8_t data[2] = {led_byte_1, led_byte_2};
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_LED_CMD, (uint8_t *) & (data[0]), 2, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_set_id_packet(uint8_t id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_SET_ID_CMD, (uint8_t *)&id, 1, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_pwm_packet4(int16_t pwm0, int16_t pwm1, int16_t pwm2, int16_t pwm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
return voxl2_io_create_pwm_packet4_fb(pwm0, pwm1, pwm2, pwm3, led0, led1, led2, led3, -1, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_pwm_packet4_fb(int16_t pwm0, int16_t pwm1, int16_t pwm2, int16_t pwm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
int32_t fb_id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
uint16_t data[5];
|
||||
uint16_t leds = 0;
|
||||
|
||||
if (fb_id != -1) { fb_id = fb_id % 4; }
|
||||
|
||||
//limit the pwm commands
|
||||
|
||||
if (pwm0 > 800) { pwm0 = 800; } if (pwm0 < -800) { pwm0 = -800; }
|
||||
|
||||
if (pwm1 > 800) { pwm1 = 800; } if (pwm1 < -800) { pwm1 = -800; }
|
||||
|
||||
if (pwm2 > 800) { pwm2 = 800; } if (pwm2 < -800) { pwm2 = -800; }
|
||||
|
||||
if (pwm3 > 800) { pwm3 = 800; } if (pwm3 < -800) { pwm3 = -800; }
|
||||
|
||||
//least significant bit is used for feedback request
|
||||
pwm0 &= ~(0x0001); pwm1 &= ~(0x0001); pwm2 &= ~(0x0001); pwm3 &= ~(0x0001);
|
||||
|
||||
if (fb_id == 0) { pwm0 |= 0x0001; } if (fb_id == 1) { pwm1 |= 0x0001; }
|
||||
|
||||
if (fb_id == 2) { pwm2 |= 0x0001; } if (fb_id == 3) { pwm3 |= 0x0001; }
|
||||
|
||||
leds |= led0 & 0b00000111;
|
||||
leds |= (led1 & 0b00000111) << 3;
|
||||
leds |= ((uint16_t)(led2 & 0b00000111)) << 6;
|
||||
leds |= ((uint16_t)(led3 & 0b00000111)) << 9;
|
||||
|
||||
data[0] = pwm0; data[1] = pwm1; data[2] = pwm2; data[3] = pwm3; data[4] = leds;
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_PWM_CMD, (uint8_t *) & (data[0]), 10, out, out_size);
|
||||
}
|
||||
|
||||
|
||||
int32_t voxl2_io_create_rpm_packet4(int16_t rpm0, int16_t rpm1, int16_t rpm2, int16_t rpm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
return voxl2_io_create_rpm_packet4_fb(rpm0, rpm1, rpm2, rpm3, led0, led1, led2, led3, -1, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_rpm_packet4_fb(int16_t rpm0, int16_t rpm1, int16_t rpm2, int16_t rpm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
int32_t fb_id, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
uint16_t data[5];
|
||||
uint16_t leds = 0;
|
||||
|
||||
if (fb_id != -1) { fb_id = fb_id % 4; }
|
||||
|
||||
//least significant bit is used for feedback request
|
||||
rpm0 &= ~(0x0001); rpm1 &= ~(0x0001); rpm2 &= ~(0x0001); rpm3 &= ~(0x0001);
|
||||
|
||||
if (fb_id == 0) { rpm0 |= 0x0001; } if (fb_id == 1) { rpm1 |= 0x0001; }
|
||||
|
||||
if (fb_id == 2) { rpm2 |= 0x0001; } if (fb_id == 3) { rpm3 |= 0x0001; }
|
||||
|
||||
leds |= led0 & 0b00000111;
|
||||
leds |= (led1 & 0b00000111) << 3;
|
||||
leds |= ((uint16_t)(led2 & 0b00000111)) << 6;
|
||||
leds |= ((uint16_t)(led3 & 0b00000111)) << 9;
|
||||
|
||||
data[0] = rpm0; data[1] = rpm1; data[2] = rpm2; data[3] = rpm3; data[4] = leds;
|
||||
return voxl2_io_create_packet(VOXL2_IO_PACKET_TYPE_RPM_CMD, (uint8_t *) & (data[0]), 10, out, out_size);
|
||||
}
|
||||
|
||||
int32_t voxl2_io_create_packet(uint8_t type, uint8_t *data, uint16_t size, uint8_t *out, uint16_t out_size)
|
||||
{
|
||||
uint16_t packet_size = size + 5;
|
||||
|
||||
if (packet_size > 255) { return -1; }
|
||||
|
||||
if (out_size < packet_size) { return -2; }
|
||||
|
||||
out[0] = 0xAF;
|
||||
out[1] = packet_size;
|
||||
out[2] = type;
|
||||
|
||||
memcpy(&(out[3]), data, size);
|
||||
|
||||
uint16_t crc = voxl2_io_crc16_init();
|
||||
crc = voxl2_io_crc16(crc, &(out[1]), packet_size - 3);
|
||||
|
||||
memcpy(&(out[packet_size - 2]), &crc, sizeof(uint16_t));
|
||||
|
||||
return packet_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//feed in a character and see if we got a complete packet
|
||||
int16_t voxl2_io_packet_process_char(uint8_t c, VOXL2_IOPacket *packet)
|
||||
{
|
||||
int16_t ret = VOXL2_IO_NO_PACKET;
|
||||
|
||||
uint16_t chk_comp;
|
||||
uint16_t chk_rcvd;
|
||||
|
||||
if (packet->len_received >= (sizeof(packet->buffer) - 1)) {
|
||||
packet->len_received = 0;
|
||||
}
|
||||
|
||||
//reset the packet and start parsing from beginning if length byte == header
|
||||
//this can only happen if the packet is de-synced and last char of checksum
|
||||
//ends up being equal to the header, in that case we can end up in endless loop
|
||||
//unable to re-sync with the packet
|
||||
if (packet->len_received == 1 && c == VOXL2_IO_PACKET_HEADER) {
|
||||
packet->len_received = 0;
|
||||
}
|
||||
|
||||
switch (packet->len_received) {
|
||||
case 0: //header
|
||||
packet->bp = packet->buffer; //reset the pointer for storing data
|
||||
voxl2_io_packet_checksum_reset(packet); //reset the checksum to starting value
|
||||
|
||||
if (c != VOXL2_IO_PACKET_HEADER) { //check the packet header
|
||||
packet->len_received = 0;
|
||||
ret = VOXL2_IO_ERROR_BAD_HEADER;
|
||||
break;
|
||||
}
|
||||
|
||||
packet->len_received++;
|
||||
*(packet->bp)++ = c;
|
||||
break;
|
||||
|
||||
case 1: //length
|
||||
packet->len_received++;
|
||||
*(packet->bp)++ = c;
|
||||
packet->len_expected = c;
|
||||
|
||||
if (packet->len_expected >= (sizeof(packet->buffer) - 1)) {
|
||||
packet->len_received = 0;
|
||||
ret = VOXL2_IO_ERROR_BAD_LENGTH;
|
||||
break;
|
||||
}
|
||||
|
||||
voxl2_io_packet_checksum_process_char(packet, c);
|
||||
break;
|
||||
|
||||
default: //rest of the packet
|
||||
packet->len_received++;
|
||||
*(packet->bp)++ = c;
|
||||
|
||||
if (packet->len_received < (packet->len_expected - 1)) { //do not compute checksum of checksum (last 2 bytes)
|
||||
voxl2_io_packet_checksum_process_char(packet, c);
|
||||
}
|
||||
|
||||
if (packet->len_received < packet->len_expected) { //waiting for more bytes
|
||||
break;
|
||||
}
|
||||
|
||||
//grab the computed checksum and compare against the received value
|
||||
chk_comp = voxl2_io_packet_checksum_get(packet);
|
||||
|
||||
memcpy(&chk_rcvd, packet->bp - 2, sizeof(uint16_t));
|
||||
|
||||
if (chk_comp == chk_rcvd) { ret = packet->len_received; }
|
||||
|
||||
else { ret = VOXL2_IO_ERROR_BAD_CHECKSUM; }
|
||||
|
||||
packet->len_received = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
/****************************************************************************
|
||||
* Copyright (c) 2017 The Linux Foundation. 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 The Linux Foundation nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
* THIS LICENSE.
|
||||
* 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.
|
||||
* In addition Supplemental Terms apply. See the SUPPLEMENTAL file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/*
|
||||
* This file contains function prototypes for Voxl2 IO UART interface
|
||||
*/
|
||||
|
||||
#ifndef VOXL2_IO_PACKET
|
||||
#define VOXL2_IO_PACKET
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "voxl2_io_crc16.h"
|
||||
|
||||
// Define byte values that correspond to setting red, greed, and blue LEDs
|
||||
#define VOXL2_IO_LED_RED_ON 1
|
||||
#define VOXL2_IO_LED_GREEN_ON 2
|
||||
#define VOXL2_IO_LED_BLUE_ON 4
|
||||
|
||||
|
||||
// Header of the packet. Each packet must start with this header
|
||||
#define VOXL2_IO_PACKET_HEADER 0xAF
|
||||
|
||||
enum { VOXL2_IO_ERROR_BAD_LENGTH = -3,
|
||||
VOXL2_IO_ERROR_BAD_HEADER = -2,
|
||||
VOXL2_IO_ERROR_BAD_CHECKSUM = -1,
|
||||
VOXL2_IO_NO_PACKET = 0
|
||||
};
|
||||
|
||||
// Defines for the constatnt offsets of different parts of the packet
|
||||
enum { VOXL2_IO_PACKET_POS_HEADER1 = 0,
|
||||
VOXL2_IO_PACKET_POS_LENGTH,
|
||||
VOXL2_IO_PACKET_POS_TYPE,
|
||||
VOXL2_IO_PACKET_POS_DATA
|
||||
};
|
||||
|
||||
// Definition of the structure that holds the state of the incoming data that is being recived (i.e. incomplete packets)
|
||||
typedef struct {
|
||||
uint8_t len_received; // Number of chars received so far
|
||||
uint8_t len_expected; // Expected number of chars based on header
|
||||
uint8_t *bp; // Pointer to the next write position in the buffer
|
||||
uint16_t crc; // Accumulated CRC value so far
|
||||
uint8_t buffer[64]; // Buffer to hold incoming data that is being parsed
|
||||
} VOXL2_IOPacket;
|
||||
|
||||
|
||||
// Definition of the response packet from ESC, containing the ESC version information
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t length; // Total length of the packet
|
||||
uint8_t type; // This will be equal to ESC_PACKET_TYPE_VERSION_RESPONSE
|
||||
|
||||
uint8_t id; // ID of the ESC that responded
|
||||
uint16_t sw_version; // Software version of the ESC firmware
|
||||
uint16_t hw_version; // HW version of the board
|
||||
|
||||
uint32_t unique_id; // Unique ID of the ESC, if available
|
||||
uint16_t crc;
|
||||
} __attribute__((__packed__)) VOXL2_IO_VERSION_INFO;
|
||||
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t length;
|
||||
uint8_t type;
|
||||
uint8_t id;
|
||||
uint16_t sw_version;
|
||||
uint16_t hw_version;
|
||||
uint8_t unique_id[12];
|
||||
char firmware_git_version[12];
|
||||
char bootloader_git_version[12];
|
||||
uint16_t bootloader_version;
|
||||
uint16_t crc;
|
||||
} __attribute__((__packed__)) VOXL2_IO_EXTENDED_VERSION_INFO;
|
||||
|
||||
// Definition of the feedback response packet from ESC
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t length; // Total length of the packet
|
||||
uint8_t type; // This will be equal to ESC_PACKET_TYPE_FB_RESPONSE
|
||||
|
||||
uint8_t state; // bits 0:3 = state, bits 4:7 = ID
|
||||
uint16_t rpm; // Current RPM of the motor
|
||||
uint8_t cmd_counter; // Number of commands received by the ESC
|
||||
uint8_t reserved0;
|
||||
int8_t voltage; // Voltage = (-28)/34.0 + 9.0 = 8.176V. 0xE4 --> 228 (-28)
|
||||
uint8_t reserved1;
|
||||
|
||||
uint16_t crc;
|
||||
} __attribute__((__packed__)) VOXL2_IO_FB_RESPONSE;
|
||||
|
||||
// Definition of the feedback response packet from ESC
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t length; // Total length of the packet
|
||||
uint8_t type; // This will be equal to ESC_PACKET_TYPE_FB_RESPONSE
|
||||
uint8_t id_state; // bits 0:3 = state, bits 4:7 = ID
|
||||
|
||||
uint16_t rpm; // Current RPM of the motor
|
||||
uint8_t cmd_counter; // Number of commands received by the ESC
|
||||
uint8_t power; // Applied power [0..100]
|
||||
|
||||
uint16_t voltage; // Voltage measured by the ESC in mV
|
||||
int16_t current; // Current measured by the ESC in 8mA resolution
|
||||
int16_t temperature; // Temperature measured by the ESC in 0.01 degC resolution
|
||||
|
||||
uint16_t crc;
|
||||
} __attribute__((__packed__)) VOXL2_IO_FB_RESPONSE_V2;
|
||||
|
||||
|
||||
// Definition of the feedback response packet from ESC, which contains battery voltage and total current
|
||||
typedef struct {
|
||||
uint8_t header;
|
||||
uint8_t length;
|
||||
uint8_t type;
|
||||
uint8_t id; //ESC Id (could be used as system ID elsewhere)
|
||||
uint16_t voltage; //Input voltage (Millivolts)
|
||||
int16_t current; //Total Current (8mA resolution)
|
||||
uint16_t crc;
|
||||
} __attribute__((__packed__)) VOXL2_IO_FB_POWER_STATUS;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//Below are functions for generating packets that would be outgoing to ESCs
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Create a generic packet by providing all required components
|
||||
// Inputs are packet type, input data array and its size, output array and maximum size of output array
|
||||
// Resulting packet will be placed in the output data array together with appropriate header and checksum
|
||||
// Output value represents total length of the created packet (if positive), otherwise error code
|
||||
int32_t voxl2_io_create_packet(uint8_t type, uint8_t *input_data, uint16_t input_size, uint8_t *out_data,
|
||||
uint16_t out_data_size);
|
||||
|
||||
// Create a packet for requesting version information from ESC with desired id
|
||||
// If an ESC with this id is connected and receives this command, it will reply with it's version information
|
||||
int32_t voxl2_io_create_version_request_packet(uint8_t id, uint8_t *out, uint16_t out_size);
|
||||
int32_t voxl2_io_create_extended_version_request_packet(uint8_t id, uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for requesting an ESC with desired id to reset
|
||||
// When ESC with the particular id receives this command, and it's not spinning, ESC will reset
|
||||
// This is useful for entering bootloader without removing power from the system
|
||||
int32_t voxl2_io_create_reset_packet(uint8_t id, uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for generating a tone packet (signals are applied to motor to generate sounds)
|
||||
// Inputs are relative frequency (0-255), relative duration (0-255), power (0-255) and bit mask for which ESCs should play a tone
|
||||
// Bit mask definition: if bit i is set to 1, then ESC with ID=i will generate the tone
|
||||
// Note that tones can only be generated when motor is not spinning
|
||||
int32_t voxl2_io_create_sound_packet(uint8_t frequency, uint8_t duration, uint8_t power, uint8_t mask, uint8_t *out,
|
||||
uint16_t out_size);
|
||||
|
||||
// Create a packet for standalone LED control
|
||||
// Bit mask definition:
|
||||
// led_byte_1 - bit0 = ESC0 Red, bit1 = ESC0, Green, bit2 = ESC0 Blue, bit3 = ESC1 Red, bit4 = ESC1 Green,
|
||||
// bit5 = ESC1 Blue, bit6 = ESC2 Red, bit7 = ESC2 Green
|
||||
// led_byte_2 - bit0 = ESC2 Blue, bit1 = ESC3 Red, bit2 = ESC3 Green, bit3 = ESC3 Blue, bits 4:7 = unused
|
||||
// Note that control can only be sent when motor is not spinning
|
||||
int32_t voxl2_io_create_led_control_packet(uint8_t led_byte_1, uint8_t led_byte_2, uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for setting the ID of an ESC
|
||||
// Return value is the length of generated packet (if positive), otherwise error code
|
||||
// Note that all ESCs that will receive this command will be set to this ID
|
||||
int32_t voxl2_io_create_set_id_packet(uint8_t id, uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for sending open-loop command and LED command to 4 ESCs without requesting any feedback
|
||||
// Return value is the length of generated packet (if positive), otherwise error code
|
||||
int32_t voxl2_io_create_pwm_packet4(int16_t pwm0, int16_t pwm1, int16_t pwm2, int16_t pwm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for sending open-loop command and LED command to 4 ESCs, also request feedback from one ESC (with id=fb_id)
|
||||
// Return value is the length of generated packet (if positive), otherwise error code
|
||||
int32_t voxl2_io_create_pwm_packet4_fb(int16_t pwm0, int16_t pwm1, int16_t pwm2, int16_t pwm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
int32_t fb_id, uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for sending closed-loop RPM command and LED command to 4 ESCs without requesting any feedback
|
||||
// Return value is the length of generated packet (if positive), otherwise error code
|
||||
int32_t voxl2_io_create_rpm_packet4(int16_t rpm0, int16_t rpm1, int16_t rpm2, int16_t rpm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
uint8_t *out, uint16_t out_size);
|
||||
|
||||
// Create a packet for sending closed-loop RPM command and LED command to 4 ESCs, also request feedback from one ESC (with id=fb_id)
|
||||
// Return value is the length of generated packet (if positive), otherwise error code
|
||||
int32_t voxl2_io_create_rpm_packet4_fb(int16_t rpm0, int16_t rpm1, int16_t rpm2, int16_t rpm3,
|
||||
uint8_t led0, uint8_t led1, uint8_t led2, uint8_t led3,
|
||||
int32_t fb_id, uint8_t *out, uint16_t out_size);
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Below are functions for processing incoming packets
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
|
||||
// Feed one char and see if we have accumulated a complete packet
|
||||
int16_t voxl2_io_packet_process_char(uint8_t c, VOXL2_IOPacket *packet);
|
||||
|
||||
// Get a pointer to the packet type from a pointer to VOXL2_IOPacket
|
||||
static inline uint8_t voxl2_io_packet_get_type(VOXL2_IOPacket *packet) { return packet->buffer[VOXL2_IO_PACKET_POS_TYPE]; }
|
||||
|
||||
// Get a pointer to the packet type from a uint8_t pointer that points to the raw packet data as it comes from UART port
|
||||
static inline uint8_t voxl2_io_packet_raw_get_type(uint8_t *packet) { return packet[VOXL2_IO_PACKET_POS_TYPE]; }
|
||||
|
||||
//get a pointer to the packet payload from a pointer to VOXL2_IOPacket
|
||||
static inline uint8_t *voxl2_io_packet_get_data_ptr(VOXL2_IOPacket *packet) { return &(packet->buffer[VOXL2_IO_PACKET_POS_DATA]); }
|
||||
|
||||
// Get a pointer to the packet payload from a uint8_t pointer that points to the raw packet data as it comes from UART port
|
||||
static inline uint8_t *voxl2_io_packet_raw_get_data_ptr(uint8_t *packet) { return &(packet[VOXL2_IO_PACKET_POS_DATA]); }
|
||||
|
||||
// Get the total size (length) in bytes of the packet
|
||||
static inline uint8_t voxl2_io_packet_get_size(VOXL2_IOPacket *packet) { return packet->buffer[VOXL2_IO_PACKET_POS_LENGTH]; }
|
||||
|
||||
// Get checksum of the packet from a pointer to VOXL2_IOPacket
|
||||
static inline uint16_t voxl2_io_packet_checksum_get(VOXL2_IOPacket *packet) { return packet->crc; }
|
||||
|
||||
// Calculate the checksum of a data array. Used for packet generation / processing
|
||||
static inline uint16_t voxl2_io_packet_checksum(uint8_t *buf, uint16_t size)
|
||||
{
|
||||
uint16_t crc = voxl2_io_crc16_init();
|
||||
return voxl2_io_crc16(crc, buf, size);
|
||||
}
|
||||
|
||||
// Reset the checksum of the incoming packet. Used internally for packet reception
|
||||
static inline void voxl2_io_packet_checksum_reset(VOXL2_IOPacket *packet) { packet->crc = voxl2_io_crc16_init(); }
|
||||
|
||||
// Process one character for checksum calculation while receiving a packet (used internally for packet reception)
|
||||
static inline void voxl2_io_packet_checksum_process_char(VOXL2_IOPacket *packet, uint8_t c)
|
||||
{
|
||||
packet->crc = voxl2_io_crc16_byte(packet->crc, c);
|
||||
}
|
||||
|
||||
|
||||
// Initialize an instance of an VOXL2_IOPacket. This should be called once before using an instance of VOXL2_IOPacket
|
||||
static inline void voxl2_io_packet_init(VOXL2_IOPacket *packet)
|
||||
{
|
||||
packet->len_received = 0;
|
||||
packet->len_expected = 0;
|
||||
packet->bp = 0;
|
||||
|
||||
voxl2_io_packet_checksum_reset(packet);
|
||||
}
|
||||
|
||||
// Reset status of the packet that is being parsed. Effectively, this achieves the same thing as _packet_init
|
||||
// so that _packet_init may be redundant
|
||||
static inline void voxl2_io_packet_reset(VOXL2_IOPacket *packet)
|
||||
{
|
||||
packet->len_received = 0;
|
||||
}
|
||||
|
||||
#endif //VOXL2_IO_PACKET
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
/****************************************************************************
|
||||
* Copyright (c) 2017 The Linux Foundation. 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 The Linux Foundation nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
* THIS LICENSE.
|
||||
* 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.
|
||||
* In addition Supplemental Terms apply. See the SUPPLEMENTAL file.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/*
|
||||
* This file contains the type values of all supported VOXL2_IO UART packets
|
||||
*/
|
||||
|
||||
#ifndef VOXL2_IO_PACKET_TYPES
|
||||
#define VOXL2_IO_PACKET_TYPES
|
||||
|
||||
#define VOXL2_IO_PACKET_TYPE_VERSION_REQUEST 0
|
||||
#define VOXL2_IO_PACKET_TYPE_PWM_CMD 1
|
||||
#define VOXL2_IO_PACKET_TYPE_RPM_CMD 2
|
||||
#define VOXL2_IO_PACKET_TYPE_SOUND_CMD 3
|
||||
#define VOXL2_IO_PACKET_TYPE_STEP_CMD 4
|
||||
#define VOXL2_IO_PACKET_TYPE_LED_CMD 5
|
||||
#define VOXL2_IO_PACKET_TYPE_RESET_CMD 10
|
||||
#define VOXL2_IO_PACKET_TYPE_SET_ID_CMD 11
|
||||
#define VOXL2_IO_PACKET_TYPE_SET_DIR_CMD 12
|
||||
#define VOXL2_IO_PACKET_TYPE_CONFIG_BOARD_REQUEST 20
|
||||
#define VOXL2_IO_PACKET_TYPE_CONFIG_USER_REQUEST 21
|
||||
#define VOXL2_IO_PACKET_TYPE_CONFIG_UART_REQUEST 22
|
||||
#define VOXL2_IO_PACKET_TYPE_CONFIG_TUNE_REQUEST 23
|
||||
#define VOXL2_IO_PACKET_TYPE_VERSION_EXT_REQUEST 24
|
||||
|
||||
#define VOXL2_IO_PACKET_TYPE_SET_FEEDBACK_MODE 50 //reserved for future use
|
||||
|
||||
#define VOXL2_IO_PACKET_TYPE_EEPROM_WRITE_UNLOCK 70
|
||||
#define VOXL2_IO_PACKET_TYPE_EEPROM_READ_UNLOCK 71
|
||||
#define VOXL2_IO_PACKET_TYPE_EEPROM_WRITE 72
|
||||
|
||||
#define VOXL2_IO_PACKET_TYPE_VERSION_RESPONSE 109
|
||||
#define VOXL2_IO_PACKET_TYPE_PARAMS 110
|
||||
#define VOXL2_IO_PACKET_TYPE_BOARD_CONFIG 111
|
||||
#define VOXL2_IO_PACKET_TYPE_USER_CONFIG 112
|
||||
#define VOXL2_IO_PACKET_TYPE_UART_CONFIG 113
|
||||
#define VOXL2_IO_PACKET_TYPE_TUNE_CONFIG 114
|
||||
#define VOXL2_IO_PACKET_TYPE_FB_RESPONSE 128
|
||||
#define VOXL2_IO_PACKET_TYPE_VERSION_EXT_RESPONSE 131
|
||||
#define VOXL2_IO_PACKET_TYPE_FB_POWER_STATUS 132
|
||||
#define VOXL2_IO_PACKET_TYPE_RC_DATA_RAW 133
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2020 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* UART M0065 baud rate
|
||||
*
|
||||
* Default rate is 921600, which is used for communicating with M0065.
|
||||
*
|
||||
* @group VOXL2 IO
|
||||
* @unit bit/s
|
||||
*/
|
||||
PARAM_DEFINE_INT32(VOXL2_IO_BAUD, 921600);
|
||||
|
||||
/**
|
||||
* M0065 PWM Min
|
||||
*
|
||||
* Minimum duration (microseconds) for M0065 PWM
|
||||
*
|
||||
* @min 0
|
||||
* @max 2000
|
||||
* @group VOXL2 IO
|
||||
* @unit us
|
||||
*/
|
||||
PARAM_DEFINE_INT32(VOXL2_IO_MIN, 1000);
|
||||
|
||||
/**
|
||||
* M0065 PWM Max
|
||||
*
|
||||
* Maximum duration (microseconds) for M0065 PWM
|
||||
* @min 0
|
||||
* @max 2000
|
||||
* @group VOXL2 IO
|
||||
* @unit us
|
||||
*/
|
||||
PARAM_DEFINE_INT32(VOXL2_IO_MAX, 2000);
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2020 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include "string.h"
|
||||
#include "voxl2_io_serial.hpp"
|
||||
|
||||
Voxl2IoSerial::Voxl2IoSerial()
|
||||
{
|
||||
}
|
||||
|
||||
Voxl2IoSerial::~Voxl2IoSerial()
|
||||
{
|
||||
if (_uart_fd >= 0) {
|
||||
uart_close();
|
||||
}
|
||||
}
|
||||
|
||||
int Voxl2IoSerial::uart_open(const char *dev, speed_t speed)
|
||||
{
|
||||
if (_uart_fd >= 0) {
|
||||
PX4_ERR("Port in use: %s (%i)", dev, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Open UART */
|
||||
#ifdef __PX4_QURT
|
||||
_uart_fd = qurt_uart_open(dev, speed);
|
||||
#else
|
||||
_uart_fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
#endif
|
||||
|
||||
if (_uart_fd < 0) {
|
||||
PX4_ERR("Error opening port: %s (%i)", dev, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef __PX4_QURT
|
||||
/* Back up the original UART configuration to restore it after exit */
|
||||
int termios_state;
|
||||
|
||||
if ((termios_state = tcgetattr(_uart_fd, &_orig_cfg)) < 0) {
|
||||
PX4_ERR("Error configuring port: tcgetattr %s: %d", dev, termios_state);
|
||||
uart_close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Fill the struct for the new configuration */
|
||||
tcgetattr(_uart_fd, &_cfg);
|
||||
|
||||
/* Disable output post-processing */
|
||||
_cfg.c_oflag &= ~OPOST;
|
||||
|
||||
_cfg.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
|
||||
_cfg.c_cflag &= ~CSIZE;
|
||||
_cfg.c_cflag |= CS8; /* 8-bit characters */
|
||||
_cfg.c_cflag &= ~PARENB; /* no parity bit */
|
||||
_cfg.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
|
||||
_cfg.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
|
||||
|
||||
/* setup for non-canonical mode */
|
||||
_cfg.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
||||
_cfg.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||
|
||||
if (cfsetispeed(&_cfg, speed) < 0 || cfsetospeed(&_cfg, speed) < 0) {
|
||||
PX4_ERR("Error configuring port: %s: %d (cfsetispeed, cfsetospeed)", dev, termios_state);
|
||||
uart_close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((termios_state = tcsetattr(_uart_fd, TCSANOW, &_cfg)) < 0) {
|
||||
PX4_ERR("Error configuring port: %s (tcsetattr)", dev);
|
||||
uart_close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
_speed = speed;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Voxl2IoSerial::uart_set_baud(speed_t speed)
|
||||
{
|
||||
#ifndef __PX4_QURT
|
||||
|
||||
if (_uart_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cfsetispeed(&_cfg, speed) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tcsetattr(_uart_fd, TCSANOW, &_cfg) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
_speed = speed;
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Voxl2IoSerial::uart_close()
|
||||
{
|
||||
#ifndef __PX4_QURT
|
||||
|
||||
if (_uart_fd < 0) {
|
||||
PX4_ERR("invalid state for closing");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tcsetattr(_uart_fd, TCSANOW, &_orig_cfg)) {
|
||||
PX4_ERR("failed restoring uart to original state");
|
||||
}
|
||||
|
||||
if (close(_uart_fd)) {
|
||||
PX4_ERR("error closing uart");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
_uart_fd = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Voxl2IoSerial::uart_write(FAR void *buf, size_t len)
|
||||
{
|
||||
if (_uart_fd < 0 || buf == NULL) {
|
||||
PX4_ERR("invalid state for writing or buffer");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef __PX4_QURT
|
||||
return qurt_uart_write(_uart_fd, (const char *) buf, len);
|
||||
#else
|
||||
return write(_uart_fd, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
int Voxl2IoSerial::uart_read(FAR void *buf, size_t len)
|
||||
{
|
||||
if (_uart_fd < 0 || buf == NULL) {
|
||||
PX4_ERR("invalid state for reading or buffer");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef __PX4_QURT
|
||||
#define ASYNC_UART_READ_WAIT_US 2000
|
||||
// The UART read on SLPI is via an asynchronous service so specify a timeout
|
||||
// for the return. The driver will poll periodically until the read comes in
|
||||
// so this may block for a while. However, it will timeout if no read comes in.
|
||||
return qurt_uart_read(_uart_fd, (char *) buf, len, ASYNC_UART_READ_WAIT_US);
|
||||
#else
|
||||
return read(_uart_fd, buf, len);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2020 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <px4_log.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
|
||||
#ifdef __PX4_QURT
|
||||
#include <drivers/device/qurt/uart.h>
|
||||
#define FAR
|
||||
#endif
|
||||
|
||||
class Voxl2IoSerial
|
||||
{
|
||||
public:
|
||||
Voxl2IoSerial();
|
||||
virtual ~Voxl2IoSerial();
|
||||
|
||||
int uart_open(const char *dev, speed_t speed);
|
||||
int uart_set_baud(speed_t speed);
|
||||
int uart_close();
|
||||
int uart_write(FAR void *buf, size_t len);
|
||||
int uart_read(FAR void *buf, size_t len);
|
||||
bool is_open() { return _uart_fd >= 0; };
|
||||
int uart_get_baud() {return _speed; }
|
||||
|
||||
private:
|
||||
int _uart_fd = -1;
|
||||
|
||||
#if ! defined(__PX4_QURT)
|
||||
struct termios _orig_cfg;
|
||||
struct termios _cfg;
|
||||
#endif
|
||||
|
||||
int _speed = -1;
|
||||
};
|
||||
Reference in New Issue
Block a user