Firmware
sensor_bridge.hpp
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 
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 
49 class IUavcanSensorBridge : uavcan::Noncopyable, public ListNode<IUavcanSensorBridge *>
50 {
51 public:
52  static constexpr unsigned MAX_NAME_LEN = 20;
53 
54  virtual ~IUavcanSensorBridge() { }
55 
59  virtual const char *get_name() const = 0;
60 
65  virtual int init() = 0;
66 
70  virtual unsigned get_num_redundant_channels() const = 0;
71 
75  virtual void print_status() const = 0;
76 
81  static void make_all(uavcan::INode &node, List<IUavcanSensorBridge *> &list);
82 };
83 
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;
99  const orb_id_t _orb_topic;
100  Channel *const _channels;
101  bool _out_of_channels = false;
102 
103 protected:
104  static constexpr unsigned DEFAULT_MAX_CHANNELS = 5; // 640 KB ought to be enough for anybody
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 
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 };
An intrusive linked list.
Definition: List.hpp:45
uORB published object driver.
A sensor bridge class must implement this interface.
Definition: sensor_bridge.hpp:49
static void make_all(uavcan::INode &node, List< IUavcanSensorBridge *> &list)
Sensor bridge factory.
Definition: sensor_bridge.cpp:48
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
This is the base class for redundant sensors with an independent ORB topic per each redundancy channe...
Definition: sensor_bridge.hpp:88
virtual int init()=0
Starts the bridge.
Abstract class for any character device.
Definition: CDev.hpp:60
Definition: List.hpp:59
API for the uORB lightweight object broker.
virtual void print_status() const =0
Prints current status in a human readable format to stdout.
virtual const char * get_name() const =0
Returns ASCII name of the bridge.
Object metadata.
Definition: uORB.h:50
virtual unsigned get_num_redundant_channels() const =0
Returns number of active redundancy channels.