Go to Post Funding and success is no accident. - waialua359 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 15-01-2007, 19:48
JBotAlan's Avatar
JBotAlan JBotAlan is offline
Forever chasing the 'bot around
AKA: Jacob Rau
FRC #5263
Team Role: Mentor
 
Join Date: Sep 2004
Rookie Year: 2004
Location: Riverview, MI
Posts: 723
JBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond reputeJBotAlan has a reputation beyond repute
Send a message via AIM to JBotAlan Send a message via Yahoo to JBotAlan
Re: Floating Points and rounding

Quote:
Originally Posted by chapmatr View Post
If you're dealing with the FRC controller, you can't deal with signed or floats (unless something's changed), just integers. You'll have to multiply your numbers by 10^n, [i]n/I] of the number of decimals you want. Please, correct me if I'm wrong, this is my first official year as a programmer.
You aren't *supposed* to use floats, because the processor doesn't natively do floating point calculations, but the compiler spits out code that works. I need to learn assembler and look at the output, but I don't have time for that right now. Signed variables are fine AFAIK. They work, too.

JBot
__________________
Aren't signatures a bit outdated?
  #2   Spotlight this post!  
Unread 15-01-2007, 21:25
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: Floating Points and rounding

Quote:
Originally Posted by JBotAlan View Post
You aren't *supposed* to use floats, because the processor doesn't natively do floating point calculations, but the compiler spits out code that works.
The problem here is that on a processor that supports floating-point math, doing any floating-point arithmetic takes a single assembly operation. On processors that don't support it (like the RC processor), it has to be done in software. So, each time you do anything with a floating point number, the compiler will end up generating a LOT of code to do it (something like 100 lines of assembly instead of just one). This uses up a lot of extra code space and is obviously a lot slower.

The answer has already been provided here. If you need 1/10 accuracy, just multiply all of your numbers by 10. Also, if you're trying to multiply something by a fraction, but the result only needs to be an integer, keep this trick in mind. If you're multiplying by a fraction, you can multiply by the numerator first and then divide by the denominator afterwards to avoid floating point (just be careful of overflows). So when someone here was talking about multiplying by 180/127, do this instead:

(x * 180) / 127
  #3   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
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding numbers teh_pwnerer795 Programming 13 17-12-2006 12:16
pic: Team 228 and the floating tetra artdutra04 Extra Discussion 8 02-05-2005 19:58
pic: YMTC: 150 points or 100 points? CD47-Bot Extra Discussion 4 25-03-2004 01:53
5 points I make..(and a little excess) archiver 1999 7 23-06-2002 22:18
Awards and points... Ian W. Rules/Strategy 4 24-03-2002 13:30


All times are GMT -5. The time now is 01:26.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi