Thread: Variable Typing
View Single Post
  #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,521
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?