All you need to write Python code is a text editor. Similar to the extension .c or .m for C or MATLAB code, respectively, python code source files are saved with extension .py. Open a text editor and save to following as hello.py:

# Any line that begins with a`#` is treated as a comment
print("Hello World")

# Let's import a built-in module and do some math
import math

r = 2
A = math.pi * r**2 # Compute the Area = pi x r^2
print("Circle with radius %.1f has an area = %.1f" % (r, A))

The code is written, but how do we run it?

Python Interpretor

Like MATLAB (and unlike C), Python is an interpreted language. So we need the Python interpretor in order to run code. Download and install the Python interpretor. Now you should be able to run hello.py or alternatively type the commands into the Python interpretor line-by-line and see the results.

So What is IPython?

The Python interpretor runs our code, but certain features are missing from the basic interpretor if we plan to use it interactively. The IPython program extends the Python interpretor by providing features like:

  • tab completion
  • colored input/output
  • a set of terminal/bash commands like cd, ls, rm

which, together, provides a much better interactive experience.

Standard Library vs Add-on Modules

Python by default comes with a set of modules called the Standard Library. Among others, these include basic math in the math module, system path and error checking in the sys module, and file and directory operations with the os module. A number of useful data types are also defined like strings, lists, sets, and dictionaries.

More advance functions and data types are defined by add-on modules. For example, numpy defines arrays, matrices, and random number generators, scipy has signal processing and numerical solver routines, and pandas has excellent data manipulation support. These add-on modules must be installed by the user. A wealth of such modules are available, many of which are open source with permissive licenses, making them attractive foundations and tools with which to build a project.

Back to GNC

Guidance, navigation, and control (GNC) work is at the intersection of algorithms, hardware, and software. It involves algorithm development, software-based simulation, hardware-in-the-loop simulation, and the analysis of data collected during testing campaigns. A general purpose programming language like Python coupled with the wealth of libraries and resources available pair beautifully with the broad demands of GNC and systems engineering.

Sample Program Flow: AHRS

An Attitude and Heading Reference System (AHRS) can rely on sensors like gyros, magnetometers, and accelerometers to continuously compute the 3D orientation. This could be a small aircraft or a virtual reality headset. Below is a snapshot of a program developed to test and evaluate several AHRS implementations.


# Import modules
import numpy as np
import pandas as pd

# Custom modules
import calib_mag
import AHRS

# Load Sensor Data
df = pd.read_csv('2018_imu_test_data.csv')
t = df['time']
mag = df[['hx', 'hy', 'hz']] # magnetometer
accel = df[['ax', 'ay', 'az']] # accelerometer
gyro  = df[['wx', 'wy', wz']]

# Calibrate magnetometer
mag = calib_mag.calibrate_magnetometer(mag, calib_param='3Bias')

# Declare AHRS object
ahrsObj = AHRS(name='2018 Test Data', mag_aiding = True,
               refine_technique='average')

# User first epoch of sensor data to initialize pitch, roll, and heading
k = 0
wx , wy, wz = gyro.loc[k]
ax, ay, az = accel.loc[k]
hx, hy, hz = mag.loc[k]

ahrsObj.initialize(t[k], wx, wy, wz, ax, ay, az, hx, hy, hz)
ahrsObj.store(t[k])

k+=1
while ahrsObj.is_static(wx, wy, wz, ax, ay, az):
    wx , wy, wz = gyro.loc[k]
    ax, ay, az = accel.loc[k]
    hx, hy, hz = mag.loc[k]

    ahrsObj.refine_init(t[k], wx, wy, wz, ax, ay, az, hx, hy, hz);
    ahrsObj.store(t[k])
    k+=1

while k < len(t):
    wx , wy, wz = gyro.loc[k]
    ax, ay, az = accel.loc[k]
    hx, hy, hz = mag.loc[k]

    ahrsObj.step(t[k], wx, wy, wz, ax, ay, az, hx, hy, hz)
    ahrsObj.store(t[k])
    k+=1

The definition of the algorithm as a class called AHRS provides for a cleaner final script and is setup for reuse. Below is a plot comparing several AHRS algorithm implementations. The analysis was implemented and visualized using a variant of the above Python-based program.

Plot of Pitch and Roll estimates comparing AHRS implementations