mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-15 02:52:41 +00:00
official_models/ev3/educator: add line follower
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
|
||||
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
|
||||
|
||||
// List of extensions which should be recommended for users of this workspace.
|
||||
"recommendations": [
|
||||
"lego-education.ev3-micropython"
|
||||
],
|
||||
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
|
||||
"unwantedRecommendations": [
|
||||
"ms-python.python"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Download and Run",
|
||||
"type": "ev3devBrowser",
|
||||
"request": "launch",
|
||||
"program": "/home/robot/${workspaceRootFolderName}/main.py",
|
||||
"interactiveTerminal": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.eol": "\n",
|
||||
"debug.openDebug": "neverOpen",
|
||||
"python.linting.enabled": false,
|
||||
"python.languageServer": "None"
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env pybricks-micropython
|
||||
from pybricks.ev3devices import Motor, ColorSensor
|
||||
from pybricks.parameters import Port
|
||||
from pybricks.tools import wait
|
||||
from pybricks.robotics import DriveBase
|
||||
|
||||
# Initialize the motors.
|
||||
left_motor = Motor(Port.B)
|
||||
right_motor = Motor(Port.C)
|
||||
|
||||
# Initialize the color sensor.
|
||||
line_sensor = ColorSensor(Port.S3)
|
||||
|
||||
# Initialize the drive base.
|
||||
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)
|
||||
|
||||
# Calculate the light threshold. Choose values based on your measurements.
|
||||
BLACK = 9
|
||||
WHITE = 85
|
||||
threshold = (BLACK + WHITE) / 2
|
||||
|
||||
# Set the drive speed at 100 millimeters per second.
|
||||
DRIVE_SPEED = 100
|
||||
|
||||
# Set the gain of the proportional line controller. This means that for every
|
||||
# percentage point of light deviating from the threshold, we set the turn
|
||||
# rate of the drivebase to 1.2 degrees per second.
|
||||
|
||||
# For example, if the light value deviates from the threshold by 10, the robot
|
||||
# steers at 10*1.2 = 12 degrees per second.
|
||||
PROPORTIONAL_GAIN = 1.2
|
||||
|
||||
# Start following the line endlessly.
|
||||
while True:
|
||||
# Calculate the deviation from the threshold.
|
||||
deviation = line_sensor.reflection() - threshold
|
||||
|
||||
# Calculate the turn rate.
|
||||
turn_rate = PROPORTIONAL_GAIN * deviation
|
||||
|
||||
# Set the drive base speed and turn rate.
|
||||
robot.drive(DRIVE_SPEED, turn_rate)
|
||||
|
||||
# You can wait for a short time or do other things in this loop.
|
||||
wait(10)
|
||||
Reference in New Issue
Block a user