2b. Motors - Amarygdala/FRC-5911 GitHub Wiki
Exercise proper precautions when handling motors!
Overview
Motors cover the motors for driving the robot, potential servo motors, and extra DC motors.
What you need
- Motors connected to the right ports
- Motor controllers for each motor
Code
Controlling the motors is an easy process after you connect them properly. You don't directly control the motor, rather you use a motor controller.
Drive
First, you declare the drive
private DifferentialDrive drive;
then the driving motors (controllers), in their respective ports
SpeedControllerGroup leftDriveMotors = new SpeedControllerGroup(new PWMVictorSPX(0), new PWMVictorSPX(1));
SpeedControllerGroup rightDriveMotors = new SpeedControllerGroup(new PWMVictorSPX(2), new PWMVictorSPX(3));
The SpeedControllerGroup is used to bundle up the left and right sides of the drive motors since there are two on each side. It's basically the same as any Victor.
finally, under robotInit() you assign the motors to the drive
drive = new DifferentialDrive(leftDriveMotors,rightDriveMotors);
After that, assuming you have the controller set up,
drive.tankDrive(stick1.getY(Hand.kRight), stick1.getY(Hand.kLeft));
is used in teleopPeriodic.
where the drive is using the tankDrive method. It takes in two values, both being the Y-axis movement of 'stick1' the controller. Each of the "getY" methods takes in one parameter which is the L R stick of the controller.
In plain terms, this drive is like a tank, L stick on the controller moves the left side of the robot, R stick on the controller moves the right side of the robot.
side note for driving
you may see that there are a bunch of stuff with the gyro in the Bravo1 code where the drive code is like this
drive.tankDrive(stick1.getY(Hand.kLeft)+ 1.0 * error/30, stick1.getY(Hand.kLeft)- 1.0 * error/30);
this just makes it so that the robot moves straight when A button is held down. You may find that the robot has trouble moving straight if you already drove it.
Servo
The servos are even easier than driving.
first, you declare them.
private Servo s;
private Servo s2;
initialize
s = new Servo(0);
s2 = new Servo(1);
then control them with methods in the teleop
if(stick.getAButton()){
s.setAngle(180);
s2.setAngle(180);
}else if(stick.getBButton()){
s.close();
s2.close();
}else{
s.set(0);
s2.set(0);
}
side note
The only thing you really need to remember is to close the servos. You need s.close() to be somewhere in the code or they might not run. Check the Drive folder for this code if you need it.
Other dc motors
Just use victor to initialize them
Victor v1 = new Victor(0);
Victor v2 = new Victor(1);
You can set their speed using
v1.set(speed from -1 to 1)