FIRST Forums  

Go Back   FIRST Forums > FRC Archive > 2009 FRC Control System Public Forum > Programming Showcase

Reply
 
Thread Tools
  #1  
Old 10-23-2008, 01:21 PM
Dave Doerr Dave Doerr is offline
Member
 
Join Date: Oct 2008
Location: Milford MI USA
Posts: 32
Default Team 67 C++ Code

Team 67 have ported our 2007 Rack n' Roll teleoperated code to the Beta control system. We're working on porting our autonomous code, and you can see the beginnings of that in the attached files.

The 2007 Hot Bot is a six-wheel-center-drop tank drive with two CIMS. A four-bar linkage arm is driven by a single CIM. The "hand" at the arm end is driven by two motors to grab, release and tilt the tube.

Driver control is achieved via two Logitech GamePads, one for tank-driving and one to operate the arm. One GamePad joystick can be used to manually operate the arm, and buttons 1, 2, 3 and 4 are used to move the arm to setpoints by means of PID control. Another GamePad joystick is used to operate the two hand motors. The hand motor direction can be changed by pressing one of the front GamePad buttons, so the Rack n' Roll tube can either be grabbed/released or tilted depending on motor phasing.

The robot has a potentiometer to measure arm angle, a gyro, left and right-side encoders, and a Axis 206 camera that fits nicely into the 2007 CMUCam pan-tilt frame.

Two C++ robot-programming classes were available to choose from -- a SimpleRobot and IterativeRobot.

IterativeRobot works very much like the FRC code we've been using the past few years in that a master while-loop is used to repetatively execute our user-written code. For example when running the teleop code in the attached example, TeleopInit() is called once on entry, and then TeleopPeriodic() is called at a 10ms rate. Autonomous and disabled mode work the same way.

SimpleRobot, on the other hand, does not run a master while-loop, so user-written-code is executed once sequentially. If you wanted to run part of your code repetatively -- to wait for something, for example -- you would use a loop within your code.

Team 67 chose to use -- at least for now -- the IterativeRobot class because it is similar to what we've been using in past seasons. We thought it would be easier for us to implement quickly and also to explain to other teams.

I won't try to explain more of the code right now -- I can probably do that best by answering questions if they arise.

Dave D
Attached Files
File Type: zip HOT_Bot_2008.10.23.zip (7.7 KB, 634 views)
__________________
FRC: HOT, Team 67, Mentor, C++ Beta Test Lead
FLL: Dark Matter Dark Energy, Team 3069, Coach
Reply With Quote
  #2  
Old 10-23-2008, 04:57 PM
willson.thomas
Guest
 
Posts: n/a
Default Re: Team 67 C++ Code

Awesome!

I am so glad to finally see some C++ Code!!!
Reply With Quote
  #3  
Old 10-24-2008, 03:15 AM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

hallelujah!!

Finaly, some code I can read!

MORE!!!!! O_O!!!!! I NEED MORE C++!!!!


P.S
Time for questionts: 1) Why use a pointer for almost all variables (when you create them)?

Last edited by Bomberofdoom; 10-24-2008 at 03:27 AM.
Reply With Quote
  #4  
Old 10-24-2008, 08:39 AM
Dave Doerr Dave Doerr is offline
Member
 
Join Date: Oct 2008
Location: Milford MI USA
Posts: 32
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Bomberofdoom View Post
hallelujah!!
1) Why use a pointer for almost all variables (when you create them)?
Glad to see others share my enthusiasm for C++!

Those pointers you see declared are later initialized to point to C++ objects such as gamepads, drive motors, etc. The pointers allow you to later access the objects and perform operations on them (methods in C++ speak). For instance, declare a pointer to a "Victor" object:

Victor *armMotor;

Then intialize the pointer to the arm victor on PWM output 7:

armMotor = new Victor(7);

Then turn on the arm motor by using the Victor method "Set()":

armMotor->Set(gamePad2->GetLeftY());

Cool, huh! Somebody with more C++ experience under their belt might need to chime in and help with my explanation. BTW, there are lots of non-pointer variables in there, too.

Dave D
__________________
FRC: HOT, Team 67, Mentor, C++ Beta Test Lead
FLL: Dark Matter Dark Energy, Team 3069, Coach
Reply With Quote
  #5  
Old 10-24-2008, 10:00 AM
Hjelstrom Hjelstrom is offline
Junior Member
 
Join Date: Oct 2008
Location: Las Vegas
Posts: 18
Default Re: Team 67 C++ Code

Thanks for posting this code! We're excited to use C++ this year so its great to see some actual examples.

How is the debugger support? Can you step through code running on the cRio? Can you inspect the values of variables? Print to a debug window on the driver station?

Greg
Reply With Quote
  #6  
Old 10-24-2008, 12:58 PM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Dave Doerr View Post
Glad to see others share my enthusiasm for C++!

...

Cool, huh! Somebody with more C++ experience under their belt might need to chime in and help with my explanation. BTW, there are lots of non-pointer variables in there, too.

Dave D
I'm a "Moderate" C++ programmer (pretty much, at least....over 2 years of programming, created some PC aps and working on a flight sim game). Even so, I still forget some fundamentals and basic tricks of the language.

I can understand the idea of using a pointer, but from my expirence with OOP, you really don't have to.

If it's possible, I'd like Team 67 to try to write the following instead of:

Victor *ArmMotor;
ArmMotor = new Victor(7);

Like this:

Victor ArmMotor = new Victor(7);

