I can't turn my pusher (indexer) in Auton, It works fine in driver control

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       devalmukherjee                                            */
/*    Created:      Tue Oct 18 2022                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

#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;

// A global instance of competition
competition Competition;

// Robot configuration code.
motor RightFront = motor(PORT20, ratio18_1, true);

motor LeftFront = motor(PORT17,ratio18_1);

motor RightBack = motor(PORT18,ratio18_1, true);

motor LeftBack = motor(PORT6, ratio18_1);

motor FlyWheel = motor(PORT16,ratio6_1);

motor Intake = motor(PORT19, ratio18_1, true);

motor pusher = motor(PORT5, ratio6_1);

motor roller = motor(PORT10, ratio6_1);

controller Controller1 = controller(primary);

int forward1;
int sideways;
int turn;



void pre_auton(void) 
{
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
}


int drivePID()
{
  bool enableDrivePID = false;

  // Settings
  double kp = 200;
  // double Ki = 200;
  // double Kd = 200;

  int desiredValue = 200;
  int error;
  int prevError = 0;
  int derivative = 0;
  int totalError = 0 ;
  int desiredturnvalue;

  while(enableDrivePID)
  {
    forward1 = Controller1.Axis3.position();
    sideways = Controller1.Axis4.position();
    turn = Controller1.Axis1.position();
  
    // Getting the values from the sensors.
    int leftBackMotorposition = LeftBack.position(degrees);
    int leftFrontMotorposition = LeftFront.position(degrees);
    int RightBackMotorposition = RightBack.position(degrees);
    int RightFrontMotorposition = RightFront.position(degrees);
    ///////////////////////////////////////////////////////////////////

    // Making the mean value for all the wheels in the robot.
    int averagePosition = (leftBackMotorposition+leftFrontMotorposition+RightBackMotorposition+RightFrontMotorposition);

    error = averagePosition - desiredValue;

    derivative = error - prevError;

    totalError += error;

    double lateralmotorpower = (error * kp );
    //////////////////////////////////////////////////////////////////////////////////////

    LeftBack.setVelocity(lateralmotorpower, rpm);
    LeftFront.setVelocity(lateralmotorpower, rpm);
    RightBack.setVelocity(lateralmotorpower, rpm);
    RightBack.setVelocity(lateralmotorpower, rpm);
    

    prevError = error;
    vex::task::sleep(20);
  }
  return 1;
}

void push( double revolutions){
  pusher.spinFor(vex::directionType::fwd,revolutions, vex::rotationUnits::rev);
}

void driveForward( float inches ) {
    float inchesPerDegree = (4 * 3.1416) / 360;
    float degrees = inches / inchesPerDegree;
    // startRotate doesn't wait for completion
    // this way, the other wheel can turn at same time
    LeftBack.startRotateFor(vex::directionType::fwd,
        degrees, vex::rotationUnits::deg, 
        100, vex::velocityUnits::pct
    );
    RightBack.startRotateFor(vex::directionType::fwd,
        degrees, vex::rotationUnits::deg,
        100, vex::velocityUnits::pct
    );
    LeftFront.startRotateFor(vex::directionType::fwd,
        degrees, vex::rotationUnits::deg, 
        100, vex::velocityUnits::pct
    );
    RightFront.startRotateFor(vex::directionType::fwd,
        degrees, vex::rotationUnits::deg,
        100, vex::velocityUnits::pct
    );
}
void turn1( float degrees ) {
    // Note: +90 degrees is a right turn
    float turningRatio = 27 / 4;
    float wheelDegrees = turningRatio * degrees;    
    // Divide by two because each wheel provides half the rotation
    LeftBack.startRotateFor(vex::directionType::rev,
        wheelDegrees, vex::rotationUnits::deg, 
        100, vex::velocityUnits::pct
    );
    LeftFront.startRotateFor(vex::directionType::rev,
        wheelDegrees , vex::rotationUnits::deg, 
        100, vex::velocityUnits::pct
    );
    RightFront.startRotateFor(
        wheelDegrees, vex::rotationUnits::deg,
        100, vex::velocityUnits::pct
    );
    RightBack.rotateFor(
        wheelDegrees, vex::rotationUnits::deg,
        100, vex::velocityUnits::pct
    );

}



void autonomous(void) 
{
  // vex::task f(drivePID); 
    // LeftBack.setVelocity(90, vex::percentUnits::pct);
    // LeftFront.setVelocity(90, vex::percentUnits::pct);
    // RightBack.setVelocity(90, vex::percentUnits::pct);
    // RightFront.setVelocity(90, vex::percentUnits::pct);
    FlyWheel.setVelocity(100, pct);
  //   roller.setVelocity(100, pct);
   pusher.setVelocity(100, pct);
//push(10);

  // driveForward(-1); 
  // Intake.spinFor(vex::directionType::rev, 1, vex::rotationUnits::rev);
  // turn1(50);
  // wait(200, msec);
  // driveForward(5);
  
  // Intake.startRotateFor(vex::directionType::rev, 10, vex::rotationUnits::rev);
  // wait(200, msec);
  // driveForward(-5);
  // wait(200, msec);
  // turn1(-40);
  
  // FlyWheel.spinFor(vex::directionType::rev, 10, sec);
  // roller.spinFor(vex::directionType::fwd, 10, sec);
  // wait(1, sec);
  pusher.startRotateFor(vex::directionType::fwd, 10, vex::rotationUnits::rev);



}

int shoot()
{
  while (true)
  {


   if (Controller1.ButtonUp.pressing())
    {
      pusher.spin(vex::directionType::fwd,100,vex::velocityUnits::pct);
    }
   else if(Controller1.ButtonDown.pressing())
    {
      pusher.spin(vex::directionType::rev,100,vex::velocityUnits::pct);
    }
      // pusher.spinFor(.25, sec, 100, vex::velocityUnits::pct);
      // wait(.1, sec);
      // pusher.spinFor(.25, sec, 100, vex::velocityUnits::pct);
      // wait(.3, sec);
      // pusher.spinFor(.5, sec, 100, vex::velocityUnits::pct);
      // // wait(1, sec);
    
    else 
    {
      pusher.stop();
    }
    vex::task::sleep( 25 );
   
  }
  return 0;
}

void usercontrol(void) 
{
  while (true)
  {



    // FlyWheel.spin(vex::directionType::rev, 50, pct);
    printf("%.7lf\n", FlyWheel.velocity(pct));

    forward1 = Controller1.Axis3.position();
    sideways = -Controller1.Axis4.position();
    turn = Controller1.Axis1.position();

    //driver control

    RightFront.spin(vex::forward, forward1 + sideways - turn, vex::velocityUnits::pct);
    LeftFront.spin(vex::forward,  forward1 - sideways + turn, vex::velocityUnits::pct);
    RightBack.spin(vex::forward,  forward1 - sideways - turn, vex::velocityUnits::pct);
    LeftBack.spin(vex::forward,   forward1 + sideways + turn, vex::velocityUnits::pct);

    //flywheel
    if(FlyWheel.velocity(pct) <= -(9.4/12)*100)
    {
      Controller1.rumble("-");
    }
  
    if (Controller1.ButtonR2.pressing()) 
     {
      FlyWheel.spin(vex::directionType::rev, 9.5, vex::voltageUnits::volt);
      roller.spin(vex::directionType::fwd, 9.5, vex::voltageUnits::volt);


     } 
     else 
     {
      FlyWheel.stop(vex::brakeType::coast);
      roller.stop(vex::brakeType::coast);    
     }


    //intake
     if (Controller1.ButtonL1.pressing()) 
     {
      Intake.spin(vex::directionType::rev, 100, vex::velocityUnits::pct);;
     }
     else if (Controller1.ButtonL2.pressing()) 
     {
      Intake.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);;
     }
     else 
     {
      Intake.stop(vex::brakeType::brake);  
     }
     
     
  //   // Pusher
  //   if (Controller1.ButtonUp.pressing())
  //   {
  //     pusher.spin(vex::directionType::fwd,100,vex::velocityUnits::pct);
  //   }
  //  else if(Controller1.ButtonDown.pressing())
  //   {
  //     pusher.spin(vex::directionType::rev,100,vex::velocityUnits::pct);
  //   }
  //   else if(Controller1.ButtonA.pressing())
  //   {
  //     pusher.spinFor(1, sec, 50, vex::velocityUnits::pct);
  //     wait(1, sec);
  //     pusher.spinFor(1, sec, 50, vex::velocityUnits::pct);
  //     wait(1, sec);
  //     pusher.spinFor(1, sec, 50, vex::velocityUnits::pct);
  //     wait(1, sec);
  //   }

  //   else
  //   {
  //     pusher.stop();
  //   }

    // Pneumatics
    if(Controller1.ButtonY.pressing())
    {
      DigitalOutH.set(true);
      DigitalOutG.set(true);
    }

    else
    {
      DigitalOutH.set(false);
      DigitalOutG.set(false);
    }
  }
    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
}


//
// Main will set up the competition functions and callbacks.
//
int main() 
{
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);
  vex::task t(shoot);
  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  vex::task::sleep( 10 );
}

Well, you start a task in main called shoot that will always be sending stop() to it during autonomous as well.

vex::task t(shoot);

perhaps also read these two topics.

4 Likes

I would like to spin the pusher and other motors at the same time in driver control, any solutions to that?

The simplest way is to just put that code (the code in shoot, without the while loop) in usercontrol. You already have very similar code for your intake, the pusher can be done in the same way.

5 Likes

I see the problem, you are trying to control the pusher motor in different ways. which is confusing the brain as to which order to follow. Instead you should create a task for the whole pusher.