1 of 39

Programming 201:�Line following, Variables and My Blocks�

Coach Series

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.

LEGO® Education SPIKETM Prime Word Blocks

1

Robot Design: Programming 201 – Coach Serries

2 of 39

Learning Objectives

  • Knowledge of proportional line following.
  • How to create and use Variables.
  • How to create and use a line following My Block.

Recommended Prerequisites:

Programming 101 – Introduction and Common Practices

Programming 101 – Basic Movement

2

Robot Design: Programming 201 – Coach Serries

3 of 39

Line Following Basics

Line following is really following the edge of a line and not the line itself. The robot must continually turn towards the edge of the line to stay on course.

Basic concept:

    • When the robot sees black (dark), turn one direction.
    • When the robot sees white (light), turn in opposite direction.
  • This causes the robot to alternate turns along the “edge” of the line where white and black meet.
    • Observed by the robot “wagging” along the edge of line.

FOLLOW THE LEFT EDGE OF LINE

  • For line following, the color sensor must be mounted in front of the robot’s wheels in the direction the robot is traveling.
  • The robot’s position in relation to the line is based on where the color sensor is mounted to the robot (left or right side). This can prevent the robot from getting close to a mission model.

3

Robot Design: Programming 201 – Coach Serries

4 of 39

Proportional Line Following

Proportion line following has four steps:

    • Establish the target value.
      • Ideally the target value should be a sensor reading of 50, halfway between black and white, rarely is it.
    • Determine what is the current sensor reading?
    • Calculate how far is the reading from the target?
      • Error is how far the sensor reading is from the target.
    • Have the robot take action to correct the error.
      • The action should be proportional to the error. The further from the target the greater the action.

4

Robot Design: Programming 201 – Coach Serries

5 of 39

Proportional Line Following

  • The light sensor measures an average of the reflected in an area.
  • The range is 100 (completely on white) to 0 (completely on black).

0 reading

50 reading

25 reading

75 reading

100 reading

Light sensor should be mounted 16 mm above the surface it is scanning

Black line with white borders (Typical FLL-C®)

5

Robot Design: Programming 201 – Coach Serries

6 of 39

Proportional Line Following

  • From the Movement group, select and drag the “set movement motors to” word block to the programming canvas and connect to the “when program starts”.
  • From the More Motors group, select and insert the “[input] set relative position to [0]” word block after the “set movement motors to [input]” block. Set to right motor.

6

Robot Design: Programming 201 – Coach Serries

7 of 39

