Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Technical Discussion (http://www.chiefdelphi.com/forums/forumdisplay.php?f=22)
-   -   how to toggle motors in c++ using buttons (http://www.chiefdelphi.com/forums/showthread.php?t=140231)

Bluejackets 12-12-2015 11:33

how to toggle motors in c++ using buttons
 
hey guys! I am new to C++ and I am told to try to get motors to run by pushing button 1 on the joystick... I am having issues doing that... anytime I try doing it it runs like tank drive... I try using an if statement like below.

if(stick.GetRawButton(1)){
talon.Set(1);
}else{
talon.Set(0);
}
if(stick2.GetRawButton(1)){
talon2.Set(1);
}else{
talon2.Set(0);

thanks for your time!
bluejacket robotics!

Anthony Galea 12-12-2015 11:52

Re: how to toggle motors in c++ using buttons
 
Have you tried setting the else (the off mode) to 0.5?

Bluejackets 12-12-2015 12:01

Re: how to toggle motors in c++ using buttons
 
just did... still the exact same results... only moves with the joysticks. i tried changing it to arcade drive and that did not make a difference...

Poseidon5817 12-12-2015 12:34

Re: how to toggle motors in c++ using buttons
 
Quote:

Originally Posted by Bluejackets (Post 1511708)
just did... still the exact same results... only moves with the joysticks. i tried changing it to arcade drive and that did not make a difference...

Are the motors a part of a RobotDrive object somewhere else? If so, the commands that you are sending the RobotDrive are probably overriding your motor commands. Try deleting the line where you use the tankDrive or arcadeDrive method and see if that works.

Bluejackets 12-12-2015 12:42

Re: how to toggle motors in c++ using buttons
 
okay... i'll try that poseidon. i have to leave for a meeting and will get back to you monday wheather it worked

nighterfighter 12-12-2015 13:42

Re: how to toggle motors in c++ using buttons
 
If you can post your entire code (or just the teleoperated part) that will help.

I imagine the problem is what Poseidon said, your Robot object's TankDrive() method is taking priority over your code.

The code that you posted SHOULD be working,(when the trigger is pressed on the joystick, the Talon moves the motor at full speed), assuming everything is initialized properly and the robot class isn't using those Talons for tank drive.

Bluejackets 12-12-2015 19:25

Re: how to toggle motors in c++ using buttons
 
Quote:

Originally Posted by nighterfighter (Post 1511718)
If you can post your entire code (or just the teleoperated part) that will help.

I imagine the problem is what Poseidon said, your Robot object's TankDrive() method is taking priority over your code.

The code that you posted SHOULD be working,(when the trigger is pressed on the joystick, the Talon moves the motor at full speed), assuming everything is initialized properly and the robot class isn't using those Talons for tank drive.

okay here is the whole code


Code:

        /**
        * Runs during test mode
        */
        #include "WPILib.h"


        /**
 * This is a demo program showing the use of the RobotDrive class.
 * The SampleRobot 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.
 *
 * WARNING: While it may look like a good choice to use for your code if you're inexperienced,
 * don't. Unless you know what you are doing, complex code will be much more difficult under
 * this system. Use IterativeRobot or Command-Based instead if you're new.
 */
        class Robot: public SampleRobot
{
        RobotDrive myRobot; // robot drive system
        Joystick stick; // only joy stick
        Joystick stick2;
        TalonSRX talon;
        TalonSRX talon2;


public:
        Robot() :
                        myRobot(0, 1),        // these must be initialized in the same order
                        stick(0),                // as they are declared above.
                        stick2(1),
                        talon(2),
                        talon2(1)
{
                myRobot.SetExpiration(0.1);
        }

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


        }

        /**
        * Runs the motors with arcade steering.
        */
        void OperatorControl()
        {
                myRobot.SetSafetyEnabled(true);
                while (IsOperatorControl() && IsEnabled())
                {
                        myRobot.TankDrive(stick, stick2); // drive with arcade style (use right stick)
                        Wait(0.005);                                // wait for a motor update time
                        talon.Get();
                        talon2.Get();
                        if(stick.GetRawButton(1)){
                                talon.Set(1);
                        }else{
                                talon.Set(0);
                        }
                        if(stick2.GetRawButton(1)){
                                talon2.Set(1);
                        }else{
                                talon2.Set(0);
                        }
                }

        }



        /**
        * Runs during test mode
        */
        void Test()
        {
        }
};

START_ROBOT_CLASS(Robot);


Poseidon5817 12-12-2015 19:38

Re: how to toggle motors in c++ using buttons
 
Quote:

Originally Posted by Bluejackets (Post 1511762)
okay here is the whole code


Code:

        /**
        * Runs during test mode
        */
        #include "WPILib.h"


        /**
 * This is a demo program showing the use of the RobotDrive class.
 * The SampleRobot 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.
 *
 * WARNING: While it may look like a good choice to use for your code if you're inexperienced,
 * don't. Unless you know what you are doing, complex code will be much more difficult under
 * this system. Use IterativeRobot or Command-Based instead if you're new.
 */
        class Robot: public SampleRobot
{
        RobotDrive myRobot; // robot drive system
        Joystick stick; // only joy stick
        Joystick stick2;
        TalonSRX talon;
        TalonSRX talon2;


public:
        Robot() :
                        myRobot(0, 1),        // these must be initialized in the same order
                        stick(0),                // as they are declared above.
                        stick2(1),
                        talon(2),
                        talon2(1)
{
                myRobot.SetExpiration(0.1);
        }

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


        }

        /**
        * Runs the motors with arcade steering.
        */
        void OperatorControl()
        {
                myRobot.SetSafetyEnabled(true);
                while (IsOperatorControl() && IsEnabled())
                {
                        myRobot.TankDrive(stick, stick2); // drive with arcade style (use right stick)
                        Wait(0.005);                                // wait for a motor update time
                        talon.Get();
                        talon2.Get();
                        if(stick.GetRawButton(1)){
                                talon.Set(1);
                        }else{
                                talon.Set(0);
                        }
                        if(stick2.GetRawButton(1)){
                                talon2.Set(1);
                        }else{
                                talon2.Set(0);
                        }
                }

        }



        /**
        * Runs during test mode
        */
        void Test()
        {
        }
};

START_ROBOT_CLASS(Robot);


Yep, there is your issue. Your motor button commands are getting overridden by your myRobot.tankDrive (stick1, stick2) call. If you delete that it should work. You are basically telling the motor to drive, and then immediately telling it to bit drive (or to drive at whatever your stick value is).

Bluejackets 12-12-2015 19:45

Re: how to toggle motors in c++ using buttons
 
Quote:

Originally Posted by Poseidon1671 (Post 1511765)
Yep, there is your issue. Your motor button commands are getting overridden by your myRobot.tankDrive (stick1, stick2) call. If you delete that it should work. You are basically telling the motor to drive, and then immediately telling it to bit drive (or to drive at whatever your stick value is).

okay thanks! ill have to check it out in morning... no bot at home! thank you!

nighterfighter 12-12-2015 19:50

Re: how to toggle motors in c++ using buttons
 
Quote:

Originally Posted by Poseidon1671 (Post 1511765)
Yep, there is your issue. Your motor button commands are getting overridden by your myRobot.tankDrive (stick1, stick2) call. If you delete that it should work. You are basically telling the motor to drive, and then immediately telling it to bit drive (or to drive at whatever your stick value is).

That, and the Wait() command.

The code is currently doing this, line by line (assuming the joysticks are in the neutral position)

Both Talons are being set to 0 speed (from the myRobot.TankDrive() method).
The motors run at this speed for 0.005 seconds.

Your if else blocks come into play, and set the speed of the motor to 1, or full speed.

They do this 0.000 seconds, before the loop restarts, and then get set to 0 again, from your TankDrive.

Your code should look like this:

Code:

while (IsOperatorControl() && IsEnabled())
                {
                       
                        //While buttons are pressed, move the motors.
                        if(stick.GetRawButton(1)){
                                talon.Set(1);
                        }else{
                                talon.Set(0);
                        }
                        if(stick2.GetRawButton(1)){
                                talon2.Set(1);
                        }else{
                                talon2.Set(0);
                        }
                        Wait(0.005); //Wait for a motor update time

                }


You'll notice that I removed your TankDrive method, and also those 2 talon.Get(); lines.

What were those talon.Get() for? talon.Get() returns a float value, based on the previous PWM value that object had. In your code, they weren't doing anything, or being assigned to anything.


If you want to implement both TankDrive() and your button pressing, using the same motors, you'll need to do some additional logic. Let me know if you need help implementing that.

Bluejackets 12-12-2015 19:55

Re: how to toggle motors in c++ using buttons
 
yeah i will need help mplimenting that... im used to labview... the talon.get was there because i left my pc with my team to work with while at a training i could not attend, so they added it to the code...

Bluejackets 13-12-2015 07:24

Re: how to toggle motors in c++ using buttons
 
nothing... absolutely nothing
any ideas?

nighterfighter 13-12-2015 14:22

Re: how to toggle motors in c++ using buttons
 
Here is a quick and dirty way to do it.

What this code will do: While you are pressing a joystick trigger, the respective motor will spin at full speed. If you are not pressing the trigger, it will drive like normal tank drive.

The first thing you'll see is that I made 2 float variables, starting at 0.0. These will be passed into the TankDrive function.

The next thing is the "if" statement. If you are pressing the trigger on stick OR stick2, you then check to see which stick is being pressed, and assign 1.0 to the respective float values.

If you are NOT pressing the joysticks trigger, then the float values will be determined by the Y axis of the joystick.

Finally, those float values will be passed to the TankDrive method.

Forgive any formatting, I wrote it all in here. Also you might have to make one of the values negative, depending on how your motors are set up.


Code:

while (IsOperatorControl() && IsEnabled())
                {
                        float leftValueToUse = 0.0; //2 values that will
                        float rightValueToUse = 0.0; // be passed to TankDrive
                       
                      if(stick.GetRawButton(1) || stick2.GetRawButton(1) )
                      {
                        if(stick.GetRawButton(1))
                      {
                                leftValueToUse = 1.0;
                        }
                        if(stick2.GetRawButton(1))
                      {
                                rightValueToUse = 1.0;
                        }
                     
                      }
                    else
                    {
                    leftValueToUse = stick.GetY();
                    rightValueToUse = stick.GetY();
                   
                    }
                    myRobot.TankDrive(leftValueToUse,rightValueToUse);

                        Wait(0.005); //Wait for a motor update time

                }


Also, this isn't the best way to do it. There is a problem with this way. Think about what will happen if you are driving like normal, and then press and hold one of the joystick triggers. What will happen to the motor you are NOT pressing the respective joystick for? How can you fix that?


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

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