Position Control with Quadrature Encoders
Hi,
I am trying to create a User Application where I pass in commands from a terminal application (ex: Putty or Ucon) which results in the motor doing something.
I have configured the MC Workbench with the following options:


Not using the MC Pilot so I have the following in User Interface:

For the first part of the application, I was passing 'SH\r' to start the motor and 'MO\r' to stop the motor.
The code in main.c is as follows:
#define BUFFER_SIZE 3
uint8_t data_buffer[BUFFER_SIZE];
main()
{
// auto generated code
...
HAL_UART_Receive_DMA(&huart1, data_buffer, BUFFER_SIZE);
while(1)
{
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
if(data_buffer[0] == 'S' && data_buffer[1] == 'H')
{
MC_StartMotor1();
}
else if (data_buffer[0] == 'M' && data_buffer[1] == 'O')
{
MC_StopMotor1();
}
}
}
The Motor Holds a position (not sure what is the default position set to?) when I send 'SH/r' and then rotor vibrates at that position.
Sending 'MO/r' stops the motor.
This is desirable but not quite.
I have the following BLDC motor:
BL17E40-01 | BL17 Series | BLDC Motor | Lin Engineering
and I have installed the following encoder:
AMT11 Series | Modular | Incremental | Rotary Encoders (cuidevices.com)
The problem I think is these are relative encoders, and the MC workbench calls encoders with Z pins as absolute encoders.
I want to extend my user application and send commands that move the rotor to a particular position in some time.
In order to do that, I first followed the example as given in AN5464 (Position control of a three-phase permanent magnet motor using X‑CUBE‑MCSDK or X‑CUBE‑MCSDK-FUL)
I ran the following code :
/* USER CODE BEGIN 2 */
MC_StartMotor1();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(MC_GetAlignmentStatusMotor1()!=TC_ALIGNMENT_COMPLETED){}
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MC_ProgramPositionCommandMotor1(3.14/2,0.1);
HAL_Delay(2000);
MC_ProgramPositionCommandMotor1(-3.14/2,0.1);
HAL_Delay(2000);
}
/* USER CODE END 3 */
When I run it, the rotor seems to move to a position which is not pie/2 or -pie/2 and then it just vibrates at whatever position it got to.
When I tried stepping through the code and added a breakpoint at
while(MC_GetAlignmentStatusMotor1()!=TC_ALIGNMENT_COMPLETED){} I see that the Alignment never completes and the value on the left doesn't change to 2.
So, this example doesn't work as expected as given in the application note.
I want to pass strings like 'JG,3.14,1000\r' which should move the rotor 180 degrees in 1 second (something like that) but I don't even see this happening with the Application Example shared by STM32.
Please help resolve.
