Invert MK4i motor direction in code

Because of the bumper restrictions this year, my team has opted to do a “below-bumper” intake. What we’ve done for extra height is moved the motors from the bottom of the modules to the top like so:

What would I have to change in our swerve module configuration code to facilitate this new orientation?

Here’s the current configs I’m using:

SwerveModule::SwerveModule(const int driveMotorID,     const int angleMotorID,       const int angleEncoderID, double magnetOffset)
					  : m_driveMotor{driveMotorID}, m_angleMotor{angleMotorID}, m_angleEncoder{angleEncoderID} {

	driveMotorConfig.Slot0.WithKP(Drivetrain::Swerve::PID::Motor::Drive::P);
	driveMotorConfig.Slot0.WithKI(Drivetrain::Swerve::PID::Motor::Drive::I);
	driveMotorConfig.Slot0.WithKD(Drivetrain::Swerve::PID::Motor::Drive::D);
	driveMotorConfig.MotorOutput.WithNeutralMode(ctre::phoenix6::signals::NeutralModeValue::Brake);
	driveMotorConfig.Feedback.WithFeedbackSensorSource(ctre::phoenix6::signals::FeedbackSensorSourceValue::RotorSensor);
	m_driveMotor.GetConfigurator().Apply(driveMotorConfig);

	angleMotorConfig.Slot0.WithKP(Drivetrain::Swerve::PID::Motor::Angle::P);
	angleMotorConfig.Slot0.WithKI(Drivetrain::Swerve::PID::Motor::Angle::I);
	angleMotorConfig.Slot0.WithKD(Drivetrain::Swerve::PID::Motor::Angle::D);
	angleMotorConfig.MotorOutput.WithNeutralMode(ctre::phoenix6::signals::NeutralModeValue::Brake);
	angleMotorConfig.Feedback.WithSensorToMechanismRatio(1);
	angleMotorConfig.Feedback.WithRemoteCANcoder(m_angleEncoder);
	angleMotorConfig.ClosedLoopGeneral.ContinuousWrap = true;
	m_angleMotor.GetConfigurator().Apply(angleMotorConfig);

	angleEncoderConfig.WithMagnetOffset(magnetOffset);
	angleEncoderConfig.WithAbsoluteSensorRange(ctre::phoenix6::signals::AbsoluteSensorRangeValue::Unsigned_0To1);
	m_angleEncoder.GetConfigurator().Apply(angleEncoderConfig);

	m_turningPIDController.EnableContinuousInput(0_deg, 360_deg);
}

I’ve been thinking it might be as simple as adding the line:

driveMotorConfig.MotorOutput.WithInverted(ctre::phoenix6::signals::InvertedValue::CounterClock>wise_Positive);

But I don’t know how that would affect our odometry

Are you using all CTRE products?

Yep! Each module is 2 falcon 500s and a cancoder

and a pigeon2?
If you are using all CTRE products I highly recommend the Tuner X Swerve Generator.

Does using the Tuner X Swerve Generator give you an option to invert the motors if you are flipping them to the top on an MK4i module? Would you be able to flip just one of the motors? i.e. just the steering motor on one module, and just the drive motor on another module?

I think you may have to redo the swerve config at that point, I am not sure.

Does that mean re-running the Swerve Generator application? I’m looking at screen shots and have Tuner X pulled up on my laptop (without access to the robot) and don’t see any options to indicate that a motor is flipped. Is there a chance that this happens during the verification stage? i.e. If we say that the wheels didn’t move in the proper direction? We’ve only ran through the process a couple of times before and the motors weren’t flipped so they moved correctly. I’m not sure what happens if they don’t. Thanks for any ideas and/or suggestions.

Yes, i don’t use Tuner X Swerve Gen normally.

Update:

I ended up getting the code working (finally) after about a week of searching through the program.

What I’ve ended up with is this:

driveMotorConfig.MotorOutput.Inverted = true;
angleMotorConfig.MotorOutput.Inverted = false;

So, the drive motor is inverted and the angle motor isn’t. We’ve been doing drive practice with the new motor setup and it works like a charm.

2 Likes

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