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

Added altimeter .hpp and .cpp files #47

Closed
wants to merge 12 commits into from
Closed

Conversation

FolaFatola
Copy link

Description

What was completed, changed, or updated?
Added altimeter driver .hpp and .cpp files.

Reads temperature and pressure and calculates height in meters.

Why was this done (if applicable)?

Testing

What manual tests were used to validate the code?
Printed values to terminal using UART.

What unit tests were used to validate the code?

Documentation

Milestone number and name:
M4

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

Link to Confluence documentation:

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! Can't wait to see it get used in ZP


#define TIMEOUT 1 << 31

typedef struct cal_coeffs{
Copy link
Contributor

Choose a reason for hiding this comment

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

could you change the struct name according to the ZP style standards? https://uwarg-docs.atlassian.net/wiki/spaces/ZP/pages/2268692486/Style+Standards

*
* @return none
*/
AltDevice(GPIO_TypeDef *ps_port, uint16_t ps_pin);
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, a driver code could be a lot easier to use if we only initiate the device once with the SPI, cs, ps line bundled with this driver code so that the user of your driver doesn't have to know the SPI it is using every time they call get_altitude function. That means I think SPI_HandleTypeDef and GPIO_TypeDef they should be the private variable of this class.

HAL_GPIO_WritePin(cs_port, cs_pin, GPIO_PIN_RESET);

/* send the digital pressure or temperature with the command */
HAL_SPI_TransmitReceive(spi_handle, digital_pres_temp, receive_buffer, 1, TIMEOUT);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this function could be changed to an interrupt based version of SPI receive in the next revision, so when the worst situation happened, we are not waiting for this timeout forever. Every return value from the communication function call to an external device should be checked. The failed communication should be handled explicitly, and inform to the function caller.

Copy link
Author

Choose a reason for hiding this comment

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

Okay

/* at least 8.22 ms delay required for ADC conversion.*/
osDelay(9);

uint8_t tx_buffer[4] = {ADC_READ, 0xFF, 0xFF, 0xFF};
Copy link
Contributor

Choose a reason for hiding this comment

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

this variable is never changed, please add const and capitalize this variable

HAL_GPIO_WritePin(cs_port, cs_pin, GPIO_PIN_SET);

//get 24-bit uncompensated pressure or temperature value value.
uint32_t uncompensated_value = (rx_buffer[1] << 16 | rx_buffer[2] << 8 | rx_buffer[3]);
Copy link
Contributor

Choose a reason for hiding this comment

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

also do a & 0x00FFFFFF at the end?


}

float pressure = (digital_pressure * (sens / 2097152.0f) - off) / 32768.0f;
Copy link
Contributor

Choose a reason for hiding this comment

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

A lot of magic numbers here in this function, it will be the best if each of them can be assigned with a name. No need for the small whole numbers such as 2, 5.

Copy link
Author

Choose a reason for hiding this comment

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

Done

float temp_lapse_rate = 0.0065f;
float exp_gmrl = 5.2558;

float reference_pressure = 101325.0f; /* in Pa */
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah like what you are doing here is awesome

Copy link
Member

@antholuo antholuo left a comment

Choose a reason for hiding this comment

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

Overall looks pretty good and solid, very pleased with what we have so far.

