sora62896 wrote:so that means take out the Code: Select allif (ang > 180.0) (ang -= 360.0;)
if (ang < -180.0) (ang += 360.0;)
?
what is that suppose to do?? There are two if statements--aren't if statements suppose to reflect certain actions? and here, none are specified!!
No, the part above just limits the angle to be between +180 deg and -180 deg. Look at your code where you determine the steering correction.
Code: Select allif ((OrientationDiff >= -15) && (OrientationDiff <= 15)) //if difference ia greater or equal to -15 and less than or equal to 15
Steering.write(105); //go straight
else if ((OrientationDiff < -15) && (OrientationDiff > -215)) //if the difference is greater than 0 and less than 180
Steering.write(50); //turn left
else //anything else (if the difference is less than 0 and greater than 180
Steering.write(140); //turn right
If the difference (the heading error) between the compass reading (which is the measured heading) and the goal (the desired heading) is less than +/- some amount, the correction is 0 (you go straight). If the error is off by more than 15 deg and to the left, you issue a fixed amount of steering to the right. If the error is off by more than the 15 deg threshold and to the right, you issue a fixed amount of steering to the left. These amounts of steering don't change no matter how big the error is (they're fixed in value !).
Now look at the code fll-freak provided, especially this part ...
Code: Select all // Defining OrientationDiff as the target minus the current orientaion
OrientationDiff = NormalizeHalfCircle(GOAL - headingInt);
// Compute correction.
#define GAIN 50.0*30.0 // 50 ms servo change for 30 degree error
#define OFFSET 105.0 // Servo neutral position
correction = OrientationDiff * GAIN + OFFSET;
Note that the correction is now
proportional to the error. That's the "P" he's talking about. A small error makes a small steering correction. A large error makes a large correction. It's like what you do when you drive a car. You don't twist the wheel a fixed amount as you drift slightly offcenter in your lane. (At least I hope you don't). You turn the wheel as hard as needed, a small amount if you're slightly offcenter and more if you weren't paying attention and are now crossing into another lane !