I am trying to use the pow() function to create an exponential map. I have looked in various places online and have been able to locate very little info on the function and even less on using it for exponential mapping. Any suggestions and/or links that might be able to help me achieve my goal would be greatly appreciated!
Possibly? I am not very familiar with using multimap(). What I am trying to do is take the input from a ps2 controller analog stick (0-255 and scale it exponentially to 0-2000. I am using the the ps2 controller to control a differential drive robot, but currently just pushing the stick forward a tiny bit sends the not shooting across the room. This is why I need to create the exponential map(or similar) : to make the controls less sensitive.
I don’t think you need an exponential mapping function. A simple quadratic might work well. The function y = 3xx/100 will map the input range of 0-255 to an output range of 0-1950 (close enough to 2000?). The multiplies are done using long integers to avoid overflow problems. This code tests the function:
Thanks for all the suggestions! using a quadratic equation would certainly be worth a try. I’ll work through all the suggestions and see what works best. However, It will probably take me a while as I’m currently swamped with school work.
For all who are interested, here is the code as it stands without any modifications:
#include <SoftPWM.h>
#include <SoftPWM_timer.h>
#include <Servo.h>
#include <PS2X_lib.h> //for v1.6
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
Servo Signal1; //creates servo object
Servo Signal2;
int Input1 = 1500;
int Input2 = 1500;
int Speed1 = 1500;
int Speed2 = 1500;
int setMotorSensitivity()
{
float gamma = 1.5;
for (x=0; x<256; x++)
{
float normx = x / 255.0;
float normy = pow (normx, gamma);
int y = (int) (0.5 + normy * 255);
serial.print("x: ");
serial.print(x, DEC);
serial.print(" y: ");
serial.println(y, DEC);
}
}
void setup()
{
Serial.begin(57600);
Speed1.attach(4); //the pin for the servo control
Speed2.attach(5);
error = ps2x.config_gamepad(13,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
if(error == 0)
Serial.println("Controller found! You may now send commands");
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type)
{
case 0:
Serial.println("Unknown Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
}
}
//My sabortooth motordriver can use the two signals 1000us-2000us to control the two motors
void loop()
{
if(error == 1)
{
return;
}
else
{
ps2x.read_gamepad(false, 0);
Input1 = map(ps2x.Analog(PSS_LY), 0, 255, 2000, 1000);
Speed1 = setMotorSensitivity(Input1);
Signal1.writeMicroseconds(Speed1);
//Serial.print ("Input1: ");
// Serial.println (Input1);
Input2 = map(ps2x.Analog(PSS_LX), 0, 255, 2000, 1000);
Speed2 = setMotorSensitivity(Input2);
Signal2.writeMicroseconds(Speed2);
//Serial.print ("Input2: ");
// Serial.println (Input2);
delay(8);
}
}