Drone.Adapters.Crazyflie.Logging (ex_drone v0.3.0)

View Source

Pure CRTP logging (port 5) packet builders and parsers.

Used to subscribe to battery and flight-readiness variables on a real Crazyflie via TOC download + log block create/start. Packet layouts follow the documented Bitcraze logging protocol (TOC V2 / control V2).

Typical sequence

reset = Drone.Adapters.Crazyflie.Logging.reset()
info = Drone.Adapters.Crazyflie.Logging.get_toc_info()
item = Drone.Adapters.Crazyflie.Logging.get_toc_item(0)
create = Drone.Adapters.Crazyflie.Logging.create_block_v2(0, [{1, 10}, {1, 20}])
start = Drone.Adapters.Crazyflie.Logging.start_block_v2(0, 100)

Preferred variables:

  • pm.batteryLevel (uint8 %) — fallback pm.vbat (float volts → %)
  • sys.canfly (uint8) — non-zero means estimator / platform ready to fly

Summary

Types

Log block layout entry used when decoding data packets.

One TOC entry resolved from GET_ITEM_V2.

Functions

Maps battery voltage in volts to an approximate charge percent.

Log control channel (1).

True when a control result indicates success (result == 0).

Builds CREATE_BLOCK_V2.

Log data channel (2).

Default log block id used by the Crazyflie adapter (0).

Default log period in milliseconds (100).

Builds GET_INFO_V2 (TOC size / CRC / limits).

Builds GET_ITEM_V2 for TOC entry id.

Converts a layout into CREATE_BLOCK_V2 ops.

Parses a control-channel reply (<<cmd, block_id, result>>).

Parses a log-data channel payload using a known layout.

Parses GET_INFO_V2 reply payload (without CRTP header).

Parses GET_ITEM_V2 reply payload into a TOC item.

Builds RESET — deletes all log blocks.

Finds battery and can-fly TOC entries and builds a decode layout.

Builds START_BLOCK_V2 with period in milliseconds.

Log TOC channel (0).

Byte size of a log type id.

Types

layout_entry()

@type layout_entry() :: %{
  key: :battery | :estimator_ready | atom(),
  type: non_neg_integer(),
  id: non_neg_integer(),
  source: :battery_level | :vbat | :canfly
}

Log block layout entry used when decoding data packets.

toc_item()

@type toc_item() :: %{
  id: non_neg_integer(),
  type: non_neg_integer(),
  group: String.t(),
  name: String.t()
}

One TOC entry resolved from GET_ITEM_V2.

Functions

battery_percent_from_vbat(volts)

@spec battery_percent_from_vbat(number()) :: 0..100

Maps battery voltage in volts to an approximate charge percent.

Crazyflie LiPo range used: 3.0 V (0%) … 4.2 V (100%).

control_channel()

@spec control_channel() :: 1

Log control channel (1).

control_ok?(arg1)

@spec control_ok?(map()) :: boolean()

True when a control result indicates success (result == 0).

create_block_v2(block_id, ops)

@spec create_block_v2(byte(), [{byte(), non_neg_integer()}]) ::
  Drone.Adapters.Crazyflie.CRTP.packet()

Builds CREATE_BLOCK_V2.

ops is a list of {log_type, variable_id} tuples.

data_channel()

@spec data_channel() :: 2

Log data channel (2).

default_block_id()

@spec default_block_id() :: 0

Default log block id used by the Crazyflie adapter (0).

default_period_ms()

@spec default_period_ms() :: 100

Default log period in milliseconds (100).

get_toc_info()

@spec get_toc_info() :: Drone.Adapters.Crazyflie.CRTP.packet()

Builds GET_INFO_V2 (TOC size / CRC / limits).

get_toc_item(id)

Builds GET_ITEM_V2 for TOC entry id.

layout_ops(layout)

@spec layout_ops([layout_entry()]) :: [{byte(), non_neg_integer()}]

Converts a layout into CREATE_BLOCK_V2 ops.

parse_control_result(arg1)

@spec parse_control_result(binary()) ::
  {:ok, %{command: byte(), block_id: byte(), result: byte()}} | {:error, term()}

Parses a control-channel reply (<<cmd, block_id, result>>).

parse_data(arg1, layout)

@spec parse_data(binary(), [layout_entry()]) :: {:ok, map()} | {:error, term()}

Parses a log-data channel payload using a known layout.

parse_toc_info(arg1)

@spec parse_toc_info(binary()) :: {:ok, map()} | {:error, :invalid_toc_info}

Parses GET_INFO_V2 reply payload (without CRTP header).

parse_toc_item(arg1)

@spec parse_toc_item(binary()) :: {:ok, toc_item()} | {:error, term()}

Parses GET_ITEM_V2 reply payload into a TOC item.

reset()

Builds RESET — deletes all log blocks.

resolve_layout(items)

@spec resolve_layout([toc_item()]) :: {:ok, [layout_entry()]} | {:error, term()}

Finds battery and can-fly TOC entries and builds a decode layout.

Prefers pm.batteryLevel, falls back to pm.vbat. Requires sys.canfly.

start_block_v2(block_id, period_ms)

@spec start_block_v2(byte(), pos_integer()) :: Drone.Adapters.Crazyflie.CRTP.packet()

Builds START_BLOCK_V2 with period in milliseconds.

toc_channel()

@spec toc_channel() :: 0

Log TOC channel (0).

type_size(arg1)

@spec type_size(non_neg_integer()) :: pos_integer() | {:error, :unknown_log_type}

Byte size of a log type id.