Initializing and Using Buttons to Control Victor Motor Controllers

Hello everyone, I have made a thing to use buttons 4 & 5 to control the servo motor to our Axis camera. The servos are connected to the PWM channels and we just need to program the motor. Would the following code work to accomplish our task?

#include "WPILib.h"

class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick stickRight, stickLeft; // only joystick
	Victor (victora);
	Victor (victorb);
	Victor (victorc);
	Victor (victord);

public:
	RobotDemo(void):
		myRobot(victora, victorb),	// these must be initialized in the same order
		stickRight(1),		// as they are declared above.
		stickLeft(2),
		victora (1),
		victorb (2),
		victorc (3),
		victord (4)
	{
		myRobot.SetExpiration(0.1);
	}

	/**
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous(void)
	{
		myRobot.SetSafetyEnabled(false);
		while (IsAutonomous()) //drive commands continuously loop
        {
		myRobot.Drive(-0.5, 0.0); 	// drive forwards half speed
		Wait(2.0); 	                // for 2 seconds
		myRobot.Drive(0.0, -0.5);   // Turn
		Wait(1.0);					// for 1 second
		myRobot.Drive(0.0, -0.5);   // Turn again
		Wait(1.0);                  // for 1 second
		}
	}

	/**
	 * Runs the motors with arcade steering. 
	 */
	void OperatorControl(void)
	{
		myRobot.SetSafetyEnabled(true);
		while (IsOperatorControl())
		{
			myRobot.ArcadeDrive(stickRight); // drive with arcade style (use right stick)
			Wait(0.005);				// wait for a motor update time
			if ((stickRight->GetRawButton(4) == 1))
			{
				Intakevictorc->Set(.7f);
			}
			else if (stickRight->GetRawButton(4) == 0)
			{
				Intakevictorc->Set(0.0f);
			}
			
			if ((stickRight->GetRawButton(5) == 1))
			{
				Intakevictord->Set(-.7f);
			}
			else if (stickRight->GetRawButton(5) == 0)
			{
				Intakevictord->Set(0.0f);
			}
		}
	}
	
	/**
	 * Runs during test mode
	 */
	void Test() {

	}
};

START_ROBOT_CLASS(RobotDemo);


Thanks for your help!

You titled your note with “Victor Motor Controllers”, but you asked about “the servo motor”. I don’t see any servos in your code. What are you really trying to control?

Why do you have parentheses around the names of your Victor objects when you declare them?

You declared “victorc” and “victord”, but you’re trying to set “Intakevictorc” and “Intakevictord”.

If you fix the names to match, your code will run motor C on PWM 3 forward when you press button 4, and motor D on PWM 4 backward when you press button 5. It never runs those motors in the other direction.

Sorry, I meant to ask about the servo motors not the victor motors. If one were trying to initialize and use the servo motors would it be initialized like this (initializing two servos):


class RobotDemo : public SimpleRobot
{
	RobotDrive myRobot; // robot drive system
	Joystick stickRight, stickLeft; // only joystick
        Servo (servoa); //Initialize servo 1 (or a)
        Servo (servob);  //Initialize servo 2 (or b)

and if this is correct, then how would one go about programming these to buttons? Could this code work?


//Example code
if ((stickRight->GetRawButton(4) == 1))
	{
		servoa->Set(.7f);
	}
else if (stickRight->GetRawButton(4) == 0)
	{
		servoa->Set(0.0f);
	}

Thank you for your help.