tools.calibrate

class tools.calibrate.Calibrate

A script to interactively calibrate the module

General flow: #. Sanity check (rack power, necessary file structure exists)

  1. Input calibration. One of:

    • low-accuracy 10V in

    • low-accuracy 5V in

    • high-accuracy 0-10V in

  2. Output calibration

  3. Save calibration

  4. Idle for reboot

Output calibration only applies to CV1; all other outputs will use the same calibration values.

calibrate_output(cv_n, calibration_values, input_readings)

Send volts from CVx to AIN, adjusting the duty cycle so the output is correct.

Parameters
  • cv_n – A value 0 <= cv_n < len(cvs) indicating which CV output we’re calibrating

  • calibration_values – The array of calibration values we append our results to

  • input_readings – The duty cycles of AIN corresponding to 0, 1, 2, …, 9, 10 volts

check_directory()

Check if /lib exists. If it does not, create it

coarse_output_calibration(cv, goal_duty, start_duty, step_size)

Perform a fast, coarse calibration to bring the CV output’s duty cycle somewhere close

This will exit if either the calibration is within +/- the step_size OR if the measured duty cycle is higher than the goal (i.e. we’ve over-shot the goal)

Parameters
  • cv – The CV output pin we’re adjusting

  • goal_duty – The AIN duty cycle we’re expecting to read

  • start_duty – The CVx duty cycle we’re applying to the output initially

  • step_size – The amount by which we adjust the duty cycle up to reach the goal

Returns

The adjusted output duty cycle

classmethod config_points() List[ConfigPoint]

Returns a list of ConfigPoints describing this script’s configuration options. By default this function returns an empty list. Override it if you’d like configuration points.

classmethod display_name()

Push this script to the end of the menu.

fine_output_calibration(cv, goal_duty, start_duty, step_size, prev_step_size)

Perform a slower, fine calibration to bring the CV output’s duty cycle towards the goal

This exits if the measured duty cycle is within +/- 2*step_size OR if we make 2*prev_step_size adjustments

Parameters
  • cv – The CV output pin we’re adjusting

  • goal_duty – The AIN duty cycle we’re expecting to read

  • start_duty – The CVx duty cycle we’re applying to the output initially

  • step_size – The amount by which we adjust the duty cycle up/down to reach the goal

  • prev_step_size – The previous iteration’s step size, used to limit how many adjustments we make

Returns

The adjusted output duty cycle

input_calibration_high()

High accuracy input calibration

User is prompted to connect 0, 1, 2, …, 9, 10V

Returns

The sample readings for 0 to 10V (inclusive)

input_calibration_low10()

Low-accuracy, 10V input calibration

Prompt the user for 0 and 10V inputs only

Returns

The sample readings for 0 and 10V

input_calibration_low5()

Low-accuracy, 5V input calibration

Prompt the user for 0 and 5V inputs only. The 5V reading is extrapolated to 10V.

Returns

The sample readings for 0 and 10V

last_saved()

Return the ticks in milliseconds since last save.

load_state_bytes() bytes

Check disk for saved state, if it exists, return the raw state value as bytes.

Check for a previously saved state. If it exists, return state as a byte string. If no state is found, an empty string will be returned.

load_state_json() dict

Load previously saved state as a dict.

Check for a previously saved state. If it exists, return state as a dict. If no state is found, an empty dictionary will be returned.

main()

Override this method with your script’s main loop method.

read_sample()

Read from the raw ain pin and return the average across several readings

Returns

The average across several distinct readings from the pin

remove_state()

Remove the state file for this script.

save_state()

Encode state and call the appropriate persistence save method.

Override this class with the script specific encoding of state into the persistence format that best matches its use case. Then call the appropriate save method, such as save_state_json(state). See the class documentation for a full example.

save_state_bytes(state: bytes)

Take state in persistence format as bytes and write to disk.

Note

Be mindful of how often save_state_bytes() is called because writing to disk too often can slow down the performance of your script. Only call save state when state has changed and consider adding a time since last save check to reduce save frequency.

save_state_json(state: dict)

Take state as a dict and save as a json string.

Note

Be mindful of how often save_state_json() is called because writing to disk too often can slow down the performance of your script. Only call save state when state has changed and consider adding a time since last save check to reduce save frequency.

text_wait(text, duration)

Display text on the screen and block for the specified duration

Parameters
  • text – The text to display

  • duration – The duration to wait in seconds

wait_for_b1(wait_fn=None)

Wait for the user to press B1, returning to the original state when they have

Parameters

wait_fn – A function to execute while waiting (e.g. refresh the UI)

wait_for_b2(wait_fn=None)

Wait for the user to press B2, returning to the original state when they have

Parameters

wait_fn – A function to execute while waiting (e.g. refresh the UI)

wait_for_voltage(voltage)

Wait for the user to connect the desired voltage to ain & press b1

Parameters

voltage – The voltage to instruct the user to connect. Used for display only

Returns

The average samples read from ain (see read_sample)

class tools.calibrate.CalibrationValues(mode)

Wrapper class for the input & output calibration values used for analogue inputs & outputs

In low-accuracy mode, input_calibration_values is a length 2 array with the raw samples taken at 0V and 10V

In high-accuracy mode, input_calibration_values is a length 11 array with the raw samples taken at 0-10V, in 1V increments.

In either mode, output_calibration_values is a length 11 array with the raw samples taken at 0-10V, in 1V increments.

Parameters

mode – The calibration mode, one of MODE_LOW_10, MODE_LOW_5, or MODE_HIGH

save()

Save the calibration readings to /lib/calibration_values.py

Note: this will overwrite all previous calibrations