PDU_Code/Core/Src/Current_Monitoring.c

124 lines
4.0 KiB
C

/*
* Current_Monitoring.c
*
* Created on: 24. April, 2024
* Author: nived
*/
#include "Current_Monitoring.h"
#include "main.h"
volatile union adc1_channels {
struct {
uint16_t isense9;
uint16_t isense8;
uint16_t isense2;
uint16_t isense3;
uint16_t dietemp;
uint16_t vbat;
uint16_t vrefint;
} adcbank1;
uint16_t adcbuffer[7]; // array 7*16 bit
} adc_channels1;
// ADC's anpassen adc1 - 9, adc2 ist halt 5 , buffer anpassen und namen auch ( isense usw)
volatile union adc2_channels {
struct {
uint16_t isense11;
uint16_t isense10;
uint16_t isense5;
uint16_t isense4;
uint16_t isense1;
uint16_t isense7;
uint16_t isense6;
} adcbank1;
uint16_t adcbuffer[7];
} adc_channels2;
CurrentMeasurements current_measurements_adc_val;
GPIO_PinState adcbank1 = GPIO_PIN_RESET;
GPIO_PinState adcbank2 = GPIO_PIN_RESET;
ADC_HandleTypeDef* adc1;
ADC_HandleTypeDef* adc2;
void currentMonitor_init(ADC_HandleTypeDef* hadc1, ADC_HandleTypeDef* hadc2, // init ist initilisierung
TIM_HandleTypeDef* trigtim) {
HAL_GPIO_WritePin(DSEL_3_GPIO_Port, DSEL_3_Pin, adcbank1); //DSELs zu adc's neu zuordnen
HAL_GPIO_WritePin(DSEL_4_GPIO_Port, DSEL_4_Pin, adcbank2);
HAL_GPIO_WritePin(DSEL_5_GPIO_Port, DSEL_5_Pin, adcbank2);
HAL_GPIO_WritePin(DSEL_6_GPIO_Port, DSEL_6_Pin, adcbank2);
adc1 = hadc1;
adc2 = hadc2;
HAL_TIM_Base_Start(trigtim);
HAL_ADC_Start_DMA(hadc1, (uint32_t*)adc_channels1.adcbuffer, 7);
HAL_ADC_Start_DMA(hadc2, (uint32_t*)adc_channels2.adcbuffer, 7); // wie adc mit dma geht , red mit jasper
}
uint8_t currentMonitor_checklimits() { return 0; }
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
if (hadc == adc2) {
if (adcbank2 == GPIO_PIN_RESET) {
current_measurements_adc_val.always_on =
adc_channels2.adcbank1.isense10 * CURR_SENSE_FACTOR_2A;
current_measurements_adc_val.modevalve_1 =
adc_channels2.adcbank1.isense5 * CURR_SENSE_FACTOR_5A;
current_measurements_adc_val.ebsvalve_1 =
adc_channels2.adcbank1.isense4 * CURR_SENSE_FACTOR_5A;
adcbank2 = GPIO_PIN_SET;
} else {
current_measurements_adc_val.shutdown_circuit =
adc_channels2.adcbank1.isense10 * CURR_SENSE_FACTOR_2A;
current_measurements_adc_val.modevalve_2 =
adc_channels2.adcbank1.isense5 * CURR_SENSE_FACTOR_5A;
current_measurements_adc_val.ebsvalve_2 =
adc_channels2.adcbank1.isense4 * CURR_SENSE_FACTOR_5A;
adcbank2 = GPIO_PIN_RESET;
}
current_measurements_adc_val.fans =
adc_channels2.adcbank1.isense6 * CURR_SENSE_FACTOR_21A;
current_measurements_adc_val.aggregat =
adc_channels2.adcbank1.isense1 * CURR_SENSE_FACTOR_21A;
current_measurements_adc_val.inverter =
adc_channels2.adcbank1.isense11 * CURR_SENSE_FACTOR_10A;
current_measurements_adc_val.steering =
adc_channels2.adcbank1.isense7 * CURR_SENSE_FACTOR_10A;
HAL_GPIO_WritePin(DSEL_4_GPIO_Port, DSEL_4_Pin,
adcbank2);
HAL_GPIO_WritePin(DSEL_5_GPIO_Port, DSEL_5_Pin,
adcbank2);
HAL_GPIO_WritePin(DSEL_6_GPIO_Port, DSEL_6_Pin,
adcbank2);
}
if (hadc == adc1) {
if (adcbank1 == GPIO_PIN_RESET) {
current_measurements_adc_val.servos =
adc_channels1.adcbank1.isense8 * CURR_SENSE_FACTOR_5A;
adcbank1 = GPIO_PIN_SET;
} else {
current_measurements_adc_val.misc =
adc_channels1.adcbank1.isense8 * CURR_SENSE_FACTOR_5A;
adcbank1 = GPIO_PIN_RESET;
}
current_measurements_adc_val.servicebrake =
adc_channels1.adcbank1.isense3 * CURR_SENSE_FACTOR_2A;
current_measurements_adc_val.sensorbox =
adc_channels1.adcbank1.isense2 * CURR_SENSE_FACTOR_21A;
current_measurements_adc_val.pumps =
adc_channels1.adcbank1.isense9 * CURR_SENSE_FACTOR_10A;
HAL_GPIO_WritePin(DSEL_3_GPIO_Port, DSEL_3_Pin,
adcbank1);
}
}
// current monitoring c und h anpassen