Go Down

Topic: help understanding multiMap function (Read 1 time) previous topic - next topic

Lsnyman

I need assistane understanding the multiMap() function found in the playground.

I also have 2 arrays, one is a series of known analogRead points and the second is a list of corresponding output results that I want with interpolation between.

I do not understand the structure and meaning of the multiMap() declaration statement in line 1. Can someone please break down what each of the 4 items are?
thanks
The post is at http://arduino.cc/playground/Main/MultiMap


Code: [Select]
The code:

int multiMap(int val, int* _in, int* _out, uint8_t size)
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}

Usage
Some snippets shows how multiMap() can be used:


  //My calibrated distance sensor - SHARP 2Y0A02 F 9Y
  // out[] holds the values wanted in cm
  int out[] = {150,140,130,120,110,100, 90, 80, 70, 60, 50, 40, 30, 20};
  // in[] holds the measured analogRead() values for defined distances
  int in[]  = { 90, 97,105,113,124,134,147,164,185,218,255,317,408,506};
  val = analogRead(A0);
  cm = multiMap(val, in, out, 14);




Lsnyman

As an add to above, here is my code but I get a compilation error "undefined reference to multiMap(int, int*, int*, unsigned char)


Code: [Select]
int multiMap(int rawMM, int* _mils, int* _anlogin, uint8_t size);

 

  rawMM = analogRead(0); 
 
  int mils[] = { 1,25,50,75,100,125,150,175,200,225,250,300,350,400,475,550,600}; //output required
 
  int anlogin[] = { 338,381,420,456,492,518,544,571,592,614,640,670,696,719,743,765,779 }; //sampled anlogread
 
 

 
int mm = multiMap(rawMM, anlogin, mils, 17);

PaulS

Quote
I do not understand the structure and meaning of the multiMap() declaration statement in line 1. Can someone please break down what each of the 4 items are?

Code: [Select]
int multiMap(int val, int* _in, int* _out, uint8_t size)
The first argument is the value to map. (rawMM in your case)
The 2nd argument is the array of values to map from. (analogin in your case)
The 3rd argument is the array of values to map to. (mils in your case)
The 4th argument is the number of items in the arrays.

Quote
As an add to above, here is my code but I get a compilation error "undefined reference to multiMap(int, int*, int*, unsigned char)

"here is my code" is wrong. "here is a snippet of my code" would be correct. You need to post ALL of your code.

robtillaart


Code: [Select]
int multiMap(int rawMM, int* _mils, int* _anlogin, uint8_t size);

Your signature seems to have swapped analogRead and the mils, that can cause undesired results..

The call itself is correct:    int mm = multiMap(rawMM, anlogin, mils, 17);

Think of multiMap as Y = f(X);  Y is a function of X;  X is your rawMM;

Hope this helps...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lsnyman

Helps a lot, thank you.
I have never come accross the uint_t size parameter before..

What does this mean? I have searched but am not clear on the meaning.

PaulS

Quote
I have never come accross the uint_t size parameter before..

That name is missing something...

The u at the beginning means that the type is unsigned. The int part means that the value is integer type. The missing part is the size (typically, the name is uint8_t, uint16_t, uint32_t, or uint64_t). The _t on the end means that the variable occupies the specified number of bits regardless of the platform.

An int is different sizes based on whether it is on an Arduino, a PC, a Unix machine, or a mainframe computer. A uint16_t, on the other hand, is 16 bits, regardless of where the code runs.

Lsnyman

Thank you very much. Appreciate your time.

uncle_inside

hello forum from....

I just started to learn arduino less than 4 weeks ago, just wanted to try multimap function, can someone check my code, there have an error: 23: undefined reference to `Multimap (int, int *, int *, unsigned char) '
collect2: error: ld returned 1 exit status

anyone can?

Code: [Select]
int inVal;
int outVal;
int  Val;
int analogInPin = A0;
int analogOutPin = 9;
int sensorValue = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
 
}

void loop() {
  // put your main code here, to run repeatedly:
  int multiMap(int val, int * _in, int * _out, uint8_t size);
  int out[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256  };
  // in[] holds the measured analogRead() values for defined distances
  int in[]  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 };
  uint8_t pos = 1;
  while (Val > in[pos]) pos++;

  Val = analogRead(analogInPin);
  outVal = multiMap(Val, in, out, 256);
 


}

robtillaart

#8
Jan 18, 2015, 04:33 pm Last Edit: Jan 18, 2015, 04:33 pm by robtillaart
you cannot declare functions inside loop()

also the arrays are better declared global as they do not change

Youshould also copy the multimap() function code inside your sketch
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

uncle_inside

ok thank you... i hop its work for me....

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy