Get Encoder

5 years 5 months ago #29530360 by tako
Get Encoder was created by tako
Hi all,
I am writing a function to return the encoder values but dont know why it does not work. Anyhelp?
Thanks.

int Get_Encoder_L(void)
{

int Return_Encoder_L=0;
int i=0;
uint8_t tx_motor_L[5]; // data transmit to motor controller
uint8_t rx_motor_L[10]={0};
tx_motor_L[0]='?';
tx_motor_L[1]='C';
tx_motor_L[2]=' ';
tx_motor_L[3]='1';
tx_motor_L[4]='\r';

HAL_UART_Transmit(&huart3, tx_motor_L,5, 20);

for( i=0;i<10;i++) // received C=???????\r
{

HAL_UART_Receive(&huart3, &rx_motor_L,1, 1);

if(rx_motor_L=='\r')
break;
}
//HAL_UART_Receive(&roboteq_huart, rx_motor_R,8, 20);


if (rx_motor_L[2]=='-')
{
Return_Encoder_L=rx_motor_L[3]-48;
for(int j=4;j<i;j++)
Return_Encoder_L= Return_Encoder_L*10+rx_motor_L[j]-48;
}
else
{
Return_Encoder_L=rx_motor_L[2]-48;
for(int j=3;j<i;j++)
Return_Encoder_L= Return_Encoder_L*10+rx_motor_L[j]-48;
}


// Load Home Counter
tx_motor_L[0]='!';
tx_motor_L[1]='H';
tx_motor_L[2]=' ';
tx_motor_L[3]='1';
tx_motor_L[4]='\r';

HAL_UART_Transmit(&huart3, tx_motor_L,5, 20);


return Return_Encoder_L;
}

Please Log in or Create an account to join the conversation.

Moderators: tonysantoni
Time to create page: 0.261 seconds