tools – General purpose tools

Common tools for timing, data logging, and linear algebra.

Timing tools

wait(time)

Pauses the user program for a specified amount of time.

Parameters

time (Number, ms) – How long to wait.

class StopWatch

A stopwatch to measure time intervals. Similar to the stopwatch feature on your phone.

time() int: ms

Gets the current time of the stopwatch.

Returns

Elapsed time.

pause()

Pauses the stopwatch.

resume()

Resumes the stopwatch.

reset()

Resets the stopwatch time to 0.

The run state is unaffected:

  • If it was paused, it stays paused (but now at 0).

  • If it was running, it stays running (but starting again from 0).

Input tools

read_input_byte() int | None

Reads one byte from standard input without blocking.

Returns

The numeric value of the byte read or None if no data is available.

Linear algebra tools

Changed in version 3.3: These tools were previously located in the pybricks.geometry module.

class Matrix(rows)

Mathematical representation of a matrix. It supports addition (A + B), subtraction (A - B), and matrix multiplication (A * B) for matrices of compatible size.

It also supports scalar multiplication (c * A or A * c) and scalar division (A / c).

A Matrix object is immutable.

Parameters

rows (list) – List of rows. Each row is itself a list of numbers.

T

Returns a new Matrix that is the transpose of the original.

shape

Returns a tuple (m, n), where m is the number of rows and n is the number of columns.

vector(x, y) Matrix
vector(x, y, z) Matrix

Convenience function to create a Matrix with the shape (2, 1) or (3, 1).

Parameters
  • x (float) – x-coordinate of the vector.

  • y (float) – y-coordinate of the vector.

  • z (float) – z-coordinate of the vector (optional).

Returns

A matrix with the shape of a column vector.

cross(a, b) Matrix

Gets the cross product a × b of two vectors.

Parameters
  • a (Matrix) – A three-dimensional vector.

  • b (Matrix) – A three-dimensional vector.

Returns

The cross product, also a three-dimensional vector.