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

Feature/86dt0u36a/lim temp polling #17

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Common/Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ const Data_Segment_t BMS_ERROR_CODE = {BMS_BOARD, 8, 8};

// BEGIN SENSORS BOARD DEFS
//FIRST FRAME
const Data_Segment_t PRESSURE = {SENSOR_BOARD_1, 1, 2};
const Data_Segment_t LIM_ONE_TEMP = {SENSOR_BOARD_1, 3, 4};
const Data_Segment_t LIM_TWO_TEMP = {SENSOR_BOARD_1, 5, 6};
const Data_Segment_t PRESSURE = {SENSOR_BOARD_1, 1, 1};
const Data_Segment_t LIM_ONE_TEMP_ONE = {SENSOR_BOARD_1, 2, 2};
const Data_Segment_t LIM_ONE_TEMP_TWO = {SENSOR_BOARD_1, 3, 3};
const Data_Segment_t LIM_ONE_TEMP_THREE = {SENSOR_BOARD_1, 4, 4};
const Data_Segment_t LIM_TWO_TEMP_ONE = {SENSOR_BOARD_1, 5, 5};
const Data_Segment_t LIM_ONE_TEMP_TWO = {SENSOR_BOARD_1, 6, 6};
const Data_Segment_t LIM_TWO_TEMP_THREE = {SENSOR_BOARD_1, 7, 7};
const Data_Segment_t SENSORS_ERROR_CODE_1 = {SENSOR_BOARD_1, 8, 8};
//SECOND (IMU) FRAME
const Data_Segment_t X_ACCEL = {SENSOR_BOARD_2, 1, 2};
Expand Down
30 changes: 16 additions & 14 deletions Sensors/.mxproject

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions Sensors/Core/Inc/adc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file adc.h
* @brief This file contains all the function prototypes for
* the adc.c file
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ADC_H__
#define __ADC_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern ADC_HandleTypeDef hadc3;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_ADC3_Init(void);

/* USER CODE BEGIN Prototypes */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __ADC_H__ */

33 changes: 33 additions & 0 deletions Sensors/Core/Inc/lim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* lim.h
*
* Created on: May 9, 2024
* Author: mahir
*/

#ifndef INC_LIM_H_
#define INC_LIM_H_

#include <stdint.h>

#define NUM_THERM_TOTAL 6U
#define NUM_LIMS 2U
#define NUM_THERM_PER_LIM NUM_THERM_TOTAL / NUM_LIMS
#define NUM_MUX 1U

//Steinhart-Hart constants
#define MAX_ADC_COUNT 4095U
#define ABSOLUTE_ZERO 273.15
#define NOMINAL_TEMPERATURE 298.15
#define NOMINAL_RESISTANCE 10000U
#define B_COEFFICIENT 3950U

#define VOLTAGE_SUPPLY 5
#define MAX_VOLTAGE 3.3
#define R10K 10000U


void get_lim_data(uint8_t lim_temps[NUM_LIMS]);
uint8_t get_temp(uint16_t adc_value);

#endif /* INC_LIM_H_ */
2 changes: 1 addition & 1 deletion Sensors/Core/Inc/stm32f7xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define HAL_MODULE_ENABLED

/* #define HAL_CRYP_MODULE_ENABLED */
/* #define HAL_ADC_MODULE_ENABLED */
#define HAL_ADC_MODULE_ENABLED
#define HAL_CAN_MODULE_ENABLED
/* #define HAL_CEC_MODULE_ENABLED */
/* #define HAL_CRC_MODULE_ENABLED */
Expand Down
128 changes: 128 additions & 0 deletions Sensors/Core/Src/adc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file adc.c
* @brief This file provides code for the configuration
* of the ADC instances.
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "adc.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

ADC_HandleTypeDef hadc3;

/* ADC3 init function */
void MX_ADC3_Init(void)
{

/* USER CODE BEGIN ADC3_Init 0 */

/* USER CODE END ADC3_Init 0 */

ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC3_Init 1 */

/* USER CODE END ADC3_Init 1 */

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc3.Instance = ADC3;
hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc3.Init.Resolution = ADC_RESOLUTION_12B;
hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc3.Init.ContinuousConvMode = DISABLE;
hadc3.Init.DiscontinuousConvMode = DISABLE;
hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc3.Init.NbrOfConversion = 1;
hadc3.Init.DMAContinuousRequests = DISABLE;
hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc3) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC3_Init 2 */

/* USER CODE END ADC3_Init 2 */

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC3)
{
/* USER CODE BEGIN ADC3_MspInit 0 */

/* USER CODE END ADC3_MspInit 0 */
/* ADC3 clock enable */
__HAL_RCC_ADC3_CLK_ENABLE();

__HAL_RCC_GPIOF_CLK_ENABLE();
/**ADC3 GPIO Configuration
PF9 ------> ADC3_IN7
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

/* USER CODE BEGIN ADC3_MspInit 1 */

/* USER CODE END ADC3_MspInit 1 */
}
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

if(adcHandle->Instance==ADC3)
{
/* USER CODE BEGIN ADC3_MspDeInit 0 */

/* USER CODE END ADC3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_ADC3_CLK_DISABLE();

/**ADC3 GPIO Configuration
PF9 ------> ADC3_IN7
*/
HAL_GPIO_DeInit(GPIOF, GPIO_PIN_9);

/* USER CODE BEGIN ADC3_MspDeInit 1 */

/* USER CODE END ADC3_MspDeInit 1 */
}
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
15 changes: 14 additions & 1 deletion Sensors/Core/Src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,27 @@

/* USER CODE END 1 */

/** Pinout Configuration
/** Configure pins
*/
void MX_GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);

/*Configure GPIO pins : PF0 PF1 PF2 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

}

/* USER CODE BEGIN 2 */
Expand Down
38 changes: 38 additions & 0 deletions Sensors/Core/Src/lim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* lim.c
*
* Created on: May 9, 2024
* Author: mahir
*/

#include "lim.h"
#include "adc.h"
#include <math.h>

void get_lim_data(uint8_t lim_temps[NUM_THERM_TOTAL]) {
for (uint8_t i = 0; i < NUM_LIMS; i++) {
lim_temps[i] = 0;
}

for (uint8_t i = 0; i < NUM_THERM_TOTAL; i++) {
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_0, i & 0x1);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, (i >> 1) & 0x1);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, (i >> 2) & 0x1);

HAL_ADC_Start(&hadc3);
HAL_ADC_PollForConversion(&hadc3, 100);
uint16_t adc_data = HAL_ADC_GetValue(&hadc3);

lim_temps[i] += get_temp(adc_data);
}
}

uint8_t get_temp(uint16_t adc_value) {
float voltage_in = adc_value * (MAX_VOLTAGE / MAX_ADC_COUNT);
float thermistor_resistance = (voltage_in * R10K) / (VOLTAGE_SUPPLY - voltage_in);

uint8_t temp_steinhart = -ABSOLUTE_ZERO + (NOMINAL_TEMPERATURE * B_COEFFICIENT)/(NOMINAL_TEMPERATURE
* log(thermistor_resistance / R10K) + B_COEFFICIENT);

return temp_steinhart;
}
16 changes: 11 additions & 5 deletions Sensors/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "can.h"
#include "i2c.h"
#include "gpio.h"
Expand All @@ -27,6 +28,7 @@
#include "can_driver.h"
#include "config.h"
#include "mpu6050.h"
#include "lim.h"

/* USER CODE END Includes */

Expand Down Expand Up @@ -92,6 +94,7 @@ int main(void)
MX_GPIO_Init();
MX_CAN3_Init();
MX_I2C2_Init();
MX_ADC3_Init();
/* USER CODE BEGIN 2 */
HAL_CAN_Start(&hcan3);
MPU6050_Init(hi2c2);
Expand All @@ -102,8 +105,7 @@ int main(void)
int8_t x_gyro = 0;
int8_t y_gyro = 0;
int8_t z_gyro = 0;
uint8_t lim_temp_1 = 0;
uint8_t lim_temp_2 = 0;
uint8_t lim_temps[NUM_THERM_TOTAL];
uint8_t error_code_1 = 0;
uint8_t error_code_2 = 0;

Expand All @@ -123,12 +125,16 @@ int main(void)
MPU6050_Read_Gyro(&x_gyro, &y_gyro, &z_gyro);

//poll LIM thermistors
//...
get_lim_data(lim_temps);

//Pack CAN messages
CAN_set_segment(&tx_frame, PRESSURE, pressure);
CAN_set_segment(&tx_frame, LIM_ONE_TEMP, lim_temp_1);
CAN_set_segment(&tx_frame, LIM_TWO_TEMP, lim_temp_2);
CAN_set_segment(&tx_frame, LIM_ONE_TEMP_ONE, lim_temps[0]);
CAN_set_segment(&tx_frame, LIM_ONE_TEMP_TWO, lim_temps[1]);
CAN_set_segment(&tx_frame, LIM_ONE_TEMP_THREE, lim_temps[2]);
CAN_set_segment(&tx_frame, LIM_TWO_TEMP_ONE, lim_temps[3]);
CAN_set_segment(&tx_frame, LIM_TWO_TEMP_TWO, lim_temps[4]);
CAN_set_segment(&tx_frame, LIM_TWO_TEMP_THREE, lim_temps[5]);
CAN_set_segment(&tx_frame, SENSORS_ERROR_CODE_1, error_code_1);

CAN_set_segment(&imu_frame, X_ACCEL, x_accel);
Expand Down
Loading