PX4 Firmware
PX4 Autopilot Software http://px4.io
sensor_bridge.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2014, 2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @author Pavel Kirienko <pavel.kirienko@gmail.com>
36  */
37 
38 #pragma once
39 
40 #include <containers/List.hpp>
41 #include <uavcan/uavcan.hpp>
42 #include <drivers/device/device.h>
43 #include <drivers/drv_orb_dev.h>
44 #include <uORB/uORB.h>
45 
46 /**
47  * A sensor bridge class must implement this interface.
48  */
49 class IUavcanSensorBridge : uavcan::Noncopyable, public ListNode<IUavcanSensorBridge *>
50 {
51 public:
52  static constexpr unsigned MAX_NAME_LEN = 20;
53 
54  virtual ~IUavcanSensorBridge() = default;
55 
56  /**
57  * Returns ASCII name of the bridge.
58  */
59  virtual const char *get_name() const = 0;
60 
61  /**
62  * Starts the bridge.
63  * @return Non-negative value on success, negative on error.
64  */
65  virtual int init() = 0;
66 
67  /**
68  * Returns number of active redundancy channels.
69  */
70  virtual unsigned get_num_redundant_channels() const = 0;
71 
72  /**
73  * Prints current status in a human readable format to stdout.
74  */
75  virtual void print_status() const = 0;
76 
77  /**
78  * Sensor bridge factory.
79  * Creates all known sensor bridges and puts them in the linked list.
80  */
81  static void make_all(uavcan::INode &node, List<IUavcanSensorBridge *> &list);
82 };
83 
84 /**
85  * This is the base class for redundant sensors with an independent ORB topic per each redundancy channel.
86  * For example, sensor_mag0, sensor_mag1, etc.
87  */
89 {
90  struct Channel {
91  int node_id = -1;
92  orb_advert_t orb_advert = nullptr;
93  int class_instance = -1;
94  int orb_instance = -1;
95  };
96 
97  const unsigned _max_channels;
98  const char *const _class_devname;
101  bool _out_of_channels = false;
102 
103 protected:
104  static constexpr unsigned DEFAULT_MAX_CHANNELS = 5;
105 
106  UavcanCDevSensorBridgeBase(const char *name, const char *devname, const char *class_devname,
107  const orb_id_t orb_topic_sensor,
108  const unsigned max_channels = DEFAULT_MAX_CHANNELS) :
109  device::CDev(name, devname),
110  _max_channels(max_channels),
111  _class_devname(class_devname),
112  _orb_topic(orb_topic_sensor),
113  _channels(new Channel[max_channels])
114  {
115  _device_id.devid_s.bus_type = DeviceBusType_UAVCAN;
116  _device_id.devid_s.bus = 0;
117  }
118 
119  /**
120  * Sends one measurement into appropriate ORB topic.
121  * New redundancy channels will be registered automatically.
122  * @param node_id Sensor's Node ID
123  * @param report Pointer to ORB message object
124  */
125  void publish(const int node_id, const void *report);
126 
127 public:
128  virtual ~UavcanCDevSensorBridgeBase();
129 
130  unsigned get_num_redundant_channels() const override;
131 
132  void print_status() const override;
133 };
virtual unsigned get_num_redundant_channels() const =0
Returns number of active redundancy channels.
static void make_all(uavcan::INode &node, List< IUavcanSensorBridge *> &list)
Sensor bridge factory.
uORB published object driver.
API for the uORB lightweight object broker.
UavcanCDevSensorBridgeBase(const char *name, const char *devname, const char *class_devname, const orb_id_t orb_topic_sensor, const unsigned max_channels=DEFAULT_MAX_CHANNELS)
const char *const _class_devname
An intrusive linked list.
static constexpr unsigned MAX_NAME_LEN
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
Abstract class for any character device.
Definition: CDev.hpp:60
This is the base class for redundant sensors with an independent ORB topic per each redundancy channe...
device identifier information
Definition: Device.hpp:240
Definition: List.hpp:59
virtual const char * get_name() const =0
Returns ASCII name of the bridge.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
virtual ~IUavcanSensorBridge()=default
const char * name
Definition: tests_main.c:58
Object metadata.
Definition: uORB.h:50
virtual int init()=0
Starts the bridge.
virtual void print_status() const =0
Prints current status in a human readable format to stdout.
A sensor bridge class must implement this interface.