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
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
PC Windows10 Python 3.7.3 Development environment Visual Studio Code
EV3 ev3dev
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.
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.
Etcher starts automatically after the installation is completed.
If it doesn't start up or closes, you can find it by searching for balena Etcher
.
Download the image file to be written to the SD card from the following and unzip the Zip file. https://www.ev3dev.org/downloads/ 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. <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.
The Select target window will open. Check the inserted SD card and select "Select (1)".
Select "Flash!" And start writing the image file.
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.
If the EV3 screen looks like the one below, the EV3 OS is ready.
After completing the EV3 environment construction, pair the PC and EV3 with Bluetooth.
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.
Then select "Bluetooth".
On the next screen, select "Powered", check it, and select "Start Scan". (Selecting "Start Scan" changes the choice to "Stop Scan".)
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.
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.
Then, the passkey is confirmed on both the EV3 and PC terminals. Select "Accept" on the EV3 side and "Yes" on the PC side.
If you can confirm the passkey, the screen on the EV3 side will be as follows, so select "Network Connection".
The following screen will appear, so select "Connect".
When pairing is completed, "State:" becomes "Connected" as shown below. This completes the pairing work.
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.)
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/
Download and execute the installer according to the environment to be used, and install Python.
As with any Python version, be sure to check the following checkboxes during installation.
Download and install Visual Studio Code (hereafter VS Code) according to your environment from the following. https://code.visualstudio.com/download
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.
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.
Click "Install" to install the extension.
This completes the basic environment construction for both EV3 and PC.
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.
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.
Explorer will open, so create a folder in a suitable location. This time, I created a folder named "ev3 workspace". Select the created folder and click "Select Folder".
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.
Create a Python program in the created workspace. Select "New File" to the right of the added workspace name in "Explorer".
The file is created by entering the name of the file. This time, let's call it "hello-ev3.py".
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.
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.)
When the access to EV3 from VS Code is completed, a green circle will be displayed as shown below.
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".
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 /".
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.
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.
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.)
Enter the following command in Powershell to install ev3dev.
pip install python-ev3dev2
Recommended Posts