Programming Reversible Drive

Hi. My name is Brendan, and I am the lead of my VEX team, 9816D. I just downloaded EasyC, and it is a HUGE relief after struggling through RobotC. I do have one question. Our robot has manipulators off both the front and back. I am the main driver, and I find it quite difficult to remember that, when using the tail, the drive controls are reversed. I was wondering if there was a way to set up the program so that every time the D button on channel 7 is pressed, the program toggles between two different User Functions (the forward and reversed controls)?

I would also like to set up a system like this so that, every time I press the D button on channel 8, It toggles between two different user functions (single user control and partner control).

Thanks in advance for the help, it would be invaluable for our team!

Ps. This is our robot, for reference…

Oops, turns out I don’t know how to post pictures!

From what your telling me, and correct me if I am wrong, but it seems as if you just have 4 motors set up on a drive, and switch between the front and back. In my opinion, just write up the code for a 4-motor drive. As for the other part, the first thing that comes to mind is in the user control function, set up an if statement with a while loop inside of it. Have the if statement be for if a button had be pressed, and the while statement just be a simple while true. One version of your drive code would be within the while loop, and the other would be outside of the if statement. The only problem with this would be the fact that you couldn’t switch between them in the middle of a match, but I can see no reason as to why you would need to.

Yeah. It’s actually a six motor tank drive, but the rear motors are y-cabled together, making it appear as a 4 motor drive. I think I see what you are saying (I really am new to programming, especially on EasyC). I’m not so much worried about the single user/partner control as I am the reversible drive (although, if I figure one out, the other should follow in suit). I found myself wasting close to 15 seconds trying to figure out which side was which on the robot, I forgot every single time that I turned the robot around.

Our robot is designed to be an aid to the typical stacker/scorer robot. We play defense. Mean defense. Our strategy is to push the entire centre stack of blocks to the corner with the high goals, and then either simply play guard over the other alliance, or to begin scoring ground/cube stacking goals. So, for the most part, I drive forward for 30 seconds, then in reverse for 1:15. I do not, however, just want to reverse the controls permanently. The beginning phase is too tricky to be trying to remember which side is which. I’m really looking for a push-button toggle between two user functions. I would be shocked if there was no way to do this. I know that, in RobotC, the programming seemed a little more open-sourced. This made it more difficult usually, but could have come in handy at times like this. The code would Ideally go something like this…

If. Ch. 7D = 1;
(some type of code to alternate between two variables A & B)
A. Forward Control
B. Reverse Control

Else. 0

Hopefully saying that, If the button is pushed, then the variables A & B alternate, and when the button is released, nothing more happens. Like scrolling through a menu with a single button. Again, I’m not sure if this is feasible, but it would have to be something that I could alter in-match.

As for the single user control/partner control:
I already have both of these control codes written in as user functions. When I have my partner with me, I just open up the code real quick, drag the partner control function into the Driver Control portion of EasyC, and go. Then, when she leaves, I open up the code again, switch back to single user control, compile and download, and go. I just would like to be able to make use of some of these unused buttons on the joystick, so I don’t have to open up the program every time she has to leave the room.

I’ll throw some pictures of our bot up here for reference, if someone can explain to me how.

I don’t know EasyC, but here’s how you might do something like this in RobotC. This takes two buttons to be pressed at the same time so the driver doesn’t switch modes by accidentally hitting a button. I haven’t tested this particular snippet of code, so beware of errors. It’s meant to give you the basic idea, though. Hope it helps.



int DriverMode=1;

task usercontrol()
{
	while (true)	//Creates an infinite loop
	{
		//Toggle the Driver Mode when buttons 7D AND 8D are pressed AT THE SAME TIME.....
                //If the two toggling buttons are pressed at the same time AND DriverMode is set to 1, then switch to driver mode 2...
		if(vexRT[Btn7D] && vexRT[Btn8D] && DriverMode==1)
		{
			DriverMode = 2;
			wait1Msec(1000); //Debounce buttons.
		}

               //If the two toggling buttons are pressed at the same time AND DriverMode is set to 2, then switch to driver mode 1...
		if(vexRT[Btn7D] && vexRT[Btn8D] && DriverMode==2)
		{
			DriverMode = 1;
			wait1Msec(1000); //Debounce buttons.
		}
		// End of Driver Mode toggling.*******************


           //Begin driver control based on which mode you selected up above...

		if(DriverMode == 2) //Activate this part of the code.
		{
                   //**** Stick all your driver control in here for this mode.
			}
			//**** End of DriverMode == 2 code 



		if(DriverMode == 1) //Activate this part of the code.
		{
                      //**** Stick all your driver control in here for this mode.
			}
			//**** End of DriverMode == 1 code 

	} // End of infinite While Loop keeping Cortex alive...
}// End of task main...
****************************************************



