Servo Controller Fail State

20 Views Asked by At

Hi, I am relatively new to programming and the robotics world, so I apologize if this is trivial. I am trying to control a DXW90 Servo using a KY-040 standard Rotary Encoder. The microcontroller I am using is NUCLEO-L476RG.

Below is the current code I have scrapped together from a few tutorials I have watched. While the code returns no errors my Putty won't read anything past "Starting Up" and the encoder dose not control the servo at all. I feel like I need to write a fail statement, but am unsure where to begin.

I appreciate any time or advice anyone can provide me on my code, or on how to get into robotics in general.

int counter=0;
uint16_t PWMVal =0;
uint8_t dir;
uint8_t MSG[50] = {'\0'};
/* 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_USART1_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
sprintf(MSG, "Starting Up");

HAL_UART_Transmit(&huart1, MSG, sizeof(MSG), 100);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

/* USER CODE END 2 */

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

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
  if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET) //If the OUTA is RESET
  {
        if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_RESET)// If the OUTB is also RESET
      {
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_RESET){}; // Wait for OUTB to go high
        counter++;
        dir = "CW";
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET){}; // Wait for OUTA to go high
        HAL_Delay(10); //Wait for some time
      }

      if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_SET)// If the OUTB is also SET
      {
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_SET){}; // Wait for OUTB to go low
        counter--;
        dir = "CCW";
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET){}; // Wait for OUTA to go high
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_RESET){}; // Wait for OUTB to go high
        HAL_Delay(10); //Wait for some time
      }
 if (counter<0) counter = 0;
 if (counter>180) counter =180;
  }
  PWMVal = counter*55/10;
  htim1.Instance->CCR1 = 250 + PWMVal;
// NEED TO ADD A FAILED STATE SOMEWHERE EITHER AN ELSE STATEMENT OR SOMETHING OF THAT NATURE
 if(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_2))
 {
     sprintf(MSG, "Counter= %d\n\r", counter);
     HAL_UART_Transmit(&huart1, MSG, sizeof(MSG), 100);
 }
 HAL_Delay(100);

` }

0

There are 0 best solutions below