|
| 1 | +--- |
| 2 | +title: Setting Up a Python Development Environment with PyCharm |
| 3 | +subject: Software - Python |
| 4 | +author: Anton Vanhoucke |
| 5 | +--- |
| 6 | + |
| 7 | +* Table of Contents |
| 8 | +{:toc} |
| 9 | + |
| 10 | +This tutorial shows how to set up a nice working environment for developing |
| 11 | +Python on Ev3dev. The platform used is a BrickPi, but most of it is usable |
| 12 | +for regular LEGO EV3 bricks. |
| 13 | +{:.lead} |
| 14 | + |
| 15 | +Ev3dev is a lean and modern system to run on the Raspberry Pi that powers the |
| 16 | +BrickPi. It's got multiple languages that work in a pretty similar way and |
| 17 | +make switching easy. Moreover, if you code in ev3dev on the BrickPi, people |
| 18 | +with a regular EV3 and Ev3dev will be able to run your code! Alas, it doesn't |
| 19 | +work the other way around as the BrickPi has a more limited set of motor |
| 20 | +commands. |
| 21 | + |
| 22 | +For this tutorial, I will suppose you have burned an SD disk, connected your |
| 23 | +RPi and BrickPi to an ethernet cable and booted it. If you have an EV3 brick |
| 24 | +without ethernet, [set up another networking connection first][network]. |
| 25 | + |
| 26 | +[network]: /docs/tutorials |
| 27 | + |
| 28 | +## Log in to ev3dev ## |
| 29 | + |
| 30 | +Now you can either attach a screen and keyboard to it for the setup, or |
| 31 | +connect to it via ssh. I prefer the latter. My router has a DNS that registers |
| 32 | +hostnames. It allows me to start a Terminal (on Mac) or Putty (on Windows) and |
| 33 | +simply do: |
| 34 | + |
| 35 | +`ssh robot@ev3dev` |
| 36 | + |
| 37 | +If you don't have a DNS try: `robot@ev3dev.local` or connect to it's IP |
| 38 | +address directly. eg. `robot@192.168.0.2` |
| 39 | + |
| 40 | +The password is `maker` by default. If you used screen and keyboard, you're |
| 41 | +automatically logged in. |
| 42 | + |
| 43 | +##Configure the right BrickPi## |
| 44 | + |
| 45 | +You're logged in to the command line. Cool! This step will activate the |
| 46 | +BrickPi. Skip it if you have a normal EV3. Now several Mindstorms shields |
| 47 | +are available for the RPi, we have to setup the right one first. That is, |
| 48 | +if you haven't done so already after burning the image. For this go: |
| 49 | + |
| 50 | +`sudo nano /boot/flash/config.txt` (the password is still `maker`) |
| 51 | +Scroll down and uncomment the appropriate lines for your hardware. |
| 52 | + |
| 53 | +{% include icon.html type="info" %} |
| 54 | +I have multiple Raspberry Pis and Mindstorms brains. I like to keep |
| 55 | +them apart on the network by giving them separate names. I renamed mine like |
| 56 | +this:<br />`sudo hostnamectl set-hostname ev3dev-rpi` |
| 57 | +{:.alert .alert-info} |
| 58 | + |
| 59 | +Finally do `sudo reboot`. |
| 60 | + |
| 61 | +## Set up development environment ## |
| 62 | + |
| 63 | +I prefer to have a passwordless ssh to the robot. For this I [add a user with |
| 64 | +the same login name as on my development computer.][users] Remember to give |
| 65 | +yourself sudo rights as explained on that same page. After adding that user, |
| 66 | +you can [set up passwordless ssh][passwordless]. |
| 67 | + |
| 68 | +{% include icon.html type="danger" %} |
| 69 | +This doesn't matter on BrickPi, but on robots with a screen, you will *not* be |
| 70 | +able to launch programs using the Brickman interface. For this, you *must* use |
| 71 | +the `robot` user! Only set up your own user account if you really know what you |
| 72 | +are doing. |
| 73 | +{:.alert .alert-danger} |
| 74 | + |
| 75 | +[users]: https://www.raspberrypi.org/documentation/linux/usage/users.md |
| 76 | +[passwordless]: https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md |
| 77 | + |
| 78 | +With passwordless ssh and the same username on the ev3dev machine, you can go |
| 79 | +`ssh ev3dev` and you're in! |
| 80 | + |
| 81 | +Next we need a versioning system. That's a tool for tracking changes you made |
| 82 | +to your code. Coincidentally the same tool is also very handy for transferring |
| 83 | +code between computers or sharing it on the web. There are several, but I |
| 84 | +prefer git. It's not installed by default so you have to do: |
| 85 | + |
| 86 | + sudo apt-get update |
| 87 | + sudo apt-get install git |
| 88 | + git config --global user.email "you@example.com" |
| 89 | + git config --global user.name "Your Name" |
| 90 | + |
| 91 | +Now let's make a new project using our versioning system. Just type: |
| 92 | + |
| 93 | + mkdir pyproject.git |
| 94 | + git init --bare myproject.git |
| 95 | + |
| 96 | +Yay! Git is initialised in the new folder we created. We have a new project |
| 97 | +repository. Now we can clone this repository on our development machine, but |
| 98 | +first we need to do a little more configuring, to automatically deploy our |
| 99 | +code when we push it back to the ev3dev brick. |
| 100 | + |
| 101 | + nano myproject.git/hooks/post-receive |
| 102 | + |
| 103 | +add these lines. If you created a user with your own name, replace `robot` |
| 104 | +with that name: |
| 105 | + |
| 106 | + #!/bin/sh |
| 107 | + git --work-tree=/home/robot/myproject --git-dir=/home/robot/myproject.git checkout -f |
| 108 | + |
| 109 | +and finally do: |
| 110 | + |
| 111 | + chmod +x myproject.git/hooks/post-receive |
| 112 | + |
| 113 | +## On to PyCharm ## |
| 114 | + |
| 115 | +Now typing code in nano is fine, but it's much nicer to do it inside a real |
| 116 | +IDE (Integrated Development Environment). There are many, like Visual Studio, |
| 117 | +Eclipse and PyCharm. My favourite is [PyCharm Community Edition][pycharm]. |
| 118 | +So go grab a copy of that one and start it on your development machine. |
| 119 | + |
| 120 | +[pycharm]: https://www.jetbrains.com/pycharm/ |
| 121 | + |
| 122 | +{% include screenshot.html source="/images/osx/PyCharm/welcome.png" %} |
| 123 | + |
| 124 | +What we are going to do now is make a clone of our project on the ev3dev |
| 125 | +machine to start working on it on the development machine. |
| 126 | +In the Welcome dialog choose: 'Checkout from version control' > 'Git' |
| 127 | +Now type the hostname of the ev3dev machine, followed by a semicolon the |
| 128 | +projectname. In the other fields choose a nice parent and project directory. |
| 129 | +(You might have to add `.local` after `ev3dev` depending on your DNS setup.) |
| 130 | + |
| 131 | +{% include screenshot.html source="/images/osx/PyCharm/clone-repo.png" %} |
| 132 | + |
| 133 | +When all goes wel you get a new window with your fresh empty project. If the |
| 134 | +'testing' dialog stays on screen for a long time, it might be that your |
| 135 | +PyCharm master password is needed for your PyCharm password storage. Cancel |
| 136 | +the checkout, type the master password and try again. |
| 137 | + |
| 138 | +So let's add some code. Right click on the 'myproject' folder in the left |
| 139 | +column and choose new > python file. Name it `run-motor`. PyCharm will ask |
| 140 | +if you want to add it to Git. That's nice! Of course we want that. |
| 141 | + |
| 142 | +Now add the following code: |
| 143 | + |
| 144 | +{% highlight python %} |
| 145 | +from ev3dev.auto import OUTPUT_A, Motor |
| 146 | +import time |
| 147 | + |
| 148 | +m = Motor(OUTPUT_A) |
| 149 | +m.run_forever(duty_cycle_sp = 100) |
| 150 | +time.sleep(1) |
| 151 | +m.stop() |
| 152 | +{% endhighlight %} |
| 153 | + |
| 154 | +When you're done press cmd-k or choose VCS > Commit Changes... |
| 155 | + |
| 156 | +The commit message is box describing the changes you made to your code. That's |
| 157 | +handy later on when you do a lot of changes. For now type: `first commit`. |
| 158 | +Then go over the commit button and choose "commit and push" from the dropdown. |
| 159 | +In the next dialog click make sure to select the "alternative branch checkbox" |
| 160 | +and click "Push". Voila! Our code has arrived on the ev3dev brick. Let's take |
| 161 | +a look. |
| 162 | + |
| 163 | +## Running the code ## |
| 164 | + |
| 165 | +It's time to let the first motor run. Plug it into port A on your brick and |
| 166 | +ssh to your ev3dev brick. Maybe you have the terminal still open. |
| 167 | + |
| 168 | +Now go |
| 169 | + |
| 170 | + cd myproject |
| 171 | + python run_motor.py |
| 172 | + |
| 173 | +Tataaa! The motor is running full power for a second. |
| 174 | + |
| 175 | +## Installing ev3dev-python on the development machine ## |
| 176 | + |
| 177 | +Back to the development machine. Maybe you noticed a problem: PyCharm puts |
| 178 | +red curly lines under the ev3dev library. |
| 179 | + |
| 180 | +{% include screenshot.html source="/images/osx/PyCharm/missing-lib.png" %} |
| 181 | + |
| 182 | +And that's logical, because the ev3dev library is missing on the development |
| 183 | +machine. If we install it we won't be able to run motors, but the documentation |
| 184 | +and autocomplete will be active. So on your development machine start a |
| 185 | +terminal and do: |
| 186 | + |
| 187 | + git clone https://github.com/rhempel/ev3dev-lang-python.git |
| 188 | + python ev3dev-lang-python/setup.py install |
| 189 | + rm -r ev3dev-lang-python/ # Optional to remove the downloaded files. It's installed now anyway. |
| 190 | + |
| 191 | +## Using the power of the IDE ## |
| 192 | + |
| 193 | +With the IDE (PyCharm) set up and the library installed you can code much |
| 194 | +faster. PyCharm will highlight most coding errors and typos. It will also |
| 195 | +suggest to autocomplete your code and show documentation. You can start |
| 196 | +typing `m.` and pycharm will suggest all possible methods and properties. |
| 197 | +Choose one. Now you can put your cursor inside the property and press F1 to |
| 198 | +see the docs. Or press cmd-down arrow to look inside the library where this |
| 199 | +property is defined. Neat huh? Happy coding. |
0 commit comments