Modules

biorobotics

Tickers

class biorobotics.Ticker(timer: int, freq: float, callback: callable, enable_gc: bool = False, use_hardware_id: bool = False)[source]

Timed callback manager

BioRobotics hardware timer ISR abstraction. Compared to pyb.Timer:

  • Removing the need of explicit tim.callback(micropython.schedule())

  • Timely garbage collection set on a ticker with GC=Enable

  • Other timer related functions

Note: the ticker is not started on initialization!

Construct Ticker

Parameters
  • timer – Number of the on-board hardware timer to use (start at 0)

  • freq – Frequency (in Hz) of the callback

  • callback – Callback to be attached to the timer

  • enable_gc – Set to True to disable general automatic garbage collection and instead run GC at the end of the user callback - This is recommended when the function is the loop() of the program

  • use_hardware_id – When set to True, the timer id is used as the id of an on-board timer. When set to False (default), the id is used to count over general purpose timers. The latter is useful to avoid conflicts with attached pins

start()[source]

Start ticker

stop()[source]

Stop ticker

Tic / Toc

biorobotics.tic() None[source]

Start time measurement

biorobotics.toc() int[source]

End time measurement, print and return results in microseconds

SerialPC

class biorobotics.SerialPC(channels: int)[source]

Create serial connection to send data asynchronously

Non-blocking serial TX over CN1 (ST-Link VCOM, UART3) on STM32 H743ZI2

On every .send(), a floating point value (4 bytes) for every channel is transmitted. The protocol is:

  • 3 identifier header bytes

  • 1 byte with the number of channels

  • 4 bytes with the current runtime in microseconds

  • multiples of 4 bytes

Constructor

Parameters

channels – Number of channels to send data over

send()[source]

Send packet

set(channel, value)[source]

Set data in channel, prepared to be sent

set_list(array)[source]

Set all channels with an array

AnlogIn

class biorobotics.AnalogIn(pin: str)[source]

Read analog signal

Extension of ADC

Constructor

An error will be throws if the pin does not have an Analog-to-Digital converted connected.

Parameters

pin – Name of a pin or Pin object

read() float[source]

Get analog input value between 0.0 and 1.0

Corresponding to 0 to 3.3V. The Nucleo appears to be 5V tolerant. The resolution is 16 bit.

read_u16() int[source]

Get analog input as integer, from 0 to 65535

PWM

class biorobotics.PWM(pin: str, freq: float = 1000)[source]

Pulse-Width Modulation interface

Main purpose of the class is to automatically select the correct timers and channels based on the target pin.

Constructor

An error is thrown if the pin is not connected to a timer channel.

Parameters
  • pin – Name of the pin (either STM or Arduino name), or a Pin object directly

  • freq – PWM frequency

Raises

ValueError – If the selected pin does not have a registered timer

write(pwm: float)[source]

Write PWM as a number between 0.0 and 1.0

write_percentage(percentage: float)[source]

Write PWM as a decimal percentage between 0.0 and 100.0

Encoder

class biorobotics.Encoder(pin_a: str, pin_b: str)[source]

Class to wrap around hardware timers used for motor encoders

The encoder takes two digital-in pins, A and B, which need to be connected to the same hardware timer. The encoder is always type X4.

The class also takes care of unwrapping the value. The returned value will continue indefinitely, ignoring the integer overflow.

Constructor

Parameters
  • pin_a – Encoder pin A (name or Pin object)

  • pin_b – Encoder pin B (name or Pin object)

Raises
  • ValueError – If any of the selected pins do not have a registered timer

  • ValueError – If the pins do not share the same timer

counter() int[source]

Return pulse count

Pulse count has already been unwrapped, it will start at 0 and increase and decrease indefinitely (including going negative).

Note: the unwrap happens when calling this read function, so it is possible to miss an encoder wrap if the read function is called only very rarely.

set_counter(new_counter: int)[source]

Reset the counter to a specified value

pyb

Timer

