Ev3 lego mindstorms python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Use python3 to program your LEGO EV3. Communicate via Bluetooth, WiFi or USB. Send direct commands.

License

ChristophGaukel/ev3-python3

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.rst

Use python3 to program your LEGO Mindstorms EV3. The program runs on the local host and sends direct commands to the EV3 device. It communicates via Bluetooth, WiFi or USB. There is no need to boot the EV3 device from an SD Card or manipulate its software. You can use it as it is, the EV3 is designed to execute commands which come from outside. If you prefer coding from scratch, read this blog, if you like to benefit from preliminary work, then use module ev3_dc .

python3 -m pip install --user ev3_dc

Writing and sending direct commands

The following program communicates via USB with an EV3 device and plays a tone with a frequency of 440 Hz for a duration of 1 sec. This says, you need to connect the EV3 device and your computer with an USB cable.

import ev3_dc as ev3 my_ev3 = ev3.EV3(protocol=ev3.USB) my_ev3.verbosity = 1 ops = b''.join(( ev3.opSound, ev3.TONE, ev3.LCX(1), # VOLUME ev3.LCX(440), # FREQUENCY ev3.LCX(1000), # DURATION )) my_ev3.send_direct_cmd(ops)

The output shows the request, which was sent to the EV3 device and the corresponding response:

13:02:23.425843 Sent 0x|0E:00|2A:00|00|00:00|94:01:01:82:B8:01:82:E8:03| 13:02:23.432733 Recv 0x|03:00|2A:00|02|

Subclasses of EV3 with specialized APIs

Читайте также:  Isinstance nonetype python 3

Method play_tone of class Jukebox also plays tones:

import ev3_dc as ev3 jukebox = ev3.Jukebox(protocol=ev3.BLUETOOTH, host='00:16:53:42:2B:99') jukebox.verbosity = 1 jukebox.play_tone("a'", duration=1)

This program does the very same thing via Bluetooth! Before you can run it, you need to pair the two devices (the computer, that executes the program and the EV3 device) and replace the MAC-address 00:16:53:42:2B:99 by the one of your EV3. The output:

13:05:11.324701 Sent 0x|0E:00|2A:00|80|00:00|94:01:01:82:B8:01:82:E8:03|
  • Protocol USB replies all requests to provide collisions. Protocol BLUETOOTH is much slower and replies only requests, which demand it.
  • Module ev3_dc provides objects with specialized functionality. Behind the scene, this functionality depends on messages, which are sent to the EV3 device and replied by the EV3 device.

Specialized classes (e.g. class Jukebox) provide factory methods, which return thread_task.Task objects. Thread tasks allow to start, stop and continue independent tasks.

import ev3_dc as ev3 jukebox = ev3.Jukebox(protocol=ev3.WIFI) antemn = jukebox.song(ev3.EU_ANTEMN) antemn.start()

This program plays the EU antemn. Before you can execute it, you need to connect both devices (the computer, that runs the program and the EV3 device) with the same LAN (local area network), the EV3 device must be connected via WiFi. If you don’t own a WiFi dongle, modify the protocol and select ev3.BLUETOOTH or ev3.USB.

  • Method song() returns a thread_task.Task object and we need to start it explicitly. It plays tones and changes the LED-colors.
  • Starting a thread task does not block the program nor does it block the EV3 device. It runs in the background and allows to do additional things parallel.
  • Alternatively, you can start a thread task without mutlithreading by antemn.start(thread=False) . This lets it behave like any normal callable.

Specialized Subclasses handle Sensors and Motors

Specialized subclasses of class EV3, like Touch, Infrared, Ultrasonic, Color, Gyro, Motor, TwoWheelVehicle, Sound or Voice handle sensors, motors, sound or text to speech. You can use multiple of these objects parallel and all of them can share a single physical EV3 device. You can also build complex robots with more than one EV3 device and control these robots easily by a single python program.

import ev3_dc as ev3 with ev3.Touch(ev3.PORT_1, protocol=ev3.USB) as touch_1: touch_4 = ev3.Touch(ev3.PORT_4, ev3_obj=touch_1) voice = ev3.Voice(ev3_obj=touch_1, volume=100) touched_1 = touch_1.touched touched_4 = touch_4.touched if touched_1 and touched_4: txt = 'both sensors are touched' elif touched_1: txt = 'only sensor 1 is touched' elif touched_4: txt = 'only sensor 4 is touched' else: txt = 'none of the sensors is touched' voice.speak(txt).start(thread=False)
  • Connect two touch sensors, one at port 1, the other at port 4 and connect your EV3 device and your computer with an USB cable.
  • Class EV3 and all its subclasses support the with statement.
  • touch_4 and voice use the connection of touch_1. This is done by setting keyword argument ev3_obj=touch_1 .
  • If you have more than a single EV3 device connected via USB, this program will fail. To handle this special case identify the device by using keyword argument host, e.g. ev3.Touch(ev3.PORT_1, protocol=ev3.USB, host=’00:16:53:42:2B:99′) . For protocol BLUETOOTH keyword argument host is mandatory.
  • Method speak() returns a thread_task.Task object, which we start threadless.
  • This program depends on the tool ffmpeg and you need to have it installed on your computer.
Читайте также:  Java root exception class

About

Use python3 to program your LEGO EV3. Communicate via Bluetooth, WiFi or USB. Send direct commands.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

a project to run lego ev3 in python

License

LEGO-Robotics/Python-EV3

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Program Lego Mindstorms EV3 using Python on ev3dev

You need a working ev3dev on your ev3 and have an ssh session. Please reference the ev3dev site to burn such system.
Current python-ev3 is developed on ev3-ev3dev-jessie-2015-05-20

Both python 2.7 and python 3.4 are supported

python-ev3 is tested on the ev3-dev in python2.7 and python3.4. I have no all lego devices, so if there’s something wrong or not working, please file an issue or create a pull request. I’m quite happy to receive more devices code.

Current python-ev3 is not stable enough, virtualenv is recommend. Below examples are in virtualenv. However, python-ev3 should work in a native python environment.

Install the python-ev3 on EV3

  • apt-get update
  • apt-get install virtualenv virtualenvwrapper python-setuptools python-smbus python-pil
  • source /etc/bash_completion.d/virtualenvwrapper
  • mkvirtualenv ev3_py27 —python=/usr/bin/python2.7 —system-site-packages
  • workon ev3_py27
  • easy_install -U python-ev3
  • type deactive to exit
  • apt-get update
  • apt-get install virtualenv virtualenvwrapper python3-setuptools python3-smbus python3-pil
  • source /etc/bash_completion.d/virtualenvwrapper
  • mkvirtualenv ev3_py34 —python=/usr/bin/python3.4 —system-site-packages
  • workon ev3_py34
  • easy_install -U python-ev3
  • type deactive to exit
(ev3_py27)root@ev3dev:~# python Python 2.7.8 (default, Jul 4 2014, 16:59:40) [GCC 4.9.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from ev3.lego import MediumMotor >>> d = MediumMotor() >>> d.reset() >>> d.run_forever(50, regulation_mode=False) >>> d.stop() >>> exit()

To exit the virtual env, type deactivate

Читайте также:  Python создать массив функций

Plese see test to know how to use other devices.
To create new sensor class please see How to create a new sensor class

Python3 vs Python2 performance

@fuzzycow found there’s some performance problem when using Python3. Please see topikachu#22

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Python Lego Mindstorms EV3 module.

License

hmml/ev3

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Program Lego Mindstorms EV3 in Python

You need to have custom distribution. Prebuild image may not come with the latest version of ev3 module so make sure you follow instuction below to update it.

Once you have a working environment, connect to EV3 brick and launch Python (executed on EV3RSTORM):

$ ssh root@10.0.1.1 root@python-ev3:~# python [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ev3 >>> ev3.open_all_devices() >>> ev3.get_battery() (6911.138911088911, 46.41822823641005, 0.0) >>> ev3.set_led(ev3.LED_GREEN) >>> ev3.close_all_devices() 
>>> import ev3 >>> from ev3.sensor.lego import EV3TouchSensor >>> ev3.open_all_devices() >>> touch_sensor = EV3TouchSensor(ev3.SENSOR_1) >>> while True: . if touch_sensor.is_pressed(): print "pressed" . pressed # I've pressed touch sensor (ctrl+c to break the loop) 

Generated documentation available at readthedocs.org.

You may encounter the following problem:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/dist-packages/ev3/__init__.py", line 72, in open_all_devices analogdevice.open_device() File "/usr/local/lib/python2.7/dist-packages/ev3/rawdevice/analogdevice.py", line 16, in open_device _analogfile = os.open(lms2012.ANALOG_DEVICE_NAME, os.O_RDWR) OSError: [Errno 2] No such file or directory: '/dev/lms_analog' 

In such a situation please invoke:

Some parts are still not implemented or lack high level API — pull requests are welcome.

Updating module on EV3 brick

Configuring key based authorisation. This step is required only once:

$ ssh root@10.0.1.1 mkdir -p .ssh $ cat ~/.ssh/id_rsa.pub | ssh root@10.0.1.1 'cat >> .ssh/authorized_keys' 

Whenever you want to update ev3 module just invoke:

Note: this may take a while.

Источник

Оцените статью