Go to Post The more women we have as 'rock stars' in FIRST, the more girls we'll see celebrating math and science and the doors they can open. - JaneYoung [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:39
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Floating Points and rounding

Hey guys,

for the camera this year, i'd like to set the PAN and TILT values into degrees. Now im sure there is code already formulated somewhere within kevin code. but i'd rather wrap my mind around how to properly do it.

This is my code so far.

//(1.42 degree) per (1pwm)
PAN_DEGREE = (180 / 127);
True_PAN_Degree = PAN_DEGREE * PAN_SERVO;

Now. my question to u guys. i want to use floating points instead of intergers. i would also like to noe how to round the floating point number to 4 decimals. However. i do know that floating points do take alot of space and processing power! but! the maximum value being produced is 180. ( 180 degrees). If you could leave some input that would be great
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #2   Spotlight this post!  
Unread 15-01-2007, 19:44
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

I am personally using floating points in my code. It hasn't posed a problem, except that you can expect problems with printf related to floating points. Just multiply it by however many decimal places * 10 and printf that typecast as an integer.

Now, I'll warn you now, Al S. (I don't remember all of the last name) is going to give you some heat for using floating points on our non-floating point-native controller... but I see no problems with it...yet. It is bad practice, and I agree with him wholeheartedly, except that I'm too lazy to figure out how to represent what I need as a long/integer. Al, if you have any suggestions, I'd be glad to hear them.

JBot
__________________
Aren't signatures a bit outdated?
  #3   Spotlight this post!  
Unread 15-01-2007, 19:45
chapmatr's Avatar
chapmatr chapmatr is offline
Zippidy bling-zap whippy-wog snizin
AKA: Travis (Lead Programmer)
FRC #1676 (Pascack Pioneers)
Team Role: Driver
 
Join Date: Dec 2005
Rookie Year: 2005
Location: Hillsdale NJ
Posts: 19
chapmatr is an unknown quantity at this point
Send a message via AIM to chapmatr
Re: Floating Points and rounding

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, n of the number of decimals you want. Please, correct me if I'm wrong, this is my first official year as a programmer.
__________________
Did you know that 87% of all online statiscs are made up?

Last edited by chapmatr : 15-01-2007 at 19:50.
  #4   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?
  #5   Spotlight this post!  
Unread 15-01-2007, 19:49
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Floating Points and rounding

Quote:
Originally Posted by JBotAlan View Post
I am personally using floating points in my code. It hasn't posed a problem, except that you can expect problems with printf related to floating points. Just multiply it by however many decimal places * 10 and printf that typecast as an integer.

Now, I'll warn you now, Al S. (I don't remember all of the last name) is going to give you some heat for using floating points on our non-floating point-native controller... but I see no problems with it...yet. It is bad practice, and I agree with him wholeheartedly, except that I'm too lazy to figure out how to represent what I need as a long/integer. Al, if you have any suggestions, I'd be glad to hear them.

JBot
kool. how do i set them.

double = PAN_DEGREE;?
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #6   Spotlight this post!  
Unread 15-01-2007, 19:50
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: Floating Points and rounding

you can most certainly deal with floating point numbers! just be careful about typecasting, because PIC C defaults to integers.... so you have to typecast everything.

multiplying by 180/127 is not a good idea... first of all because the range on the servo is not 180... its either 160 or 210 (i forgot). multiply by the decimal... you wil run into less problems.

otherwise...

float PAN_DEGREE = (160.0f / 127.0f);
TRUE_PAN_DEGREE = (float)PAN_SERVO * PAN_DEGREE;

notice the forced decimals and the f.
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY

Last edited by Uberbots : 15-01-2007 at 20:07. Reason: wrong ratio
  #7   Spotlight this post!  
Unread 15-01-2007, 19:54
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 Uberbots View Post
float PAN_DEGREE = (160.0f / 180.0f);
Hmm, I use (for example):
Code:
double PAN_DEGREE = whatever
I didn't even know "float" was a data type! C is obviously not my native language; VB6 is. Which has more resolution? Forget it, I'll Google it.

I didn't know about the "f" after the numbers after, but I'd seen that at some point...can't remember where.

Thanks, I'll use some of that in my code!

JBot
__________________
Aren't signatures a bit outdated?
  #8   Spotlight this post!  
Unread 15-01-2007, 20:08
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: Floating Points and rounding

Quote:
Originally Posted by JBotAlan View Post
Hmm, I use (for example):
Code:
double PAN_DEGREE = whatever
I didn't even know "float" was a data type! C is obviously not my native language; VB6 is. Which has more resolution? Forget it, I'll Google it.

I didn't know about the "f" after the numbers after, but I'd seen that at some point...can't remember where.

Thanks, I'll use some of that in my code!

JBot
ill save you the trouble- double has more resolution. but considering that a double is a 64-bit data type, i wouldn't use it all that much.
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY
  #9   Spotlight this post!  
Unread 15-01-2007, 20:18
Phalanx's Avatar
Phalanx Phalanx is offline
Formerly Team 1089 (Mercury)
AKA: Michael Reffler
FRC #5431 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 1999
Location: Lewisville, TX (previously NJ)
Posts: 384
Phalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond repute
Re: Floating Points and rounding

Quote:
Originally Posted by teh_pwnerer795 View Post
the maximum value being produced is 180. ( 180 degrees). If you could leave some input that would be great
Just a thought, but the the full range of motion of the pan/tilt servos is 180 degrees.
__________________
Don't just ask the experts, become one!
Leadership is not about ability. It's about responsibility!
Diagonally Parked in a Parallel Universe. It's okay we do Quantum Physics


  #10   Spotlight this post!  
Unread 15-01-2007, 20:26
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: Floating Points and rounding

Quote:
Originally Posted by Phalanx View Post
Just a thought, but the the full range of motion of the pan/tilt servos is 180 degrees.
i really dont know at this point which way is the right way..

well i was moving the camera box up and down (tilt).. i was able to move it past 180 degrees... and if the values are 210... then the servos are able to move 210 degrees.. if im not rite... i ono i could be wrong... im extremely tired..
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #11   Spotlight this post!  
Unread 15-01-2007, 20:29
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: Floating Points and rounding

If you are only going up to 180 i don't see why a float is really necessary??? why not just multiply by 100? it'll still get you two digit accuracy

i.e: 180.00 = 18000

as for rounding a floating point number down to four decimals... it is not too hard to do, but why would you need to do it? it's not like reducing the number of decimal points will reduce the processing time more than the cost it takes to round the number.

I think the easiest way to round to four decimals would probably be to mask off the bits of the less significant digits using "&" and a masking variable
(look up bitwise operators)


this may also be of use to u:
http://en.wikipedia.org/wiki/IEEE_754
__________________
Team 701
  #12   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
  #13   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
  #14   Spotlight this post!  
Unread 16-01-2007, 11:22
6600gt's Avatar
6600gt 6600gt is offline
Registered User
AKA: Lohit
FRC #0226 (Hammerhead)
Team Role: Alumni
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Troy, MI
Posts: 221
6600gt is a jewel in the rough6600gt is a jewel in the rough6600gt is a jewel in the rough
Re: Floating Points and rounding

Quote:
Originally Posted by Uberbots View Post
ill save you the trouble- double has more resolution. but considering that a double is a 64-bit data type, i wouldn't use it all that much.
floats and doubles are both 32bit variables in this compiler!
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 20:49.

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