Go to Post ...why do programmers have a nesting habit? - Andrew Schreiber [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 13-01-2004, 18:09
misterikkit's Avatar
misterikkit misterikkit is offline
will code for food
AKA: Jon B
#0258 (Sea Dawgs)
Team Role: Programmer
 
Join Date: Dec 2003
Location: San Jose, CA
Posts: 7
misterikkit will become famous soon enoughmisterikkit will become famous soon enough
Send a message via AIM to misterikkit
Question Trouble with pwm outputs

We're having a hard time with pwm outputs in the default code.

We assign an 8-bit value to, say, pwm02, and then printf pwm02. The output looked like gibberish, but when we printf-ed it in binary format, the 8-bit value was in the high byte of the value.

We've tried various methods of casting, shifting, and other wierd things, all to no avail. (see some examples below) Can anyone help?
Code:
	pwm02 = (unsigned char)Get_Analog_Value(rc_ana_in01)>>2;
	printf("%d\n",  pwm02);
	// output: 10496


Thanks in advance,
Jon B (:
  #2   Spotlight this post!  
Unread 13-01-2004, 18:18
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: Trouble with pwm outputs

Try
Code:
	printf("%d\n",  (int) pwm02);
__________________
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
  #3   Spotlight this post!  
Unread 13-01-2004, 18:35
misterikkit's Avatar
misterikkit misterikkit is offline
will code for food
AKA: Jon B
#0258 (Sea Dawgs)
Team Role: Programmer
 
Join Date: Dec 2003
Location: San Jose, CA
Posts: 7
misterikkit will become famous soon enoughmisterikkit will become famous soon enough
Send a message via AIM to misterikkit
Re: Trouble with pwm outputs

I don't think it was a problem with the printf() function. We were trying to cast an int as an unsigned char, in order to be able to properly work with the pwm outputs. The variable pwm02 is #define'd to txdata.rc_pwm02, which is an unsigned char.
__________________
-j
[:\\|//: ]
  #4   Spotlight this post!  
Unread 13-01-2004, 18:41
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: Trouble with pwm outputs

Quote:
Originally Posted by misterikkit
I don't think it was a problem with the printf() function. We were trying to cast an int as an unsigned char, in order to be able to properly work with the pwm outputs. The variable pwm02 is #define'd to txdata.rc_pwm02, which is an unsigned char.
It wasn't clear from your response if you tried my suggestion or not. I went through similar machinations trying to get a printf to work, and casting within the printf was the only way that would work for me. (BTW, I eventually tried it that way because I noticed that that was the way the default program did it.)
__________________
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
  #5   Spotlight this post!  
Unread 13-01-2004, 20:01
FotoPlasma FotoPlasma is offline
\: |
no team
 
Join Date: Jul 2001
Rookie Year: 2001
Location: San Jose
Posts: 1,900
FotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond reputeFotoPlasma has a reputation beyond repute
Send a message via AIM to FotoPlasma
Re: Trouble with pwm outputs

Okay, we finally figured some things out. PWM outputs require the 7.2V battery to be present for logic and supply. It should have been obvious to us when the red pin was associated with "7.2V," but apparently it wasn't. Whatever.
  #6   Spotlight this post!  
Unread 14-01-2004, 22:44
lpramo55's Avatar
lpramo55 lpramo55 is offline
Registered User
#0353 (POBOTS)
Team Role: Alumni
 
Join Date: Oct 2003
Rookie Year: 2003
Location: Plainview, NY
Posts: 7
lpramo55 is an unknown quantity at this point
Send a message via AIM to lpramo55 Send a message via MSN to lpramo55 Send a message via Yahoo to lpramo55
Re: Trouble with pwm outputs

Quote:
Originally Posted by gwross
Try
Code:
	printf("%d\n",  (int) pwm02);
i found that
Code:
 printf("%3d\n",  (int) pwm02);
i think the 3 has something to do with the digits displayed if i am correct. i used the existing code as a basis for the assumption.
  #7   Spotlight this post!  
Unread 15-01-2004, 00:18
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: Trouble with pwm outputs

The 3 in %3d causes printf() to blank fill the data to three characters. %03d would zero fill it. (This is the way most C compilers implement printf, anyway.)

As FotoPlasma mentioned, though, their problem was that their backup battery was not connected. It turns out it was not a display problem.
__________________
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 15-01-2004, 00:19
Just3D Just3D is offline
Programmer/Electrical Sys
AKA: Justin Layne Nordin
#1460 (Robolution)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Houston, Texas
Posts: 4
Just3D has a little shameless behaviour in the past
Re: Trouble with pwm outputs

If for some reason you get the power set up once more and it still fails, try this, it will help clear up any future printf statements:

Code:
printf("%d\n", &pwm02);
Notice the ampherstand (&). This will tell printf to read the value of pwn02 rather than the memory address of pwn02 (this is why 10000 or so seemed so out of range; memory addresses range all the way up to the maximum RAM).

As for the digits between the % and the 'd', lpramo55 was right in saying the number controls the number of digits output. If you write a 3, it will display 3 digits, adding zeroes as necessary. This also works with decimal places. For example: If you wrote

Code:
float f = 34.67;
printf("%2.1f", &f);
The output would be 34.7, rather than 34.670000.
The number between the '%' and the 'f' in this case represents 2 digits, plus a decimal point, plus 1 additional digit.

Hope this helps!

NOTE: Do not use floats with the robot programming because it has no floating point processor. Instead, stick to ints and unsigned chars, using "%i", and "%d" as needed.

Last edited by Just3D : 15-01-2004 at 00:22. Reason: Clarify information.
  #9   Spotlight this post!  
Unread 15-01-2004, 00:24
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Trouble with pwm outputs

Quote:
Originally Posted by Just3D
Notice the ampherstand (&). This will tell printf to read the value of pwn02 rather than the memory address of pwn02
Correct me if I'm wrong but from my knowledge of C syntax and printf() the & would cause printf() to read the address of the variable, not the value. If you are working with a pointer (which you are not in this case) you would use * to access the value, not &. Unless in a function declaration (or in a binary/logic statement) & always references the address of a variable.

Still, I could be wrong with the C18-based printf() library, you never know...
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #10   Spotlight this post!  
Unread 15-01-2004, 00:29
Just3D Just3D is offline
Programmer/Electrical Sys
AKA: Justin Layne Nordin
#1460 (Robolution)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Houston, Texas
Posts: 4
Just3D has a little shameless behaviour in the past
Re: Trouble with pwm outputs

You are correct. The & symbol denotes the address of a variable, passing this information to the printf function. Printf requires a pointer, which is why the & is needed.

However, I realized my mistake:

This is the behavior of the standard C printf function, and I believe the default code comes with a new re-written printf function, in which case the & might not be necessary at all. I just got started with this specific compiler, so I'm still getting accustomed to the new conventions. Thanks for the insight.
  #11   Spotlight this post!  
Unread 15-01-2004, 00:46
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Trouble with pwm outputs

also, if you are required to pass a value, but you have a pointer-based variable, the asterisk (*) will get you what you need.

quick pointers tutorial:
Code:
char *ptr;                   //declares pointer to a char
ptr = whatever;          //assigns "whatever" as the *address* where ptr is pointing (usually for assigning the address of one pointer to another)
*ptr = whatever;        //assigns "whatever" as the value stored where ptr is pointing (the normal "i want to assign a value")
ptr = &whatever;       //assigns the address of "whatever" to the ptr (essentially, makes ptr point at "whatever")
  #12   Spotlight this post!  
Unread 15-01-2004, 07:53
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Trouble with pwm outputs

Quote:
Originally Posted by Just3D
This is the behavior of the standard C printf
Is that so? Most of my experience with the *printf* standards has been the c++ libraries' implementation of sprintf(). With sprintf() you send the value of the variable, rather than the address. It's odd that printf() would require an address, any reasoning behind it?
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #13   Spotlight this post!  
Unread 15-01-2004, 10:34
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Trouble with pwm outputs

Quote:
Originally Posted by Just3D
You are correct. The & symbol denotes the address of a variable, passing this information to the printf function. Printf requires a pointer, which is why the & is needed.
So that's why printf hasn't worked for me the past 8 years, I should have tried adding an '&' and would have all been good

Please do NOT answer questions if you do not know the answer. Giving people an authoritative answer when you don't really know what you're talking about is bad and is one of the huge downfalls of CD.

printf does not require the address of the variable unless you want to print out the address. It takes the variable itself. Do not use an '&'.

Code:
int a;
a = 5;
printf("%d %d\n", a, &a);
will print
Code:
5 2293484
Notice that the first one printed the value of a and the second printed the address of a (address will vary).

Also, as gwross (someone you should always listen to) said, you need to cast your variables to an integer to get them to print correctly with this implementation of printf:
Code:
printf("%d\n", (int)a);

Quote:
As for the digits between the % and the 'd', lpramo55 was right in saying the number controls the number of digits output. If you write a 3, it will display 3 digits, adding zeroes as necessary. This also works with decimal places.
Also not correct.
Code:
int a;
a = 5;
printf("%3d\n%03d\n", a, a);
will print out:
Code:
  5
005
The zeros will only be printed out if you have a '0' before the number.
  #14   Spotlight this post!  
Unread 15-01-2004, 10:39
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Trouble with pwm outputs

Thanks for the clarification Mike! I was really worried for a while that all the work I had done for years only worked due to an error in my string libraries!! I thought it was pretty strange that a print function would ask for pointers...

I've always done my work with printf() using values but I haven't worked with the C18 stuff yet this year so I wasn't sure if maybe they changed the library for the controller. I'm glad somebody knows the answer!

And as for Just3D, 2 questions: 1) Which team number are you? 2) Have you been using & in your printf() statments all along??
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #15   Spotlight this post!  
Unread 15-01-2004, 12:47
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Trouble with pwm outputs

the only situation in which I could think printf would use a pointer is in printing a string, as that is a character array and thus must be passed by reference.

as Mike said, you have to have the 0 in the % code to tell printf to pad with zeros - otherwise it pads with spaces.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
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
Tether for new EduBot Controller gburlison Programming 9 18-12-2003 17:53
pwm 13-15 wayne 05 Programming 2 04-10-2003 12:08
PWM and burning out motors patrickrd Technical Discussion 7 19-06-2003 15:30
Hints For Rookie Programmers Noah Programming 30 20-02-2003 20:07
relay and pwm outputs nick_champ_2 Electrical 1 31-01-2003 13:08


All times are GMT -5. The time now is 02:40.

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