EV3 x Pyrhon Machine Learning Part 1 Environment Construction

The content of this article is in beta and is subject to change. This article summarizes the procedure for building a development environment for ev3dev, an OS that runs on the educational version of LEGO® MINDSTORMS EV3 (hereafter EV3). In addition, I will post an article that solves the problems of linear regression and classification by controlling EV3 using Python twice from the next time.

EV3 x Pyrhon Machine Learning Part 1 Environment Construction: This article EV3 x Pyrhon Machine Learning Part 2 Linear Regression: here EV3 x Pyrhon Machine Learning Part 3 Classification: here

reference

In the content of this article, Halloworld-like content is done in cooperation with EV3, but in the following books It covers the basic control of EV3 using Python.

Introduction to AI starting with robots

Environment to be built in this article

table of contents

  1. EV3 environment construction
  2. Bluetooth pairing of EV3 and PC
  3. PC environment construction
  4. Create / transfer / execute sample program
  5. Appendix

EV3 environment construction

We will build the ev3dev environment, which is the OS of EV3 to be used this time, on a micro SD card (hereinafter SD card). Unlike normal firmware, it enables control of motors and sensors in C ++ and Python.

Installation of image file writing tool

Install software called Etcher to write the OS image file from the PC to the SD card. https://www.balena.io/etcher/

When you access the URL, the following screen will appear. Download the setup file according to the PC environment, execute it, and install it. 1.png

Etcher starts automatically after the installation is completed. 1-2.png

If it doesn't start up or closes, you can find it by searching for balena Etcher. 1-2-1.png

Download ev3dev image file

Download the image file to be written to the SD card from the following and unzip the Zip file. https://www.ev3dev.org/downloads/ 2.png You can download it by selecting "Download ev3dev-stretch for EV3".

After unzipping the downloaded Zip file, operate from Etcher. Insert the micro SD card into your PC at this point. Select "Flash from file" from Etcher, and select "ev3dev-strech-ev3-generic- ~~~~-~~ .img" in the unzipped folder. 1-2.png <img width="500" alt=1-3".png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/440646/79a6a983-f973-be0c-433d-82357af9d5c8.png ">

"Select target" can be selected in Etcher, so select it. 1-4.png

The Select target window will open. Check the inserted SD card and select "Select (1)". 1-5.png

Select "Flash!" And start writing the image file. 1-6.png

EV3 startup

When writing is complete, insert the SD card into the slot on the side of the EV3's intelligent block and press the center button to start it. 3.png

If the EV3 screen looks like the one below, the EV3 OS is ready. 3-1.png

Bluetooth connection between PC and EV3

After completing the EV3 environment construction, pair the PC and EV3 with Bluetooth.

Pairing

First, operate on the EV3 side. Select "Wireless and Networks" from the ev3dev menu using the up and down buttons and the center button of the intelligent block. N1.png

Then select "Bluetooth". N2.png

On the next screen, select "Powered", check it, and select "Start Scan". (Selecting "Start Scan" changes the choice to "Stop Scan".) N3.png

Select "Start Scan" and wait for a while, then the names of nearby pairable devices will be detected below. Select the PC name you want to pair with. N4.png

After selecting the PC name, select "Pair" to start pairing. When you select "Pair", the "Add device" notification will be displayed at the bottom right of the screen on the PC side, so click it. N5.png          N5-1.png

Then, the passkey is confirmed on both the EV3 and PC terminals. Select "Accept" on the EV3 side and "Yes" on the PC side. N6.png          N6-1.png

If you can confirm the passkey, the screen on the EV3 side will be as follows, so select "Network Connection". N7.png

The following screen will appear, so select "Connect". N8.png

When pairing is completed, "State:" becomes "Connected" as shown below. N11.png This completes the pairing work.

PC environment construction

When the pairing of EV3 and PC is completed, we will start building the PC environment. (Actually, pairing may be done after the PC environment is built, but it is done in this order because the procedure goes back and forth.)

Python download

Download Python from the following and install it on your PC. Avoid the latest Python and recommend installing the previous version. (3.5 ~ 3.8) The environment in this article uses 3.7.3. https://www.python.org/downloads/

5.png

Download and execute the installer according to the environment to be used, and install Python. 5-1.png

As with any Python version, be sure to check the following checkboxes during installation. .png

Download Visual Studio Code

Download and install Visual Studio Code (hereafter VS Code) according to your environment from the following. https://code.visualstudio.com/download 6.png

Follow the installer's instructions to install. There is nothing special to mention, so I will omit it. When the installation is completed and VS Code starts, the following screen will be displayed. 6-1.png

Install VS Code extension

When VS Code starts, select the "Extension" mark from the tab on the left side of the screen and enter "ev3dev" in the search field. Select "ev3 dev-browser" from the search results. 6-2.png

Click "Install" to install the extension. 6-3.png

This completes the basic environment construction for both EV3 and PC.

Sample program creation / transfer / execution

Now, let's create a simple sample program on VSCode, including checking if the environment construction is complete, and transfer and execute it to EV3.

Creating a workspace

Since the entire workspace will be transferred to EV3, first create a working folder. Select "Open Folder" from the "File" tab at the top of VS Code. 7-1.png

Explorer will open, so create a folder in a suitable location. This time, I created a folder named "ev3 workspace". .png Select the created folder and click "Select Folder". .png

When you open a folder with VS Code, the name of the opened folder will be displayed in the "Explorer" column. Also, if the above-mentioned extension "ev3dev-browser" is installed, "EV3DEV DEVICE BROWSER" is displayed under the folder name. 7-4.png

Creating a program

Create a Python program in the created workspace. Select "New File" to the right of the added workspace name in "Explorer". 9.png

The file is created by entering the name of the file. This time, let's call it "hello-ev3.py". 9-1.png

Edit the contents of hello-ev3.py as follows.

from ev3dev2.display import Display
import ev3dev2.fonts as fonts
import time

screen = Display()
font = fonts.load('luBS12')
screen.draw.text((10, 10), 'Hello EV3!', font=font)
screen.update()
time.sleep(3.0)

print('Hello World')

It looks like the following on VS Code. 9-2.png

Access EV3 from VS Code

From here, access EV3 from VS Code using the installed extension "ev3dev-browser". Expand "EV3 DEV DEVICE BROWSER" from the "Explorer" field and select "Click here to connnect to a device".

Then, the command palette is displayed at the top of the screen. In the list, EV3 that is connected to the PC via Bluetooth is displayed as "ev3dev Bluetooth network connection", so select it. (By the way, if EV3 and PC are connected via USB instead of Bluetooth, "ev3dev Ethernet ~" will be displayed.) 8-1.png

When the access to EV3 from VS Code is completed, a green circle will be displayed as shown below. 8-2.png

Program transfer / execution

Now that I have access to EV3, I will transfer the created program and execute it.

First, click "Send workspace to device" to the right of "EV3 DEV DEVICE BROWSER" to transfer the workspace "ev3 workspace". 9-3.png

When the transfer is complete, "Download to ev3dev complete" will be displayed at the bottom right of VS Code, and "ev3 workspace" will be copied under "/ home / robot /". 9-4.png

Next, try running the program from the terminal. Right-click in the "ev3dev" field where the green circle is displayed, and select "Open SSH Terminal" from the displayed menu. 10-1.png

The terminal SSH logged into EV3 is displayed at the bottom of VS Code, and it can be operated on a command basis, so enter the following command. ls cd ev3workspace/ python3 hello-ev3.py

When the program is executed, the following characters are displayed on the screen of the EV3 side for 3 seconds, and then "Hello World" is displayed on the terminal. 10-3.png 10-2.png

Now you have an environment to control EV3 in ev3dev environment from VS Code. From the next time, we will execute programs on the PC side and EV3 side respectively, exchange data, and handle linear regression and classification machine learning tasks.

Appendix
When creating a program using the ev3dev library on VS Code, intellisense may work and a red wavy line may be displayed. There is no problem in creating the program, but if you want to use automatic proofreading including ev3dev, you can use it by installing ev3dev on your PC as shown below.

Right-click on the Windows mark and select "Windows PowerShell (Administrator)" to open Powershell. (You need to open it with administrator privileges.) 99-2.png

Enter the following command in Powershell to install ev3dev. pip install python-ev3dev2 99-3.png

Recommended Posts

EV3 x Pyrhon Machine Learning Part 1 Environment Construction
EV3 x Pyrhon Machine Learning Part 3 Classification
EV3 x Python Machine Learning Part 2 Linear Regression
Machine learning environment construction macbook 2021
Python and machine learning environment construction (macOS)
Build a machine learning environment natively on Windows 10 (x64)
Build a machine learning environment
Python + Unity Reinforcement learning environment construction
Promised victory ("Hobby x Machine Learning")
Deep learning tutorial from environment construction
Python & Machine Learning Study Memo: Environment Preparation
Mac OS X Mavericks 10.9.5 Development environment construction
Machine learning
GeoDjango + SQLite environment construction on OS X
Mac OS X Yosemite 10.10 Development environment construction
Mac OS X development environment construction memo
[Windows 10] "Deep Learning from scratch" environment construction
Environment construction for MXNet tutorial (gluon part)
Creating a development environment for machine learning
Build AI / machine learning environment with Python
[Super Introduction] Machine learning using Python-From environment construction to implementation of simple perceptron-
Machine learning memo of a fledgling engineer Part 1
Machine learning starting with Python Personal memorandum Part2
Machine learning starting with Python Personal memorandum Part1
[Machine learning] Supervised learning using kernel density estimation Part 2
[Machine learning] Supervised learning using kernel density estimation Part 3
Mac OS X Mountain Lion 10.8.5 Development environment construction
Machine learning memo of a fledgling engineer Part 2
[Environment construction] Oracle DB x Pro * C [Now]
Classification of guitar images by machine learning Part 2
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
Build a Python machine learning environment with a container
Django environment construction
DeepIE3D environment construction
Emacs-based environment construction
Linux environment construction
Report_Deep Learning (Part 2)
Report_Deep Learning (Part 1)
Report_Deep Learning (Part 1)
Python environment construction
Environment construction (python)
django environment construction
Report_Deep Learning (Part 2)
CodeIgniter environment construction
python environment construction
Python --Environment construction
Python environment construction
[Memo] Machine learning
Machine learning classification
Golang environment construction
python environment construction
Machine Learning sample
Word2vec environment construction
I tried to build an environment for machine learning with Python (Mac OS X)
Build an interactive environment for machine learning in Python
Build a machine learning Python environment on Mac OS
Web application made with Python3.4 + Django (Part.1 Environment construction)
Machine Learning Amateur Marketers Challenge Kaggle's House Prices (Part 1)
Build a machine learning application development environment with Python
Memo for building a machine learning environment using Python
Create a machine learning environment from scratch with Winsows 10