Joystick Buttons (or general C++ guidance)

I need guidance or an example of how to use joystick buttons to control whether a motor is on or off (the motor is being controlled by a Jaguar). I’m operating using the SimpleRobot Template. Also, although it feels strange to ask this, how do I open the GearsBot Sample code in Windriver?

Note: I’m a complete newbie. I’ve been trying to watch videos and understand WindRiver and the general C++ programming environment but my understanding completely halts at how to access joystick buttons as boolean for if statements. Perhaps I misunderstand C++ at it’s base level, but any help would be wonderful. I am working out of the SimpleTemplate currently, but as for developing past that point I cannot seem to find where to even start on understanding it any better. I’ve watched http://users.wpi.edu/~bamiller/UsingWorkbench/TankDriveSample/TankDriveSample.html as well as the “Intro to Workbench” video. I was hoping to go to
http://thinktank.wpi.edu/article/173 to watch the Programming 101 video, but instead I get an issue where it says the video codec is unable to be played. Considering it’s a .wmv I’m really not sure why it wouldn’t play.

That being said, if there are any videos explaining simple applications of C++ to FRC, that would be great. I apologize in advance for my scattered question, but at this point I’m frantic (I’ve been searching around threads and various forums for help for several days already).

Part of the problem is I can’t deduce exactly -what- my problem is in learning C++, so please ask me questions if it’d help you to help me, or at least show me in the right direction.

Well, I believe that the JoyStick class has a “JoyStickButton”(or something similar) function.

There is a WPIlib documentation file which is very useful, it came with your WPIlib download.
Here is something that should make the robot drive forward for 1 second when “1” is pressed on the controller.


if(JoyStick1.RawButtonValue(1)
Drive(.5,1);
else
Drive(0.0,0.0);

Try putting that in the OperatorControl function
By the way, you would need to edit “JoyStick1” to the name of your mainstick.
If you are using SimpleTemplate and have not changed anything, it should be “stick”

Thanks so much, I found out the function GetRawButton(1) will get me the boolean I need. I really needed the syntax for using it as well, so thanks!

However I still have one more question: How do I use other jaguars not included in the drivetrain? How do I configure/call them and have them run?

Thanks again!

Edit: http://www.chiefdelphi.com/forums/showthread.php?t=99896 <— chanced upon this thread, helped me out a bit. I had the html file but didn’t know about the unblocking thing. I need to try a couple things to see if I understand it correctly, but if anyone has a straight answer for me it’d be appreciated!

To use a jaguar not in the drive train you do something like this:


#include "WPILib.h"
#define AJAG_CHANNEL 1

class myRobot: public SimpleRobot
{
private:
    Jaguar aJag;
    Joystick aStick;
    myRobot():
           aJag(AJAG_CHANNEL),
           aStick(1)
    {
    }
    
    void Teleop(void) 
    {
         if(aStick.GetRawButton(4))
         {
              aJag.Set(1.0);
         }
         else
         {
              aJag.Set(0.0);
          } 
    }
}


All that should be correct and compilable but I didn’t test is.

A few helpful tips:
When you have an object such as a jaguar that you want to interact with, type it’s name and then a dot, then hit control + space on your keyboard. This key combo will show you a list of all things of the methods and attributes a class has (such as Set(float)), this can help you see what methods a class has for interacting with it’s attributes.
(Methods are a fancy way of saying a function that a class has, and are marked with solid green dots next to them in the ctrl space popup)

Some common types of methods start with Get and Set, it’s a pretty common for getters and setters to be provided for any attributes of a class that you should be able to tinker with (such as a motor’s speed)

Ctrl + Space will also finish words for you, so if you name a two variables like this:
Jaguar jagWithNameSoLongItCausesCarpalTunnelSimplyToTypeItsFullName;
Jaguar aJag;

if you type jag (Ctrl+Space) you’ll see both variables popup in a window, if you type a W, the ‘jagShooter’ will go away, and if you hit CTRL+Space it will finish typing the whole rest of the other jagWithName…

Another great way to explore classes is to CTRL+ left click on them, if you CTRL+click aJag, you will go to where is declared, if you CTRL+click Jaguar, you will go where it is declared, which will be “Jaguar.h” which will show you this:

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.							  */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib.  */
/*----------------------------------------------------------------------------*/

#ifndef JAGUAR_H
#define JAGUAR_H

#include "SafePWM.h"
#include "SpeedController.h"
#include "PIDOutput.h"

/**
 * Luminary Micro Jaguar Speed Control
 */
class Jaguar : public SafePWM, public SpeedController, public PIDOutput
{
public:
	explicit Jaguar(UINT32 channel);
	Jaguar(UINT8 moduleNumber, UINT32 channel);
	virtual ~Jaguar();
	virtual void Set(float value, UINT8 syncGroup=0);
	virtual float Get();
	virtual void Disable();

	virtual void PIDWrite(float output);

private:
	void InitJaguar();
};

#endif


so you can see all the methods and attributes of the class.

These two things are great for getting to know what your options are in any objects in an object oriented environment!

Hope you found this helpful :smiley:

Edit: Also, http://www.cplusplus.com/doc/tutorial/ is an excellent resource for learning (and for reference), it’s easier to follow along than a video you gotta pause all the time :slight_smile: (but you do have to read :P)