Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line-following-robot #5618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/projects/line-following-robot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# line following robot

## @description A basic example of a line-following robot. If you have any ideas, this can provide you with some inspiration.

## ~avatar avatar

Understanding the logic of line-following robots~

## ~

https://youtube.com/shorts/GOhKmu1nFlQ?feature=share

## Duration

Getting it running takes only ten minutes. Afterwards, you can delve into the logic of the line-following code and make optimizations yourself.


## Materials

* TabbyBot
* microbit:bit
* 1 18650 battery.
* Line map



![Materials](/static/mb/projects/line-following-robot/materials.JPG)

## Activities

* [Make](/projects/line-following-robot/make)
* [Code](/projects/line-following-robot/code)

## ~button /projects/line-following-robot/make

Let's get started!

## ~
96 changes: 96 additions & 0 deletions docs/projects/line-following-robot/code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Code

## @description code to make the inchworm alive

## ~avatar avatar

Add code to make the inchworm move.

## ~

## Duration: ~30 minutes

## Run logic
Before starting coding again, everyone can take a look at this diagram, which represents the operating logic of the car.
![Materials](/static/mb/projects/line-following-robot/run-logic.jpg)

## Step 1: Initialize variables.

``rightSensor``: Infrared sensor on the right
``leftSensor``: Infrared sensor on the left
``flag``: Car's operational state
``lineThreshold``: Trigger threshold, actions are taken when sensor values exceed this value
```blocks
let rightSensor = 0
let leftSensor = 0
let flag = false
let lineThreshold = 600
flag = false
```

## Step 2: Change the car's state.

When button ```A``` is pressed, set the ```flag``` to the opposite state.
```blocks
input.onButtonPressed(Button.A, function () {
flag = !(flag)
})
```

# Step 3: Line following

Set wheel states accordingly based on sensor values.


```blocks
basic.forever(function () {
leftSensor = tabbyRobot.line(tabbyRobot.LeftRight.LEFT)
rightSensor = tabbyRobot.line(tabbyRobot.LeftRight.RGIHT)
if (flag) {
if (leftSensor >= lineThreshold && rightSensor >= lineThreshold) {
tabbyRobot.motorRun(20, 20)
} else if (leftSensor < lineThreshold && rightSensor >= lineThreshold) {
tabbyRobot.motorRun(15, 60)
} else if (leftSensor >= lineThreshold && rightSensor < lineThreshold) {
tabbyRobot.motorRun(60, 15)
} else {
tabbyRobot.motorStop()
}
} else {
tabbyRobot.motorStop()
}
})
```

# Complete program
```blocks
input.onButtonPressed(Button.A, function () {
flag = !(flag)
})
let rightSensor = 0
let leftSensor = 0
let flag = false
let lineThreshold = 600
flag = false
basic.forever(function () {
leftSensor = tabbyRobot.line(tabbyRobot.LeftRight.LEFT)
rightSensor = tabbyRobot.line(tabbyRobot.LeftRight.RGIHT)
if (flag) {
if (leftSensor >= lineThreshold && rightSensor >= lineThreshold) {
tabbyRobot.motorRun(20, 20)
} else if (leftSensor < lineThreshold && rightSensor >= lineThreshold) {
tabbyRobot.motorRun(15, 60)
} else if (leftSensor >= lineThreshold && rightSensor < lineThreshold) {
tabbyRobot.motorRun(60, 15)
} else {
tabbyRobot.motorStop()
}
} else {
tabbyRobot.motorStop()
}
})
```

## ~ hint
After downloading the program, place the car on the track, then press the ```A``` button to start line following.
## ~
34 changes: 34 additions & 0 deletions docs/projects/line-following-robot/make.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Make

## @description Explanation of the accessories.

## ~avatar avatar

Manufacture of the necessary equipment completed.

## ~

## Duration: ~5 minutes

## Car

Assemble the battery and micro:bit onto the car. Just pay attention to the direction.

![](/static/mb/projects/line-following-robot/assembly-car.JPG)

Sensor used for detecting lines, corresponding to both sides behind.
![](/static/mb/projects/line-following-robot/back.JPG)

## Line map

Suggest printing the picture on A3 paper, or you can use electrical tape to draw the route yourself on a light-colored surface.
![](/static/mb/projects/line-following-robot/line-map.JPG)
## It's ready!

Your inchworm is ready!

![](/static/mb/projects/line-following-robot/ready.JPG)

## ~button /projects/line-following-robot/code
NEXT: Code
## ~
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.