aeroshield

Subpackages

Submodules

Package Contents

Classes

AeroShield

DummyShield

AeroController

Attributes

__version__

aeroshield.__version__
class aeroshield.AeroShield(baudrate: int | None = 115200, port: str | None = None)[source]
RUN = 1
STOP = 2
TIMEOUT = 3
find_arduino() str[source]

Get the name of the port that is connected to Arduino. Raises exception if no port was found.

Raises:

AeroShieldException – Raised if no Arduino was found.

Returns:

COM port of the Arduino.

Return type:

str

static raw_angle_to_deg(raw: int) float[source]

Convert raw angle to degrees.

Parameters:

raw (int) – 12-bit value of angle sensor.

Returns:

Angle value scaled to degrees.

Return type:

float

static raw_angle_to_rad(raw: int) float[source]

Convert raw angle to radians.

Parameters:

raw (int) – 12-bit value of angle sensor.

Returns:

Angle value scaled to radians.

Return type:

float

calibrated_angle_deg(raw_angle: int) float[source]

Convert angle to degrees and calibrate with zero angle.

Parameters:

raw_angle (int) – Raw 12-bit angle value.

Returns:

Calibrated angle in degrees.

Return type:

float

static raw_pot_to_percent(raw: int) float[source]

Convert 10-bit potentiometer reading to percentage value.

Parameters:

raw (int) – 10-bit potentiometer value.

Returns:

Potentiometer value as percentage [0, 1).

Return type:

float

read() tuple[float][source]

Read data from Arduino. Convert potentiometer and angle readings. Calibrate angle to zero reference.

Raises:

AeroShieldException – Raised if no data was received. This can happen if there was no write command preceding a call to read.

Returns:

Converted and calibrated potentiometer and angle readings, in that order.

Return type:

tuple[float]

static saturate(value: float, bits: int) int[source]

Saturate value between 0 and 2**bits - 1.

Parameters:
  • value (float) – Raw value.

  • bits (int) – Number of bits.

Returns:

Saturated value.

Return type:

int

write(flag: int, motor: float) int[source]

Write run/stop flag and motor value to Arduino. Saturate the motor value.

Parameters:
  • flag (int) – AeroShield.RUN or AeroShield.STOP. The former signals normal running mode, the latter tells the Arduino to stop the motor.

  • motor (float) – Motor value.

Returns:

Saturated 8-bit motor value.

Return type:

int

calibrate()[source]

Read out a zero reference. Pendulum should be at rest when calling this method.

stop()[source]

Send stop signal to Arduino.

open()[source]

Reset buffers and open connection to Arduino if it is not open already. Wait for AeroShield.TIMEOUT seconds to make sure connection is established.

close(*args)[source]

Close connection to Arduino.

__enter__()[source]
__exit__(*args)[source]
exception aeroshield.AeroShieldException[source]

Bases: Exception

Common base class for all non-exit exceptions.

class aeroshield.DummyShield[source]

Bases: aeroshield.aeroshield.aeroshield.AeroShield

read() tuple[float][source]

Read data from Arduino. Convert potentiometer and angle readings. Calibrate angle to zero reference.

Raises:

AeroShieldException – Raised if no data was received. This can happen if there was no write command preceding a call to read.

Returns:

Converted and calibrated potentiometer and angle readings, in that order.

Return type:

tuple[float]

write(flag: int, motor: float)[source]

Write run/stop flag and motor value to Arduino. Saturate the motor value.

Parameters:
  • flag (int) – AeroShield.RUN or AeroShield.STOP. The former signals normal running mode, the latter tells the Arduino to stop the motor.

  • motor (float) – Motor value.

Returns:

Saturated 8-bit motor value.

Return type:

int

open()[source]

Reset buffers and open connection to Arduino if it is not open already. Wait for AeroShield.TIMEOUT seconds to make sure connection is established.

close(*args)[source]

Close connection to Arduino.

class aeroshield.AeroController(aero_shield: aeroshield.aeroshield.AeroShield)[source]
variables() None[source]

Define variables to be used by the controller or saved during the experiment.

add_tracked_variable(name: str, size: int | None = 1) dict[str, int][source]

Add a variable to the list of variables whose value should be tracked during the experiment and returned afterwards. Variables should be instance variables of the class, otherwise they won’t be accessible!

Parameters:
  • name (str) – Name of the variable, without ‘self.’

  • size (int, optional) – Size of the variable, e.g. 3 for a three-dimensional position vector. Defaults to 1, i.e. single values.

Returns:

A copy of the current map of tracked variables and their respective size.

Return type:

dict[str, int]

controller(t: float, dt: float, ref: float, pot: float, angle: float) float[source]

Implement the controller here. You can subclass AeroController and overwrite the controller.

Parameters:
  • t (float) – Time since start of run in seconds.

  • dt (float) – Length of current time step in seconds.

  • ref (float) – reference value for the current step.

  • pot (float) – potentiometer value in percent.

  • angle (float) – calibrated angle in degrees.

Returns:

input value for motor. the motor value will be saturated (int between 0 and 255 incl.) afterwards

Return type:

float

run(freq: int, cycles: int, ref: float | int | Iterable[float | int] | None = None, live_plotter: aeroshield.plotting.LivePlotter | None = None) numpy.ndarray[source]

Run the controller on the AeroShield.

Parameters:
  • freq (int) – Desired frequency of the loop.

  • cycles – Number of cycles to run the experiment.

  • ref (np.ndarray[float|int]) – The reference to follow should have a lenght equal to freq * time.

  • live_plotter (LivePlotter) – Optional. LivePlotter instance to use for displaying a live plot.

_update_hist(hist: numpy.ndarray[float], cntr: int, t: float, ref: float, pot: float, angle: float, motor: float)[source]

Update hist array with variables of the current iteration (cntr). If variables were added to extra_hist_vars, add them to the hist as well.

Parameters:
  • hist (np.ndarray[float]) – array to update.

  • cntr (int) – Iteration counter. Provides the first index to hist.

  • t (float) – Time.

  • ref (float) – Current reference value.

  • angle (float) – Current pendulum angle

  • motor (float) – Current Motor value.