LogoLogo
  • Introduction
  • Getting Started with Control Hub
    • Connect to the Robot Controller Console
    • Updating Wi-Fi Settings
    • Connecting Driver Station to Control Hub
    • Wiring Diagram
    • Next Steps
  • Getting Started with Driver Hub
  • Adding More Motors
    • SPARKmini Motor Controller
    • Adding an Expansion Hub
  • Troubleshooting the Control System
    • General Troubleshooting
    • Control Hub Troubleshooting
    • Driver Hub Troubleshooting
      • Driver Hub Battery Troubleshooting
    • Expansion Hub Troubleshooting
    • Status LED Blink Codes
  • System Overview
    • Control Hub Specifications
    • Expansion Hub Specifications
    • Driver Hub Specifications
    • Port Pinouts
    • Protection Features
    • Cables and Connectors
      • XT-30 - Power Cable
      • JST VH - Motor Power
      • JST PH - Sensors and RS485
    • Integrated Sensors
    • Dimensions and Important Component Locations
  • Updating and Managing
    • Managing Wi-Fi on the Control Hub
    • REV Hardware Client
    • Updating Firmware
      • Firmware Changelog
    • Updating Operating System
      • Control Hub Operating System Changelog
    • Updating Robot Controller Application
      • Updating Robot Controller Application via Android Studio
    • Updating the Driver Hub
      • Driver Hub OS - Change Log
    • Accessing Log Files
    • Android Studio - Deploying Code Wirelessly
  • Hello Robot - Intro to Blocks Programming
    • Welcome to Hello Robot!
    • Where to Program - Client vs. Browser
      • What is an OpMode?
    • Setting up a Configuration
      • Common Errors in Configuration
    • Using a Gamepad
    • Part 1: Tackling the Basics
      • Tackling the Basics Directory - Blocks
      • Creating an OpMode - Blocks
      • Programming Essentials
      • Programming Servos
        • Programming Servo Basics
        • Using a Gamepad with a Servo
        • Programming Servo Telemetry
      • Programming Motors
        • Programming Motors Basics
        • Programming a Motor with a Gamepad
        • Programming Motor Telemetry
      • Programming Touch Sensors
      • Programming Color Sensors
        • Color Sensor Telemetry
        • Detecting Color
    • Part 2: Robot Control
      • Robot Control Blocks Directory
      • Programming Drivetrain Motors
      • Arcade Style TeleOp - Blocks
        • Establishing Variables in Blocks
        • Motor Power vs. Robot Movement
        • Programming Arcade Drive
      • Arm Control - Blocks
        • Adding a Limit Switch
      • Robot Control Full Program
    • Part 3: Autonomous and Encoders
      • ElapsedTime - Blocks
        • ElapsedTime Setup
        • ElapsedTime Logic
        • ElapsedTime - Multiple Movements
      • Encoder Basics
      • Drivetrain Encoders - Blocks
        • Converting Encoder Ticks to a Distance
        • Moving to a Target Distance
        • Setting Velocity
        • Turning the Drivetrain Using RUN_TO_POSITION
      • Arm Control with Encoders - Blocks
        • Estimating the Position of the Arm
        • Calculating Target Position
        • Using Limits to Control Range of Motion
    • Part 4: Going Beyond!
      • Exploring Functions
      • Programming Mecanum - Simplified
      • Programming Mecanum - Refined
  • Hello Robot - Intro to OnBot Java Programming
    • Welcome to Hello Robot!
    • Where to Program - Client vs. Browser
      • What is an OpMode?
    • Setting up a Configuration
      • Common Errors in Configuration
    • Using a Gamepad
    • Part 1: Tackling the Basics
      • Tackling the Basics Directory - OnBot
      • Creating an OpMode - OnBot
      • Programming Essentials
      • Programming Servos
        • Programming Servo Basics
        • Using a Gamepad with a Servo
        • Programming Servo Telemetry
      • Programming Motors
        • Programming Motor Basics
        • Programming a Motor with a Gamepad
        • Programming Motor Telemetry
      • Programming Touch Sensors
    • Part 2: Robot Control
      • Robot Control OnBot Java Directory
      • Programming Drivetrain Motors
      • Arcade Style TeleOp - OnBot Java
        • Establishing Variables in OnBot Java
        • Motor Power vs. Robot Movement
        • Programming Arcade Drive
      • Arm Control - OnBot Java
        • Adding a Limit Switch
      • Robot Control Full Program
    • Part 3: Autonomous and Encoders
      • ElapsedTime - OnBot Java
        • ElapsedTime Setup
        • ElapsedTime Logic
        • ElapsedTime - Multiple Movements
      • Encoder Basics
      • Drivetrain Encoders - OnBot Java
        • Converting Encoder Ticks to a Distance
        • Moving to a Target Distance
        • Setting Velocity
        • Turning the Drivetrain Using RUN_TO_POSITION
      • Arm Control with Encoders - OnBot Java
        • Estimating the Position of the Arm
        • Calculating Target Position
        • Using Limits to Control Range of Motion
  • Sensors
    • Introduction to Sensors
    • Digital
    • Analog
    • I2C
      • IMU
        • Orientating the IMU
      • Adding an External IMU to your Hub
    • Encoders
      • REV Motor Encoders
      • Through Bore Encoder
    • Using 3rd Party Sensors
      • Sensor Compatibility Chart
  • Useful Links
    • REV DUO Build System
  • Legacy Documentation
    • Configuring Your Android Devices
    • Expansion Hub with Android Device Robot Controller
      • Driver Station and Robot Controller Pairing
      • Wiring Diagram
      • Configuration
    • REV Hub Interface Software
Powered by GitBook
On this page
  • Creating X and Y Variables:
  • What is a Variable?
  • When or Why do we use Variables?

Was this helpful?

Export as PDF
  1. Hello Robot - Intro to OnBot Java Programming
  2. Part 2: Robot Control
  3. Arcade Style TeleOp - OnBot Java

Establishing Variables in OnBot Java

Creating X and Y Variables:

You may not expect it, but there is a little bit of math that needs to be done to get our robot moving smoothly. But before we dive too deeply into that let's start with the basics of movement we'll need.

To start, create two variablesxxxand yyy. In OnBot Java to establish a variable with a numerical value we will use the object double. Our variables are established during our initialization process.

public void runOpMode() {
        rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
        leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");

        rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);
        double x;
        double y;
        
        telemetry.addData("Status", "Initialized");
        telemetry.update();
        waitForStart();

Then within our loop assign yyy as y = -gamepad1.right_stick_y; and the xxx as the x = gamepad1.right_stick_x;.

Remember positive/negative values inputted by the gamepad's y-axis are inverse of the positive/negative values of the motor.

while (opModeIsActive()) {
        x = gamepad1.right_stick_x;
        y = -gamepad1.right_stick_y;
        
        rightmotor.setPower(1);
        leftmotor.setPower(1);
        }

Setting x = gamepad1.right_stick_x; and y = -gamepad1.right_stick_y; assigns values from the gamepad joystick to x and y. Depending on the orientation of the joystick, these valuables will receive some value between -1 and 1.

For a quick reference let's take a look at what number each variable would be assigned at their far ends:

Joystick Direction

0

1

0

-1

-1

0

1

0

What is a Variable?

Right now we have x and y assigned values based on our joystick's movement, but what does that mean? Why is that useful?

Maybe you have seen in a math class before something like this:

a+8=15a + 8 = 15a+8=15

In this case, a is our variable that has been assigned some value. For this example, we can determine that value is 7. But what does that mean in programming?

Variables used in programming follow this same principle. We can define a variable within our code to hold a set value or a value that changes, such as we are doing here. Then whenever that variable is referenced the robot will read it as that assigned value!

So using our example above if I had:

a+10=?a + 10 = ?a+10=?

My robot would know my variable of a is equal to 7 and therefore calculate the answer as 17 for me!

When or Why do we use Variables?

Consider for a moment, why should we use a variable when we could just use the number on its own?

We'll be using variables in greater detail in later sections, but even for our drive code you will be able to see the use of variables helps keep our program clean and easier to follow.

By using setting our y variable at the beginning of our code we can inverse it without needing to do so every time we may reference the joystick's y-axis. Within a longer program, having our variables defined at the start would allow us to quickly change a value without having to hunt down or double check that every possible instance in the code has been updated to reflect this change. Instead we are able to change it once and continue testing!

PreviousArcade Style TeleOp - OnBot JavaNextMotor Power vs. Robot Movement

Last updated 11 months ago

Was this helpful?

xxx
yyy