PX4 Firmware
PX4 Autopilot Software http://px4.io
spektrum_rssi.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-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  * @file spektrum_rssi.h
36  *
37  * RSSI dBm to percentage conversion for Spektrum telemetry receivers
38  *
39  * @author Kurt Kiefer <kekiefer@gmail.com>
40  */
41 
42 #pragma once
43 
44 #include <climits>
45 
46 /*
47 min_rssi_dbm = -92.0
48 max_rssi_dbm = -42.0
49 
50 def dbm_to_percent(abs_dbm):
51  if -abs_dbm < min_rssi_dbm:
52  return 0.0
53  elif -abs_dbm > max_rssi_dbm:
54  return 100.0
55  else:
56  return 100.0 * math.log10(1.0 + (float)(-abs_dbm - min_rssi_dbm) * (9.0 / (max_rssi_dbm - min_rssi_dbm)))
57 */
58 constexpr int8_t lu_dbm_percent[] = {
59  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
60  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
61  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 99, 98, 98, 97, 96,
62  95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 79,
63  78, 77, 75, 74, 73, 71, 70, 68, 66, 65, 63, 61, 59, 57, 55, 52,
64  50, 47, 45, 42, 39, 35, 32, 28, 24, 19, 13, 7, 0, 0, 0, 0,
65  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67  0,
68 };
69 
70 #define MASK(n) (n >> (sizeof(n) * CHAR_BIT - 1))
71 
72 /* constexpr-compatible version of abs() */
73 constexpr unsigned c_abs(int n)
74 {
75  return (n + MASK(n)) ^ MASK(n);
76 }
77 
78 /* convert signed spektrum dbm (-92 to -42) to percentage */
79 constexpr int8_t spek_dbm_to_percent(int8_t dbm)
80 {
81  return lu_dbm_percent[c_abs(dbm)];
82 }
83 
84 static_assert(spek_dbm_to_percent(0) == 100, "0 dbm should be 100%");
85 static_assert(spek_dbm_to_percent(-42) == 100, "-42 dbm should be 100%");
86 static_assert(spek_dbm_to_percent(-80) == 50, "-80 dbm should be 50%");
87 static_assert(spek_dbm_to_percent(-92) == 0, "-92 dbm should be 0%");
88 static_assert(spek_dbm_to_percent(-128) == 0, "-128 dbm should be 0%");
#define MASK(n)
Definition: spektrum_rssi.h:70
constexpr unsigned c_abs(int n)
Definition: spektrum_rssi.h:73
constexpr int8_t spek_dbm_to_percent(int8_t dbm)
Definition: spektrum_rssi.h:79
constexpr int8_t lu_dbm_percent[]
Definition: spektrum_rssi.h:58