Go Down

Topic: Map() function for curve (Read 1 time) previous topic - next topic

robtillaart


Check out my multimap() - http://playground.arduino.cc/Main/MultiMap - code. It is made for this...
Rob Tillaart

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

johnwasser


Thanks. I think I will need to do it with variables though, as my high and low limits can change, depending on other factors (READING is from neutralVal to MaxVal rather than 0 to 1024) .


For a range of NeutralVal to MaxVal I would try something like this:
Code: [Select]

unsigned long NeutralVal, MaxVal;
unsigned long SQmax = (MaxVal-NeutralVal) * (MaxVal-NeutralVal);

unsigned long reading = analogRead(inputPin) - NeutralVal;
reading = reading * reading;
reading = reading / (SQmax / 799);


Of course if your 'reading' ever goes below NeutralVal you will cause an underflow (trying to store a negative number in an unsigned variable).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

SouthernAtHeart

Thanks.  I'll test this out and see how it works.


LROBBINS

Attached are some procedures, from a much larger sketch, that I've used to scale and curve the outputs of a 2-axis Hall-effect sensor joystick whose normal outputs run from 0.05V to 4.95 V (assuming 5V reference source).  These procedures:
(1) reduce output to 0 for voltages < 0.05V and > 4.95 V as that indicates a broken or shorted lead or internal joystick failure.
(2) establishes a deadband as no joystick will perfectly self-center.
(3) maps the voltage to a -1000 to +1000 scale (distinguishing between forward/reverse or left/right).
(4) applies five levels of scaling ranging from linear to squared.
The result to this point is illustrated in the attached graph.

After that, these functions:
(5) re-map the throttle axis to a new when going in reverse, and re-map the steering axis, with different steering sensitivities when going forward or revers.
(6) re-map Throttle and Steering in accord with a Speedpot connected to third analog input pin.

As the results of these calculations are fed into a CANbus network, there are probably some lines of code that refer to sending CAN messages, and I have not included the #define statements or variable declarations that precede all of this, but this does illustrate one way that one can get curving and scaling and so on using entirely integer arithmetic.  It's probably not the most efficient code for this, so any suggestions would, of course, be welcome.
Ciao,
Lenny

SouthernAtHeart

Thanks, Robbins!  That's what I'm doing, too.  Even made some crude out of bounds voltages to detect connection failure, so things don't go haywire on a broken wire! 
I should've post a little detail about what I was doing/needing for the benefit of other readers.
I'll take a look at your code...

Go Up