cancel
Showing results for 
Search instead for 
Did you mean: 

UART not listening for transmission after function executing

MaciejG
Associate II

Hi I encountered problem while working on project. I am trying to establish connection between devices using RS485 interface. Slave device has been designed to control servo motors, positions to be set are sent from the master. Everything worked fine until I added slower_move function for slowing initial movement of the motors. Function is called upon request of calibration or starting movement. Calibration works fine with this function but when I try to start movement servos are going to desired position and then stop. It looks like HAL_UARTEx_ReceiveToIdle_IT is not executed or somehow function is entering infinite loop. When I tried debugging slave code execution RX_DATA buffer is not updated after calling initial movement. Also motors start to move after restarting slave device which again indicates either infinite loop or not executed HAL_UARTEx_ReceiveToIdle_IT. I've checked data sent from master and it looks good.
Master device is controlled with external PC using bluetooth module HC-05. RS485 connection is implemented using MAX3485 module.
Thanks in advance for any ideas. Here are slave's and master's code:

 

 

/*SLAVE CODE*/

#include "main.h"

TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
uint8_t SLAVE_ID = 4;
uint8_t STARTUP_ID = 44;
uint8_t CALIBRATE_ID = 20;
uint8_t TX_DATA[4];
uint8_t RX_DATA[4];
uint8_t STARTUP_REQUEST = 0;
uint8_t DESIRED_HOR;
uint8_t DESIRED_VER;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void sendData(uint8_t *data)
{
	HAL_GPIO_WritePin(TX_EN_GPIO_Port, TX_EN_Pin, GPIO_PIN_SET);
	HAL_UART_Transmit(&huart1, data, 4, 1000);
	HAL_GPIO_WritePin(TX_EN_GPIO_Port, TX_EN_Pin, GPIO_PIN_RESET);
}

uint8_t crc8(uint8_t *buffer, uint8_t buff_len)
{
	uint8_t crc = 0xFF;
	while (buff_len--)
	{
		crc = crc ^ (*buffer++);
	}
	return crc;
}

void prepare_data()
{
	TX_DATA[0] = RX_DATA[0];
	TX_DATA[1] = RX_DATA[1];
	TX_DATA[2] = RX_DATA[2];
	TX_DATA[3] = crc8(RX_DATA, 3);
}

void slower_move(uint8_t desired_pos_hor, uint8_t desired_pos_ver)
{
	int add_hor = (desired_pos_hor > htim3.Instance->CCR1)? 1 : -1;
	int add_ver = (desired_pos_ver > htim3.Instance->CCR3)? 1 : -1;

	while (1)
	{
		if (desired_pos_hor != htim3.Instance->CCR1)
			htim3.Instance->CCR1+=add_hor;
		if (desired_pos_ver != htim3.Instance->CCR3)
			htim3.Instance->CCR3+=add_ver;
		if (desired_pos_hor == htim3.Instance->CCR1 && desired_pos_ver == htim3.Instance->CCR3)
			break;
		HAL_Delay(5);
	}
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (RX_DATA[0]==SLAVE_ID)
	{
		htim3.Instance->CCR1 = RX_DATA[1];
		htim3.Instance->CCR3 = RX_DATA[2];
		prepare_data();
		sendData(TX_DATA);
		HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_DATA, 4);
	}
	else if (RX_DATA[0]==CALIBRATE_ID)
	{
		DESIRED_HOR = RX_DATA[1];
		DESIRED_VER = RX_DATA[2];
		STARTUP_REQUEST = 1;
	}
	else if (RX_DATA[0]==STARTUP_ID)
	{
		htim3.Instance->CCR1 = 150;
		htim3.Instance->CCR3 = 150;
		DESIRED_HOR = RX_DATA[1];
		DESIRED_VER = RX_DATA[2];
		STARTUP_REQUEST = 1;
	}
	else
		HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_DATA, 4);
}




/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* 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_TIM3_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
  HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_DATA, 4);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  if (STARTUP_REQUEST)
	  {
		  slower_move(DESIRED_HOR, DESIRED_VER);
		  HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_DATA, 4);
		  STARTUP_REQUEST = 0;
	  }
    /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */
}
/**********************************************************/
/*MASTER's CODE */

#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <math.h>

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
//values to count servo positions
uint8_t PTS_NUM = 45;
uint8_t ZERO_SERVO = 150;
uint8_t MODULES_AMOUNT = 9;
uint8_t HOR_MAX_VAL = 180;
uint8_t VER_MAX_VAL = 166;
double HOR_PERIOD = 2 * M_PI;
double VER_PERIOD = 4 * M_PI;
double HOR_OFFSET = 0;
double VER_OFFSET = (-1.0/2.0) * M_PI;
//data buffers for communication
uint8_t RX_DATA[4];
uint8_t TX_DATA[4];
uint8_t BLU_BUFF;
uint8_t CALIBRATE_ID = 20;
uint8_t STARTUP = 1;
//enumerators defining state and errors in robot
typedef enum {
	NO_ERROR = 0,
	CRC_ERROR = 1,
	TIMEOUT_ERROR = 2
} error_code;

