 Convert Easing Functions: Actionscript -> Wiring

Hi! - I’m trying to convert some cubic easing functions from Actionscript to Wiring for use in some motor controller code I am writing. The AS code I am using is based on examples/tutorials by Robert Penner (good stuff: Robert Penner's Easing Functions). The function I am testing is ‘easeInOutCubic’.

When I run it in Wiring I expected to get stepped values from the start value to the change value. Instead what I am getting is: a run of 60, then 1305 in the Serial output window. I understand that Serial.print() truncates floats but I expected to see a range from 60 (start) to 2490 (change amount).

I’m guessing I have some problem with data typing - but that’s a guess. Where I am going with this is to use the easing functions to PWM my motor so it doesn’t lurch to full speed but gradually eases in/out.

Anyway - hope this is clear : - Any nudges in the right direction much appreciated!

–Roy

-------------ActionScript

function easeInOutCubic(t, b, c, d) {
if ((t /= d/2)<1) {
return c/2*Math.pow(t, 3)+b;
}
return c/2*(Math.pow(t-2, 3)+2)+b;
}

-----------Wiring

//
int time;
int start;
int end;
int change;
int duration;
float speed;
boolean active;

void setup(){
Serial.begin(115200);

start = 60;
end =2550;
change = end - start;
time = 0;
duration = 50 ;
active = true;
}

void loop(){

if (active == true){
speed = easeInCubic(time++, start, change, duration);
Serial.println(speed, DEC);
if (time ==duration){
active = false;
}
}
}

float easeInOutCubic(int t, int b, int c, int d) {
if ((t /= d/2)<1) {
return c/2* pow(t, 3)+b;
}
return c/2*(pow(t-2, 3)+2)+b;
}

float easeInCubic(int t, int b, int c, int d) {
return c * pow (t/d, 3) + b;
};

float easeOutCubic(int t, int b, int c, int d) {
return c * (pow (t/d-1, 3) + 1) + b;
};

In Wiring/Arduino (i.e. C), when you divide two integers, you get an integer result. In easeInCubic, t and d are both integers with t < d, so you get 0. You need to cast one of them to a float first, something like ((float) t) / d

AH! That helps a lot - I am now seeing a more stepped output - which is what I was expecting. Now I can move on to working on the actual routine.

I don't have much experience with strict data typing or converting between types (casting) so when I run into it it's usually like running face first into a wall.

thanks! :)

--Roy

No problem. This is discussed in the reference for arithmetic: http://www.arduino.cc/en/Reference/Arithmetic but I know that it’s not something you would usually think of as being a problem. This is one of those things that comes along with our underlying language (C/C++) otherwise it’d probably be worth changing the behavior.