First Year with Java, Help Requested

This is our first year using Java, our team does not have any programming mentors so I have pretty much been hung out to dry. I’ve been banging my head against the wall because I can not get this code to work.

Here is our code: (Some things are commented out due to me trying to debug.

/*----------------------------------------------------------------------------*/
/* 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.Compressor;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Watchdog;



/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the SimpleRobot
 * 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 Team2501Robot extends SimpleRobot {
   RobotDrive drive = new RobotDrive(1, 2, 3, 4);

  /* Jaguar frontLeft = new Jaguar(1);    // Front Left Motor
    Jaguar frontRight = new Jaguar(2);   // Front Right Motor
   Jaguar backLeft = new Jaguar(3);     // Back Left Mottr
   Jaguar backRight = new Jaguar(4);    // Back Right Motor
   Jaguar armLower = new Jaguar(5);     // Motor for Arm
   Jaguar armUpper = new Jaguar(6);     // Upper Motor for Arm
   Compressor c = new Compressor(6,7);
*/


   Joystick leftStick = new Joystick(1);                    // Drive Stick
   Joystick rightStick = new Joystick(2);                   // Arm stick
   double magnitude = leftStick.getMagnitude();             // Magnitude
   double direction = leftStick.getDirectionDegrees();      // Direction
   double rotation = leftStick.getX();                      // Rotation
   double rmagnitude = rightStick.getY();


    public void autonomous() {
        for (int i = 0; i < 14; i++){        // Autonomius Mode
  //          c.start();
            drive.drive(0.5, 0.0);          // Drive forward at half speed
            Timer.delay(2.0);               // Wait two seconds

            
        }
        drive.drive(0.0, 0.0);              // Stop
    }

    public void operatorControl() {
        getWatchdog().setEnabled(true);
        while (isOperatorControl() && isEnabled()){         //loop during telop
    //        c.start();
            drive.mecanumDrive_Polar(leftStick.getDirectionDegrees(),leftStick.getMagnitude(), rightStick.getY());
           if(rmagnitude > 0.0)
            {
                /*armLower.set(rmagnitude);
                armUpper.set(-rmagnitude);*/
            }
            else if (rmagnitude < 0.0)
            {
                /*armLower.set(-rmagnitude);
                armUpper.set(rmagnitude);*/

            }
            
            else if(leftStick.getRawButton(4)){     // For Strafe Left
                    /*frontLeft.set(0.5);
                    backRight.set(0.5);
                    frontRight.set(-0.5);
                    backLeft.set(-0.5); */
                }
            
            else if(leftStick.getRawButton(5)){     // For Strafe Right
      /*              frontLeft.set(-0.5);
                    backRight.set(-0.5);
                    frontRight.set(0.5);
                    backLeft.set(0.5); */
                }
            
           }
            Timer.delay(0.005);
            // c.stop();
        
    }
}


If its your first year with Java. Maybe start by using arcadeDrive, and then once you get that running, upgrade to mecanum

also your polar code is wrong
first param is magnitude, second direction, third rotation
also you could try cartesian

having your jags and drive mapped to the same ports may cause problems later

I agree with lineskier on all points; it would be simpler to start with arcade drive, then upgrade to mecanum later. On the other hand I can imagine your team wants this to work soon, so here’s my interpretation.

First of all, you don’t need the while loop. It doesn’t hurt, but the system already calls the operatorControl method in a loop - getting rid of it cleans things up.

Secondly, I strongly recommend you use Cartesian drive. It’s just much simpler, and I find it’s more intuitive to drive (it’s a lot like an FPS). Even if you don’t plan on directly mapping controls in this way, it’s way easier to conceptualize - x and y directions and rotation (just put 0 for the gyro until you understand it).

The other thing is that for simplicity reasons, you should only use the drive method to control the drive system. None of this manual Jaguar control for the drive system - it could conflict with the drive method. (Don’t worry about the arm control, that should be fine.)

When I write semi-complex drive code, I like to set variables at the beginning as my drive values. So in this example, I’d set three variables as: xMagnitude, yMagnitude and rotation. Then I’d manipulate these values and after that call the drive method.

By setting variables I can start with a controller input, then adjust them as I see fit. It also makes the code way simpler.

One final thing that is probably a fundamental misconception is that you need to set values that you want to change (ie. rmagnitude) in the operatorControl method. Otherwise it sets the value to whatever the joystick was at when you start the robot and doesn’t change. Values that won’t change during the runtime can stay above the method (ie. RobotDrive) (There’s a lot of complexities as to why, but that’s not important now - let me know if you want more info later.)

So, unless I missed something, here is working code for you:

 public void operatorControl() {
           getWatchdog().setEnabled(true);
		
 	   double xMagnitude = 0; // unless you have a spare axis for this.
	   double yMagnitude = leftStick.getY();
	   double rotation = leftStick.getX();	
           double rmagnitude = rightStick.getY();


           if(rmagnitude > 0.0)
            {
                armLower.set(rmagnitude);
                armUpper.set(-rmagnitude);
            }
            else if (rmagnitude < 0.0)
            {
                armLower.set(-rmagnitude);
                armUpper.set(rmagnitude);

            }
            
            else if(leftStick.getRawButton(4)){     // For Strafe Left
                    xMagnitude = -0.5;
		    // If you want, you could set yMagnitude or rotation to zero here.
                }
            
            else if(leftStick.getRawButton(5)){     // For Strafe Right
           	    xMagnitude = 0.5;        
		    // If you want, you could set yMagnitude or rotation to zero here.
                }
            
           }
            Timer.delay(0.005); // I don't know why this is here - you don't really 
			        // need to slow anything down
	    
	    drive.mecanumDrive_Cartesian(xMagnitude, yMagnitude, rotation, 0);
	   
    

I did make a few assumptions and didn’t explain everything, but I hope I communicated most of it to you. Let me know if you have questions.

EDIT: I just realized I didn’t do anything about your compressor. Just start it in the autonomous or even the initializer (if you know what that is) and leave it. I don’t see a reason to turn it off as the power of the robot should just turn it off.

We are also in our first year and need help with programming. Our forward/backward and rotation works but strafing does not work.

 
    drive = new RobotDrive(1, 3, 2, 4); 
    driverStick = new Joystick(1);
    shooterStick = new Joystick(2);
    robotHeadingGyro = new Gyro(1);


double rotation = driverStick.getZ()*-1 / 2;                                                if (driverStick.getRawButton(3)){
rotation += .4;
 }else if(driverStick.getRawButton(4)){
rotation += -.4;
      }
               
drive.mecanumDrive_Cartesian(driverStick.getX(),
rotation,
driverStick.getY()*-1,
0);// we plan on putting a gyro in when the strafe works
               


Are you positive that your wheels are oriented correctly?

Yes because we recently switched from labview and strafing worked then but doesn’t now.

It looks like you have your parameters out of order when you call the drive method. Try this:


drive.mecanumDrive_Cartesian(driverStick.getX(), driverStick.getY()*-1, rotation, 0);

Also when we strafe right the wheels spin like:

^ ^
\ /

/
v v
and left:

\ /
v v

^ ^
/ \

How are your motors numbered?

Ours are always :
1 2
3 4

I believe
1 2
3 4
but i will check them at the meeting tonight.