# Equations

### From Torque OBD2 Wiki

The PID Editor contains a reasonably powerful equation editor that allows you to manipulate the incoming data from an OBD device, Sensor, or other data input to the app

## Functions that manipulate data

The equation editor supports the following functions

#### EWMAF(weight:value)

- Exponentially weighted moving average filter - filter noisy signals here. 'weight' should be between 0(more smoothing) and 1(less smoothing)

#### TAVG(seconds:value)

- Timed average. This averages the incoming data for the configured amount of time (in seconds). Data older than the configured time is removed from being averaged

#### RAVG(value)

- Rolling average. This averages incoming values until it is reset. No values are removed from the average until it is reset.

#### AVG(bucketSize:value)

- Point average. This averages incoming values from a bucket of s set size. The bucket can be configured to average a set amount of values, so AVG(10,value) would average the last 10 values

#### TDLY(seconds:value)

- Time based delay - causes data to be delayed by 'seconds' amount before being returned. Returns 0 until data buffer is full

#### RDLY(polls:value)

- Read based delay - data is delayed by X polling loops before being returned

#### TOT(seconds:value)

- Totalizer function based upon difference from time/value

#### SIN(value), COS(value) ,TAN(value)

- Trigonometric functions are supported

#### LOG(value)

- Returns the natural logarithm (base e) of a value

#### LOG10(value)

- Returns the base 10 logarithm of a value

#### LOG1P(value)

- Returns the natural logarithm of the sum of the value and 1

#### INT(value)

- Converts the incoming number to an integer

#### SQRT(value)

- Returns the correctly rounded positive square root of a value.

#### ABS(value)

- Returns the absolute value of the number given

#### SIGNED(value)

- Treats the incoming value as 8bit signed

#### INT16(A:B)

- Returns a 16 bit int from values A and B. Can be used in place of (A*255)+B

#### INT24(A:B:C)

- Returns a 24bit int from the input values

#### INT32(A:B:C:D)

- Returns a 32bit int from the input values

#### FLOAT32(A:B:C:D)

- Returns an IEEE754 float based on the supplied 4 inputs

#### FLOAT64(A:B:C:D:E:F:G:H)

- Returns an IEEE754 float based on the supplied 8 (value:0-255) inputs

#### MIN(A:B)

- Returns the smallest number of A or B

#### MAX(A:B)

- Returns the largest number of A or B

#### BIT(value:bit)

- Returns the bit specified at 'bit' from the 'value'. This function replaces the use of the {value:bit} notation

#### LOOKUP({VALUE}:{DEFAULT}:{KEY1}{OPERATOR}{VALUE1}:{KEY2}{OPERATOR}{VALUE2}:…etc)

Look up a value from a list of key/value pairs. Can be used to substitute values, reply with a text string, etc

Examples:

String, exact matching, returning a string:

`LOOKUP(A::1=’moo’:2=’boo’)`

Numeric, exact, with a default of 0 for no match:

`LOOKUP(A:0:1=100:2=200:3=300)`

Numeric, exact, with a default of the input value for no match:

`LOOKUP(A:A:1=100:2=200:3=300)`

Numeric, range matching – 0 to 3 = 4 and 4 to 5 = 7:(the 'tilde' character separates the range, not the minus character)

`LOOKUP(A::0~3=4:4~5=7)`

String, exact matching, returning a string, value is also returned into the PID. The value will be ‘5’ when A is 2, and 4 when A is 1 (the display will show boo and moo however):

`LOOKUP(A::1=’moo’:2=’boo’)+3`

Mixed matching with strings and numbers (Strings are shown on the display with numbers being passed through to the equation) exact, with a default of the input value for no match:

`LOOKUP(A:A:1=100:2=200:3=’this is a test’)`

#### CLOSEST({VALUE}:{DEFAULT}:{KEY1}={VALUE1}....)

Similar to the LOOKUP function, this returns the value which is numerically closest to the key

Examples:

Return 'low' when less than 128 and 'high' when near 255:

`CLOSEST(A:A:1='low':255='high')`

## Functions that generate or retrieve data

#### RANDOM()

- Returns a random number between 0 and 1

#### BARO()

- Returns the barometric pressure from the android device or measured from the vehicle ECU if it supports it in psi