STM32 single-line serial port control of bus servo

Publisher:VS821001Latest update time:2018-06-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction to bus servos
Bus servo servos are serial bus intelligent servos, which can actually be understood as derivatives of digital servos . Compared with analog servos , digital servos are a subversion of control system design, while bus servo servos are a subversion of servos in terms of function and application. The application of servos can actually only bring out a very small part of the functions of bus servo servos. This TS-315 controls the rotation through a single-line serial communication. The control string protocol is as follows:



String protocol:

#1P1500T100 Control the servo to rotate, no return

(1P is the servo with ID 1. The 1500 in the middle is a parameter with a range of 500-2500, which controls the servo range. The parameter 100 at the end indicates the time parameter of the servo rotation, that is, the speed. It means that the servo takes 100ms to reach the position of 1500 in the command from the current angle. The time range is 1-50000. The longer the time, the slower the speed. However, the maximum speed of the servo depends on the actual servo parameters.)

#1PRAD reads the angle, returns the format #001P1467  

#1PULK Release torque, no return

#1PID002 Change ID (change 1 to 2, the following parameter must be 3 digits, if not enough, add 0), return format #002P

#1PBD1 Returns#OK!

Change the baud rate (9600, 19200, 38400, 115200, 128000) (will take effect after the servo is restarted)

//1:9600, 2:19200, 3:38400, 4:57600, 5:115200, 6:128000

#1PCSD Set the current position as the initial position of the servo and return #OK! (After setting, the servo will rotate to the set position after power-on)

#1PCLE (restore factory settings) (will take effect only after the servo is restarted), return to #OK!

#1PMOD1 , return #OK!

(The last parameter 1 indicates the working mode, 0: 270 degrees reverse, 1: 270 degrees forward, 2: 180 degrees reverse, 3: 180 degrees forward, 4: continuous rotation)

Reverse and forward indicate the rotation direction of the servo. Reverse: The pulse signal is from 500 to 2500, and the servo rotates counterclockwise. Forward: The pulse signal is from 500 to 2500, and the servo rotates clockwise.

The continuous rotation mode is a normal 360-degree servo that rotates continuously and cannot control the angle. In this mode, the function of the command #1P1500T100 to control the servo is changed. The function of this command in this mode is:

The middle parameter 1500 has a range of 500-2500, where 500-1500 controls the forward rotation of the servo, and 1500-2500 controls the reverse rotation of the servo. The closer to 1500, the slower the speed, and the farther away from 1500, the faster the speed (that is, 500 and 2500 are the fastest speeds, and 1500 is the stop)

The last parameter 100 indicates the number of revolutions of the servo. 100 means 100 revolutions (there is an error of one or two revolutions, which is normal!). If this parameter is changed to 50000, it will rotate infinitely. If the parameter is 49999, it will only rotate 49999 revolutions.

The 1P above indicates the servo numbered 1. All the above commands must end with a carriage return and line feed (0x0d and 0x0a)

The default ID of the servo is 1. The ID 255 is broadcast, and the broadcast command is valid for all servos. The default baud rate is 128000


2 STM32 single-wire serial communication

The reference manual describes it this way, very simply. Single-wire half-duplex mode is selected by setting the HDSEL bit in the USART_CR3 register. In this mode, the following bits must remain clear:
● LINEN and CLKEN bits in the USART_CR2 register
● SCEN and IREN bits in the USART_CR3 register
The USART can be configured to follow the single-wire half-duplex protocol. In single-wire half-duplex mode, the TX and RX pins are interconnected internally on the chip. Half-duplex and full-duplex communication are selected using the control bit "HALF DUPLEX SEL" (HDSEL bit in USART_CR3).
When HDSEL is '1'
● RX is no longer used
● TX is always released when no data is being transmitted. Therefore, it behaves as a standard I/O port in the idle state or in the receiving state. This means that this I/O must be configured as a floating input (or an open-drain output high) when it is not driven by the USART.
Apart from this, communication is similar to the normal USART mode. Conflicts on the line are managed by software (for example by using a central arbitrator). In particular, transmission is never blocked by hardware. When the TE bit is set, transmission continues as soon as data is written to the data register.

The reference initialization source code is as follows

// Initialize IO serial port 2

//pclk2 CLK2 clock frequency (Mhz)

//bound: baud rate

