View Single Post
  #3   Spotlight this post!  
Unread 17-01-2006, 01:03
Matt Adams's Avatar
Matt Adams Matt Adams is offline
b(o_o)d
FRC #1525 (Warbots)
Team Role: Engineer
 
Join Date: Dec 2003
Rookie Year: 2000
Location: Arlington Hts. IL
Posts: 375
Matt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond reputeMatt Adams has a reputation beyond repute
Send a message via AIM to Matt Adams
Re: Strange Problem, varible not subtracting?

Quote:
Originally Posted by chakorules
Hi Matt,

Thanks for the time in checking this out.

We don't want to set that varible first, or before the math. It is true that the first loop through the CPU it will be ZERO. But then it gets a value (50) at the bottom on the first loop. Shouldn't the CPU remember this value, so in loop pass #2, now that varible is set to 50 and it should correctly excute the subtract math?

We only want to update this varible at the end because we are doing a velocity measurement sampling the encoder counts.
This is a special case, because its a seperate function. Each time you call this function, everything starts from scratch, assuming you are not sending the value of enc_Right_Old_Count to this function each time (which is appears you aren't). I notice that you are only returning the value of enc_Right_Distance, so the primary loop that is calling this function has no recollection of what might have been previously use internally with this function (read: it doesn't know what the value of enc_Right_Old_Count might have been).

If you want to reuse the value of enc_Right_Old_Count, one way is to return this value from your function and then send it back as an arguement each time its called in your main loop.

This is where... my syntax experience with C gets foggy. If you want to return and reuse both parameters, I think you'll actually need to return them as an array, since you can't return more than 1 variable from a function. You'll need to make a 2 element array, return them and if you wish for cosmetic reasons, split them up.

Some brief example code inside the function right_velocity would be:

Code:
long velocity_right(enc_Right_Old_Count)
{
long distance_and_old_count[2]; // declares this array
 
** MATH **
 
distance_and_old_count[0] = enc_Right_Difference;
distance_and_old_count[1] = enc_Right_Old_Count;
 
return distance_and_old_count;
}
The [2] specifies the size, but the array actually starts at element [0]. That's what makes C a little different than some other languages.


In the main outter loop, you would use something like the following to send the en_Right_Old_Count value to the function:
Code:
distance_and_old_count = right_velocity(enc_Right_Old_Count);
enc_Right_Difference = distance_and_old_count[0];
enc_Right_Old_Count = distance_and_old_count[1];
Matt

Last edited by Matt Adams : 17-01-2006 at 01:15.