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.is_bittle_port — Function
is_bittle_port(port; timeout = DEFAULT_TIMEOUT)Checks if the port responds with Start and Bittle upon connection. Only waits for timeout seconds for the responses (defaults to PetoiBittle.DEFAULT_TIMEOUT). Returns true/false.
See also: find_bittle_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
PetoiBittle.Connection — Type
ConnectionA 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!).
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.
PetoiBittle.disconnect — Function
disconnect(connection::Connection)Disconnects the connection.
See also: PetoiBittle.Connection, PetoiBittle.connect
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.5parse_number(bytes)Short-hand for parse_number(Float64, bytes).
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)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.
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)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)PetoiBittle.DigitsIterator — Type
DigitsIteratorA 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))
6PetoiBittle.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))
6PetoiBittle 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 inUInt8tab:UInt8code of the tab character '\t'newline:UInt8code of the newline character '\n'caret:UInt8code of the caret character '\r'zero:UInt8code of the zero character '0' (a digit)one:UInt8code of the one character '1' (a digit)two:UInt8code of the two character '2' (a digit)three:UInt8code of the three character '3' (a digit)four:UInt8code of the four character '4' (a digit)five:UInt8code of the five character '5' (a digit)six:UInt8code of the six character '6' (a digit)seven:UInt8code of the seven character '7' (a digit)eight:UInt8code of the eight character '8' (a digit)nine:UInt8code of the nine character '9' (a digit)null:UInt8code of the null character '\0' (a terminator)dot:UInt8code of the dot character '.'minus:UInt8code of the minus character '-'space:UInt8code of the space character ' 'tilde:UInt8code of the tilde character '~' (a binary command terminator)
julia> PetoiBittle.Constants.char.tab
0x09PetoiBittle 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 = 9600These 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 = falsePetoiBittle.BUFFER_CAPACITY — Constant
PetoiBittle.BUFFER_CAPACITYSize, 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.
PetoiBittle.BAUD_RATE — Constant
PetoiBittle.BAUD_RATEBaud rate used when opening the serial connection in PetoiBittle.connect. Defaults to 115200. Override with the "baud_rate" preference.
PetoiBittle.MAX_RETRIES — Constant
PetoiBittle.MAX_RETRIESMaximum number of times PetoiBittle.send_command re-reads the response when the output fails PetoiBittle.validate_return_type before giving up with an error. Defaults to 5. Override with the "max_retries" preference.
PetoiBittle.DEFAULT_TIMEOUT — Constant
PetoiBittle.DEFAULT_TIMEOUTDefault timeout, in seconds, used for opening a connection and for serial read/write operations. Defaults to 5. Override with the "default_timeout" preference.
PetoiBittle.PRECOMPILE_WORKLOAD — Constant
PetoiBittle.PRECOMPILE_WORKLOADWhether 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.
PetoiBittle.ConstantsPetoiBittle.BAUD_RATEPetoiBittle.BUFFER_CAPACITYPetoiBittle.DEFAULT_TIMEOUTPetoiBittle.MAX_RETRIESPetoiBittle.NO_TERMINATORPetoiBittle.PRECOMPILE_WORKLOADPetoiBittle.BackFlipPetoiBittle.BackwardPetoiBittle.BackwardLeftPetoiBittle.BackwardRightPetoiBittle.BalancePetoiBittle.BoundPetoiBittle.ButtUpPetoiBittle.CalibratePetoiBittle.CalibrationPosePetoiBittle.CheckAroundPetoiBittle.CommandPetoiBittle.ConnectionPetoiBittle.CrawlForwardPetoiBittle.CrawlLeftPetoiBittle.CrawlRightPetoiBittle.DigitsIteratorPetoiBittle.GreetingPetoiBittle.GyroCalibratePetoiBittle.GyroStatsPetoiBittle.GyroStatsOutputPetoiBittle.MimicDeathPetoiBittle.MoveJointSequencePetoiBittle.MoveJointSpecPetoiBittle.MoveJointsPetoiBittle.NoResponsePetoiBittle.PausePetoiBittle.PeePetoiBittle.PlayMelodyPetoiBittle.PlayMusicPetoiBittle.PushUpPetoiBittle.RawCommandPetoiBittle.RawQueryPetoiBittle.RawResponsePetoiBittle.RecoverPetoiBittle.RestPetoiBittle.SetAllJointsPetoiBittle.SitPetoiBittle.SkillPetoiBittle.SleepPetoiBittle.SteppingPetoiBittle.StretchPetoiBittle.SwitchGyroPetoiBittle.TonePetoiBittle.TrotForwardPetoiBittle.TrotLeftPetoiBittle.TrotRightPetoiBittle.WalkForwardPetoiBittle.WalkLeftPetoiBittle.WalkRightPetoiBittle.ZeroPetoiBittle._serialize_raw_i8!PetoiBittle._serialize_token!PetoiBittle.after_commandPetoiBittle.back_flipPetoiBittle.balancePetoiBittle.before_commandPetoiBittle.bound_forwardPetoiBittle.butt_upPetoiBittle.calibration_posePetoiBittle.check_aroundPetoiBittle.command_return_typePetoiBittle.command_terminatorPetoiBittle.connectPetoiBittle.crawl_forwardPetoiBittle.crawl_leftPetoiBittle.crawl_rightPetoiBittle.deserialize_from_bytesPetoiBittle.disconnectPetoiBittle.find_bittle_portPetoiBittle.greetPetoiBittle.is_bittle_portPetoiBittle.iterate_digitsPetoiBittle.napPetoiBittle.parse_numberPetoiBittle.peePetoiBittle.play_deadPetoiBittle.push_upPetoiBittle.send_commandPetoiBittle.serialize_to_bytes!PetoiBittle.sitPetoiBittle.skills_overviewPetoiBittle.step_in_placePetoiBittle.straightenPetoiBittle.stretchPetoiBittle.trot_forwardPetoiBittle.trot_leftPetoiBittle.trot_rightPetoiBittle.validate_return_typePetoiBittle.walk_backwardPetoiBittle.walk_backward_leftPetoiBittle.walk_backward_rightPetoiBittle.walk_forwardPetoiBittle.walk_leftPetoiBittle.walk_right