Creating Variables

  • A variable stores information for later use in the program.
    • Variable name is set by the programmer.
  • Type of variables:
    • Variable (Stores a number or text)
      • Note: No Boolean or logic in variables
    • List (Holds a set of numbers or text [1, 2, 3, 4] or [apple, orange, peach]
  • Variables can:
    • Write – put a value into the variable for later use
    • Read – retrieve the last value written to the variable.

7

Robot Design: Programming 201 – Coach Serries

8 of 39

Creating Variables

  • Scroll down to the Variables group.
  • Select “Make a Variable”.
  • New Variable dialog displays.
  • Enter variable name. �(Recommend “Correction”)
  • Select “OK”.

NEW VARIABLE DIALOG

8

Robot Design: Programming 201 – Coach Serries

9 of 39

Creating Variables

  • The “[variable] input”, “set [variable] to [#]” and “change [variable] by [#]” are added to Variables group.
  • Create a second variable named “Distance”.

9

Robot Design: Programming 201 – Coach Serries

10 of 39

Variable Tab

Variables tab displays the current variable value

10

Robot Design: Programming 201 – Coach Serries

11 of 39

Proportional Line Following Code

  • Select the Variables group, then the “Make a Variable” button.
  • Create a variable named “Distance”.
  • From the Variables group, select and insert the “set [Variable] to [input]” word block after the “set movement motors to [input]” block. Use the dropdown to select “Distance”.
  • From the Operators group, select and insert the “* (multiply)” report block in the “Distance” variable input.

11

Robot Design: Programming 201 – Coach Serries

12 of 39

Proportional Line Following Code

  • From the Operators group, select and insert the “/ (divide)” report block in the first input of the “* (multiple)” Boolean block.
  • In the first input of the “/” reporter block enter the distance to travel in centimeters.
  • In the second input of the “/” reporter block enter the circumference of the wheels on the robot.
    • SPIKE™ Prime large wheel is 27.6 cm
    • SPIKE™ Prime small wheel is 17.5 cm

12

Robot Design: Programming 201 – Coach Serries

13 of 39

Proportional Line Following Code

  • In the second input of the “* (multiple)” reporter block enter “360”.
    • This converts the number of centimeter to travel into degrees, because the motors only measure in degrees.
  • From the Control group, select and insert the “repeat until” word block into the program.
  • From the Operators group, select and insert the “> (greater than)” Boolean block in the “repeat until” input.

13

Robot Design: Programming 201 – Coach Serries

14 of 39

Proportional Line Following Code

  • From the More Motors group, select and insert the “[motor▼] relative position ” reporter block in the “>” Boolean block first input. Use the dropdown to set motor to the right motor of the robot.
  • From the Variables group, select and insert the “Distance” reporter block in the “>” Boolean block second input.

14

Robot Design: Programming 201 – Coach Serries

15 of 39

Proportional Line Following Code

  • From the Variables group, select and insert the “set [Variable▼] to [input]” word block in the “repeat until” word block. Set to “Correction”.
  • From the Operators group, select and insert the “* (multiple)” reporter block in the “Correction” input.
  • From the Operators group, select and insert the “- (subtract)” reporter block in the first input of the “*” Boolean block.

15

Robot Design: Programming 201 – Coach Serries

16 of 39

Proportional Line Following Code

  • Enter the “target” value in the-” reporter block first input.
    • This is the value when the light sensor is centered over the edge of the black line.
  • From the Sensors group, select and insert the “[port] reflective light” reporter block into the “-” reporter block second input. Select the light sensor port of the light sensor being used.

16

Robot Design: Programming 201 – Coach Serries

17 of 39

Proportional Line Following Code

  • Enter the “correction” value in the*” reporter block second input.
    • This value is a multiple between 0.1 and 1.0, the lower the number the less the robot will “waggle”, too low and the robot will drift off the line.
  • From the Movement group, select and insert the “start moving at [input] [input] % speedin the “repeat until” word block.

17

Robot Design: Programming 201 – Coach Serries

18 of 39

Proportional Line Following Code

  • From the Operators group, select and insert the “+ (add)” report block in the “start moving at” first input.
  • In the “+” reporter block first input, enter the speed.
    • When following a line “speed kills”. Start slow and increase the speed until the robot fails to follow the line.
  • From the Variables group, select and insert the “Correction” reporter block in the “+” reporter block second input.

18

Robot Design: Programming 201 – Coach Serries

19 of 39

Proportional Line Following Code

  • From the Operators group, select and insert the “- (subtract)” report block in the “start moving at” second input.
  • In the “-” reporter block first input, enter the speed.
  • From the Variables group, select and insert the “Correction” reporter block in the “-” reporter block second input.
  • From the Movement group, select and insert the “stop moving” word block after the “repeat until” block.

19

Robot Design: Programming 201 – Coach Serries

20 of 39

My Block Overview

  • A My Block is a combination of one or more existing blocks grouped into a single block.
  • My Blocks can be use in multiple programs.
  • My Blocks can have both inputs and outputs (parameters).
  • Helps organize, simplify and the speed development of the code.
  • When should a My Block be used?
    • Whenever a group of word blocks are repeated in the program or in a different program.

20

Robot Design: Programming 201 – Coach Serries

21 of 39

Creating a My Block – Line Follower

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

21

Robot Design: Programming 201 – Coach Serries

22 of 39

Creating a My Block - Line Follower

1. Enter a “block name

Suggestion: LineFollow

22

Robot Design: Programming 201 – Coach Serries

23 of 39

Creating a My Block - Line Follower

2. Select “Add an input

Adds: Input

3. Select “Add a label

Adds: Label

23

Robot Design: Programming 201 – Coach Serries

24 of 39

Creating a My Block - Line Follower

4. Select the input area and enter “Value

5. Select the label area and enter “Target

24

Robot Design: Programming 201 – Coach Serries

25 of 39

Creating a My Block - Line Follower

7. Select “Add a label

6. Select “Add an input

25

Robot Design: Programming 201 – Coach Serries

26 of 39

Creating a My Block - Line Follower

8. Select the input area and enter “Factor

9. Select the label area and enter “Correction

26

Robot Design: Programming 201 – Coach Serries

27 of 39

Creating a My Block - Line Follower

11. Select “Add a label

10. Select “Add an input

27

Robot Design: Programming 201 – Coach Serries

28 of 39

Creating a My Block - Line Follower

12. Select the input area and enter “%”

13. Select the label area and enter “Speed

28

Robot Design: Programming 201 – Coach Serries

29 of 39

Creating a My Block - Line Follower

15. Select “Add a label

14. Select “Add an input

29

Robot Design: Programming 201 – Coach Serries

30 of 39

Creating a My Block - Line Follower

16. Select the input area and enter “CM

17. Select the label area and enter “Distance

18. Select “SAVE

30

Robot Design: Programming 201 – Coach Serries

31 of 39

Creating a My Block - Line Follower

1. LineFollow My Block is inserted in the Word Block list and on the Programming Canvas.

31

Robot Design: Programming 201 – Coach Serries

32 of 39

Creating a My Block - Line Follower

3. Select the “[motor▼] set relative position to [0]” and drag to the LineFollow My Block and connect.

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

32

Robot Design: Programming 201 – Coach Serries

33 of 39

Creating a My Block - Line Follower

  1. Clear value from “set [Distance to…] Operator” first input.
  1. Select “CM” from the LineFollow My Block and drag to the set [Distance to…] Operator” first input.

33

Robot Design: Programming 201 – Coach Serries

34 of 39

Creating a My Block - Line Follower

  1. Clear value from “set [Correction to…] Operator” first input.
  1. Select “Value” from the LineFollow My Block and drag to the set [Correction to…] Operator” first input.

34

Robot Design: Programming 201 – Coach Serries

35 of 39

Creating a My Block - Line Follower

  1. Clear value from “set [Distance to…] Operator” last input.
  1. Select “Factor” from the LineFollow My Block and drag to the set [Distance to…] Operator” last input.

35

Robot Design: Programming 201 – Coach Serries

36 of 39

Creating a My Block - Line Follower

  1. Select the “%” from the LineFollow My Block and drag to the start moving at… Operator” first inputs.
  1. Clear values from “start moving at [input] [input] Operator” first inputs.

36

Robot Design: Programming 201 – Coach Serries

37 of 39

How the My Block Works

A LineFollow My Block insert into the code pass the Target, Correction, Speed, and Distance values to the code.

37

Robot Design: Programming 201 – Coach Serries

38 of 39

Switching side of the line to follow

  • This line following My Block follows the right side of a line.
  • To follow the left side, switch Operator blocks in the “start moving at” word block.

Create two My Blocks, one to follow the left side and the other to follow the right side.

38

Robot Design: Programming 201 – Coach Serries

39 of 39

Everything is awesome!

-Emmet Joseph Brickowski

39

Robot Design: Programming 201 – Coach Serries