Determine the Orientation of an Accelerometer using a JavaScript function in Node-RED

Is there a way to determine the orientation of a sensor using a JavaScript function? I came across an example on 3D Accelerometer calculate the orientation, but I'm struggling to understand how to utilize the values of msg.payload.xAxis/yAxis/zAxis for the calculation. If possible, I would like to incorporate this into the sample payload below by creating a new field.

{
"device":"368640",
"deviceType":"Sigfox-Tracker",
"version":3,
"timestamp":"1535121228",
"rssi":"-113.00",
"battery":3.28,
"temp":21.5,
"soil":2107,
"xAxis":61,
"yAxis":-26,
"zAxis":994
}

Answer №1

The text you provided seems to be a javascript object, and one way to access its properties is through dot notation, such as using data.xAxis.

I've created two functions based on this answer from your link. These functions utilize the built-in Math object for the provided formula.

Here's an example:

let data = {
  "device": "368640",
  "deviceType": "Sigfox-Tracker",
  "version": 3,
  "timestamp": "1535121228",
  "rssi": "-113.00",
  "battery": 3.28,
  "temp": 21.5,
  "soil": 2107,
  "xAxis": 61,
  "yAxis": -26,
  "zAxis": 994
}

function calcRoll(y, z){
  return Math.atan2(y, z) * 180/Math.PI;
}

function calcPitch(x, y, z){
  return Math.atan2(-x, Math.sqrt(y*y + z*z)) * 180/Math.PI;
}

let dataX = data.xAxis;
let dataY = data.yAxis;
let dataZ = data.zAxis;

let roll = calcRoll(dataY, dataZ);
let pitch = calcPitch(dataX, dataY, dataZ);

console.log(`roll is: ${roll} and pitch is ${pitch}`);

Answer №2

Hey Andrew, I'm wondering if this is the correct way to capture msg.payload since each payload contains different values?

let data = msg.payload

function calculateRoll(y, z){
  return Math.atan2(y, z) * 180/Math.PI;
}

function calculatePitch(x, y, z){
  return Math.atan2(-x, Math.sqrt(y*y + z*z)) * 180/Math.PI;
}

let dataX = data.xAxis;
let dataY = data.yAxis;
let dataZ = data.zAxis;

let roll = calculateRoll(dataY, dataZ);
let pitch = calculatePitch(dataX, dataY, dataZ);

msg.payload.roll = roll;
msg.payload.pitch = pitch;
return msg;

This is the debug message I see in Node-RED when the updated payload arrives:

{"device":"368640","deviceType":"Sigfox-Tracker","version":3,"timestamp":"1535136195","rssi":"-109.00","battery":3.28,"temp":19.5,"soil":2105,"xAxis":71,"yAxis":-32,"zAxis":1002,"roll":-1.8291836314227448,"pitch":-4.05104777734416}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way for me to locate the ownerID?

I have a simple task that I need to complete My goal is for the user to be identified using my token when creating a post This is how the create function looks like: exports.create = async (req, res) => { const user = req.token; const users = awai ...

Is it possible to extract the value from the JSON response using this.responseText?

Having trouble extracting data from a json response This is the HTML code I am using. index.html <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head ...

Difficulty transferring function to child element

It seems like I may be overlooking something simple due to exhaustion, but I'm hoping someone can assist me! I have a function that the parent component inherits from the provider, and I am trying to pass it to the child as shown below: console.log(t ...

Create a fresh page using JQuery's bPopup feature

Currently, I am utilizing bPopup to launch a page within a popup container. However, my issue arises when there is a link on the popup page that I want to open in the existing bPopup container. Unfortunately, assigning a new page link in an anchor tag caus ...

Looping through each combination of elements in a Map

I have a Map containing Shape objects with unique IDs assigned as keys. My goal is to loop through every pair of Shapes in the Map, ensuring that each pair is only processed once. While I am aware of options like forEach or for..of for looping, I'm s ...

Implementing JavaScript functionality based on a specific body class

Is there a way to execute this script only on a specific page with a particular body class? For example, if the page has <body class="category-type-plp"> How can I target my script to work specifically for "category-type-plp"? plpSpaceRemove: fun ...

How to handle a Node.js promise that times out if execution is not finished within a specified timeframe

return await new Promise(function (resolve, reject) { //some work goes here resolve(true) }); Using Delayed Timeout return await new Promise(function (resolve, reject) { //some work goes here setTimeout(function() { resolve(true); }, 5000); } ...

When using Material-UI's <Table/> component, an error is thrown stating that the element type is invalid

Struggling with material-ui Table is not familiar territory for me, especially since I have used it in numerous projects before. Currently, I am utilizing @material-ui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33505c41567 ...

I'm currently trying to scrape websites that have JavaScript enabled, but I'm having trouble extracting any content from them

from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import ...

Transitioning to the Bootstrap library from the jQuery library with the use of several image modals

After coming across this specific question about implementing multiple image modals on a webpage, I noticed that it primarily focused on javascript and jQuery. However, my project involves utilizing the latest version of Bootstrap, so I'm curious if t ...

The preselected value in an AngularJS select box is set to static HTML by

In my HTML, I have a select tag that I am populating using ng-repeat. <td> <select ng-model="item.data.region" style="margin-bottom: 2px;"> <option ng-repeat="region in vm.regions track by $index" value="{{region}}">{{region} ...

The custom directive containing ng-switch isn't being reevaluated when the value is updated

I have developed a unique directive that acts as a reusable form. The form includes an error message display section which utilizes ng-switch: <div ng-switch="status"> <span ng-switch-when="Error" class="text-error">An Error occurred</spa ...

Sending information from the parent component to the child Bootstrap Modal in Angular 6

As a newcomer to Angular 6, I am facing challenges with passing data between components. I am trying to launch a child component bootstrap modal from the parent modal and need to pass a string parameter to the child modal component. Additionally, I want t ...

Transform written content into visual data using canvas technology

Trying to develop a basic application that converts form data into an image. For instance, when the form is filled out dynamically and submitted, the goal is to translate the input into an image. Is it possible to achieve this using just javascript, vue. ...

Verify the changing text within a Span tag with the use of Selenium in Java

Can anyone assist me in creating a logic to verify a dynamic text? The text within the tag below is constantly changing (to 6 distinct words), and I need to validate if those 6 unique words match the expected text. Is there a method to do this verification ...

Inconsistency in @nuxtjs/i18n locales after refreshing the page

I am currently working on an application where I am implementing language management, but I have encountered a difficulty. I am using the latest version of @nuxtjs/i18n. Changing the language successfully updates the URL and labels, yet upon refreshing the ...

Top method for showcasing animated images (HTML/CSS/JS)

For my website, I want to create an engaging animation showing a coin being flipped multiple times in the air before landing on a specific side. After the animation finishes, I would like it to transform into a static image of the final result. I've ...

Having trouble modifying a nested object array within a treeview component in Reactjs

Thanks for your help in advance! Question: I'm having trouble updating a nested object of an array in a treeview using Reactjs. Please refer to the code and sandbox link below: https://codesandbox.io/s/cocky-leakey-ptjt50?file=/src/Family.js Data O ...

Delightful Bootstrap Tabs with Dynamic Content via Ajax

My website has a lot of tabs designed with Bootstrap. I wanted to make them responsive, so I tried using a plugin called Bootstrap Tabcollapse from https://github.com/flatlogic/bootstrap-tabcollapse (you can see a demo here: http://tabcollapse.okendoken.co ...

What exactly occurs when a "variable is declared but its value is never read" situation arises?

I encountered the same warning multiple times while implementing this particular pattern. function test() { let value: number = 0 // The warning occurs at this line: value is declared but its value is never read value = 2 return false } My curi ...