Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

2019 update #732

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
## General Info for 2018 Code:

```
DRIVETRAIN CONFIGURATION
front
C D
left right
B A
back

Works with the hub:port configuration
```

Competition Autonomous: Cratersample2
* drops and turns left to unhook
* linear OpMode

Competition TeleOp: FourwdTeleop2
* gamepad1 for drivetrain, gamepad2 for the other 4 motors
* uses tuner to adjust drivetrain multiplier and senscurve exponents
* iterative opmode

Tuner:
* use this to adjust values of something while program is running.


look in individual classes for more info.






## ABOUT THE APP
=======
## NOTICE

This repository contains v5.0 of the FTC SDK. No further versions will pushed to https://github.com/ftctechnh/ftc_app.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* Copyright (c) 2017 FIRST. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided that
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;

/**
* This file contains an example of an iterative (Non-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 two wheeled robot
* It includes all the skeletal structure that all iterative OpModes contain.
*
* Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
* Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
*/

@TeleOp(name="Basic: Iterative OpMode", group="Iterative Opmode")
public class BasicOpModeSample_Iterative extends OpMode
{
// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
private DcMotor aDrive = null;
private DcMotor bDrive = null;

/*
* Code to run ONCE when the driver hits INIT
*/
@Override
public void init() {
telemetry.addData("Status", "Initialized");

// Initialize the hardware variables. Note that the strings used here as parameters
// to 'get' must correspond to the names assigned during the robot configuration
// step (using the FTC Robot Controller app on the phone).
aDrive = hardwareMap.get(DcMotor.class, "aDrive");
bDrive = hardwareMap.get(DcMotor.class, "bDrive");

// Most robots need the motor on one side to be reversed to drive forward
// Reverse the motor that runs backwards when connected directly to the battery
aDrive.setDirection(DcMotor.Direction.FORWARD);
bDrive.setDirection(DcMotor.Direction.REVERSE);

// Tell the driver that initialization is complete.
telemetry.addData("Status", "Initialized");
}

/*
* Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
*/
@Override
public void init_loop() {
}

/*
* Code to run ONCE when the driver hits PLAY
*/
@Override
public void start() {
runtime.reset();
}

/*
* Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
*/
@Override
public void loop() {
// Setup a variable for each drive wheel to save power level for telemetry
double leftPower;
double rightPower;

// Choose to drive using either Tank Mode, or POV Mode
// Comment out the method that's not used. The default below is POV.

// POV Mode uses left stick to go forward, and right stick to turn.
// - This uses basic math to combine motions and is easier to drive straight.
double drive = -gamepad1.left_stick_y*0.5;
double turn = gamepad1.right_stick_x*0.2;
leftPower = Range.clip(drive + turn, -1.0, 1.0) ;
rightPower = Range.clip(drive - turn, -1.0, 1.0) ;

// Tank Mode uses one stick to control each wheel.
// - This requires no math, but it is hard to drive forward slowly and keep straight.
// leftPower = -gamepad1.left_stick_y ;
// rightPower = -gamepad1.right_stick_y ;

// Send calculated power to wheels
aDrive.setPower(leftPower);
bDrive.setPower(rightPower);

// Show the elapsed game time and wheel power.
telemetry.addData("Status", "Run Time: " + runtime.toString());
telemetry.addData("Motors", "left (%.2f), right (%.2f)", leftPower, rightPower);
}

/*
* Code to run ONCE after the driver hits STOP
*/
@Override
public void stop() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.firstinspires.ftc.teamcode;

/**
* Manages toggling an action.
*
* Call checkToggleStatus once every loop to determine whether a full button press has
* occurred or not.
*/
final class ButtonPress
{
/**
* Holds all the states that a toggle can be in. When pressing a button, there are 3 states:
* 1. Not begun
* 2. In progress
* 3. Complete
*
* If you're checking a button press using a conventional on/off check and using it to
* flip a boolean, then you'll flip once for every time the button is held and the
* loop iterates.
*/
enum Status
{
NOT_BEGUN ,
IN_PROGRESS ,
COMPLETE
}


private Status _status = Status.NOT_BEGUN; // Current status of the toggle


/**
* Monitors and adjusts the toggle value based on previous toggle values and the
* state of the boolean passed in.
*/
final public Status status(boolean buttonStatus)
{
// If the button is being held
if(buttonStatus && _status == Status.NOT_BEGUN)
_status = Status.IN_PROGRESS;

// If the button is not being pressed and the toggle was in progress
else if(!buttonStatus && _status == Status.IN_PROGRESS)
_status = Status.COMPLETE;

// If the toggle is finished
else if(_status == Status.COMPLETE)
_status = Status.NOT_BEGUN;

return _status;
}
}
106 changes: 106 additions & 0 deletions TeamCode/src/main/java/org/firstinspires/ftc/teamcode/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.firstinspires.ftc.teamcode;


import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;

public class Calculate {

//DRIVE
public static double[] FOD(double x, double y, double gyroAngle, boolean inputSquare, boolean outputSquare) {
//input the components of the vector (in circle or square) in which you want to robot to go and the heading of the robot
//outputs the relative x and y to the global coordinate plane (circle or square)

double localU;
double localV;

//if not a circle coordinate, convert to circle
if (inputSquare) {
double[] coord = squareToCircle(x, y);
localU = coord[0];
localV = coord[1];
} else {
localU = x;
localV = y;
}

//does rotation in circled coordinate plane because rotation is a circle
//uses the rotate point around origin formula
double rotAngle = -Math.toRadians(gyroAngle);
double globalX = 0.0001 * (Math.round((localU * Math.cos(rotAngle) - localV * Math.sin(rotAngle)) * 10000));
double globalY = 0.0001 * (Math.round((localV * Math.cos(rotAngle) + localU * Math.sin(rotAngle)) * 10000));

//output as requested
if (outputSquare) {
return circleToSquare(globalX, globalY);
} else {
return new double[]{globalX, globalY};
}
}


//COORDINATE PLANE

public static double sensCurve(double joystickVal, double exponent){
return Math.copySign( Math.pow( Math.abs( joystickVal), exponent), joystickVal);
}

public static double[] polarToCartesian(double magnitude, double angle, boolean angleInRadians) {
//converts a power and angle to x and y coordinates
//output: x, y
double radians;
if (angleInRadians) {
radians = angle;
} else {
radians = Math.toRadians(angle);
}
return new double[] {magnitude * Math.cos(radians), magnitude * Math.sin(radians)};
}

public static double[] cartesianToPolar(double x, double y) {
//converts x and y coordinates to power and angle
//output: magnitude, direction
double radians = Math.atan2(y,x); //get diretion (but it outputs in radians)
double degrees = Math.toDegrees(radians); //convert to degrees
double magnitude = Math.sqrt(x*x + y*y); //pythagorean theorem

return new double[] {magnitude, degrees};
}

public static double[] circleToSquare(double u, double v) {
//when you want to "stretch" points of a circular coordinate plane onto a square one, ex (0.707, 0.707) maps to (1, 1)
//visit http://squircular.blogspot.com/2015/09/mapping-circle-to-square.html for more info (this is the inverse)

double u2 = u * u;
double v2 = v * v;
double twosqrt2 = 2.0 * Math.sqrt(2.0);
double subtermx = 2.0 + u2 - v2;
double subtermy = 2.0 - u2 + v2;
double termx1 = subtermx + u * twosqrt2;
double termx2 = subtermx - u * twosqrt2;
double termy1 = subtermy + v * twosqrt2;
double termy2 = subtermy - v * twosqrt2;
double x = (0.5 * Math.sqrt(termx1) - 0.5 * Math.sqrt(termx2));
double y = (0.5 * Math.sqrt(termy1) - 0.5 * Math.sqrt(termy2));
return new double[]{x, y};
}

public static double[] squareToCircle(double x, double y) {
//when you want to "squeeze" points of a square coordinate plane onto a circle one, ex (1, 1) maps to (0.707, 0.707)
//visit http://squircular.blogspot.com/2015/09/mapping-circle-to-square.html for more info

double u = x * Math.sqrt(1 - y * y / 2);
double v = y * Math.sqrt(1 - x * x / 2);
return new double[]{u, v};
}


//ANGLES
public static double normalizeAngle(double degrees) {
//Takes any degree angle and converts it to between -179 and +180 degrees
return AngleUnit.DEGREES.normalize(degrees);
}




}
Loading