Introduction:
Recently, I have been working on a project to use Python on the ev3dev platform. This is useful, because you can write complex programs for your robots. For example, you could connect the ev3 to a PS4 controller. For me, I was working on a SumoBot, a robot that battles with another, and tries to push it out of a ring. However, this approach will work with an sort of robot.
Overview:
In this tuorial, I will show you how to:
- Download the ev3dev operating system for your brick.
- Connect your brick to your Windows or Mac computer.
- Setup PyCharm on your computer.
- Connect to a GitHub repo.
- Sync the code to your brick.
Materials:
You will need the following:
- A Windows or Mac with a working Internet and Bluetooth connection.
- An ev3 brick with various sensor and motors.
- A 2-16gb SD card.
- A way to put the SD card into your computer.
1: Getting started:
- First, you will have to download the ev3dev boot image for the ev3 brick. This enables it to work with the Python programming language. You can find the image here.
- Unzip the file, and move the contents to somewhere where you can easily access them.
- Next, flash the image onto your SD card.
Note: to do this, use a program like Etcher, or Rufus, or use the command line. Make sure that you select the correct disk image.
- Insert the SD card into the slot in the brick. Make sure that your brick is off.
- Next, turn on your brick. It will flash wierdly and show lots of text. If it doesn’t show a menu screen in 15 minutes, repeat steps 4-5.
- Next, we need to connect the brick to your computer.
- View this website for instructions on how to connect your brick to the computer.
- Connect to your computer with ssh:
- If you have a Windows computer download PuTTy, and launch it. Next type in
ev3dev.local
- Then, type
robot
as the username. - If you have a Mac, then open Terminal, then type
ssh robot@ev3dev.local
- Next type in
maker
as the password.
- If you have a Windows computer download PuTTy, and launch it. Next type in
Having an error message? Make sure your robot is on, and that you have connected the brick. Also, make sure you have the correct username and password.
- You have access to your brick’s inner workings! Try typing
fortune
to get a witty response from the computer. - Your home folder is
/home/robot
this is where you are put when you connect to the brick.
Some basic commands:
- cd {path} : changes the current working directory to
- ls : This shows all the files and folders in the current working directory.
- cat {file} : This returns the contents of the specified file.
- nano {file} : This opens an editor for the specified file. Press
ctrl+x
(^x
)to save and exit.- clear : Clears the terminal.
- logout : Logs out of remote connection.
- rm -rf {path} : Deletes file or folder. - python3 {file} : Runs the specified python program.
- Right now, you can
nano
files into existence and run them from the terminal, but this is less than ideal, in the next section, you will set up PyCharm and Git.
2: Setting up a Development Environment:
- Download PyCharm community edition (or the Pro version if you have it, it’s not needed.)
- Open the file and install it to your computer.
- If you have Windows, you have to configure Git, if you have Mac, you already have it installed, so skip to step 4.
- Download git-scm for Windows.
- Open and follow the install instructions with default options.
- Open PyCharm, and push
ctrl + alt + s
to open preferences. - Goto
Version Control > git
and in the top file path putC:\Users\<your username>\AppData\Local\Programs\Git\git.exe
- Now, get a GitHub account if you don’t have one yet. They are free, and tremendously useful.
- Goto https://github.com/new and create a new repository with the name
ev3
, and select the option to include aREADME.md
. - Open PyCharm.
- Goto
Checkout from Version Control
and go to the GitHub tab. Sign in, and select yourev3
repository. - Now, you should have your code in PyCharm. As you can see, all that you have now is a file called
README.md
.
3: Adding Code to the Program.
- First, in the upper lefthand corner, it will say your project name. Right click on it, and push Create New Python file.
- Pycham should ask you if you wan to add the file to Git. You should do this, and select the option to automatically do this.
- In the new file, type:
import time
print('The program is starting...')
time.sleep(2)
print('The program is ending.')
- In the VCs tab in PyCharm, press commit changes.
- Click the blue arrow next to the COMMIT button, and push
Commit and Push
. - Then push
Push
. - Your code is now on GitHub, but how to get it on the brick?
You can view your code by going to https://github.com/{your-username}/ev3.
4: Adding code to the brick:
- If you are on Windows, open Git-Bash from the start menu, if you have a Mac, open Terminal.
- Type
git clone https://github.com/{ github username }/ev3.git
. This copies your code into a local file. - Next, type
sftp robot@ev3dev.local
, notice it is notssh
- Put in your password.
- Now type
cd ~
, thenput ev3
- Now open up your SSH session. Type
ls
to view your files. - You should see a blue or green folder called
ev3
- Go into the folder by typing
cd ~/ev3
- Then, type
python3 main.py
- Your program takes a while to load, but then it runs!
5. Simplifying the process:
- Every time you would like to add code to your robot, you will have to remove the old code from the robot, and then do all of sections 3 and 4 again.
- We will simplify this process.
- Close your terminal. (Either Git-Bash, or Terminal)
- Open a new terminal.
- Type nano
e.sh
- Add this code to the file:
echo "Starting..."
rm -rf ev3
git clone htttps://github.com/< username >/ev3
sftp robot@ev3.local:~ <<< $'put {ev3}'
echo "Done."
- Push
^X
to save, and typechmod + x e.sh
- Now, all you have to do is type
rm -rf ~/ev3
on the robot, andsh e.sh
on the local computer to update the brick. - We can go even further and make custom commands on the brick!
- On the brick, type
sudo nano /etc/bash/bash.rc
and enter your password. - Add this to the end of the file:
alias prep='rm -rf ~/ev3'
alias run='python3 ~/ev3/main.py'
- Restart the brick.
6. So far…
Now, you write the program in Pycharm, and commit+push to GitHub. Then, on an SSH session, you type prep
to clear the brick. Then, on your local terminal, type sh e.sh
. On your SSH session, type run
.
Top comments (0)