17 changed files with 910 additions and 134 deletions
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
#!nsh |
||||
# |
||||
# UAVCAN initialization script. |
||||
# |
||||
|
||||
if param compare UAVCAN_ENABLE 1 |
||||
then |
||||
if uavcan start |
||||
then |
||||
# First sensor publisher to initialize takes lowest instance ID |
||||
# This delay ensures that UAVCAN-interfaced sensors would be allocated on lowest instance IDs |
||||
sleep 1 |
||||
echo "[init] UAVCAN started" |
||||
else |
||||
echo "[init] ERROR: Could not start UAVCAN" |
||||
tone_alarm $TUNE_OUT_ERROR |
||||
fi |
||||
fi |
@ -0,0 +1,117 @@
@@ -0,0 +1,117 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#include "baro.hpp" |
||||
#include <cmath> |
||||
|
||||
static const orb_id_t BARO_TOPICS[2] = { |
||||
ORB_ID(sensor_baro0), |
||||
ORB_ID(sensor_baro1) |
||||
}; |
||||
|
||||
const char *const UavcanBarometerBridge::NAME = "baro"; |
||||
|
||||
UavcanBarometerBridge::UavcanBarometerBridge(uavcan::INode& node) : |
||||
UavcanCDevSensorBridgeBase("uavcan_baro", "/dev/uavcan/baro", BARO_DEVICE_PATH, BARO_TOPICS), |
||||
_sub_air_data(node) |
||||
{ |
||||
} |
||||
|
||||
int UavcanBarometerBridge::init() |
||||
{ |
||||
int res = device::CDev::init(); |
||||
if (res < 0) { |
||||
return res; |
||||
} |
||||
|
||||
res = _sub_air_data.start(AirDataCbBinder(this, &UavcanBarometerBridge::air_data_sub_cb)); |
||||
if (res < 0) { |
||||
log("failed to start uavcan sub: %d", res); |
||||
return res; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
int UavcanBarometerBridge::ioctl(struct file *filp, int cmd, unsigned long arg) |
||||
{ |
||||
switch (cmd) { |
||||
case BAROIOCSMSLPRESSURE: { |
||||
if ((arg < 80000) || (arg > 120000)) { |
||||
return -EINVAL; |
||||
} else { |
||||
log("new msl pressure %u", _msl_pressure); |
||||
_msl_pressure = arg; |
||||
return OK; |
||||
} |
||||
} |
||||
case BAROIOCGMSLPRESSURE: { |
||||
return _msl_pressure; |
||||
} |
||||
default: { |
||||
return CDev::ioctl(filp, cmd, arg); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void UavcanBarometerBridge::air_data_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::air_data::StaticAirData> &msg) |
||||
{ |
||||
auto report = ::baro_report(); |
||||
|
||||
report.timestamp = msg.getUtcTimestamp().toUSec(); |
||||
if (report.timestamp == 0) { |
||||
report.timestamp = msg.getMonotonicTimestamp().toUSec(); |
||||
} |
||||
|
||||
report.temperature = msg.static_temperature; |
||||
report.pressure = msg.static_pressure / 100.0F; // Convert to millibar
|
||||
|
||||
/*
|
||||
* Altitude computation |
||||
* Refer to the MS5611 driver for details |
||||
*/ |
||||
const double T1 = 15.0 + 273.15; // temperature at base height in Kelvin
|
||||
const double a = -6.5 / 1000; // temperature gradient in degrees per metre
|
||||
const double g = 9.80665; // gravity constant in m/s/s
|
||||
const double R = 287.05; // ideal gas constant in J/kg/K
|
||||
|
||||
const double p1 = _msl_pressure / 1000.0; // current pressure at MSL in kPa
|
||||
const double p = double(msg.static_pressure) / 1000.0; // measured pressure in kPa
|
||||
|
||||
report.altitude = (((std::pow((p / p1), (-(a * R) / g))) * T1) - T1) / a; |
||||
|
||||
publish(msg.getSrcNodeID().get(), &report); |
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include "sensor_bridge.hpp" |
||||
#include <drivers/drv_baro.h> |
||||
|
||||
#include <uavcan/equipment/air_data/StaticAirData.hpp> |
||||
|
||||
class UavcanBarometerBridge : public UavcanCDevSensorBridgeBase |
||||
{ |
||||
public: |
||||
static const char *const NAME; |
||||
|
||||
UavcanBarometerBridge(uavcan::INode& node); |
||||
|
||||
const char *get_name() const override { return NAME; } |
||||
|
||||
int init() override; |
||||
|
||||
private: |
||||
int ioctl(struct file *filp, int cmd, unsigned long arg) override; |
||||
|
||||
void air_data_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::air_data::StaticAirData> &msg); |
||||
|
||||
typedef uavcan::MethodBinder<UavcanBarometerBridge*, |
||||
void (UavcanBarometerBridge::*) |
||||
(const uavcan::ReceivedDataStructure<uavcan::equipment::air_data::StaticAirData>&)> |
||||
AirDataCbBinder; |
||||
|
||||
uavcan::Subscriber<uavcan::equipment::air_data::StaticAirData, AirDataCbBinder> _sub_air_data; |
||||
unsigned _msl_pressure = 101325; |
||||
}; |
@ -0,0 +1,123 @@
@@ -0,0 +1,123 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#include "mag.hpp" |
||||
|
||||
static const orb_id_t MAG_TOPICS[3] = { |
||||
ORB_ID(sensor_mag0), |
||||
ORB_ID(sensor_mag1), |
||||
ORB_ID(sensor_mag2) |
||||
}; |
||||
|
||||
const char *const UavcanMagnetometerBridge::NAME = "mag"; |
||||
|
||||
UavcanMagnetometerBridge::UavcanMagnetometerBridge(uavcan::INode& node) : |
||||
UavcanCDevSensorBridgeBase("uavcan_mag", "/dev/uavcan/mag", MAG_DEVICE_PATH, MAG_TOPICS), |
||||
_sub_mag(node) |
||||
{ |
||||
_scale.x_scale = 1.0F; |
||||
_scale.y_scale = 1.0F; |
||||
_scale.z_scale = 1.0F; |
||||
} |
||||
|
||||
int UavcanMagnetometerBridge::init() |
||||
{ |
||||
int res = device::CDev::init(); |
||||
if (res < 0) { |
||||
return res; |
||||
} |
||||
|
||||
res = _sub_mag.start(MagCbBinder(this, &UavcanMagnetometerBridge::mag_sub_cb)); |
||||
if (res < 0) { |
||||
log("failed to start uavcan sub: %d", res); |
||||
return res; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
int UavcanMagnetometerBridge::ioctl(struct file *filp, int cmd, unsigned long arg) |
||||
{ |
||||
switch (cmd) { |
||||
case MAGIOCSSCALE: { |
||||
std::memcpy(&_scale, reinterpret_cast<const void*>(arg), sizeof(_scale)); |
||||
return 0; |
||||
} |
||||
case MAGIOCGSCALE: { |
||||
std::memcpy(reinterpret_cast<void*>(arg), &_scale, sizeof(_scale)); |
||||
return 0; |
||||
} |
||||
case MAGIOCSELFTEST: { |
||||
return 0; // Nothing to do
|
||||
} |
||||
case MAGIOCGEXTERNAL: { |
||||
return 0; // We don't want anyone to transform the coordinate frame, so we declare it onboard
|
||||
} |
||||
case MAGIOCSSAMPLERATE: { |
||||
return 0; // Pretend that this stuff is supported to keep the sensor app happy
|
||||
} |
||||
case MAGIOCCALIBRATE: |
||||
case MAGIOCGSAMPLERATE: |
||||
case MAGIOCSRANGE: |
||||
case MAGIOCGRANGE: |
||||
case MAGIOCSLOWPASS: |
||||
case MAGIOCEXSTRAP: |
||||
case MAGIOCGLOWPASS: { |
||||
return -EINVAL; |
||||
} |
||||
default: { |
||||
return CDev::ioctl(filp, cmd, arg); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void UavcanMagnetometerBridge::mag_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::ahrs::Magnetometer> &msg) |
||||
{ |
||||
auto report = ::mag_report(); |
||||
|
||||
report.range_ga = 1.3F; // Arbitrary number, doesn't really mean anything
|
||||
|
||||
report.timestamp = msg.getUtcTimestamp().toUSec(); |
||||
if (report.timestamp == 0) { |
||||
report.timestamp = msg.getMonotonicTimestamp().toUSec(); |
||||
} |
||||
|
||||
report.x = (msg.magnetic_field[0] - _scale.x_offset) * _scale.x_scale; |
||||
report.y = (msg.magnetic_field[1] - _scale.y_offset) * _scale.y_scale; |
||||
report.z = (msg.magnetic_field[2] - _scale.z_offset) * _scale.z_scale; |
||||
|
||||
publish(msg.getSrcNodeID().get(), &report); |
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include "sensor_bridge.hpp" |
||||
#include <drivers/drv_mag.h> |
||||
|
||||
#include <uavcan/equipment/ahrs/Magnetometer.hpp> |
||||
|
||||
class UavcanMagnetometerBridge : public UavcanCDevSensorBridgeBase |
||||
{ |
||||
public: |
||||
static const char *const NAME; |
||||
|
||||
UavcanMagnetometerBridge(uavcan::INode& node); |
||||
|
||||
const char *get_name() const override { return NAME; } |
||||
|
||||
int init() override; |
||||
|
||||
private: |
||||
int ioctl(struct file *filp, int cmd, unsigned long arg) override; |
||||
|
||||
void mag_sub_cb(const uavcan::ReceivedDataStructure<uavcan::equipment::ahrs::Magnetometer> &msg); |
||||
|
||||
typedef uavcan::MethodBinder<UavcanMagnetometerBridge*, |
||||
void (UavcanMagnetometerBridge::*) |
||||
(const uavcan::ReceivedDataStructure<uavcan::equipment::ahrs::Magnetometer>&)> |
||||
MagCbBinder; |
||||
|
||||
uavcan::Subscriber<uavcan::equipment::ahrs::Magnetometer, MagCbBinder> _sub_mag; |
||||
mag_scale _scale = {}; |
||||
}; |
@ -0,0 +1,142 @@
@@ -0,0 +1,142 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#include "sensor_bridge.hpp" |
||||
#include <cassert> |
||||
|
||||
#include "gnss.hpp" |
||||
#include "mag.hpp" |
||||
#include "baro.hpp" |
||||
|
||||
/*
|
||||
* IUavcanSensorBridge |
||||
*/ |
||||
void IUavcanSensorBridge::make_all(uavcan::INode &node, List<IUavcanSensorBridge*> &list) |
||||
{ |
||||
list.add(new UavcanBarometerBridge(node)); |
||||
list.add(new UavcanMagnetometerBridge(node)); |
||||
list.add(new UavcanGnssBridge(node)); |
||||
} |
||||
|
||||
/*
|
||||
* UavcanCDevSensorBridgeBase |
||||
*/ |
||||
UavcanCDevSensorBridgeBase::~UavcanCDevSensorBridgeBase() |
||||
{ |
||||
for (unsigned i = 0; i < _max_channels; i++) { |
||||
if (_channels[i].redunancy_channel_id >= 0) { |
||||
(void)unregister_class_devname(_class_devname, _channels[i].class_instance); |
||||
} |
||||
} |
||||
delete [] _orb_topics; |
||||
delete [] _channels; |
||||
} |
||||
|
||||
void UavcanCDevSensorBridgeBase::publish(const int redundancy_channel_id, const void *report) |
||||
{ |
||||
Channel *channel = nullptr; |
||||
|
||||
// Checking if such channel already exists
|
||||
for (unsigned i = 0; i < _max_channels; i++) { |
||||
if (_channels[i].redunancy_channel_id == redundancy_channel_id) { |
||||
channel = _channels + i; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
// No such channel - try to create one
|
||||
if (channel == nullptr) { |
||||
if (_out_of_channels) { |
||||
return; // Give up immediately - saves some CPU time
|
||||
} |
||||
|
||||
log("adding channel %d...", redundancy_channel_id); |
||||
|
||||
// Search for the first free channel
|
||||
for (unsigned i = 0; i < _max_channels; i++) { |
||||
if (_channels[i].redunancy_channel_id < 0) { |
||||
channel = _channels + i; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
// No free channels left
|
||||
if (channel == nullptr) { |
||||
_out_of_channels = true; |
||||
log("out of channels"); |
||||
return; |
||||
} |
||||
|
||||
// Ask the CDev helper which class instance we can take
|
||||
const int class_instance = register_class_devname(_class_devname); |
||||
if (class_instance < 0 || class_instance >= int(_max_channels)) { |
||||
_out_of_channels = true; |
||||
log("out of class instances"); |
||||
(void)unregister_class_devname(_class_devname, class_instance); |
||||
return; |
||||
} |
||||
|
||||
// Publish to the appropriate topic, abort on failure
|
||||
channel->orb_id = _orb_topics[class_instance]; |
||||
channel->redunancy_channel_id = redundancy_channel_id; |
||||
channel->class_instance = class_instance; |
||||
|
||||
channel->orb_advert = orb_advertise(channel->orb_id, report); |
||||
if (channel->orb_advert < 0) { |
||||
log("ADVERTISE FAILED"); |
||||
(void)unregister_class_devname(_class_devname, class_instance); |
||||
*channel = Channel(); |
||||
return; |
||||
} |
||||
|
||||
log("channel %d class instance %d ok", channel->redunancy_channel_id, channel->class_instance); |
||||
} |
||||
assert(channel != nullptr); |
||||
|
||||
(void)orb_publish(channel->orb_id, channel->orb_advert, report); |
||||
} |
||||
|
||||
unsigned UavcanCDevSensorBridgeBase::get_num_redundant_channels() const |
||||
{ |
||||
unsigned out = 0; |
||||
for (unsigned i = 0; i < _max_channels; i++) { |
||||
if (_channels[i].redunancy_channel_id >= 0) { |
||||
out += 1; |
||||
} |
||||
} |
||||
return out; |
||||
} |
@ -0,0 +1,124 @@
@@ -0,0 +1,124 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include <containers/List.hpp> |
||||
#include <uavcan/uavcan.hpp> |
||||
#include <drivers/device/device.h> |
||||
#include <drivers/drv_orb_dev.h> |
||||
|
||||
/**
|
||||
* A sensor bridge class must implement this interface. |
||||
*/ |
||||
class IUavcanSensorBridge : uavcan::Noncopyable, public ListNode<IUavcanSensorBridge*> |
||||
{ |
||||
public: |
||||
static constexpr unsigned MAX_NAME_LEN = 20; |
||||
|
||||
virtual ~IUavcanSensorBridge() { } |
||||
|
||||
/**
|
||||
* Returns ASCII name of the bridge. |
||||
*/ |
||||
virtual const char *get_name() const = 0; |
||||
|
||||
/**
|
||||
* Starts the bridge. |
||||
* @return Non-negative value on success, negative on error. |
||||
*/ |
||||
virtual int init() = 0; |
||||
|
||||
/**
|
||||
* Returns number of active redundancy channels. |
||||
*/ |
||||
virtual unsigned get_num_redundant_channels() const = 0; |
||||
|
||||
/**
|
||||
* Sensor bridge factory. |
||||
* Creates a bridge object by its ASCII name, e.g. "gnss", "mag". |
||||
* @return nullptr if such bridge can't be created. |
||||
*/ |
||||
static void make_all(uavcan::INode &node, List<IUavcanSensorBridge*> &list); |
||||
}; |
||||
|
||||
/**
|
||||
* This is the base class for redundant sensors with an independent ORB topic per each redundancy channel. |
||||
* For example, sensor_mag0, sensor_mag1, etc. |
||||
*/ |
||||
class UavcanCDevSensorBridgeBase : public IUavcanSensorBridge, public device::CDev |
||||
{ |
||||
struct Channel |
||||
{ |
||||
int redunancy_channel_id = -1; |
||||
orb_id_t orb_id = nullptr; |
||||
orb_advert_t orb_advert = -1; |
||||
int class_instance = -1; |
||||
}; |
||||
|
||||
const unsigned _max_channels; |
||||
const char *const _class_devname; |
||||
orb_id_t *const _orb_topics; |
||||
Channel *const _channels; |
||||
bool _out_of_channels = false; |
||||
|
||||
protected: |
||||
template <unsigned MaxChannels> |
||||
UavcanCDevSensorBridgeBase(const char *name, const char *devname, const char *class_devname, |
||||
const orb_id_t (&orb_topics)[MaxChannels]) : |
||||
device::CDev(name, devname), |
||||
_max_channels(MaxChannels), |
||||
_class_devname(class_devname), |
||||
_orb_topics(new orb_id_t[MaxChannels]), |
||||
_channels(new Channel[MaxChannels]) |
||||
{ |
||||
memcpy(_orb_topics, orb_topics, sizeof(orb_id_t) * MaxChannels); |
||||
} |
||||
|
||||
/**
|
||||
* Sends one measurement into appropriate ORB topic. |
||||
* New redundancy channels will be registered automatically. |
||||
* @param redundancy_channel_id Redundant sensor identifier (e.g. UAVCAN Node ID) |
||||
* @param report ORB message object |
||||
*/ |
||||
void publish(const int redundancy_channel_id, const void *report); |
||||
|
||||
public: |
||||
virtual ~UavcanCDevSensorBridgeBase(); |
||||
|
||||
unsigned get_num_redundant_channels() const override; |
||||
}; |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/****************************************************************************
|
||||
* |
||||
* Copyright (C) 2014 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. |
||||
* |
||||
****************************************************************************/ |
||||
|
||||
/**
|
||||
* @author Pavel Kirienko <pavel.kirienko@gmail.com> |
||||
*/ |
||||
|
||||
#include <nuttx/config.h> |
||||
#include <systemlib/param/param.h> |
||||
|
||||
/**
|
||||
* Enable UAVCAN. |
||||
* |
||||
* Enables support for UAVCAN-interfaced actuators and sensors. |
||||
* |
||||
* @min 0 |
||||
* @max 1 |
||||
* @group UAVCAN |
||||
*/ |
||||
PARAM_DEFINE_INT32(UAVCAN_ENABLE, 0); |
||||
|
||||
/**
|
||||
* UAVCAN Node ID. |
||||
* |
||||
* Read the specs at http://uavcan.org to learn more about Node ID.
|
||||
* |
||||
* @min 1 |
||||
* @max 125 |
||||
* @group UAVCAN |
||||
*/ |
||||
PARAM_DEFINE_INT32(UAVCAN_NODE_ID, 1); |
||||
|
||||
/**
|
||||
* UAVCAN CAN bus bitrate. |
||||
* |
||||
* @min 20000 |
||||
* @max 1000000 |
||||
* @group UAVCAN |
||||
*/ |
||||
PARAM_DEFINE_INT32(UAVCAN_BITRATE, 1000000); |
||||
|
||||
|
||||
|
Loading…
Reference in new issue