Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include "C:/Program Files (x86)/VEX Robotics/VEXcode/sdk/vexv5/include/v5_apitypes.h"
- #include "C:/Program Files (x86)/VEX Robotics/VEXcode/sdk/vexv5/include/v5_apiuser.h"
- #include "C:/Program Files (x86)/VEX Robotics/VEXcode/sdk/vexv5/include/vex_units.h"
- #include "v5.h"
- #include "v5_vcs.h"
- #include <cmath>
- #include "vex.h"
- using namespace vex;
- vex::brain Brain;
- vex::motor FL_mtr(vex::PORT10, vex::gearSetting::ratio18_1, true);
- vex::motor BL_mtr(vex::PORT3, vex::gearSetting::ratio18_1, true);
- vex::motor FR_mtr(vex::PORT2, vex::gearSetting::ratio18_1, false);
- vex::motor BR_mtr(vex::PORT4, vex::gearSetting::ratio18_1, false);
- vex::motor Tilt(vex::PORT5, vex::gearSetting::ratio18_1, false);
- vex::motor Lift(vex::PORT6, vex::gearSetting::ratio18_1, false);
- vex::motor LeftIntake(vex::PORT8, vex::gearSetting::ratio18_1, false);
- vex::motor RightIntake(vex::PORT7, vex::gearSetting::ratio18_1, false);
- vex::line Line(Brain.ThreeWirePort.C);
- vex::controller (Controller1);
- vex::competition Competition;
- void wait_until_settled(){
- int average_velocity = 1;
- while (average_velocity >= 1){
- average_velocity = (FL_mtr.velocity(vex::velocityUnits::rpm) + FR_mtr.velocity(vex::velocityUnits::rpm) + BL_mtr.velocity(vex::velocityUnits::rpm) + BR_mtr.velocity(vex::velocityUnits::rpm)) / 4;
- vex::task::sleep(10);
- FL_mtr.setStopping(vex::brakeType::brake);
- FR_mtr.setStopping(vex::brakeType::brake);
- BL_mtr.setStopping(vex::brakeType::brake);
- BR_mtr.setStopping(vex::brakeType::brake);
- vex::task::sleep(50);
- }
- FL_mtr.setStopping(vex::brakeType::coast);
- FR_mtr.setStopping(vex::brakeType::coast);
- BL_mtr.setStopping(vex::brakeType::coast);
- BR_mtr.setStopping(vex::brakeType::coast);
- vex::task::sleep(50);
- }
- void drive_inches(int distance, int max_power = 90, int min_power = 35, bool wait_for_rest = true){
- float error;
- float previous_error;
- float drive_power;
- float previous_drive_power;
- float distance_covered;
- float max_voltage = 80 * max_power;
- float min_voltage = 80 * min_power;
- const int Kp = 200;
- const int Kd = -100;
- BL_mtr.resetRotation();
- BR_mtr.resetRotation();
- if (distance <= 2){
- distance = 3;
- }
- error = distance - (fabs((BL_mtr.rotation(vex::rotationUnits::deg)) + fabs(BR_mtr.rotation(vex::rotationUnits::deg)) / 2) / 20.64);
- while(error > 2){
- error = distance - (fabs((BL_mtr.rotation(vex::rotationUnits::deg)) + fabs(BR_mtr.rotation(vex::rotationUnits::deg)) / 2) / 20.64);
- drive_power = (error * Kp) + ((previous_error - error) * Kd);
- if (distance_covered < 0.25 * distance){
- drive_power = (drive_power - previous_drive_power) * 0.6;
- }
- if (drive_power < min_voltage && error > 2){
- drive_power = min_voltage;
- } else if (drive_power > min_voltage){
- drive_power = max_voltage;
- } else if (drive_power < min_voltage && error <= 2){
- drive_power = min_voltage * 0.6;
- }
- FL_mtr.spin(vex::directionType::undefined, drive_power*-1, vex::voltageUnits::mV);
- BR_mtr.spin(vex::directionType::undefined, drive_power*-1, vex::voltageUnits::mV);
- BL_mtr.spin(vex::directionType::undefined, drive_power*-1, vex::voltageUnits::mV);
- FR_mtr.spin(vex::directionType::undefined, drive_power*-1, vex::voltageUnits::mV);
- previous_error = error;
- previous_drive_power = drive_power;
- }
- FL_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- BR_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- BL_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- FR_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- if (wait_for_rest == true){
- wait_until_settled();
- } else {
- FL_mtr.setStopping(vex::brakeType::brake);
- FR_mtr.setStopping(vex::brakeType::brake);
- BL_mtr.setStopping(vex::brakeType::brake);
- BR_mtr.setStopping(vex::brakeType::brake);
- vex::task::sleep(100);
- FL_mtr.setStopping(vex::brakeType::coast);
- FR_mtr.setStopping(vex::brakeType::coast);
- BL_mtr.setStopping(vex::brakeType::coast);
- BR_mtr.setStopping(vex::brakeType::coast);
- }
- }
- void drive_inches_reverse(int distance, int max_power = 90, int min_power = 35, bool wait_for_rest = true){
- float error;
- float previous_error;
- float drive_power;
- float previous_drive_power;
- float distance_covered;
- float max_voltage = 80 * max_power;
- float min_voltage = 80 * min_power;
- const int Kp = 200;
- const int Kd = -100;
- BL_mtr.resetRotation();
- BR_mtr.resetRotation();
- if (distance <= 2){
- distance = 3;
- }
- error = distance - (fabs((BL_mtr.rotation(vex::rotationUnits::deg)) + fabs(BR_mtr.rotation(vex::rotationUnits::deg)) / 2) / 7);
- while(error > 2){
- error = distance - (fabs((BL_mtr.rotation(vex::rotationUnits::deg)) + fabs(BR_mtr.rotation(vex::rotationUnits::deg)) / 2) / 7);
- drive_power = (error * Kp) + ((previous_error - error) * Kd);
- if (distance_covered < 0.25 * distance){
- drive_power = (drive_power - previous_drive_power) * 0.6;
- }
- if (drive_power < min_voltage && error > 2){
- drive_power = min_voltage;
- } else if (drive_power > min_voltage){
- drive_power = max_voltage;
- } else if (drive_power < min_voltage && error <= 2){
- drive_power = min_voltage * 0.6;
- }
- FL_mtr.spin(vex::directionType::undefined, drive_power, vex::voltageUnits::mV);
- BR_mtr.spin(vex::directionType::undefined, drive_power, vex::voltageUnits::mV);
- BL_mtr.spin(vex::directionType::undefined, drive_power, vex::voltageUnits::mV);
- FR_mtr.spin(vex::directionType::undefined, drive_power, vex::voltageUnits::mV);
- previous_error = error;
- previous_drive_power = drive_power;
- }
- FL_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- BR_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- BL_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- FR_mtr.spin(vex::directionType::undefined, 0, vex::voltageUnits::mV);
- if (wait_for_rest == true){
- wait_until_settled();
- } else {
- FL_mtr.setStopping(vex::brakeType::brake);
- FR_mtr.setStopping(vex::brakeType::brake);
- BL_mtr.setStopping(vex::brakeType::brake);
- BR_mtr.setStopping(vex::brakeType::brake);
- vex::task::sleep(100);
- FL_mtr.setStopping(vex::brakeType::coast);
- FR_mtr.setStopping(vex::brakeType::coast);
- BL_mtr.setStopping(vex::brakeType::coast);
- BR_mtr.setStopping(vex::brakeType::coast);
- }
- }
- void turnright(int inches, bool speed){
- bool autonomous_running = true;
- int mastermotorpower;
- int slavemotorpower;
- FL_mtr.resetRotation();
- FR_mtr.resetRotation();
- float Kp1 = 0.65;
- float Kp2 = 0.4;
- if (speed == true){
- while (autonomous_running == true && std::abs(FL_mtr.rotation(vex::rotationUnits::deg)) < inches * 2){
- mastermotorpower = ((inches*2) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg))) * Kp1;
- if (mastermotorpower > 110){
- mastermotorpower = 110;
- } else if (mastermotorpower < 50){
- mastermotorpower = 50;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FL_mtr.rotation(vex::rotationUnits::deg)) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg))));
- FL_mtr.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- FR_mtr.spin(vex::directionType::rev,slavemotorpower, vex::velocityUnits::rpm);
- BL_mtr.spin(vex::directionType::rev,mastermotorpower*-1, vex::velocityUnits::rpm);
- BR_mtr.spin(vex::directionType::fwd,slavemotorpower*-1, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FL_mtr.stop(vex::brakeType::brake);
- FR_mtr.stop(vex::brakeType::brake);
- BL_mtr.stop(vex::brakeType::brake);
- BR_mtr.stop(vex::brakeType::brake);
- } else {
- while(autonomous_running == true && std::abs(FL_mtr.rotation(vex::rotationUnits::deg))< inches * 2){
- mastermotorpower = ((inches*2) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg)))* Kp2;
- if (mastermotorpower > 70){
- mastermotorpower = 70;
- } else if (mastermotorpower < 30){
- mastermotorpower = 30;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FL_mtr.rotation(vex::rotationUnits::deg))- std::abs(FR_mtr.rotation(vex::rotationUnits::deg))));
- FL_mtr.spin(vex::directionType::fwd,mastermotorpower, vex::velocityUnits::rpm);
- FR_mtr.spin(vex::directionType::rev,slavemotorpower, vex::velocityUnits::rpm);
- BL_mtr.spin(vex::directionType::rev,mastermotorpower*-1, vex::velocityUnits::rpm);
- BR_mtr.spin(vex::directionType::fwd,slavemotorpower*-1, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FL_mtr.stop(vex::brakeType::brake);
- BL_mtr.stop(vex::brakeType::brake);
- FR_mtr.stop(vex::brakeType::brake);
- BR_mtr.stop(vex::brakeType::brake);
- }
- }
- void turnleft(int inches, bool speed){
- bool autonomous_running = true;
- int mastermotorpower;
- int slavemotorpower;
- FL_mtr.resetRotation();
- FR_mtr.resetRotation();
- float Kp1 = 0.65;
- float Kp2 = 0.4;
- if (speed == true){
- while (autonomous_running == true && std::abs(FL_mtr.rotation(vex::rotationUnits::deg)) < inches * 2){
- mastermotorpower = ((inches*2) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg))) * Kp1;
- if (mastermotorpower > 150){
- mastermotorpower = 150;
- } else if (mastermotorpower < 50){
- mastermotorpower = 50;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FL_mtr.rotation(vex::rotationUnits::deg)) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg))));
- FL_mtr.spin(vex::directionType::fwd,mastermotorpower*-1, vex::velocityUnits::rpm);
- FR_mtr.spin(vex::directionType::rev,slavemotorpower*-1, vex::velocityUnits::rpm);
- BL_mtr.spin(vex::directionType::rev,mastermotorpower, vex::velocityUnits::rpm);
- BR_mtr.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FL_mtr.stop(vex::brakeType::brake);
- FR_mtr.stop(vex::brakeType::brake);
- BL_mtr.stop(vex::brakeType::brake);
- BR_mtr.stop(vex::brakeType::brake);
- } else {
- while(autonomous_running == true && std::abs(FL_mtr.rotation(vex::rotationUnits::deg))< inches * 2){
- mastermotorpower = ((inches*2) - std::abs(FL_mtr.rotation(vex::rotationUnits::deg)))* Kp2;
- if (mastermotorpower > 70){
- mastermotorpower = 70;
- } else if (mastermotorpower < 30){
- mastermotorpower = 30;
- }
- slavemotorpower = mastermotorpower + ((std::abs(FL_mtr.rotation(vex::rotationUnits::deg))- std::abs(FR_mtr.rotation(vex::rotationUnits::deg))));
- FL_mtr.spin(vex::directionType::fwd,mastermotorpower*-1, vex::velocityUnits::rpm);
- FR_mtr.spin(vex::directionType::rev,slavemotorpower*-1, vex::velocityUnits::rpm);
- BL_mtr.spin(vex::directionType::rev,mastermotorpower, vex::velocityUnits::rpm);
- BR_mtr.spin(vex::directionType::fwd,slavemotorpower, vex::velocityUnits::rpm);
- vex::task::sleep(5);
- }
- FL_mtr.stop(vex::brakeType::brake);
- BL_mtr.stop(vex::brakeType::brake);
- FR_mtr.stop(vex::brakeType::brake);
- BR_mtr.stop(vex::brakeType::brake);
- }
- }
- void pre_auton( void ) {
- Tilt.resetRotation();
- Lift.resetRotation();
- FL_mtr.resetRotation();
- FR_mtr.resetRotation();
- BL_mtr.resetRotation();
- BR_mtr.resetRotation();
- }
- void autonomous( void ) {
- Lift.rotateTo(-600, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Deploy
- Lift.rotateTo(0, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Deploy
- vex::task::sleep(300);
- RightIntake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);// Intake
- LeftIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);// Intake
- drive_inches(48,90,35, true);//1st Row
- drive_inches_reverse(48,90,35, true);//Backs Up
- drive_inches(6,90,35, true);//Towards Zone
- }
- void resetTilt(int tiltSpeed){
- Tilt.rotateTo(0, vex::rotationUnits::deg, tiltSpeed, vex::velocityUnits::pct);
- Tilt.stop(vex::brakeType::hold);
- }
- void lowTower(){
- RightIntake.startRotateFor(30, vex::rotationUnits::deg, 70, vex::velocityUnits::pct);
- LeftIntake.rotateFor(-30, vex::rotationUnits::deg, 70, vex::velocityUnits::pct);
- vex::task::sleep(10);
- Lift.rotateTo(-115, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Moves Lift up to Score in Tower
- RightIntake.startRotateFor(150, vex::rotationUnits::deg, 25, vex::velocityUnits::pct);
- LeftIntake.startRotateFor(-150, vex::rotationUnits::deg, 25, vex::velocityUnits::pct);
- Lift.rotateTo(-625, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Moves Lift up to Score in Tower
- }
- void highTower(){
- RightIntake.startRotateFor(30, vex::rotationUnits::deg, 70, vex::velocityUnits::pct);
- LeftIntake.rotateFor(-30, vex::rotationUnits::deg, 70, vex::velocityUnits::pct);
- vex::task::sleep(10);
- Lift.rotateTo(-115, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Moves Lift up to Score in Tower
- RightIntake.startRotateFor(150, vex::rotationUnits::deg, 25, vex::velocityUnits::pct);
- LeftIntake.startRotateFor(-150, vex::rotationUnits::deg, 25, vex::velocityUnits::pct);
- Lift.rotateTo(-915, vex::rotationUnits::deg, 90, vex::velocityUnits::pct);// Moves Lift up to Score in Tower
- }
- int tilterTasks (){
- Tilt.resetRotation();
- while(1){
- int TilterSpeed(-1175-(Tilt.rotation(vex::rotationUnits::deg)));
- if(Controller1.ButtonL1.pressing()){
- Lift.spin(vex::directionType::rev, 90, vex::percentUnits::pct);
- }
- else if (Controller1.ButtonL2.pressing()) {
- Lift.spin(vex::directionType::fwd, 90, vex::percentUnits::pct);
- }
- else if(Controller1.ButtonA.pressing()){
- Lift.stop(vex::brakeType::coast);
- Tilt.spin(vex::directionType::rev, ((TilterSpeed)/-8), vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonB.pressing()){
- resetTilt(90);
- }
- else if(Controller1.ButtonX.pressing()){
- lowTower();
- }
- else if(Controller1.ButtonY.pressing()){
- highTower();
- }
- else{
- Tilt.stop(vex::brakeType::hold);
- Lift.stop(vex::brakeType::hold);
- }
- if(Controller1.ButtonR2.pressing()){
- RightIntake.spin(vex::directionType::fwd, 55, vex::velocityUnits::pct);
- LeftIntake.spin(vex::directionType::rev, 55, vex::velocityUnits::pct);
- }
- else if(Controller1.ButtonR1.pressing()){
- RightIntake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);
- LeftIntake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
- }
- else{
- RightIntake.stop(vex::brakeType::hold);
- LeftIntake.stop(vex::brakeType::hold);
- }
- }
- return 1;
- }
- void usercontrol( void ) {
- vex::task tilter = vex::task(tilterTasks);
- while (1) {
- FR_mtr.spin(directionType::fwd, (Controller1.Axis3.value() + (Controller1.Axis1.value())*.75)/1.25, velocityUnits::pct); //(Axis3+Axis4)/2;
- FR_mtr.spin(directionType::fwd, (Controller1.Axis3.value() - (Controller1.Axis1.value())*.75)/1.25, velocityUnits::pct);//(Axis3-Axis4)/2;
- BL_mtr.spin(directionType::fwd, (Controller1.Axis3.value() + (Controller1.Axis1.value())*.75)/1.25, velocityUnits::pct); //(Axis3+Axis4)/2;
- BR_mtr.spin(directionType::fwd, (Controller1.Axis3.value() - (Controller1.Axis1.value())*.75)/1.25, velocityUnits::pct);//(Axis3-Axis4)/2;
- }
- vex::task::sleep(10);
- }
- int main() {
- //Set up callbacks for autonomous and driver control periods.
- Competition.autonomous( autonomous );
- Competition.drivercontrol( usercontrol );
- //Run the pre-autonomous function.
- pre_auton();
- //Prevent main from exiting with an infinite loop.
- while(1) {
- vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement