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 — Type
CommandAn abstract type that encapsulates all available commands. A command may customize its behaviour by implementing the following interface related functions:
PetoiBittle.command_return_type- specifies the return type of the command
Use
julia> subtypes(PetoiBittle.Command)to get the list of all available commands.
See also: PetoiBittle.send_command, before_command, after_command
PetoiBittle.send_command — Function
send_command(connection::Connection, command::Command)A function to send a command to a Bittle robot through opened PetoiBittle.Connection.
Use
julia> subtypes(PetoiBittle.Command)to get the list of all available commands.
See also: PetoiBittle.Command, before_command, after_command
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:
Constants.char.newline('\n', the default) for ASCII text commands such as skills andMoveJoints.Constants.char.tilde('~') for raw-binary commands that carry packed byte arguments (for example a transform-to-frame / set-all-joints command).PetoiBittle.NO_TERMINATORfor the few commands that take no terminator at all.
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.
PetoiBittle.NO_TERMINATOR — Constant
A sentinel value returned by PetoiBittle.command_terminator to indicate that a command should be sent without any trailing terminator byte. 0xff is never a valid terminator in the Petoi serial protocol, so it is safe as a sentinel.
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
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
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}) = MySpecificCommandResponseas well as
function PetoiBittle.deserialize_from_bytes(bytes, ::Type{MySpecificCommandResponse}, startidx, endidx)
# ...
return MySpecificCommandResponse(...)
endto 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
endto 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
PetoiBittle.NoResponse — Type
A simple command response, indicating that the command has no response.
PetoiBittle.validate_return_type — Function
validate_return_type(bytes, ::Type, startidx, endidx)Validates that the content in bytes between startidx and endidx can be deserialized into the specified Type. See PetoiBittle.command_return_type for more details.
PetoiBittle.deserialize_from_bytes — Function
deserialize_from_bytes(bytes, ::Type, startidx, endidx)Deserializes the content in bytes between startidx and endidx into Type. See PetoiBittle.command_return_type for more details.
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.
| Category | Command type | Convenience verb | Token | Description |
|---|---|---|---|---|
| Gaits | Stepping | step_in_place | kvt | march in place without moving forward |
| Gaits | Backward | walk_backward | kbk | walk backward |
| Gaits | BackwardLeft | walk_backward_left | kbkL | walk backward and to the left |
| Gaits | BackwardRight | walk_backward_right | kbkR | walk backward and to the right |
| Gaits | CrawlForward | crawl_forward | kcrF | crawl forward |
| Gaits | CrawlLeft | crawl_left | kcrL | crawl forward and to the left |
| Gaits | CrawlRight | crawl_right | kcrR | crawl forward and to the right |
| Gaits | WalkForward | walk_forward | kwkF | walk forward |
| Gaits | WalkLeft | walk_left | kwkL | walk forward and to the left |
| Gaits | WalkRight | walk_right | kwkR | walk forward and to the right |
| Gaits | TrotForward | trot_forward | ktrF | trot forward |
| Gaits | TrotLeft | trot_left | ktrL | trot forward and to the left |
| Gaits | TrotRight | trot_right | ktrR | trot forward and to the right |
| Gaits | Bound | bound_forward | kbdF | bound forward |
| Postures | Balance | balance | kbalance | stand still and self-balance |
| Postures | ButtUp | butt_up | kbuttUp | raise the hindquarters (butt up) |
| Postures | CalibrationPose | calibration_pose | kcalib | stand in the calibration pose |
| Postures | Sit | sit | ksit | sit down |
| Postures | Sleep | nap | ksleep | fold down into the sleep posture |
| Postures | Stretch | stretch | kstr | stretch the body |
| Postures | Zero | straighten | kzero | straighten all joints to the neutral (zero) pose |
| Behaviors | CheckAround | check_around | kck | look around to check the surroundings |
| Behaviors | Greeting | greet | khi | greet with a friendly gesture |
| Behaviors | Pee | pee | kpee | lift a leg to pee |
| Behaviors | PushUp | push_up | kpu | do push-ups |
| Behaviors | MimicDeath | play_dead | kpd | play dead (mimic death) |
| Behaviors | BackFlip | back_flip | kbf | perform 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")PetoiBittle.NO_TERMINATORPetoiBittle.CommandPetoiBittle.NoResponsePetoiBittle.after_commandPetoiBittle.before_commandPetoiBittle.command_return_typePetoiBittle.command_terminatorPetoiBittle.deserialize_from_bytesPetoiBittle.send_commandPetoiBittle.skills_overviewPetoiBittle.validate_return_typePetoiBittle.BackwardPetoiBittle.BackwardLeftPetoiBittle.BackwardRightPetoiBittle.BoundPetoiBittle.CrawlForwardPetoiBittle.CrawlLeftPetoiBittle.CrawlRightPetoiBittle.SteppingPetoiBittle.TrotForwardPetoiBittle.TrotLeftPetoiBittle.TrotRightPetoiBittle.WalkForwardPetoiBittle.WalkLeftPetoiBittle.WalkRightPetoiBittle.bound_forwardPetoiBittle.crawl_forwardPetoiBittle.crawl_leftPetoiBittle.crawl_rightPetoiBittle.step_in_placePetoiBittle.trot_forwardPetoiBittle.trot_leftPetoiBittle.trot_rightPetoiBittle.walk_backwardPetoiBittle.walk_backward_leftPetoiBittle.walk_backward_rightPetoiBittle.walk_forwardPetoiBittle.walk_leftPetoiBittle.walk_rightPetoiBittle.BalancePetoiBittle.ButtUpPetoiBittle.CalibrationPosePetoiBittle.SitPetoiBittle.SleepPetoiBittle.StretchPetoiBittle.ZeroPetoiBittle.balancePetoiBittle.butt_upPetoiBittle.calibration_posePetoiBittle.napPetoiBittle.sitPetoiBittle.straightenPetoiBittle.stretchPetoiBittle.BackFlipPetoiBittle.CheckAroundPetoiBittle.GreetingPetoiBittle.MimicDeathPetoiBittle.PeePetoiBittle.PushUpPetoiBittle.back_flipPetoiBittle.check_aroundPetoiBittle.greetPetoiBittle.peePetoiBittle.play_deadPetoiBittle.push_upPetoiBittle.MoveJointSequencePetoiBittle.MoveJointSpecPetoiBittle.MoveJointsPetoiBittle.SetAllJointsPetoiBittle.CalibratePetoiBittle.GyroCalibratePetoiBittle.GyroStatsPetoiBittle.GyroStatsOutputPetoiBittle.PausePetoiBittle.RecoverPetoiBittle.RestPetoiBittle.SwitchGyroPetoiBittle.PlayMelodyPetoiBittle.PlayMusicPetoiBittle.TonePetoiBittle.RawCommandPetoiBittle.RawQueryPetoiBittle.RawResponsePetoiBittle.SkillPetoiBittle._serialize_raw_i8!PetoiBittle._serialize_token!