MicroForce Sensors, Compensated/Amplified
Diff: MicroForce_FMA.cpp
- Revision:
- 1:4b8a700b3f5c
- Parent:
- 0:e4146617a5fc
- Child:
- 2:2f45d928f6e0
--- a/MicroForce_FMA.cpp Wed Jun 30 12:54:49 2021 +0000 +++ b/MicroForce_FMA.cpp Wed Jun 30 13:29:31 2021 +0000 @@ -1,23 +1,23 @@ /** - * @brief MAX7219.h - * @details Serially Interfaced, 8-Digit LED Display Drivers. - * Header file. + * @brief MicroForce_FMA.h + * @details MicroForce Sensors, Compensated/Amplified. + * Functions file. * * - * @return NA + * @return N/A * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN. - * @warning NaN - * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre N/A. + * @warning N/A + * @pre This code belongs to Nimbus Centre ( https://www.nimbus.cit.ie ). */ -#include "MAX7219.h" +#include "MicroForce_FMA.h" -MAX7219::MAX7219 ( PinName mosi, PinName miso, PinName sclk, PinName cs, uint32_t freq ) +MicroForce_FMA::MicroForce_FMA ( PinName mosi, PinName miso, PinName sclk, PinName cs, uint32_t freq ) : _spi ( mosi, miso, sclk ) , _cs ( cs ) { @@ -25,297 +25,304 @@ } -MAX7219::~MAX7219() +MicroForce_FMA::~MicroForce_FMA() { } /** - * @brief MAX7219_Mode ( MAX7219_shutdown_reg_t ) + * @brief MicroForce_FMA_GetRawForce ( MicroForce_FMA_status_bits_data_t* , uint16_t* ) * - * @details It puts the device in shutdown mode. + * @details It gets the raw force data. * - * @param[in] myMAX7219mode: Shutdown or Normal operation mode. + * @param[in] N/A. * - * @param[out] NaN. + * @param[out] myStatus: Status bits data. + * @param[out] myRawBridgeData: Raw bridge data. * * - * @return Status of MAX7219_Mode. + * @return Status of MicroForce_FMA_GetRawForce. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre N/A. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_Mode ( MAX7219_shutdown_reg_t myMAX7219mode ) +MicroForce_FMA::MicroForce_FMA_status_t MicroForce_FMA::MicroForce_FMA_GetRawForce ( MicroForce_FMA_status_bits_data_t* myStatus, uint16_t* myRawBridgeData ) { - char cmd[] = { SHUTDOWN, 0 }; - int mySPI_status = 0; - + char cmd[] = { 0, 0 }; + uint32_t mySPI_status; - cmd[ 1 ] = myMAX7219mode; - + /* Get the data */ _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); + mySPI_status = _spi.write ( &cmd[0], 0U, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); _cs = 1; + /* Parse the data */ + myStatus->status_bits = (MicroForce_FMA_status_bits_t)( cmd[0] & MicroForce_FMA_STATUS_BITS_MASK ); - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; + *myRawBridgeData = ( cmd[0] & 0x3F ); + *myRawBridgeData <<= 8U; + *myRawBridgeData |= cmd[1]; + + if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) { + return MicroForce_FMA_SUCCESS; + } else { + return MicroForce_FMA_FAILURE; + } } /** - * @brief MAX7219_DisplayTest ( MAX7219_display_test_reg_t ) + * @brief MicroForce_FMA_SoftwareShutdown ( MicroForce_FMA_transfer_function_t , char , uint16_t ) * - * @details It turns all the LEDs on ( Test mode enabled ) or normal operation. + * @details It calculates the force data in Newton (N). * - * @param[in] myMAX7219DisplayTestMode: Mode: Test or Normal operation. + * @param[in] myBridgeData: Raw bridge data. + * @param[in] myCalibration: Calibration point. + * @param[in] myForceRange: Force range. * - * @param[out] NaN. + * @param[out] N/A * * - * @return Status of MAX7219_DisplayTest. + * @return Force value in Newton. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre The function: MicroForce_FMA_GetRawForce must be called to update the bridge data values. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_DisplayTest ( MAX7219_display_test_reg_t myMAX7219DisplayTestMode ) +float MicroForce_FMA::MicroForce_FMA_CalculateForce ( MicroForce_FMA_transfer_function_t myCalibration, uint8_t myForceRange, uint16_t myRawBridgeData ) { - char cmd[] = { DISPLAY_TEST, 0 }; - int mySPI_status = 0; + uint16_t output_max; + uint16_t output_min; - cmd[ 1 ] = myMAX7219DisplayTestMode; + /* It calculates the calibration point depending on the Transfer Function point of the device selected. ( Datasheet. FIGURE 2. NOMENCLATURE AND ORDER GUIDE 32347833 Issue D ) */ + if ( myCalibration == MicroForce_FMA_TRANSFER_FUNCTION_10_TO_90 ) { + output_max = 14745U; + output_min = 1638U; + } else { + output_max = 13107U; + output_min = 3276U; + } - _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); - _cs = 1; - - - - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; + /* It calculates the force in N */ + return (float)( ( myRawBridgeData - output_min ) * myForceRange )/( output_max - output_min ); } /** - * @brief MAX7219_DecodeMode ( MAX7219_decode_mode_reg_t ) + * @brief MicroForce_FMA_GetRaw8bitTemperature ( MicroForce_FMA_status_bits_data_t* , char* ) * - * @details It enables and configures the decode-mode or turns it off. + * @details It gets the raw 8-bit temperature. * - * @param[in] myMAX7219DecodeMode: Decode-mode option. + * @param[in] N/A * - * @param[out] NaN. + * @param[out] myStatus: Status bits data. + * @param[out] myRaw8bitTemp: Raw 8-bit Temperature data. * * - * @return Status of MAX7219_DecodeMode. + * @return Status of MicroForce_FMA_GetRaw8bitTemperature. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre N/A. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_DecodeMode ( MAX7219_decode_mode_reg_t myMAX7219DecodeMode ) +MicroForce_FMA::MicroForce_FMA_status_t MicroForce_FMA::MicroForce_FMA_GetRaw8bitTemperature ( MicroForce_FMA_status_bits_data_t* myStatus, uint8_t* myRaw8bitTemp ) { - char cmd[] = { DECODE_MODE, 0 }; - int mySPI_status = 0; + char cmd[] = { 0, 0, 0 }; + uint32_t mySPI_status; + + /* Get the data */ + _cs = 0; + mySPI_status = _spi.write ( &cmd[0], 0U, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); + _cs = 1; + + /* Parse the data */ + myStatus->status_bits = (MicroForce_FMA_status_bits_t)( cmd[0] & MicroForce_FMA_STATUS_BITS_MASK ); + + *myRaw8bitTemp = cmd[2]; - cmd[ 1 ] = myMAX7219DecodeMode; - - - _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); - _cs = 1; - - - - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; + if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) { + return MicroForce_FMA_SUCCESS; + } else { + return MicroForce_FMA_FAILURE; + } } /** - * @brief MAX7219_SetIntensity ( MAX7219_intensity_reg_t ) + * @brief MicroForce_FMA_GetRaw11bitTemperature ( MicroForce_FMA_status_bits_data_t* , uint16_t* ) * - * @details It configures the intensity of the device. + * @details It gets the raw 11-bit temperature. * - * @param[in] myMAX7219Intensity: Intensity option. + * @param[in] N/A. * - * @param[out] NaN. + * @param[out] myStatus: Status bits data. + * @param[out] myRaw11bitTemp: Raw 11-bit Temperature data. * * - * @return Status of MAX7219_SetIntensity. + * @return Status of MicroForce_FMA_GetRaw11bitTemperature. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre N/A. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_SetIntensity ( MAX7219_intensity_reg_t myMAX7219Intensity ) +MicroForce_FMA::MicroForce_FMA_status_t MicroForce_FMA::MicroForce_FMA_GetRaw11bitTemperature ( MicroForce_FMA_status_bits_data_t* myStatus, uint16_t* myRaw11bitTemp ) { - char cmd[] = { INTENSITY, 0 }; - int mySPI_status = 0; + char cmd[] = { 0, 0, 0, 0 }; + uint32_t mySPI_status; + + /* Get the data */ + _cs = 0; + mySPI_status = _spi.write ( &cmd[0], 0U, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); + _cs = 1; + + /* Parse the data */ + myStatus->status_bits = (MicroForce_FMA_status_bits_t)( cmd[0] & MicroForce_FMA_STATUS_BITS_MASK ); + + *myRaw11bitTemp = cmd[2]; + *myRaw11bitTemp <<= 8U; + *myRaw11bitTemp |= cmd[3]; + + *myRaw11bitTemp >>= 5U; - cmd[ 1 ] = myMAX7219Intensity; - - - _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); - _cs = 1; - - - - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; + if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) { + return MicroForce_FMA_SUCCESS; + } else { + return MicroForce_FMA_FAILURE; + } } /** - * @brief MAX7219_SetScanLimit ( MAX7219_scan_limit_reg_t ) + * @brief MicroForce_FMA_Calculate8bitTemperature ( char ) * - * @details It configures duty cycle of the device. + * @details It calculates the 8-bit temperature in Celsius degree. * - * @param[in] MAX7219_scan_limit_reg_t: Duty cycle option. + * @param[in] myRawTemperature: Raw 8-bit temperature value. * - * @param[out] NaN. + * @param[out] N/A * * - * @return Status of MAX7219_SetScanLimit. + * @return Temperature value in Celsius degree. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre The function:MicroForce_FMA_GetRaw8bitTemperature must be called to update the temperature value. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_SetScanLimit ( MAX7219_scan_limit_reg_t myMAX7219ScanLimit ) +float MicroForce_FMA::MicroForce_FMA_Calculate8bitTemperature ( uint8_t myRawTemperature ) { - char cmd[] = { SCAN_LIMIT, 0 }; - int mySPI_status = 0; - + uint16_t aux_temp; - cmd[ 1 ] = myMAX7219ScanLimit; - - - _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); - _cs = 1; + /* It processes the data correctly. ( Datasheet. Equation 3: Temperature Conversion Function */ + aux_temp = (uint16_t)myRawTemperature; + aux_temp <<= 3U; + aux_temp &= 0x7F8; - - - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; + /* It calculates the temperature in C */ + return (float)( ( ( aux_temp / 2047.0 ) * 200.0 ) - 50.0 ); } /** - * @brief MAX7219_SetDigit ( MAX7219_digit_t , MAX7219_code_b_font_reg_t , MAX7219_code_b_dp_t ) + * @brief MicroForce_FMA_Calculate11bitTemperature ( uint16_t ) * - * @details It sets the digit to be written and its value. + * @details It calculates the 11-bit temperature in Celsius degree. * - * @param[in] myMAX7219Digit: Digit to be written. - * @param[in] myCharacter: The current value. - * @param[in] myDP_status: DP enabled/disabled. + * @param[in] myRawTemperature: Raw 11-bit temperature value. * - * @param[out] NaN. + * @param[out] N/A * * - * @return Status of MAX7219_SetDigit. + * @return Temperature value in Celsius degree. * * * @author Manuel Caballero - * @date 9/October/2017 - * @version 9/October/2017 The ORIGIN - * @pre NaN - * @warning NaN. + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre The function:MicroForce_FMA_GetRaw11bitTemperature must be called to update the temperature value. + * @warning N/A. */ -MAX7219::MAX7219_status_t MAX7219::MAX7219_SetDigit ( MAX7219_digit_t myMAX7219Digit, MAX7219_code_b_font_reg_t myCharacter, MAX7219_code_b_dp_t myDP_status ) +float MicroForce_FMA::MicroForce_FMA_Calculate11bitTemperature ( uint16_t myRawTemperature ) { - char cmd[] = { 0, 0 }; - int mySPI_status = 0; - - - switch ( myMAX7219Digit ) { - default: - case SET_DIGIT_0: - cmd[ 0 ] = DIGIT_0; - break; - - case SET_DIGIT_1: - cmd[ 0 ] = DIGIT_1; - break; - - case SET_DIGIT_2: - cmd[ 0 ] = DIGIT_2; - break; - - case SET_DIGIT_3: - cmd[ 0 ] = DIGIT_3; - break; - - case SET_DIGIT_4: - cmd[ 0 ] = DIGIT_4; - break; - - case SET_DIGIT_5: - cmd[ 0 ] = DIGIT_5; - break; - - case SET_DIGIT_6: - cmd[ 0 ] = DIGIT_6; - break; - - case SET_DIGIT_7: - cmd[ 0 ] = DIGIT_7; - break; - } - - - if ( myDP_status == DP_ENABLED ) - cmd[ 1 ] = ( myCharacter | 0x80 ); - else - cmd[ 1 ] = myCharacter; - - - _cs = 0; - mySPI_status = _spi.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), &cmd[0], 0 ); - _cs = 1; + /* It calculates the temperature in C */ + return (float)( ( ( myRawTemperature / 2047.0 ) * 200.0 ) - 50.0 ); +} - if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) - return MAX7219_SUCCESS; - else - return MAX7219_FAILURE; +/** + * @brief MicroForce_FMA_GetAllRawData ( MicroForce_FMA_status_bits_data_t* , uint16_t* , uint16_t* ) + * + * @details It gets the all raw data ( bridge data and 11-bit temperature ). + * + * @param[in] N/A. + * + * @param[out] myStatus: Status bits data. + * @param[out] myRawBridgeData: Raw bridge data. + * @param[out] myRaw11bitTemp: Raw 11-bit Temperature data. + * + * + * @return Status of MicroForce_FMA_GetAllRawData. + * + * + * @author Manuel Caballero + * @date 29/June/2021 + * @version 29/June/2021 The ORIGIN + * @pre N/A. + * @warning N/A. + */ +MicroForce_FMA::MicroForce_FMA_status_t MicroForce_FMA::MicroForce_FMA_GetAllRawData ( MicroForce_FMA_status_bits_data_t* myStatus, uint16_t* myRawBridgeData, uint16_t* myRaw11bitTemp ) +{ + char cmd[] = { 0, 0, 0, 0 }; + uint32_t mySPI_status; + + /* Get the data */ + _cs = 0; + mySPI_status = _spi.write ( &cmd[0], 0U, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); + _cs = 1; + + /* Parse the data */ + myStatus->status_bits = (MicroForce_FMA_status_bits_t)( cmd[0] & MicroForce_FMA_STATUS_BITS_MASK ); + + *myRawBridgeData = ( cmd[0] & 0x3F ); + *myRawBridgeData <<= 8U; + *myRawBridgeData |= cmd[1]; + + *myRaw11bitTemp = cmd[2]; + *myRaw11bitTemp <<= 8U; + *myRaw11bitTemp |= cmd[3]; + + *myRaw11bitTemp >>= 5U; + + + if ( ( mySPI_status / ( sizeof( cmd )/sizeof( cmd[0] ) ) ) == SPI_SUCCESS ) { + return MicroForce_FMA_SUCCESS; + } else { + return MicroForce_FMA_FAILURE; + } }