Go to Post So take it all with a grain of salt, form your own opinions, lead by example, act responsibly, be graciously professional, and everything will be ok. - LauraN [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #16   Spotlight this post!  
Unread 06-09-2009, 18:20
mikelowry mikelowry is offline
Registered User
FRC #1771 (N.G.R.)
Team Role: Engineer
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Suwanee GA
Posts: 63
mikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to all
Re: very basic code help

Thanks for all the help. I'm sure i'll be back with a lot more questions after i get some more time with the robot on tuesday.
__________________


2008 Xerox Creativity Award - Championships
2009 Rockwell Automation Innovation in Control award - Peachtree
2009 GM Industrial Design Award - Palmetto
2009 Palmetto Champions <1771 2415 21>
Reply With Quote
  #17   Spotlight this post!  
Unread 08-09-2009, 18:06
garyk garyk is offline
Programming Mentor: 668, 972, 2643
FRC #0668 (Apes of Wrath)
Team Role: Mentor
 
Join Date: Dec 2006
Rookie Year: 2005
Location: Santa Clara (Silicon Valley) Calif.
Posts: 89
garyk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the rough
Re: very basic code help

I've reproduced the running-Autonomous-forever problem (and thanks to Gunderson/2643 for the loan of their control system).

The problem is that Autonomous() never exits, it sits in the while(1) loop forever. Autonomous() gets called only once - ref: WPI's C/C++ Programming Guide for the First Robotics Competition http://first.wpi.edu/FRC/frcupdates.html => C/C++ Programming Guide.

Even removing the while(1) the Auton routine runs 28 seconds: 4*(2+4) and 4 more at the end. I'm sure you know this; the problem is that it's completely blind to Auton mode ending until that 28 seconds is over. In a match, if Teleop mode were to start within that 28 seconds, the 'bot would continue its Auton behaviour until the 28 seconds had lapsed.

I've taken the liberty of fixing this and adding some comments that I hope will explain how, feel free to post questions. It's tested and works. And, congratulations on taking the plunge into C++ programming!

Gary

Code:
#include "WPILib.h"

// if we put the important constants here, they're
// easy to find!
#define WAIT_INTERVAL          0.1 // 0.1 sec = 10 milliseconds
#define DRIVE1_INTERVAL_TENTHS 40  // 40 * 0.1 = 4 seconds
#define DRIVE2_INTERVAL_TENTHS 20  // 2 seconds
#define AUTON_LOOPS             4  // our autonomous loop runs this many times

RobotDrive myRobot(1,2); // motors are driven by PWMs 1 and 2
Joystick leftStick(1);   // left joystick is in USB 1 of the Drivers Station
Joystick rightStick(2);  // and right JS is in USB 2 of the DS

class RobotDemo : public SimpleRobot 
{
public:
	RobotDemo(void) // this function is called only once,
	                // when the 'bot powers on or is reset.
	{
		GetWatchdog().SetEnabled(false); // disable the WDT
	}

	void Autonomous(void) // this function is called only once,
				       // when auton mode is enabled. It's NOT
	                               // called repeatedly.
	
	{  int j; // oldSchool programmers declare variables here :-)
	
		GetWatchdog().SetEnabled(false);
		
		// drive our 'bot four times (AUTON_LOOPS) of two legs each:
			for(int i = 0; i < AUTON_LOOPS; i++)
			{
				myRobot.TankDrive(0.5,-0.5); // start driving, half power (straight? turning?)
				
/* when we call Wait() the program is "blind" to any changes - eg, if Auton mode ends,
 * we can't check this 'till the wait is over. If we were to code Wait(100), for example,
 * the program would obligingly hang for 100 seconds. It couldn't interpret the joysticks,
 * test if Auton mode had ended, etc. The motors would just keep doing what they were
 * doing for those 100 seconds. The only thing that would override this is the disable switch
 * or the equivalent of the Field Control System during a match. The FCS is going to kill
 * the motors after Auton ends and before Teleop begins, but if Teleop began during that
 * 100 second interval, the 'bot would merrily resume doing what it had been doing and ignore
 * the joysticks. 
 * 
 * Until our code exits Autonomous(), OperatorControl() can't begin.
 * 
 * we're going to handle this problem by decreasing the wait to 100 msec. after
 * each 100 msec we're going to check to see if Auton mode is over, or we've looped
 * long enough time to allow the motors to drive the time interval we want.
 * 
 * why 100 msec? why not? the worst we're going to loose is the first 0.1 second
 * of Teleop mode, and no matter how much sugar, caffeine, and adrenaline the drivers
 * are on, it's going to take them that long just to get to the controls.
 * 
 * this isn't a particularly sophisticated way to do this, **but it works**. programmers
 * with more experience may want to look into state machines to do this in a more
 * structured and flexible manner.
 */
				
				// loop as long as j < DRIVE_INTERVAL_TENTHS *AND* we're in auton mode:
				for (j=0; (j < DRIVE1_INTERVAL_TENTHS) && IsAutonomous(); j++)
					{
					 Wait(WAIT_INTERVAL);
					}
				
				myRobot.TankDrive(0.5,0.5); // start driving (turning? straight?)
				
				// as above, keep driving 'till our interval is over, or auton ends:
				for (j=0; (j < DRIVE2_INTERVAL_TENTHS) && IsAutonomous(); j++)
						{
						 Wait(WAIT_INTERVAL);
						}
			} // outer for loop
			myRobot.TankDrive(0.0,0.0); // done, stop the 'bot
			
			
	} // Autonomous()
	
	void OperatorControl(void) // this function is called only once,
							   // when teleop mode begins. It's NOT
							   // called repeatedly.
							   
	{
			GetWatchdog().SetEnabled(false);
			
			while (IsOperatorControl()) // keep looping as long as in teleop mode.
			{
				myRobot.TankDrive(leftStick, rightStick);
				Wait(0.005);
			} // while operator control is true
		
	} // OperatorControl()
};
START_ROBOT_CLASS(RobotDemo);
__________________

Silicon Valley Regional 2005, 2006 972
Silicon Valley Regional 2007 668 Xerox Creativity Award
Championship Event 2007 668
Portland Regional 2008 668
Silicon Valley Regional 2008 668, 972
Beta Test Team 2008 668 (with 100 & 254)
Silicon Valley Regional 2009 668 Regional Chairman's Award; 2643
Sacramento Regional 2009 668 Winning Alliance (thanks, 1717 & 2473!), 2010 Winning Alliance 3256
CalGames 2006, 2007, 2008, 2009, 2010, 2011 Field Tech
NorCal FTC Regional 2008, 2009 Inspector
Championship Event 2009
San Diego, Silicon Valley Regionals; Champ. Event 2010 668, 2643, 3256
Silicon Valley, Madera Regional 2012 2643
WRRF Programming Instructor 2006-2016
Regional Woodie Flowers Award 2014 2643 Utah Regional

Reply With Quote
  #18   Spotlight this post!  
Unread 10-09-2009, 19:42
nighterfighter nighterfighter is offline
1771 Alum, 1771 Mentor
AKA: Matt B
FRC #1771 (1771)
Team Role: Mentor
 
Join Date: Sep 2009
Rookie Year: 2007
Location: Suwanee/Kennesaw, GA
Posts: 835
nighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant future
Re: very basic code help

Wow, thank you so much!

That REALLY explains why we have been having so much frustration, having to power cycle the bot to switch to tele-op! Haha.

Now, I have a very specific question for WindRiver...

How do you change what code is being built and downloaded onto the robot?

We just recently got the code from our Lunacy bot, and are trying to tinker with it on our bot. However, in WindRiver, it is only compiling the code we wrote, and not building the .out file to go onto the robot.

Basically, how do we switch what file we want to build and work on?
Reply With Quote
  #19   Spotlight this post!  
Unread 10-09-2009, 21:41
garyk garyk is offline
Programming Mentor: 668, 972, 2643
FRC #0668 (Apes of Wrath)
Team Role: Mentor
 
Join Date: Dec 2006
Rookie Year: 2005
Location: Santa Clara (Silicon Valley) Calif.
Posts: 89
garyk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the roughgaryk is a jewel in the rough
Re: very basic code help

Hi, NF:

I'm not sure if you mean:

a) How to load your Lunacy code into WRW so you can work on it, download, etc.
b) You have the Lunacy code in WRW and are working on it, but WRW isn't finding the right *.out file with which to download.

I'll go through both these as we've got a lot of hits on this ChiefDelphi thread.

First of all, please backup your hard-earned Auton code we've all been working on. There's a good way to do this, please get into the habit:

In WRW, click on your Auton project in the upper left Project Explorer panel, then do Project => Build Project. This will write any modified files to disc. Then, Project => Clean, "Clean Projects Selected Below", and check your project, leave "Start a new build immediately" off, OK. This will remove some temp files you don't care about.

Exit WRW.

Go to My Computer => C:\WindRiver\workspace. You should see a directory (folder) with the name of your project. If in doubt, click on one, open the *.cpp file with Notepad, and satisfy yourself that it's the project in question.

In C:\WindRiver\workspace, right-click on that folder and select "Send to compressed (zipped) folder". You'll get a .zip folder as a result. Click on it and rename it something meaningful. Mine is "BuiltinDefaultCode.zip" and I'll rename it "BuiltinDefaultCode20090910AutonWorks.zip".

Right-click on that renamed .zip, Properties, Read-only, OK.

Plug in a USB drive and right-click on that .zip, Send to => Removable Disc. Or, burn it on a CD. Or, email it to your gmail. Somehow, MOVE A COPY OFF YOUR COMPUTER. Now, no matter how bad it gets, you have a copy of that project.

