Go to Post No! I will not be an enabler for this madness. Go outside. See a movie. Read a book. LIVE!!!! - Herodotus [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 11-01-2008, 08:24
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,534
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Variable Typing

My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).

Changing it as one of the team members suggested:

int(p1_x) and int(p1_y) resulted in a syntax error.

Our final result was this, which works fine:

x_joy_1 = p1_x;
y_joy_1 = p1_y;

x_joy_1 = x_joy_1 - 127; // Makes the range -127 to 127
y_joy_1 = y_joy_1 - 127;

printf("x_joy_1 is %d\r\n",x_joy_1);
printf("y_joy_1 is %d\r\n",y_joy_1);

Could someone shine some light on why this works the way it does? I'm sick of trial and error with variable int types Isn't there a much more elegant way of coding this?
  #2   Spotlight this post!  
Unread 11-01-2008, 08:29
tseres's Avatar
tseres tseres is offline
obsessed with FIRST...
FRC #1565 (Think Tank Tech)
Team Role: Leadership
 
Join Date: Apr 2007
Rookie Year: 2007
Location: Cambridge, ON
Posts: 305
tseres is a glorious beacon of lighttseres is a glorious beacon of lighttseres is a glorious beacon of lighttseres is a glorious beacon of lighttseres is a glorious beacon of lighttseres is a glorious beacon of light
Re: Variable Typing

it works because the

x_joy_1 = p1_x;
y_joy_1 = p1_y;

makes thevariables equal to the joystick values. the second part just subtracts 127 from them. the point is that the variable took the values them manipulated them.....i hope that answered your question.
__________________


  #3   Spotlight this post!  
Unread 11-01-2008, 08:32
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,534
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Variable Typing

Thank you, but that doesn't really help. I'm interested in why the first statements don't work - why isn't the value from p1_x and p1_y treated as a value I can subtract 127 from?
  #4   Spotlight this post!  
Unread 11-01-2008, 08:36
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Variable Typing

Your first try looks fine to me. What was the error you encountered?
  #5   Spotlight this post!  
Unread 11-01-2008, 08:49
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,534
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Variable Typing

When we print out the result of the first two statements using the printf's at the end, we get values that don't make sense (0-255) and don't follow the joystick values in any way.
  #6   Spotlight this post!  
Unread 11-01-2008, 08:52
Daniel Jones Daniel Jones is offline
Registered User
AKA: Hanes
FRC #0190 (Gompei and the HERD)
Team Role: Mentor
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Shrewsbury MA
Posts: 13
Daniel Jones is on a distinguished road
Re: Variable Typing

The variable p1_y is an unsigned character with a range from 0 -255.
When you subtract 127 from it, if it would result in a negative number it instead overflows, resulting in a positive number above 127. This number is then stored in your int.
There are several ways to fix this, and you were close with int(p1_y). Try using (int)p1_y - 127 instead, this forces the program to treat p1_y as an integer while it subtracts so you should not have conversion problems. This is called typecasting.
The code that did work works because you used an integer to store p1_y before doing the subtraction, achieving the same effect as typecasting.
Also keep in mind that p1_y is not of type define, it is an unsigned character. A define statement literally replaces one string with another, so the define statement just makes p1_y an alias for another variable.
__________________
"there are 10 types of people in this world, those who understand binary and those who don't"
  #7   Spotlight this post!  
Unread 11-01-2008, 09:04
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,534
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Variable Typing

Thank you! We didn't go far enough back to discover that it was an unsigned character, though I suspected it was not the appropriate type which is why I tried to typecast it. Go figure, after a year off I tried treating it like I would an excel statement rather than a C statement.
  #8   Spotlight this post!  
Unread 11-01-2008, 10:02
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,586
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Variable Typing

Quote:
Originally Posted by Tom Line View Post
My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).
This statement would work in Visual Studio or another ISO/ANSI compliant compiler. Microchip's deviation is described in the C18 compiler user's guide:

Quote:
2.7.1 Integer Promotions
ISO mandates that all arithmetic be performed at int precision or greater. By default,
MPLAB C18 will perform arithmetic at the size of the largest operand, even if both
operands are smaller than an int. The ISO mandated behavior can be instated via the
-Oi command-line option.

For example:
Code:
unsigned char a, b;
unsigned i;
a = b = 0x80;
i = a + b; /* ISO requires that i == 0x100, but in C18 i == 0 */
Note that this divergence also applies to constant literals. The chosen type for constant
literals is the first one from the appropriate group that can represent the value of the
constant without overflow.

For example:
Code:
#define A 0x10 /* A will be considered a char unless -Oi
                  specified */
#define B 0x10 /* B will be considered a char unless -Oi
                  specified */
#define C (A) * (B)
unsigned i;
i = C; /* ISO requires that i == 0x100, but in C18 i == 0 */
We use -Oi because it makes things a lot easier to troubleshoot and makes the code clearer without a bunch of casts.
  #9   Spotlight this post!  
Unread 11-01-2008, 13:07
ay2b's Avatar
ay2b ay2b is offline
Registered User
AKA: Andy
FRC #2928
Team Role: Mentor
 
Join Date: Mar 2004
Rookie Year: 1994
Location: Seattle, WA
Posts: 211
ay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant future
Re: Variable Typing

Quote:
Originally Posted by Tom Line View Post
My team wants to subtract 127 from each joystick to change the range from 0 to 255 to -127 to 127. We originally had trouble with this statement:

x_joy_1 = p1_x - 127; // Makes the range -127 to 127
y_joy_1 = p1_y - 127;

p1_x is a defined term from the rcdata. x_joy_1 is an int. I'm not precisely sure why we had trouble - I suspect it's because p1_x is of the wrong type (it's a define, not an int).

Changing it as one of the team members suggested:

int(p1_x) and int(p1_y) resulted in a syntax error.
The correct syntax is (int)p1_x. This is called "type casting".

Try this:
Code:
  x_joy_1 = (int)p1_x - 127;  // Makes the range -127 to 127
  y_joy_1 = (int)p1_y - 127;

Quote:
Originally Posted by Tom Line View Post
Could someone shine some light on why this works the way it does? I'm sick of trial and error with variable int types Isn't there a much more elegant way of coding this?
The problem you're facing is one of one of order of operations. Consider this statement: 5 + 5 * 5. What is the value? Is it 50 or 30? If you do the addition first, you'll get 10 * 5 = 50. If you do the multiplication first, you'll get 5 + 25 = 30. In C, multiplication is defined to come before addition.

In the statement above, x_joy_1 = p1_x - 127, everything to the right of the equals sign is evaluated before the assignment operator (i.e. the equals sign). First the compiler does p1_x - 127. p1_x is an unsigned char which means it has a range of 0-255. Suppose it had a value of 122. Now, when you subtract 127, you get -5. However, since that is outside of the range for its type, it wraps around, and so you get 250. Next comes the assignment operator. x_joy_1 is of type int, while the value on the right is an unsigned char. The compiler now automatically typecasts the value on the right to be the same type as the variable on the left, in order to do the assignment.

What you want to do is make sure that the value on the right is an int before the subtraction operator happens. To do this, you typecast the unsigned char to an int with the statement (int)p1_x.


Quote:
Originally Posted by Tom Line View Post
p1_x is of the wrong type (it's a define, not an int)
While p1_x is "a define", that is not a type. To find out what type it is, you have to see what it's defined as. It's defined as something which has a type of unsigned char.
__________________

2011 - SD Quarterfinalists (980), LA Quarterfinalists (980)
2010 - LA (2404) Finalists (980), AZ Motorola Quality (980)
2009 - LA Semifinalists (980); Las Vegas Quarterfinalists (980); SD (2404); IRI #1 Seed, Finalist (980)
2008 - SD Quarterfinalists (980), LA Champions (980), LA Rookie Inspiration Award (2404); CalGames Finalists
2007 - So.Cal Finalists (980), SD Quarterfinalists (980); CalGames Finalists
2006 - So.Cal Regional Champion (4), Toronto Judge's Award Day 1 (4)
2005 - SVR Champions, Delphi "Driving Tomorrow's Technology" (980); AZ Xerox Creativity (980); So.Cal Finalists, RadioShack Innovation in Control (980); Championship Archimedes Division Semifinalists; IRI Finalists (980)
2004 - So.Cal Regional Champions, Leadership in Controls (980); AZ GM Industrial Design (980); Championship Galileo Division #2 Seed; IRI Champions
2003 - PNW Semi-finalists (488)
2002 - PNW Finalists (488)
2000 - X-bot / 488 - Mentor / Founder
1994 - Sunny Delight - Driver - champion
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
disabled typing Devo1098 Programming 2 22-03-2007 18:43
variable? manodrum Programming 11 01-04-2003 17:20
26 variable limit Gusman1188 Programming 9 12-02-2003 22:38
PBasic Variable Matt Reiland Programming 4 15-01-2002 13:16


All times are GMT -5. The time now is 18:42.

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