Spark max with neo position control

Hi

I am using a Neo motor to control a joint in an arm and I would like to use position control, but I would like to have joystick control to the position. In other words if I move joystick up, arm goes up and stays at the position when I stop moving the joystick up. What is the best way to do this in java.

Thanks in advance

You can use


CANSparkMax controller = new CANSparkMax(sparkmaxport);
void run(Joystick js){
   spark.set(js.getRawAxis(axis));
}

If it goes to the wrong direction put minus sign in front of js.getRawAxis(axis). Also be careful to not break anything, I mean if the arm must stop somewhere and you force it to move with motor something might break.

You don’t specify so I’m going to assume you’re using the Command-Based Robot.

So basically you want to apply “offsets” or “deltas” to your current/previous setpoint based on your joystick’s value.

I would have a method in my subsystem that handles applying a delta, something like so:

public void adjustSetpoint(double delta) {
    m_setpoint += delta;
    m_pidController.setSetpoint(m_setpoint);
}

Then you need a command that takes the joystick value and applies it as a delta to the subsystem, possibly with some scaling factor to give you the rate of change you want. This could be as straightforward as such:

new RunCommand( () -> 
    m_arm.adjustSetpoint(
        m_controller.getRawAxis(OIConstants.ArmAxis)*ArmConstants.AxisScalingFactor
    ), 
    m_arm
);

When thinking about a scaling factor, remember that the scheduler loop in command based runs every 20ms, so you’d be adjusting the setpoint at a rate of 50hz (50 times a second).

Then you just need to have that command run, either as the default for the subsystem like we do with the Drivetrain, or when you press/hold a specific button or whatever.