1. 2 weeks ago

    abennett5139

    Jan 4 Concord, NC 5139

    When working with a gyro code that helps with veering and trying to integrate a sonar to it so it stops when it reaches the sonar value but instead the robot drives forward a little bit and then proceeds on with the rest of the code. If you could help me figure out why that is that'd would be amazing!

    Here's the function

    void DriveREVforCMGyroSonar (double TravelTarget, int Velocity)
    {
        //Formula to convert velocity percent to rpm
        Velocity *= 2;
       
        Brain.Screen.clearScreen();
        
        if ((Sonar.distance(distanceUnits::cm)) > TravelTarget)
        {
            while (Gyro.value(rotationUnits::deg) < -0.01)
            {
                FL_Drive.spin(directionType::rev,Velocity*0.75,velocityUnits::rpm);
                FR_Drive.spin(directionType::rev,Velocity*0.71,velocityUnits::rpm);
                BL_Drive.spin(directionType::rev,Velocity*0.75,velocityUnits::rpm);
                BR_Drive.spin(directionType::rev,Velocity*0.71,velocityUnits::rpm);
                
                Brain.Screen.print("Sonar value in cm is %f",Sonar.distance(distanceUnits::cm));
                Brain.Screen.newLine();
                task::sleep(125);
            }
            
            while (Gyro.value(rotationUnits::deg) > 0.01)
            {
                FL_Drive.spin(directionType::rev,Velocity*0.735,velocityUnits::rpm);
                FR_Drive.spin(directionType::rev,Velocity*0.75,velocityUnits::rpm);
                BL_Drive.spin(directionType::rev,Velocity*0.735,velocityUnits::rpm);
                BR_Drive.spin(directionType::rev,Velocity*0.75,velocityUnits::rpm);
                
                Brain.Screen.print("Sonar value in cm is %f",Sonar.distance(distanceUnits::cm));
                Brain.Screen.newLine();
                task::sleep(125);
            }
            
            while ( Gyro.value(rotationUnits::deg) > -0.01 &&  Gyro.value(rotationUnits::deg) < 0.01)
            {
                FL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                FR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                BL_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                BR_Drive.spin(directionType::rev,Velocity*.75,velocityUnits::rpm);
                
                Brain.Screen.print("Sonar value in cm is %f",Sonar.distance(distanceUnits::cm));
                Brain.Screen.newLine();
                task::sleep(125);
            }
            task::sleep(50);
        }
            
        else
        {
            task::sleep(100);
        
            FL_Drive.stop();  
            FR_Drive.stop();
            BL_Drive.stop();  
            BR_Drive.stop();
        }
    }

    You only checked the sonar a single time at the very beginning. Plus, once originally aligning (the first two while loops), the code will only keep going so long as the gyro robot stays on target. As soon as it goes off-target the code ends. But in the end you never tell it to stop so the robot will just keep running of the program hasn't ended. Your whole if-then and while loop structure needs to be reconsidered. Essentially, your if should be a while and your whiles should be ifs.

  2. callen

    Jan 4 Answer Braintree, MA, USA

    You only checked the sonar a single time at the very beginning. Plus, once originally aligning (the first two while loops), the code will only keep going so long as the gyro robot stays on target. As soon as it goes off-target the code ends. But in the end you never tell it to stop so the robot will just keep running of the program hasn't ended. Your whole if-then and while loop structure needs to be reconsidered. Essentially, your if should be a while and your whiles should be ifs.

 

or Sign Up to reply!