Qt 6.11 is out! See what's new in the release
blog
Qt IOs Magnetic field
-
Hello! Sorry for my bad English.
Trying to get magnetic field data using the QMagnetometer class.
Everything turns out if you set the returnGeoValues parameter to false (as by default). In this case, raw data is issued, but I need processed data. If I set the returnGeoValues parameter to true, then the values of the magnetic field x, y, z are set to 0. At the same time, reading-> calibrationLevel () also gives 0, but it displays 1 for the first two or three conclusions.I am enclosing the code of my class for working with magnetic fields.
Tell me, please, what am I doing wrong?
Developed for IOs, version Qt 5.15.
Thank!#ifndef MAGNETO_H #define MAGNETO_H #include <QObject> #include <QMagnetometer> #include <QMagnetometerFilter> #include <QMagnetometerReading> #include "math.h" #include <QDebug> class Magneto : public QObject, public QMagnetometerFilter { Q_OBJECT public: explicit Magneto(QObject *parent = nullptr); ~Magneto(); bool filter(QMagnetometerReading *reading); void start(); void stop(); bool isMagnetoStarted(); private slots: void onSensorError(const int &errorCode); signals: void dataReceived(const qreal &x, const qreal &y, const qreal &z); private: QMagnetometer *m_magnetometer; qreal m_x; qreal m_y; qreal m_z; bool m_isStarted; }; #endif // MAGNETO_H#include "magneto.h" Magneto::Magneto(QObject *parent) : QObject(parent) { m_magnetometer = new QMagnetometer(this); m_magnetometer->addFilter(this); m_magnetometer->setReturnGeoValues(true); connect(m_magnetometer, SIGNAL(sensorError(int)), this, SLOT(onSensorError(int))); m_isStarted = false; } Magneto::~Magneto() { m_magnetometer->deleteLater(); } bool Magneto::filter(QMagnetometerReading *reading) { qDebug() << reading->calibrationLevel() << reading->x() << reading->y() << reading->z(); emit dataReceived(reading->x() * pow(10, 6), reading->y() * pow(10, 6), reading->z() * pow(10, 6)); } void Magneto::onSensorError(const int &errorCode) { qDebug() << errorCode; } void Magneto::start() { m_isStarted = true; m_magnetometer->start(); } void Magneto::stop() { m_isStarted = false; m_magnetometer->stop(); } bool Magneto::isMagnetoStarted() { return m_isStarted; }