typedef enum {
	STOPPED = 0,
	MOVING = 1,
	CALIBRATE = 2
} robot_state;

uint8_t ERROR_VAL = NO_ERROR;
uint8_t STATE = STOPPED;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void sendData(const uint8_t *data)
{
	HAL_GPIO_WritePin(TX_EN_GPIO_Port,TX_EN_Pin, GPIO_PIN_SET);
	HAL_UART_Transmit(&huart1, data, 4, 1000);
	HAL_GPIO_WritePin(TX_EN_GPIO_Port,TX_EN_Pin, GPIO_PIN_RESET);
}

void sendBluetooth(const uint8_t *data)
{
	HAL_UART_Transmit(&huart2, data, 1, 1000);
}

void handle_bluetooth(const uint8_t *data)
{
	switch (*data)
	{
	case 2:
		STATE=MOVING;
		break;
	case 3:
		STATE=CALIBRATE;
		break;
	case 4:
		STATE=STOPPED;
		break;
	case 5:
		STATE=STOPPED;
		break;
	default:
		break;
	}
	sendBluetooth(data);
}

void check_crc(const uint8_t *rx_buff, const uint8_t *tx_buff)
{
	if (rx_buff[3] != tx_buff[3])
	{
		uint8_t err;
		ERROR_VAL = CRC_ERROR;
		err = ERROR_VAL * 10 + tx_buff[0];
		sendBluetooth(&err);
	}
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart->Instance==USART2)
	{
		handle_bluetooth(&BLU_BUFF);
		HAL_UARTEx_ReceiveToIdle_IT(huart, &BLU_BUFF, 1);
	}
	else if (huart->Instance==USART1)
	{
		check_crc(RX_DATA, TX_DATA);
		HAL_UARTEx_ReceiveToIdle_IT(huart, RX_DATA, 4);
	}
}



uint8_t crc8(uint8_t *buffer, uint8_t buff_len)
{
	uint8_t crc = 0xFF;
	while (buff_len--)
	{
		crc = crc ^ (*buffer++);
	}
	return crc;
}

//return 1 if there is no error in calc and 0 if there is

void preparePackets(uint8_t *buffer, const uint8_t slave_id, const uint8_t servo_hor, const uint8_t servo_ver)
{
	buffer[0] = slave_id;
	buffer[1] = servo_hor;
	buffer[2] = servo_ver;
	buffer[3] = crc8(buffer, 3);
}

uint8_t map(double sin_val, const uint8_t max_val)
{
	double multiplier = max_val - ZERO_SERVO;
	uint8_t mapped_val = sin_val * multiplier + ZERO_SERVO;
	return mapped_val;
}

void init_sin_values(uint8_t* sin_arr, const uint8_t max_val, const double period, const double offset)
{
	double alfa = period / (double)PTS_NUM;
	double curr_sin;
	for (int i = 0; i < PTS_NUM; i++)
	{
		curr_sin = sin(offset + i * alfa);
		*sin_arr++ = map(curr_sin, max_val);
	}
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* 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 */
  HAL_Delay(100);
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UARTEx_ReceiveToIdle_IT(&huart2, &BLU_BUFF, 1);
  HAL_UARTEx_ReceiveToIdle_IT(&huart1, RX_DATA, 4);
  uint8_t count_modules = 1;
  uint8_t current_point = 0;
  uint8_t SinArrHor[PTS_NUM];
  uint8_t SinArrVer[PTS_NUM];
  init_sin_values(SinArrHor, HOR_MAX_VAL, HOR_PERIOD, HOR_OFFSET);
  init_sin_values(SinArrVer, VER_MAX_VAL, VER_PERIOD, VER_OFFSET);
  uint8_t index_module = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	  for (; count_modules <= MODULES_AMOUNT && STATE==MOVING; count_modules++)
	  {
		  index_module = ((PTS_NUM/MODULES_AMOUNT) * (count_modules - 1) + current_point) % PTS_NUM;

		  if (STARTUP)
			  preparePackets(TX_DATA, count_modules+40, SinArrHor[index_module], SinArrVer[index_module]);
		  else
			  preparePackets(TX_DATA, count_modules, SinArrHor[index_module], SinArrVer[index_module]);

		  sendData(TX_DATA);
	  	  HAL_Delay(5);
	  }
	  if (STARTUP && STATE==MOVING)
	  {
		  HAL_Delay(100);
		  STARTUP = 0;
	  }
	  if (STATE==MOVING)
	  {
		  current_point++;
		  current_point = current_point % PTS_NUM;
		  if (count_modules > MODULES_AMOUNT)
			  count_modules = 1;
  	  }
	  else if (STATE==CALIBRATE)
	  {
		  HAL_Delay(5);
		  preparePackets(TX_DATA, CALIBRATE_ID, 150, 150);
		  sendData(TX_DATA);
		  count_modules = 1;
		  current_point = 0;
		  STATE=STOPPED;
		  STARTUP = 1;
	  }
	  else if (STATE==STOPPED)
	  {
		  continue;
	  }
  }
  /* USER CODE END 3 */
}

 

 

 

0 REPLIES 0