1 of 29

Programming 201:�Gyro Turns

Coach Series

LEGO® Education SPIKETM Prime Word Blocks

Disclaimer: This private training is provided by the North Texas Region and is not affiliated or operated by the LEGO® Group or by FIRST ®. The LEGO Group® and FIRST® are not responsible for any of its activities.

v. 3.2.4

v. 2.0.10

IN DEVELOPMENT

1

Robot Design: Programming Gyro Turns – Coach Serries

2 of 29

Learning Objectives

  • Code a Gyro Turn and a GyroTurn My Block

Recommended Prerequisites:

Programming 101 – Introduction and Common Practices

Programming 101 – Basic Movement

Programming 201 – Line following, Variables and My Blocks

2

Robot Design: Programming Gyro Turns – Coach Serries

3 of 29

Coding a Gyro Turn Program

  • Start a new project.
    • Select Home icon.
    • Get Started with SPIKE™ Prime Window displays.
    • Select New Project icon.
    • New Project canvas displays.
  • In toolbar select three dots to rename the project.
  • Change “storage position” to avoid overwriting previous project.

3

Robot Design: Programming Gyro Turns – Coach Serries

4 of 29

Coding a Gyro Turn Program

  • Copy and past the set movement word blocks from your previous project.
  • Programming canvas and connect above the “set movement motors to [brake] at stop” word block.
  • From the Sensor group select and drag the “set yaw angle to 0
  • output word block to the From the Control group select the “wait for [1] second” word block. Set to 0.1 second.

The set yaw angle to 0 output word block resets the reading to zero. It is not a calibration function.

4

Robot Design: Programming Gyro Turns – Coach Serries

5 of 29

Introduction: If blocks

  • If / Then blocks, ask the robot a question and do something different based on the answer.
  • If / Then ask if [something] is true, the do this.
  • If / Then / Else, ask if [something] is true, then do this, if it is false do that.

  • Examples: Does the color sensor see “black”? Is a value greater than?

5

Robot Design: Programming Gyro Turns – Coach Serries

6 of 29

Coding a Gyro Turn Program

  • From the Control group select and drag the “if, then, else” word block to the Programming canvas and connect to the “wait” word block.
  • From the Operators group select and drag the “> (greater than)” Boolean block to the Programming canvas and insert into the “if, then, else” word block.
  • Set the values to “90 > 0”.

6

Robot Design: Programming Gyro Turns – Coach Serries

7 of 29

