UPDATE: Here is a link to my forked version of the library updated for the Spark Core:
I would really love to use the Sparkfun Triple Axis Accelerometer Breakout - MMA8452Q with the Spark Core but it appears the library would need to be ported.
Here is the original library:
I took the example sketch from Sparkfun here:
https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
And then started manually trying to work through this post to see about porting the libraries:
I got through the first several errors it reported, added copies of the libraries and updated the references to them… but after the easy errors it got way over my head abruptly
Any tips on how to get started porting something like this to the Spark Core?
@DanDawson, you are close!
In the example:
- comment out #include “Wire.h” since it is already built-in to the Core firmware
- change #include <SFE_MMA8452Q.h> to #include “SFE_MMA8452Q.h”
In SFE_MMA8452Q.cpp:
- comment out #include <Arduino.h> and #include <Wire.h>
In SFE_MMA8452Q.h:
- comment out #include <Arduino.h> and add #include “application.h”
Give that a shot and let me know how it goes
1 Like
Thank you SO MUCH @peekay123 I really appreciate your assistance!
And I feel better about my attempt too, I had every single portion of your suggestion done up until that very last item, once I added #include “application.h” to SFE_MMA8452Q.h it verified successfully and runs perfectly!
Here is my fork of the GitHub project, I’ll work on converting the Advanced Examples in there as well, but for now the items in the Firmware/libraries/SFE_MMA8452Q folder are working properly.
Thanks again @peekay123!
2 Likes
I wish to point out that the calculation of the acceleration in G is wrong.
The actual code assumes for the calculation that the values are 16 bit integers, however the values received are 12 bit integers, so it is required to calculate the two’s compliment by hand (AND by 2048 to see if it is + or - G).
Although this calculation seems fine, I get X and Y directly maximum of scale when I move it (which is physically impossible, Z axis seems correct).
It should be something like the code here below:
float getGFromRaw( int val )
{
int mult = 0;
int signAnd = 2048;
int calcAnd = 2047;
int fdiv = 2048;
int calcVal = ( val & calcAnd );
float result = 0;
if ( ( val & signAnd ) == signAnd )
{
mult = -1;
}
else
{
mult = 1;
}
result = float( mult ) * float( calcVal ) / float( fdiv ) * scale;
return( result );
}
This is the complete correct conversion as described in the datasheet of the MMA8452Q (p.42), and it is fully working in X, Y and Z axis (I just tested by shaking my device):
float getGFromRaw( int val )
{
float result = 0;
float fVal = 0;
if ( val < 2048 )
{
fVal = float( val );
}
else
{
fVal = float( ( val & 2047 ) - 2048 );
}
result = fVal / float( 2048 ) * float( 8 );
return( result );
}```
@GrtVHecke, great work! You should do a pull request on @DanDawson’s github repo for the fix
@peekay123 , I would gladly fix the bug in Spark libraries. However, can you please tell me how I must do that (and how to publish new libraries as well). Thank you
@GrtVHecke, to suggest a fix for @DanDawson’s library you need to post a “pull request” on his github repository. If you are not familiar with github, search “github pull request” on Google to give you some guidance. As for creating and contributing Web IDE library, take a look at the Particle documentation here:
http://docs.particle.io/core/build/#flash-apps-with-particle-build-contribute-a-library
Thanks @peekay123 for helping out @GrtVHecke, this is what the Particle community is all about! As soon as I see @peekay123’s pull request come in I will make it happen and update the Library for all!
1 Like
@GrtVHecke, I can’t seem to find the getGFromRaw() function in @DanDawson’s port of the Sparkfun library. Can you tell me which file you were proposing to change?
@peekay123 , that is correct, the function is not available. because I could not change the library I create an external function which ends to be called 3 times (X, Y and Z) to have the correct G measurements.
File MMA8452-ACCELEROMETER-LIBRARY-SPARK-CORE.CPP should be updated in function read()
The calculation of cx, cy and cz (currently):
cx = (float) x / (float)(1<<11) * (float)(scale);
cy = (float) y / (float)(1<<11) * (float)(scale);
cz = (float) z / (float)(1<<11) * (float)(scale);
should be changed to:
cx = getGFromRaw( x );
cy = getGFromRaw( y );
cz = getGFromRaw( z );
and of course the function getGFromRaw should be added as private function to the class.