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.
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.
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.
/**
* 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).
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:
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.
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…
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.
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?