void uart2_init(u32 pclk2,u32 bound)

{         

       

        u32 temp;          

        temp=(pclk2*1000000+bound/2)/bound; //Get USARTDIV@OVER8=0, use rounding calculation

       


        RCC->AHB1ENR|=1<<0; //Enable PORTA clock  

        RCC->APB1ENR|=1<<17; //Enable serial port 2 clock

        GPIO_Set(GPIOA,PIN2|PIN3,GPIO_MODE_AF,GPIO_OTYPE_OD,GPIO_SPEED_50M,GPIO_PUPD_PU); //PA2,PA3, multiplexing function, pull-up output

        GPIO_AF_Set(GPIOA,2,7); //PA2,AF7

        GPIO_AF_Set(GPIOA,3,7);//PA3,AF7            

        USART2->CR1=0; //Clear CR1 register

        USART2->BRR=temp/2; //Baud rate setting @OVER8=0

       

//The following 5 lines of code are to set the registers according to the reference manual. Note that enabling HDSEL is placed at the end.

        USART2->CR3&=0<<5; //Clear SCEN

        USART2->CR3&=0<<1; //Clear IREN

        USART2->CR2&=0<<11; //Clear CLKEN

        USART2->CR2&=0<<14; //Clear LINEN

        USART2->CR3|=1<<3; //Enable HDSEL

       

       

       

        USART2->CR1|=0<<28; //Set M1=0

        USART2->CR1|=0<<12; //Set M0=0&M1=0, select 8-bit word length

        USART2->CR1|=0<<15; //Set OVER8=0, 16 times oversampling

        USART2->CR1|=1<<3; //Serial port transmission enable

#if EN_USART2_RX //If reception is enabled

        // Enable receive interrupt

        USART2->CR1|=1<<2; //Serial port reception enable

        USART2->CR1|=1<<5; //Receive buffer not empty interrupt enable                   

        MY_NVIC_Init(0,0,USART2_IRQn,2); //Group 2, lowest priority

#endif

        USART2->CR1|=1<<0; //Serial port enable

       


}


3. Notes on data reading

Through the analysis of the logic analyzer, after the command was transmitted to the servo, the servo gave the correct feedback. However, since the speed of the servo's feedback data was very fast, it caused the problem of packet loss. The data could not be correctly transmitted to the MCU. Because in the single-line mode, RXD always received the data sent by TXD, which caused the packet loss of the data that was really needed. Therefore, it only needed to modify the program to correct it. The modified program only judged whether it was the command sent by itself. If so, it would not receive it. Finally, it was verified that the feedback value of the servo could be read correctly.


u16 USART2_RX_STA=0; //Receive status flag          


void USART2_IRQHandler(void)

{

        u8 res;       

#if SYSTEM_SUPPORT_OS //If SYSTEM_SUPPORT_OS is true, OS support is required.

        OSIntEnter();   

#endif

        if(USART2->ISR&(1<<5))//Receive data

        {       

                res=USART2->RDR;

                //if(USART2->RDR!=USART2->TDR)

                {

                       

                        if((USART2_RX_STA&0x8000)==0)//receiving is not completed

                        {

                                if(USART2_RX_STA&0x4000) //Received 0x0d

                                {

                                        if(res!=0x0a)USART2_RX_STA=0; //Receive error, restart       

                                        else if (USART2_RX_BUF[5]==0x44)USART2_RX_STA=0; //Prevent packet loss and prohibit receiving the name sent by yourself                               

                                        else

                                        {

                                                USART2_RX_STA|=0x8000; //Receiving completed

                                        }

                                }

                                else //Has not received 0X0D

                                {       

                                        if(res==0x0d)USART2_RX_STA|=0x4000;

                                        else

                                        {

                                                USART2_RX_BUF[USART2_RX_STA&0X3FFF]=res;

                                                USART2_RX_STA++;

                                                if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0; //Receive data error, restart receiving          

                                        }                 

                                }

                        }  

                }               

        }

  • Logic Analyzer Data Capture

    Logic Analyzer Data Capture




  • QQ screenshot 20170307160119.jpg


Keywords:STM32 Reference address:STM32 single-line serial port control of bus servo

Previous article:Realization of Parallel Communication between ARM9 and FPGA
Next article:STM32 bus idle + DMA mode reception

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号