From 2392e1bc666c953ded3cf7e7b684ff22cd52ff56 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 24 Apr 2020 13:01:06 +0200 Subject: [PATCH] official_models/ev3/educator: add line follower --- .../robot_educator_line/.gitignore | 3 ++ .../.vscode/extensions.json | 13 ++++++ .../robot_educator_line/.vscode/launch.json | 15 +++++++ .../robot_educator_line/.vscode/settings.json | 7 +++ .../robot_educator_line/main.py | 45 +++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 official_models/ev3/education_core/robot_educator_line/.gitignore create mode 100644 official_models/ev3/education_core/robot_educator_line/.vscode/extensions.json create mode 100644 official_models/ev3/education_core/robot_educator_line/.vscode/launch.json create mode 100644 official_models/ev3/education_core/robot_educator_line/.vscode/settings.json create mode 100644 official_models/ev3/education_core/robot_educator_line/main.py diff --git a/official_models/ev3/education_core/robot_educator_line/.gitignore b/official_models/ev3/education_core/robot_educator_line/.gitignore new file mode 100644 index 0000000..00f2d38 --- /dev/null +++ b/official_models/ev3/education_core/robot_educator_line/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +.venv/ diff --git a/official_models/ev3/education_core/robot_educator_line/.vscode/extensions.json b/official_models/ev3/education_core/robot_educator_line/.vscode/extensions.json new file mode 100644 index 0000000..f8f1a44 --- /dev/null +++ b/official_models/ev3/education_core/robot_educator_line/.vscode/extensions.json @@ -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" + ] +} \ No newline at end of file diff --git a/official_models/ev3/education_core/robot_educator_line/.vscode/launch.json b/official_models/ev3/education_core/robot_educator_line/.vscode/launch.json new file mode 100644 index 0000000..d933aeb --- /dev/null +++ b/official_models/ev3/education_core/robot_educator_line/.vscode/launch.json @@ -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 + } + ] +} diff --git a/official_models/ev3/education_core/robot_educator_line/.vscode/settings.json b/official_models/ev3/education_core/robot_educator_line/.vscode/settings.json new file mode 100644 index 0000000..b0968c1 --- /dev/null +++ b/official_models/ev3/education_core/robot_educator_line/.vscode/settings.json @@ -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" +} diff --git a/official_models/ev3/education_core/robot_educator_line/main.py b/official_models/ev3/education_core/robot_educator_line/main.py new file mode 100644 index 0000000..394ea27 --- /dev/null +++ b/official_models/ev3/education_core/robot_educator_line/main.py @@ -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)