1 of 28

Programming 201:�Using Force and Distance Sensors

LEGO® Education SPIKETM Prime Word 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.

1

Robot Design: Programming 201 Sensors – Coach Series

2 of 28

Learning Objectives

  • Familiarization with the SPIKE™ Prime Force sensor.
  • How to use the Force sensor with the Gyro sensor to control movement.
  • Familiarization with the SPIKE™ Distance sensor.
  • How to use the Distance sensor with the Gyro sensor to control movement.

Recommended Prerequisites:

Programming 101 – Introduction and Common Practices

Programming 101 – Basic Movement

Programming 201 – Line following, Variables and My Blocks

2

Robot Design: Programming 201 Sensors – Coach Series

3 of 28

SPIKE™ Sensor Overview

MEDIUM MOTOR

COLOR SENSOR

LARGE MOTOR

DISTANCE SENSOR

FORCE SENSOR

SPIKE™ HUB

GYRO SENSOR

3

Robot Design: Programming 201 Sensors – Coach Series

4 of 28

Sensors included in the core and expansion sets

Sensor

Core Set (45678)

Expansion Set (45680)

Force sensor (force, touch)

1

0

Color sensor (color, light)

1

1

Distance sensor

1

0

Medium motor (rotation)

2

0

Large Motor (rotation)

1

1

SPIKE Prime Hub (gyro, accelerometer)

1

0

https://education.lego.com/en-us/product-resources/spike-prime/downloads/technical-specifications/

4

Robot Design: Programming 201 Sensors – Coach Series

5 of 28

Force Sensor – Modes and Specifications

The three Force sensor modes are:

  • Pressed – detects a slight tap of the sensor
  • Hard-pressed – pressing the sensor in about 60%
  • Released – hold the sensor in and release it any amount

5

Robot Design: Programming 201 Sensors – Coach Series

6 of 28

Force Sensor – Modes and Specifications

  • Sensor sample rate: 100Hz
  • Touch sensing activation zone: 0 to 2 mm
  • Force sensing activation zone: 2 to 8 mm
  • Activation on force: 2.5 to 10 newtons
  • Resolution 0.1 newtons
  • Accuracy +/1 0.65 newtons

6

Robot Design: Programming 201 Sensors – Coach Series

7 of 28

Force Sensor – Pseudocode

  1. Set standard robot movement.
  2. Set % speed.
  3. Start moving straight.
  4. Wait until the Force Sensor is pressed.
  5. Stop moving.

7

Robot Design: Programming 201 Sensors – Coach Series

8 of 28

Force Sensor – Basics

  • Set standard movement:
    • Set movement motors to [input].
    • Set 1 motor rotation to [input] [cm/in] moved.
    • Set movement motors to [brake/hold position/coast at stop.
  • From the More Movement group, select and drag the “start moving at [input] [input] % power” word block to the Programming canvas and insert into the “set movement motors to” word block. Set both motor powers to 30%.

8

Robot Design: Programming 201 Sensors – Coach Series

9 of 28

Force Sensor – Basics

  • From the Control group, select and drag the “wait until [input]” word block to the Programming canvas and connect to the “start moving at [input] [input] % power” word block.
  • From the Sensors group, select and drag the “Force Sensor [port] is [pressed]?” input to the Programming canvas and insert into the “wait until” word block input.
  • Set Force Sensor “port” and set to “pressed”.

9

Robot Design: Programming 201 Sensors – Coach Series

10 of 28

Force Sensor – Basics

  • From the Movement group, select and drag the “stop movingword block to the Programming canvas and it to the “wait until word block.

Download and test your code.

10

Robot Design: Programming 201 Sensors – Coach Series

11 of 28

Control Movement with Force and Gyro Sensors

Using the Gyro sensor to move straight to the target and the Force sensor to stop at the target

Create a My Block named “ForceSensor” with three inputs and labels:

    • Input: “%”, Label: “Power”.
    • Input: “Degree”, Label: “Direction”.
    • Input: “Factor”, Label: “KP”.

1

3

2

11

Robot Design: Programming 201 Sensors – Coach Series

12 of 28

Control Movement with Force and Gyro Sensors

  • From the Control group, select and drag the “repeat until [input]” word block to the Programming canvas and connect to the “ForceSensor” My Block.
  • From the Sensors group, select and drag the “Force Sensor [port] is [pressed]?” input to the Programming canvas and insert into the “wait until” word block input. Set the sensor port.
  • Set Force Sensor settings to “port” and set to “pressed”.

12

Robot Design: Programming 201 Sensors – Coach Series

13 of 28

Control Movement with Force and Gyro Sensors

  • From the Variables group, select and drag the “set [Variable] to [input]” variable block to the Programming canvas and insert into the “repeat until” word block. Set to “Correction”.
  • From the Operator group, select and drag the “* (multiply) operator block to the Programming canvas and insert into the “Correction” variable block input.
  • From the Operator group, select and drag the “- (subtract) operator block to the Programming canvas and insert into the “* (multipy) operator block second input.

13

Robot Design: Programming 201 Sensors – Coach Series

14 of 28

Control Movement with Force and Gyro Sensors

  • From the My Block group, select the factor (KP) input and drag to the first input in the “* (multiply) operator block position 1.
  • From the My Block group, select the Degrees (Direction) input and drag to the first input in the “- (subtract) operator block.
  • From the Sensor group, select and drag the “Gyro [pitch] angle” input to the Programming canvas and insert into the “-” (subtract) operator block second position.

14

Robot Design: Programming 201 Sensors – Coach Series

15 of 28

Control Movement with Force and Gyro Sensors

  • From the More Movement group, select and drag the “start moving at [input] [input] % power” input to the Programming canvas and insert into the repeat until” operator block after the “set [Variable] to [input].
  • From the Operator group, select and drag the “+ (plus) operator block to the Programming canvas and insert into the “start moving at” variable block first input.
  • From the Operator group, select and drag the “- (subtract) operator block to the Programming canvas and insert into the “start moving at” operator block second input.

15

Robot Design: Programming 201 Sensors – Coach Series

16 of 28

Control Movement with Force and Gyro Sensors

  • From the My Block group, select the % (Power) input and drag to the first input in the “start moving at / [input] + [input] operator block position 1.
  • Repeat for the [input] - [input] operator block first position.
  • From the Variable group, select and drag the “Correction” variable input to the “start moving at [input] [input]” word block, “+” operator second position.
  • Repeat for the “-” operator block second position.

16

Robot Design: Programming 201 Sensors – Coach Series

17 of 28

Control Movement with Force and Gyro Sensors

  • From the Movement group, select and drag the “stop movingword block to the Programming canvas and it to the “wait until word block.

Write a program to test your code.

17

Robot Design: Programming 201 Sensors – Coach Series

18 of 28

Distance Sensor - Basics

  • Code is the same as the Force sensor except for the “wait until” operator block.
  • Copy the Force sensor code.
  • Delete the Force sensor operator input.
  • From the Sensors group, select and drag the “Ultrasonic Sensor [port] is [closer than] [input] [cm/in]?” input to the Programming canvas and insert into the “wait until” word block input.

Download and test your code.

18

Robot Design: Programming 201 Sensors – Coach Series

19 of 28

Distance Sensor - Description

  • The Distance Sensor measures distance to an object or surface using ultrasound. The sensor sends a high frequency sound waves that bounce off an object in range, and measuring how long it takes the sound to return to the sensor.
  • The Distance Sensor can also be used as a light source, with four LED segments around the "eyes" that can be controlled individually.
  • Distance Sensing from 50 to 2000 mm
  • Fast distance sensing from 50 to 300 mm
  • White light output around the sensor “eyes” divided into four segments – two upper and two lower segments. (Lower segments are Python only).

19

Robot Design: Programming 201 Sensors – Coach Series

20 of 28

Control Movement with Distance and Gyro Sensors

Create a My Block named “ForceSensor” with four inputs and labels:

    • Input: “Degree”, Label: “Direction
    • Input: “%”, Label: “Power
    • Input: “Factor”, Label: “KP
    • Input: “CM”, Label: “DistanceFrom

1

3

2

4

20

Robot Design: Programming 201 Sensors – Coach Series

21 of 28

Control Movement with Distance and Gyro Sensors

  • From the Control group, select and drag the “repeat until [input]” word block to the Programming canvas and connect to the “UltrasonicMove” My Block.
  • From the Sensors group, select and drag the “Distance Sensor [port] is [closer than/farther than/exactly at] [input] [cm/inch]?” input to the Programming canvas and insert in the “repeat until” word block input.

21

Robot Design: Programming 201 Sensors – Coach Series

22 of 28

Control Movement with Distance and Gyro Sensors

  • From the Variables group, select and drag the “set [Variable] to [input]” variable block to the Programming canvas and insert into the “repeat until” word block. Set to “Correction”.

  • From the Operator group, select and drag the “* (multiply) operator block to the Programming canvas and insert into the “Correction” variable block input.
  • From the Operator group, select and drag the “- (subtract) operator block to the Programming canvas and insert into the “* (multipy) operator block second input.

22

Robot Design: Programming 201 Sensors – Coach Series

23 of 28

Control Movement with Distance and Gyro Sensors

  • From the My Block group, select the factor (KP) input and drag to the first input in the “* (multiply) operator block position 1.
  • From the My Block group, select the Degrees (Direction) input and drag to the first input in the “- (subtract) operator block.
  • From the Sensor group, select and drag the “Gyro Sensor [pitch/roll/yaw] angle” input to the Programming canvas and insert into the “-” (subtract) operator block second input position. Set to “yaw”.

23

Robot Design: Programming 201 Sensors – Coach Series

24 of 28

Control Movement with Distance and Gyro Sensors

  • From the More Movement group, select and drag the “start moving at [input] [input] % power” input to the Programming canvas and insert into the “repeat until” operator block after the “set [Variable] to [input].
  • From the Operator group, select and drag the “+ (plus) operator block to the Programming canvas and insert into the “start moving at” variable block first input.
  • From the Operator group, select and drag the “- (subtract) operator block to the Programming canvas and insert into the “start moving at” operator block second input.

24

Robot Design: Programming 201 Sensors – Coach Series

25 of 28

Control Movement with Distance and Gyro Sensors

  • From the My Block group, select the % (Power) input and drag to the first input in the “start moving at / [input] + [input] operator block first position. Repeat for the [input] - [input] operator block first position
  • From the Variable group, select and drag the “Correction” input to the “start moving at [input] [input]” word block, “+” operator second position. Repeat for the “-” operator second position.

25

Robot Design: Programming 201 Sensors – Coach Series

26 of 28

Control Movement with Distance and Gyro Sensors

  • From the Movement group, select and drag the “stop movingword block to the Programming canvas and it to the “repeat until word block.

26

Robot Design: Programming 201 Sensors – Coach Series

27 of 28

Control Movement with Distance and Gyro Sensors

Write a program to test your code.

0

20

.4

5

.4 x (0 – 0) = 0

Current yaw reading is 0

20

20

.4

0

5

0

0

20 + 0 = 20

20 - 0 = 20

Both motors at same speed = robot move straight

27

Robot Design: Programming 201 Sensors – Coach Series

28 of 28

Everything is awesome!

-Emmet Joseph Brickowski

28

Robot Design: Programming 201 Sensors – Coach Series