Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Using a arduino compass module with a PicoMite
Author | Message | ||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
OK, as part of a rover project I need to know compass direction. I'm using the arduino compass module from Jaycar. This uses I2C to communicate, and although the datasheet says 5v, mine is happy running off 3.3v. I was first using this module on a ESP32, using the... #include <Adafruit_Sensor.h> #include <Adafruit_HMC5883_U.h> ... libraries, but want to migrate my rover code to a PicoMite, so needed to write my own code to read the compass. Below is some code I wanted to share. First up we need to get some calibration offsets from the compass. Every compass module will give different results, so some calibration is needed. This basically looks for the minimum and maximum readings for each axis as you rotate the thing around in every possible plane your can contort you arm into doing. 'Initiage compass I2C WRITE &H1E, 0, 2, &H02, &H Dim MagData(6); XMax=0 : XMin=0 YMax=0 : YMin=0 ZMax=0 : ZMin=0 Do I2C WRITE &H1E, 0,1,&H03 I2C READ &H1E, 0, 6, MagData() X=(MagData(0)*256)+MagData(1) Y=(MagData(2)*256)+MagData(3) Z=(MagData(4)*256)+MagData(5) If X>32768 Then X=X-65536 If Y>32768 Then Y=Y-65536 If Z>32768 Then Z=Z-65536 If X<XMin Then XMin=X If X>XMax Then XMax=X If Y<YMin Then YMin=Y If Y>YMax Then YMax=Y If Z<ZMin Then ZMin=Z If Z>ZMax Then ZMax=Z XOffset=0-((XMax+XMin)/2) YOffset=0-((YMax+YMin)/2) ZOffset=0-((ZMax+ZMin)/2) Print "X Axis: ";XMin;" to ";XMax;" offset is ";XOffset Print "Y Axis: ";YMin;" to ";YMax;" offset is ";YOffset Print "Z Axis: ";ZMin;" to ";ZMax;" offset is ";ZOffset Pause 100 Loop Once you have the offset data, you can add those figures to the code below. This will display the compass reading in degrees positive and negative from magnetic north, or true north if you edit the DeclinationAngle value to suit your area. In the line CompassInRad = Atan2(Z+190, X+127) + DeclinationAngle, I'm using the X and Z axis to get my results. You need to pick which axis to use based on the orientation of the compass module. 'Initiage compass I2C WRITE &H1E, 0, 2, &H02, &H DeclinationAngle=0.17 Dim MagData(6); Do I2C WRITE &H1E, 0,1,&H03 I2C READ &H1E, 0, 6, MagData() X=(MagData(0)*256)+MagData(1) Y=(MagData(2)*256)+MagData(3) Z=(MagData(4)*256)+MagData(5) If X>32768 Then X=X-65536 If Y>32768 Then Y=Y-65536 If Z>32768 Then Z=Z-65536 ' Added the +190 And +127 to calibrate the sensor for my location CompassInRad = Atan2(Z+190, X+127) + DeclinationAngle CompassInDeg=Int(CompassInRad * 180/Pi) Print CompassInDeg Pause 100 Loop Sure is neater than the libraries and code I needed for the ESP32. Just a side note, I went into my Jaycar last week to grab another compass module, and they are out of stock. Hopefully only temporarily. My thinks was averaging the results from 2 compass modules would give more accuracy. Edited 2024-04-30 16:49 by Gizmo The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
Glenn Have you thought about using a 9-axis sensor and the MATH SENSORFUSION routines in the PicoMite. Together they should give a much more stable bearing than a compass alone https://www.thebackshed.com/forum/ViewTopic.php?TID=13459&PID=166962#166962 Edited 2024-04-30 17:07 by matherp |
||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
Thanks, no haven't seen the 9-axis sensors, but I'll keep it in mind, see how this goes first. I'm fairly confident my new rover algorithm will follow a nice line between waypoints, see how it goes. Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Print this page |