Go Down

Topic: From linear to exponential PWM output (Read 45940 times)previous topic - next topic

robtillaart

#120
Nov 17, 2012, 10:33 pm

try this variation of the code

Code: [Select]
`void fillLookupTable(){  speedstartValueold = speedstartValue;   switchpointXValueold = switchpointXValue;   switchpointYValueold = switchpointYValue;   switchpointXValue = constrain (switchpointXValue,deadbandXvalue, 255);  switchpointYValue = constrain(switchpointYValue,speedstartValue, 255);    Serial.print("B,");   Serial.println(speedstartValue);  Serial.print("C,");  Serial.println(switchpointXValue);  Serial.print("D,");  Serial.println(switchpointYValue);  Serial.println(deadbandXvalue);  for (int i = 0; i < deadbandXvalue; i++)       lookupTable[i] = 0;  for (int i = deadbandXvalue; i < switchpointXValue; i++)        lookupTable[i] = map(i, 0, switchpointXValue, speedstartValue, switchpointYValue);  for (int i = switchpointXValue; i < 256; i++)        lookupTable[i] =  map(i, switchpointXValue, 255, switchpointYValue, 255 );           streamUp();}   `

makes the code a bit more straightforward, as it has clearly 3 parts

What is the output of above function? as expected?
Rob Tillaart

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

backbone

#121
Nov 18, 2012, 10:03 amLast Edit: Nov 18, 2012, 04:14 pm by backbone Reason: 1
Hi Rob,

Output is the same.
The lookuptable is filled correctly again.
I checked with Serial.print and the first speed value outside deadbandXValue is 104.

in the Void loop this where the speed values which use the lookuptable are coming from.

Code: [Select]
` //  +++++++++++++++  regular speed - brake mode  +++++++++++++++  sensorRawValue = analogRead(sensorspeedPin); // read the raw value from the linear hall sensor  sensorRawValue = constrain(sensorRawValue, sensorMin, sensorMax); // constrain the maximum value to 255  sensorValue = map(sensorRawValue, sensorMin, sensorMax, 0, 255); // map the calibration to the sensor reading from the hall sensor  sensormappedValue = map(sensorValue, deadbandXvalue, 255, speedstartValue, 255); // full linear control mapped with startspeed    //  ++++  curve section  +++++++  //speedfinalValue = sensormappedValue;  speedfinalValue = lookupTable[sensormappedValue];  //  ++++  curve section  +++++++`

Codewise the speedfinalValue clearly looks into the sensormappedValue or am I mistaken?

Paco
Never to old to learn and I learn every day

backbone

#122
Nov 26, 2012, 06:43 pm
Thanks to Rob the problem is solved.

By removing one line and chaning one variable inside the lookuptable.
here the correct code.

Code: [Select]
`//  +++++++++++++++  regular speed - brake mode  +++++++++++++++  sensorRawValue = analogRead(sensorspeedPin); // read the raw value from the linear hall sensor  sensorRawValue = constrain(sensorRawValue, sensorMin, sensorMax); // constrain the maximum value to 255  sensorValue = map(sensorRawValue, sensorMin, sensorMax, 0, 255); // map the calibration to the sensor reading from the hall sensor     //  ++++  curve section  +++++++  speedfinalValue = lookupTable[sensorValue];  //  ++++  curve section  +++++++`
Never to old to learn and I learn every day

dhenry

#123
Nov 27, 2012, 01:45 am
Quote
here the correct code.

That's very redundant. Your problem really is to map a [0..255] value to [0..255] logarithmically. If you need speed, you calculate the logarithmic transformation offline and store that results in a lookup table.

So something like this would be much faster / simpler:

Code: [Select]
`//  ++++  curve section  +++++++  speedfinalValue = lookupTable[analogReed(sensorspeedPin) >> 2]; //read the sensor and return the highest 8 bits.  //  ++++  curve section  +++++++`

lookupTable[] is also very easy to calculate: for sensor value 0..255, you want the lookupTable to return (255/log(255)) * log(sensor value), other than for sensor value 0, the lookupTable should return 0.

So the table should look like this (using natural base, not 10 base):

Code: [Select]
`0031506374828995101105110114118121124127130133135137140142144146148149151153154156158159160162163164166167168169170172173174175176177178179180180181182183184185186186187188189189190191192192193194194195196196197198198199199200201201202202203203204204205206206207207208208209209210210210211211212212213213214214215215215216216217217217218218219219219220220221221221222222222223223223224224225225225226226226227227227228228228229229229229230230230231231231232232232232233233233234234234234235235235236236236236237237237237238238238238239239239239240240240240241241241241242242242242243243243243244244244244244245245245245246246246246246247247247247247248248248248249249249249249250250250250250251251251251251252252252252252252253253253253253254254254254254255`

backbone

#124
Nov 27, 2012, 10:05 am
Dhenry,

We already use offline lookuptable filling.
The lookuptable is filled with data in a seperate function. "void fillLookupTable"
See the code in the response 120 from Rob.

I will post the full code as attachment now too.
Rob also said he liked to view the whole code.
Problem is it is so large now to place here in code tags.
Threrefor the snippets are less confusing...........? :-)

So I attach the last code I have as zip.
It is dirty as you can get from a low level programmer like me.
Could be faster in the end.........

Thanks, Paco
Never to old to learn and I learn every day

dhenry

#125
Nov 27, 2012, 12:50 pm
Quote
The lookuptable is filled with data in a seperate function.

Unless you have a mcu with lots of ram, that would be the wrong thing to do.

backbone

#126
Nov 27, 2012, 01:42 pm
Dhenry,

Currently use a Mega so enough Ram or am I mistaken.
An UNO or Nano would be on the edge of Free ram.

Paco

Never to old to learn and I learn every day

dhenry

#127
Nov 27, 2012, 02:52 pm
It uses at least 256 bytes of ram, as coded.

It may not be an issue for your platform but in general, you want to store constant data in the rom.

backbone

#128
Nov 27, 2012, 04:44 pm
You mean to write the lookuptable data everytime it changes in the EEProm and read it from there instead of the lookuptable using sRAM?

Paco
Never to old to learn and I learn every day

robtillaart

#129
Nov 27, 2012, 06:33 pm
You can use PROGMEM - to put the table in ROM - see reference section.
Rob Tillaart

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

backbone

#130
Nov 28, 2012, 07:40 am
Rob, I read progmem reference but also memory section.

You mean to BURN the table in the flashrom?
What I understand this will only work if you have only one curve to use.
If that is the case it will not work?
During use of the controller the lookuptable will change by changing startspeed, X and Y pointvalue and if we use an real exponential curve the curveValue.

Paco
Never to old to learn and I learn every day

robtillaart

#131
Nov 28, 2012, 08:21 pm

you are right, if the LUT changes runtime you cannot use PROGMEM.
Rob Tillaart

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

Go Up