Pages: [1]   Go Down
Author Topic: weird calculation issue  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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);
  }

  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?
thanks in advance!
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 177
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Northern California (by San Francisco)
Offline Offline
Sr. Member
****
Karma: 2
Posts: 251
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are right, your types are not consistent.

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

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
void doTurn(double radius){ //turn around certain radius

This radius should be float instead of double.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Northern California (by San Francisco)
Offline Offline
Sr. Member
****
Karma: 2
Posts: 251
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: