Go Down

Topic: weird calculation issue (Read 1 time) previous topic - next topic


hi everyone

I'm in the process of building a simple robot that can navigate by dead reckoning.
I've come to a really weird issue and I'm not really sure exactly what's the problem,
though I think it has something to do with data types, in this case specifically floats.
I'm using stepper motors to control wheel movement and try to calculate how much
steps are necessary to travel a certain distance for example. Maybe you can look at the
code... the thing is if I calculate the steps by hand it turns out perfectly, though if
I let it be calculated by my arduino there seems to be some kind of error happening:

Code: [Select]

int DIR1 = 2;
int STEP1 = 3;
int DIR2 = 4;
int STEP2 = 5;

float pi = 3.14159265;
float wheelCircumference = 0.949;
float wholeTurn = 0.422*pi; //distance of the two wheels
float gearRatio = 11/3;

void doTurn(double radius){ //turn around certain radius

  lcdWrite("do turn", "radius");

  float stepAmount;
  stepAmount = (radius/360)*(wholeTurn/wheelCircumference)*1600*gearRatio;

//360° turn would equal: 8196 steps
//judging by the behavior of my robot, something else gets calculated, if I enter
// 8196 directly everything works fine...

  if (stepAmount < 0){
    digitalWrite(DIR1, HIGH);
    digitalWrite(DIR2, HIGH);

    digitalWrite(DIR1, LOW);
    digitalWrite(DIR2, LOW);

  int i = 0;

  while (i < stepAmount){

    digitalWrite(STEP1, LOW);
    digitalWrite(STEP2, LOW);
    digitalWrite(STEP1, HIGH);
    digitalWrite(STEP2, HIGH);


can anybody help me with that?
thanks in advance!

Coding Badly

These cry out to be constants...

const float pi = 3.14159265;
const float wheelCircumference = 0.949;
const float wholeTurn = 0.422*pi; //distance of the two wheels
const float gearRatio = 11/3;

Try this...

const float gearRatio = 11.0 / 3.0;


You are right, your types are not consistent.

stepAmount = (radius/360)*(wholeTurn/wheelCircumference)*1600*gearRatio;

This will do integer arithmetic, I think. Should be 360.0 and 1600.0 instead.

float gearRatio = 11/3;

Also, this should be 11.0/3.0

void doTurn(double radius){ //turn around certain radius

This radius should be float instead of double.


thanks alot you two! I'll try that out asap.
I have another tiny question. as you can see
I print out some debug information to an lcd
display. how would I print out these float values
to the display? is there an easy way to convert
a float value to a char array?



is there an easy way to convert a float value to a char array?

This may work: http://www.arduino.cc/playground/Main/FloatToString

Go Up