Hi! we are working on an autonomous code right now, but when we click run, our autonomous gets skipped over by our driver control. Some help would be appreciated.
here’s the code:
int main(){
while (1==1){
if(Controller1.ButtonX.pressing()) {
//drive to goal
driveFWD(21);
//turn towards goal
turnLT(90);
//push ball into goal
driveFWD(17);
driveBWD(19);
// square around triball
turnRT(90);
driveFWD(38);
turnRT(90);
driveFWD(20);
turnRT(90);
driveFWD(12);
turnRT(90);
//push triball into goal
driveFWD(30);
}
}
while (true){
if (Controller1.ButtonB.pressing()){
while (1==1){
bool toggleEnabled = false;
bool buttonPressed = false;
//Direction only needs to be set once.
rightMotor1.spin(forward);
rightMotor2.spin(forward);
leftMotor1.spin(forward);
leftMotor2.spin(forward);
while(1)
{
rightMotor1.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27, percent);
leftMotor1.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27,percent);
leftMotor2.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27, percent);
rightMotor2.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27,percent);
rightMotor1.spin(forward);
rightMotor2.spin(forward);
leftMotor1.spin(forward);
leftMotor2.spin(forward);
bool buttonA = Controller1.ButtonA.pressing();
if (buttonA && !buttonPressed){
buttonPressed = true;
toggleEnabled = !toggleEnabled;
}
else if (!buttonA) buttonPressed = false;
if(toggleEnabled){
wings.set(true);
//what toggled
}
else{
//not toggle
wings.set(false);
}
// Note the last if statement block could be replaced with
// wings.set(toggleEnabled)
wait(20, msec);
}
}
}
}
}
Edit: Code tags added by mods, please remember to use them
The program should only have one while (true) statement. Once code enters a while loop with no escape it will stay in that loop forever.
Create one outside while loop for the whole program and use if statements to enable the logic.
int main(){
while (true) {
if(Button1.pressing()){
// Do this subset of code
motor1.setVelocity(100);
}
if(Button2.pressing() {
// Do Something else
motor1.stop();
}}
if(something) {
// Do this
}
else {
// Do the other thing
}
}
}
like this??
when we did this, the robot still refused to do the autonomous upon clicking the button. it would go forward and then just stop. the drive code works.
int main(){
bool toggleEnabled = false;
bool buttonPressed = false;
wings.set(true);
while (1==1){
//auton
if(Controller1.ButtonX.pressing()) {
//drive to goal
driveFWD(10.5);
//turn towards goal
turnLT(90);
//push ball into goal
driveFWD(17);
driveBWD(19);
// square around triball
turnRT(90);
driveFWD(38);
turnRT(90);
driveFWD(20);
turnRT(90);
driveFWD(12);
turnRT(90);
//push triball into goal
driveFWD(30);
}
//drive
if (Controller1.ButtonB.pressing()){
//Direction only needs to be set once.
rightMotor1.spin(forward);
rightMotor2.spin(forward);
leftMotor1.spin(forward);
leftMotor2.spin(forward);
rightMotor1.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27, percent);
leftMotor1.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27,percent);
leftMotor2.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27, percent);
rightMotor2.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27,percent);
//toggles for wings
bool buttonA = Controller1.ButtonA.pressing();
if (buttonA && !buttonPressed){
buttonPressed = true;
toggleEnabled = !toggleEnabled;
}
else if (!buttonA) buttonPressed = false;
if(toggleEnabled){
wings.set(true);
//what toggled
}
else{
//not toggle
wings.set(false);
}
// Note the last if statement block could be replaced with
// wings.set(toggleEnabled)
wait(20, msec);
}
//else
else {
rightMotor1.stop();
rightMotor2.stop();
leftMotor1.stop();
leftMotor2.stop();
}
}
}
First, you need to format your code when posting in the forum. Add three ticks (```) before and after your text when you post so it formats it properly. It makes it much easier for everyone to read.
Code Review
Let’s put the rest of your code into functions. This will make it way easier to read and maintain. It looks like you have already created a few with driveFWD(), etc.
C++ has Events which make calling the functions much easier. They will also handle the issue with toggles.
Put your Auton in a function.
void MyAuton(){
//drive to goal
driveFWD(10.5);
//turn towards goal
turnLT(90);
//push ball into goal
driveFWD(17);
driveBWD(19);
// square around triball
turnRT(90);
driveFWD(38);
turnRT(90);
driveFWD(20);
turnRT(90);
driveFWD(12);
turnRT(90);
//push triball into goal
driveFWD(30);
}
int main(){
//Direction only needs to be set once.
rightMotor1.spin(forward);
rightMotor2.spin(forward);
leftMotor1.spin(forward);
leftMotor2.spin(forward);
// Register your auton and wings events to the buttons (before the while loop)
Controller1.ButtonX.pressed(MyAuton);
Controller1.ButtonA.pressed(FlapWings);
FlapWings(); //If you want them to automatically open at the start.
while (true){
rightMotor1.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27, percent);
leftMotor1.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27,percent);
leftMotor2.setVelocity((Controller1.Axis3.position()+Controller1.Axis1.position())/1.27, percent);
rightMotor2.setVelocity((Controller1.Axis3.position()-Controller1.Axis1.position())/1.27,percent);
wait(20, msec);
}
}
Note that if you put your code into a competition template, there are already function events for Auton and Driver Control.