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

IMU Driver for ICM42688 #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

kelvinos2624
Copy link

Created cpp and header files for the ICM42688. The code has been structured from the tested code found at https://github.com/UWARG/efs-kitchen-sinks/blob/main/icm42688.zip

Description

What was completed, changed, or updated?
The ICM Driver (previously created, tested, and stored in https://github.com/UWARG/efs-kitchen-sinks/blob/main/icm42688.zip) has been restructured into a virtual class header file, the actual driver header file, and the driver code.

Why was this done (if applicable)?
This was done to leave the option for the flight controller to initiate a different imu hardware driver or run a mock imu class for unit test purposes

Testing

What manual tests were used to validate the code?
Functionality was validated with the original code on an STM board, where accelerometer and gyroscope data were successfully read from the IMU. The accuracy of accelerometer data was determined by testing for a 1g reading on each axis. The accuracy of gyroscope data was determined by temporarily implementing sensor fusion to obtain angles, from which the angles were validated.
Note: Testing for the code in this PR has not been done; only for the code in kitchensinks

What unit tests were used to validate the code?
N/A

Documentation

Milestone number and name:
M3

Link to Asana task:
https://app.asana.com/0/1203458353737758/1204645027312230

Link to Confluence documentation:
N/A

Created cpp and header files for the ICM42688. Code has been structured from the tested code found at https://github.com/UWARG/efs-kitchen-sinks/blob/main/icm42688.zip
@kelvinos2624 kelvinos2624 self-assigned this Sep 25, 2024
@kelvinos2624 kelvinos2624 marked this pull request as ready for review September 25, 2024 02:09
Copy link
Contributor

@HardyYu HardyYu left a comment

Choose a reason for hiding this comment

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

Good stuff! I like how you made detailed comments in the code, and clean format. They make the code super readable. Thanks for the effort you spent in this pr.

// icm42688.cpp

#include "icm42688.hpp"
#include "stm32l5xx_hal_conf"
Copy link
Contributor

Choose a reason for hiding this comment

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

this line looks weird, it doesn't have a file extension

Copy link
Author

Choose a reason for hiding this comment

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

Initially i didn't have a file extension because my intellisense was giving me errors when I included a .h at the end and I wasn't able to find out why. Are these squiggles something that I can ignore if I do include the file extension?
image

Copy link
Member

Choose a reason for hiding this comment

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

Are you on Linux? Try building on linux, that might tell you where the error is.

Copy link
Member

Choose a reason for hiding this comment

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

I also just went through the build - I can't find any reference to "icm" or "hal_conf", are we sure your files are included properly in cmakelists.txt and are being built?

*
* @return none
*/
void readRegister(uint8_t sub_address, uint8_t count, uint8_t * dest);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, we keep all the methods below private. Since the three methods above are the methods that users would call/interface, the functions below are those that help facilitate imu reading, not something that users want to interact with directly. We should keep them private and hide them.


class ICM42688 : public IMUDriver {
public:
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably the constructor is missing here. It would be great if we could make the constructor take in an SPI instance and the CS pin. This way, this driver is not limited to only working with spi1 and cs pin at GPIOA4.

float gyroBD[3] = {0, 0, 0};
float gyrB[3] = {0, 0, 0};
float gyr[3] = {0, 0, 0};
uint8_t gyro_buffer[14];
Copy link
Contributor

Choose a reason for hiding this comment

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

The numbers 14, 7, and 7 here are magic numbers in this context. I would suggest creating macros to name these numbers, so the code would be much more readable.

I would actually also recommend replacing lines such as const uint8_t REG_BANK_SEL = 0x76;, to #define REG_BANK_SEL 0x76. In this way, these numbers won't take up any memory, they are handled by the preprocessor instead.


void ICM42688::readRegister(uint8_t sub_address, uint8_t count, uint8_t * dest) {
//Set read bit for register address
uint8_t tx = sub_address | 0x80;
Copy link
Member

Choose a reason for hiding this comment

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

Be careful with magic numbers

Comment on lines +128 to +134
IMUData.accx = (float)raw_meas[1] / 2048.0; //Sensitivity Scale Factor in LSB/g
IMUData.accy = (float)raw_meas[2] / 2048.0; //2048.0 Corresponds with +/- 16g
IMUData.accz = (float)raw_meas[3] / 2048.0;

IMUData.gyrx = (float)raw_meas[4] / 16.4; //Sensitivity Scale Factor LSB/dps
IMUData.gyry = (float)raw_meas[5] / 16.4; //16.4 Corresponds with +/- 2000dps
IMUData.gyrz = (float)raw_meas[6] / 16.4;
Copy link
Member

Choose a reason for hiding this comment

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

Are these scales selectable? Why did we choose these values?

Copy link
Author

Choose a reason for hiding this comment

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

They are selectable and I believe the values chosen are scale factors reflective of the IMU's default accel and gyro ranges (which are +/- 16g and +/- 2000dps respectively). These values prioritize a larger range over higher sensitivity. If we want more sensitivity, we would need to sacrifice range to do so. Currently, the code is not designed to adjust accelFS; if required though, it could be added. In the meantime, I'll use macros for these values for better readability and to make future changes easier.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

If you're doing this properly, create an ENUM (const) or defines (follow the warg style guide), and then when you call these numbers you would go SENSITIVITY.12G or SENSITIVITY.LOW.

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.

3 participants