Drone.Adapters.Sim.State (ex_drone v0.2.0)

View Source

In-process simulator state for Drone.Adapters.Sim.

Tracks pose, battery, mode, and command history without network I/O. Created via new/1 from the simulator adapter's connect/1 callback.

Summary

Types

t()

Simulator adapter state.

Functions

Increments the flight time by the given number of seconds.

Decreases battery by amount, floored at 0.

Builds a new simulator state from connection options.

Records cmd as the last command and prepends it to history.

Types

t()

@type t() :: %Drone.Adapters.Sim.State{
  battery: number(),
  command_history: [Drone.Command.t()],
  config: map(),
  flight_time_seconds: non_neg_integer(),
  flying: boolean(),
  last_command: Drone.Command.t() | nil,
  mode: :idle | :sdk_mode | :flying | :emergency,
  speed: integer(),
  x: integer(),
  y: integer(),
  yaw: integer(),
  z: integer()
}

Simulator adapter state.

Fields

FieldTypeDefaultMeaning
xinteger()0 or :initial_xWorld X cm
yinteger()0 or :initial_yWorld Y cm (yaw 0 faces +Y)
zinteger()0 or :initial_zAltitude cm
yawinteger()0 or :initial_yawHeading degrees
flyingboolean()falseMotors / airborne
batterynumber()100Percent (may be fractional internally)
speedinteger()0cm/s
mode:idle | :sdk_mode | :flying | :emergency:idleState machine mode
flight_time_secondsnon_neg_integer()0Cumulative motor-on time
last_commandDrone.Command.t() | nilnilMost recent command
command_history[Drone.Command.t()][]Newest-first history
configmap()drain / failure optsSimulator configuration

Example

%Drone.Adapters.Sim.State{
  x: -50,
  y: 0,
  z: 30,
  yaw: 0,
  flying: true,
  battery: 97.5,
  speed: 0,
  mode: :flying,
  flight_time_seconds: 8,
  last_command: %Drone.Command{type: :takeoff, args: [], raw: nil},
  command_history: [],
  config: %{failure_rate: 0.0, fail_commands: []}
}

Functions

add_flight_time(state, seconds)

@spec add_flight_time(t(), non_neg_integer()) :: t()

Increments the flight time by the given number of seconds.

Used to simulate time elapsed during command execution.

Parameters

  • state (t/0)
  • seconds (non_neg_integer())

Returns

Updated t/0.

drain_battery(state, amount)

@spec drain_battery(t(), float()) :: t()

Decreases battery by amount, floored at 0.

Parameters

  • state (t/0)
  • amount (float())

Returns

Updated t/0.

new(opts \\ [])

@spec new(keyword()) :: t()

Builds a new simulator state from connection options.

Parameters

  • opts (keyword()) — supported keys:
    • :initial_x, :initial_y, :initial_z, :initial_yaw (integer())
    • :battery (number())
    • :battery_drain_per_move, :battery_drain_per_takeoff, :battery_drain_per_land, :battery_drain_per_query (float())
    • :failure_rate (float() 0.0..1.0)
    • :fail_commands ([atom()]) — always fail these command types

Returns

t/0

Example

state = Drone.Adapters.Sim.State.new(initial_x: -50, battery: 80)
state.x
#=> -50

push_command(state, cmd)

@spec push_command(t(), Drone.Command.t()) :: t()

Records cmd as the last command and prepends it to history.

Parameters

  • state (t/0)
  • cmd (Drone.Command.t())

Returns

Updated t/0.