Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   General Forum (http://www.chiefdelphi.com/forums/forumdisplay.php?f=16)
-   -   Joystick Trigger and Button C++ Programming (http://www.chiefdelphi.com/forums/showthread.php?t=104550)

KeyFalcon 12-03-2012 22:20

Joystick Trigger and Button C++ Programming
 
Hello. I am on a Rookie team and I am a Rookie programmer. I have no idea how to program the buttons on the joystick. I just need help programming the buttons for our robot's shooter motor and intake motor. Any help would be appreciated.

Thank you!
Code:

/**
 * 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
{
        Victor (victora);
        Victor (victorb);
        Victor (victorc);
        Victor (victord);
        RobotDrive myRobot; // robot drive system
    Joystick rightstick;
   
 public:
        RobotDemo(void):
                victora (1),
                victorb (2),
                victorc (3),
                victord (4),
                myRobot(victora,victorb,victorc,victord),        // these must be initialized in the same order
                rightstick(2)                // as they are declared above.
           
        {
                myRobot.SetExpiration(0.1);
        }

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

        /**
        * Runs the motors with arcade steering.
        */
        void OperatorControl(void)
        {
                myRobot.SetSafetyEnabled(true);
                while (IsOperatorControl())
                {
                        myRobot.ArcadeDrive(rightstick); // drive with arcade style (use right stick)
                        Wait(0.005);                                // wait for a motor update time
                }
        }
};

START_ROBOT_CLASS(RobotDemo);


Alan Anderson 12-03-2012 22:50

Re: Joystick Trigger and Button C++ Programming
 
Can you be a little more detailed about what you want? What exactly do you want the motors to do when you press a button, and when you release the button? Which buttons do you want to use? Be as specific as you can.

KeyFalcon 12-03-2012 23:06

Re: Joystick Trigger and Button C++ Programming
 
I want the motors to run when I press it, and stop when I release it. I also wanted to use number 6 and 7 buttons.

Gigakaiser 12-03-2012 23:22

Re: Joystick Trigger and Button C++ Programming
 
In your operator control loop, you can write something like this:
Code:

if ((rightstick->GetRawButton(6) == 1) && (rightstick->GetRawButton(7) == 0)) //if button 6 is pressed and button 7 is released
{
    //In this check you will either "set" your victors
            victora->Set(.7f);
    //Or use a myrobot function
            myrobot.drive(//set parameters);
}
else if((rightstick->GetRawButton(7) == 1) && (rightstick->GetRawButton(6) == 0)) //if button 7 is pressed and button 6 is released
{
      // "set" your victors
            victora->Set(-.7f);
    //Or use a myrobot function
            myrobot.drive(//set parameters);
}
else // if nothing is pressed set all victors to zero or use a myrobot.drive with 0.0f
{
// turn off everything
            victora->Set(0.0f);
    //Or use a myrobot function
            myrobot.drive(//set parameters to 0.0f);

}

This will make sure your victors are being set to zero if both buttons are pressed or released. Test your robot on blocks before trying to drive!

KeyFalcon 12-03-2012 23:26

Re: Joystick Trigger and Button C++ Programming
 
Thank you!
Would this make it so that button 6 controls one motor, and button 7 controls the other one?

Gigakaiser 12-03-2012 23:36

Re: Joystick Trigger and Button C++ Programming
 
Actually, that would reverse your victors per button (for an intake?) - pressing button 6 would set your victor to +.7, whilie pressing button 7 would set your victor to -.7.

If you want each button to turn on a different victor, write this:
Code:

//pressing 6 will turn on intake, releasing 6 will stop intake
if(rightstick->GetRawButton(6) == 1)
{
    Intakevictor->Set(.7f);
}
else if(rightstick->GetRawButton(6) == 0)
{
    Intakevictor->Set(0.0f);
}

//you can use this code again for your shooter wheel if you change the button to seven and the victor to your shooter wheel victor.


Alan Anderson 12-03-2012 23:40

Re: Joystick Trigger and Button C++ Programming
 
Brandon posted example code that uses victora as a placeholder. You'll need to define the other two motors you want to control, initialize them appropriately, and set whichever one you want to run in the proper place in the code.

Are you actually using Victor speed controllers? As a rookie team, I'd expect you to have Jaguars. They have different responses to PWM signals, and you won't get the best results unless the programming matches the hardware.

KeyFalcon 12-03-2012 23:54

Re: Joystick Trigger and Button C++ Programming
 
Yes, we are using victors.
Also, I'm not sure where to paste the code. I understand it should be in the operator control loop, but I'm unsure where exactly.

Gigakaiser 12-03-2012 23:59

Re: Joystick Trigger and Button C++ Programming
 
Paste the code in the "while(IsOperator)" loop. Any code pasted in that while loop will run for the duration of the match.

KeyFalcon 13-03-2012 00:12

Re: Joystick Trigger and Button C++ Programming
 
Like this?
Code:

void OperatorControl(void)
        {
                myRobot.SetSafetyEnabled(true);
                while (IsOperatorControl())
                {
                        myRobot.ArcadeDrive(rightstick); // drive with arcade style (use right stick)
                        Wait(0.005);                                // wait for a motor update time
                        //pressing 6 will turn on intake, releasing 6 will stop intake
                        if(rightstick->GetRawButton(6) == 1)
                        {
                            Intakevictor->Set(.7f);
                        }
                        else if(rightstick->GetRawButton(6) == 0)
                        {
                            Intakevictor->Set(0.0f);
                        }
                }
        }
};

START_ROBOT_CLASS(RobotDemo);

Or Like this
Code:

void OperatorControl(void)
        {
                myRobot.SetSafetyEnabled(true);
                while (IsOperatorControl())
                {
                        myRobot.ArcadeDrive(rightstick); // drive with arcade style (use right stick)
                        Wait(0.005);                                // wait for a motor update time
                }
                //pressing 6 will turn on intake, releasing 6 will stop intake
                                        if(rightstick->GetRawButton(6) == 1)
                                        {
                                            Intakevictor->Set(.7f);
                                        }
                                        else if(rightstick->GetRawButton(6) == 0)
                                        {
                                            Intakevictor->Set(0.0f);
                                        }
        }
};

START_ROBOT_CLASS(RobotDemo);


Gigakaiser 13-03-2012 00:30

Re: Joystick Trigger and Button C++ Programming
 
Your first example is correct. Just check that you are using the correct victors.

- You should move the Wait(0.005); directly below
Code:

else if(rightstick->GetRawButton(6) == 0)
{
    Intakevictor->Set(0.0f);
}



All times are GMT -5. The time now is 16:56.

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