Ok… Good to know. Again, though, one easy solution to your problem would be the use of a while loop. Something similar to this:

if channel 7D
(
While (1)
{
Normal drive code
}
)
if channel 7U
(
While (1)
{
Reversed drive code
}
)
Although I do not like the idea of using a while(1) loop, it is not the worst thing. The only drawback is once you hit one of those two buttons, you are using that drive for the rest of the driver control. It’s also possible, though, I might mention, to make that a Boolean value. You could put the variable within the while loop and alter it using the controller

If (channel 5d)
{
BoolValueReverse = false
BoolValueForward = true

}
If (channel 5u)
{
BoolValueReverse = true
BoolValueForward = false
}

You would have to make the value false, getting yourself out of the while loop you are in, and the code would simultaneously switch you over to the other drive ( assuming you had put in boolvalueforward for the while loop with the forward code, and the reverse with the reversed one).

FullMetalMentor, That actually does make sense. I’d just have to figure out how translate that to EasyC, but you seem to have hit the nail on the head as far as what I want to do.

ShadowIQ, I See what you’re saying (I think). But, wouldn’t that mean that, if I want to drive, I have to hold down either channel 7D or 7U? Also, I don’t understand what you mean when you’re talking about using Boolean values. (I know Boolean/DeMorgan theorems for simplifying logic expressions, but I’m not sure how that applies…)

Also, I really appreciate the help. Sorry if I sould like a newb, it’s because I am. This is our second year competing for my school.

Ok. I played around with it for a bit. I have the While loops kind of figured out. I set it so pushing 7L or 7R at the beginning of the match determines which kind of control to use (Single user or partner). The problem with this is that I can not actively change this during a match, so it will not work for the drive train rotation that I want to do.

I also fiddled with If statements. I got it so that I can switch between the two user functions in match by pressing 7L or 7R. The problem with this is that I have to hold the button down for as long as I want that condition to be true. For obvious reasons, I can’t drive a whole match holding down 7R.

Are there any other options? Possible a combination of if and while statements? Maybe something that I’m not seeing?

(I’m not sure if I did this right, but I created integer variables (CH7L and CH7R) to put in my While/If statements. I then used the Get Joystick Digital block to send the values from the physical buttons to these variables. Essentially rendering CH7L and CH7R virtual representatives of the Channel 7 buttons L and R respectively. Not sure if this is the right way to do this, but it seems to be working, kind of.)

Here’s something that requires you to press the 5U button.


bool reverseDrive=false;
while(true)
{
	if(vexRT[Btn5U]==1 && reverseDrive==true)
	{
		reverseDrive=false;
	}
	if(vexRT[Btn5U]==1 && reverseDrive==false)
	{
		reverseDrive=true;
	}
	//drive code
	if(reverseDrive==true)
	{
		//forward drive code
	}
	else
	{
		//backward drive code
	}
}

It was written for robotc, but could be easily translated for easyc.

The code basically checks when a button is pressed. The boolean “reverseDrive” is then changed based on what its current value is. These series of if statements are placed in an infinite loop so the robot is constantly updating the value of “reverseDrive”. There is then a if-else statement to change the controls of the drive based on the value of “reverseDrive”

If you are unable to use two conditions in the if statement, then you could have an if statement within an if statement.


if(vexRT[Btn5U]==1)
{
	if(reverseDrive==true)
	{
		reverseDrive=false;
	}
	else
	{
		reverseDrive=true;
	}
}

The same if statement for the drive code would apply in the situation above.

Sorry if I did not make myself clear. You would have to put those if statements in both of the while loops for it to work, as it would simultaneously exit the while loop you were in and enter the other. In my opinion though, Full Metal Mentor’s code is much more effective, since it utilizes the structure of driver control (since driver control pretty much is a while loop)…

Thank you guys so much for your help!!! I came to a realization in my sleep, and I was logging on here to tell you that I figured it out!

I made two user functions, forward and reverse. In the forward, I retrieved 7L from the joystick and sent that to variable CH7L. In the forward user function, I put a while statement, saying that while CH7L == 1, it drives in reverse. In the reverse user function, I retrieved 7R and sent it to variable CH7R. In the function, i put a while statement, saying that while CH7R == 1, it goes to the forward control. By pressing these two buttons, I can choose forward or reverse control in real time, and as many times as I want!!!

I love EasyC!!!