346
void UpdatePWM(void) { if (motorRunning) { uint32_t pwmValue; char msg[100]; float tau_max = 1.0; // Maximum torque in Nm (example value) float moment_of_inertia = 0.002; // Effective mass in kg·m² float angular_acceleration; switch (currentDutyCycle) { case 0: pwmValue = 65535 * 0.25; // 25% of 65535 angular_acceleration = (0.25 * tau_max) / moment_of_inertia; sprintf(msg, "Duty Cycle: 25%%, Acceleration: %.2f rad/s^2\n\r", angular_acceleration); break; case 1: pwmValue = 65535 * 0.50; // 50% of 65535 angular_acceleration = (0.50 * tau_max) / moment_of_inertia; sprintf(msg, "Duty Cycle: 50%%, Acceleration: %.2f rad/s^2\n\r", angular_acceleration); break; case 2: pwmValue = 65535 * 0.75; // 75% of 65535 angular_acceleration = (0.75 * tau_max) / moment_of_inertia; sprintf(msg, "Duty Cycle: 75%%, Acceleration: %.2f rad/s^2\n\r", angular_acceleration); break; case 3: pwmValue = 65535 * 0.99; // 99% of 65535 angular_acceleration = (0.99 * tau_max) / moment_of_inertia; sprintf(msg, "Duty Cycle: 99%%, Acceleration: %.2f rad/s^2\n\r", angular_acceleration); break; default: pwmValue = 0; sprintf(msg, "Duty Cycle: Error\n\r"); break; } __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, pwmValue); SendUartMessage(msg); // Send duty cycle and acceleration via UART } else { __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 0); // Set PWM to 0 if motor is not running } } // Function to send UART message void SendUartMessage(char* message) { HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY); }