Sunday, February 17, 2019

Python and the EV3: Part 4 Writing a Program

This is part of a series, if you haven't already, go read the previous installments:
Part 1: Installing EV3Dev

Just let me write some python already!

In VS Code (or your development environment, if it is different) create a new file.  Name it test.py and save it to the same folder you created when setting up deployment in the last session.  Note that if your EV3 is on when you save it, then it should automatically deploy over to the EV3.  For now, copy the following lines into the file:


#!/usr/bin/env python3
# This tells the OS to use python3 when executing this file

from ev3dev2 import motor, sound
# This means that you are grabbing the motor and sound pieces from the ev3-dev libraries.

car = motor.MoveTank("outB", "outC")
# replace B and C with the left and right ports for your motors.  Case matters!
# Also, you only need to do this once in the file

car.on_for_seconds(left_speed = 50, right_speed = 50, seconds = 5)
# speed is in percents for this example

There!  You have your very first python file.  This time, when you save the file, make sure your EV3 is setup, because we're going to want it deployed in order to run it.

Wait! How do I run it?


Use the built in terminal at the bottom of the screen in VS Code to SSH into your robot.  If you need a reminder, just type in:
ssh robot@ev3dev.local
and the default password is "maker"

Now you are going to want to switch to the directory that your file is in.  You do this by typing:
cd <directoryname>
You can use the tab key to autocomplete the name once you've gotten it typed out a bit.

The first thing you need to do for any new file is make it executable so that the operating system knows you can run it. To do this you will type:
chmod a+x test.py
You only need to do this once after creating the file.

 Finally, to actually run the file, you just need to type:
./test.py
There you go! You've written and run your very first python program for the EV3. Go back to part two, and try out some of the suggestions there and string together a few commands in a row.

Hey!  I told the motor to do a bunch of things, but it only did the last one.


One more weird detail about python and the EV3.  One command will not wait for the previous one to finish running before it starts, so if you pile up a bunch of move commands, it will really only complete the last one.  You can solve this buy placing this line between each of the commands:

Friday, February 15, 2019

Python and the EV3: Part 3 A Development Environment

This is part of a series, if you haven't already, go read the previous installments:
Part 1: Installing EV3Dev

What's a development environment?


A development environment is [usually] an application that you use to write your program.  It often comes with the ability to have multiple files open at once, automatic formatting for your code, color-coding of your code, and something that will let you know if you have errors.  Sometimes it also includes extras like version control (being able to update and roll back changes), deployment (sending files off to another location), and sftp (editing files on a different computer).  So all in all, it is much handier than just writing your code in a text file and copying it over to your EV3.

How do I get one?


There are three environments that I have played with and generally like:  BBEdit, Atom, and Visual Studio Code.  I eventually settled on Visual Studio Code, so I'll talk about how to set up that one, but it won't matter beyond the setup stage which one you choose.

Visual Studio Code.

Head over, download, and install a copy of Visual Studio Code.  Once you've got that installed, opened up, and running, you'll want to install a deployment extension.  Just like in Chrome or Firefox, extensions are bits that people have written to add functionality to the application.  Head over to the deploy extension and click "Install".  This will allow you to edit files locally and send them off to your robot every time you save them.

In order to setup deploy, you'll need to click the settings icon in the bottom left of the screen and choose "Settings." In the new pane that pops open, scroll down to extensions and choose "Deploy."  Click the link "Edit in settings.json" and copy the text below into the new "User Settings" pane that opens.

{
    "deploy": {
        "packages": [
            {
                "name": "robut",
                "description": "robut schtuff",
                "files": [
                    "*/*.py",
                    "*.py"
                ],
                "deployOnSave": true
            }
        ],

        "targets": [
        {
            "type": "sftp",
            "name": "robut",
            "description": "The ev3",

            "dir": "/home/robot/robut",
            "host": "ev3dev.local", "port": 22,
            "user": "robot", "password": "maker"
        }
        ]
    }
}

You'll want to change the name and description in 'packages' to match the local folder that holds your python files and whatever description you want.  Similarly, you'll want to change the name, description, and dir file in 'targets.'  This sends any python files in your local folder and any of its subfolders off to your robot to be run there.

That's enough for now.  Next time we'll actually create a python file, make it executable, and run it on our robot.

Python and the EV3

Thursday, February 14, 2019

Python and the EV3: Part 2 SSH and Python

This is part of a series, if you haven't already, go read the previous installment:
Part 1: Installing EV3Dev

Great, I've got EV3-Dev, what do I do with it?

First get an SSH client running:


It would be pretty terrible if we had to do our programming on the EV3 itself, luckily we can use another computer to talk to it (that's why we wanted the wireless connection).

First you want to open a terminal.  If you use Linux, I'll assume you know how to do this already.

For Mac OS:

Just hit Command-Space and type terminal into your spotlight window.  You'll get a small black window that is mostly text.  You'll type your commands into this.

For Windows 10: 
You'll want to open the command prompt window.  You can get to this via Start -> All Programs -> Accessories -> Command Prompt.

For earlier versions of Windows:  
Here is a good resource for picking an SSH client to install.  I recommend Putty, but I haven't had a Windows system since the early 2000's so I don't have much of an opinion.

https://www.linode.com/docs/networking/ssh/using-ssh-on-windows/

SSHing into the EV3:


Into your terminal you are going to type:
ssh robot@ev3dev.local

This means you are logging into the robot user for the computer named ev3dev on your local network.  If you have more than one EV3 running ev3dev at the same time you can use the ip address instead.  That's the number in the top left corner of the EV3.  It looks something like 192.168.1.321

so instead you would type:
ssh robot@<ip address>
You will need a password.  The default password is "maker"  I leave mine that way because I'm not too worried about anyone stealing all of my secrets of of my EV3...

Now everything that you type into your terminal will actually run on your EV3 instead of your computer.

Running Interactive Python:


All that is good and well, but now we get to the real fun.  Making our EV3 do stuff!  First we're just going to play with the Python interactive console.  This allows us to type in commands and get instant results instead of running a complete program.

Type into your terminal:
python3
This will start up a python interpreter.  If you are unfamiliar with python, play with some basic things before you get going with the EV3 commands.  Some ideas to try:

4+1
print("hello")
print(4 + 1)
print("a" + "b")
print("a" + 4)  # this should give you an error
a = "hello"
print(a)
So now, let's get on to the really fun stuff.  In order to interact with the EV3, you need to import the ev3-dev library.  This is where folks have put in the hard work to figure out the interfacing for the motors for you.  You have to run the following lines once every time you restart python:
from ev3dev2 import motor, sound
# This means that you are grabbing the motor and sound pieces from the ev3-dev libraries.
I'm going to assume you are starting out with a robot that has two drive wheels with tank-style steering.

To create the car object you need to run the following (after setting your robot on the floor!):
car = motor.MoveTank("outB", "outC")
# replace B and C with the left and right ports for your motors.  Case matters!
And now to drive it!
car.on_for_seconds(left_speed = 50, right_speed = 50, seconds = 5)
# speed is in percents for this example
Now mix things up.  Type the above command again (or hit up arrow in your terminal) and make both speeds negative.  Try making only one of the speeds negative.  Make each wheel go at a different speed.

You can also try out on_for_degrees(left_speed, right_speed, degrees) where degrees is the degrees of rotation of the wheel that has the highest rotational speed.  Using some geometry, you can use this to make your car go a particular distance.

Now if driving a car around isn't your thing, you can play some music instead.  The ev3-dev developers have a great example, so I feel no need to mess with perfection, I will just copy it below.

sound = sound.Sound()
sound.play_song((
    ('D4', 'e3'), # intro anacrouse
    ('D4', 'e3'),
    ('D4', 'e3'),
    ('G4', 'h'), # meas 1
    ('D5', 'h'),
    ('C5', 'e3'), # meas 2
    ('B4', 'e3'),
    ('A4', 'e3'),
    ('G5', 'h'),
    ('D5', 'q'),
    ('C5', 'e3'), # meas 3
    ('B4', 'e3'),
    ('A4', 'e3'),
    ('G5', 'h'),
    ('D5', 'q'),
    ('C5', 'e3'), # meas 4
    ('B4', 'e3'),
    ('C5', 'e3'),
    ('A4', 'h.'),
))


That should be enough to give you something to play with, but if you're already anxious to learn more about what you can do, check out the motor and sound APIs for more info.  Next time we'll talk about setting up a development environment so that you can save and execute whole programs.

Python and the EV3

Python and the EV3: Part 1 Installing EV3Dev


Supplies: 



What's this all about?


The folks over at EV3-Dev have done some amazing things and created a version of Debian (a flavor of Linux) to be run on the EV3.  It runs on an SD card, so you aren't hacking your EV3 at all.  Once you write the SD card just insert it into the EV3 and start it up.  When you want your EV3 back to normal again, just shut it down, take out the card and turn it on as normal and everything is back the way you want it!

So how do you get started? 


Head on over to the EV3-Dev site and they have some great instructions:

https://www.ev3dev.org/docs/getting-started/

I've never wanted to bother downloading Etcher (like they recommend on the EV3-Dev site), so I do everything in my terminal.  The instructions for doing that with MacOS are here courtesy of Raspberry Pi:

https://www.raspberrypi.org/documentation/installation/installing-images/mac.md

and here is the same thing for Linux:

https://www.raspberrypi.org/documentation/installation/installing-images/linux.md

Windows users should probably just stick with Etcher.

What's next?


Once you have the SD card written (that should take about 30 minutes) insert it into the SD slot and boot up your EV3.  It'll take a little while and you'll see lots of text scrolling by.  Once you get to the menu screen hit the down button twice to navigate to "Wireless and Networking" and hit the center square button.  Scroll down and select "Wi-Fi".  Selecting "powered" will turn on your wifi. After doing that select your wireless network and enter your password.

You've got your wi-fi all set up.  We'll talk about logging in and playing around with the python interactive console next time!

Python and the EV3

Python and the EV3: Part 4 Writing a Program

This is part of a series, if you haven't already, go read the previous installments: Part 1: Installing EV3Dev Part 2: SSH and Python...