PX4 Firmware
PX4 Autopilot Software http://px4.io
SimpleMixer.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 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 <mixer/Mixer/Mixer.hpp>
37 
38 /** simple channel scaler */
40  float negative_scale{1.0f};
41  float positive_scale{1.0f};
42  float offset{0.0f};
43  float min_output{-1.0f};
44  float max_output{1.0f};
45 };
46 
47 /** mixer input */
49  uint8_t control_group; /**< group from which the input reads */
50  uint8_t control_index; /**< index within the control group */
51  mixer_scaler_s scaler; /**< scaling applied to the input before use */
52 };
53 
54 #define MIXER_SIMPLE_SIZE(_icount) (sizeof(struct mixer_simple_s) + (_icount) * sizeof(struct mixer_control_s))
55 
56 /** simple mixer */
58  uint8_t control_count; /**< number of inputs */
59  mixer_scaler_s output_scaler; /**< scaling for the output */
60  mixer_control_s controls[]; /**< actual size of the array is set by control_count */
61 };
62 
63 /**
64  * Simple summing mixer.
65  *
66  * Collects zero or more inputs and mixes them to a single output.
67  */
68 class SimpleMixer : public Mixer
69 {
70 public:
71  /**
72  * Constructor
73  *
74  * @param mixinfo Mixer configuration. The pointer passed
75  * becomes the property of the mixer and
76  * will be freed when the mixer is deleted.
77  */
78  SimpleMixer(ControlCallback control_cb, uintptr_t cb_handle, mixer_simple_s *mixinfo);
79  virtual ~SimpleMixer();
80 
81  // no copy, assignment, move, move assignment
82  SimpleMixer(const SimpleMixer &) = delete;
83  SimpleMixer &operator=(const SimpleMixer &) = delete;
84  SimpleMixer(SimpleMixer &&) = delete;
85  SimpleMixer &operator=(SimpleMixer &&) = delete;
86 
87  /**
88  * Factory method with full external configuration.
89  *
90  * Given a pointer to a buffer containing a text description of the mixer,
91  * returns a pointer to a new instance of the mixer.
92  *
93  * @param control_cb The callback to invoke when fetching a
94  * control value.
95  * @param cb_handle Handle passed to the control callback.
96  * @param buf Buffer containing a text description of
97  * the mixer.
98  * @param buflen Length of the buffer in bytes, adjusted
99  * to reflect the bytes consumed.
100  * @return A new SimpleMixer instance, or nullptr
101  * if the text format is bad.
102  */
103  static SimpleMixer *from_text(Mixer::ControlCallback control_cb, uintptr_t cb_handle, const char *buf,
104  unsigned &buflen);
105 
106  unsigned mix(float *outputs, unsigned space) override;
107 
108  void groups_required(uint32_t &groups) override;
109 
110  /**
111  * Check that the mixer configuration as loaded is sensible.
112  *
113  * Note that this function will call control_cb, but only cares about
114  * error returns, not the input value.
115  *
116  * @return Zero if the mixer makes sense, nonzero otherwise.
117  */
118  int check();
119 
120  unsigned set_trim(float trim) override;
121  unsigned get_trim(float *trim) override;
122 
123 private:
124 
125  /**
126  * Perform simpler linear scaling.
127  *
128  * @param scaler The scaler configuration.
129  * @param input The value to be scaled.
130  * @return The scaled value.
131  */
132  static float scale(const mixer_scaler_s &scaler, float input);
133 
134  /**
135  * Validate a scaler
136  *
137  * @param scaler The scaler to be validated.
138  * @return Zero if good, nonzero otherwise.
139  */
140  static int scale_check(struct mixer_scaler_s &scaler);
141 
142  static int parse_output_scaler(const char *buf, unsigned &buflen, mixer_scaler_s &scaler);
143  static int parse_control_scaler(const char *buf, unsigned &buflen, mixer_scaler_s &scaler, uint8_t &control_group,
144  uint8_t &control_index);
145 
147 
148 };
mixer_scaler_s output_scaler
scaling for the output
Definition: SimpleMixer.hpp:59
simple mixer
Definition: SimpleMixer.hpp:57
Generic, programmable, procedural control signal mixers.
int(* ControlCallback)(uintptr_t handle, uint8_t control_group, uint8_t control_index, float &control)
Fetch a control value.
Definition: Mixer.hpp:154
float positive_scale
Definition: SimpleMixer.hpp:41
mixer_simple_s * _pinfo
simple channel scaler
Definition: SimpleMixer.hpp:39
mixer input
Definition: SimpleMixer.hpp:48
float negative_scale
Definition: SimpleMixer.hpp:40
uint8_t control_count
number of inputs
Definition: SimpleMixer.hpp:58
uint8_t control_index
index within the control group
Definition: SimpleMixer.hpp:50
Simple summing mixer.
Definition: SimpleMixer.hpp:68
uint8_t control_group
group from which the input reads
Definition: SimpleMixer.hpp:49
mixer_scaler_s scaler
scaling applied to the input before use
Definition: SimpleMixer.hpp:51
Abstract class defining a mixer mixing zero or more inputs to one or more outputs.
Definition: Mixer.hpp:136