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
Learning Objectives
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
Coding a Gyro Turn Program
3
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
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
Introduction: If blocks
5
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
6
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
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
Coding a Gyro Turn Program
8
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
1
2
9
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
1
2
10
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
1
2
11
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
1
2
12
Robot Design: Programming Gyro Turns – Coach Serries
Coding a Gyro Turn Program
13
Robot Design: Programming Gyro Turns – Coach Serries
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
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
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
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
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
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
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
Creating a My Block - GyroTurn
7. Select “Add a label”
6. Select “Add an input”
21
Robot Design: Programming Gyro Turns – Coach Serries
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
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
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
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
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
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
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
Everything is awesome!
-Emmet Joseph Brickowski
29
Robot Design: Programming Gyro Turns – Coach Serries