Skip to content

Instantly share code, notes, and snippets.

@nilayp
Last active October 17, 2024 19:26
Show Gist options
  • Save nilayp/fb4675ef17ffb754f7096f4c5b897989 to your computer and use it in GitHub Desktop.
Save nilayp/fb4675ef17ffb754f7096f4c5b897989 to your computer and use it in GitHub Desktop.
/*
Copyright 2021 FIRST Tech Challenge Team 0000
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.hardware.ColorSensor;
import com.qualcomm.robotcore.hardware.DistanceSensor;
/**
* This file contains an minimal example of a Linear "OpMode". An OpMode is a 'program' that runs in either
* the autonomous or the teleop period of an FTC match. The names of OpModes appear on the menu
* of the FTC Driver Station. When an selection is made from the menu, the corresponding OpMode
* class is instantiated on the Robot Controller and executed.
*
* This particular OpMode just executes a basic Tank Drive Teleop for a PushBot
* It includes all the skeletal structure that all linear OpModes contain.
*
* Remove a @Disabled the on the next line or two (if present) to add this opmode to the Driver Station OpMode list,
* or add a @Disabled annotation to prevent this OpMode from being added to the Driver Station
*/
public class simpleAutonomous extends LinearOpMode {
DcMotor backLeftDrive;
DcMotor backRightDrive;
ColorSensor frontColorSensor;
DistanceSensor frontDistanceSensor;
@Override
public void runOpMode() {
backLeftDrive = hardwareMap.get(DcMotor.class, "backLeft");
backRightDrive = hardwareMap.get(DcMotor.class, "backRight");
frontColorSensor = hardwareMap.get(ColorSensor.class, "frontColorSensor");
frontDistanceSensor = hardwareMap.get(DistanceSensor.class, "frontDistanceSensor");
backRightDrive.setDirection(DcMotor.Direction.REVERSE);
telemetry.addData("Status", "Initialized");
telemetry.update();
// Wait for the game to start (driver presses PLAY)
waitForStart();
backLeftDrive.setPower(1);
backRightDrive.setPower(1);
tankDrive(backLeftDrive, backRightDrive, 1, 1);
showTelemetry(backLeftDrive, backRightDrive, frontColorSensor, frontDistanceSensor);
sleep(500);
tankDrive(backLeftDrive, backRightDrive, 0, 0);
showTelemetry(backLeftDrive, backRightDrive, frontColorSensor, frontDistanceSensor);
tankDrive(backLeftDrive, backRightDrive, 1, -1);
showTelemetry(backLeftDrive, backRightDrive, frontColorSensor, frontDistanceSensor);
sleep(1600);
tankDrive(backLeftDrive, backRightDrive, 0, 0);
showTelemetry(backLeftDrive, backRightDrive, frontColorSensor, frontDistanceSensor);
// run until the end of the match (driver presses STOP)
while (opModeIsActive()) {
telemetry.addData("Status", "Running");
telemetry.update();
tankDrive(backLeftDrive, backRightDrive, 1, 1);
showTelemetry(backLeftDrive, backRightDrive, frontColorSensor, frontDistanceSensor);
}
}
public void showTelemetry(DcMotor left, DcMotor right, ColorSensor front, DistanceSensor distance) {
telemetry.addData("Left Power", left.getPower());
telemetry.addData("Right Power", right.getPower());
telemetry.addData("Front Red Value", front.red());
telemetry.addData("Front Green Value", front.green());
telemetry.addData("Front Blue Value", front.blue());
telemetry.addData("Distance Sensor", distance.getDistance(DistanceUnit.CM));
telemetry.update();
}
public void tankDrive(DcMotor left, DcMotor right, double leftPower, double rightPower) {
left.setPower(leftPower);
right.setPower(rightPower);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment