From 1dfeed60d4ba073e7f06c1d8ac9bf749b45a28f0 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 12 Nov 2024 18:52:57 -0500 Subject: [PATCH] Made sampling work --- CM4/Core/Src/main.c | 23 ++++++++++----------- CM7/Core/Inc/gatedriver.h | 4 ++-- CM7/Core/Src/foc_ctrl.c | 2 +- CM7/Core/Src/gatedriver.c | 24 ++++++++++++++-------- CM7/Core/Src/main.c | 22 ++++++++------------ Test/devkit-freertos/Core/Src/gatedriver.c | 1 - 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/CM4/Core/Src/main.c b/CM4/Core/Src/main.c index 5b60712..b64ff04 100644 --- a/CM4/Core/Src/main.c +++ b/CM4/Core/Src/main.c @@ -504,22 +504,21 @@ void StartDefaultTask(void *argument) /* Infinite loop */ for (;;) { - //printf("BALLS"); - - // ipcc_transfer(&ipcc, &msg); - // ipcc_signal(&ipcc); - // osDelay(1000); + HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15); + ipcc_transfer(&ipcc, &msg); + ipcc_signal(&ipcc); + osDelay(1000); // Take the semaphore and notify Cortex-M7 - // uint32_t sem_id = 0; // Use semaphore 0 - // uint32_t core_mask = HSEM_CR_COREID_Msk; // Notify Cortex-M7 core + uint32_t sem_id = 0; // Use semaphore 0 + uint32_t core_mask = HSEM_CR_COREID_Msk; // Notify Cortex-M7 core - // if (HAL_HSEM_Take(sem_id, core_mask) == HAL_OK) { - // // Successfully took semaphore, Cortex-M7 will be notified - // } - // HAL_HSEM_Release(sem_id, core_mask); + if (HAL_HSEM_Take(sem_id, core_mask) == HAL_OK) { + // Successfully took semaphore, Cortex-M7 will be notified + } + HAL_HSEM_Release(sem_id, core_mask); - // i++; + i++; } /* USER CODE END 5 */ } diff --git a/CM7/Core/Inc/gatedriver.h b/CM7/Core/Inc/gatedriver.h index 5f3af4d..ac6b8b7 100644 --- a/CM7/Core/Inc/gatedriver.h +++ b/CM7/Core/Inc/gatedriver.h @@ -7,7 +7,7 @@ #include "foc_ctrl.h" #include #include -#define NUM_SAMPLES 100 +#define NUM_SAMPLES 3000 /* * Note that these phases readings should ALWAYS be mapped to the corresponding indices * Ensure the ADC DMA is mapped the same across boards @@ -54,7 +54,7 @@ typedef struct { uint32_t time_first_sample; uint32_t timing_data_index; - gatedriver_timing_t timing_data[NUM_SAMPLES]; + gatedriver_timing_t* timing_data; } gatedriver_t; diff --git a/CM7/Core/Src/foc_ctrl.c b/CM7/Core/Src/foc_ctrl.c index 674bad3..a082596 100644 --- a/CM7/Core/Src/foc_ctrl.c +++ b/CM7/Core/Src/foc_ctrl.c @@ -31,7 +31,7 @@ enum { void foc_ctrl_init(foc_ctrl_t *controller) { - printf("FOC INIT \r\n"); + // printf("FOC INIT \r\n"); controller->data_queue = osMessageQueueNew(INBOUND_QUEUE_SIZE, sizeof(foc_data_t), NULL); controller->command_queue = osMessageQueueNew(OUTBOUND_QUEUE_SIZE, sizeof(pwm_signal_t[3]), NULL); diff --git a/CM7/Core/Src/gatedriver.c b/CM7/Core/Src/gatedriver.c index cd14eae..96865d9 100644 --- a/CM7/Core/Src/gatedriver.c +++ b/CM7/Core/Src/gatedriver.c @@ -71,6 +71,7 @@ void gatedrv_init(gatedriver_t *gatedriver, TIM_HandleTypeDef* tim, ADC_HandleTy /* Start Fake PWM signal for ADC timing */ assert(HAL_TIM_PWM_Start(gatedriver->tim, TIM_CHANNEL_4) == HAL_OK); + gatedriver->timing_data = (gatedriver_timing_t*)malloc(sizeof(gatedriver_timing_t) * NUM_SAMPLES); gatedriver->time_first_sample = 0; gatedriver->timing_data_index = 0; gatedriver->initial_reading_taken = 0; @@ -110,7 +111,7 @@ void gatedrv_get_phase_currents(gatedriver_t *drv, float phase_currents[3]) drv->time_first_sample = us_timer_get(); drv->timing_data[drv->timing_data_index].time = 0; } - if(!(drv->timing_data_index < NUM_SAMPLES)) + else if(!(drv->timing_data_index < NUM_SAMPLES)) { uint64_t total_time_interval = 0; for(int i = 0; i < NUM_SAMPLES - 1; i++) @@ -119,14 +120,21 @@ void gatedrv_get_phase_currents(gatedriver_t *drv, float phase_currents[3]) total_time_interval += time_interval; } float avg_period = (float)total_time_interval / (float)4999; - float adc_sampling_frequency = 1 / (avg_period * pow(10, -6)); - printf("Average sampling frequency measured over %ld samples: %ld Hz", NUM_SAMPLES, adc_sampling_frequency); + uint16_t adc_sampling_frequency = (uint16_t)(1 / (avg_period * pow(10, -6))); + printf("Average sampling frequency measured over %i samples: %i Hz\r\n", NUM_SAMPLES, adc_sampling_frequency); + free(drv->timing_data); + drv->timing_data = (gatedriver_timing_t*)malloc(sizeof(gatedriver_timing_t) * NUM_SAMPLES); + drv->timing_data_index = 0; } - drv->timing_data[drv->timing_data_index].time = us_timer_get() - drv->time_first_sample; - drv->timing_data[drv->timing_data_index].phase_data[0] = phase_currents[0]; - drv->timing_data[drv->timing_data_index].phase_data[1] = phase_currents[1]; - drv->timing_data[drv->timing_data_index].phase_data[2] = phase_currents[2]; - drv->timing_data_index++; + else + { + drv->timing_data[drv->timing_data_index].time = us_timer_get() - drv->time_first_sample; + drv->timing_data[drv->timing_data_index].phase_data[0] = phase_currents[0]; + drv->timing_data[drv->timing_data_index].phase_data[1] = phase_currents[1]; + drv->timing_data[drv->timing_data_index].phase_data[2] = phase_currents[2]; + drv->timing_data_index++; + } + } int16_t gatedrv_read_igbt_temp(gatedriver_t* drv) diff --git a/CM7/Core/Src/main.c b/CM7/Core/Src/main.c index 32672be..1035947 100644 --- a/CM7/Core/Src/main.c +++ b/CM7/Core/Src/main.c @@ -306,7 +306,7 @@ int main(void) while (1) { /* USER CODE END WHILE */ - printf("COOKED"); + // printf("COOKED"); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ @@ -1395,24 +1395,20 @@ void StartDefaultTask(void *argument) /* Infinite loop */ for (;;) { - printf(" BALLS \r \n"); - HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14); - printf("BALLS 2 \r \n"); - // osDelay(500); - printf("BALLS 3: %ld \r \n", (uint32_t)(duty_cycles[0])); - // printf("U: %ld A, V: %ld A, W: %ld A, Time: %ld us\r\n", - // (uint32_t)(duty_cycles[0] * 100), - // (uint32_t)(duty_cycles[1] * 100), - // (uint32_t)(duty_cycles[2] * 100), - // us_timer_get() - curr_time); + HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); + osDelay(500); + printf("U: %ld A, V: %ld A, W: %ld A, Time: %ld us\r\n", + (uint32_t)(duty_cycles[0] * 100), + (uint32_t)(duty_cycles[1] * 100), + (uint32_t)(duty_cycles[2] * 100), + us_timer_get() - curr_time); duty_cycles[0] = duty_cycles[0] + 0.01 >= 1.0 ? 0.0 : duty_cycles[0] + 0.01; - printf("BALLS 4 \r \n"); duty_cycles[1] = duty_cycles[1] + 0.01 >= 1.0 ? 0.0 : duty_cycles[1] + 0.01; duty_cycles[2] = duty_cycles[2] + 0.01 >= 1.0 ? 0.0 : duty_cycles[2] + 0.01; gatedrv_write_pwm(&gatedrv_left, duty_cycles); gatedrv_write_pwm(&gatedrv_right, duty_cycles); curr_time = us_timer_get(); - printf("Failure: %d\r\n", ipcc_transfer(&ipcc, &msg)); + // printf("Failure: %d\r\n", ipcc_transfer(&ipcc, &msg)); } /* USER CODE END 5 */ } diff --git a/Test/devkit-freertos/Core/Src/gatedriver.c b/Test/devkit-freertos/Core/Src/gatedriver.c index 8e0563e..d6cc2b1 100644 --- a/Test/devkit-freertos/Core/Src/gatedriver.c +++ b/Test/devkit-freertos/Core/Src/gatedriver.c @@ -15,7 +15,6 @@ void gatedrv_init(gatedriver_t *gatedriver, TIM_HandleTypeDef* tim, ADC_HandleTy /* Assert hardware params */ assert(tim); assert(phase_adc); - printf("GATE DRIVER INIT\r\n"); /* Set interfaces */ gatedriver->tim = tim; gatedriver->phase_adc = phase_adc;