Skip to content

Commit

Permalink
Some more microsecond timer stuff, janky timing
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Jun 4, 2024
1 parent 81fe9c1 commit d944ebd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions CM7/Core/Src/foc_ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ static void open_loop_ctrl(foc_ctrl_t *controller)
uint32_t dt = (uint32_t) ((uint32_t)current_time_us - (uint32_t) controller->last_run_us);
if ((int32_t)dt < 0) dt = 180; /* Account for wrap around of timer */

//printf("Time us: %ld\r\n", dt);

/* Ramp amplitude */
controller->open_loop_amplitude += ((OPEN_LOOP_MAX_AMPLITUDE ) / OPEN_LOOP_RAMP_TIME) * (dt / 1000000.0);

Expand Down Expand Up @@ -126,7 +128,7 @@ static void closed_loop_ctrl(foc_ctrl_t *controller, foc_data_t *msg)
PID_run_parallel(&controller->q_pid, 0, id_iq[D], 0, &id_iq_pid[D]);
PID_run_parallel(&controller->d_pid, controller->ref_current, id_iq[Q], 0, &id_iq_pid[Q]);

//IPARK_run(controller->ipark_transform, id_iq, alpha_beta);
IPARK_run(&controller->ipark_transform, id_iq, alpha_beta);
SVGEN_run(&controller->svm, alpha_beta, v_abc_pu);

calc_cmd[0] = (v_abc_pu[0] + 1.0) / 2.0;
Expand Down Expand Up @@ -215,10 +217,10 @@ void vFOCctrl(void *pv_params)
/* Unknown State */
break;
}
//uint32_t first_time = us_timer_get();
uint32_t first_time = us_timer_get();
//closed_loop_ctrl(controller, &msg);
open_loop_ctrl(controller);
//printf("Time us:%ld\r\n", us_timer_get() - first_time);
//open_loop_ctrl(controller);
printf("Time us:%ld\r\n", us_timer_get() - first_time);
}

/* Yield to other tasks */
Expand Down
14 changes: 7 additions & 7 deletions CM7/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ static void MX_GPIO_Init(void)
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN 5 */
int curr_time = HAL_GetTick();
int curr_time = us_timer_get();
float duty_cycles[3] = {0.5, 0.5, 0.5};
gatedrv_write_pwm(&gatedrv_left, duty_cycles);
gatedrv_write_pwm(&gatedrv_right, duty_cycles);
Expand All @@ -1397,17 +1397,17 @@ void StartDefaultTask(void *argument)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
osDelay(500);
//printf("U: %ld A, V: %ld A, W: %ld A, Time: %ld ms\r\n",
// (uint32_t)(duty_cycles[0] * 100),
// (uint32_t)(duty_cycles[1] * 100),
// (uint32_t)(duty_cycles[2] * 100),
// HAL_GetTick() - curr_time);
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;
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 = HAL_GetTick();
curr_time = us_timer_get();
//printf("Failure: %d\r\n", ipcc_transfer(&ipcc, &msg));
}
/* USER CODE END 5 */
Expand Down
2 changes: 1 addition & 1 deletion CM7/Core/Src/us_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ void us_timer_init(TIM_HandleTypeDef *htim)
uint32_t us_timer_get()
{
// the count is prescaled in CubeMX to increment once per microsecond, so the count corresponds to microseconds
return timer->Instance->CNT;
return timer->Instance->CNT / 2;
}
2 changes: 1 addition & 1 deletion Makefile/CM4/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Mon Jun 03 20:13:34 EDT 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Mon Jun 03 20:45:16 EDT 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Makefile/CM7/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Mon Jun 03 20:13:35 EDT 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Mon Jun 03 20:45:16 EDT 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down

0 comments on commit d944ebd

Please sign in to comment.