Need Programming help- Executing two actions at once

I just made the switch to using two controllers the other day, and I’ve noticed a few issues that have arisen as a result. When using one controller, it was easy to hit the button to open the claw on our robot and then back up away from a cube or skyrise piece as the claw closed (it is set to send about 20 units of power or whatever you call it to the claw always to help it grip heavier things), although now that one controller is doing the claw opening and another is doing the backing up, We’ll need to be able to keep the claw open while the other driver reacts and drives backwards. I currently have all of the commands entered as “else if” functions, and it doesn’t allow the two controllers to send two commands at once, even if they are pertaining to different motors.

How should I edit the programming to allow for multiple commands at once? I can post the current code tomorrow sometime if that would be helpful too.

Thanks for the help!

You need to create separate tasks. One does the driving and one does the claw and other stuff. Assuming its robotc, you can create a task just like the user or main one. Then you can have StartTask(name of other task) in the user one.

If you are in easyC, you will not be able to use tasks.

If you do something using just if loops, you should be fine. What I like to do is have if and else ifs depending on my system (drive, lift, intake)


if(GetJoystickDigital(1,5,1)==1)
{
  Open Claw
}
else if(GetJoystickDigital(1,5,2)==1)
{
 Close Claw
}

if(GetJoystickDigital(1,6,1)==1)
{
 Lift Up
}
else if(GetJoystickDigital(1,6,2)==1)
{
 Lift Down
}

Obviously the above is half real code/ half pseudo code

That looks like a really good option. Would I be able to do all of that in the same task or does it need a new Task above each if statement?

Sorry if that’s a dumb question, but my only knowledge of C is from trial and error and looking off of the awful code that our team had last year…:rolleyes:

One can defend for Easy C, but that cannot change the fact that currently easy c cannot multitask… Multitasking makes your loops significantly neater. You first declare a task, then use startTask(); and stopTask(); to control them.

I forgot to mention this, but no, I’m not using Easy C. A copy of RobotC came with the Vex package that we got and it seems like that is the best option anyways.

