Wondering if Code will Run

I am very new to programming for an FRC. We currently do not have a controller to test the Code I wrote but I was hoping somebody could tell me if I was on the right track.

Any Feedback is appreciated, Thanks!


// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.drive.MecanumDrive;
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;

public class Robot extends TimedRobot {

  //Me
  private static final int kFrontLeftChannel = 6;
  private static final int kFrontRightChannel = 7;
  private static final int kRearRightChannel = 8;
  private static final int kRearLeftChannel = 9;

  private static final int kJoystickChannel = 0;

  private MecanumDrive m_robotDrive;
  private Joystick m_stick;

  @Override
  public void robotInit() {
    
    PWMVictorSPX frontLeft = new PWMVictorSPX(kFrontLeftChannel);
    PWMVictorSPX rearLeft = new PWMVictorSPX(kRearLeftChannel);
    PWMVictorSPX frontRight = new PWMVictorSPX(kFrontRightChannel);
    PWMVictorSPX rearRight = new PWMVictorSPX(kRearRightChannel);

    m_robotDrive = new MecanumDrive(frontLeft, rearLeft, frontRight, rearRight);

    m_stick = new Joystick(kJoystickChannel);
  }
  /** This function is called periodically during operator control. */
  @Override
  public void teleopPeriodic() {
    //Updates Stick Position
     double xAxis = m_stick.getRawAxis(0);
     double yAxis = m_stick.getRawAxis(1);
     double zAxis = m_stick.getRawAxis(4);
            
     // Converts Axis values to speed the drive is set to
    m_robotDrive.driveCartesian(xAxis, yAxis, zAxis, 0.0);}

 }
1 Like

This is almost unreadable. Please edit and format it as code (triple backticks (`) before and after, or indent every line with 4 spaces).

Or better yet, upload the entire project to github and share a link.

Thank you,I am very new to this.

I will update my formatting ASAP

Besides missing the class header (which I believe you simply forgot to copy), it looks fine. Nice work!

Something which may be helpful: the SIM gui does support using a keyboard to emulate joystick input.

I deleted anything I hadn’t worked on directly to make reading easier. Besides that, that’s great to hear. Thanks for your help!

Thanks!

We should have a controller soon but i will look into this.

If your robot is using a mecanum drivetrain then this seems good to me (although I would confirm with your mechanical team whether this is the case; it is rather unusual for rookie teams, or any FRC team for that matter, to use a mecanum drive).

I will point out two things just as a nitpick:

1.) You seem to use two spaces for indentation, which is fine, although the standard in most projects seems to be four spaces as it’s more readable that way. Establish consensus with your programming team before changing this.

2.) At the end of your teleopPeriodic() method, you have the end curly brace in a weird position:

  public void teleopPeriodic() {
  ...
    m_robotDrive.driveCartesian(xAxis, yAxis, zAxis, 0.0);}
}

The code will run just the same in any case, but you should move the curly brace to a new line:

  public void teleopPeriodic() {
  ...
    m_robotDrive.driveCartesian(xAxis, yAxis, zAxis, 0.0);
  }
}

Aside from these stylistic nitpicks, assuming you are using a mecanum drive this should work fine.

You appear to be using the command template, but not actually using commands. Is that what you intended?

Thank you!

Im not very familiar with the official formatting so I will definitely make those changes.

Yes my team is working with a mecanium drive. This is our 3rd year entering the competition, and they voted on i, so I did my best to program it.

I am unfamiliar with Commands. I began programming in the “recommended” template by the tutorial I was following. Could you explain what Commands are and how to use them? or linking to any resources explaining this?

Any help you can provide would be great,
Thanks!

Hello, if you’re looking for a good coding resource, the official WPILib documentation is great.

For Commands: Link

Front Page: Link

Hope this helps.

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