Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Help with toggle-button (http://www.chiefdelphi.com/forums/showthread.php?t=134178)

PredaFran 07-02-2015 22:30

Help with toggle-button
 
We would like to be able to press a button in the joystick, and that for example if the joystick was controlling motor 1, it would control motor 2.
Thanks in advance.

tomy 08-02-2015 00:00

Re: Help with toggle-button
 
My team does type of action for our dual acting solenoid.

Code:


bool isReady;
bool Piston;

isReady = true;
Piston = false;


if (DriverJoyStick.GetTrigger() && isReady && !Piston){
                Piston = true;
                isReady = false;
} else if (DriverJoyStick.GetTrigger() && isReady && Piston {
                Piston = false;
                isReady = false;
} else if (!DriverJoyStick.GetTrigger()){
                isReady = true;
}

Where when you press the DriverJoystick Trigger and release it, it toggles the Boolean HalfSpeed. The Boolean you can set later in your code. We use it such as.

Code:

if(Piston){
        piston.Set(Relay::kForward);
} else if (!Piston){
        piston.Set(Relay::kReverse);
}


PredaFran 08-02-2015 00:50

Re: Help with toggle-button
 
Could you give me more info about it? or a simpler version using two motors only?
Thanks in advance

tomy 08-02-2015 01:26

Re: Help with toggle-button
 
All you would have to change is this part of the code assuming you keep Booleons named the same:

Code:

if(Piston){
        piston.Set(Relay::kForward);
} else if (!Piston){
        piston.Set(Relay::kReverse);
}

To something like this:

Code:

if(Piston){
        Talon1.Set(Stick.GetY()); // Where as the talon1 would be controlled by the Y axis of the Joystick
} else if (!Piston){
        Talon2.Set(Stick.GetY());// Where as another talon, talon2, would be controlled by the Y axis of the Joystick
}


PredaFran 08-02-2015 16:53

Re: Help with toggle-button
 
i have this code so far, i cant get it to work, any idea of what could be wrong?



Code:

#include "WPILib.h"

class Robot: public SampleRobot
{
        bool Piston;
        Joystick stick; // joystick

        Piston = false;
        Talon m_motor1;
        Talon m_motor2;
public:
        Robot() :
                m_motor1(1),       
                stick(0)
}

{
                myRobot.SetExpiration(0.1);
       
}
        if(Piston){
                m_motor1.Set(Stick.GetY()); // Where as the talon1 would be controlled by the Y axis of the Joystick
        } else if (!Piston){
                m_motor2.Set(Stick.GetY());// Where as another talon, talon2, would be controlled by the Y axis of the Joystick
        }





};

START_ROBOT_CLASS(Robot);


Alan Anderson 08-02-2015 18:17

Re: Help with toggle-button
 
Quote:

Originally Posted by PredaFran (Post 1440330)
i have this code so far, i cant get it to work, any idea of what could be wrong?

You left out the part where you read the joystick button and set the flag you're calling Piston. It's always going to be false.

PredaFran 08-02-2015 18:22

Re: Help with toggle-button
 
could you give me a hand with it? im still new in the field of c++ robotics.
Thanks in advance

Alan Anderson 08-02-2015 18:45

Re: Help with toggle-button
 
tomy's first reply to you gave the toggle code you are looking for.

PredaFran 08-02-2015 19:00

Re: Help with toggle-button
 
thanks for the reply, but i dont quite get what im supposed to do
Thanks in advance

Alan Anderson 08-02-2015 19:51

Re: Help with toggle-button
 
I'm not skilled enough with C++ to help you in this kind of back-and-forth forum posting. It seems likely that your level of understanding is such that you would be best served by someone helping you in person. Do you know any other teams in the Long Beach area?

PredaFran 09-02-2015 01:54

Re: Help with toggle-button
 
Unfortunately, I don't thanks for the help tho
Any thing you think i should read to get more informed in the subject or another place to get help from are appreciated

FleventyFive 09-02-2015 22:02

Re: Help with toggle-button
 
Here is some commented code that does what you want to get you started. If you are the only programmer on the team though, I agree you should try to find a mentor or another student who can help you out if you don't have much experience. Also, don't be afraid to read the errors and warnings eclipse is giving, often they make sense. Good luck! :)

Code:

#include "WPILib.h"

class Robot: public SampleRobot
{ //Define any variables like motors, sensors, or values here
        bool UseSecondMotor;
        Joystick stick;
        Talon m_motor1;
        Talon m_motor2;
public:
        Robot() : //Here is where you say what motors/sensors go into what ports on the RoboRio or the DriveStation computer, for example m_motor1 is in PWM port 1
                stick(0), //Change the 0 to the correct joystick, they are numbered from the order you plug them in
                m_motor1(1), //Change this to the correct PWM port where motor 1 is plugged in (ask electrical team)
                m_motor2(2) //Change this to the correct PWM port where motor 2 is plugged in (ask electrical team)
{
                //You can set the initial value of variables and do things like enable motor safety here
                UseSecondMotor = false;
}
        void Autonomous() //Where you will put the code that runs durring the Autonomous period of the match
        {
        }
        void OperatorControl()
        {
                //Put anything you want to happen once at the start of teleop here

                while (IsOperatorControl() && IsEnabled()) //This runs every 5 milliseconds in teleop, use it to read joystick values and write speeds to motors
                {
                        UseSecondMotor = stick.GetRawButton(1); //Change the 1 to whatever button on the joystick you want to use to toggle (they are labled)
                        if (UseSecondMotor) m_motor2.Set(stick.GetY());
                        else m_motor1.Set(stick.GetY());
                        Wait(0.005); //Don't remove this, this adds a delay so the computer doesn't crash and the motors have time to update
                }
        }
        void Test() //This is what runs when you click on test mode in the driver station, use to test motors, sensors, etc
        {
        }
};
START_ROBOT_CLASS(Robot);



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

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