cancel
Showing results for 
Search instead for 
Did you mean: 

UART Commands to control B-G431B-ESC1

garivin
Visitor

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.

 

6 REPLIES 6
Karl Yamashita
Principal

Use the debugger to view the rpm value before you pass the argument to MC_ProgramSpeedRampMotor1.

 

 

 

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.

This way should I view the rpm value?

void process_command(char *cmd)
{
if (strncmp(cmd, "SPD", 3) == 0)
{
int rpm = atoi(&cmd[3]); // Set breakpoint here

// Debug line (optional if you use printf)
printf("Parsed RPM: %d\n", rpm); // Requires a working printf() setup

// Or use debugger: watch the value of rpm here

if (MC_GetMotorState(0) == RUN)
{
MC_ProgramSpeedRampMotor1(rpm, 1000);
}
}
else if (strcmp(cmd, "START") == 0)
{
MC_StartMotor1();
}
else if (strcmp(cmd, "STOP") == 0)
{
MC_StopMotor1();
}
}

 

And the code of the transmiter looks good to you? The way of sendingthe speed I mean

Hello @garivin,

What is your version of the MCSDK used to generate ESC code?

If I understood correctly, you want to implement your own communication mechanism through the UART right?

 

Hello @cedric H ,

I am using the las version of MCSDK_v6.4.0.

I have a cubeide main.c code in a computer with a NUCLEO-F446ZE board connected via ST-Link, and this NUCLEO board is connected using UART (tz and rx crossed and gnd) with the B-G431B-ESC1, which is connected via ST-Link to another computer with the main.c code of the project generated with MC Workbench in CubeIDE. So i have two computers with a microcontroler in each one with the own main.c in each one.

From the Nucleo I want to send a speed signal via UART for the ESC1 board to read it and move the motor MAXON 251601 that is already well connected to the ESC1. I think that the problem that I have is the way of sending the signal "SPD1500\n", but i dont know the input that the motor should receive.

 

If anything is not clear I will be happy to respond.

 


I think that the problem that I have is the way of sending the signal "SPD1500\n", but i dont know the input that the motor should receive.

So, you're just sending some arbitrary number like 1500 to the function MC_ProgramSpeedRampMotor1 hoping that it'll change the motor speed?

I don't know what argument that MC_ProgramSpeedRampMotor1 needs because you didn't show that part of the code, so only you can see what kind of value the function needs.

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.

UART is the peripheral we use to connect the Motor control projects with the MC Pilot, but to do so, we implement a full protocol called MCP. So my question is, do you plan to reuse this protocol (which is fully documented in the User manual) or do you just plan to develop your own way to send command speed?

Alternatively, ESC speed can be also controlled with a PWM command signal that you can generate with your NUCLEO-F446.