My team want’s to use the gyro to keep the base straight when driving.
I almost have it working except for turning, for that I figured I wanted the gyro to not reset when it gets to 360. This is what we have now. It’s a mess and only works as long as the gyro value is positive.
There isn’t a good way to have the gyro value accumulate because just checking if the value jumped from round 360 to 0 could result in the value being off if the robot turns too fast. Since you said that you need to…
I would assume that you would need an error angle of how much to turn to get to the a target angle. This isn’t exactly what you asked for, but I think it would work better in this situation…
Simply subtracting the current position from the target position will give you the angle you need to turn. This however is sometimes not the most optimal angle. We know that no angle is more than half a rotation way so the program checks to make sure and if it isn’t it adds or subtracts 360. This will result in the shortest angle (positive is one way, negative is the other). This will cover the issue of gyro values going from 360 to 0 and even let you set targetAngle to angles greater than 360 or less than 0.
We can also shorten the code and make it more efficient…
The braces aren’t required. All it means is that only the first statement will be read as part of the loop. If, for example I had:
if(x == sentinel)
x = 0;
sentinel = x;
Despite the indentation here, only that first line (x=0;) is read as part of the if statement. So, one line conditionals don’t need brackets, due to the single statement assumption. If you plan on adding more to a conditional though just be wary.