Some formatting changes requested (take a look at WARG's clang-format script).

Also wanted to make sure we have clear variable names, and are managing our classes / structs / data properly in order to make future-us lives easier!

edit: Also try and include full test-methodology and screenshots/photos in the PR! You want to make it as easy as possible for someone else to re-create your test and verify results, be it now or 5 months in the future

Comment on lines 13 to 37
#define PROM_READ_ADDRESS_0 0xA0
#define PROM_READ_ADDRESS_1 0xA2
#define PROM_READ_ADDRESS_2 0xA4
#define PROM_READ_ADDRESS_3 0xA6
#define PROM_READ_ADDRESS_4 0xA8
#define PROM_READ_ADDRESS_5 0xAA
#define PROM_READ_ADDRESS_6 0xAC
#define PROM_READ_ADDRESS_7 0xAE


/*digital pressure/temperature commands at different sampling rates*/
#define CONVERT_D1_OSR_256 0x40
#define CONVERT_D1_OSR_512 0x42
#define CONVERT_D1_OSR_1024 0x44
#define CONVERT_D1_OSR_2048 0x46
#define CONVERT_D1_OSR_4096 0x48
#define CONVERT_D2_OSR_256 0x50
#define CONVERT_D2_OSR_512 0x52
#define CONVERT_D2_OSR_1024 0x54
#define CONVERT_D2_OSR_2048 0x56
#define CONVERT_D2_OSR_4096 0x58

#define ADC_READ 0x0

#define TIMEOUT 1 << 31
Copy link
Member

Choose a reason for hiding this comment

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

Unless you have a very specific reason to use #define, I would strongly recommend using constexpr.

Not only is it safer, you can also then move your values into the private field of your device Class, making other functions safer // cleaner!

Depending on the compiler, you might also see performance benefits from using constexpr, although it depends on your optimization level and chip architecture.


#define TIMEOUT 1 << 31

typedef struct cal_coeffs{
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer a longer, more descriptive name here. What are these cal coeffs for? What is a cal? etc.

If it's possible to, I would recommend typing out baro_calibration_coefficients, or even ms5611_calibration_coeffs.

I also want to ask why this needs to be a stand-alone struct and not part of the class?

Comment on lines 40 to 45
uint16_t sens;
uint16_t off;
uint16_t tcs;
uint16_t tco;
uint16_t t_ref;
uint16_t temp_sens;
Copy link
Member

Choose a reason for hiding this comment

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

The same comment about descriptive names from earlier applies here as well.

Its hard for me to read this and understand what tcs or t_ref or tco mean? Temperature controlled oscillator? Timer clock speed? etc...

I can make a guess for sure, but it's best not to let the reader or future editors make guesses and assumptions when reading code that's flying.

uint16_t tco;
uint16_t t_ref;
uint16_t temp_sens;
}cal_coeffs;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}cal_coeffs;
} cal_coeffs;

This one is really just me being nitpicky, i'm sorry :(



/* To get temperature/pressure/altitude values, call (step 1) AltDevice, then (step 2) AltInit once.
* Then before getting (step 4) pressure/temp/altitude, (step 3) calculateTempPres should be called beforehand.
Copy link
Member

Choose a reason for hiding this comment

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

Why does all of this need separate steps? Should our init() handle all of these calls so that a user can initialize the barometer and then use it?

uint32_t AltDevice::uncompensatedPressureTemperature(SPI_HandleTypeDef *spi_handle, GPIO_TypeDef *cs_port, uint16_t cs_pin, uint8_t conversion_command){

uint8_t digital_pres_temp[1] = {conversion_command};
uint8_t receive_buffer[1];
Copy link
Member

Choose a reason for hiding this comment

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

same here , why are we making an array of depth 1?

Comment on lines 108 to 109
float off = coeffs_.off * 65536.0f + (coeffs_.tco * dt) / 128.0f;
float sens = coeffs_.sens * 32768.0f + (coeffs_.tcs * dt) / 256.0f;
Copy link
Member

Choose a reason for hiding this comment

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

Descriptive naming here too please!

float sens = coeffs_.sens * 32768.0f + (coeffs_.tcs * dt) / 256.0f;

/*Second order temperature compensation */
if(temp < 2000){
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(temp < 2000){
if (temp < 2000) {

Take a look at warg's clang-format if you haven't already, it should take care of stuff like this

Comment on lines 129 to 134
float pressure = (digital_pressure * (sens / 2097152.0f) - off) / 32768.0f;
pressure /= 100.0f;
temp /= 100.0f;

pres_ = pressure;
temp_ = temp;
Copy link
Member

Choose a reason for hiding this comment

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

Comments for magic numbers would be helpful.

In general, we try and avoid magic numbers and instead use consts with clear names!



float AltDevice::getAltitude(){
height_ = getAltAboveSeaLvl();
Copy link
Member

Choose a reason for hiding this comment

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

is height_ a private var? Why are we modifying a private var here but not in the above function?

@FolaFatola FolaFatola mentioned this pull request Apr 29, 2024
@DerekTang04 DerekTang04 closed this May 6, 2024
@DerekTang04 DerekTang04 deleted the drivers/altimeter branch May 6, 2024 22:05
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.

4 participants