Created
September 13, 2015 01:35
-
-
Save aj-foster/cd4d8c06057c9f230c46 to your computer and use it in GitHub Desktop.
Sample TankDrive code for Robot in 1 Weekend 2015
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Robot in 1 Weekend | |
* September 12-13, 2015 | |
* | |
* The following is a sample of code used during the Robot in 1 Weekend event (see the Ri3d channel | |
* on YouTube: https://www.youtube.com/channel/UCgtwgUTTxuldvAGW1u8SDSA). It was written by | |
* Christina Grajales (formerly Univ. of Auburn and team 1902) and AJ Foster (UCF and formerly | |
* team 2425). | |
* | |
* Use of this code requires the Android Studio software and various packages. Please see the | |
* documentation in the FTC App repository (https://github.com/ftctechnh/ftc_app). The following | |
* Android Studio packages are needed via the SDK Manager: | |
* | |
* Tools: | |
* - Android SDK Build-tools | |
* - 23.0.1 (preinstalled - can we uninstall?) | |
* - 21.1.2 | |
* - 19.1 | |
* | |
* Android 5.0.1 (API 21) | |
* - SDK Platform | |
* - Google APIs | |
* | |
* Android 4.4.2 (API 19) | |
* - SDK Platform | |
* - Google APIs (x86 System Image) | |
* - Google APIs (ARM System Image) | |
* | |
* This file should be located in the src/main/java/com.../opmodes directory of the | |
* FtcRobotController project (see https://github.com/ftctechnh/ftc_app). In order to use this | |
* OpMode, you'll have to declare it in the FtcOpModeRegister class. | |
* | |
* We give you NO WARRANTY about the utility or safety of this code. It is up to you to learn and | |
* understand what it does before using it on an operational robot. | |
*/ | |
// Each OpMode must be included in this package. | |
package com.qualcomm.ftcrobotcontroller.opmodes; | |
// Include packages as necessary for predefined classes. | |
import com.qualcomm.robotcore.eventloop.opmode.OpMode; | |
import com.qualcomm.robotcore.hardware.DcMotor; | |
import com.qualcomm.robotcore.hardware.DcMotorController; | |
/* This public class represents our OpMode. Inside of it you will find a number of methods - some | |
* are required, some are not. For more information about the structure of this class, see the | |
* Javadocs in the ftc_app repo. | |
*/ | |
public class RI1WTankDrive extends OpMode { | |
// Define the left and right drive train motors. We will instantiate (set) these variables | |
// below, in the init() method. | |
DcMotor ri1w_drive_left; | |
DcMotor ri1w_drive_right; | |
// These variables will be used to set the drive train motor speed. | |
double left_drive = 0.0; | |
double right_drive = 0.0; | |
// This variable will be used to decide whether to scale down the drive train speed. | |
boolean scale_down = false; | |
/* init() runs as soon as you arm the OpMode using the FTC Driver Station app. Use it to set | |
* servos, calibrate sensors, or otherwise initialize your hardware. | |
* | |
* This method is required - if you do not have it, the OpMode cannot be run. | |
*/ | |
@Override | |
public void init () { | |
// Initialize the motors. The motor names you see here are defined using the FTC Robot | |
// Controller app, when you create a robot configuration. | |
ri1w_drive_left = hardwareMap.dcMotor.get("motor_1"); | |
ri1w_drive_right = hardwareMap.dcMotor.get("motor_2"); | |
// Set the right drive train to be reversed. | |
ri1w_drive_right.setDirection(DcMotor.Direction.REVERSE); | |
} | |
/* start() runs as soon as you press the play button in the FTC Driver Station app. | |
* | |
* This method is not required - if you have nothing to do at the start, you can leave it out. | |
* Think of it like the first loop of your program. | |
*/ | |
@Override | |
public void start () { | |
// We don't have anything to do here. | |
} | |
/* loop() is called repeatedly during the OpMode's execution. | |
* | |
* This method runs about 180 times per second. The majority of your code will probably be | |
* here. This method is required - if you do not have it, the OpMode cannot be run. | |
*/ | |
@Override | |
public void loop () { | |
// Get the current joystick values from the controller. The gamepad{1,2} variables are | |
// predefined for you. See the ftc_app Javadocs for more information about the methods | |
// you can call on the gamepad. | |
left_drive = gamepad1.left_stick_y; | |
right_drive = gamepad1.right_stick_y; | |
// Also get some information about the buttons on the gamepad. We'll use these to set the | |
// boolean variable scale_down. | |
if (gamepad1.x) | |
scale_down = true; | |
if (gamepad1.y) | |
scale_down = false; | |
// If we wish to scale down the motor speed, do that now. | |
if (scale_down) { | |
left_drive /= 1.5; | |
right_drive /= 1.5; | |
} | |
// Set the drive train motor speed for this iteration. | |
ri1w_drive_left.setPower(left_drive); | |
ri1w_drive_right.setPower(right_drive); | |
} | |
/* stop() is called just before the robot is stopped by the FTC Driver Station app. | |
* | |
* This method is not required, but you may wish to make sure your motors are stopped. | |
*/ | |
@Override | |
public void stop () { | |
// Stop the motors! | |
ri1w_drive_left.setPower(0.0); | |
ri1w_drive_right.setPower(0.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment