Skip to content

Commit

Permalink
Made sampling work
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaIqbal69 committed Nov 12, 2024
1 parent 3555a67 commit 1dfeed6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
23 changes: 11 additions & 12 deletions CM4/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
Expand Down
4 changes: 2 additions & 2 deletions CM7/Core/Inc/gatedriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "foc_ctrl.h"
#include <stdbool.h>
#include <stdint.h>
#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
Expand Down Expand Up @@ -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;


Expand Down
2 changes: 1 addition & 1 deletion CM7/Core/Src/foc_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 16 additions & 8 deletions CM7/Core/Src/gatedriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
Expand All @@ -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)
Expand Down
22 changes: 9 additions & 13 deletions CM7/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
}
Expand Down
1 change: 0 additions & 1 deletion Test/devkit-freertos/Core/Src/gatedriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1dfeed6

Please sign in to comment.