@ircam/sc-motion
v1.4.3
Published
Processing tools for motion
Readme
@ircam/sc-motion
Simple motion processing utilities.
Install
npm install --save @ircam/sc-motion
Format
Our goal is to provide unified axis and units across the different platforms. For this reason, we choose to follow the W3C specifications.
Please see FORMAT.md for OSC and WebSocket messages format.
In particular, we support the R-IoT hardware device and the CoMote phone app.
| | | - | | ⚠️ Please note that the messages format changed between version 2 and version 3. The old format for CoMote v2 is documented here. | | |
API
Table of Contents
format
Format utilities for sensor data.
Examples
import { xyzToArray } from '@ircam/sc-motion/format.js';
const accelerometer = { x: 9.81, y: 0, z: 0 };
const accelerometerArray = xyzToArray(accelerometer);
console.log({ accelerometer, accelerometerArray });
// {
// accelerometer: { x: 9.81, y: 0, z: 0 },
// accelerometerArray: [ 9.81, 0, 0 ]
// }import { apiConvert } from '@ircam/sc-motion/format.js';
const sensorData = {
api: 'v3',
accelerometer: { x: 9.81, y: 0, z: 0 },
gyroscope: { x: 0, y: 0, z: 0 },
};
const sensorDataConverted = apiConvert({
...sensorData,
outputApi: 'riot-v2-array',
});
console.log(sensorDataConverted);
// {
// api: 'riot-v2-array',
// accelerometer: [ 0, 9.81, 0 ],
// gyroscope: [ 0, 0, 0 ],
// }dataXyz
Data object with x, y, and z properties, for API 'v3'.
Type: Object
Properties
dataAlphaBetaGamma
Data object with alpha, beta and gamma properties, for API 'v3'.
Type: Object
Properties
alphanumber The x-coordinate value.betanumber The y-coordinate value.gammanumber The z-coordinate value.
dataArray
Data array with three numerical values, for API 'v3'.
Properties
0number The first coordinate value.1number The second coordinate value.2number The third coordinate value.
dataMotion
Data sensor according to API version.
Type: (dataXyz | dataAlphaBetaGamma | dataArray)
degreeToRadian
Converts an angle from degrees to radians.
Parameters
anglenumber The angle in degrees to be converted.
Returns number The angle in radians.
radianToDegree
Converts an angle from radians to degrees.
Parameters
anglenumber The angle in radians to be converted.
Returns number The angle converted to degrees.
g
The standard acceleration due to gravity (g) in meters per second squared (m/s²).
Type: number
gToNewton
Converts a value from g-force (g) to Newtons (N).
Parameters
forcenumber The value in g-force to be converted.
Returns number The equivalent value in Newtons.
newtonToG
Converts a value from Newtons to G-force.
Parameters
forcenumber The value in Newtons to be converted.
Returns number The equivalent value in G-force.
arrayNormaliseInPlace
Normalises a 3D vector in place and returns its original magnitude.
This function modifies the input array array directly by dividing each of its
components by the vector's magnitude (Euclidean norm). If the magnitude is
zero, the vector remains unchanged.
Parameters
arraydataArray A 3-element array representing the 3D vector to normalise.
Returns number The original magnitude (Euclidean norm) of the vector.
xyzToArray
Converts an object with x, y, and z properties into an array.
Parameters
coordinatesdataXyz The object containing x, y, and z properties.
Returns Array<number> An array containing the x, y, and z values in order.
arrayToXyz
Converts an array of three numerical values into an object with x, y,
and z properties.
Parameters
coordinatesdataArray An array containing three numbers [x, y, z].coordinates.0coordinates.1coordinates.2
Returns dataXyz An object with x, y,
and z properties corresponding to the input array values.
alphaBetaGammaToArray
Converts an object containing alpha, beta, and gamma properties into an array.
Parameters
angledataAlphaBetaGamma The input object.
Returns dataArray An array containing the alpha, beta, and gamma values in order.
arrayToAlphaBetaGamma
Converts an array of three elements into an object with properties alpha, beta, and gamma.
Parameters
$0Array$0.0$0.1$0.2
angledataArray An array containing three numeric elements [alpha, beta, gamma].anglenumber [0] - The alpha value.anglenumber [1] - The beta value.anglenumber [2] - The gamma value.
Returns dataAlphaBetaGamma An object with properties alpha, beta, and gamma.
apiValidate
Validates if the provided API is included in the list of valid APIs.
Parameters
apistring The API name to validate.
Returns boolean Returns true if the API is valid, otherwise false.
apiConvert
- See: apiValid
Copy and converts sensor data between different API formats.
Parameters
optionsObject The format options.
- Throws Error If the format between the specified input and output APIs is unsupported.
Returns Object The converted data. The output is a deep copy of the input data, even if the input and output APIs are the same.
Gravity
Gravity class for estimating gravity using accelerometer and gyroscope.
Parameters
$0Object (optional, default{})$0.outputApi$0.gyroscopeWeightLinear(optional, default0.9)$0.sampleRate(optional, defaultundefined)
optionsObject Configuration options for the Gravity instance. (optional, default{})
Examples
import { Gravity } from '@ircam/sc-motion/gravity.js';
const gravityProcessor = new Gravity({ outputApi: 'v3'});
let motionSensor;
let gravity;
motionSensor = {
api: 'v3',
accelerometer: { x: 9.81, y: 0, z: 0 },
gyroscope: { x: 0, y: 0, z: 0 },
sampleTime: 0,
};
({ gravity } = gravityProcessor.process(motionSensor) );
console.log(gravity);
// {
// api: 'v3',
// gravity: { x: 9.80665, y: 0, z: 0 },
// }
motionSensor = {
api: 'v3',
accelerometer: { x: 4.40, y: 4.40, z: 0 },
gyroscope: { x: -0.001, y: -0.001, z: 0 },
sampleTime: 0.01,
};
({ gravity } = gravityProcessor.process(motionSensor) );
console.log(gravity);
// {
// api: 'v3',
// gravity: { x: 6.934348715723057, y: 6.934348715723057, z: 0 },
// }reset
Resets the internal state of the Gravity instance. Clears the last recorded sample time and resets the accelerometer and gyroscope estimates to null.
set
Sets the attributes for the Gravity instance.
Parameters
attributesObject The attributes to set. Same as constructor.
- Throws Error Same as constructor.
process
Processes accelerometer and gyroscope data to estimate gravity.
accelerometer, gyroscope and gravity conform to the api version.
Parameters
paramsObject The input parameters. (optional, default{})params.accelerometerdataMotion The accelerometer data, conforming to the API version.params.gyroscopedataMotion The gyroscope data, conforming to the API version.params.sampleTimenumber? The timestamp of the current sample in seconds.params.api
- Throws Error Throws an error if API version is missing.
- Throws Error Throws an error if accelerometer data is missing.
- Throws Error Throws an error if gyroscope data is missing.
- Throws Error Throws an error if both sample interval and sample rate are missing (sample rate comes from the constructor or the set method).
Returns dataMotion An object containing the estimated gravity vector. The gravity vector is normalised and conforms to the output API version specified in the constructor.
