HM55b Digital Compass - Small problem, + 'C' Code for Everyone
Eric Shufro
Posts: 9
Dear All,
I am having some trouble with the HM55B digital compass. I am able to determine the degree heading, but am experiencing dead zones in the reading. I do not use the calibration routines, but do not think that is the reason for not obtaining a full circle of 64 different readings between 0 and 360 degrees.
Attached is some bit-bang source code that·I have written in 'C' for a Luminary Micro Cortex M3 processor. I have similar code for the Freescale MC9S12C128. It is fairly easy to port and i am working on a generic set of shift routines as well.
The files that I would like everyone to look at are attached. They are named hm55b.c and hm55b.h.
I have also attached shift.c and shift.h which are currently in the works and untested. However, they are well documented and may help others port Parallax sensors to non Parallax micro-controllers.
My compass files have been derived from the shift files.
All of these source files are free for others to use. Hopefully, the problem that I am experiencing can be fixed easily. When this is done, I will also provide some code for the H48C Tri-Axis accelerometer. I have that one working on a different chip currently.
Any additional recommendations (from experience)·may also prove helpful.
Thank you in advance,
--Eric
I am having some trouble with the HM55B digital compass. I am able to determine the degree heading, but am experiencing dead zones in the reading. I do not use the calibration routines, but do not think that is the reason for not obtaining a full circle of 64 different readings between 0 and 360 degrees.
Attached is some bit-bang source code that·I have written in 'C' for a Luminary Micro Cortex M3 processor. I have similar code for the Freescale MC9S12C128. It is fairly easy to port and i am working on a generic set of shift routines as well.
The files that I would like everyone to look at are attached. They are named hm55b.c and hm55b.h.
I have also attached shift.c and shift.h which are currently in the works and untested. However, they are well documented and may help others port Parallax sensors to non Parallax micro-controllers.
My compass files have been derived from the shift files.
All of these source files are free for others to use. Hopefully, the problem that I am experiencing can be fixed easily. When this is done, I will also provide some code for the H48C Tri-Axis accelerometer. I have that one working on a different chip currently.
Any additional recommendations (from experience)·may also prove helpful.
Thank you in advance,
--Eric
Comments
What is the function of the baudrate? The HM55B uses SPI so it can work
with slow speeds as well.
Attached are my javelin classes for the HM55B. You should have no
trouble understanding these, as javelin java is almost C.
The important function is poll(), that retrieves data from the HM55B
once a measurement is completed.
You can open these files with any text editor.
regards peter
The baud rate is just a way to set the frequency of the software SPI port. The part can actually be clocked much faster, easily 4MHz i believe, so its ideal to have a faster baud rate to reduce the amount of time that is spent waiting in a loop before togglling the clock and other control lines. However, for testing, i have it set to 100KHz.
Thanks for the code, i will have a look at it now.
--Eric
In your poll() function, i noticed that you do not disable the compass if the result is NOT ready:
··· if (result) {
····· m_x = CPU.shiftIn(m_DinDout, m_Clk, 11, CPU.POST_CLOCK_MSB );
····· m_y = CPU.shiftIn(m_DinDout, m_Clk, 11, CPU.POST_CLOCK_MSB );
····· CPU.writePin(m_En, true);
····· if ((m_x & 0x0400) != 0) m_x |= (short)0xF800;
····· if ((m_y & 0x0400) != 0) m_y |= (short)0xF800;
··· }
··· else {·········································· ··· /* Missing code? */
····· CPU.writePin(m_En, false);
··· }
Also, you only reset the device once after initialization. What happens if an error occurs, how do your recover during run-time if you are out of synch with the device? (I reset everytime before i issue a measure command, is this bad?)
Otherwise, my code looks nearly identical to yours. Do you know more about how the CPU.shiftin() and CPU.shiftout() methods work?
I want to make sure that i am putting data on the lines during the correct clock edge and that the timing makes sense.
thanks,
--Eric
Woops, in the /* missing code? */ section that i posted previously, i meant to set the pin to true (disable the HM55B). Sorry for the typo.
--Eric
OK, i see why its not disabled there, but, if the result never becomes ready due to a communication error, the compass is never reset and remains enabled until the next measurement start. Is it possible to get out of synch?
--eric
http://www.robotstore.com/download/Compassdocumentation.pdf
You can perform a reset before any new measurement if you like,
or if it takes too long for a measurement to complete.
The above document states 40 msec per measurement.
regards peter
I defintely keep EN low until the mesaurement is complete.
By chance, is it possible to obtain a printout of your X and Y, and degree values as you rotate the compass around?
I have attached a spread sheet with my output values. (i also use the PC to compute the radians and degrees for comparison).
Also, do you use any calibration routines? My range for X and Y do not seem to be even. Im not sure if this is normal or not.
Thanks,
--Eric
The class was originally from this thread:
http://forums.parallax.com/showthread.php?p=552817
with minor adjustements regarding the shiftin and shiftout methods.
regards peter