PetoiBittle.jl logo

PetoiBittle

Documentation for PetoiBittle.

PetoiBittle.jl drives Petoi Bittle / OpenCat robots over a serial port. It pairs a fast, zero-allocation low-level command layer with high-level convenience verbs so beginners can get moving in a few lines while advanced users keep full control.

Quick start

using PetoiBittle

# find and connect to the robot
connection = PetoiBittle.connect(PetoiBittle.find_bittle_port())

# high-level verbs
PetoiBittle.sit(connection)
PetoiBittle.walk_forward(connection)
PetoiBittle.balance(connection)

# the equivalent low-level form
PetoiBittle.send_command(connection, PetoiBittle.WalkForward())

PetoiBittle.disconnect(connection)

See the Commands overview for the full list of commands, the convenience verbs, and per-category guides (gaits, postures, behaviors, joint control, control & state, sound, and low-level primitives).

PetoiBittle connection

PetoiBittle.jl implements convenience functions to find and/or check if a port is connected to Petoi Bittle Dog robot as well as establish a connection with a given port.

PetoiBittle.find_bittle_port — Function
find_bittle_port(; individual_port_timeout = DEFAULT_TIMEOUT, verbose = true)

Scan all ports and find the one that returns true when calling is_bittle_port. Passes individual_port_timeout as timeout to is_bittle_port (defaults to PetoiBittle.DEFAULT_TIMEOUT). If verbose=true (default) prints info messages to track scanning progress. If no port Bittle is found, throws an error.

See also: is_bittle_port

source
PetoiBittle.Connection — Type
Connection

A connection to Petoi Bittle robot. Use PetoiBittle.connect to open a connection.

The connection is parametric in the serial-port type S. In normal usage S is a LibSerialPort.SerialPort, but the parameter allows substituting an alternative transport (for example a fake serial port in tests) as long as it implements the internal transport methods (_transport_drain!, _transport_flush!, _transport_write! and _transport_read_byte!).

source
PetoiBittle.connect — Function
connect(port::String; [timeout = DEFAULT_TIMEOUT])

Open a PetoiBittle.Connection at a specified port. Try for no longer than timeout (defaults to PetoiBittle.DEFAULT_TIMEOUT). The port must be manually PetoiBittle.disconnect-ed when unneeded. The timeout is also being used to set read and write timeouts. The connection is opened at the PetoiBittle.BAUD_RATE baud rate with a PetoiBittle.BUFFER_CAPACITY-byte buffer.

See PetoiBittle.is_bittle_port and PetoiBittle.find_bittle_port.

source

Users can also use PetoiBittle.LibSerialPort.get_port_list to get a list of all ports compatible with LibSerialPort.

PetoiBittle parsers

PetoiBittle.jl implements simple parsers to parse responses from serial ports of PetoiBittle directly from bytes

PetoiBittle.parse_number — Function
parse_number(::Type{T}, bytes)

Parse decimal representation of a number of type T from bytes. Stops at non-digits characters, with the exception of - and ..

julia> PetoiBittle.parse_number(Int, [ 0x34, 0x32 ])
42

julia> PetoiBittle.parse_number(Float64, [ 0x2d, 0x30, 0x2e, 0x35 ])
-0.5
source
parse_number(bytes)

Short-hand for parse_number(Float64, bytes).

source
parse_number(::Type{T}, bytes, firstindex, lastindex)

Parse decimal representation of a number of type T from bytes between firstindex and lastindex. The firstindex and lastindex should be valid indices of bytes. Stops at non-digits characters, with the exception of - and ..

Returns (result, nextindex), where result is the actual parsed number and nextindex is either:

  • an index next to a non-digit character or;
  • lastindex + 1
julia> PetoiBittle.parse_number(Float64, [ 0x34, 0x32 ], 1, 1)
(4.0, 2)

julia> PetoiBittle.parse_number(Float64, [ 0x34, 0x32 ], 1, 2)
(42.0, 3)
source

PetoiBittle serializers

PetoiBittle.jl implements simple serializers to write responses to serial ports of PetoiBittle directly as bytes

PetoiBittle.serialize_to_bytes! — Function
serialize_to_bytes!(bytes, number::Int, startidx)

Write the number digit by digit to bytes starting at startidx. Negative number start with the - character (0x2d, see PetoiBittle.Constants) Returns the modified bytes and the next to last modified index.

source
serialize_to_bytes!(bytes, spec::MoveJointSpec, startidx)

Write the spec to bytes starting at startidx. Returns the modified bytes and the next to last modified index.

julia> PetoiBittle.serialize_to_bytes!(zeros(UInt8, 4), PetoiBittle.MoveJointSpec(7, 0), 1)
(UInt8[0x37, 0x20, 0x30, 0x00], 4)
source
serialize_to_bytes!(bytes, task::MoveJoints, startidx)

Write the task to bytes starting at startidx. The MoveJoints task always starts with the I character. Returns the modified bytes and the next to last modified index.

