Go to Post We may not build the best robots but we're building better students in the long run and means much much more. - Koko Ed [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 07-11-2003, 08:34
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
unions in C

This is a question for any embedded C experts. If you declare a union, is that handled completely by the compiler? One thing that I like about PBASIC is the way you can access bits and nibbles of a byte directly. If you declare a union in c like this:
Code:
union pbyte {
	struct {
		unsigned b0:1;
		unsigned b1:1;
		unsigned b2:1;
		unsigned b3:1;
		unsigned b4:1;
		unsigned b5:1;
		unsigned b6:1;
		unsigned b7:1;
	};
	struct {
		unsigned nib0:4;
		unsigned nib1:4;
	};
	unsigned char byte;
};
will there be a performance hit? According the the C specs it will allocate enough room for the biggest item, so the total size should only be a byte. Is it better practice in the long run to just write macros to get/set/clear the bits instead?
  #2   Spotlight this post!  
Unread 07-11-2003, 11:44
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
Unions should be handled entirely by the compiler so there really shouldn't be too large of a performance hit. However, I'd recommend using macro's to manipulate bits using binary math (and, or, xor, not, etc.). After all, that's all the compiler is doing and you may be able to optomize it better than the compiler does.

Matt
  #3   Spotlight this post!  
Unread 09-11-2003, 13:00
Joe Johnson's Avatar Unsung FIRST Hero
Joe Johnson Joe Johnson is offline
Engineer at Medrobotics
AKA: Dr. Joe
FRC #0088 (TJ2)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Raynham, MA
Posts: 2,648
Joe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond repute
Unions & natural PWM represenation...

One of the things I HOPED Innovation First woud do with the C controller was to use signed charaters types for joystick and PWM data.

Basically, this would allow for easy scaling, etc.

0 = off
-127 = full reverse
127 = full forward

It is beautiful and elegant for many reasons.

BUT...

I would rather not just waste the variable space to have 16 inputs from the OI and 16 PWM outputs more or less wasted just because I want to use an elegant solution.

I would like to use unions to allow these modified PWM outputs and Joystick values to be stored in the same location as the unsigned character variables that Innovation First defines.

To use this I would just do something like this just after the getdata statement:
p1_y.DrJoe = p1_y.InnovationFirst - (signed char) 127

The (signed char) is a cast that is required allow the math on the right side of the equation to be performed using signed math.

Also, just before the PWM output statements, I would have to do something like this:
pwm01.InnovationFirst = pwm01.DrJoe + (unsigned char) 127

Again, the cast is needed to get the math work out right (actually, now that I think about it, I may need to put in an (unsigned int)... ...I have to go look it up or try it out)

Anyway, I have 2 questions for Union Geeks...

In order to do this, I will have to dig into the structure that Innovation First defines for the recieve data as well as the #define statement where they define p1_y. I will also have to redefine the transmit data structure and its associated macros.

#1 Will this screw up the library functions (which we don't have access to) which access the same structure?

#2 Is there a better way to do this?

Do tell.

Joe J.
  #4   Spotlight this post!  
Unread 10-11-2003, 15:39
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Here is how you actually can do it: (Yes I've tried this)


In ifi_default.h change the rc_pwm** variables to signed chars

then, right before PutData(),

pwm01 += 127;
pwm02 += 127;
...



A similar thing should work for the inputs (don't have a rc unit hany to try)
  #5   Spotlight this post!  
Unread 10-11-2003, 18:42
Joe Johnson's Avatar Unsung FIRST Hero
Joe Johnson Joe Johnson is offline
Engineer at Medrobotics
AKA: Dr. Joe
FRC #0088 (TJ2)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Raynham, MA
Posts: 2,648
Joe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond repute
cool...

I am going to try it tonight.

I am a little disturbed by this but I think I will settle down after I break out my Kernighan & Ritchie.

Here is the nub of my concern:

Are we creating processor/compiler specific code (i.e. not portable code)? The library functions for getting and putting data are expecting unsigned character types. The fact that we are sending signed character types to them seems not right somehow (yes I know that we are actually storing an unsigned character in them when we send the data -- the += 127 bit, but it still creeps me out a bit).

Anyway, I will have to try it out.

Thanks for the help.

Joe J.
  #6   Spotlight this post!  
Unread 10-11-2003, 20:21
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
It will matter in some cases what you use and not matter in other cases.

Let me try and explain why. A signed byte and an unsigned byte represent the numbers from 0 to 127 in exactly the same manner. The values from 128 to 255 in an unsigned byte are represented as the numbers from -128 to -1 in a signed byte. Basically, 255 is represented in the same manner that -1 is represented. Now, for 0 to 255 output values, you don't want this to occur.

Now, as long as you correctly subtract 127 and then add 127, there should be no difference in the ending output as long as simple arithmetic is used (addition, subtraction, multiplication, and division). That's a function of binary arithmetic and not dependent on a certain processor. Basically, the code should be portable in that case. If you're using bitwise operations, I'd be a bit more careful.

My suggestion is to use a a word variable for calculations and then cast it to an unsigned char for output. It will waste a bit of RAM space but it will make sure there's enough room for all variable calculations.

Matt
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
How much do you have to pay.... Vin211 General Forum 24 25-03-2002 20:59


All times are GMT -5. The time now is 19:54.

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