It supposed to be the exact same thing, without the pointer(*) and doing the initialization the same time the ArmMotor object is defined. If it doesn't work, then I'll have to look further into this to see why.
Reply With Quote
  #7  
Old 10-24-2008, 05:58 PM
sharrington
Guest
 
Posts: n/a
Default Re: Team 67 C++ Code

Quote:
Victor ArmMotor = new Victor(7);

It supposed to be the exact same thing, without the pointer(*) and doing the initialization the same time the ArmMotor object is defined. If it doesn't work, then I'll have to look further into this to see why.
Actually, the code to declare a local variable is:

Code:
Victor ArmMotor(7);
If you wanted to use local/member variables in a class, you can initialize them at the beginning of your constructor, like:

Code:
class HOT_Bot : public IterativeRobot
{
	// Declare robot drive system variable
	RobotDrive myRobot;			// robot will use PWM 1-4 for drive motors
	
	// Declare driver station object variable
	DriverStation *ds;				// driver station object
	
	TrackingThreshold tdata; // image data for tracking
	
	// Declare GamePad variables
	GamePad gamePad1;			// gamepad 1 (drive control)
	GamePad gamePad2;			// gamepad 2 (arm control)

	// Declare drive motor variables
	// PWM 1 and 2 are unused, we'll save them for a four-motor drivetrain
	Victor leftDriveMotor;		// left drive motor will be on PWM 3
	Victor rightDriveMotor;	// right drive motor will be on PWM 4

        ......

HOT_Bot(void)
: myRobot(1,2,3,4),
  gamePad1(1),
  gamePad2(2),
  leftDriveMotor(3),
  rightDriveMotor(4),
  ......
	{
		
		PRINTFDEBUG(("HotBot Constructor Started\n"));
		
		// Acquire the Driver Station object
		ds = DriverStation::GetInstance();
		
                ......
Please note, these changes aren't necessary and the method given here works well enough. I would recommend them though, because this will speed up the loading of the code by a very small amount, and simplify the constructor.

Thanks,
Sam

Last edited by sharrington; 10-24-2008 at 06:21 PM. Reason: Expand, add disclaimer, fix typo
Reply With Quote
  #8  
Old 10-24-2008, 06:05 PM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Quote:
Originally Posted by sharrington View Post
Actually, the code to declare a local variable is:

Code:
Victor ArmMotor(7);
Hmmm...While trying to responed to this, I think I figured out why there's the Pointer(*) on the objects.

I can't really explain what happens while you write the code you wrote there sharrington, but you're technacly supposed to Allocate Memory for the ArmMotor object you create. That is done by using the "new" prefix. Accessing the memory is done by using a reference, which is done by using the pointer (*), therefor it's probablly the most right way to write the code like this:

Victor *ArmMotor = new Victor(7);

The best way to know is to ask the Beta teams currently using C++ to check these methods and give us back the response of the compiler (errors, built successfuly, robot crazyness or something).

Last edited by Bomberofdoom; 10-24-2008 at 07:29 PM. Reason: Basic C++ fundamentals....Moderate programmer....My memory does not serve me well...-_-'
Reply With Quote
  #9  
Old 10-24-2008, 07:06 PM
sharrington
Guest
 
Posts: n/a
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Bomberofdoom View Post
Hmmm...While trying to responed to this, I think I figured out why there's the Pointer(*) on the objects.

I can't really explain what happens while you write the code you wrote there sharrington, but you're technacly supposed to Allocate Memory for the ArmMotor object you create. That is done by using the "new" prefix. Accessing the memory is doing using a reference, which is done by using the pointer (*), therefor it's probablly the most right way to right the code like this:

Victor *ArmMotor = new Victor(7);

The best way to know is to ask the Beta teams currently using C++ to check these methods and give us back the response of the compiler (errors, built successfuly, robot crazyness or something).

P.S
You're right sharrington. to declare a local VARIABLE, you do such:

int variable_int;

With objects it's a bit diffrent.
Correct, except you can have object variables as well, they take parameters after the variable name to be passed to the constructor:

Code:
ClassName varName (param1,param2,...);
In addition, if the variable is in a class, you pass the parameters to the constructor using the containing class's constructor's initialization list:

Code:
class ClassName {
 public:
  AnotherClassName varName;  /* the member variable of type AnotherClassName */

  ClassName()  /* the ClassName constructor */
    : varName(param1,param2,...)  /* call the member variable's constructor */
  {
    /* stuff */
  }
}
Quote:
P.P.S
Your code is wrong anyway. What your code said is: I (the compiler) am creating an instance of class Victor using function ArmMotor() which receives the parameter 7.
Actually, a class's constructor has the special syntax with the colon specifically to allow using constructors in this way. This will, when the class's constructor is called, immediately call the constructors of the member variables listed this way.

Quote:
Don't worry. We're all learning the C++ part and I'm sure that by the beginning of December everything will be clear enough.

For more information on C++ and classes see http://www.cplusplus.com/doc/tutorial/ and cprogramming.com/tutorials/.

Thanks,
Sam

Last edited by sharrington; 10-24-2008 at 07:09 PM. Reason: Add comments to code example
Reply With Quote
  #10  
Old 10-24-2008, 07:31 PM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Sigh...I got used to using the "new" prefix that I totally forgot that this initialization is possible....

Thanks for the refreshner, Sam. Good thing you're around to fix my mistakes.

Oh boy, I can expect this to be an exciting year for everyone, messing up the code in the wrong way....
__________________
Team 2230 Programming sub-team Leader. Non-Beta Tester.
C/C++ moderate programmer. Labview Programmer. FIRST ADDICT.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.