2021-09-13 07:55 AM
Hi, I am using the code below to spin my brushless DC motor. When I used another code where I have a potentiometer to vary the motor speed, the motor spins without problem so I don't think the DC motor and ESC are faulty. Is there something wrong with my code? Am I starting the PWM at wrong time?
I have checked the PWM generated by this code. The signal is there.
main() function:
int main(void) {
/* USER CODE BEGIN 1 */
char data[20] = { '\0' };
uint8_t controller_cnt = 0; // default is P controller
char controller = 'P';
uint8_t row = 0;
const uint8_t coef_origin[3] = { 11, 2, 10 };
uint8_t coef_loc[3] = { 11, 2, 10 };
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_TIM2_Init();
MX_USART2_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
lcd_init();
mpu6050_init();
HAL_Delay(500);
mpu6050_calc_offset();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // start PWM signal
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// Change controller_cnt
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12)) {
if (controller_cnt < 2)
controller_cnt++;
else
controller_cnt = 0;
switch (controller_cnt) {
case 0:
controller = 'P';
break;
case 1:
controller = 'I';
break;
case 2:
controller = 'D';
break;
}
// move cursor and record cursor position
if (controller_cnt == 0)
row = 0;
else
row = 1;
lcd_put_cur(row, coef_origin[controller_cnt]);
coef_loc[controller_cnt] = coef_origin[controller_cnt];
lcd_clear();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_Delay(150);
}
// Up
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13)) {
switch (coef_loc[controller_cnt] - coef_origin[controller_cnt]) {
case 0:
if (pid_coef[controller_cnt] <= 89.99)
pid_coef[controller_cnt] += 10.00;
break;
case 1:
if (pid_coef[controller_cnt] <= 98.99)
pid_coef[controller_cnt] += 1.00;
break;
case 3:
if (pid_coef[controller_cnt] <= 99.89)
pid_coef[controller_cnt] += 0.10;
break;
case 4:
if (pid_coef[controller_cnt] <= 99.98)
pid_coef[controller_cnt] += 0.01;
break;
}
lcd_clear();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_Delay(150);
}
// Down
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14)) {
switch (coef_loc[controller_cnt] - coef_origin[controller_cnt]) {
case 0:
if (pid_coef[controller_cnt] >= 10.00)
pid_coef[controller_cnt] -= 10.00;
break;
case 1:
if (pid_coef[controller_cnt] >= 1.00)
pid_coef[controller_cnt] -= 1.00;
break;
case 3:
if (pid_coef[controller_cnt] >= 0.10)
pid_coef[controller_cnt] -= 0.10;
break;
case 4:
if (pid_coef[controller_cnt] >= 0.01)
pid_coef[controller_cnt] -= 0.01;
break;
}
lcd_clear();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_Delay(150);
}
// Left
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15)) {
if (coef_loc[controller_cnt] > coef_origin[controller_cnt]) {
coef_loc[controller_cnt] -= 1;
lcd_send_cmd(0x10);
}
lcd_clear();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_Delay(150);
}
// Right
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8)) {
if (coef_loc[controller_cnt] < coef_origin[controller_cnt] + 4) {
coef_loc[controller_cnt] += 1;
lcd_send_cmd(0x14);
}
lcd_clear();
lcd_put_cur(0, 0);
sprintf(data, "Tuning:%c P:%05.2f", controller, pid_coef[0]);
lcd_send_string(data);
lcd_put_cur(1, 0);
sprintf(data, "I:%05.2f D:%05.2f", pid_coef[1], pid_coef[2]);
lcd_send_string(data);
lcd_put_cur(row, coef_loc[controller_cnt]);
HAL_Delay(150);
}
}
/* USER CODE END 3 */
}
Timer callback:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
float error;
static float past_error;
float roll_pid_p;
static float roll_pid_i = 0;
float roll_pid_d;
float roll_pid;
uint16_t left_pwm, right_pwm;
const uint16_t max_pwm = 1800;
const uint16_t thrust = 1300;
mpu6050_read_data();
mpu6050_calc_angle();
// COMPUTE PID
error = comp_roll - 0; // setpoint = 0
roll_pid_p = pid_coef[0] * error;
roll_pid_i = roll_pid_i + pid_coef[1] * error;
roll_pid_d = pid_coef[2] * ((error - past_error) / 0.02);// 0.02 is update rate
roll_pid = roll_pid_p + roll_pid_i + roll_pid_d;
past_error = error;
// compute PWM pulse width
left_pwm = thrust - (int) roll_pid;
right_pwm = thrust + (int) roll_pid;
if (left_pwm > 1800)
left_pwm = 1800;
else if (left_pwm < 1200)
left_pwm = 1200;
if (right_pwm > 1800)
right_pwm = 1800;
else if (right_pwm < 1200)
right_pwm = 1200;
uint16_t ccr1 = (uint16_t) ((((float)left_pwm / 1000000) / 0.02) * 65535);
uint16_t ccr2 = (uint16_t) ((((float)right_pwm / 1000000) / 0.02) * 65535);
TIM2->CCR1 = ccr1;
TIM2->CCR2 = ccr2;
}
2021-10-20 05:28 AM
Dear @M7890.1
Welcome to the STM32 Community
Sorry for late answer.
Could you give more details to the STM32 Community about your setup -the material you use- ?
(HW and also SW: CPU(s), tools and versions, board(s), motor(s) and so on)
And more especially did you use STM32 MC tools (such as MC_suite, STM32 MC Motor Profile, STM32 MC SDK, STM32 MC Workbench, the used example, the origin of the base of your application source code, and so on)?
Best regards