Commands overview

PetoiBittle.jl is built around a small, fast command layer. Every action you can ask the robot to perform is a PetoiBittle.Command; you send it over an open PetoiBittle.Connection with PetoiBittle.send_command.

On top of that low-level layer, the built-in skills (gaits, postures, behaviors) also expose convenience verbs so you can write expressive, high-level code:

using PetoiBittle

port = PetoiBittle.find_bittle_port()
connection = PetoiBittle.connect(port)

PetoiBittle.sit(connection)            # high-level verb
PetoiBittle.walk_forward(connection)   # ... and another

# the verb above is exactly equivalent to the explicit, low-level form:
PetoiBittle.send_command(connection, PetoiBittle.WalkForward())

PetoiBittle.disconnect(connection)

Both styles compile down to the same zero-allocation write path, so the convenience verbs cost nothing extra at runtime.

The full Petoi protocol is documented on the Petoi website in the Serial Protocol section. This library implements a curated subset; commands that are not modelled yet can still be sent with the low-level PetoiBittle.RawCommand / PetoiBittle.RawQuery. PRs adding new typed commands are welcome!

The command interface

PetoiBittle.command_terminator — Function
command_terminator(::Type{<:Command})

Returns the single byte appended after the serialized command bytes when writing to the robot. The Petoi firmware expects one of three terminators depending on the command:

Note that this concerns only the outgoing command. Responses coming back from the robot are always newline-terminated regardless of the command terminator, so the response reader in PetoiBittle.send_command always keys on a newline.

source
PetoiBittle.before_command — Function
before_command(connection::Connection, command::Command)

A callback that is being called right before sending the command over the connection. A more specific Command can add custom logic that needs to be executed right before sending the command. By default does nothing.

See also: after_command

source
PetoiBittle.after_command — Function
after_command(connection::Connection, command::Command)

A callback that is being called right after sending the command over the connection but before reading the output. A more specific Command can add custom logic that needs to be executed right after sending the command. By default does nothing.

See also: before_command

source
PetoiBittle.command_return_type — Function
command_return_type(::Type{<:Command})

Different commands may have different response (return) types while some commands don't imply any response.

If a command has a specific response type, it should implement

PetoiBittle.command_return_type(::Type{<:MySpecificCommand}) = MySpecificCommandResponse

as well as

function PetoiBittle.deserialize_from_bytes(bytes, ::Type{MySpecificCommandResponse}, startidx, endidx)
    # ...
    return MySpecificCommandResponse(...)
end

to deserialize the response from raw bytes. The bytes will contain everything that has been captured after the command has been sent until the symbol.

In this case, a command should also implement

function PetoiBittle.validate_return_type(bytes, ::Type{MySpecificCommandResponse}, startidx, endidx)
    return true # false
end

to validate the content in bytes. The PetoiBittle.validate_return_type will be called before PetoiBittle.deserialize_from_bytes. If the validate function returns false, the PetoiBittle.send_command discards the content in bytes and read the output again.

Default return type is assumed to be PetoiBittle.NoResponse, which is a convention that the command does not have any response. In this case the return value of the PetoiBittle.send_command is nothing and the deserialization procedure is not being called. That also means everything the Petoi Bittle sends after the command has been executed is being ignored.

See also: PetoiBittle.deserialize_from_bytes

source

Built-in skills at a glance

The table below lists every built-in skill (gait, posture, behavior): its command type, its convenience verb, and the raw firmware token it sends. It is generated directly from PetoiBittle.skills_overview, so it always matches the code.

CategoryCommand typeConvenience verbTokenDescription
GaitsSteppingstep_in_placekvtmarch in place without moving forward
GaitsBackwardwalk_backwardkbkwalk backward
GaitsBackwardLeftwalk_backward_leftkbkLwalk backward and to the left
GaitsBackwardRightwalk_backward_rightkbkRwalk backward and to the right
GaitsCrawlForwardcrawl_forwardkcrFcrawl forward
GaitsCrawlLeftcrawl_leftkcrLcrawl forward and to the left
GaitsCrawlRightcrawl_rightkcrRcrawl forward and to the right
GaitsWalkForwardwalk_forwardkwkFwalk forward
GaitsWalkLeftwalk_leftkwkLwalk forward and to the left
GaitsWalkRightwalk_rightkwkRwalk forward and to the right
GaitsTrotForwardtrot_forwardktrFtrot forward
GaitsTrotLefttrot_leftktrLtrot forward and to the left
GaitsTrotRighttrot_rightktrRtrot forward and to the right
GaitsBoundbound_forwardkbdFbound forward
PosturesBalancebalancekbalancestand still and self-balance
PosturesButtUpbutt_upkbuttUpraise the hindquarters (butt up)
PosturesCalibrationPosecalibration_posekcalibstand in the calibration pose
PosturesSitsitksitsit down
PosturesSleepnapksleepfold down into the sleep posture
PosturesStretchstretchkstrstretch the body
PosturesZerostraightenkzerostraighten all joints to the neutral (zero) pose
BehaviorsCheckAroundcheck_aroundkcklook around to check the surroundings
BehaviorsGreetinggreetkhigreet with a friendly gesture
BehaviorsPeepeekpeelift a leg to pee
BehaviorsPushUppush_upkpudo push-ups
BehaviorsMimicDeathplay_deadkpdplay dead (mimic death)
BehaviorsBackFlipback_flipkbfperform a backflip

Commands that take arguments (joint control, sound) and the low-level primitives are described on their own pages: Joint control, Control & state, Sound, and Low-level & advanced.

PetoiBittle.skills_overview — Function
skills_overview()

Return the metadata table of all built-in named skills (gaits, postures, behaviors) as a tuple of named tuples with fields category, julia_name, verb, token, description.

Each row corresponds to a generated PetoiBittle.Command subtype (julia_name) and a convenience verb (verb). This table powers the documentation overview as well as the package's own tests.

julia> row = first(filter(r -> r.julia_name === :Sit, PetoiBittle.skills_overview()));

julia> (row.verb, row.token)
(:sit, "ksit")
source