View Single Post
  #7   Spotlight this post!  
Unread 16-01-2007, 09:12
charrisTTI charrisTTI is offline
Ramblin' Wreck
AKA: Charles Harris
FRC #0623
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Vienna, VA
Posts: 106
charrisTTI has a spectacular aura aboutcharrisTTI has a spectacular aura about
Send a message via AIM to charrisTTI
Re: Floating Points and rounding

The compiler can do some of the floating point calculations you might like to do keep your code readable. Note I said compiler, not the controller. The calculations are done once at compile time, and the resulting long, short, char value is used in the code.

Here is an example using an encoder to control distance moved in autonomous code:

In xxx.h the following macros are defined:

#define COUNTS_PER_REVOLUTION 100.0
#define PI 3.14159
#define WHEEL_DIAMETER 5.25
#define INCHES_TO_COUNTS( inches ) ( inches / ( PI * WHEEL_DIAMETER ) ) * COUNTS_PER_REVOLUTION

enum MoveDirection
{
MoveForward,
MoveRotateCW,
MoveRotateCCW,
MoveReverse
};

void Move( enum MoveDirection directionParam, unsigned char speedParam, unsigned short distanceParam );

In yyy.c is a section of a switch statement used to implement a state machine for autonomous mode


case 3:
// move forward 45 inches
Move( MoveForward, 0x20, INCHES_TO_COUNTS( 45.0 ) );
break;


The macro INCHES_TO_COUNTS allows the compiler to do the floating point math once at compile time to determine how many encoder counts are required for the wheel to move 45 inches.

Now assuming a wheel base of 18 inches how would you code the macro for
DEGREES_TO_COUNTS so that we could write:

case 4:
// turn right 90 degrees
Move( MoveRotateCW, 0x20, DEGREES_TO_COUNTS( 90.0 ) );
break;
__________________
FRC 623 2003,2004,2005,2006,2007,2008, 2009, 2010, 2011
FRC 1900 2007
FVC 60 and 193 2006
FVC 3271 2007
FTC 226 and 369 2008, 2009, 2010, 2011
FTC 3806 2010

Last edited by charrisTTI : 16-01-2007 at 09:16. Reason: expand example