Inspiring SPIKE Prime Programming for FLL Part 1: Angle Wrapping and Reusable Gyro Turn My Block

For Cargo Connect season, we switched from EV3 to SPIKE Prime. We found SPIKE Prime was easier to program and build than EV3. We had a very successful season and advanced to the World Championship.

Our SPIKE Prime programs covered gyro turn, gyro PID move straight, PID line following, and parallel tasks. Along with our well designed robot, were able to consistently score the full scores during practices and 600+ points during competitions.

As we had done using EV3 hub, we created equivalent my blocks in SPIKE Prime so that we could reuse them for different launches. In this post, we will tell you step by step how to create a Gyro Turn my block in SPIKE Prime to consistently and accurately turning a robot. 

For basic concept of Gyro Turn, please check our post of EV3 Gyro Turn. For official SPIKE Prime tutorials, please check out this.

First, we need to create a SPIKE Prime project as shown below:

Install SPIKE Prime Extensions

Our Gyro Turn my block uses the extended block. The following screenshots show the steps.

SPIKE Prime Hub Yaw Angle Reading

In EV3, the yaw angle reading is continuous. However, in SPIKE Prime, the yaw angle becomes negative when the angle reads over 180° as shown in the video below. That is, SPIKE Prime’s yaw angle reading is wrapped to the range of [-179° 180°].

For example, when the robot rotates from yaw angle reading of 170 degree to -170 degree shown in the following diagram, the rotation angle is 20 degrees. However, the difference between two yaw angle readings will be -340 degrees (-170 – 170). This result can not be used directly to turn our robot.

Wrap an angle

The angle wrapping methods is simple: 

If an angle is less than -179, add by 360, else if an angle is great than 180, minus by 360, otherwise, no change.

Create SPIKE Prime Variables

We will create a variable named “startYawAngle” to store the yaw angle reading just before rotation and “delta” to store the difference of current yaw angle reading and “startYawAngle”.  If “delta” exceeds the rotation degrees, Gyro Turn completes.

Click “VARIABLES'” menu on the left side then click “Make a Variable” button as shown below:

In the popup window, enter variable name then click “OK” button.

By repeating the above steps twice, the two newly created variables will be listed as shown below.

Create the Gyro Turn My Block

Now, we will create a My Block.

Lets implement the “gyro turn” My Block:

Use the My Block

Gyro Turn my block can be used for both pivot turn and spin turn. The following is an example of how to turn a robot using Gyro Turn my block. Note that, between these two turns, there is no need to reset yaw angle to 0 which could be very useful to have consistent global heading.