SoftWare Guide PDF
SoftWare Guide PDF
Software Guide
1 Introduction
Welcome to the software guide. For working with the labs, we've designed a set of software that
should let you easily vary feedback control parameters in live-running systems controlled by your
Teensy Board. This software is brand new, and part of what we're doing with this iteration of the
course is testing it out. It is all open-source, and built using open-source tools (part of why we
picked Teensy!) so if you want to mess with it, do whatever you want with it, just be aware that
if you change things about it, it may not work, so be ready to re-download the code if needed!
The idea behind the code is shown in the Figure 1 below.
Figure 1: The basic workow of our software for controlling and analyzing things!
1. The Teensy interfaces with the DC Motor by sending pulse-width modulated (pwm) signals
to the H-Bridge motor driver and by reading the position/speed sensor (encoder). Its pwm
output signal is derived from sensor readings in a feedback control loop (that you will modify).
The control loop's behavior is adjusted by values/parameters sent to it from a python-based
local server running on your computer using serial communication. In addition, at regular
intervals, the Teensy sends its measurements back to the Python server.
3. The Browser-based GUI plots measured data from the Teensy, passed to it by the local server,
and provides simple interface for users to change the Teensy feedback loop parameters, which
it passes on to the local server.
• Server Code: This is a Python script that talks with the Teensy over serial, does some
calculations, and then talks with the GUI code in your browser. It is the "middle-person" of
our software stack.
1
• GUI Code: Your GUI (Graphical User Interface) is basically just a web-page that is hosted
on your machine. It is written in standard html/css/javascript. Don't worry, it is locally
hosted and only you have access to it! It lives at localhost:3000 in your browser url eld when
the server script is running.
3 Questions
Why did we pick a browser-based GUI for controlling the DC Motor?:
The reason really comes down to compatibility across lots of platforms. One of the goals we
have with this course is to see how eectively we can enable the study of control theory in a dis-
tributed environment. Python does a pretty good job of maintaining libraries that function reliably
(or at least predictably) across the Windows/Mac/Unix/Linux spectrum, but plotting/graphics,
particularly in real-time, is something that is bit tougher to do across all platforms. Web Browsers
can be seen as a nice solution to this problem. Pretty much everybody and their respective operat-
ing system has a web browser, and most modern web-browsers can provide a uniform environment
in which we can deploy graphics, since graphics in the browser are pretty standardized (with some
exceptions).
1. Flask and its support for Eventletis used for creating a functioning WebSocket server. This
guy is basically like the Flask expert and his work was very helpful in getting this aspect of
the software working
2. Pyserial is for its ability (as its name suggests) to give Python reliable serial port access.
For the Web GUI, standard html, css, Javascript, Bootstrap, jQuery were all used. Plotting
was done using the D3 library.
4 Installation
4.1 Getting Started
The code distribution we'll use for this software introduction is found in the supported les pro-
vided to you with this document. This is the base le for testing and setting up.
While the Python and html/javascript les are the same for all labs, we use dier-
ent microcontroller code sets for each lab, these codes will be provide to you in each
lab.
Download the .rar le, extract it, and look inside. You'll see the following things:
• An empty directory called csv_les. Don't worry about this thing for right now.
• The le server .py. This is the Python script you will run on your machine for establishing
communiation with your Teensy and browser GUI (Graphical User Interface)
• The le req.txt. This is a le that lists libraries needed in addition to normal Python. Don't
worry about it right now, but we'll use it during install.
• The directory static. This has the javascript/css libraries your browser GUI uses. We use
local copies of these libraries so you can run this software when not connected to the internet.
Just leave this as it is.
2
• The directory called teensy_test. These contain simple communication check microcontroller
code le for the Teensy 3.2. They are good "sanity check" les.
Ok onto the install. We'll rst start with the Arduino/Teensy Environment.
In the standard way of taking this course, you'll be using an Teensy 3.2 to directly control the
systems we're analyzing (DC Motor). As a result, you will need a functioning Arduino IDE
environment on your machine both to upload code and to let your computer have serial access to
it (This is not the only way to program the Teensy, but the one we will use). We'll point you to
get the Arduino IDE running (if you don't have that already).
Depending on what your background is, you can either just head on over to the Arduino
Download Site, and install it. Or maybe, if you'd like more details with the install, Arduino has
some nice walkthroughs for each of the big operating system families:
• Windows: https://www.arduino.cc/en/Guide/Windows
• Mac OS X: https://www.arduino.cc/en/Guide/MacOSX
• Linux: http://playground.arduino.cc/Learning/Linux
Windows does not come with Python usually. If you already have it, determine what version you
have:
One easy way to check on your Python is to open up a Windows PowerShell up and inside, type:
python version
Some text should come back telling you if you have Python and what version it is. Red text
probably means you don't have it. Our code should work on both Python versions 2.7 and
If you do not have Python installed, you will need
above as well as Python 3.3 and above.
to do that. I'd recommend getting Python 3 for this software, though both families (2.7+
and 3+ will work). Go to the Python Download Page: https://www.python.org/downloads/,
I'd recommend the .exe installer if
nd the appropriate install version for your computer (
you're ok with that), follow the well-written directions, and you should be good to go.
IMPORTANT!!!: It is very important that during the install you check the option
about adding Python to your Path which happens on the rst/second window of the
installer executable!!!
3
4.3.1 PIP
The next piece which you need is pip, which is a nice Python Package Manager that we use to
install the appropriate install les. If you download either the newest version of Python 2 (after
v 2.7.9) and any version of Python3 starting with Python 3.4 you should have pip preinstalled.
To make sure you have pip, run the following command in the Windows PowerShell (not the
Python shell!) or Windows Command prompt!:
pip version
If some stu comes back giving you a version number, you'll be good to go. If not
I'd recommend you update your Python (using the Python download link above) and
in the process get pip by default!
4.3.2 TROUBLESHOOTING
Python can sometimes be a little dicult to get working on a Windows machine. The following
resources may be helpful:
(Note: if you're using Python3 you may (not necessarily) need to do pip3> install -r
req.txt instead!)
It may take a minute or so depending on your machine. A whole bunch of text should y by
in a neutral color like green (if red stu appears, it *may* mean there was an error in install. At
that point I'd head to the email and we'll try to see what we can gure out.).
If the install was a success, the next thing you need to do is start the server using Python. Type:
python server.py
(Note: Depending on how you set up your Python, it may be called by the command "python3"
rather than just "python". Try both if you have issues. On my several test installs on "clean"
Windows machines, when installing Python 3.5, it just established "python" and "pip" as the ap-
propriate commands, but it is possible that the could end up as "python3" and "pip3".)
After starting running, some stu will appear, but as long as it isn't very long it means you
should be good. Head on over to a browser (Google Chrome, Mozilla Firefox,...etc) and in the
4
URL eld type:
localhost:3000
You should see something similar to that shown in the image 2 below.
Figure 2 shows what the Browser-Based GUI should look (sort of depending on version) like
when you rst open it. In the top pull-down tab at right, you should hopefully see the Serial port
associated with your microcontroller (assuming drivers have been isntalled for it which will have
been the case if you succssessfully uploaded the test code in the previous step).
If you've got stu working the way describe above, you should be good.
Note: If you are able to, updating to the most recent version of OSX can minimize the chance of
installation problems popping up with Python associated with stdio.h not being present
If you've got a Mac, chances are you most likely already have Python installed on your machine.
If you go to the Launchpad and search for "Terminal" you should nd a symbol that looks sort of
like Figure 3:
If you're not familiar with the terminal, I would check out this link:
just to see some basic commands. It can seem a bit intimidating at rst, but it we aren't doing
anything too dicult with it so just a bit of practice should be enough.
python version
python3 version
to see if you have Python Version 3. Some text should come back telling you if you have Python
and what version it is. Our code should work on both Python versions 2.7 and above as well as
5
Python 3.3 and above. If you do not have Python installed, you need to do that. I'd recommend
getting Python 3 for this software, though both families (2.7+ and 3+ will work). Go to the
Python Download Page,
https://www.python.org/downloads/
nd the appropriate install version for your computer, follow the directions, and you should be
good.
4.4.1 PIP
The next piece which you need is pip, which is a nice Python Package Manager that we use to
install the appropriate install les. If you download either the newest version of Python 2 (after v
2.7.9) and any version of Python3 starting with Python3.4 come with pip preinstalled. To make
sure you have pip if you're using the Python 2 family, run:
pip version
pip3 version
If some stu comes back giving you a version number, you'll be good to go. If not I'd recommend
you update your Python (using the Python download link above) and in the process get pip by
default!
(Note: if you're using Python3 you may (not necessarily) need to do pip3> install -r
req.txt instead!)
It may take a minute or so depending on your machine. A whole bunch of text should y by
in a neutral color like green (if red stu appears, it *may* mean there was an error in install. At
that point I'd head to the email and we'll try to see what we can gure out.).
If the install was a success, the next thing you need to do is start the server using Python. Type:
python server.py
(Note: Depending on how you set up your Python, it may be called by the command "python3"
rather than just "python". Try both if you have issues. On my several test installs, when installing
Python 3.5, it just established "python" and "pip" as the appropriate commands, but it is possible
that the could end up as "python3" and "pip3".)
After starting running, some stu will appear, but as long as it isn't very long it means you
should be good. Head on over to a browser (Safari, Vivaldi,...etc) and in the URL eld type:
localhost:3000
You should see something similar to that shown in the image 4 below.
Figure 4 shows what the Browser-Based GUI should look (sort of depending on version) like
when you rst open it. In the top pull-down tab at right, you should hopefully see the Serial port
6
Figure 4: Browser-Based GUI
associated with your microcontroller (assuming drivers have been isntalled for it which will have
been the case if you succssessfully uploaded the test code in the previous step).
If you've got stu working the way describe above, you should be good.
Depending on your Unix/Linux distribution, you may already have Python. You can check by
opening up a terminal up and typing:
python version
python3 version
to see if you have Python Version 3. Some text should come back telling you if you have Python
and what version it is. Our code should work on both Python versions 2.7 and above as well as
Python 3.3 and above. If you do not have Python installed, you need to do that. I'd recommend
getting Python 3 for this software, though both families (2.7+ and 3+ will work). Go to the
Python Download Page:
https://www.python.org/downloads/
nd the appropriate install version for your computer, follow the directions, and you should be
good.
Depending on your settings you could also just do:
for Python 2 or
for Python 3.
4.5.1 PIP
The next piece which you need is pip, which is a nice Python Package Manager that we use to
install the appropriate install les. If you download either the newest version of Python 2 (after v
2.7.9) and any version of Python3 starting with Python3.4 come with pip preinstalled. To make
sure you have pip if you're using the Python 2 family, run:
pip version
7
If you're using the Python 3 series, run:
pip3 version
If some stu comes back giving you a version number, you'll be good to go. If not I'd recommend
you update your Python (using the Python download link above) and in the process get pip by
default!
It may take a minute or so depending on your machine. A whole bunch of text should y by
in a neutral color like green (if red stu appears, it *may* mean there was an error in install. At
that point I'd head to the email and we'll try to see what we can gure out.).
Note: One bug I ran into when installing on my Ubuntu v15.04 machine was I didn't have
developer packages installed...If you get an error, try running:
If the install was a success, the next thing you need to do is start the server using Python. Type:
python server.py
After starting running, some stu will appear, but as long as it isn't very long it means you
should be good. Head on over to a browser (Chromium, Firefox...etc) and in the URL eld type:
localhost:3000
You should see something similar to that shown in the image 5 below.
8
Figure 5 shows what the Browser-Based GUI should look (sort of depending on version) like
when you rst open it. In the top pull-down tab at right, you should hopefully see the Serial port
associated with your microcontroller (assuming drivers have been isntalled for it which will have
been the case if you succssessfully uploaded the test code in the previous step).
If you've got stu working the way describe above, you should be good.