Firmware
airspeedsim.h
1 /****************************************************************************
2  *
3  * Copyright (c) 2013, 2014 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 
41 #include <px4_config.h>
42 #include <px4_defines.h>
43 
44 #include <sys/types.h>
45 #include <stdint.h>
46 #include <stdlib.h>
47 #include <stdbool.h>
48 #include <semaphore.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <poll.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <math.h>
55 #include <unistd.h>
56 
57 #include <px4_workqueue.h>
58 #include <arch/board/board.h>
59 
60 #include <systemlib/err.h>
61 #include <parameters/param.h>
62 #include <perf/perf_counter.h>
63 
64 #include <drivers/drv_airspeed.h>
65 #include <drivers/drv_hrt.h>
67 #include <lib/cdev/CDev.hpp>
68 
69 #include <uORB/uORB.h>
70 #include <uORB/topics/differential_pressure.h>
71 
72 /* Default I2C bus */
73 #define PX4_I2C_BUS_DEFAULT PX4_I2C_BUS_EXPANSION
74 
75 #ifndef CONFIG_SCHED_WORKQUEUE
76 # error This requires CONFIG_SCHED_WORKQUEUE.
77 #endif
78 
80 {
81 public:
82  AirspeedSim(int bus, int address, unsigned conversion_interval, const char *path);
83  virtual ~AirspeedSim();
84 
85  virtual int init();
86 
87  virtual ssize_t read(cdev::file_t *filp, char *buffer, size_t buflen);
88  virtual int ioctl(cdev::file_t *filp, int cmd, unsigned long arg);
89 
93  virtual void print_info();
94 
95 private:
96  ringbuffer::RingBuffer *_reports;
97 
98  unsigned _retries; // XXX this should come from the SIM class
99 
100  /* this class has pointer data members and should not be copied */
101  AirspeedSim(const AirspeedSim &);
102  AirspeedSim &operator=(const AirspeedSim &);
103 
104 protected:
105  virtual int probe();
106 
111  virtual void cycle() = 0;
112  virtual int measure() = 0;
113  virtual int collect() = 0;
114 
115  virtual int transfer(const uint8_t *send, unsigned send_len,
116  uint8_t *recv, unsigned recv_len);
117 
118  struct work_s _work {};
119  bool _sensor_ok;
120  int _measure_ticks;
121  bool _collect_phase;
122  float _diff_pres_offset;
123 
124  orb_advert_t _airspeed_pub;
125 
126  int _class_instance;
127 
128  int _conversion_interval;
129 
130  perf_counter_t _sample_perf;
131  perf_counter_t _comms_errors;
132 
133 
141  int probe_address(uint8_t address);
142 
149  void start();
150 
154  void stop();
155 
162  static void cycle_trampoline(void *arg);
163 
169  void new_report(const differential_pressure_s &report);
170 };
171 
172 
A flexible ringbuffer class.
Definition: airspeedsim.h:79
virtual ssize_t read(file_t *filep, char *buffer, size_t buflen)
Perform a read from the device.
Definition: CDev.hpp:105
Configuration flags used in code.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
High-resolution timer with callouts and timekeeping.
Generally used magic defines.
Global flash based parameter store.
Header common to all counters.
Definition: perf_counter.cpp:65
Definition: I2C.hpp:51
API for the uORB lightweight object broker.
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Definition: cdev_platform.hpp:20
Abstract class for any character device.
Definition: CDev.hpp:58
Definition: video_device.h:50
Definition: airspeedsim.h:118
virtual int ioctl(file_t *filep, int cmd, unsigned long arg)
Perform an ioctl operation on the device.
Definition: CDev.cpp:192
Airspeed driver interface.
Performance measuring tools.