Hi everyone,
my fellow coder managed to code a switch for reversed drive control. Unfortunately, he wouldn´t give me his driving code for worlds, so I´m trying to reproduce his code. Any ideas how to code something like this (the back of the robot becomes the front)
So some psudo code here since I’m not on a computer that has RobotC on it.
In your driver control …
int whichWay;
whichWay=0;
while (true) {
if (button == 1)
whichWay = 1;
if (otherbutton == 1)
whichWay = 0; // toggle whichWay,
// try to not press both buttons at the same time
if (whichWay == 0) { // move with claw as the "front"
motor[left]=vexRT[ch3];
motor[right] = vexRT[ch2];
} else { // move with the shooter on the "front"
motor[left]=vexRT[ch2];
motor[right] = vexRT[ch3];
}
// rest of the code
} // end of while (true) loop
thank you so much, that might help. I`m writing code for V5.
Your while (reversed == true) loop doesn’t exit, so once it gets to be reversed it stays. Make it an If(reversed== true)
If you look at the code I posted the driver code in a if { } else { } block
Changing channel to vcs support
First of all, you need to write in competition template or you could cause problems with the tournament and/or inspections.
But to continue, in order to make a button press work to reverse drivercontrol, you would need two integer variables that the first would be used to run a specific portion of code once per button press(to change the second integer) and the second to hold a value of what action to do.
int buttonPressed = 0;
bool reverseDrive = false;
while(true){
//Checks to see if button pressed, and if so then run once
if(Controller1.ButtonL1.Pressing() && buttonPressed == 0){
buttonPressed = 1;//Sets this value to 1 to prevent this portion from running again
//Value Switch
if(reverseDrive == false) reverseDrive = true;
else if(reverseDrive == true) reverseDrive = false;
}
else if(!Controller1.L1.Pressing())//If the button is released
buttonPressed = 0;//Reset to 0 to be ran a second time
//Use outputted value
if(reverseDrive == false){
//Insert drive code 1 here
}
else{
//Insert drive code 2 here
}
}//End of while loop
Competition Template : I´ll copy the code over as soon as I´m ready, this is just for testing. Thanks for your help!
Just as a user interface, having one button to get it go one way and another button to go the other way was easier for the drivers to use. There was never a problem on knowing which way the robot was going next.
You might want to consider stopping the motors when you change the direction. Unless you are going to do the finger dance of “let go of the joysticks, then press the direction button, start pressing the joysticks again”,
I would do this:
bool button_pressed = false;
signed char dirSign = 1;
while (true) {
if(Controller.ButtonA.pressing() && !button_pressed) {
dirSign *= -1;
button_pressed = true;
}
else if (!Controller.ButtonA.pressing()) {
button_pressed = false
}
//Calculate motor power, probably using arcade joysticks
Motor_Power *= dirSign;
//Spin the motors at the speed Motor_Power
vex::task::sleep(20);
}
How you calculate the power to the motors based on the joysticks is up to you, as is how you tell them to spin. Connor’s solution is nice for readability, however, this should be more efficient since it uses multiplication rather than several comparisons.
but how would you do the variable switch with two buttons? (I just started coding this season, so I´m a bloody beginner…)
if (Controller.ButtonA.pressing() ) whichWay = 1; // toggle whichWay,
if (Controller.ButtonB.pressing() ) whichWay = 0; // try to not press both buttons at the same time
Or on the other hand you could use a non toggle button which I find easier generally just for driving. I think that this makes it easier to switch back and forth. I’m bored, so I’m going to type out all the code below. If this isn’t helpful feel free to ignore it. Thank you for letting me post .
int main ()
{
//Main driver control while loop
while(true)
{
int lspeed = Controller.Axis3.value() + Controller.Axis4.value();
int rspeed = Controller.Axis3.value() - Controller.Axis4.value();
//if the reverse button is not being pressed then run the motors normally
if(Controller.ButtonL2.pressing() == false)
{
left.spin(directionType::fwd, lspeed ,velocityUnits::pct);
right.spin(directionType::rev, rspeed ,velocityUnits::pct);
}
//If the reverse button is being pressed then run the motors backwards
if(Controller.ButtonL2.pressing() == true)
{
left.spin(directionType::rev, lspeed ,velocityUnits::pct);
right.spin(directionType::fwd, rspeed ,velocityUnits::pct);
}
}
}
I need every help I can get… Thank you very much!
This afternoon I got pretty close, switching to reversed already worked, but switching back to normal didn´t work yet, because “true” wasn´t exactly defined.
This is what I´m going to try next, “else if (reverseDrive ==true) { reversed driving code}”. The “reversed drive == true” might get the switch working…
You seem to occasionally be putting assignment operators, =
, into conditionals instead of comparison operators, ==
. I’m betting a lot of your flow control logic problems are stemming from that. To boot, you’re also making the opposite mistake in some cases, too.
Actually, one of the if...else if
blocks where you are making this transposition can be replaced with a single reverseDrive = !reverseDrive;
to handle toggling. The places where you try to ButtonRightPressed==1;
or ButtonRightPressed==0;
do need to be changed to an assignment operator =
, though.
I did it!! It´s working! Thanks everybody for your help, you are much more helpful than our other coder. But I learned a lot.
Where exactly does that need to be changed?
Is there a way of turning “reverseDrive” it to “true” with one button and "false with another button?