Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Creating SparkObjects
  • void set(double speed)
  • Inverting Spark Motors
  • Challenge: The moveRobot() Method
  1. Romi Curriculum- Timed Base
  2. Cool stuff i will rename this category later

Spark Motors

PreviousCool stuff i will rename this category laterNextPIDs

Last updated 2 months ago

Spark Motors are just the type of motors the Romi uses. After this, we'll refer to them as Sparks. Here are them on the Romi itself:

Creating SparkObjects

This is the constructor of a Spark:

Spark s = new Spark(int motorID)

The motor ID of a Spark is usually written/printed on the motor itself. Ask someone (probably an engineer) to know how to get it.

Each motor has its specific ID to differentiate between each other. Usually, their IDs are written on them (if they aren't, ask an experienced programmer for help). No two motors should have the same ID, or your motors won't work properly.

In the Romi template, both motors were already created, and their IDs were filled out. Here is some code for creating said motors:

// The Romi has the left and right motors set to
// PWM channels 0 and 1 respectively
private final Spark m_leftMotor = new Spark(0);
private final Spark m_rightMotor = new Spark(1);

Now that you can access the motors through your code (yay!), you need to make them move. It's a lot easier than you think.


void set(double speed)

As you can probably tell, set() is a method that sets the motor's speed to whatever value speed is. Speed can only be between -1.0 and 1.0. Anything more or less would probably not work.

/* sets the left motor's speed to 50%. */
m_leftMotor.set(0.5);

After you set a motor to a speed, the motor will keep that speed unless you explicitly stop it/the code.


Inverting Spark Motors

One other thing you should know besides setting motors is inverting motors. Inverting motors allows you to make 2 opposite-facing motors move in the same direction. One example of where you would need this is on the Romi because the 2 motors are facing opposite directions, so if we make both the motors move at 100% speed, the Romi won't go straight, but instead spin because one motor is pushing the opposite way as the other motor.

Inverting a motor is also a method in the motor object like the .set(); method. Here is an example with the m_rightMotor and the m_leftMotor object.

//The right motor is now inverted
m_rightMotor.setInverted(true);
//The left motor is now not inverted
m_leftMotor.setInverted(false);

Luckily, the RomiDrivetrain already inverts the m_rightMotor for us, so we don't have to worry about it. However, if we were to create a DriveTrain from scratch we'd need to invert the motor itself.


Challenge: The moveRobot() Method

Now that you learned about how to set motors, at the bottom of the RomiDrivetrain class, make a helper method called go.

Conditions:

For the last point, although you can do a bunch of if statements, there's a way that has significantly less lines of code.

WPILIB's MathUtil class has a clamp method, clamp(number, lowestBound, highestBound).

Whatever's in the number parameter will never be smaller than the lowestBound and greater than the highestBound.

For example, if I were to do int num = MathUtil.clamp(3, 4, 6), num will be equal to 4 as 3 is less than the lowestBound.

Make sure to import it! (put this in your class header: import edu.wpi.first.math.MathUtil)

Once you have what you believe to be your answer, compare your code with the answer code below.

Answer Code
/* make sure that you import MathUtil!!!
*/
import edu.wpi.first.math.MathUtil;

// Moves the Romi robot.
public void moveRobot(double lSpeed, double rSpeed){
  double checkedLSpeed = MathUtil.clamp(lSpeed, -1, 1);
  double checkedLSpeed = MathUtil.clamp(rSpeed, -1, 1);
  
  m_leftMotor.set(checkedLSpeed);
  m_rightMotor.set(checkedRSpeed);
}
Alternate Answer Code
// Moves the Romi robot.
public void moveRobot(double lSpeed, double rSpeed){
  double checkedLSpeed = lSpeed;
  double checkedRSpeed = rSpeed;
  
  /* look at how cleaner the other one is!!!!!! */
  if (lSpeed > 1) {
    lSpeed = 1;
  
  } else if (lSpeed < -1) {
    lSpeed = -1;
  }
  
  if (rSpeed > 1) {
    rSpeed = 1;
  
  } else if (rSpeed < -1) {
    rSpeed = -1;
    
  }
  
  
  m_leftMotor.set(checkedLSpeed);
  m_rightMotor.set(checkedRSpeed);
}

🕑