1 Star 0 Fork 0

q6200/ev3dev-lang-python2023

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Python language bindings for ev3dev

https://travis-ci.org/ev3dev/ev3dev-lang-python.svg?branch=ev3dev-stretch Documentation Status Chat at https://gitter.im/ev3dev/chat

A Python3 library implementing an interface for ev3dev devices, letting you control motors, sensors, hardware buttons, LCD displays and more from Python code.

If you haven't written code in Python before, you can certainly use this library to help you learn the language!

Getting Started

This library runs on ev3dev. Before continuing, make sure that you have set up your EV3 or other ev3dev device as explained in the ev3dev Getting Started guide. Make sure you have an ev3dev-stretch version greater than 2.2.0. You can check the kernel version by selecting "About" in Brickman and scrolling down to the "kernel version". If you don't have a compatible version, upgrade the kernel before continuing.

Usage

To start out, you'll need a way to work with Python. We recommend the ev3dev Visual Studio Code extension. If you're interested in using that, check out our Python + VSCode introduction tutorial and then come back once you have that set up.

Otherwise, you can can work with files via an SSH connection with an editor such as nano, use the Python interactive REPL (type python3), or roll your own solution. If you don't know how to do that, you are probably better off choosing the recommended option above.

The template for a Python script

Every Python program should have a few basic parts. Use this template to get started:

#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds

# TODO: Add code here

The first line should be included in every Python program you write for ev3dev. It allows you to run this program from Brickman, the graphical menu that you see on the device screen. The other lines are import statements which give you access to the library functionality. You will need to add additional classes to the import list if you want to use other types of devices or additional utilities.

You should use the .py extension for your file, e.g. my-file.py.

If you encounter an error such as /usr/bin/env: 'python3\r': No such file or directory, you must switch your editor's "line endings" setting for the file from "CRLF" to just "LF". This is usually in the status bar at the bottom. For help, see our FAQ page.

Important: Make your script executable (non-Visual Studio Code only)

To be able to run your Python file, your program must be executable. If you are using the ev3dev Visual Studio Code extension, you can skip this step, as it will be automatically performed when you download your code to the brick.

To mark a program as executable from the command line (often an SSH session), run chmod +x my-file.py.

You can now run my-file.py via the Brickman File Browser or you can run it from the command line by preceding the file name with ./: ./my-file.py

Controlling the LEDs with a touch sensor

This code will turn the LEDs red whenever the touch sensor is pressed, and back to green when it's released. Plug a touch sensor into any sensor port before trying this out.

ts = TouchSensor()
leds = Leds()

print("Press the touch sensor to change the LED color!")

while True:
    if ts.is_pressed:
        leds.set_color("LEFT", "GREEN")
        leds.set_color("RIGHT", "GREEN")
    else:
        leds.set_color("LEFT", "RED")
        leds.set_color("RIGHT", "RED")

If you'd like to use a sensor on a specific port, specify the port like this:

ts = TouchSensor(INPUT_1)

Heads-up: If you are using a BrickPi instead of an EV3, you will need to manually configure the sensor. See the example here: https://github.com/ev3dev/ev3dev-lang-python-demo/blob/stretch/platform/brickpi3-motor-and-sensor.py

Running a single motor

This will run a LEGO Large Motor at 75% of maximum speed for 5 rotations.

m = LargeMotor(OUTPUT_A)
m.on_for_rotations(SpeedPercent(75), 5)

You can also run a motor for a number of degrees, an amount of time, or simply start it and let it run until you tell it to stop. Additionally, other units are also available. See the following pages for more information:

Driving with two motors

The simplest drive control style is with the MoveTank class:

tank_drive = MoveTank(OUTPUT_A, OUTPUT_B)

# drive in a turn for 5 rotations of the outer motor
# the first two parameters can be unit classes or percentages.
tank_drive.on_for_rotations(SpeedPercent(50), SpeedPercent(75), 10)

# drive in a different turn for 3 seconds
tank_drive.on_for_seconds(SpeedPercent(60), SpeedPercent(30), 3)

There are also MoveSteering and MoveJoystick classes which provide different styles of control. See the following pages for more information:

Using text-to-speech

If you want to make your robot speak, you can use the Sound.speak method:

from ev3dev2.sound import Sound

sound = Sound()
sound.speak('Welcome to the E V 3 dev project!')

More Demo Code

There are several demo programs that you can run to get acquainted with this language binding. The programs are available at this GitHub site.

You can also copy and run the programs in the utils directory to understand some of the code constructs to use the EV3 motors, sensors, LCD console, buttons, sound, and LEDs.

We also highly recommend ev3python.com where one of our community members, @ndward, has put together a great website with detailed guides on using this library which are targeted at beginners. If you are just getting started with programming, we highly recommend that you check it out at ev3python.com!

Using Micropython

Normal Python too slow? Review Micropython to see if it supports the features your project needs.

Library Documentation

Class documentation for this library can be found on our Read the Docs page. You can always go there to get information on how you can use this library's functionality.

Frequently-Asked Questions

Experiencing an odd error or unsure of how to do something that seems simple? Check our our FAQ to see if there's an existing answer.

The MIT License (MIT) Copyright (c) 2015 Ralph Hempel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Python 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/q6200/ev3dev-lang-python2023.git
[email protected]:q6200/ev3dev-lang-python2023.git
q6200
ev3dev-lang-python2023
ev3dev-lang-python2023
benchmarking

搜索帮助