Skip to content

protocols

File: lua/vehicle/protocols.lua · VM: Vehicle · 282 lines · BeamNG.drive 0.40.5 (buildbot 302427 on GitLab)

Access: global protocols

Manager of the vehicle's UDP telemetry protocols (OutGauge for external dashboards, MotionSim for motion platforms). At init, it loads each module of lua/vehicle/protocols/*.lua enabled in the settings, opens one UDP socket per protocol, fills a C packet (ffi) at the requested rate (real time, not simulation time) and sends it. It also computes the shared smoothed quantities (speed, acceleration, angles and angular speeds) that the modules read in protocols.xxx. Only acts for the first seated player, and never in multi-seat.

Example

-- A custom protocol module: lua/vehicle/protocols/myProto.lua must expose
-- init, reset (optional), getAddress, getPort, getMaxUpdateRate, isPhysicsStepUsed, getStructDefinition, fillStruct(o, dtSim)
-- It is enabled by the setting protocols_others_enabled.

Notes

  • Settings read: protocols_outgauge_enabled, protocols_motionSim_enabled, protocols_others_enabled (all the other files in the folder), multiseat (disables everything), and protocols_smoothing_ for the exponential smoothing constants.
  • Fields read by the protocol modules in the protocols table (M): posX, posY, posZ (m, world), velXSmoothed/velYSmoothed/velZSmoothed (m/s), accXSmoothed/accYSmoothed/accZSmoothed (m/s², sign inverted from the sensors, gravity not included according to the motionSim comment), upX/upY/upZ (vehicle up vector), rollPosSmoothed/pitchPosSmoothed/yawPosSmoothed (angles; roll and yaw negative "by simulator convention"), rollVelSmoothed/pitchVelSmoothed/yawVelSmoothed (rad/s), rollAccSmoothed/pitchAccSmoothed/yawAccSmoothed (rad/s², derivative of the smoothed speeds). These fields are created dynamically at the first update of the smoothers.
  • Sending happens on the wall clock: updatePeriod = 1 / getMaxUpdateRate(); a protocol that uses the physics step (isPhysicsStepUsed true) is updated in update(), the others in updateGFX().
  • The packet is zeroed (ffi.fill) before each fillStruct; it is sent over UDP to (getAddress(), getPort()) even if fillStruct exited early (see outgauge.fillStruct: fully zero packet sent when the vehicle is not initialized).
  • The C structure is declared only once per protocol name (typedef 'protocol__t'). A change to getStructDefinition of an already loaded module requires a restart (ctrl-R is not enough because the ffi definition persists).
  • The files in the protocols folder are listed only once (filesCache): a new file is only seen after reloading Lua (ctrl-R).
  • M.update and M.updateGFX are nop at load and replaced in init(); ⚠ nop is a game global.

Functions

Function Description Line
protocols.destroy() Closes the UDP sockets of all active protocols and empties the list. (internal) 193
protocols.init() Destroys the existing protocols, then (if enabled in the settings and not in multi-seat) loads, initializes and activates each protocol module found, installs update/updateGFX and initializes the smoothers. (internal) 206
protocols.isPhysicsStepUsed() Indicates whether at least one active protocol requests the physics step; used to register update() in the physics loop. (internal) 257
protocols.onPlayersChanged(anyPlayerSeated) Hook called when the seated players change: if a first player is seated, resets the smoothers. (internal) (hook) 265
protocols.reset() Resets the smoothers and calls reset() of each protocol module that defines one. (internal) 125
protocols.settingsChanged() Hook called when the user settings change: reruns init() to apply the new protocol settings. (internal) 261

Parameter details

protocols.isPhysicsStepUsed()

Indicates whether at least one active protocol requests the physics step; used to register update() in the physics loop.

  • returns: boolean true if a protocol uses the physics step.

protocols.onPlayersChanged(anyPlayerSeated)

Hook called when the seated players change: if a first player is seated, resets the smoothers.

  • anyPlayerSeated (boolean) : True if at least one player is seated (not used directly).

Fields

Field Type Description
update function nop, or updatePhysics(dtSim) if at least one protocol uses the physics step. Called at each physics step.
updateGFX function nop, or the graphics update if at least one protocol does not use the physics step.

Calls into the engine (C++)

  • FS: findFiles
  • obj: getDirectionVectorUp, getPositionXYZ, getRollPitchYaw, getRollPitchYawAngularVelocity, getVelocityXYZ

tim19Atlas (Tim0219800) — derived from BeamNG GmbH Lua code (bCDDL 1.1, see LICENSE). Source: lua/vehicle/protocols.lua.