Now, I'm going to assume your Lunacy code is a whole folder like we worked on above. If not, skip down after the following prose.


If WRW is running, exit it.
Put the Lunacy code folder under that same folder, C:\WindRiver\workspace. Maybe it's the same name as an existing folder, rename it to "Lunacy" or something else meaningful.

Start WRW. File => Import => Existing Projects Into Workspace, Next. Select Root Directory, Browse, find that folder, OK, Finish.

Now in the upper left WRW "Project Explorer" panel you should see that new project. Click on it and in the center panel, click on the .cpp tab and you should see the Lunacy source code. Project => Build Project and see if it compiles.

(If you have just the Lunacy .cpp file, with WRW not running go to the
workspace\ folder, copy and paste an existing project folder. Rename it to Lunacy, open it, and replace the .cpp file with your Lunacy cpp file. (This is hacky but I don't know a better way to do it, does anyone else?) Then, start WRW and go to the Import step above and see if you get your Lunacy cpp file. If not, let us know. Sneaking files around this way isn't the best way to do it.)

But, we're not done, and we're on to b), telling WRW which file to download to the 'bot. WRW doesn't seem to understand it should find this file automatically, maybe the 2010 season version will. In WRW, click on your Lunacy project in the project-explorer panel, and do a build. Make sure it compiles cleanly because that's the only way to get a .out file.

In WRW: Window => Preferences => First Downloader Preferences. First of all, make sure your team number is correct. Now, you have to find the location of the .out file. Mine is (ready?):

C:\WindRiver\workspace\BuiltinDefaultCode\PPC603gn u\BuiltinDefaultCode\Debug\BuiltinDefaultCode.out

Find the corresponding .out file that matches your project, OK.

If you aren't sure or it doesn't work, do this:

My Computer => C:\WindRiver and right-click the workspace folder => Search. Say:

*.out

in the "All or Part of the Filename" box, Search. (wait). You should have list with one or more files. Click on the Date Modified heading and it will sort it by age. Find the one with the timestamp that matches the WRW build you did. In the WRW First Downloader Preferences window, browse to that file, OK.

After all this, WRW FIRST => Download and it should download because you already have this working. Now, you're in business.

As long as you (and everybody lurking) are still reading, please do this as a good practice. At the very bottom of your new source code, after the
START_ROBOT_CLASS(); call, paste in this paragraph:

Code:
#if 0

2009 09 10 Nightfighter
Imported the Lunacy code from 2009 season. Compiles and downloads.

TO DO:

Learn C++.
Wait for Kickoff.
Build the robot.
Win the Chairman's award.
#endif
This is a region where you can enter free-form text, I don't want to explain the #if 0 stuff now. At the end of the day, or after making major code changes, WHICHEVER COMES FIRST, do the Project => Build Project, Project => Clean, exit WRW, zip, rename, read-only, and write it it to your USB drive or get it off that computer somehow. If it's the end of the day and you have 89 syntax errors, just say you have 89 syntax errors in that text, Project => Clean, and do the rest. If you've made lots of changes but they're not tested yet, or you've found bugs, write in the TO DO section something meaningful to this effect.

