Nonlinear Mapping
Similar to Arduino’s map function, this value remapping allows a simple adaption of nonlinear input or output devices.
If you have a device like a sensor with a behavior that follows the red line for example draw some joined lines through the red curve and make note of the five node points which are occurring here.
{{ 0,10 } , { 10,50 } , { 20,150 } , { 40,200 } ,{ 50,200 }};
Put these values in the “nodepoints” array where the reMap() function calculates the linear equations (y=mx+b) and according to that the resulting output value.
If you need more nodes simply enlarge the “nodepoints” Array. The node values can be positive or negative .
// curve mapping with n interpolation points
// KHM 2010 Lab3
// nodes for linear equations / nodepoits are concatening lines
float nodepoints[5][2]= {
{
0,10 }
, {
10,50 }
, {
20,150 }
, {
40,200 }
,{
50,200 }
};
void setup() {
Serial.begin(115200);
}
void loop() {
// test
// a given linear input leads to remapped output
for ( int ii = 0; ii <= 50; ii++) {
Serial.print(ii);
Serial.print(" ");
int result= reMap(nodepoints,ii);
Serial.print(result);
Serial.println(" ");
}
while(1);
}
//***************************************************************************
//
int reMap(float pts[10][2], int input) {
int rr;
float bb,mm;
for (int nn=0; nn < 4; nn++) {
if (input >= pts[nn][0] && input <= pts[nn+1][0]) {
mm= ( pts[nn][1] - pts[nn+1][1] ) / ( pts[nn][0] - pts[nn+1][0] );
mm= mm * (input-pts[nn][0]);
mm = mm + pts[nn][1];
rr = mm;
}
}
return(rr);
}
Martin Nawrath / KHM 2010
