Go to Post Why assume a full trophy case and spreading the message of FIRST (mentoring rookies) are mutually exclusive? Team 16 supported 6 rookies this season and earned 5 banners - I am equally proud of both. - Meredith Novak [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 16-01-2006, 23:07
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
FRC #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 751
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Strange Problem, varible not subtracting?

Ok, this is one of those stupid simple problems that we don't get it. In this post is FIVE screen shots of our code and a zip file of the project in easyC along with the screen shots again.

What we are trying to do is subtract two varibles, then assign it to another varible in easyC. Sounds simple right?

In screen shot #1 you see the varible we are having problems with:

enc_Right_Difference = 10-5

Then in the terminal window, you see enc_Right_Difference = 5. This is correct. No problems here.

In screen shot #2 you see the varible enc_Right_Difference = Count minus 100. And the terminal window reports a 100 difference between the two varible. No problems here.

In screen shot #3 you see the varible enc_Right_Difference = 100 - Old_Count. And the terminal window reports a 100....uh oh problems here....But wait...Shouldn't enc_Right_Old_Count get updated by enc_Right_Count?? Two lines down. Hmm.....It's not updating varible enc_Right_Old_Count so then now enc_Right_Difference is ALWAYS equal to 100. No good.

In screen shot #4 you see enc_Right_Old_Count is now assigned to a constant 50, and the terminal screen shows that indeed that varible is 50, BUT WAIT....just above back up two line for varible enc_Right_Difference = 100 - enc_Right_Old_Count. The controller KNOWS THAT enc_Right_Old_Count is equal to 50, but why isn't it SUBTRACTING????

Are we losing our minds?

In screen shot #5 is our orginal code before doing all this research debugging to a terminal window...

Why won't in screen shot #4 it subtract? It's like that varible gets blown away.

Is it because when it returns a varible at the bottom is loses it's orginal value? Just trying to help our kids understand it and it stumped me too....
Attached Thumbnails
Click image for larger version

Name:	screen1.jpg
Views:	45
Size:	53.2 KB
ID:	3832  Click image for larger version

Name:	screen2.jpg
Views:	40
Size:	55.2 KB
ID:	3833  Click image for larger version

Name:	screen3.jpg
Views:	33
Size:	51.8 KB
ID:	3834  Click image for larger version

Name:	screen4.jpg
Views:	40
Size:	53.4 KB
ID:	3835  Click image for larger version

Name:	screen5.jpg
Views:	37
Size:	57.3 KB
ID:	3836  

__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #2   Spotlight this post!  
Unread 16-01-2006, 23:08
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
FRC #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 751
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Re: Strange Problem, varible not subtracting?

Here is the project file ZIP incase you want to open in easyC.
Attached Files
File Type: zip mess.zip (578.1 KB, 34 views)
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #3   Spotlight this post!  
Unread 16-01-2006, 23:55
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?

I see 3 lines involved, minus comments:

enc_Right_Difference = 100 - Enc_Right_Count_Old;
enc_Right_Old_Count = 50;
Print enc_Right_Difference


So... basically this is saying

enc_Right Difference = 100 - enc_Right_Count_Old_Which_Isn't_Set_To_Anything_Ye t
Set enc_Right_Count_Old to 50
print enc_Right_Difference

I don't really see anything that suprises me. I think you wanted to do:

Set enc_Right_Count_Old to 50
enc_Right Difference = 100 - enc_Right_Count_Old
print enc_Right_Difference (which will be 50)

You need to set your variable enc_Right_Count_Old = 50 before you want to use math involving it, or it will just use zero (it doesn't know any better!). Move it to the line above the subtraction, it should work as expected.

To better visualize this, you can put print statements right before you do the math and right after the math, and you can see what the variables are set equal to. You happened to put them in places that give you (seemingly) unexpected results.

Hope this helps,

Matt

Last edited by Matt Adams : 17-01-2006 at 00:02.
  #4   Spotlight this post!  
Unread 17-01-2006, 00:22
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
FRC #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 751
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Re: Strange Problem, varible not subtracting?

Quote:
Originally Posted by Matt Adams
You need to set your variable enc_Right_Count_Old = 50 before you want to use math involving it, or it will just use zero (it doesn't know any better!). Move it to the line above the subtraction, it should work as expected.Matt

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.

I posted Screen 1 through 4 to show you what we are trying to do our testing, basiclly to eliminate "Have you tried this? Have you tried that?" Sort of thing....

Screen shot #5 is what we want, but we found out the problem in screen shot 3 and 4. By the varible updating but not being able to get subtracted above it.
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369

Last edited by Chris_Elston : 17-01-2006 at 00:24.
  #5   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.
  #6   Spotlight this post!  
Unread 17-01-2006, 07:55
Gdeaver Gdeaver is offline
Registered User
FRC #1640
Team Role: Mentor
 
Join Date: Mar 2004
Rookie Year: 2001
Location: West Chester, Pa.
Posts: 1,370
Gdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond reputeGdeaver has a reputation beyond repute
Re: Strange Problem, varible not subtracting?

Use global variables if you want variables to be persistent through many function calls.

Last edited by Gdeaver : 17-01-2006 at 07:57.
  #7   Spotlight this post!  
Unread 17-01-2006, 08:20
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Strange Problem, varible not subtracting?

Quote:
Originally Posted by chakorules
Here is the project file ZIP incase you want to open in easyC.
What Matt Adams said is correct.

Here's what's going on. In your "right_velocity" function the variables are all implicitly declared as auto storage class.

long enc_Right_Old_Count = 0;

What this means is that the variables are allocated as the function is entered and deleted when it exits. This is the default in C to save space - temporary variables in one function can use the space of variables in another function. And in your case you initialize them to 0, so that subtraction of 100-enc_Right_Old_Count will always be 100 since enc_Right_Old_Count is initialized to zero each time the function is entered.

Fortunately the fix is easy - you need to make "enc_Right_Old_Count" not get reinitialized each time. You can do this by declaring the variable as a global in the main function. That way it will only be initialized once when the program starts and not every time the function is declared. To do this, go to the main function, and double click on the "Globals" block and add it there.

Be sure to remove it from the right_velocity funtion otherwise the local declaration will override the global for that function and you'll still get the same results.

There might be better ways of doing this without global variables if you were hand coding in C, but this will solve the problem easily in EasyC.

Hope this helps.
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
  #8   Spotlight this post!  
Unread 17-01-2006, 22:17
Chris_Elston's Avatar
Chris_Elston Chris_Elston is offline
Controls Engineer
AKA: chakorules
FRC #1501 (Team THRUST)
Team Role: Engineer
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Huntington, Indiana
Posts: 751
Chris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond reputeChris_Elston has a reputation beyond repute
Re: Strange Problem, varible not subtracting?

Quote:
Originally Posted by BradAMiller
There might be better ways of doing this without global variables if you were hand coding in C, but this will solve the problem easily in EasyC.

Hope this helps.

Thanks again guys. I dunno how we got through last year. We must have used all globals and didn't know it. I don't remember this "rule" when programming in C. But now I will never forget and neither will my programmers. BTY, it works like a champ now using globals. Looks too messy to try and do with an array in EasyC, or even doing your own structures...It doesn't look like EasyC support structures, but I did see something in the help on Arrays, we need to read up on that...


Again, thanks a bunch.
__________________
Team T.H.R.U.S.T. 1501
Download all of our past robot's source code here:Repository

Favorite CD quote:
"That can't be their 'bot. not nearly enough (if any) rivets to be a 1501 machine." ~RogerR: Team #1369
  #9   Spotlight this post!  
Unread 18-01-2006, 09:31
Kingofl337's Avatar
Kingofl337 Kingofl337 is offline
You didn't see anything....
AKA: Adam
FRC #0501 (Power Knights)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 1998
Location: Manchester, NH
Posts: 861
Kingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond repute
Send a message via Yahoo to Kingofl337
Re: Strange Problem, varible not subtracting?

Arrays work fine in EasyC. The problem is the complier limits the program to 250 "char" slots total in the program. Int is limited to 120.
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
Need a realistic Statics Problem sanddrag Technical Discussion 10 05-12-2005 15:07
Strange system problem Bill Becker Control System 16 12-04-2005 17:19
Strange Multi-Turn Potentiometer Problem Yellow Eyes Electrical 10 24-02-2005 13:53
Strange Encoder Problem AIBob Electrical 3 20-02-2005 22:20
Strange Auto Problem NotQuiteFree Programming 6 20-02-2005 17:12


All times are GMT -5. The time now is 23:10.

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