Go Down

### Topic: weird calculation issue (Read 992 times)previous topic - next topic

#### yves

##### Jan 28, 2011, 08:46 pm
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 wheelsfloat 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);  }  else{    digitalWrite(DIR1, LOW);     digitalWrite(DIR2, LOW);   }  int i = 0;  while (i < stepAmount){    i++;    digitalWrite(STEP1, LOW);    digitalWrite(STEP2, LOW);    delayMicroseconds(1200);    digitalWrite(STEP1, HIGH);    digitalWrite(STEP2, HIGH);  }}`

can anybody help me with that?

#1
##### Jan 28, 2011, 08:56 pm
These cry out to be constants...

Quote
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;

#### Daanii

#2
##### Jan 28, 2011, 09:03 pm
You are right, your types are not consistent.

Quote

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

Quote
float gearRatio = 11/3;

Also, this should be 11.0/3.0

Quote

#### yves

#3
##### Jan 28, 2011, 09:49 pm
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?

thanks!

#### Daanii

#4
##### Jan 28, 2011, 10:39 pm
Quote
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

Please enter a valid email to subscribe