Firmware
PWMSim.hpp
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2018 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 #pragma once
35 
36 #include <string.h>
37 
38 #include <drivers/device/device.h>
39 #include <drivers/drv_hrt.h>
40 #include <drivers/drv_mixer.h>
41 #include <drivers/drv_pwm_output.h>
42 #include <lib/mixer/mixer.h>
43 #include <perf/perf_counter.h>
44 #include <px4_common.h>
45 #include <px4_config.h>
46 #include <px4_module.h>
47 #include <px4_tasks.h>
48 #include <px4_time.h>
49 #include <uORB/topics/actuator_armed.h>
50 #include <uORB/topics/actuator_controls.h>
51 #include <uORB/topics/actuator_outputs.h>
52 #include <uORB/topics/parameter_update.h>
53 
54 class PWMSim : public cdev::CDev, public ModuleBase<PWMSim>
55 {
56  static constexpr uint32_t PWM_SIM_DISARMED_MAGIC = 900;
57  static constexpr uint32_t PWM_SIM_FAILSAFE_MAGIC = 600;
58  static constexpr uint32_t PWM_SIM_PWM_MIN_MAGIC = 1000;
59  static constexpr uint32_t PWM_SIM_PWM_MAX_MAGIC = 2000;
60 
61 public:
62 
63  enum Mode {
64  MODE_8PWM,
65  MODE_16PWM,
66  MODE_NONE
67  };
68 
69  PWMSim();
70  virtual ~PWMSim();
71 
73  static int task_spawn(int argc, char *argv[]);
74 
76  static PWMSim *instantiate(int argc, char *argv[]);
77 
79  static int custom_command(int argc, char *argv[]);
80 
82  static int print_usage(const char *reason = nullptr);
83 
85  void run() override;
86 
88  int print_status() override;
89 
90 
91  int ioctl(device::file_t *filp, int cmd, unsigned long arg) override;
92 
93  int set_pwm_rate(unsigned rate);
94 
95  int set_mode(Mode mode);
96  Mode get_mode() { return _mode; }
97 
98 private:
99  static constexpr unsigned MAX_ACTUATORS = 16;
100 
101  Mode _mode{MODE_NONE};
102 
103  int _update_rate{400};
104  int _current_update_rate{0};
105 
106  int _control_subs[actuator_controls_s::NUM_ACTUATOR_CONTROL_GROUPS];
107 
108  px4_pollfd_struct_t _poll_fds[actuator_controls_s::NUM_ACTUATOR_CONTROL_GROUPS] {};
109  unsigned _poll_fds_num{0};
110 
111  int _armed_sub{-1};
112 
113  actuator_outputs_s _actuator_outputs = {};
114  orb_advert_t _outputs_pub{nullptr};
115  orb_advert_t _mixer_status{nullptr};
116 
117  unsigned _num_outputs{0};
118 
119  unsigned _pwm_min[MAX_ACTUATORS] {};
120  unsigned _pwm_max[MAX_ACTUATORS] {};
121 
122  uint32_t _groups_required{0};
123  uint32_t _groups_subscribed{0};
124 
125  bool _armed{false};
126  bool _lockdown{false};
127  bool _failsafe{false};
128 
129  MixerGroup *_mixers{nullptr};
130 
131  actuator_controls_s _controls[actuator_controls_s::NUM_ACTUATOR_CONTROL_GROUPS] {};
132  orb_id_t _control_topics[actuator_controls_s::NUM_ACTUATOR_CONTROL_GROUPS] {};
133 
134  Mixer::Airmode _airmode{Mixer::Airmode::disabled};
135 
136  perf_counter_t _perf_control_latency;
137 
138  static int control_callback(uintptr_t handle, uint8_t control_group, uint8_t control_index, float &input);
139 
140  void subscribe();
141 
142  void update_params();
143 };
144 
int print_status() override
Definition: PWMSim.cpp:683
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.
int ioctl(device::file_t *filp, int cmd, unsigned long arg) override
Perform an ioctl operation on the device.
Definition: PWMSim.cpp:360
Group of mixers, built up from single mixers and processed in order when mixing.
Definition: mixer.h:329
Mixer ioctl interfaces.
static int task_spawn(int argc, char *argv[])
Definition: PWMSim.cpp:585
static int print_usage(const char *reason=nullptr)
Definition: PWMSim.cpp:651
Header common to all counters.
Definition: perf_counter.cpp:65
static PWMSim * instantiate(int argc, char *argv[])
Definition: PWMSim.cpp:608
void run() override
Definition: PWMSim.cpp:157
static int custom_command(int argc, char *argv[])
Definition: PWMSim.cpp:613
Definition: cdev_platform.hpp:20
Object metadata.
Definition: uORB.h:50
Abstract class for any character device.
Definition: CDev.hpp:58
Definition: PWMSim.hpp:54
Performance measuring tools.