Here’s the revised code:

 while(true){
  	if (vexRT[Btn6D] == 1) {
  		motor[leftSpitMotor] = -127;  ////retract arm
  	}//////////////////////////////
  	else if (vexRT[Btn6U] == 1) {
  		motor[leftSpitMotor] = 127; ////extend arm
  	}////////////////////////////////
  	if (vexRT[Btn5D] == 1) {
  		motor[leftLift] = 20;       // lower main lift arm
  		motor[rightLift] = -20;
  	}/////////////////////////////////////
  	else if (vexRT[Btn5U] == 1) {
  		motor[leftLift] = -70;       // raise main lift arm
  		motor[rightLift] = 70;
		}////////////////////////////////////////
		if (vexRT[Btn7R] == 1) {
			motor[port6] = -40;
			motor[port7] = -40;
			motor[port8] = -40;          // move straight sideways (right)
			motor[port9] = -40;
		}//////////////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn7L] == 1) {
			motor[port6] = 40;
			motor[port7] = 40;         // move straight sideways(left)
			motor[port8] = 40;
			motor[port9] = 40;
		}/////////////////////////////////////////////////////////////////////////////////////
		if (vexRT[Btn7U] == 1) {
			motor[port10] = 100;         // open claw
		}///////////////////////////////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn8R] == 1) {
			motor[port1] = 60;     //lift claw up slowly
		}//////////////////////////////////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn8L] == 1) {
			motor[port1] = -20;                   // lower claw slowly
		}//////////////////////////////////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn8U] == 1) {
			motor[port1] = 127;
		}////////////////////////////////////////////////
		if (vexRT[Btn8D] == 1) {
			motor[port1] = -60;
	}/////////////////////////////////////////////////////


		/////Controller 2


		if (vexRT[Btn8DXmtr2] == 1) {
			motor[port6] = -100;       //backwards
			motor[port7] = 100;
			motor[port8] = -95;
			motor[port9] = 100;
		}/////////////////////////////////////////////
		else if (vexRT[Btn8UXmtr2] == 1) {
			motor[port6] = 115;         // forwards
			motor[port7] = -115;
			motor[port8] = 85;
			motor[port9] = -80;
		}///////////////////////////////////////////////////////
		else if (vexRT[Btn8RXmtr2] == 1) {
			motor[port6] = -60;
			motor[port7] = 60;         // spin right
			motor[port8] = 60;
			motor[port9] = -60;
		}//////////////////////////////////////////////////////////
		else if (vexRT[Btn8LXmtr2] == 1) {
			motor[port6] = 60;
			motor[port7] = -60;       // spin left
			motor[port8] = -60;
			motor[port9] = 60;
		}///////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn7UXmtr2] == 1) {
			motor[port6] = 50;         // forwards
			motor[port7] = -60;
			motor[port8] = 55;
			motor[port9] = -50;
	}/////////////////////////////////////////////////////////////////////////
	else if (vexRT[Btn7DXmtr2] == 1) {
			motor[port6] = -60;       //backwards
			motor[port7] = 60;
			motor[port8] = -55;
			motor[port9] = 60;
		}/////////////////////////////////////////////////////////////////////
	else if (vexRT[Btn7LXmtr2] == 1) {
			motor[port6] = 30;
			motor[port7] = -30;       // spin left
			motor[port8] = -30;
			motor[port9] = 30;
		}//////////////////////////////////////////////////////////////////////
		else if (vexRT[Btn7RXmtr2] == 1) {
			motor[port6] = -30;
			motor[port7] = 30;         // spin right
			motor[port8] = 30;
			motor[port9] = -30;
		}////////////////////////////////////////////////////////////////////////
  	else {
  		motor[port1] = 18;
  		motor[leftLift] = -25;        //
  		motor[rightLift] = -25;       //
  		motor[leftSpitMotor] = 0;
  		motor[port6] = 0;
  		motor[port7] = 0;
  		motor[port8] = 0;
  		motor[port9] = 0;
  		motor[port10] = -50;           // helps the claw continue squeezing and hold things
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  	}}}

It seems to be working well, even if it isn’t the neatest way of going about things. I’m just hoping to have it working by Saturday for our next competition.

Thanks for the help!

This should do it.

So the key to the whole multiple loops thing is to have multiple “while” components? I tried just substituting some of the “else if” commands with “if” commands and it caused all kinds of weird things to happen. The motors switched on and off rapidly and it had kind of a sputtering effect.

On the other hand, it wouldn’t let me separate each controller’s code into its own “while(true) loop”

I’d hate to ask any of you to write out too much code because I’m a complete coding noob, but if I could just get an example of how I should edit the current program to give each controller its own “loop”, whatever that is, and not cause it to sputter and generally mess up.

Okay, I’m sorry.

I assumed you were using EasyC. If you are using RobotC, tasks make multitasking much simpler.

More pseudo code.

Task drive{
Motor[port1]=127;
Motor[port2]=127;
Motor[port3]=127;
Motor[port4]=127;
}
Task lift
Motor[port5]=127
Motor[port6]=127;
Motor[port7]=127;
Motor[port8]=127;
}
Task claw{
Motor[port9]=127;
Motor[port10]=127;
}

Task autonomous {
Starttask(drive);
Starttask(lift);
Wait1msec(3000);
Stoptask(drive);
Stoptask(lift);
Allmotorsoff();
}

Please correct me if I am incorrect.

Those really really really should be functions.

Agreed. Usually muktitasking is used for auton and LCD display. One huge loop handles all driver control commands fine.

I meant to type Task autonomous. Instead of main. I will go edit that to avoid confusion.

It’s still a really bad example of how to use a task. This code

Task drive{
Motor[port1]=127;
Motor[port2]=127;
Motor[port3]=127;
Motor[port4]=127;
}

