Snap Lesson 13: Gyro Turn
In this lesson we will learn about how to control the CoDrone’s turning better

We will use the CoDrone’s gyro that we learned about last lesson to control the turning of the CoDrone. We do this because up until this point we have only used time to control our autonomous turning, and as you may have noticed that is not the most reliable turning system since if you have a low battery you will turn slower than with a full battery. If the CoDrone spins at different speed, but for the same amount of time you will end up turning a different distance. By using the gyro we control the angle we are turning to, rather than how long we are turning, which is much more reliable.

For this program we are going to make the CoDrone turn towards 90 degrees. We are going to give it a small target range as it take a little bit to receive the gyro angle which makes it difficult to arrive at an exact angle. We will use a repeat until loop to have it turn until we get to the right angle.

Again we will save the ATTITUDE_YAW to a variable to make it so we only have to read the gyro sensor once. We will add in the code to make the CoDrone turn next inside of the repeat until loop. Once the CoDrone is between 85 and 95 degrees the CoDrone can stop turning and will exit the loop.

For making the CoDrone turn we will check the direction that it is facing and turn towards 90 degrees. By using this if statement we can make the gyro turns more efficient since if we were only a few degrees off, but our turn direction was the opposite way, the CoDrone would have to almost make a full circle to face the right direction. After the repeat until loop ends we will set the yaw to 0 so that the CoDrone will stop spinning.

Now adding in a takeoff and landing commands we have a full code. No matter which way the CoDrone faces it will turn towards 90 degrees.

Activity: Gyro Turning

The challenge is to have the CoDrone takeoff and turn towards the 4 main directions (0, 90, 180, 270)

Challenge Rules:

1. The CoDrone will takeoff, turn towards the next direction
2. Once it faces the proper direction have it wait 3 seconds
3. Then turn to the next direction until all 4 directions have been faced