wrc

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 9 Imported by: 0

README

Logo

A small and easy to use library for WRC 23 telemetry data.

version

PRs welcome activity

Table of Contents

Getting Started

Installation

# Run this to install
go get github.com/stelmanjones/wrc

Usage


var (
  	conn, err = net.ListenPacket("udp4", ":6969")
  Client = wrc.New()
)

func main() {

    go Client.Run()

    for {
      p,err := Client.Latest()
      if err != nil {
       // Error 
      }
      log.Info("Packet: ", "RPM",p.VehicleEngineRpmCurrent)

      spd,err := Client.AverageSpeedKmph()
      log.Infof("Average speed: %f",spd)

    }
}


Issues

See the open issues for a list of proposed features (and known issues).

Contributing

First off, thanks for taking the time to contribute! Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated.

Please try to create bug reports that are:

  • Reproducible. Include steps to reproduce the problem.
  • Specific. Include as much detail as possible: which version, what environment, etc.
  • Unique. Do not duplicate existing opened issues.
  • Scoped to a Single Bug. One bug per report.

License

This project is licensed under the MIT license. Feel free to edit and distribute this repo as you like.

Documentation

Index

Constants

View Source
const (
	MpsToMph  float32 = 2.237
	MpsToKmph float32 = 3.6
)

Conversion values

Variables

View Source
var (
	// ErrNoData is an error that represents a nil data scenario.
	ErrNoData = errors.New("data is equal to nil")

	// ErrStoreEmpty is an error that represents an empty datastore scenario.
	ErrStoreEmpty = errors.New("the datastore is empty")

	// ErrStoreUpdate gets returned if there was a problem updating the
	//internal datastore.
	ErrStoreUpdate = errors.New("there was an error updating the internal store")

	// ErrUDPData represents an udp error.
	ErrUDPData = errors.New("there was an error getting udp data")
)

Functions

func Listen

func Listen(conn net.PacketConn, ch chan Packet) error

Listen is a function that listens for incoming packets on a given connection. It reads the packets into a buffer, decodes them into a Packet struct, and sends them on a channel.

go Listen(conn,ch)
ch: The channel to send the decoded packets on.

Types

type Client

type Client struct {
	*DataStore

	Debug bool
	// contains filtered or unexported fields
}

Client is used to manage incoming UDP data.

func New

func New() *Client

New returns a new client.

func NewDebug

func NewDebug() *Client

NewDebug returns a new client with some extra debug info.

func (*Client) AverageFrameTime

func (c *Client) AverageFrameTime() (float32, error)

AverageFrameTime returns your average frame time based on all 'GameDeltaTime' values in the store.

func (*Client) AverageSpeedKmph

func (c *Client) AverageSpeedKmph() (float32, error)

AverageSpeedKmph returns your average speed based on all 'VehicleSpeed' values in the store.

func (*Client) AverageSpeedMph

func (c *Client) AverageSpeedMph() (float32, error)

AverageSpeedMph returns your average speed based on all 'VehicleSpeed' values in the store.

func (*Client) Run

func (c *Client) Run(conn net.PacketConn) error

Run starts the UDP client and begins to listen for incoming packets, decoding them, and pushing them to the datastore.

type DataStore

type DataStore struct {
	// contains filtered or unexported fields
}

DataStore represents a data store for Wrc packets with thread-safe operations.

func NewWrcDataStore

func NewWrcDataStore(d []*Packet) *DataStore

NewWrcDataStore initializes a new WrcDataStore with the provided packets.

func (*DataStore) Clear

func (w *DataStore) Clear() error

Clear the internal store.

func (*DataStore) Latest

func (w *DataStore) Latest() (*Packet, error)

Latest returns the latest packet from the store.

func (*DataStore) Push

func (w *DataStore) Push(p *Packet) error

Push new data to the store.

func (*DataStore) SetSize

func (w *DataStore) SetSize(s int)

SetSize changes the maixmum number of elements in the store.

func (*DataStore) Size

func (w *DataStore) Size() int

Size returns the current number of elements in the store.

type Packet

type Packet struct {
	// A rolling unique identifier for the current packet. Can be used to order and drop received packets.
	PacketUID uint64 `json:"packet_uid"`

	// Time spent in game since boot.
	GameTotalTime float32 `json:"game_total_time"`

	// Time spent since last frame.
	GameDeltaTime float32 `json:"game_delta_time"`

	// Frame count in game since boot.
	GameFrameCount uint64 `json:"game_frame_count"`

	// For shift lights, from 0 ('vehicle_engine_rpm_current'='shiftlights_rpm_start') to 1 ('vehicle_engine_rpm_current'='shiftlights_rpm_end').
	ShiftlightsFraction float32 `json:"shiftlights_fraction"`

	// Shift lights start at 'vehicle_engine_rpm_current' value.
	ShiftlightsRpmStart float32 `json:"shiftlights_rpm_start"`

	// Shift lights end (i.e. optimal shift) at 'vehicle_engine_rpm_current' value.
	ShiftlightsRpmEnd float32 `json:"shiftlights_rpm_end"`

	// Are shift lights RPM data valid: 'vehicle_engine_rpm_current', 'shiftlights_rpm_start', 'shiftlights_rpm_end'
	ShiftlightsRpmValid bool `json:"shiftlights_rpm_valid"`

	// Gear index or value of 'vehicle_gear_index_neutral' or 'vehicle_gear_index_reverse'
	VehicleGearIndex uint8 `json:"vehicle_gear_index"`

	// 'vehicle_gear_index' if gearbox in Neutral.
	VehicleGearIndexNeutral uint8 `json:"vehicle_gear_index_neutral"`

	// 'vehicle_gear_index' if gearbox in Reverse.
	VehicleGearIndexReverse uint8 `json:"vehicle_gear_index_reverse"`

	// Number of forward gears.
	VehicleGearMaximum uint8 `json:"vehicle_gear_maximum"`

	// Car body speed.
	VehicleSpeed float32 `json:"vehicle_speed"`

	// Car speed at wheel/road due to transmission (for speedo use). NB. May differ from 'vehicle_speed'.
	VehicleTransmissionSpeed float32 `json:"vehicle_transmission_speed"`

	// Car position X component, positive left.
	VehiclePositionX float32 `json:"vehicle_position_x"`

	// Car position Y component, positive up.
	VehiclePositionY float32 `json:"vehicle_position_y"`

	// Car position Z component, positive forward.
	VehiclePositionZ float32 `json:"vehicle_position_z"`

	// Car velocity X component, positive left.
	VehicleVelocityX float32 `json:"vehicle_velocity_x"`

	// Car velocity Y component, positive up.
	VehicleVelocityY float32 `json:"vehicle_velocity_y"`

	// Car velocity Z component, positive forward.
	VehicleVelocityZ float32 `json:"vehicle_velocity_z"`

	// Car acceleration X component, positive left.
	VehicleAccelerationX float32 `json:"vehicle_acceleration_x"`

	// Car acceleration Y component, positive up.
	VehicleAccelerationY float32 `json:"vehicle_acceleration_y"`

	// Car acceleration Z component, positive forward.
	VehicleAccelerationZ float32 `json:"vehicle_acceleration_z"`

	// Car left unit vector X component, positive left.
	VehicleLeftDirectionX float32 `json:"vehicle_left_direction_x"`

	// Car left unit vector Y component, positive up.
	VehicleLeftDirectionY float32 `json:"vehicle_left_direction_y"`

	// Car left unit vector Z component, positive forward.
	VehicleLeftDirectionZ float32 `json:"vehicle_left_direction_z"`

	// Car forward unit vector X component, positive left.
	VehicleForwardDirectionX float32 `json:"vehicle_forward_direction_x"`

	// Car forward unit vector Y component, positive up.
	VehicleForwardDirectionY float32 `json:"vehicle_forward_direction_y"`

	// Car forward unit vector Z component, positive forward.
	VehicleForwardDirectionZ float32 `json:"vehicle_forward_direction_z"`

	// Car up unit vector X component, positive left.
	VehicleUpDirectionX float32 `json:"vehicle_up_direction_x"`

	// Car up unit vector Y component, positive up.
	VehicleUpDirectionY float32 `json:"vehicle_up_direction_y"`

	// Car up unit vector Z component, positive forward.
	VehicleUpDirectionZ float32 `json:"vehicle_up_direction_z"`

	// Wheel hub height displacement, back left, positive up.
	VehicleHubPositionBL float32 `json:"vehicle_hub_position_bl"`

	// Wheel hub height displacement, back right, positive up.
	VehicleHubPositionBR float32 `json:"vehicle_hub_position_br"`

	// Wheel hub height displacement, front left, positive up.
	VehicleHubPositionFL float32 `json:"vehicle_hub_position_fl"`

	// Wheel hub height displacement, front right, positive up.
	VehicleHubPositionFR float32 `json:"vehicle_hub_position_fr"`

	// Wheel hub vertical velocity, back left, positive up.
	VehicleHubVelocityBL float32 `json:"vehicle_hub_velocity_bl"`

	// Wheel hub vertical velocity, back right, positive up.
	VehicleHubVelocityBR float32 `json:"vehicle_hub_velocity_br"`

	// Wheel hub vertical velocity, front left, positive up.
	VehicleHubVelocityFL float32 `json:"vehicle_hub_velocity_fl"`

	// Wheel hub vertical velocity, front right, positive up.
	VehicleHubVelocityFR float32 `json:"vehicle_hub_velocity_fr"`

	// Contact patch forward speed, back left.
	VehicleCpForwardSpeedBL float32 `json:"vehicle_cp_forward_speed_bl"`

	// Contact patch forward speed, back right.
	VehicleCpForwardSpeedBR float32 `json:"vehicle_cp_forward_speed_br"`

	// Contact patch forward speed, front left.
	VehicleCpForwardSpeedFL float32 `json:"vehicle_cp_forward_speed_fl"`

	// Contact patch forward speed, front right.
	VehicleCpForwardSpeedFR float32 `json:"vehicle_cp_forward_speed_fr"`

	// Brake temperature, back left.
	VehicleBrakeTemperatureBL float32 `json:"vehicle_brake_temperature_bl"`

	// Brake temperature, back right.
	VehicleBrakeTemperatureBR float32 `json:"vehicle_brake_temperature_br"`

	// Brake temperature, front left.
	VehicleBrakeTemperatureFL float32 `json:"vehicle_brake_temperature_fl"`

	// Brake temperature, front right.
	VehicleBrakeTemperatureFR float32 `json:"vehicle_brake_temperature_fr"`

	// Engine rotation rate, maximum.
	VehicleEngineRpmMax float32 `json:"vehicle_engine_rpm_max"`

	// Engine rotation rate, at idle.
	VehicleEngineRpmIdle float32 `json:"vehicle_engine_rpm_idle"`

	// Engine rotation rate, current.
	VehicleEngineRpmCurrent float32 `json:"vehicle_engine_rpm_current"`

	// Throttle pedal after assists and overrides, 0 (off) to 1 (full).
	VehicleThrottle float32 `json:"vehicle_throttle"`

	// Brake pedal after assists and overrides, 0 (off) to 1 (full).
	VehicleBrake float32 `json:"vehicle_brake"`

	// Clutch pedal after assists and overrides, 0 (off) to 1 (full).
	VehicleClutch float32 `json:"vehicle_clutch"`

	// Steering after assists and overrides, -1 (full left) to 1 (full right).
	VehicleSteering float32 `json:"vehicle_steering"`

	// Handbrake after assists and overrides, 0 (off) to 1 (full).
	VehicleHandbrake float32 `json:"vehicle_handbrake"`

	// Time spent on current stage.
	StageCurrentTime float32 `json:"stage_current_time"`

	// Distance reached on current stage.
	StageCurrentDistance float64 `json:"stage_current_distance"`

	// Total length of current stage.
	StageLength float64 `json:"stage_length"`
}

