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

No comments:

Post a Comment

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...