Issue With Elevator Code all the values are correct but the motors won't spin I get no errors aswell

package frc.robot.subsystems;
‘’’
import edu.wpi.first.wpilibj2.command.Command;
‘’’
import com.revrobotics.CANSparkMax;
‘’’
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.drive.RobotDriveBase.MotorType;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj2.command.CommandBase;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import com.revrobotics.CANSparkMaxLowLevel;
‘’’
public class elevator extends SubsystemBase {
private final CANSparkMax sped1 = new CANSparkMax(6, com.revrobotics.CANSparkMaxLowLevel.MotorType.kBrushless);
private final CANSparkMax sped2 = new CANSparkMax(7, com.revrobotics.CANSparkMaxLowLevel.MotorType.kBrushless);
public MotorControllerGroup sped = new MotorControllerGroup(sped1, sped2);
Joystick subsystems = new Joystick(0);
JoystickButton eup = new JoystickButton(subsystems, 6);
JoystickButton edown = new JoystickButton(subsystems, 4);
‘’’
/** Creates a new ExampleSubsystem. /
//public Trigger eTrigger(){
// Trigger edown = new JoystickButton(subsystems, 4);
//return edown;
‘’’
‘’’
public elevator() {}
public void motoron(double set) {
sped.set(0.67);
}
public void motoroff(double set){
sped.set(0);
}
‘’’
/
*

  • Example command factory method.
  • @return a command
    /
    public CommandBase exampleMethodCommand() {
    return null;
    }
    ‘’’
    /
    *
  • An example method querying a boolean state of the subsystem (for example, a digital sensor).
  • @return value of some boolean subsystem state, such as a digital sensor.
    */
    public boolean exampleCondition() {
    // Query some boolean state, such as a digital sensor.
    return false;
    }
    ‘’’
    @Override
    public void periodic() {
    //edown.whileTrue(motoron(0));
    //edown.whileFalse(motoroff(1));
    if (subsystems.getRawButton(6)){
    sped.set(0.25);
    }
    else if (subsystems.getRawButton(4)){
    sped.set(-.25);
    }
    else {
    sped.set(0);
    }
    }
    ‘’’
    /public void motoron(double set) {
    sped.set(1);
    }
    public void motoroff(double set){
    sped.set(0);
    }
    /
    ‘’’
    @Override
    public void simulationPeriodic() {
    // This method will be called once per scheduler run during simulation
    }
    }

You’re not going to get any useful replies trying to help if you don’t actually explain what the issue is.

Also, please format your code correctly, or (ideally) link to github or some other source control management system of your choosing. To format the code, add triple backticks (```) on blank lines before and after the block of code. You can go one step further to add syntax highlighting by changing the preceding one to ```java

is there anything else I need to do?

What do your motors display? I had a similar issues, and it was due to the limit switched being configured improperly.

I don’t use a lot of REV motors but maybe check your limit switches or soft limits if you have any

Make sure the motors are plugged in properly (white to white, red to red, black to black). Make sure the encoder cable is in and not damaged (they are very fragile (shame on you, REV)). Make sure the motors are on the CAN bus

I agree with @Fletch1373 - please upload your code to GitHub if you are able to. This will allow people to look through all aspects of your code. At the very least, surround your code with three ` marks (above the tab key)

Your code will appear like this when it is properly formatted
1 Like

The motors are configured correctly, we just tested them by using the REV Hardware Client. The limit switches are not being used yet. https://github.com/swootton12/elevatorerror

I see you have nothing in RobotContainer.java and nothing much in Robot.java

RobotContainer is where you would put all of your button bindings for commands, as well as instantiate your subsystem

Is this on purpose?

Yes Im new to programming and like to keep everything in subsystems

No problem. Looks like should read over the WPI library tutorial first, which will get you started in the right direction: Command-Based Programming — FIRST Robotics Competition documentation

You need to remove your joysticks from your subsystem and put them in RobotContainer.java

Then in your elevator subsystem, you need to create functions to turn on/off your elevator motors. Looks like you are close with the public void motoron(double set) function, except you don’t pass the set speed to the motors. Change it to this:

public void motoron(double speed) {
    sped.set(speed);
}

Then, you need to make a command. You can try inline commands, which you seem to have attempted, but I like to separate all my commands into a commands folder. This makes it easy to reuse commands for autonomous modes

Pro Tip: if you are using VS Code, you can right click on a folder and hit “Create new class/command”:
image

The command would look something like this:

package frc.robot.commands;

import edu.wpi.first.wpilibj2.command.InstantCommand;
import frc.robot.subsystems.elevator;

public class SetElevatorSpeed extends InstantCommand {
  private final elevator m_elevator;
  private final double m_speed;

  public SetElevatorSpeed (elevator ele, double speed) {
    m_elevator = ele;
    m_speed = speed;

    addRequirements(m_elevator);
  }

  @Override
  public void initialize() {
    m_elevator.motoron(speed);
  }
}

Lastly, you will need to go in RobotContainer.java, create a joystick class (we use the XboxController class), and then assign a command to a button. Here is how to do that: Binding Commands to Triggers — FIRST Robotics Competition documentation

Also - in RobotContainer.java, you will have to create an elevator class and pass that along to the commands

Try those and see if you can get your elevator moving

Feel free to look at our code to get a good understanding on how to write command-based code:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.