Coding a Gyro Turn Program

  • From the More Movement group select and drag the “start moving at [#] {#] % speed” word block to the Programming canvas and insert into the if branch of the “if, then, else word block. Set to % speed to 20 and -20.
  • From the More Movement group select and drag the “start moving at [#] {#] % speed” word block to the Programming canvas and insert into the else branch of the “if, then, else word block. Set to % speed to -20 and 20.

Alternative: Right click on the “start moving at [#] {#] % speed” word block and select Duplicate. Drag the word block into the “else” branch and release. Change the values as required.

7

Robot Design: Programming Gyro Turns – Coach Serries

8 of 29

Coding a Gyro Turn Program

  • From the Control group select and drag the “wait until” word block to the Programming canvas and connect above the “if, then, else” word block.
  • From the Operators group select and drag the “or” Boolean block to the Programming canvas and insert into the “wait until” word block.

8

Robot Design: Programming Gyro Turns – Coach Serries

9 of 29

Coding a Gyro Turn Program

  1. From the Operators group select and drag the “[input] = (100)” Boolean block to the Programming canvas and insert into the “or” operator block position 1.
  2. From the Operators group select and drag the [input] > (100)reporter block to the Programming canvas and insert into the “or” operator block position 2.

1

2

9

Robot Design: Programming Gyro Turns – Coach Serries

10 of 29

Coding a Gyro Turn Program

  1. From the Operators group select and drag the “abs of [input]” reporter block to the Programming canvas and insert into the�“= (equals)” operator block first input.
  2. From the Operators group select and drag the abs of [input]” reporter block to the Programming canvas and insert into the�“> (greater than)” operator block first input.

1

2

10

Robot Design: Programming Gyro Turns – Coach Serries

11 of 29

Coding a Gyro Turn Program

  1. From the Sensor group select and drag the “pitch angle” reporter block to the Programming canvas and insert into the “abs of [input]” in the “equal” operator block position 1. Change output to “yaw”.
  2. From the Sensor group select and drag the “pitch angle” reporter block to the Programming canvas and insert into the “abs of [input]” in the “greater than” operator block position 1. Change output to “yaw”.

1

2

11

Robot Design: Programming Gyro Turns – Coach Serries

12 of 29

Coding a Gyro Turn Program

  1. From the Operators group select and drag the “abs of [input]” reporter block to the Programming canvas and insert into the “= (equals)” operator block “=“ input. Set “ofvalue to 90.
  2. From the Operators group select and drag the abs of [input] = (#)” reporter block to the Programming canvas and insert into the “> (greater than)” operator block “>“ input. Set “of” value to 90.

1

2

12

Robot Design: Programming Gyro Turns – Coach Serries

13 of 29

Coding a Gyro Turn Program

  • From the Movement group select and drag the “stop moving” word block to the Programming canvas and connect to the “wait until” block to your program.

13

Robot Design: Programming Gyro Turns – Coach Serries

14 of 29

Gyro Turn Breakdown

Resets gyro yaw to zero

If” the greater than statement is true, “then”, if not “else

Then: Turn robot to right (positive degrees, clockwise)”

Else: Turn robot to left (negative degrees, counter-clockwise)

Degrees to turn

abs = absolute value from zero

Read the yaw value from gyro

Set Degrees to turn

Equals

or

Greater than

Set Degrees to turn

wait until = absolute value of the yaw reading is equal to or greater than the set degrees to turn

Wait 1/10 of second for gyro to reset

14

Robot Design: Programming Gyro Turns – Coach Serries

15 of 29

Looking at the Math for the Gyro Turn

If degrees are greater than zero (positive) start moving clockwise (right)

wait until the absolute value of the gyro yaw is equal or greater than the degrees set.

stop moving

If the current yaw reading is 89.6 then:

abs value of 89.6 is not equal to or greater than the abs value 90 = keep turning.

If the current yaw reading is 90.1 then:

abs of 90.1 is equal to or greater than 90 = stop turning.

If degrees are less than zero (negative) start moving counterclockwise (left)

15

Robot Design: Programming Gyro Turns – Coach Serries

16 of 29

Gyro Turn Drawbacks

Must enter degrees to turn in three places

Must enter % speed in four places remembering to reverse the positive and negative.

A My Block simplifies input and allows the use of gyro turns over and over without entering the code every time.

16

Robot Design: Programming Gyro Turns – Coach Serries

17 of 29

Creating a My Block – GyroTurn

From the My Blocks group, select the “Make a Block” button. The Make a Block window displays.

17

Robot Design: Programming Gyro Turns – Coach Serries

18 of 29

Creating a My Block - GyroTurn

1. Enter a “block name

Suggestion: GyroTurn

Tip: Do not click outside of the My Block dialog. This will close it without saving.

18

Robot Design: Programming Gyro Turns – Coach Serries

19 of 29

Creating a My Block - GyroTurn

2. Select “Add an input

Adds: Input

3. Select “Add a label

Adds: Label

19

Robot Design: Programming Gyro Turns – Coach Serries

20 of 29

Creating a My Block - GyroTurn

4. Select the input area and enter “Degrees

5. Select the label area and enter “Degrees

20

Robot Design: Programming Gyro Turns – Coach Serries

21 of 29

Creating a My Block - GyroTurn

7. Select “Add a label

6. Select “Add an input

21

Robot Design: Programming Gyro Turns – Coach Serries

22 of 29

Creating a My Block - GyroTurn

8. Select the input area and enter “%

9. Select the label area and enter “Speed

10. Select “SAVE

22

Robot Design: Programming Gyro Turns – Coach Serries

23 of 29

Creating a My Block - GyroTurn

11. GyroTurn My Block is inserted in the Word Block list and on the Programming Canvas.

23

Robot Design: Programming Gyro Turns – Coach Serries

24 of 29

Creating a My Block - GyroTurn

13. Select the “set yaw angle to 0” and drag to the GyroTurn My Block and connect.

12. Select and drag the GyroTurn My Block to an open position on the Programming Canvas.

24

Robot Design: Programming Gyro Turns – Coach Serries

25 of 29

Creating a My Block - GyroTurn

14. Select “Degrees” from the GyroTurn My Block and drag to the “If loop” first input and both “asb of” inputs.

25

Robot Design: Programming Gyro Turns – Coach Serries

26 of 29

Creating a My Block - GyroTurn

15. Select “%” from the GyroTurn My Block and drag to the “if/then statement, start moving at” first input and the “if/eles statement start moving at” second input.

16. From the Operators group, select and drag the “multiply (*)” block to the Programming canvas and insert into the “if/then, start moving at” block second input position and the “if/eles, start moving at” block first input position.

26

Robot Design: Programming Gyro Turns – Coach Serries

27 of 29

Creating a My Block - GyroTurn

18. In both “multiply (*)” blocks second position enter “-1”.

Note: This allow the speed to be entered once in the My Block input and converted to negative where needed to make the robot turn.

17 Select “%” from the GyroTurn My Block and drag to the “if/then, start moving at, multiply operator” and the “if/eles, start moving at, multiply operator” first inputs.

27

Robot Design: Programming Gyro Turns – Coach Serries

28 of 29

Using the GyroTurn My Block

Select the “GyroTurn” My Block and drag to the programming canvas and connect. Enter the degrees and speed for the turn.

This example has the robot moving forward 30 cm, turning right, moving forward 30 cm, and turning left.

28

Robot Design: Programming Gyro Turns – Coach Serries

29 of 29

Everything is awesome!

-Emmet Joseph Brickowski

29

Robot Design: Programming Gyro Turns – Coach Serries