Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma region VEXcode Generated Robot Configuration
- // Make sure all required headers are included.
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <math.h>
- #include <string.h>
- #include "vex.h"
- using namespace vex;
- competition Competition;
- //define global variables
- int axis3Drive = 0;
- int axis1Drive = 0;
- float coef = 1.28;
- // Brain should be defined by default
- brain Brain;
- // START V5 MACROS
- #define waitUntil(condition) \
- do { \
- wait(5, msec); \
- } while (!(condition))
- #define repeat(iterations) \
- for (int iterator = 0; iterator < iterations; iterator++)
- // END V5 MACROS
- // Robot configuration code.
- controller Controller1 = controller(primary);
- rotation Rotation1 = rotation(PORT19, false);
- distance Distance1 = distance(PORT17);
- motor leftMotorA = motor(PORT3, ratio6_1, false);
- motor leftMotorB = motor(PORT7, ratio6_1, false);
- motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
- motor rightMotorA = motor(PORT4, ratio6_1, true);
- motor rightMotorB = motor(PORT8, ratio6_1, true);
- motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
- drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 219.44, 295, 40, mm, 1);
- motor cataMotorA = motor(PORT14, ratio36_1, true);
- motor cataMotorB = motor(PORT10, ratio36_1, false);
- motor_group cata = motor_group(cataMotorA, cataMotorB);
- motor intake = motor(PORT12, ratio36_1, true);
- digital_out DigitalOutB = digital_out(Brain.ThreeWirePort.B);
- digital_out DigitalOutH = digital_out(Brain.ThreeWirePort.H);
- digital_in DigitalInD = digital_in(Brain.ThreeWirePort.D);
- // define variable for remote controller enable/disable
- bool RemoteControlCodeEnabled = true;
- // define variables used for controlling motors based on controller inputs
- bool DrivetrainLNeedsToBeStopped_Controller1 = true;
- bool DrivetrainRNeedsToBeStopped_Controller1 = true;
- int axis3calc() {
- while(true){
- //get controller info and prep for processing
- //initalize variables first
- double y = 0;
- int x = fabs(Controller1.Axis3.position() * coef);
- switch(x){
- case 0 ... 60:
- y = pow(1.04029, x * 1.284467);
- break;
- case 61 ... 80:
- y = 0.75 * x -25;
- break;
- case 81 ... 108:
- y = 1.0357 * x - 47.85714857;
- break;
- case 109 ... 127:
- y = 63 + pow(1.232099, x - 108);
- break;
- default:
- y = 128;
- break;
- }
- y = y / coef;
- if(Controller1.Axis3.position() < 0){
- y = -y;
- }
- axis3Drive = -y;
- wait(2.5, msec);
- }
- return 0;
- }
- int axis1calc(){
- while(true){
- //get controller info and prep for processing
- //initalize variables first
- double y = 0;
- int x = fabs(Controller1.Axis1.position() * coef);
- switch(x){
- case 0 ... 60:
- y = pow(1.04029, x * 1.284467);
- break;
- case 61 ... 80:
- y = 0.75 * x -25;
- break;
- case 81 ... 108:
- y = 1.0357 * x - 47.85714857;
- break;
- case 109 ... 127:
- y = 63 + pow(1.232099, x - 108);
- break;
- default:
- y = 128;
- break;
- }
- y = y / coef;
- if(Controller1.Axis1.position() < 0){
- y = -y;
- }
- axis1Drive = y;
- wait(2.5, msec);
- }
- return 0;
- }
- // define a task that will handle monitoring inputs from Controller1
- int rc_auto_loop_function_Controller1() {
- // process the controller input every 20 milliseconds
- // update the motors based on the input values
- while(true) {
- vex::task ws4(axis1calc);
- vex::task ws3(axis3calc);
- if(RemoteControlCodeEnabled) {
- int drivetrainLeftSideSpeed = axis3Drive - axis1Drive;
- int drivetrainRightSideSpeed = axis3Drive + axis1Drive;
- // check if the value is inside of the deadband range
- if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
- // check if the left motor has already been stopped
- if (DrivetrainLNeedsToBeStopped_Controller1) {
- // stop the left drive motor
- LeftDriveSmart.stop();
- // tell the code that the left motor has been stopped
- DrivetrainLNeedsToBeStopped_Controller1 = false;
- }
- } else {
- // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
- DrivetrainLNeedsToBeStopped_Controller1 = true;
- }
- // check if the value is inside of the deadband range
- if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
- // check if the right motor has already been stopped
- if (DrivetrainRNeedsToBeStopped_Controller1) {
- // stop the right drive motor
- RightDriveSmart.stop();
- // tell the code that the right motor has been stopped
- DrivetrainRNeedsToBeStopped_Controller1 = false;
- }
- } else {
- // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
- DrivetrainRNeedsToBeStopped_Controller1 = true;
- }
- // only tell the left drive motor to spin if the values are not in the deadband range
- if (DrivetrainLNeedsToBeStopped_Controller1) {
- LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
- LeftDriveSmart.spin(forward);
- }
- // only tell the right drive motor to spin if the values are not in the deadband range
- if (DrivetrainRNeedsToBeStopped_Controller1) {
- RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
- RightDriveSmart.spin(forward);
- }
- }
- // wait before repeating the process
- wait(5, msec);
- }
- return 0;
- }
- task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
- #pragma endregion VEXcode Generated Robot Configuration
- bool intakeInterlock = false;
- bool cataInterlock = false;
- int switchpoint = 0;
- void setDown(){
- while(true){
- if(Rotation1.position(degrees) < 40){
- cata.setVelocity(100, percent);
- printf("%lf\n", Rotation1.position(degrees));
- cata.spin(forward);
- } else {
- cata.setVelocity(30, percent);
- cata.spin(forward);
- while(Rotation1.position(degrees) > 43 && Rotation1.position(degrees) < 43){
- printf("eidi\n");
- cata.stop();
- }
- if(Rotation1.position(degrees) > 72){
- printf("Error: 'setDown' function; %lf\n", Rotation1.position(degrees));
- }
- }
- }
- }
- int Interlock(){
- while(true){
- if(DigitalInD.value() == 0){
- intakeInterlock = true;
- } else {
- intakeInterlock = false;
- }
- if(DigitalOutB.value() == 1){
- cataInterlock = false;
- } else {
- cataInterlock = true;
- }
- wait(5, msec);
- }
- return 0;
- }
- int Intake(){
- bool intakeToggle = false;
- while(true){
- intake.setMaxTorque(100, percent);
- intake.spin(forward);
- if(Controller1.ButtonR1.pressing()){
- intakeToggle = !intakeToggle;
- wait(250, msec);
- }
- if(Distance1.objectDistance(inches) < 4.0){
- intake.setStopping(hold);
- intake.stop();
- intake.setVelocity(0, percent);
- } else {
- intake.setVelocity(60, percent);
- }
- wait(5, msec);
- }
- return 0;
- }
- int cataFire(){
- cata.setMaxTorque(100, percent);
- cata.setVelocity(100, percent);
- cata.setStopping(hold);
- while(true){
- if(Distance1.objectDistance(inches) < 4.0){
- cata.spin(forward);
- if(Rotation1.position(degrees) > 90)
- setDown();
- }
- wait(5, msec);
- printf("%lf\n", Rotation1.position(degrees));
- }
- return 0;
- }
- int wings(){
- while(true){
- if(Controller1.ButtonL1.pressing()){
- DigitalOutH.set(true);
- }else{
- DigitalOutH.set(false);
- }
- wait(5, msec);
- }
- return 0;
- }
- void whenStarted(){
- DigitalOutB.set(true);
- cata.setMaxTorque(100, percent);
- cata.setVelocity(100, percent);
- cata.setStopping(hold);
- printf("%lf\n", Rotation1.position(degrees));
- Rotation1.setPosition(0, degrees);
- wait(50, msec);
- cata.stop();
- printf("%lf\n", Rotation1.position(degrees));
- setDown();
- }
- int main() {
- // post event registration
- // set default print color to black
- printf("\033[30m");
- // wait for rotation sensor to fully initialize
- wait(30, msec);
- //run the important things
- whenStarted();
- vex::task ws5(Interlock);
- vex::task ws6(wings);
- vex::task ws1(Intake);
- vex::task ws2(cataFire);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement