pxt-ev3/docs/tutorials/line-following.md

120 lines
3.7 KiB
Markdown
Raw Normal View History

2018-04-05 22:37:24 +02:00
# Line Following
## Introduction @unplugged
2018-04-05 22:37:24 +02:00
2018-06-12 15:46:44 +02:00
Make your @boardname@ robot follow a line using the Color Sensor's Reflected Light Intensity Mode.
2018-04-05 22:37:24 +02:00
![Brick with color sensors tracking a yellow line](/static/tutorials/line-following/line-following.gif)
## Step 1
In the ``||logic:Logic||`` Toolbox drawer under the **Conditionals** section, drag out an ``||logic:If then else||`` block onto the Workspace, and drop it into the ``||loops:forever||`` loop.
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (true) {
} else {
}
2018-04-05 22:37:24 +02:00
})
```
## Step 2
Open the ``||logic:Logic||`` Toolbox drawer again. From the **Comparison** section, drag out ``||logic:0 < 0||`` comparison block and drop it into the ``||logic:if then else||`` block, replacing ``true``.
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (0 < 0) {
} else {
}
2018-04-05 22:37:24 +02:00
})
```
## Step 3
2018-06-12 15:46:44 +02:00
Open the ``||sensors:Sensors||`` Toolbox drawer. Drag out a ``||sensors:color sensor light||`` value block and drop it into the second slot of the ``||logic:0 < 0||`` comparison block, replacing the second `0`.
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (0 < sensors.color3.light(LightIntensityMode.Reflected)) {
} else {
}
2018-04-05 22:37:24 +02:00
})
```
## Step 4
2018-06-12 15:46:44 +02:00
If the value of the reflected light is greater than 40% (white or very light), our robot is outside the line, so steer to the left. In the ``||logic:0 < 0||`` comparison block change the first compared value from `0` to `40`.
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (40 < sensors.color3.light(LightIntensityMode.Reflected)) {
} else {
}
})
2018-04-05 22:37:24 +02:00
```
## Step 5
2018-06-12 15:46:44 +02:00
Open the ``||motors:Motors||`` Toolbox drawer. Drag out **2** ``||motors:tank motors||`` blocks and drop one of them into the ``||logic:if||`` part, and the other into the ``||logic:else||`` part of the ``||logic:if then else||`` block.
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (40 < sensors.color3.light(LightIntensityMode.Reflected)) {
motors.largeBC.tank(50, 50)
} else {
motors.largeBC.tank(50, 50)
}
})
2018-04-05 22:37:24 +02:00
```
## Step 6
2018-06-12 15:46:44 +02:00
In the first ``||motors:tank motors||`` block in the ``||logic:if||`` clause, change the speed values of the motors from ``50%``, ``50%`` to ``5%``, ``15%``. This slows down the robot and steers it to the left (because the **C** motor is driving faster than the **B** motor).
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (40 < sensors.color3.light(LightIntensityMode.Reflected)) {
motors.largeBC.tank(5, 15)
} else {
motors.largeBC.tank(50, 50)
}
})
2018-04-05 22:37:24 +02:00
```
2018-05-18 22:53:59 +02:00
## Step 7
2018-04-05 22:37:24 +02:00
2018-06-12 15:46:44 +02:00
In the second ``||motors:tank motors||`` block in the ``||logic:else||`` clause, change the speed values of the motors from ``50%``, ``50%`` to ``15%``, ``5%``. This slows down the robot and steers it to the right (because the **B** motor is driving faster than the **C** motor).
2018-04-05 22:37:24 +02:00
```blocks
forever(function () {
if (40 < sensors.color3.light(LightIntensityMode.Reflected)) {
motors.largeBC.tank(5, 15)
} else {
motors.largeBC.tank(15, 5)
}
})
2018-04-05 22:37:24 +02:00
```
## Step 8 @fullscreen
2018-04-05 22:37:24 +02:00
Use the EV3 simulator to try out your code.
![Brick with color sensors tracking a yellow line](/static/tutorials/line-following/line-following.gif)
2018-06-12 15:46:44 +02:00
Move the slider under the Color Sensor to change the reflected light intensity and check that motors are moving as you would expect.
## Step 9
2018-06-12 15:46:44 +02:00
Plug your EV3 Brick into the computer with the USB cable, and click the **Download** button at the bottom of your screen. Follow the directions to save your program to the EV3 Brick.
2018-06-12 15:46:44 +02:00
Attach a Color Sensor to Port 3 of your EV3 Brick, and attach your brick to a driving base with large motors attached to Ports B and C. See the instructions for building a _Driving Base with Color Sensor Down_. Test your program by positioning your robot to the right of a dark, thick line and then let it drive!