It’s plugged into the RoboRio in PWM port 2. There’s a global variable:
Servo servo = new Servo(2);
In teleopPeriodic, this code does not work to extend the actuator:
servo.set(1.0);
How do you control a servo with code?
It’s plugged into the RoboRio in PWM port 2. There’s a global variable:
Servo servo = new Servo(2);
In teleopPeriodic, this code does not work to extend the actuator:
servo.set(1.0);
How do you control a servo with code?
PLEASE HELP!
We really need to know this soon!
Have you tried values other than just 1.0? Perhaps you can try .5 and see if you get any result. As well, make sure that you don’t have the cable backwards.
Doing that didn’t work.
Can you post your whole code and a picture of your wiring. That will help us diagnose your problem.
We don’t have any wiring. The servo is plugged directly into RoboRio port 2.
Here’s the code:
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc.robot;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.SPI;
import com.kauailabs.navx.frc.AHRS;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.PWMTalonSRX;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Servo;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.cameraserver.*;
public class Robot extends TimedRobot {
// Link robot to code
DifferentialDrive robotDrive = new DifferentialDrive(new Spark(0), new Spark(1));
// Link controller to code
Joystick controller = new Joystick(0);
JoystickButton LTrigger = new JoystickButton(controller, 2);
JoystickButton RTrigger = new JoystickButton(controller, 3);
JoystickButton A = new JoystickButton(controller, 6);
JoystickButton B = new JoystickButton(controller, 7);
JoystickButton Y = new JoystickButton(controller, 9);
// Link other stuff to code
Servo servo = new Servo(2);
PWMTalonSRX talon = new PWMTalonSRX(3);
AHRS gyro;
// Control variables
double maxSpd = 0.65;
boolean lowerLance = true;
@Override
public void robotInit()
{
// Camera
CameraServer.getInstance().startAutomaticCapture();
// Gyroscope
gyro = new AHRS(SPI.Port.kMXP);
// Talon SRX
talon.enableDeadbandElimination(true);
}
@Override
public void robotPeriodic() {
}
@Override
public void autonomousInit() {
gyro.reset();
}
@Override
public void autonomousPeriodic() {
/*double angle = 180.0;
if (gyro.getAngle() < angle) {
robotDrive.arcadeDrive(0.0, -0.5);
}*/
teleopPeriodic();
}
@Override
public void teleopPeriodic()
{
// Get controller inputs
double y = -controller.getY();
double x = controller.getX();
double gear = controller.getPOV();
// Speed gears for robot
if (gear == 0) {
maxSpd = 0.8;
}
else if (gear == 270 || gear == 90) {
maxSpd = 0.7;
}
else if (gear == 180) {
maxSpd = 0.65;
}
// Limit max speed
if (y > maxSpd) {
y = maxSpd;
}
else if (y < -maxSpd) {
y = -maxSpd;
}
if (x > maxSpd) {
x = maxSpd;
}
else if (x < -maxSpd) {
x = -maxSpd;
}
// Toggle the lance's position
if (Y.get())
{
lowerLance = !lowerLance;
}
// Lower the lance
if (lowerLance && servo.get() != 0.0)
{
servo.set(0.0);
}
else if (!lowerLance && servo.get() != 1.0)
{
servo.set(1.0);
}
// Control lift
if (controller.getRawAxis(5) < -0.05 || controller.getRawAxis(5) > 0.05)
{
talon.set(controller.getRawAxis(5));
}
else
{
talon.set(0.0);
}
// Send robot angle to dashboard
SmartDashboard.putNumber("Angle", Math.round((float)gyro.getAngle()));
// Drive robot
robotDrive.arcadeDrive(y, x);
}
@Override
public void testPeriodic() {
}
}
set() is a different function from setAngle(). It ranges from 0.0 to 1.0 and is supposed to set the position of the servo.
Just for fun, can you post a picture showing the servo PWM cable in the roboRIO. And a picture of the servo itself.
Trust me, it’s in correctly. Do you know what a jumper is or if we need one?
The servo is a actuator, and it extended fully the first time we plugged it in and ran the robot, before we even had the code to control it. Now it simply does nothing.
Try simply calling servo.set(1) (or any number) in teleop, instead of trying to put it in a button control loop, just to see if your button programming is working as expected
I already tried that.
What color is the power LED on your roborio?
The RoboRio is on, as we can drive our robot.
That’s not what he’s asking. If there’s a short on one of the roboRIO power rails, the power light will turn from green to red. And that would make your servo not work.
In the future if you want help, I suggest you answer the questions and provide us the information we’re asking. Otherwise, you’re wasting our time trying to help you.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.