JuReceiver/system_sensors/compass.h

107 lines
2.5 KiB
C
Raw Permalink Normal View History

2023-09-10 15:33:34 +02:00
#ifndef _J_SYS_SENS_COMPASS_H
#define _J_SYS_SENS_COMPASS_H
#define jType_S_COMP 0x22
#define jCommand_COMP_fail 0xff
2023-09-21 11:51:54 +02:00
#define jCommand_Compass_Data 0
#define jCommand_Compass_Calibrate 1
2023-09-10 15:33:34 +02:00
typedef struct {
byte type = jType_S_COMP;
byte command = 0;
int X;
int Y;
int Z;
int angle;
} compData;
#ifdef jCOMP
2023-09-21 16:04:40 +02:00
typedef struct {
float x;
float y;
float z;
} compassPrefData;
2023-09-24 21:28:11 +02:00
#define compOff "/compOff"
#define compScale "/compScale"
2023-09-10 15:33:34 +02:00
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
2023-09-21 11:51:54 +02:00
bool compass_is_calibrating = false;
2023-09-10 15:33:34 +02:00
2023-09-21 11:51:54 +02:00
void initCOMP() {
compass.init();
2023-09-21 16:04:40 +02:00
compassPrefData buffer;
2023-09-24 21:28:11 +02:00
if (!fs_read(compOff, &buffer, sizeof(buffer))) {
2023-09-21 16:04:40 +02:00
buffer.x = 0;
buffer.y = 0;
buffer.z = 0;
}
compass.setCalibrationOffsets(buffer.x, buffer.y, buffer.z);
2023-09-24 21:28:11 +02:00
if (!fs_read(compScale, &buffer, sizeof(buffer))) {
2023-09-21 16:04:40 +02:00
buffer.x = 1;
buffer.y = 1;
buffer.z = 1;
}
compass.setCalibrationScales(buffer.x, buffer.y, buffer.z);
2023-09-21 11:51:54 +02:00
}
void COMPLoop() {
if (!compass_is_calibrating) compass.read();
}
2023-09-10 15:33:34 +02:00
/*float Altitude = mpu6050.getAngleX()/180*PI;
float Roll = mpu6050.getAngleY()/180*PI;
float xh=compass.getX()*cos(Altitude)+compass.getZ()*sin(Altitude);
float
yh=compass.getX()*sin(Roll)*sin(Altitude)+compass.getY()*cos(Roll)-compass.getZ()*sin(Roll)*cos(Altitude);
float heading = atan2(yh, xh);*/
void COMPradioTask() {
2023-09-21 11:51:54 +02:00
switch (jRCbuff[1]) {
case jCommand_Compass_Data: {
compData data;
data.X = compass.getX();
data.Y = compass.getY();
data.Z = compass.getZ();
data.angle = compass.getAzimuth();
jSendNRF(&data, sizeof(data));
} break;
case jCommand_Compass_Calibrate: {
compass_is_calibrating = true;
delay(10);
compass.calibrate();
2023-09-21 16:04:40 +02:00
compassPrefData buffer;
buffer.x = compass.getCalibrationOffset(0);
buffer.y = compass.getCalibrationOffset(1);
buffer.z = compass.getCalibrationOffset(2);
2023-09-24 21:28:11 +02:00
fs_write(compOff, &buffer, sizeof(buffer));
2023-09-21 16:04:40 +02:00
buffer.x = compass.getCalibrationScale(0);
buffer.y = compass.getCalibrationScale(1);
buffer.z = compass.getCalibrationScale(2);
2023-09-24 21:28:11 +02:00
fs_write(compScale, &buffer, sizeof(buffer));
2023-09-21 11:51:54 +02:00
compass_is_calibrating = false;
} break;
}
2023-09-10 15:33:34 +02:00
}
#else
void initCOMP() {}
void COMPLoop() {}
void MPUradioTask() {
compData data;
data.command = jCommand_COMP_fail;
jSendNRF(&data, sizeof(data));
}
#endif
#endif