Packet represents the default WRC data packet.

func NewPacket

func NewPacket() *Packet

NewPacket creates a new Packet instance with all fields initialized to their zero values.

func (*Packet) Brake

func (p *Packet) Brake() Percentage

Brake returns the current brake value as a percentage.

func (*Packet) CarPosition

func (p *Packet) CarPosition() *Position

CarPosition returns the current position of the car.

func (*Packet) Clutch

func (p *Packet) Clutch() Percentage

Clutch returns the current clutch value as a percentage. (100 = engaged)

func (*Packet) ElapsedStageTime

func (p *Packet) ElapsedStageTime() (t string)

ElapsedStageTime returns current stagetime as a formatted string. "03:42.583"

func (*Packet) Handbrake

func (p *Packet) Handbrake() Percentage

Handbrake returns the current handbrake value as a percentage.

func (*Packet) InGameTime

func (p *Packet) InGameTime() (t string)

InGameTime returns time spent ingame as a formatted string. "03:42.583"

func (*Packet) Kmph

func (p *Packet) Kmph() float32

Kmph returns the vehicle speed in kilometers per hour.

func (*Packet) Mph

func (p *Packet) Mph() float32

Mph returns the vehicle speed in miles per hour.

func (*Packet) StageProgress

func (p *Packet) StageProgress() Percentage

StageProgress returns the current stage's progress as a percentage.

func (*Packet) Throttle

func (p *Packet) Throttle() Percentage

Throttle returns the current throttle value as a percentage.

func (*Packet) ToJSON

func (p *Packet) ToJSON() ([]byte, error)

ToJSON returns the packet as marshaled JSON.

type Percentage

type Percentage int

Percentage is an alias for int

type Position

type Position struct {
	X float32
	Y float32
	Z float32
}

Position of the car in 3D

type Timespan

type Timespan time.Duration

Timespan is a helper type used to format laptimes.

	Timespan(time.Duration(10 * time.Second)).Format("04:05.000")
  	"00:10.000"

func (Timespan) Format

func (t Timespan) Format(format string) string

Format formats the Timespan as a prettified laptime.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL