I have been working on getting our mecanum drive working for the past few days and have run into trouble while trying to program using the WPILIB mecanum class. When we try to drive, the forward and strafe commands work fine, but the turn command doesnt. instead of running the left side one direction and the right side another, it spins the wheels on the right towards each other, and the same on the left. If I invert 2 wheels of any pairing, the axis that doesnt work correctly changes. Below is the code that I am trying to use.
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. 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 edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class RobotTemplate extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
Joystick MainStick;
Joystick SecStick;
Gyro gyro;
RobotDrive Drive;
//Drive_Base Drive;
public void robotInit() {
MainStick = new Joystick(1);
SecStick = new Joystick(2);
gyro = new Gyro(1);
Drive = new RobotDrive(10,1,2,3);
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
double theta = gyro.getAngle();
double strafe = 1 * MainStick.getRawAxis(1);
double forward = -1 * MainStick.getRawAxis(2);
double rotation = 1* MainStick.getRawAxis(3);
Drive.setInvertedMotor(RobotDrive.MotorType.kFrontLeft, false);
Drive.setInvertedMotor(RobotDrive.MotorType.kFrontRight, false);
Drive.setInvertedMotor(RobotDrive.MotorType.kRearLeft, false);
Drive.setInvertedMotor(RobotDrive.MotorType.kRearRight, false);
Drive.mecanumDrive_Cartesian(strafe, forward, rotation, theta);
SmartDashboard.putNumber("Gyro Angle", theta);
if(MainStick.getRawButton(8)){
gyro.reset();
}
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
gyro.reset();
}
}
However when I change the equations that calculate the wheel outputs from the ones in the WPILIB:
wheelSpeeds[MotorType.kFrontLeft_val] = xIn + yIn + rotation;
wheelSpeeds[MotorType.kFrontRight_val] = -xIn + yIn - rotation;
wheelSpeeds[MotorType.kRearLeft_val] = -xIn + yIn + rotation;
wheelSpeeds[MotorType.kRearRight_val] = xIn + yIn - rotation;
to the following:
double mFL = strafe + forward + rotation;
double mFR = strafe + forward - rotation;
double mRL = strafe - forward + rotation;
double mRR = strafe - forward - rotation;
It drives normally. My issue with changing the equations is that 1) I am using netbeans (old hardware on our practice bot) and cant change the libraries because they are all pre compiled and 2) We are trying to use field centric control and am not getting the results I’d like by trying to write the code using internet resources such as whitepapers by Ether and such.
My thought is that if I can get the WPILIB mecanum working, I can see how their field centric control operates in comparison.
I am really stumped at this point, any help is greatly appreciated.