Robot doesn't stop turning

In Autonomous we call for the robot to turn. But it just turns without stopping, so we put the code in a separate Task, called after setting a trigger integer, in the auto portion of main.cpp, that the task reads, to better ensure that the GPS3.heading(degrees) information was being updated.

We know the Brain is getting the GPS3.heading(degrees) data, because it is being printed out on Controller1’s screen real time.

At first we thought maybe the Robot blows right past the Heading and thus doesn’t stop because it now isn’t at the heading. So the code presently has a very wide range of headings within which Drivetrain should stop.
Tried inserting GPS3.value (); to see if that helped, but no. Also tried Bool (True) as the last element of the
Drivetrain.turnToHeading(315, degrees, 100, dps) but that didn’t help. It seems we are missing a method to cause this code to update the GPS data real time. Here is the code. Both the turnFor and the turnTo Heading have the same issue. Thoughts?

#include "declarations.h"

int TurnTo1 () {
  while (true) {

if (gTurnTrgr == 315 && ((GPS3.heading(degrees) < 300) 
                       || (GPS3.heading(degrees) > 325)) ){
  gHeading = round(GPS3.heading(degrees));
Drivetrain.turnToHeading(315, degrees, 100, dps);

if (GPS3.heading(degrees)>200 && GPS3.heading(degrees) < 360){
  Drivetrain.stop ();
  gTurnTrgr = 0;

 return 0;

Further to above, we did try switching the Drivetrain motors. and the following code also just has it turning endlessly. What are we missing?

int time;
while (time < 3){
wait (1,sec);
Drivetrain.stop ();

Did you link the GPS to your drivetrain? If so, you should be able to just use the following in your autonomous code without any additional logic or loops.

Drivetrain.turnToHeading(315, deg);

To get the heading you would need:

float robotHeading;

robotHeading = DrivetrainGPS.heading();

It’s hard to tell from the formatting and looking at it on my phone, but that task yield and sleep need to be moved inside the while(true) loop, or they’ll never get called, and the loop may not allow enough time for the inertial to get updated.

Thanks for all input, we did solve this by adding the bool “false” as the last parameter of driveFor. We have found that the GPS isn’t sufficiently precise to work with Drivetrain.turnToHeading(315, deg);