View Single Post
  #3   Spotlight this post!  
Unread 12-26-2008, 07:55 PM
koreabell koreabell is offline
Team 956 Safety Captain
FRC #0956 (Eagles)
Team Role: Programmer
 
Join Date: Sep 2008
Rookie Year: 2007
Location: Oregon
Posts: 24
koreabell is an unknown quantity at this point
Re: Getting Familiar with Programming in WindRiver

I'm pretty sure you'll know most of it already, but just in case for someone else who is trying to get used to c/c++ itself, i'll explain from the very beginning

Code:
#include "WPILib.h"

/**
 * This is a demo program showing the use of the RobotBase class.
 * The SimpleRobot class is the base of a robot application that will automatically call your
 * Autonomous and OperatorControl methods at the right time as controlled by the switches on
 * the driver station or the field controls.
 */
class RobotDemo : public SimpleRobot
{
	RobotDrive *myRobot; // robot drive system
	Joystick *stick; // only joystick
	DriverStation *ds; // driver station

public:
	RobotDemo(void)
	{
		ds = DriverStation::GetInstance();
		myRobot = new RobotDrive(1, 2); // create robot drive base
		stick = new Joystick(1); // create the joysticks
		GetWatchdog().SetExpiration(100);
	}

	/**
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous(void)
	{
		GetWatchdog().SetEnabled(false);
		myRobot->Drive(0.5, 0.0); 	// drive forwards half speed
		Wait(2000); 				//    for 2 seconds
		myRobot->Drive(0.0, 0.0); 	// stop robot
	}

	/**
	 * Runs the motors with arcade steering. 
	 */
	void OperatorControl(void)
	{
		GetWatchdog().SetEnabled(true);
		while (IsOperatorControl())
		{
			GetWatchdog().Feed();
			myRobot->ArcadeDrive(stick); // drive with arcade style (use right stick)
		}
	}
};

START_ROBOT_CLASS(RobotDemo);
#include "WPILib.h" <--- You're including header file called "WPILib.h" to your source to use various variables, functions, etc

class RobotDemo : public SimpleRobot <---You're declaring(and defining below) the class called "RobotDemo" which is derived from class called "SimpleRobot", and you'll access to public members of "SimpleRobot" class.
{ <--- you're now defining the class
RobotDrive *myRobot; <---you're declaring RobotDrive type pointer variable called "myRobot"
Joystick *stick; <--- you're declaring Joystick type pointer variable called "stick"
DriverStation *ds; <--- you're declaring DriverStation type pointer variable called "ds"

public: <---you're adding public members
RobotDemo(void) <---you're declaring and defining constructor
{ <--- you're now defining the constructor
ds = DriverStation::GetInstance(); <--- variable "ds" equals whatever value "GetInstance()" function under class called "DriverStation" returns
myRobot = new RobotDrive(1, 2); <--- you'll assign memory for pointer variable called "myRobot", and the size of that memory will be "RobotDrive(1,2)"
stick = new Joystick(1); <--- you'll assign memory for pointer variable called "stick", and the size of that memory will be "Joystick(1)"
GetWatchdog().SetExpiration(100); <--- you'll set the watchdog timer expiration to 100ms
} <--- you're done defining the constructor

void Autonomous(void) <--- you're declaring void type function called "Autonomous" taking no parameters
{ <--- you'll now define the function
GetWatchdog().SetEnabled(false); <--- you'll disable the watchdog timer
myRobot->Drive(0.5, 0.0); <--- execute "Drive(0.5,0.0)" using arrow operator(->) to access its(myRobot pointer's) member function
Wait(2000); <--- execute function called "Wait", taking one parameter, which value you put is 2000
myRobot->Drive(0.0, 0.0); <---- execute "Drive(0.0,0.0)" using arrow operator(->) to access its(myRobot pointer's) member function
} <--- you're done defining function


void OperatorControl(void) <--- you're declaring void type function called "OperatorControl" taking no parameters
{ <--- you're defining the function
GetWatchdog().SetEnabled(true); <--- you're setting watchdog timer
while (IsOperatorControl()) <---repeat while loop until "IsOperatorControl()" equals 0
{ <--- define while loop
GetWatchdog().Feed(); <--- feed the watchdog timer
myRobot->ArcadeDrive(stick); <---execute "ArcadeDrive" function(and parameter it takes is stick variable) using arrow operator(->) to access its(myRobot pointer's) member function
} <--- you're done defining while loop
} <--- you're done defining function

}; <--- you're done defining class

START_ROBOT_CLASS(RobotDemo); <--- you're calling function called "START_ROBOT_CLASS" with parameter "RobotDemo"
Reply With Quote