<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Biosystems Engineering Blog (Posts about python)</title><link>https://kwk.systems/blog/</link><description></description><atom:link href="https://kwk.systems/blog/categories/python.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:info@kwk.systems"&gt;Kurt Kremitzki&lt;/a&gt; 
&lt;a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"&gt;
&lt;img alt="Creative Commons License BY-SA"
style="border-width:0; margin-bottom:12px;"
src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png"&gt;&lt;/a&gt;</copyright><lastBuildDate>Thu, 16 May 2024 22:41:32 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Using Running Averages in Python to Validate Sensor Data</title><link>https://kwk.systems/blog/posts/using-running-averages-in-python-to-validate-sensor-data/</link><dc:creator>Kurt Kremitzki</dc:creator><description>&lt;div&gt;&lt;p class="h3"&gt;Handling Signals&lt;/p&gt;
&lt;p&gt;If you are writing code to respond to sensor data, you should use a bit of logical
abstraction to have clean code that doesn't become harder to work on with time.
For example, at a low level, you're getting some signal, likely a voltage, that is
supposed to represent another physical signal (in an abstract sense), like a distance
or temperature. However, the signal you are actually accessing in the code is just
a measurement, e.g. a voltage and not the &lt;em&gt;true&lt;/em&gt; distance. At some point in the code,
hopefully a well-tested library already written by someone else, this voltage signal
has some math done to it and becomes a number that is hopefully pretty close to the
measurement we care about. Later on in the code, in regions &lt;em&gt;we&lt;/em&gt; are usually responsible
for, it would be nice to treat that number as the real thing, and make whatever system
we're designing to respond accordingly. Going in blindly, though, can be a recipe for
disaster. Why?&lt;/p&gt;
&lt;p class="h3"&gt;Trust, But Verify&lt;/p&gt;
&lt;p&gt;Sometimes physical components fail. Maybe someone's skin oil, left behind during setup
or installation, is changing some overall resistance and invaliding our assumptions, and
math, about a voltage signal across a wire. Water, rust, rot, and an infinite number of
other failure mechanisms can plague the best-designed system, so a good rule of thumb is to
trust, but verify.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://kwk.systems/blog/posts/using-running-averages-in-python-to-validate-sensor-data/"&gt;Read more…&lt;/a&gt; (2 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>control_theory</category><category>python</category><guid>https://kwk.systems/blog/posts/using-running-averages-in-python-to-validate-sensor-data/</guid><pubDate>Thu, 05 May 2016 10:09:04 GMT</pubDate></item><item><title>Primer on Python Projects</title><link>https://kwk.systems/blog/posts/primer-on-python-projects/</link><dc:creator>Kurt Kremitzki</dc:creator><description>&lt;div&gt;&lt;p&gt;I'm helping all the teams in my BAEN370 class robotics competition (essentially our final) since I competed in the ASABE Robotics Competition last year in New Orleans.
This post is advice mainly targeted at everyone in the class.
Here are a few useful snippets when starting your first big Python project.&lt;/p&gt;
&lt;p class="h3"&gt;Imports&lt;/p&gt;
&lt;p&gt;If you're working on a file, e.g. &lt;code class="docutils literal"&gt;code.py&lt;/code&gt;, and it's getting large, you can break off some of that code (usually functions) into another file. Let's say you name this file &lt;code class="docutils literal"&gt;functions.py&lt;/code&gt;. Then, in your original file, you would add to the top:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_ebdc28ae787e45a3b884ea606d39186a-1" name="rest_code_ebdc28ae787e45a3b884ea606d39186a-1" href="https://kwk.systems/blog/posts/primer-on-python-projects/#rest_code_ebdc28ae787e45a3b884ea606d39186a-1"&gt;&lt;/a&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;functions&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;function1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function2&lt;/span&gt; &lt;span class="c1"&gt;# etc&lt;/span&gt;
&lt;a id="rest_code_ebdc28ae787e45a3b884ea606d39186a-2" name="rest_code_ebdc28ae787e45a3b884ea606d39186a-2" href="https://kwk.systems/blog/posts/primer-on-python-projects/#rest_code_ebdc28ae787e45a3b884ea606d39186a-2"&gt;&lt;/a&gt;&lt;span class="c1"&gt;# if you have a lot of functions, you can use something like&lt;/span&gt;
&lt;a id="rest_code_ebdc28ae787e45a3b884ea606d39186a-3" name="rest_code_ebdc28ae787e45a3b884ea606d39186a-3" href="https://kwk.systems/blog/posts/primer-on-python-projects/#rest_code_ebdc28ae787e45a3b884ea606d39186a-3"&gt;&lt;/a&gt;&lt;span class="c1"&gt;# from functions import (function1, function2, ... ,&lt;/span&gt;
&lt;a id="rest_code_ebdc28ae787e45a3b884ea606d39186a-4" name="rest_code_ebdc28ae787e45a3b884ea606d39186a-4" href="https://kwk.systems/blog/posts/primer-on-python-projects/#rest_code_ebdc28ae787e45a3b884ea606d39186a-4"&gt;&lt;/a&gt;&lt;span class="c1"&gt;#                        functionN_minus_one, functionN)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An example of how to use this, in the context of BAEN370, would be using the following project structure:&lt;/p&gt;
&lt;pre class="literal-block"&gt;baen370/
├── run.py
├── navigation.py
└── sensors.py&lt;/pre&gt;
&lt;p&gt;Then, those files' contents would look something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://kwk.systems/blog/posts/primer-on-python-projects/"&gt;Read more…&lt;/a&gt; (3 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>python</category><guid>https://kwk.systems/blog/posts/primer-on-python-projects/</guid><pubDate>Sun, 10 Apr 2016 12:58:54 GMT</pubDate></item><item><title>Equation Solving with Python &amp; SymPy</title><link>https://kwk.systems/blog/posts/equation-solving-with-python/</link><dc:creator>Kurt Kremitzki</dc:creator><description>&lt;div&gt;&lt;p&gt;In engineering applications, the same equation will be solved over and over with
different values or measurements as inputs. Anticipating this, we can either
write one function for each variable which inputs all other variables, or take a
much easier route using &lt;a class="reference external" href="http://www.sympy.org/en/index.html"&gt;SymPy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The following example is a simple implementation of &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Manning_formula"&gt;Manning's formula&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;\[
v = \frac{k}{n} {R_h}^{2/3} S^{1/2}
\]&lt;/p&gt;
&lt;p&gt;with &lt;span class="math"&gt;\(k = 1.49\)&lt;/span&gt; since we most often work in English units in American
hydrology. Note that the variable &lt;code class="docutils literal"&gt;mannings_eqn&lt;/code&gt; is actually the above
expression set equal to 0, so that we see a &lt;span class="math"&gt;\(-v\)&lt;/span&gt; term.&lt;/p&gt;
&lt;p&gt;The following code creates Sympy &lt;code class="docutils literal"&gt;Symbol&lt;/code&gt;s for each variable in the expression.
In general, if we have &lt;span class="math"&gt;\(n\)&lt;/span&gt; variables in the expression, we can pass in a
&lt;code class="docutils literal"&gt;dict&lt;/code&gt; with &lt;span class="math"&gt;\(n-1\)&lt;/span&gt; key-value pairs (equations) with known values
to solve for the &lt;span class="math"&gt;\(n\)&lt;/span&gt;th variable. We can create &lt;code class="docutils literal"&gt;dict&lt;/code&gt;s with keys, the
set of knowns, and pass in any (independent) combination of &lt;span class="math"&gt;\(n-1\)&lt;/span&gt; variables from
our expression to get a number.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://kwk.systems/blog/posts/equation-solving-with-python/"&gt;Read more…&lt;/a&gt; (1 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>python</category><guid>https://kwk.systems/blog/posts/equation-solving-with-python/</guid><pubDate>Sun, 10 Apr 2016 11:12:16 GMT</pubDate></item></channel></rss>