Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve return values of LSM6DS33 class's methods by utilizing global error codes #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

grantKinsley
Copy link

Closes #57

Changed update() method in src/sensors/lsm6ds33.cpp to return error codes defined in src/globals.h

  1. Changed function type of update() from bool to int
  2. Added new error code ERROR_NO_NEW_DATA to globals.h

…s codes defined in `src/globals.h`

1. Changed function type of update() from bool to int
2. Added new error code `ERROR_NO_NEW_DATA` to `globals.h`
Comment on lines 203 to 205
if (hasNewData() == RESULT_FALSE)
//std::cerr << "No new data" << std::endl;
return false;
return ERROR_NO_NEW_DATA;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also be returning RESULT_FALSE; having no new data is not necessarily an error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'll also get rid of the ERROR_NO_NEW_DATA code since I made it for the sole purpose of reflecting that there was no new data.

Copy link
Member

@tiffany1618 tiffany1618 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just need to also change any methods that call update() to check for the error codes, like powerOn() and pollData().

1. powerOn() calls update()
2. pollData() calls update()
Copy link
Member

@tiffany1618 tiffany1618 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good for now. We should also be implementing a way to handle proper error propagation, but I'll probably open that up in a separate issue.

Copy link
Contributor

@harrisonCassar harrisonCassar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was taking a bit of a deeper look, and realized there's a few other places we could utilize these error codes better:

  • First of all, we seem to use the same error code ERROR_ADDR for two different error reasons. Can we separate the two? Perhaps another error code should be added
    int spartan::LSM6DS33::powerOn() {
    if (m_status == STATUS_IDLE)
    return RESULT_SUCCESS;
    // set I2C address
    if (m_i2c.address(lsm6Address) != mraa::SUCCESS) {
    std::cerr << "Unable to set I2C address." << std::endl;
    return ERROR_ADDR;
    }
    // Update settings for accelerometer and gyroscope
    if (!updateSettings()) {
    std::cerr << "Update setting failed" << std::endl;
    return ERROR_ADDR;
    }
  • Second, since we're doing this for the update method, we should probably do the equivalent changes for the writeReg and both updateSettings methods:
    bool spartan::LSM6DS33::writeReg(uint8_t* buffer, unsigned short size) {
    mraa::Result msg = m_i2c.write(buffer, size);
    // Error handling
    switch (msg) {
    case mraa::SUCCESS:
    return true;
    case mraa::ERROR_INVALID_PARAMETER:
    std::cerr << "Invalid parameter." << std::endl;
    break;
    case mraa::ERROR_INVALID_HANDLE:
    std::cerr << "ERROR_INVALID_HANDLE" << std::endl;
    break;
    case mraa::ERROR_NO_RESOURCES:
    std::cerr << "ERROR_NO_RESOURCES" << std::endl;
    break;
    case mraa::ERROR_INVALID_RESOURCE:
    std::cerr << "ERROR_INVALID_RESOURCE" << std::endl;
    break;
    case mraa::ERROR_INVALID_QUEUE_TYPE:
    std::cerr << "ERROR_INVALID_QUEUE_TYPE" << std::endl;
    break;
    case mraa::ERROR_NO_DATA_AVAILABLE:
    std::cerr << "ERROR_NO_DATA_AVAILABLE" << std::endl;
    break;
    case mraa::ERROR_INVALID_PLATFORM:
    std::cerr << "ERROR_INVALID_PLATFORM" << std::endl;
    break;
    case mraa::ERROR_PLATFORM_NOT_INITIALISED:
    std::cerr << "ERROR_PLATFORM_NOT_INITIALISED" << std::endl;
    break;
    case mraa::ERROR_UNSPECIFIED:
    std::cerr << "ERROR_UNSPECIFIED" << std::endl;
    break;
    default:
    break;
    }
    return false;
    }

    bool spartan::LSM6DS33::updateSettings() {
    // Update multiplier constant
    setMultipliers();
    // Accelerometer settings
    m_buffer[0] = lsm6ds33::CTRL1_XL;
    m_buffer[1] = (m_settings.accel_odr << 4) | m_settings.accelRange | m_settings.accelAAFreq;
    if (!writeReg(m_buffer, 2))
    return false;
    m_buffer[0] = lsm6ds33::CTRL2_G;
    m_buffer[1] = (m_settings.gyro_odr << 4) | m_settings.gyroRange;
    if (!writeReg(m_buffer, 2))
    return false;
    return true;
    }
    bool spartan::LSM6DS33::updateSettings(spartan::LSM6DS33::lsm6Settings settings) {
    m_settings = settings;
    return updateSettings();
    }

Also, just a few styling issues below... Otherwise, great work Grant!

Software/src/sensors/lsm6ds33.h Outdated Show resolved Hide resolved
Software/src/sensors/lsm6ds33.cpp Outdated Show resolved Hide resolved
Software/src/globals.h Show resolved Hide resolved
@harrisonCassar harrisonCassar changed the title Error Codes Improve return values of LSM6DS33 class's methods by utilizing global error codes Jan 28, 2021
equivalent to changes made in `update`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Utilize ERROR_* codes as return value defined in globals.h
3 participants