Go to Post I wouldn't need a bullet proof vest I would need bullet proof shorts. - rees2001 [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 29-01-2005, 05:46
Unsung FIRST Hero
miketwalker miketwalker is offline
Robot Lifeguard
FRC #1902 (Exploding Bacon)
 
Join Date: Dec 2001
Rookie Year: 2002
Location: Orlando, FL
Posts: 878
miketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond repute
Send a message via AIM to miketwalker
printf problem

I'm working with floats for calculations. I'm trying to use a printf statement to display the value of various floats, but I keep having problems. When I use %d, I get a wacky number over 37,000 which makes no sense. When I use %f, I get no output of the number at all. Could somebody tell me what I'm doing wrong. Here's an example of the format of how I'm doing my printf.

printf ("Value: %f /n", (float)value_name);

I've tried it with %f and %d... as well as trying to use my variables as "double" as well. Same problems though. Any help would be very much appreciated. Thanks!
  #2   Spotlight this post!  
Unread 29-01-2005, 08:38
logicalhippo logicalhippo is offline
Registered User
AKA: Russell McClellan
#0213 (Dirty Birds)
Team Role: Programmer
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Keene, NH
Posts: 11
logicalhippo is an unknown quantity at this point
Send a message via AIM to logicalhippo
Re: printf problem

printf does not support printing of floating points :'(. sorry, it just doesn't work. (see the comment in printf_lib.c)
What SHOULD work is using %d and then explicitly casting the float to an int.
IE: printf("%d\n", (int)fFloat);
Unfortunately, for me, this ALWAYS prints 0, no matter how large fFloat is.
I'm still having trouble with how to convert floats to ints. I wrote a quick function in c that I'm going to test today, and I found assembly routines from 1997 (see product note AN575 on microchip's page) that I will try.

Around here the consensus seems to be that floating points are more trouble than they are worth on this processor. In most cases where you would use floating point on the robot it's possible to do the math in the integer domain by multiplying first then dividing down.
  #3   Spotlight this post!  
Unread 29-01-2005, 16:54
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: printf problem

If you're using the new compiler, I believe the printf() included in that works with floats (check the documentation in .\mcc18\doc).

Last edited by Astronouth7303 : 29-01-2005 at 19:59. Reason: Eating my words
  #4   Spotlight this post!  
Unread 29-01-2005, 19:06
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
FRC #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 751
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Re: printf problem

Ok so I am having the SAME problem. I can't see if my float calculations are working or not. Did you see the README in the IFI released camera code:

Code:
www.InnovationFirst.com

1/8/2004
-   Removed printf_lib.c and printf_lib.h - now using <stdio.h>, compiler needs -nw=2066 to suppress warnings
-   Added user_camera.c and user_camera.h - to remove code from build -> remove the _USE_CMU_CAMERA macro
-   Replaced file named PicSerialDrv with user_serialdrv
-   Note:  when using a 'printf' use a '\r' rather than a '\n' with the new compiler (v2.4)

So I am not using print_f.lib. I am using stdio.h. I am doing the same thing as
miketwalker but I don't get anything to the screen while trying to use floats.

Has anyone else got any input? I am using MPLAB, not CBOT, and the new 2.4 version complier....

I spent all day wondering if our float calculations are actually working.

Is float supported in this PIC?

Can I even do a calculation like this?

If WHEELBASE = 485.35 mm?

Code:
pos_mm_theta = (pos_mm_Left_Encoder_Count - pos_mm_Right_Encoder_Count)/WHEELBASE;
posx_mm = pos_mm_distance * sin(pos_mm_theta);
posy_mm = pos_mm_distance * cos(pos_mm_theta);



Quote:
Originally Posted by Astronouth7303
If you're using the new compiler, I believe the printf() included in that works with floats (check the documentation in .\mcc18\doc).
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #5   Spotlight this post!  
Unread 29-01-2005, 19:58
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: printf problem

Quote:
Originally Posted by chakorules
Is float supported in this PIC?
No.
Quote:
Originally Posted by chakorules
Can I even do a calculation like this?
Yes.

The PIC18 does not contain a floating point unit or floating point instructions. All floating point math is handled in software and handled inline by the compiler. This is why all floating point math takes a long time, and is highly discouraged.

However, from looking at the documentation, it appears that floats are not supported by printf() and related functions. See section 4.7 in .\mcc18\doc\MPLAB-C18-Libraries.pdf for the formats supported.
  #6   Spotlight this post!  
Unread 30-01-2005, 12:01
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,861
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: printf problem

To print floats you can always use a little manipulation like:

Code:
#define ACCURACY 1000	//How many decimal places are important to you
float f;
int i, i2;   // Might need to be longs if you have a lot of significant digits
 
f = 245.56;
 
/* Print float value */ 
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%d \r", i, i2); //e.g., f = 245.559
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 30-01-2005 at 12:29.
  #7   Spotlight this post!  
Unread 31-01-2005, 13:11
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: printf problem

Quote:
Originally Posted by Mark McLeod
To print floats you can always use a little manipulation like:

Code:
#define ACCURACY 1000	//How many decimal places are important to you
float f;
int i, i2;   // Might need to be longs if you have a lot of significant digits
 
f = 245.56;
 
/* Print float value */ 
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%d r", i, i2); //e.g., f = 245.559
You're going to want to be sure leading zeroes are printed for the fractional part. Does this printf implementation support "%03d"?
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #8   Spotlight this post!  
Unread 31-01-2005, 13:19
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,861
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: printf problem

Quote:
Originally Posted by gwross
You're going to want to be sure leading zeroes are printed for the fractional part. Does this printf implementation support "%03d"?
That's very true. I neglected that.
We should verify that the leading zeros can be forced. I'll call one of my kids later to test it on the controller at school to be sure.

[edit] That is supported (and has been tested), so the corrected printf would read:
Code:
printf("f = %d.%03d \r", i, i2);
Thanks Greg.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 31-01-2005 at 14:11.
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
Programming Problem: Extremely Frustrating chantilly_team Programming 19 12-02-2005 23:00
printf problem cbolin Programming 4 09-11-2004 02:44
Anyone with a replacement Printf? Larry Barello Programming 5 19-03-2004 02:20
The problem with scouting... archiver 2001 10 23-06-2002 23:49
Major problem with chipphua motors aka Scott White Motors 18 19-03-2002 19:44


All times are GMT -5. The time now is 00:20.

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