Help with programming, function stops working after one use?


#1

So I’m working on auton functions, and for some reason, void move stops working if it was used first or if there was a function before it. I can’t figure out what it is.

Heres the code:

void baseEncoderReset(){                                        //resets all the encoders on the base
    
    FrontRight.resetRotation ();
    FrontLeft.resetRotation  ();
    MidRight.resetRotation   ();
    MidLeft.resetRotation    ();
    RearRight.resetRotation  ();
    RearLeft.resetRotation   ();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int gyroTargDeg = 0;
void turn_right(double deg_to_rotate) {
    
    gyroTargDeg = deg_to_rotate * 10;
    
    baseEncoderReset();
    int rotation = deg_to_rotate * 3;        //3 is the amount the robot turns per degree
    
    gyroTargDeg += Gyro.value(vex::analogUnits::range12bit);
    
    FrontRight.rotateTo(-rotation, vex::rotationUnits::deg, 100, vex::velocityUnits::pct, false);
    FrontLeft.rotateTo(rotation, vex::rotationUnits::deg,   100, vex::velocityUnits::pct, false);
    MidRight.rotateTo(-rotation, vex::rotationUnits::deg,   100, vex::velocityUnits::pct, false);
    MidLeft.rotateTo(rotation, vex::rotationUnits::deg,     100, vex::velocityUnits::pct, false);
    RearRight.rotateTo(-rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct, false);
    RearLeft.rotateTo(rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct);

}

void turn_left(double deg_to_rotate) {
    
    gyroTargDeg = deg_to_rotate * 10;
    
    baseEncoderReset();
    int rotation = deg_to_rotate * 3;
    
    gyroTargDeg += Gyro.value(vex::analogUnits::range12bit);
    
    FrontRight.rotateTo (rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct, false);
    FrontLeft.rotateTo  (-rotation, vex::rotationUnits::deg, 100, vex::velocityUnits::pct, false);
    MidRight.rotateTo   (rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct, false);
    MidLeft.rotateTo    (-rotation, vex::rotationUnits::deg, 100, vex::velocityUnits::pct, false);
    RearRight.rotateTo  (rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct, false);
    RearLeft.rotateTo   (-rotation, vex::rotationUnits::deg,  100, vex::velocityUnits::pct);

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int current_Velocity = 10;
double in_the_course_of = 1;
int powIncrement = 5;
int powDecrement = 10;

int timeFunc(){
    
    in_the_course_of *= 1000;
    int time = in_the_course_of / (100 / powIncrement);
    return time;
}

int slew(int end_Velocity, int time) {

    if (current_Velocity < end_Velocity) {

        current_Velocity += powIncrement;
        wait(time);
    }
    return current_Velocity;
}

int decelarate(int time){
    
    if(current_Velocity > 5){
        
        current_Velocity -= powDecrement;
        wait(time);
    }
    return current_Velocity;
}

double degrees_to_moveFunc(double dist){
    
    dist *= 2.54;
    double deg_to_move =  (dist) / circumfrence; 
    return deg_to_move;
}

int error = 30;
int slow = 90;

void move(double dist, int targSpeed) {

    baseEncoderReset();
    bool slewFin = false;
    int time = timeFunc();
    current_Velocity = 10;
    if(dist > 0){

        while (degrees_to_moveFunc(dist) > MidRight.rotation(vex::rotationUnits::rev)) {
            
            if(Gyro.value(vex::analogUnits::range12bit) < gyroTargDeg - error || Gyro.value(vex::analogUnits::range12bit) > gyroTargDeg + error){
                
                if(Gyro.value(vex::analogUnits::range12bit) < gyroTargDeg - error && slewFin){
                    
                    FrontRight.spin (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                    FrontLeft.spin  (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    MidRight.spin   (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                    MidLeft.spin    (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    RearRight.spin  (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                    RearLeft.spin   (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    
                }else if(Gyro.value(vex::analogUnits::range12bit) > gyroTargDeg + error && slewFin){
                    
                    FrontRight.spin (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    FrontLeft.spin  (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                    MidRight.spin   (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    MidLeft.spin    (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                    RearRight.spin  (vex::directionType::fwd, 100, vex::velocityUnits::pct);
                    RearLeft.spin   (vex::directionType::fwd,slow, vex::velocityUnits::pct);
                }
            }else{
                
                FrontRight.spin (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                FrontLeft.spin  (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                MidRight.spin   (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                MidLeft.spin    (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                RearRight.spin  (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                RearLeft.spin   (vex::directionType::fwd, slew(targSpeed, time), vex::velocityUnits::pct);
                
            }
            
            if(MidRight.velocity(vex::velocityUnits::pct) > targSpeed - 5){

                slewFin = true;
            }
        }
    }else if(dist < 0){
        
        while (degrees_to_moveFunc(dist) < FrontRight.rotation(vex::rotationUnits::deg)) {
            
            if(Gyro.value(vex::analogUnits::range12bit) < gyroTargDeg - error || Gyro.value(vex::analogUnits::range12bit) > gyroTargDeg + error){

                if(Gyro.value(vex::analogUnits::range12bit) < gyroTargDeg - error){
                    
                    FrontRight.spin (vex::directionType::rev, 100, vex::velocityUnits::pct);
                    FrontLeft.spin  (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    MidRight.spin   (vex::directionType::rev, 100, vex::velocityUnits::pct);
                    MidLeft.spin    (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    RearRight.spin  (vex::directionType::rev, 100, vex::velocityUnits::pct);
                    RearLeft.spin   (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    
                }else if(Gyro.value(vex::analogUnits::range12bit) > gyroTargDeg + error){
                    
                    FrontRight.spin (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    FrontLeft.spin  (vex::directionType::rev, 100, vex::velocityUnits::pct);
                    MidRight.spin   (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    MidLeft.spin    (vex::directionType::rev, 100, vex::velocityUnits::pct);
                    RearRight.spin  (vex::directionType::rev,slow, vex::velocityUnits::pct);
                    RearLeft.spin   (vex::directionType::rev, 100, vex::velocityUnits::pct);
                }
            }else{
                
                FrontRight.spin (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
                FrontLeft.spin  (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
                MidRight.spin   (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
                MidLeft.spin    (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
                RearRight.spin  (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
                RearLeft.spin   (vex::directionType::rev, slew(targSpeed, time), vex::velocityUnits::pct);
            }
        }
    }
    
    /*while(current_Velocity > 5){
        
        FrontRight.setVelocity  ( decelarate(time), vex::velocityUnits::pct);
        FrontLeft.setVelocity   ( decelarate(time), vex::velocityUnits::pct);
        MidRight.setVelocity    ( decelarate(time), vex::velocityUnits::pct);
        MidLeft.setVelocity     ( decelarate(time), vex::velocityUnits::pct);
        RearRight.setVelocity   ( decelarate(time), vex::velocityUnits::pct);
        RearLeft.setVelocity    ( decelarate(time), vex::velocityUnits::pct);
    }*/
    
    FrontRight.stop ();
    FrontLeft.stop  ();
    MidRight.stop ();
    MidLeft.stop  ();
    RearRight.stop  ();
    RearLeft.stop   ();
}

In the auton, when I do move(72,100); as a first function, it works.
But if I put anything else before it, it doesn’t.
It also can’t do two move functions in a row.
I’ve been looking at it for a while, and I can’t figure out what’s wrong.

If you have any questions about this program, ask.

Thank you.


#2

Looks like you need to reset in_the_course_of back to 1 after use of timeFunc


#3

Yes, i dint catch that, thank you. It still doesn’t work tho, even if i keep in the course of a constant


#4

Should there be an extra } in front of this else or should there be only 1?


#5

What are the “anything else” that you put in front of move? turn_right and turn_left? Have you checked if either works after being put after the other? You might include some debugging commands like printing to the Brain screen so you can see exactly where it’s getting hung up. Even just changing it different colors for different functions/milestones in the program could be enough.


#6

The turns work fine, its just the move that doesnt reset