If you have a bad day and stuff that used to work doesn't, you can go back to one of the .zip folders and just start from there, it may be simpler than trying to figure out what went wrong and try to back all the changes out.

These good practices, like the backups, using #defines, and adding comments, will save you time in the long run and especially if more than one programmer is working on the source. Please get in the habit now.

Gary
__________________

Silicon Valley Regional 2005, 2006 972
Silicon Valley Regional 2007 668 Xerox Creativity Award
Championship Event 2007 668
Portland Regional 2008 668
Silicon Valley Regional 2008 668, 972
Beta Test Team 2008 668 (with 100 & 254)
Silicon Valley Regional 2009 668 Regional Chairman's Award; 2643
Sacramento Regional 2009 668 Winning Alliance (thanks, 1717 & 2473!), 2010 Winning Alliance 3256
CalGames 2006, 2007, 2008, 2009, 2010, 2011 Field Tech
NorCal FTC Regional 2008, 2009 Inspector
Championship Event 2009
San Diego, Silicon Valley Regionals; Champ. Event 2010 668, 2643, 3256
Silicon Valley, Madera Regional 2012 2643
WRRF Programming Instructor 2006-2016
Regional Woodie Flowers Award 2014 2643 Utah Regional

Reply With Quote
  #20   Spotlight this post!  
Unread 14-09-2009, 19:26
nighterfighter nighterfighter is offline
1771 Alum, 1771 Mentor
AKA: Matt B
FRC #1771 (1771)
Team Role: Mentor
 
Join Date: Sep 2009
Rookie Year: 2007
Location: Suwanee/Kennesaw, GA
Posts: 835
nighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant futurenighterfighter has a brilliant future
Re: very basic code help

Hey, thanks a bunch!

We are going to our last competition before retiring our Lunacy Bot, and the only people attending are me and 2 others!

I think it will be great coding practice, as well as driving practice for us!

Thanks for all the help garyk.

Tomorrow when I get access to the bot, I have another question to ask you.
Reply With Quote
  #21   Spotlight this post!  
Unread 18-09-2009, 18:38
mikelowry mikelowry is offline
Registered User
FRC #1771 (N.G.R.)
Team Role: Engineer
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Suwanee GA
Posts: 63
mikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to allmikelowry is a name known to all
Re: very basic code help

Thanks for all the help everyone!

an update on our progress with windriver:

Yesterday, last year's programmer came back for a visit and showed us everything about windriver and set us up with TortoiseSVN. NighterFighter and I now have separate workspaces and a shared repository. Its really cool.

and now my progress with the code:

I have been looking at our old code from last year and kind of fumbling through it trying to figure out how it works and making some of my own. Our robot last year had an intake and helix and a turreted shooter head. I currently have 2 working (mostly) classes that i wrote - one to control the intake and helix, and one to control the turret, shooter wheel, angle control, and feeder motor.

I dont have access to the code right now because my teammate has the computer in southern georgia for our offseason event while i'm stuck at home, but on monday i want to experiment with using the iterative robot class instead of the simple robot one.
Also on monday, i will put up on here the code that I have written so far.

and now one quick question:

Last year our programmer created his own drivetrain class as an extension of the robot base class to do with traction control. I dont want to write traction control, but i still want to be able to easily deal with encoders. Would it be better for me to convert what i have to the iterative robot template, or to stick with the simple one and just write my own class for the drivetrain?
__________________


2008 Xerox Creativity Award - Championships
2009 Rockwell Automation Innovation in Control award - Peachtree
2009 GM Industrial Design Award - Palmetto
2009 Palmetto Champions <1771 2415 21>
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
High CPU Usage in LabVIEW even with Basic Code Elliot Swart Programming 5 28-01-2009 08:44
Basic Math Help CoasterFuelPhil Programming 21 23-02-2007 21:30
Help programming in Visual Basic xxlshortys Programming 0 06-04-2003 08:54
Very Basic Programming Question kewlkid382 Chit-Chat 5 18-01-2003 11:11


All times are GMT -5. The time now is 12:33.

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