class pyb.Timer(timer_id: int, freq: Optional[float] = None, callback: Optional[Callable] = None, *args, **kwargs)[source]

Hardware timer class

Constructor

Calls self.init()

Parameters

timer_id – Timer id (defined by hardware)

callback(func: Optional[Callable])[source]

Set or clear the timer callback

Set func to None to clear the callback.

channel(channel_number, mode=None, pin=None, *args, **kwargs)[source]

Initialize and/or return a TimerChannel

counter(new_counter: Optional[int] = None) int[source]

Get or set timer counter

When in encoder _mode, this corresponds to the number of encoder pulses.

deinit()[source]

De-initialize timer

init(freq: Optional[float] = None, callback: Optional[Callable] = None, *args, **kwargs)[source]

Initialize timer

Parameters
  • freq – Timer frequency in Hz

  • callback – Function to be attached to timer

test_set_value(value)[source]

Set Timer value

Will be returned with counter()

LED

class pyb.LED(led_id: int)[source]

LED Class

Create LED instance

Parameters

led_id – id of the LED

intensity(value: Optional[int] = None) Optional[int][source]

Set _intensity through PWM, from 0 to 255

Does not work on our Nucleo! None of the LEDs are connected to a PWM pin. We don’t bother a universal value here.

off()[source]

Turn LED off

on()[source]

Turn LED on

test_is_on() bool[source]

Get the current state of the LED

Getting the state does not seem possible on regular micropython.

toggle()[source]

Toggle LED state

Switch

class pyb.Switch[source]

Class for a digital switch

Can only be used for the on-board user switch

Create switch for the user button

callback(func: Optional[Callable])[source]

Set an interrupt callback

Set func to None to disable it.

test_push(value: bool = True)[source]

Set button state and return it to the previous state

test_set_value(value: bool)[source]

Set button state

value() bool[source]

Get the current button state

Pin

class pyb.Pin(pin_id: str, mode: int = 0, pull: int = 0, af: int = - 1)[source]

Hardware pin class

Constructor

init(mode: int = 0, pull: int = 0, af: int = - 1)[source]

Initialize Pin

irq(handler: Optional[Callable] = None, trigger: Optional[int] = None, priority: int = 1, wake=None, hard=False)[source]

Create pin interrupt

trigger can be an OR combination of IRQ flags

mode(mode=None) Optional[int][source]

Get or set pin _mode

name() str[source]

Get internal pin name

off()[source]

Set pin value to 0

on()[source]

Set pin value to 1

pull(pull=None) Optional[int][source]

Get or set pin _mode

test_push(value: int = 1)[source]

Set a temporary pin value and return it again

test_set_value(value: int)[source]

Set a pin value

Write a pin value, even when it is in input _mode. Also trigger IRQ callback if one was set.

value(value: Optional[int] = None) Optional[int][source]

Set or get pin value

UART

class pyb.UART(port, baudrate)[source]

Functions

pyb.elapsed_millis(start=0)[source]

Get the current number of elapsed milliseconds since start

Takes care of unwrapping to extend pas integer overflow.

pyb.millis() int[source]

Get the current runtime in milliseconds

pyb.delay(ms: float)[source]

Pause for a number of milliseconds

pyb.micros() int[source]

Get the current runtime in microseconds

pyb.udelay(us: float)[source]

Pause for a number of microseconds

stm

stm package

stm.USART_CR1
stm.USART3

uart3br

uart3br.send(chunks)[source]

Send serial data

Dummy version simply does nothing

utime

Functions

utime.ticks_ms() int[source]
utime.ticks_us() int[source]
utime.test_set_ticks(time_s: float)[source]

Dummy method to set the current time, in seconds

This new value would be returned by e.g. ticks_us() (in the correct units).

Module Info

BioRobotics package.

author

Robert Roos <robert.soo@gmail.com>

license

MIT, see license file or https://opensource.org/licenses/MIT

created on

2021-01-08 16:13:00