Code For Vex Servos

Hello everybody! For our mechanism to hold the gears, we used a couple of VEX 6193a servos. We’re not even sure if they are compatible with the RoboRIO, as we had to make our own adapters for the male PWMs, but we tried using this code. We cannot test it during this time as the robot is in the bag, so can you guys tell us if this looks right? Thanks!

Servo GearLeft = new Servo(7);
Servo GearRight = new Servo(8);
boolean gearOpen = false;

GearLeft.enableDeadbandElimination(true);
GearRight.enableDeadbandElimination(true);

if (Stick2.getRawButton(4) && gearOpen == false){
int x = 0;
while(x < 90){
GearLeft.setAngle(x);
GearRight.setAngle(x);
x++;
}
gearOpen = true;
}else if (Stick2.getRawButton(4) && gearOpen == true){
int y = 90;
while(y > 0){
GearLeft.setAngle(y);
GearRight.setAngle(y);
y–;
}
gearOpen = false;

You should just be able to set the angle you want the servo to move to (the while loops should not be required). Something along the lines of:


Servo GearLeft = new Servo(7);
Servo GearRight = new Servo(8);
boolean gearOpen = false;

GearLeft.enableDeadbandElimination(true);
GearRight.enableDeadbandElimination(true);

if (Stick2.getRawButton(4) && gearOpen == false) {
  int x = 90;
  GearLeft.setAngle(x);
  GearRight.setAngle(x);
  x++;
  gearOpen = true;
} else if (Stick2.getRawButton(4) && gearOpen == true){
  int y = 0;
  GearLeft.setAngle(y);
  GearRight.setAngle(y);
  y--;
  gearOpen = false;
}

The setAngle() invocation will return right away - before the servo reaches the requested position. The servo should keep moving on its own until it reaches the last specified setAngle() position.

What about the VEX servo? Can we use Java language to make the VEX servo work normally? Just like a normal servo. Cause I have heard that people usually using RobotC to program the VEX servo

Deadband elimination is typically for speed controllers. I don’t think it’s necessary for servos. I know that for Hitec servos, the deadband is on the order of microseconds, so basically negligible.

Find the datasheet of the servo you are using, and look up the PWM input requirements. You should get a minimum width of about 1ms and maximum width of about 2ms. For example, Hitec servos are 0.9ms to 2.1ms. Then use this code when creating the servo objects


servo.setBounds(maxWidthMs, 0, 0, 0, minWidthMs);

I personally have no idea why the 3 zeros in the middle are acceptable, but it’s what the Servo constructor uses and it seems to work. If WPILib could be actually documented, it would be really helpful

So your code would look like this


// fields
Servo gearLeft;
Servo gearRight;
boolean gearOpen = false;
boolean wasButton4Pressed = false;

// robotInit, subsystem constructor, etc
// please use RobotMap
gearLeft = new Servo(RobotMap.GEAR_LEFT_SERVO_CHANNEL);
gearRight = new Servo(RobotMap.GEAR_RIGHT_SERVO_CHANNEL);
gearLeft.setBounds(maxWidthMs, 0, 0, 0, minWidthMs);
gearRight.setBounds(maxWidthMs, 0, 0, 0, minWidthMs);

// teleopPeriodic
// detect rising edge of button 4. Ignore this and use whenPressed if using command based
if (Stick2.getRawButton(4)) {
  if (!wasButton4Pressed) {
    wasButton4Pressed = true;

    // toggle code you want
    gearOpen = !gearOpen;
    gearLeft.setAngle(gearOpen ? 90 : 0);
    gearRight.setAngle(gearOpen ? 90 : 0);
  }
} else {
  wasButton4Pressed = false;
}

Thanks for helping us! :slight_smile: