Zoltan Hudak / HMC1501
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC1501.cpp Source File

HMC1501.cpp

00001 /*
00002  * Copyright (c) 2020 Zoltan Hudak <hudakz@outlook.com>
00003  * All rights reserved.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "HMC1501.h"
00020 #include "math.h"
00021 
00022 #define M_PI        3.14159265358979323846
00023 
00024 /**
00025  * @brief   Creates a HMC1501 object
00026  * @note    Minimum and maximum voltages shall be measured by rotating the magnet
00027  * @param   minV    Minimum voltage [mV]
00028  * @param   maxV    Maximum voltage [mV]
00029  */
00030 HMC1501::HMC1501(float minV, float maxV)
00031 {
00032     float peakToPeak = maxV - minV;
00033     float halfPeakToPeak = peakToPeak / 2;
00034     _amplitude = halfPeakToPeak;
00035     _offset = maxV - halfPeakToPeak;
00036 }
00037 
00038 /**
00039  * @brief   Angle in degree [°]
00040  * @note    theta = arcsin(Volt) / 2 [rad]
00041  * @param   Input voltage in mV [mV]
00042  * @retval  Angle in degree [°]
00043  */
00044 float HMC1501::angle(float mV)
00045 {
00046     float sinus = (mV - _offset) / _amplitude;
00047 
00048     return (asin(sinus) * 180) / M_PI / 2;
00049 }