cancel
Showing results for 
Search instead for 
Did you mean: 

UART Commands to control B-G431B-ESC1

garivin
Associate II

I am currently working or a project with microcontrollers STM32 and this is my current situation:

- I want to send from my NUCLEO f4476ZE the desired speed via UART to my other controller which is B-G431B-ESC1 discovery kit with STM32G431CB MCU.

- I have this discovery kit connected to the motor (I can make it run with the CubeIDE project generated from the Motor Control Workbench that i have created with this Discovery kit and a Maxon 251601 motor).

 

The problem that I have is that i don't know which structure of message should I send from the Nucleo to the ESC1 for the ESC1 to read and interpret it correctly to move the motor at the desired speed of the message.

I was trying sending this as desired speed:

char tx_buff[32];
int velocidad = 1500;

while (1)
{
  sprintf(tx_buff, "SPD%d\n", velocidad);
  HAL_UART_Transmit(&huart2, (uint8_t*)tx_buff, strlen(tx_buff), 1000);
  HAL_Delay(1000);
}

And for the receiver I wanted to add this: 

HAL_UART_Receive_IT(&huart2, &rx_buf[rx_index], 1); //after MX_USART2_UART_Init();
#include <string.h>
#include <stdlib.h>

void process_command(char *cmd)
{
  if (strncmp(cmd, "SPD", 3) == 0)
  {
    int rpm = atoi(&cmd[3]);
    MC_ProgramSpeedRampMotor1(rpm, 1000);  // Cambia velocidad con rampa de 1s
  }
  else if (strcmp(cmd, "START") == 0)
  {
    MC_StartMotor1();
  }
  else if (strcmp(cmd, "STOP") == 0)
  {
    MC_StopMotor1();
  }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == USART2)
  {
    if (rx_buf[rx_index] == '\n')
    {
      rx_buf[rx_index] = '\0';
      process_command((char*)rx_buf);
      rx_index = 0;
    }
    else
    {
      if (rx_index < RX_BUF_LEN - 1)
        rx_index++;
      else
        rx_index = 0;
    }

    HAL_UART_Receive_IT(&huart2, &rx_buf[rx_index], 1);
  }
}




The problem is that the motor is not moving at the desired speed that i tell him, but with the osciloscope i can see that there is a signal in the tx,rx,ground that i have connected correctly between the two boards, as i did the getting started tutorial from uart and the receiver received the messages from the trasmiter.
I think that the problem that i have is that it can not read correctly the velocity. Can somebody help me to send the desired speed to the motor for it to run at that speed?
Even if the code does not have anything to do with mine, I may have written it incorrectly.

 

20 REPLIES 20
Karl Yamashita
Principal

Look at my signature. It has a link to Git project for setting up UART in DMA mode with idle detection to accept any length string. 

This will queue your START, STOP and SPD as individual messages correctly.

 

 

I was told that if a devices starts to smoke, put the smoke back in. I guess I never got all the smoke because the device never worked afterwards.
Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.