Skip to content

Bug in setRange #19

Closed
Closed
@PaulZC

Description

@PaulZC

Hi Eli @edspark ,

Just a heads-up that I've found a bug in setRange. Long story short: I added support for the KX134 to the OLA; @VancouverUmbrella tested it in his glider and was seeing the accel data max out at 8g with 64g-mode selected.

sparkfun/OpenLog_Artemis#144 (comment)

The bug is two-fold: setRange isn't doing a read-modify-write; and the wrong bits are being set (0&1 instead of 3&4):

//////////////////////////////////////////////////
// setRange()
//
// Sets the operational g-range of the accelerometer.
//
// Parameter:
// range - sets the range of the accelerometer 2g - 32g depending
// on the version.
//
bool QwDevKX13X::setRange(uint8_t range)
{
int retVal;
if( range > 3 )
return false;
retVal = writeRegisterByte(SFE_KX13X_CNTL1, range);
if( retVal != 0 )
return false;
return true;
}

I've fixed this in the release_candidate branch - but haven't tested it yet. (I've run out of time today.)

Please don't be horrified if you look at the number of commits. I've updated the code so it uses the register bitfields you included (thanks!), instead of having 'duplicate' hard-coded bit definitions in the .cpp. It's a big change but makes the code a lot nicer.

Just FYI, there were other gremlins in there too, mostly to do with data being OR'd into the registers. That sets bits, but can't clear them. The bitfields allow setting/clearing in a single call.

I've also updated getRawAccelData so it can cope with data in the buffer being 8-bit as well as 16. In my mind the new code works, but is currently untested.

I'll test this tomorrow - probably by sticking the OLA and KX134 in an electric drill and taking it for a spin! - and then ask you to review the changes.

Thanks!
Paul

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions