Spark MAX & Spark FLEX - Decatur-Robotics/Common-Library-And-Wiki GitHub Wiki

Spark MAX & Spark FLEX (TeamSparkBase)

Click here for full SparkMAX/SparkFlex documentation.

This documentation is for motors created by REV Robotics, including, but not limited to:

  • The NEO 550 Brushless Motor
  • The Core Hex Motor
  • The HD Hex Motor

The code below is used to configure the SparkMax/SparkFlex configurator objects, and it should be kept in mind that SparkMax and SparkFlex are the names of the motor controllers, not the motors themselves.

To initialize a SparkMax, put

private SparkMax exampleMotor;

at the start of the function, which creates the motor object, then set it in the constructor using

exampleMotor = new SparkMax(Ports.EXAMPLE_MOTOR, MotorType.kBrushless);

(replacing example motor with the motor you're using and setting motor type based on whether we're using brushless motors or not)

Now that you've set up the motor, you can configure it!

Types of Common Configurators:

SparkClosedLoopController

Initialized in the start of the function via

private SparkClosedLoopController exampleController; 

and then set in the constructor using the code:

exampleController = exampleMotor.getClosedLoopController();

Uses:

  • Setting the motor velocity like
this.desiredVelocity = velocity;
        exampleController.setReference(value, SparkBase.ControlType.kControlType);

The Control Types are as follows:

  • kCurrent
  • kDutyCycle
  • kMAXMotionPositionControl
  • kMAXMotionVelocityControl
  • kPosition
  • kVelocity
  • kVoltage

The Two MAXMotion control types are for closed loop control, which is for when we feed data back into our program to adapt to situations, and the value parameter is a value that sets a reference the motor tries to go to based on control type. For basic duty cycle control this should be a value between -1 and 1. Otherwise:

  • Voltage Control: Voltage (volts)
  • Velocity Control: Velocity (RPM)
  • Position Control: Position (Rotations)
  • Current Control: Current (Amps)
  • other uses and information about detailed here

SparkMaxConfig

Initialized in the constructor via

SparkMaxConfig exampleConfig = new SparkMaxConfig();

Important:

If you are setting a follower for a motor, two separate configs are required, with the follower having the .follow command listed below

Uses:

  • Setting a motor to follow another motor
exampleFollowerConfig.follow(exampleMotorLeader.getDeviceId());
  • Setting what the motor does when you stop providing input to the motor (either brakes or coasts on momentum)
exampleConfig.idleMode(IdleMode.kBrake); or exampleConfig.idleMode(IdleMode.kCoast);
  • Setting a smart current limit, which is how much, in amps, that can pass through the motor controller at a time.
exampleConfig.smartCurrentLimit(exampleConstants.EXAMPLE_CURRENT_LIMIT);
  • Setting the P, I, D, and FeedForward values for the motor (PID is detailed more here)
 exampleConfig.closedLoop.pidf(exampleConstants.EXAMPLE_KP, exampleConstants.EXAMPLE_KI, exampleConstants.EXAMPLE_KD, exampleConstants.EXAMPLE_KFF);

Once you've set the configs using your SparkMaxConfig object, you'll need to apply the configurations to your SparkMax motor object by using the code:

exampleMotor.configure(exampleConfig, SparkBase.ResetMode.kResetSafeParameters, SparkBase.PersistMode.kPersistParameters);

Breakdown of above code:

  • exampleConfig: the SparkMaxConfig object you configured
  • SparkBase.ResetMode: Whether you reset the parameters on configure, following with .kResetSafeParameters as shown above will reset the parameters, while following with .kNoResetSafeParameters will not.
  • SparkBase.PersistMode: whether you want your parameters to persist in the case of a power cycle, which is a reboot. Following the method with .kPersistParameters will save the parameters for the next power cycle, and .kNoPersistParameters makes the current configuration not persist through a power cycle and should only be used when setting a temporary config.