julia> PetoiBittle.serialize_to_bytes!(zeros(UInt8, 16), PetoiBittle.MoveJoints((id = 1, angle = 10), (id = 3, angle = -10)), 1)
(UInt8[0x69, 0x31, 0x20, 0x31, 0x30, 0x20, 0x33, 0x20, 0x2d, 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00], 12)
source
PetoiBittle.DigitsIterator — Type
DigitsIterator

A special iterator that iterates over digits of a number Int without allocating an intermediate array. Use the PetoiBittle.iterate_digits function to create the iterator.

julia> PetoiBittle.iterate_digits(123) |> collect
3-element Vector{Int64}:
 1
 2
 3

julia> reduce(+, PetoiBittle.iterate_digits(123))
6
source
PetoiBittle.iterate_digits — Function
iterate_digits(number::Int)

Creates PetoiBittle.DigitsIterator from number. Use this to iterate through digits of the number without allocating intermediate array.

julia> PetoiBittle.iterate_digits(123) |> collect
3-element Vector{Int64}:
 1
 2
 3

julia> reduce(+, PetoiBittle.iterate_digits(123))
6
source

PetoiBittle Constants

PetoiBittle.Constants — Module

A sub-module of PetoiBittle containing some useful constants Constants are united in their respective namespaces.

The available namespaces are:

  • char: a list of character codes in UInt8
    • tab: UInt8 code of the tab character '\t'
    • newline: UInt8 code of the newline character '\n'
    • caret: UInt8 code of the caret character '\r'
    • zero: UInt8 code of the zero character '0' (a digit)
    • one: UInt8 code of the one character '1' (a digit)
    • two: UInt8 code of the two character '2' (a digit)
    • three: UInt8 code of the three character '3' (a digit)
    • four: UInt8 code of the four character '4' (a digit)
    • five: UInt8 code of the five character '5' (a digit)
    • six: UInt8 code of the six character '6' (a digit)
    • seven: UInt8 code of the seven character '7' (a digit)
    • eight: UInt8 code of the eight character '8' (a digit)
    • nine: UInt8 code of the nine character '9' (a digit)
    • null: UInt8 code of the null character '\0' (a terminator)
    • dot: UInt8 code of the dot character '.'
    • minus: UInt8 code of the minus character '-'
    • space: UInt8 code of the space character ' '
    • tilde: UInt8 code of the tilde character '~' (a binary command terminator)
julia> PetoiBittle.Constants.char.tab
0x09
source

PetoiBittle Configuration

A few package constants are configurable through Preferences.jl. The defaults suit a standard Petoi Bittle, but you can override them without editing the source. For example, to change the baud rate:

using PetoiBittle, Preferences
set_preferences!(PetoiBittle, "baud_rate" => 9600)

This records the value in a LocalPreferences.toml file next to your active project:

[PetoiBittle]
baud_rate = 9600
Note

These preferences are read when the package is loaded, which makes them compile-time preferences. Changing one invalidates the precompilation cache, so you need to restart Julia for the new value to take effect.

PetoiBittle also ships a PrecompileTools.jl workload that compiles and caches the command serialization, parsing, and dispatch machinery at precompilation time, so the first send_command is fast (this matters on low-power targets such as a Raspberry Pi). It is enabled by default and can be turned off with the same mechanism, for example in a headless CI environment:

using PetoiBittle, Preferences
set_preferences!(PetoiBittle, "precompile_workload" => false)
[PetoiBittle]
precompile_workload = false
PetoiBittle.BUFFER_CAPACITY — Constant
PetoiBittle.BUFFER_CAPACITY

Size, in bytes, of the pre-allocated I/O buffer held by a PetoiBittle.Connection. Commands are serialized into this buffer and responses are read back into it, so it bounds the largest single response line that can be received. Defaults to 256. Override with the "buffer_capacity" preference.

source
PetoiBittle.DEFAULT_TIMEOUT — Constant
PetoiBittle.DEFAULT_TIMEOUT

Default timeout, in seconds, used for opening a connection and for serial read/write operations. Defaults to 5. Override with the "default_timeout" preference.

source
PetoiBittle.PRECOMPILE_WORKLOAD — Constant
PetoiBittle.PRECOMPILE_WORKLOAD

Whether to run the PrecompileTools.jl workload at precompilation time. The workload drives PetoiBittle.send_command through a fake in-memory serial port for a representative set of commands, so the serialization, parsing, and command-dispatch machinery is compiled once and cached in the package image instead of on first use. This makes the first real send_command fast, which matters on low-power targets such as a Raspberry Pi.

Defaults to true. Set it to false to skip the workload (useful in headless CI or when iterating on the package, where the extra precompilation cost is not worth it):

using PetoiBittle, Preferences
set_preferences!(PetoiBittle, "precompile_workload" => false)

Override with the "precompile_workload" preference.

source