sends the value 127 to the four motors and then exits, it might just as well (and should) be a function. For a task to be useful it needs to block for some reason, for example.

task drive()
{
    SensorValue mysensor ] = 0;

    motor mymotor ] = 127;
    
    while( SensorValue mysensor ] < 1000 )
        {
        wait1Msec( 10 );
        }

    motor mymotor ] = 0;
}

The task drives 1000 counts and then exits, no need to call stopTask.

Alright so I’m totally stuck here. I tried putting each thing in their own tasks (arm lifts in one loop, wheel movements in another, etc.) but nothing worked. I tried putting each in its own while(true) loop, and that only ran the first program. I tried making each movement be under its own if statement as opposed to an else if like they had been, but that didn’t work.

How exactly am I supposed to do this? I get that there is a huge debate over whether or not to use Easy C and the deep philosophical junk behind whether to “multitask” or not, whatever that means, but I’m just a programming noob looking for some help understanding what I’m even supposed to type into the program before our team’s competition tomorrow.

Is it something like:

 
task usercontrol ()
{
if (vexRT[Btn5D] == 1)
//motor stuffs blah blah blah
else if (vexRT[Btn5U] == 1)
//blah blah blah
else 
blah blah blah
}


task something else however that works ()
{
//if,else if, else, all that good stuff
}

I can’t find any resources on what exactly i’m supposed to do here and I have no more ideas on what to do. Any useful help would be appreciated.

An example.

task clawControl()
{
    while(1)
        {
        // Claw control
        if( vexRT Btn8U ] == 1 )
            motor port2 ] = 127;  // open
        else
        if( vexRT Btn8D ] == 1 )
            motor port2 ] = -127; // close
        else
        if( vexRT Btn8R ] == 1 )
            motor port2 ] = -10;  // hold
        else
            motor port2 ] = -0;   // no power
        
        wait1Msec(10);
        }
}


task usercontrol()
{
    startTask( clawControl );
    
    while (true)
        {
        // Drive
        motor port1 ]  = vexRT Ch2 ];
        motor port10 ] = vexRT Ch3 ];
        wait1Msec(10);
        }
}

So I should set everything up as its own task? Like:


task motorControl()
{
if (vexRT[Btn6D] == 1) {
  	motor[leftSpitMotor] = -127;
}
else if (vexRT[Btn6U] == 1) {
       motor[leftSpitMotor] = 127;
else {
motor [leftSpitMotor] = 0;
}}

task armControl() {
if (vexRT[Btn5D] == 1) {
  		motor[leftLift] = 20;       // lower main lift arm
  		motor[rightLift] = -20;
  	}
  	else if (vexRT[Btn5U] == 1) {
  		motor[leftLift] = -70;       // raise main lift arm
  		motor[rightLift] = 70;
else {
motor[leftLift] = 0;
motor[rightLift] = 0;
}}

task usercontrol() {

startTask( motorControl );
startTask( armControl );

}

and it should work?

I wasn’t thinking this morning, sorry. You are correct.

When Doinig User control, I think that it would be far easier and save some CPU to do something more like this and put all of the stuff in a big while loop in the user control function:

task usercontrol() {
	while(1){
		if (vexRT[Btn6D] == 1) {
			motor[leftSpitMotor] = -127;
		}
		else if (vexRT[Btn6U] == 1) {
			motor[leftSpitMotor] = 127;
		}
		else {
			motor [leftSpitMotor] = 0;
		}
		if (vexRT[Btn5D] == 1) {
			motor[leftLift] = 20;       // lower main lift arm
			motor[rightLift] = -20;
		}
		else if (vexRT[Btn5U] == 1) {
			motor[leftLift] = -70;       // raise main lift arm
			motor[rightLift] = 70;
		}
		else {
			motor[leftLift] = 0;
			motor[rightLift] = 0;
		}
	}
}

What you have should work though.

No won’t work. Each task needs a while loop.

Today is not my day.

I would like to think that I am a good programmer, but really, i’m not :frowning: