Zeke Rich
asked on
Building a compass with MPU-9255 Magnometer
I have this module https://www.addicore.com/mpu-9250-p/ad280.htm
I have it sending me raw data for MX, MY, MZ (X,Y,Z,) from this data i am trying to create a compass.
this is example data i am getting
x = 3072
y=-6001
With these values and the script below it combines these variables to calculate a heading to location at= 209 degrees
The problem is when I rotate the compass the numbers are not linear... they jump around ate certain points.... 300, 600, 40, 180
I would like the compass to send me the correct degrees when i rotate it. Thank you for your help!
I have it sending me raw data for MX, MY, MZ (X,Y,Z,) from this data i am trying to create a compass.
this is example data i am getting
x = 3072
y=-6001
With these values and the script below it combines these variables to calculate a heading to location at= 209 degrees
The problem is when I rotate the compass the numbers are not linear... they jump around ate certain points.... 300, 600, 40, 180
I would like the compass to send me the correct degrees when i rotate it. Thank you for your help!
///JAVASCRIPT
var mx = msg.payload.mx;
var my = msg.payload.my;
var mz = msg.payload.mz;
//var heading = Math.atan2(my, mx) * 180/Math.PI;
//var heading = Math.atan2(my,mx)/Math.PI*180 + 180;
//var heading = Math.atan2(my,mx)/Math.PI*180+180;
//theta_rad = Math.atan2(my,mx);
//heading = (theta_rad/Math.PI*180) + (theta_rad > 0 ? 0 : 360);
angle = Math.atan2(mx,my)*180/Math.PI;
var heading = angle + Math.ceil( -angle / 360 ) * 360;
var lol2 = Math.ceil(heading);
msg.payload = lol2;
return msg;
From a quick scan of the spec, it looks like each axis should be linear (+32760 -> -32760) but it’s possible that the bytes are swapped .
ASKER
how are you getting the data points?I have a code written in C running on a Arduino that generates the raw data that is sent serial to a javascript node:
/*
Raw data example
This example reads raw readings from the magnetometer gyroscope and the accelerometer and then
displays them in serial monitor.
*/
#include <MPU9255.h>//include MPU9255 library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "TP-Link_5A4E";
const char* password = "31801621";
const char* mqtt_server = "192.168.0.101";
WiFiClient espClient;
PubSubClient client(espClient);
MPU9255 mpu;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
#include "mqtt.h";
void setup() {
Serial.begin(115200);//initialize Serial port
if(mpu.init())
{
Serial.println("initialization failed");
}
else
{
Serial.println("initialization succesful!");
}
//pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output.... CAN NOT USE CREATS CONFLICT!!!
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
mpu.read_acc();//get data from the accelerometer
mpu.read_gyro();//get data from the gyroscope
mpu.read_mag();//get data from the magnetometer
//print all data in serial monitor
/*
Serial.print("AX: ");
Serial.print(mpu.ax);
Serial.print(" AY: ");
Serial.print(mpu.ay);
Serial.print(" AZ: ");
Serial.print(mpu.az);
Serial.print(" GX: ");
Serial.print(mpu.gx);
Serial.print(" GY: ");
Serial.print(mpu.gy);
Serial.print(" GZ: ");
Serial.print(mpu.gz);
Serial.print(" MX: ");
Serial.print(mpu.mx);
Serial.print(" MY: ");
Serial.print(mpu.my);
Serial.print(" MZ: ");
Serial.println(mpu.mz);
*/
delay(100);
char buffer [100];
snprintf ( buffer, 100, "ax=%d,ay=%d,az=%d,mx=%d,my=%d,mz=%d", mpu.ax, mpu.ay, mpu.az, mpu.mx, mpu.my, mpu.mz );
//Serial.print("Publish message: ");
Serial.println(buffer);
client.publish("lbicep", buffer); //outTopic, set the topic HERE
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is the datasheet https://download.mikroe.com/documents/datasheets/ak8963c-datasheet.pdf