Lookup table size

Hi,

I want to control the position of a motor using a PWM output from Arduino as an input to a Polulu High Power Motor controller.

The PWM output from Arduino is calculated from the output of a 2D lookup table. The axis of the lookup table are inputs from two 0-5V sensor outputs.

Additionally, the motor position is fedback from a hall effect sensor and a PID function controls the position.

The fedback position from the motor hall-effect sensor has been measured and the range counted using the simple AnalogueReadSerial sketch runs from 136 to 964 counts. So my question is, do I need to convert this range into 0-255 so that Arduino can work with it? Does it also then need to be translated into hex values for storage in the flash memory (I am using the ideas for this found here: http://arduiniana.org/libraries/flash/

As I see it, I need a 0-255 table for the PWM output. Really I want a table of motor positions in degrees. So there will be alot of conversion going on!

  1. Convert 0-5V sensor ranges using lookup array for axis of main lookup table to 0-255 (or if linear output from sensor just use one line of code to convert (i.e. y = mx+c)
  2. Convert 0-5V motor position in degrees through measuring max/min count and convert to 0-255 using lookup array
  3. Convert desired motor position table in degrees into 0-255 for main lookup table PWM output (16x16).

Is there an easier way of doing this? Will I even have enough space on the flash for 3 linearisation curves and a 16x16 PWM lookup tabe?

Thanks!

Don't know if I understand it all correctly, but the map() function is your friend.

from 136 to 964 counts. So my question is, do I need to convert this range into 0-255

val = map(input, 136, 964, 0, 255)

1) You read the 0..5V with analogRead() I assume. analogRead() goes from 0..1023. Dividing that by 4 gives you a linear mapping to 0..255

axis = analogRead()/4;

another option is axis = map(analogRead(), 0,1023, 0, 255);

2) use the map() function here too

Angle = map(analogRead(), 0,1023, minAngle, maxAngle);

3) see above?

Hopes this helpes,

Hi,

Thanks for the reply!

The map function will be very helpful :)

Do you know if I have to store my lookup array (16x16 table) in hex format? Or can I leave it just with 0-255 decimal values? I see in the flash example sketches that I linked to above, the 2D arrays are always written in hex value. I am having trouble compiling with the 16x16 still written in decimal 0-255....

If I do have to convert to hex, is there an easier way than manually? I will make alot of changes during tuning, so each time I will have to re-convert the whole map otherwise...

Thanks

One other question,

Can I use a float as an axis to a flash stored lookup table? Or should I have a line to convert the mapped value to the nearest integer?

Thanks!

Do you know if I have to store my lookup array (16x16 table) in hex format?

No, you can code it in binary, octal or any other base, so long as you qualify the declaration with PROGMEM.

Ah, but I am avoiding using PROGMEM by using the technique described in the link: http://arduiniana.org/libraries/flash/, specifically 'FLASH_TABLE'

It comes with a library file that avoids having to call PROGMEM. I guess I either have to do it using PROGMEM or change the library file. Which would you suggest? Would using PROGMEM directly save some memory?

It comes with a library file that avoids having to call PROGMEM.

You can't "call" PROGMEM; it is a modifier, not a function.

http://www.arduino.cc/playground/Main/PROGMEM

Do you know if I have to store my lookup array (16x16 table) in hex format? Or can I leave it just with 0-255 decimal values? I see in the flash example sketches that I linked to above, the 2D arrays are always written in hex value. I am having trouble compiling with the 16x16 still written in decimal 0-255....

Hex decimal octal binary decimal is just a user representation. The computer only store things in bits and bytes.

i = 0x55; is identical to i=85 or i = B01010101

However some representations make more sense in certain context. E.g. If a number is a size in centimeter decimal notation makes more sense, but a register of a processor might preferably be in hex or binary.

One other question,

Can I use a float as an axis to a flash stored lookup table? Or should I have a line to convert the mapped value to the nearest integer?

Thanks!

No you cannot use a float as array subscript, it will give a compile error. You should map it. you typical round it in 3 ways:

int x = (int)ceill(f); // rounding up int y = (int)floor(f); // rounding down int y = (int)f; // rounding down int z = (int)floor(f+0.5); // rounding to nearest int // you may adjust the 0.5 to your specific needs