OCCRA
Go to Post "In PID, Integrals are like diamonds. They are forever!" - Sam390250 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-10-2007, 01:06 PM
billdar billdar is offline
Registered User
FRC #2085
 
Join Date: Jan 2007
Location: california
Posts: 3
billdar is an unknown quantity at this point
Variable Scope

I know its pretty late asking these questions, but we haven't gotten
to building a competition port dongle yet and I have a nagging question
on variable scope within the 3 WPI functions (Initialize, Autonomous,
Operator).

The kids have been practicing with code laid out like this:
main()
{
int someVariable;
while (1)
{
doSomethingTo(someVariable);
}
}
And they have been assuming that someVariable will retain its
value each time it loops.

Two questions on this, if they write the same kind of code in either
the Autonomous() or OperatorControl() functions:
- Will the forever loops break the background competition port monitoring
task?
- How do I define a variable that retains its value when Autonomous()
or OperatorControl() loops?
Thanks,

-Bill
Team 2085
Reply With Quote
  #2   Spotlight this post!  
Unread 02-11-2007, 05:26 AM
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 537
BradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud ofBradAMiller has much to be proud of
Re: Variable Scope

WPILib will automatically break the program out of Autonomous() when the operator control period starts. Even if you have an infinite loop in your autonomous function it will put you into the OperatorControl() function when the autonomous period ends. It will not break you out of the Initialize() function which is where you probably want to be initializing things like cameras, gyros, etc.

The variable scoping is just as you described and follows normal C rules. However those variables declared as locals in the Autonomous(), OperatorControl() or Initialize() functions will not be available to each other or to other functions (unless passed as arguments).

To make variables that are available between functions they need to be global in scope - declared outside of any functions. And you need extern declarations for them somewhere, probably in a header file.

Also, you should not put anything in the main function for a competition program - but an empty main function needs to be there to satisfy a linker requirement.
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #3   Spotlight this post!  
Unread 02-11-2007, 10:12 PM
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Variable Scope

If you are using EasyC then go to the Main() routine and create variables in the Globals block.
Reply With Quote
Reply


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
camera scope problems xrabohrok Programming 4 01-15-2007 07:48 PM
Camera variable scope question Validius Programming 1 01-28-2006 08:24 AM
Save a variable dawonn Programming 7 04-23-2005 07:30 AM
Interrupt Handlers and Variable Scope kaszeta Programming 2 02-14-2004 05:30 PM
variable? manodrum Programming 11 04-01-2003 04:20 PM


All times are GMT -5. The time now is 06:07 PM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi