Programming Question

So my friend and I were thinking of a program for our robot we built for fun. So what we were thinking as that we wanted the controls from the robot switch to the other controller.
So what essentially means is that when our first driver is driving he presses a button and the controls switch to the second driver.
Is this possible if it is then can you please post code. Mainly because my friend and I want to program this asap.
:confused::confused:

What programming environment are you using? I’ve posted some semi-pseudo code below:


While(true)
{
       /* Drive code for driver 1 */
       If(buttonToSwitchModes = true) // the button you want to trigger the change
       {
              DriverMode2();
        }
}

Meanwhile, DriverMode2() would look like this


Void DriverMode2()
{
bool quit = false;
While(!quit)
{
       /* Driver control for Driver 2 */
       If(buttonToChange = true) // button you want to switch back to driver 1
       { 
               quit = true;
        }
}
}

The problem with this is that if you make changes on one driver control, you have to make it on both, presumably because there’s an issue with one.

It is easily possible to implement a better method of changing drivers, however, I’d rather post code that can work on both ROBOTC and EasyC, so if you are using ROBOTC, I’ll show you how to use structs to change drivers.

With EasyC all you need is a variable which keeps track of which joystick is being used, this is passed to GetJoystickAnalog or whatever other joystick functions you are using.

In ROBOTC there are a few options.

  1. Just program everything twice like edjubuh suggested.
  2. wrap vexRT with a function that returns either the value for controller 1 or 2.
short   joystick = 0;

int getCh1()
{
    if( !joystick )
        return( vexRT Ch1 ] );
    else
        return( vexRT Ch1Xmtr2 ] );
}

then change the joystick variable to 0 or 1 in your switching code.

  1. Use some “magic” that understands what Ch1, Ch2 etc really mean and again create a function that returns the right data.

short   joystick = 0;

int getController( TVexJoysticks index )
{
    // analog values
    if( (index >= Ch1) && (index <= Ch4))
        {
        if( ! joystick )
            return( vexRT index ] );
        else
            return( vexRT index + 6 ] );
        }
        
        
    // buttons
    if( (index >= Btn5D) && (index <= Btn7R))
        {
        if( ! joystick )
            return( vexRT index ] );
        else
            return( vexRT index + 12 ] );
        }
        
    return(0);
}

Use getController anywhere that you would have used vexRT with controller 1 indices.

and some test code to demonstrate that.

int value;

task main()
{
    // test js1 for 2 seconds
    joystick = 0;
    for(int i=0;i<200;i++) {
        value = getController( Btn7R );
        wait1Msec(10);
    }
    // test js2 for 2 seconds
    joystick = 1;
    for(int i=0;i<200;i++) {
        value = getController( Btn7R );
        wait1Msec(10);
    }
}