Video Rendering with 8-Bit YUV Formats

Video Rendering with 8-Bit YUV Formats

Gary Sullivan and Stephen Estrop Microsoft Digital Media Division

April 2002

Updated August 2003

Applies To:    Microsoft® Windows®, Microsoft DirectShow®

Summary: This article describes the 8-bit YUV formats that are recommended for video rendering in the Microsoft Windows operating system. This article presents techniques for converting between YUV and RGB formats, and also provides techniques for upsampling YUV formats. This article is intended for anyone working with YUV video decoding or rendering in Windows. (13 printed pages)

Introduction

Numerous YUV formats are defined throughout the video industry. This article identifies the 8-bit YUV formats that are recommended for video rendering in the Microsoft® Windows® operating system. Decoder vendors and display vendors are encouraged to support the formats described in this article. This article does not address other uses of YUV color, such as still photography.

The formats described in this article all use 8 bits per pixel location to encode the Y channel (also called the luma channel) and use 8 bits per sample to encode each U or V chroma sample. However, most YUV formats use fewer than 24 bits per pixel on average, because they contain fewer samples of U and V than of Y. This article does not cover YUV formats with 10-bit and 12-bit Y channels. 

Note   For the purposes of this article, the term U is equivalent to Cb, and the term V is equivalent to Cr.

This article covers the following topics:

Identifying YUV Formats in DirectShow

Each of the YUV formats described in this article has an assigned FOURCC code. A FOURCC code is a 32-bit unsigned integer that is created by concatenating four ASCII characters.

There are various C/C++ macros that make it easier to declare FOURCC values in source code. For example, the MAKEFOURCC macro is declared in Mmsystem.h, and the FCC macro is declared in Aviriff.h. Use them as follows:

DWORD fccYUY2 = MAKEFOURCC('Y','U','Y','2');
DWORD fccYUY2 = FCC('YUY2');

You can also declare a FOURCC code directly as a character literal simply by reversing the order of the characters. For example:

DWORD fccYUY2 = '2YUY';  // Declares the FOURCC 'YUY2'

Reversing the order is necessary because the Windows operating system uses a little-endian architecture. 'Y' = 0x59, 'U' = 0x55, and '2' = 0x32, so '2YUY' is 0x32595559.

In DirectShow, formats are identified by a major-type globally unique identifier (GUID) and a subtype GUID. The major type for computer video formats is always MEDIATYPE_Video. The subtype can be constructed by mapping the FOURCC code to a GUID, as follows:

XXXXXXXX-0000-0010-8000-00AA00389B71 

where XXXXXXXX is the FOURCC code. Thus, the subtype GUID for YUY2 is:

32595559-0000-0010-8000-00AA00389B71 

Many of these GUIDs are defined already in the header file Uuids.h. For example, the YUY2 subtype is defined as MEDIASUBTYPE_YUY2. The DirectShow base class library also provides a helper class, FOURCCMap, which can be used to convert FOURCC codes into GUID values. The FOURCCMap constructor takes a FOURCC code as an input parameter. You can then cast the FOURCCMap object to the corresponding GUID:

FOURCCMap fccMap(FCC('YUY2'));
GUID g1 = (GUID)fccMap;

// Equivalent:
GUID g2 = (GUID)FOURCCMap(FCC('YUY2'));
YUV Sampling

One of the advantages of YUV is that the chroma channels can have a lower sampling rate than the Y channel without a dramatic degradation of the perceptual quality. A notation called the A:B:C notation is used to describe how often U and V are sampled relative to Y:

  • 4:4:4 means no downsampling of the chroma channels.
  • 4:2:2 means 2:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every two U or V samples.
  • 4:2:0 means 2:1 horizontal downsampling, with 2:1 vertical downsampling.
  • 4:1:1 means 4:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every U or V sample. 4:1:1 sampling is less common than other formats, and is not discussed in detail in this article.

Figure 1 shows the sampling grid used in 4:4:4 pictures. Luma samples are represented by a cross, and chroma samples are represented by a circle.

Figure 1. YUV 4:4:4 sample positions

Figure 1. YUV 4:4:4 sample positions

The dominant form of 4:2:2 sampling is defined in ITU-R Recommendation BT.601. Figure 2 shows the sampling grid defined by this standard.

Figure 2. YUV 4:2:2 sample positions

Figure 2. YUV 4:2:2 sample positions

There are two common variants of 4:2:0 sampling. One of these is used in MPEG-2 video, and the other is used in MPEG-1 and in ITU-T recommendations H.261 and H.263. Figure 3 shows the sampling grid used in the MPEG-1 scheme, and Figure 4 shows the sampling grid used in the MPEG-2 scheme.

Figure 3. YUV 4:2:0 sample positions (MPEG-1 scheme)

Figure 3. YUV 4:2:0 sample positions (MPEG-1 scheme)

Figure 4. YUV 4:2:0 sample positions (MPEG-2 scheme)

Figure 4. YUV 4:2:0 sample positions (MPEG-2 scheme)

Compared with the MPEG-1 scheme, it is simpler to convert between the MPEG-2 scheme and the sampling grids defined for 4:2:2 and 4:4:4 formats. For this reason, the MPEG-2 scheme is preferred in Windows, and should be considered the default interpretation of 4:2:0 formats.

Surface Definitions

This section describes the 8-bit YUV formats that are recommended for video rendering. These fall into several categories:

First, you should be aware of the following concepts in order to understand what follows:

  • Surface origin. For the YUV formats described in this article, the origin (0,0) is always the upper-left corner of the surface.
  • Stride. The stride of a surface, sometimes called the pitch, is the width of the surface in bytes. Given a surface origin at the upper-left corner, the stride is always positive. 
  • Alignment. The alignment of a surface is at the discretion of the graphics display driver. The surface must always be DWORD aligned, that is, individual lines within the surface are guaranteed to originate on a 32-bit (DWORD) boundary. The alignment can be larger than 32 bits, however, depending on the needs of the hardware.
  • Packed format versus planar format. YUV formats are divided into packed formats and planar formats. In a packed format, the Y, U, and V components are stored in a single array. Pixels are organized into groups of macropixels, whose layout depends on the format. In a planar format, the Y, U, and V components are stored as three separate planes.
4:4:4 Formats, 32 Bits per Pixel

A single 4:4:4 format is recommended, with the FOURCC code AYUV. This is a packed format, where each pixel is encoded as four consecutive bytes, arranged in the following sequence.

Figure 5. AYUV memory layout

Figure 5. AYUV memory layout

The bytes marked A contain values for alpha.

4:2:2 Formats, 16 Bits per Pixel

Two 4:2:2 formats are supported, with the following FOURCC codes:

  • YUY2
  • UYVY

Both are packed formats, where each macropixel is two pixels encoded as four consecutive bytes. This results in horizontal downsampling of the chroma by a factor of two.

YUY2

In YUY2 format, the data can be treated as an array of unsigned char values, where the first byte contains the first Y sample, the second byte contains the first U (Cb) sample, the third byte contains the second Y sample, and the fourth byte contains the first V (Cr) sample, as shown in Figure 6.

Figure 6. YUY2 memory layout image

Figure 6. YUY2 memory layout

If the image is addressed as an array of two little-endian WORD values, the first WORD contains Y0 in the least significant bits (LSBs) and U in the most significant bits (MSBs). The second WORD contains Y1 in the LSBs and V in the MSBs.

YUY2 is the preferred 4:2:2 pixel format for Microsoft DirectX® Video Acceleration (DirectX VA). It is expected to be an intermediate-term requirement for DirectX VA accelerators supporting 4:2:2 video.

UYVY

This format is the same as YUY2, except the byte order is reversed — that is, the chroma and luma bytes are flipped (Figure 7). If the image is addressed as an array of two little-endian WORD values, the first WORD contains U in the LSBs and Y0 in the MSBs, and the second WORD contains V in the LSBs and Y1 in the MSBs.

Figure 7. UYVY memory layout

Figure 7. UYVY memory layout

4:2:0 Formats, 16 Bits per Pixel

Two 4:2:0 16-bits per pixel formats are recommended, with the following FOURCC codes:

  • IMC1
  • IMC3

Both FOURCC codes are planar formats. The chroma channels are subsampled by a factor of two in both the horizontal and vertical dimensions. 

IMC1

All of the Y samples appear first in memory as an array of unsigned char values. This is followed by all of the V (Cr) samples, and then all of the U (Cb) samples. The V and U planes have the same stride as the Y plane, resulting in unused areas of memory, as shown in Figure 8.

Figure 8. IMC1 memory layout

Figure 8. IMC1 memory layout

IMC3

This format is identical to IMC1, except the U and V planes are swapped:

Figure 9. IMC3 memory layout

Figure 9. IMC3 memory layout

4:2:0 Formats, 12 Bits per Pixel

Four 4:2:0 12-bpp formats are recommended, with the following FOURCC codes:

  • IMC2
  • IMC4
  • YV12
  • NV12

In all of these formats, the chroma channels are subsampled by a factor of two in both the horizontal and vertical dimensions. 

IMC2

This format is the same as IMC1 except that the V (Cr) and U (Cb) lines are interleaved at half-stride boundaries. In other words, each full-stride line in the chroma area starts with a line of V samples, followed by a line of U samples that begins at the next half-stride boundary (Figure 10). This layout makes more efficient use of address space than IMC1. It cuts the chroma address space in half, and thus the total address space by 25 percent. Among 4:2:0 formats, IMC2 is the second-most preferred format, after NV12.

 Figure 10. IMC2 memory layout

Figure 10. IMC2 memory layout

IMC4

This format is identical to IMC2, except the U (Cb) and V (Cr) lines are swapped:

Figure 11. IMC4 memory layout.

Figure 11. IMC4 memory layout

YV12

All of the Y samples appear first in memory as an array of unsigned char values. This array is followed immediately by all of the V (Cr) samples. The stride of the V plane is half the stride of the Y plane, and the V plane contains half as many lines as the Y plane. The V plane is followed immediately by all of the U (Cb) samples, with the same stride and number of lines as the V plane (Figure 12).

Figure 12. YV12 memory layout

Figure 12. YV12 memory layout

NV12

All of the Y samples are found first in memory as an array of unsigned char values with an even number of lines. The Y plane is followed immediately by an array of unsigned char values that contains packed U (Cb) and V (Cr) samples, as shown in Figure 13. When the combined U-V array is addressed as an array of little-endian WORD values, the LSBs contain the U values, and the MSBs contain the V values. NV12 is the preferred 4:2:0 pixel format for DirectX VA. It is expected to be an intermediate-term requirement for DirectX VA accelerators supporting 4:2:0 video.

Figure 13. NV12 memory layout

Figure 13. NV12 memory layout

Color Space and Chroma Sampling Rate Conversions

This section provides guidelines for converting between YUV and RGB, and for converting between some different YUV formats. We consider two RGB encoding schemes in this section: 8-bit computer RGB, also known as sRGB or "full-scale" RGB, and studio video RGB, or "RGB with head-room and toe-room." These are defined as follows:

  • Computer RGB uses 8 bits for each sample of red, green, and blue. Black is represented by R = G = B = 0, and white is represented by R = G = B = 255.
  • Studio video RGB uses some number of bits N for each sample of red, green, and blue, where N is 8 or more. Studio video RGB uses a different scaling factor than computer RGB, and it has an offset. Black is represented by R = G = B = 16*2N-8, and white is represented by R = G = B = 235*2N-8. However, actual values may fall outside this range.

Studio video RGB is the preferred RGB definition for video in Windows, while computer RGB is the preferred RGB definition for non-video applications. In either form of RGB, the chromaticity coordinates are as specified in ITU-R BT.709 for the definition of the RGB color primaries. The (x,y) coordinates of R, G, and B are (0.64, 0.33), (0.30, 0.60), and (0.15, 0.06), respectively. Reference white is D65 with coordinates (0.3127, 0.3290). Nominal gamma is 1/0.45 (approximately 2.2), with precise gamma defined in detail in ITU-R BT.709.

Conversion between RGB and 4:4:4 YUV

We first describe conversion between RGB and 4:4:4 YUV. To convert 4:2:0 or 4:2:2 YUV to RGB, we recommend converting the YUV data to 4:4:4 YUV, and then converting from 4:4:4 YUV to RGB. The AYUV format, which is a 4:4:4 format, uses 8 bits each for the Y, U, and V samples. YUV can also be defined using more than 8 bits per sample for some applications.

Two dominant YUV conversions from RGB have been defined for digital video. Both are based on the specification known as ITU-R Recommendation BT.709. The first conversion is the older YUV form defined for 50-Hz use in BT.709. It is the same as the relation specified in ITU-R Recommendation BT.601, also known by its older name, CCIR 601. It should be considered the preferred YUV format for standard-definition TV resolution (720 x 576) and lower-resolution video. It is characterized by the values of two constants Kr and Kb:

Kr = 0.299
Kb = 0.114

The second conversion is the newer YUV form defined for 60-Hz use in BT.709, and should be considered the preferred format for video resolutions above SDTV. It is characterized by different values for these two constants:

Kr = 0.2126
Kb = 0.0722

Conversion from RGB to YUV is defined by starting with the following:

L = Kr * R + Kb * B + (1 – Kr – Kb) * G

The YUV values are then obtained as follows:

Y =                 floor(2^(M-8) * (219*(L–Z)/S + 16) + 0.5)
U = clip3(0, 2^M-1, floor(2^(M-8) * (112*(B-L) / ((1-Kb)*S) + 128) + 0.5))
V = clip3(0, 2^M-1, floor(2^(M-8) * (112*(R-L) / ((1-Kr)*S) + 128) + 0.5))

where

  • M is the number of bits per YUV sample (M >= 8).
  • Z is the black-level variable. For computer RGB, Z equals 0. For studio video RGB, Z equals 16*2N-8, where N is the number of bits per RGB sample (N >= 8).
  • S is the scaling variable. For computer RGB, S equals 255. For studio video RGB, S equals 219*2N-8.

The function floor(x) returns the largest integer greater than or equal to x. The function clip3(x, y, z) is defined as follows:

clip3(x, y, z) = ((z < x) ? x : ((z > y) ? y : z))

The Y sample represents brightness, and the U and V samples represent the color deviations toward blue and red, respectively. The nominal range for Y is 16*2M-8 to 235*2M-8. Black is represented as 16*2M-8, and white is represented as 235*2M-8. The nominal range for U and V are 16*2M-8 to 240*2M-8, with the value 128*2M-8 representing neutral chroma. However, actual values may fall outside these ranges. 

For input data in the form of studio video RGB, the clip operation is necessary to keep the U and V values within the range 0 to 2M-1. If the input is computer RGB, the clip operation is not required, because the conversion formula cannot produce values outside of this range.

These are the exact formulas without approximation. Everything that follows in this document is derived from these formulas.

Example: Converting RGB888 to YUV 4:4:4

In the case of computer RGB input and 8-bit BT.601 YUV output, we believe that the formulas given in the previous section can be reasonably approximated by the following:

Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16
U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128
V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128

These formulas produce 8-bit results using coefficients that require no more than 8 bits of (unsigned) precision. Intermediate results will require up to 16 bits of precision.

Example: Converting 8-bit YUV to RGB888

From the original RGB-to-YUV formulas, one can derive the following relationships for the 8-bit BT.601 definition of YUV:

Y = round( 0.256788 * R + 0.504129 * G + 0.097906 * B) +  16 
U = round(-0.148223 * R - 0.290993 * G + 0.439216 * B) + 128
V = round( 0.439216 * R - 0.367788 * G - 0.071427 * B) + 128

Therefore, given:

C = Y - 16
D = U - 128
E = V - 128

the formulas to convert YUV to computer RGB can be derived as follows:

R = clip( round( 1.164383 * C                   + 1.596027 * E  ) )
G = clip( round( 1.164383 * C - (0.391762 * D) - (0.812968 * E) ) )
B = clip( round( 1.164383 * C +  2.017232 * D                   ) )

where clip() denotes clipping to a range of [0..255]. These formulas can be reasonably approximated by the following:

R = clip(( 298 * C           + 409 * E + 128) >> 8)
G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
B = clip(( 298 * C + 516 * D           + 128) >> 8)

These formulas use some coefficients that require more than 8 bits of precision to produce each 8-bit result, and intermediate results will require more than 16 bits of precision.

Converting 4:2:0 YUV to 4:2:2 YUV

Converting 4:2:0 YUV to 4:2:2 YUV requires vertical upconversion by a factor of two. This section describes an example method for performing the upconversion. The method assumes that the video pictures are progressive scan.

Note   The 4:2:0 to 4:2:2 interlaced scan conversion process presents atypical problems and is difficult to implement. This article does not address the issue of converting interlaced scan from 4:2:0 to 4:2:2.

Let each vertical line of input chroma samples be an array Cin[] that ranges from 0 to N - 1. The corresponding vertical line on the output image will be an array Cout[] that ranges from 0 to 2N - 1. To convert each vertical line, perform the following process:

Cout[0]     = Cin[0];
Cout[1]     = clip((9 * (Cin[0] + Cin[1]) – (Cin[0] + Cin[2]) + 8) >> 4);
Cout[2]     = Cin[1];
Cout[3]     = clip((9 * (Cin[1] + Cin[2]) - (Cin[0] + Cin[3]) + 8) >> 4);
Cout[4]     = Cin[2]
Cout[5]     = clip((9 * (Cin[2] + Cin[3]) - (Cin[1] + Cin[4]) + 8) >> 4);
...
Cout[2*i]   = Cin[i]
Cout[2*i+1] = clip((9 * (Cin[i] + Cin[i+1]) - (Cin[i-1] + Cin[i+2]) + 8) >> 4);
...
Cout[2*N-3] = clip((9 * (Cin[N-2] + Cin[N-1]) - (Cin[N-3] + Cin[N-1]) + 8) >> 4);
Cout[2*N-2] = Cin[N-1];
Cout[2*N-1] = clip((9 * (Cin[N-1] + Cin[N-1]) - (Cin[N-2] + Cin[N-1]) + 8) >> 4);

where clip() denotes clipping to a range of [0..255].

Note   The equations for handling the edges can be mathematically simplified. They are shown in this form to illustrate the clamping effect at the edges of the picture.

In effect, this method calculates each missing value by interpolating the curve over the four adjacent pixels, weighted toward the values of the two nearest pixels (Figure 14). The specific interpolation method used in this example generates missing samples at half-integer positions using a well-known method called Catmull-Rom interpolation, also known as cubic convolution interpolation.

Figure 14. 4:2:0 to 4:2:2 upsampling

Figure 14. 4:2:0 to 4:2:2 upsampling

In signal processing terms, the vertical upconversion should ideally include a phase shift compensation to account for the half-pixel vertical offset (relative to the output 4:2:2 sampling grid) between the locations of the 4:2:0 sample lines and the location of every other 4:2:2 sample line. However, introducing this offset would increase the amount of processing required to generate the samples, and make it impossible to reconstruct the original 4:2:0 samples from the upsampled 4:2:2 image. It would also make it impossible to decode video directly into 4:2:2 surfaces and then use those surfaces as reference pictures for decoding subsequent pictures in the stream. Therefore, the method provided here does not take into account the precise vertical alignment of the samples. Doing so is probably not visually harmful at reasonably high picture resolutions.

If you start with 4:2:0 video that uses the sampling grid defined in H.261, H.263, or MPEG-1 video, the phase of the output 4:2:2 chroma samples will also be shifted by a half-pixel horizontal offset relative to the spacing on the luma sampling grid (a quarter-pixel offset relative to the spacing of the 4:2:2 chroma sampling grid). However, the MPEG-2 form of 4:2:0 video is probably more commonly used on PCs and does not suffer from this problem. Moreover, the distinction is probably not visually harmful at reasonably high picture resolutions. Trying to correct for this problem would create the same sort of problems discussed for the vertical phase offset.

Converting 4:2:2 YUV to 4:4:4 YUV

Converting 4:2:2 YUV to 4:4:4 YUV requires horizontal upconversion by a factor of two. The method described previously for vertical upconversion can also be applied to horizontal upconversion. For MPEG-2 and ITU-R BT.601 video, this method will produce samples with the correct phase alignment.

Converting 4:2:0 YUV to 4:4:4 YUV

To convert 4:2:0 YUV to 4:4:4 YUV, you can simply follow the two methods described previously. Convert the 4:2:0 image to 4:2:2, and then convert the 4:2:2 image to 4:4:4. You can also switch the order of the two upconversion processes, as the order of operation does not really matter to the visual quality of the result.

<think> 我们正在处理的是工业自动化领域的EtherCAT总线通信代码,特别是用于伺服驱动器回零(Homing)操作的序列。 注意:之前引用的关于Pandas中axis的内容与当前问题无关,当前问题中的axis指的是伺服轴的编号(0表示第一个轴),而不是Pandas中的行/列方向。 下面逐行解释代码: 1. `INT axis`:声明一个整数类型的变量`axis`。 2. `axis=0`:将变量`axis`的值设置为0,表示操作0号轴(第一个轴)。 3. `ecin( ecgetoffset("Statusword",axis),StatusWord0)`: - `ecgetoffset("Statusword",axis)`:获取0号轴的状态字(Statusword)在EtherCAT通信帧中的内存偏移地址。 - `ecin`:建立输入映射,将驱动器状态字实时读取到变量`StatusWord0`中。这样,我们可以通过监控`StatusWord0`来获取驱动器的当前状态。 4. `ecout(ecgetoffset("Controlword",axis),ControlWord0)`: - `ecgetoffset("Controlword",axis)`:获取0号轴的控制字(Controlword)在EtherCAT通信帧中的内存偏移地址。 - `ecout`:建立输出映射,将变量`ControlWord0`的值实时输出到驱动器的控制字。这样,我们可以通过改变`ControlWord0`的值来控制驱动器。 5. `coewrite/1 (axis,0x6060,0,6)`: - `coewrite/1`:表示写一个16位(1个字)的SDO(Service Data Object)数据。 - 参数:`(axis,0x6060,0,6)` - `axis`:轴号(0) - `0x6060`:对象字典索引(Modes of Operation,操作模式) - `0`:子索引 - `6`:设置操作模式为6(回零模式,Homing mode) - 功能:将0号轴的操作模式设置为回零模式。 6. `I0=coeread/1 (axis,0x6061,0)`: - `coeread/1`:表示读取一个16位的SDO数据。 - 参数:`(axis,0x6061,0)` - `0x6061`:对象字典索引(Modes of Operation Display,显示当前操作模式) - `0`:子索引 - 功能:读取0号轴当前的操作模式,并赋值给变量`I0`(此处可能是为了验证模式设置是否成功)。 7. `coewrite/1 (axis,0x6098,0,1)`: - 写一个16位的SDO数据。 - 参数:`(axis,0x6098,0,1)` - `0x6098`:对象字典索引(Homing Method,回零方法) - `0`:子索引 - `1`:设置回零方法为方法1(具体方法根据驱动器手册定义,例如:通过负极限开关回零)。 - 功能:设置回零方法。 8. `!coewrite/4 (0,0x609A,0,4000)`: - 注释行(以`!`开头),表示该行被注释掉,不会执行。 - 如果执行,则`coewrite/4`表示写一个32位(4个字节)的SDO数据。 - 参数:`(0,0x609A,0,4000)` - `0`:轴号(这里写0,但注意上面设置的是特定轴,这里也应该是同一个轴,但参数顺序可能不同?) - `0x609A`:对象字典索引(Homing Acceleration,回零加速度) - `0`:子索引 - `4000`:设置的加速度值(单位可能为转/分²或脉冲/秒²,具体看驱动器参数定义)。 - 功能:设置回零加速度(但当前被注释,说明未使用)。 9. `ControlWord0=6`: - 设置控制字(ControlWord0)的值为6(二进制表示为`0000 0110`)。 - 在回零模式下,控制字的位定义如下: - 位0(最低位):切换为1(开启新指令) - 位1:保留(通常为0) - 位2:保留(通常为0) - 位3:保留(通常为0) - 位4:回零启动(0表示不启动,1表示启动)——这里位4为0(因为6的二进制位4为0) - 其他位... - 实际上,6(二进制`00000110`)通常用于切换到“使能操作”状态(根据CiA402状态机)。具体步骤: - 6(二进制`0000 0110`)表示“切换开启”(Shutdown)命令,将驱动器从“准备开启”状态切换到“开启禁用”状态(或类似状态转换,具体参考CiA402状态机)。 10. `TILL StatusWord0.0=1`: - `TILL`:类似于一个等待循环,直到条件满足才继续执行。 - `StatusWord0.0`:状态字(StatusWord0)的第0位(最低位)。 - 状态字的位定义(根据CiA402): - 位0:准备开启(Ready to switch on)——当该位为1时,表示驱动器已准备好开启(处于“准备开启”状态)。 - 功能:等待驱动器进入“准备开启”状态。 11. `ControlWord0=7`: - 设置控制字的值为7(二进制`0000 0111`)。 - 7(二进制`0000 0111`)表示“开启”(Switch on)命令,驱动器将进入“开启”状态。 12. `TILL StatusWord0.0=1 & StatusWord0.1=1`: - 等待状态字的第0位和第1位同时为1。 - 状态字位1:开启(Switched on)——表示驱动器已开启。 - 因此,条件表示等待驱动器进入“开启”状态(状态字位0和位1都为1)。 13. `ControlWord0=15`: - 设置控制字的值为15(二进制`0000 1111`)。 - 15(二进制`0000 1111`)表示“启用操作”(Enable Operation)命令,驱动器将进入“操作使能”状态。 14. `TILL StatusWord0.0=1 & StatusWord0.1=1 & StatusWord0.2=1`: - 等待状态字的第0、1、2位同时为1。 - 状态字位2:操作使能(Operation enabled)——表示驱动器处于操作使能状态。 - 条件表示驱动器已经进入“操作使能”状态(状态字位0、1、2都为1)。 15. `ControlWord0=31`: - 设置控制字的值为31(二进制`0001 1111`)。 - 注意:31的二进制为`0001 1111`,与15(`0000 1111`)相比,位4(从0开始数,第4位)为1。根据控制字的定义: - 位4:回零启动(Homing operation start)。当该位被置1时,启动回零操作。 - 功能:启动回零操作。 16. `TILL StatusWord0.12=1 & StatusWord0.10=1`: - 等待状态字的第12位和第10位同时为1。 - 状态字位10:回零完成(Homing attained)——表示回零操作已完成。 - 状态字位12:设定值使能(Target reached)——表示已到达目标位置(在回零中指的是零点位置)。 - 因此,条件表示回零操作已经完成且已经到达零点位置。 17. `ControlWord0=0`: - 设置控制字为0(二进制`0000 0000`)。这将导致驱动器退出操作使能状态,进入“关闭”状态。 18. `ECUNMAPIN(ecgetoffset("Statusword",axis))`:解除输入映射(状态字)。 19. `ECUNMAPOUT(ecgetoffset("Controlword",axis))`:解除输出映射(控制字)。 20. `wait 400`:等待400毫秒。 21. `coewrite/1 (axis,0x6060,0,8)`: - 写操作模式:设置0号轴的操作模式为8(循环同步位置模式,CSP)。 - 对象字典索引:`0x6060`(操作模式),设置值为8(CSP模式)。 22. `DISABLE axis`:软件禁用该轴(可能意味着在控制器中暂时禁用该轴的控制)。 23. `WAIT 500`:等待500毫秒。 24. `enable axis`:重新使能该轴。 25. `PTP axis,0`:执行点到点(PTP)运动,将轴移动到绝对位置0(这里回零后零点已经建立,所以回到零点位置)。 26. `STOP`:停止程序(可能是结束这个回零序列的意思,或者结束整个程序)。 总结:这段代码完成了0号轴的回零操作,具体步骤包括: 1. 设置操作模式为回零模式(Homing mode)。 2. 设置回零方法(方法1)。 3. 通过控制字逐步改变驱动器的状态(从准备开启->开启->操作使能)。 4. 启动回零操作,并等待回零完成。 5. 回零完成后,解除状态字和控制字的映射。 6. 切换操作模式为CSP模式(循环同步位置模式)。 7. 重新启用轴并移动到零点位置。 注意:回零操作的具体细节(如回零方法、加速度等)需要根据驱动器的对象字典和手册来设置。 相关问题: 1. 在EtherCAT通信中,状态字(Statusword)的每一位分别代表什么含义? 2. 控制字(Controlword)的位操作如何实现驱动器状态机的切换? 3. 回零模式(Homing mode)有哪些常见的方法?它们有什么区别? 4. 如何通过SDO(Service Data Object)设置驱动器的参数? 5. 在完成回零操作后,为什么要切换到CSP模式? </think> 以下是对代码的逐行详细解释,结合 EtherCAT 总线通信和伺服控制原理: ### 1. 轴变量声明 ```codesys INT axis axis=0 ``` - `INT axis`:声明整型变量 `axis` 表示伺服轴编号 - `axis=0`:指定操作 0 号轴(第一个物理伺服轴)[^1] ### 2. EtherCAT 输入/输出映射 ```codesys ecin( ecgetoffset("Statusword",axis),StatusWord0) ecout(ecgetoffset("Controlword",axis),ControlWord0) ``` - `ecgetoffset("Statusword",axis)`:获取状态字在 EtherCAT 帧中的内存偏移地址(对象字典 0x6040) - `ecin(...)`:建立 **实时输入映射**,将驱动器状态字同步到 `StatusWord0` 变量 - `ecout(...)`:建立 **实时输出映射**,将 `ControlWord0` 变量值发送到驱动器控制字(对象字典 0x6041) - 实现周期约 1ms 的实时数据交换[^2] ### 3. 设置回零模式 ```codesys coewrite/1 (axis,0x6060,0,6) !6 is Homing mode I0=coeread/1 (axis,0x6061,0) ``` - `coewrite/1`:通过 SDO 写入 16 位数据 - `0x6060`:操作模式对象字典索引 - `6`:设置为回零模式(Homing mode) - `coeread/1`:读取当前操作模式(0x6061)到 `I0` 验证设置 ### 4. 配置回零参数 ```codesys coewrite/1 (axis,0x6098,0,1) !Homing Method !coewrite/4 (0,0x609A,0,4000)!Home Acceleration (注释) ``` - `0x6098`:回零方法对象字典索引 - `1`:设置回零方法(如负限位开关触发) - 注释行:被禁用的回零加速度设置(0x609A) ### 5. 状态机转换序列 ```codesys ControlWord0=6 TILL StatusWord0.0=1 ``` - `6`(二进制 `0110`):发送 **Shutdown** 命令 - `TILL`:阻塞等待状态字位 0(准备开启)置 1 - 状态机:**准备开启** → **开启禁用**[^2] ```codesys ControlWord0=7 TILL StatusWord0.0=1 & StatusWord0.1=1 ``` - `7`(二进制 `0111`):发送 **Switch on** 命令 - 等待状态字位 0 和位 1(开启完成)同时置 1 - 状态机:**开启禁用** → **操作准备** ```codesys ControlWord0=15 TILL StatusWord0.0=1 & StatusWord0.1=1 & StatusWord0.2=1 ``` - `15`(二进制 `1111`):发送 **Enable operation** 命令 - 等待状态字位 0-2(操作使能)全部置 1 - 状态机:**操作准备** → **操作使能** ```codesys ControlWord0=31 TILL StatusWord0.12=1 & StatusWord0.10=1 ``` - `31`(二进制 `11111`):位 4=1 启动回零 - 等待状态字: -12(Target reached):到达目标位置 - 位 10(Homing attained):回零完成 - 完成机械零点校准 ### 6. 清理与模式切换 ```codesys ControlWord0=0 ! 禁用控制 ECUNMAPIN(ecgetoffset("Statusword",axis)) ECUNMAPOUT(ecgetoffset("Controlword",axis)) ``` - 解除 EtherCAT 输入/输出映射 - 释放通信资源 ```codesys wait 400 coewrite/1 (axis,0x6060,0,8) !CSP mode ``` - 等待 400ms 确保状态稳定 - `8`:切换为 **循环同步位置模式**(CSP) ### 7. 轴复位与归零 ```codesys DISABLE axis WAIT 500 enable axis PTP axis,0 ! 点对点移动到零点 STOP ``` - `DISABLE/enable`:重启伺服驱动器 - `PTP axis,0`:执行点到点运动到绝对位置 0(机械零点) - `STOP`:结束程序 ### 状态机转换流程 ```mermaid stateDiagram-v2 [*] --> 准备开启 准备开启 --> 开启禁用: Control=6 开启禁用 --> 操作准备: Control=7 操作准备 --> 操作使能: Control=15 操作使能 --> 回零中: Control=31 回零中 --> 回零完成: Status.10=1 回零完成 --> CSP模式 ``` ### 相关问题 1. 状态字(0x6040)中位 10(Homing attained)和位 12(Target reached)的具体区别是什么? 2. 为什么在回零操作前需要依次发送 6→7→15 的控制字序列? 3. 不同回零方法(Homing Method)的适用场景有何差异? 4. 循环同步位置模式(CSP)与回零模式在运动控制上的本质区别是什么? 5. 如何通过 EtherCAT 诊断工具监控此回零过程的实时数据? [^1]: `axis=0` 表示操作第一个伺服轴 [^2]: 遵循 CiA402 标准状态机转换流程
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值