Add gain and offset to RC inputs

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
shaun2029
Posts: 10
Joined: Tue May 13, 2014 6:55 pm

Add gain and offset to RC inputs

Post by shaun2029 »

I have created a patch to add gain and offset to RC inputs to compensate for incorrect input range.

The project I am working on uses a transmitter that has limited functionality.
The TX range does not allow for a min max of 1000 to 2000.

I do not know how to compensate for this other than using the patch I wrote and tried to attached to this post.

I would like to know if this is useful, and should it be put into the official tree.

Thanks for being kind to a noob developer.

Cheers,
Shaun
Last edited by shaun2029 on Mon Jun 02, 2014 7:40 pm, edited 4 times in total.

shaun2029
Posts: 10
Joined: Tue May 13, 2014 6:55 pm

Patch Add gain and offset to RC inputs

Post by shaun2029 »

commit aaf9fd6c9864c8c9e64100a3abcf7dff7d28f645
Author: Shaun2029
Date: Mon Jun 2 19:07:03 2014 +0100

Add gain and offset to RC inputs to compensate for incorrect input range.

diff --git a/RX.cpp b/RX.cpp
index 1f5dd04..3adb5a1 100644

Code: Select all

--- a/RX.cpp
+++ b/RX.cpp
@@ -6,6 +6,7 @@
 #include "Protocol.h"
 #include "MultiWii.h"
 #include "Alarms.h"
+#include "IMU.h"
 
 /**************************************************************************************/
 /***************             Global RX related variables           ********************/
@@ -404,6 +405,19 @@ uint16_t readRawRC(uint8_t chan) {
     data = rcValue[rcChannel[chan]]; // Let's copy the data Atomically
     SREG = oldSREG;        // Let's restore interrupt state
   #endif
+
+  // Scale RC input
+  #if defined(SCALE_RC)
+    int32_t rcData;
+    int16_t rcScale = (int16_t)SCALE_RC_GAIN_NUM;
+    rcData = mul(data, rcScale); // data can safely be treated as a signed int.
+    data = uint16_t(rcData >> 10); // Divide by 1024.
+    data += SCALE_RC_OFFSET;
+  #endif 
+
+  #if defined(RC_SCALE_CONSTRAIN_MIN) && defined(RC_SCALE_CONSTRAIN_MAX)
+    data = constrain(data,1000,2000);
+  #endif           
   return data; // We return the value correctly copied when the IRQ's where disabled
 }
 
@@ -852,4 +866,4 @@ void spekBind() {
     delay(60000);         //Allow one full minute to bind, then try again.
   }
 }
-#endif
+#endif
diff --git a/config.h b/config.h
index 69ad382..a6b9c08 100644
--- a/config.h
+++ b/config.h
@@ -367,6 +367,48 @@
       //#define RX_SERIAL_PORT 1
       #define SBUS_MID_OFFSET 988 //SBUS Mid-Point at 1500
 
+    /***************                  RC scaling                       ********************/
+      /* Add gain and offset to RC inputs to compensate for incorrect input range.
+           The required input range has a minimum of 1000 and maximum of 2000.
+           Some RC systems are unable to output in this range.
+           To correct for this, GAIN is applied and then RC_OFFSET is added.
+
+           RC GAIN = (2000 - 1000) / (measured max signal - measured minimum signal)
+           RC OFFSET = (2000 - 1000) - (RC GAIN * measured minimum signal)
+
+           Example:
+
+           Min RC Signal = 1050
+           Max RC Signal = 1850
+
+           Gain = (2000 - 1000) / (1850 - 1050) = 1000/800
+           Offset = 1000 - (1000/800 * 1050) = 1000 - 1315 (rounded up) = -312
+           
+           If gain is 1000/800, then SCALE_RC_GAIN_NUM = 1000 (numerator) and SCALE_RC_GAIN_DEN = 800 (denominator)
+           To optimization the calculation speed, the denominator must be scaled to 1024.
+           
+           SCALE_RC_GAIN_NUM = (1024 / denominator) * numerator = (1024 / 800) * 1000 = 1280
+
+           For this example: To scale the RC values, uncomment and set the values as follows.
+
+           #define SCALE_RC
+           #define SCALE_RC_GAIN_NUM 1280
+           #define SCALE_RC_OFFSET -312
+
+           This will result in a range of 1000 to 2000
+
+           Note: The un-scaled RC signal must still be in the range 900 to 2200.
+                 The values for SCALE_RC_OFFSET, SCALE_RC_GAIN_NUM must be integer values.                 
+      */
+
+      //#define SCALE_RC
+      //#define SCALE_RC_GAIN_NUM 1204
+      //#define SCALE_RC_OFFSET -250
+     
+      // Uncomment to limit the scaled RC values
+      //#define RC_SCALE_CONSTRAIN_MIN 1000
+      //#define RC_SCALE_CONSTRAIN_MAX 2000
+
 /*************************************************************************************************/
 /*****************                                                                 ***************/
 /****************  SECTION  4 - ALTERNATE CPUs & BOARDS                                    *******/

waltr
Posts: 733
Joined: Wed Jan 22, 2014 3:21 pm
Location: Near Philadelphia, Pennsyvania, USA

Re: Add gain and offset to RC inputs

Post by waltr »

There is no need for the patch since the Min, max and centers are already #defines. Instead just adjust the following:
In config.h:
/* some radios have not a neutral point centered on 1500. can be changed here */
// Needed to change this for the second Receiver and keep the TX trim tabs centered and without changing the TX's sub-trims -WR
#define MIDRC 1500

in MultiWii.h:
#define MINCHECK 1100
#define MAXCHECK 1900

If the TX's endpoint range is less than desired change the RC Rate in multiwi config.exe to get the response needed.

shaun2029
Posts: 10
Joined: Tue May 13, 2014 6:55 pm

Re: Add gain and offset to RC inputs

Post by shaun2029 »

Hi Waltr,

Thank you for your reply and this information.
It is very helpful.

Post Reply