Created
August 29, 2023 13:53
-
-
Save ypelletier/034210780b466eebf2557bca4f7f9563 to your computer and use it in GitHub Desktop.
Module magnétomètre - Boussole HMC5883L relié à un Raspberry Pi Pico L'angle par rapport au nord est affiché dans la console.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Module magnétomètre - Boussole HMC5883L relié à un Raspberry Pi Pico | |
L'angle par rapport au nord est affiché dans la console. | |
Plus d'infos: | |
https://electroniqueamateur.blogspot.com/2023/08/magnetometreboussole-hmc5883l-et.html | |
''' | |
from hmc5883l import HMC5883L # https://github.com/SindormirNet/micropython-rp2040-hmc5883l/tree/master | |
import time | |
import math | |
boussole = HMC5883L() | |
# déclinaison magnétique en degrés là où vous êtes | |
# https://www.magnetic-declination.com/ | |
declinaison = -12.8 | |
# routine de calibration: | |
print ("Calibration") | |
print ("Tournez la boussole sur un tour complet dans un plan horizontal") | |
debut = time.time() | |
x, y, z = boussole.read() | |
xmin = x | |
xmax = x | |
ymin = y | |
ymax = y | |
while (time.time() - debut) < 8: # pendant 8 secondes | |
x, y, z = boussole.read() | |
if x < xmin: | |
xmin = x | |
if x > xmax: | |
xmax = x | |
if y < ymin: | |
ymin = y | |
if y > ymax: | |
ymax = y | |
# bilan de la calibration: | |
print('xmin: ' , xmin, ' xmax: ', xmax) | |
print('ymin: ' , ymin, ' ymax: ', ymax) | |
# petite pause pour laisser le temps de lire le résultat | |
time.sleep_ms(2000) | |
while True: | |
x, y, z = boussole.read() | |
# transformation des données brutes | |
xcal = (x-(xmax+xmin)/2)*(200)/(xmax-xmin) | |
ycal = (y-(ymax+ymin)/2)*(200)/(ymax-ymin) | |
# calcul de l'angle entre x et le nord | |
if xcal != 0: | |
angle = round(math.atan(ycal/xcal)*180/math.pi + declinaison,0) | |
print(angle, '°') | |
time.sleep_ms(200) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment