The invention generally relates to a method of calculating an arctangent, and more specifically to the pre-processing and post-processing of data for fast arctangent calculations.
Vehicle controllers often need to calculate an arctangent during control of various vehicle systems. For example, a vehicle controller may need to calculate an arctangent in order to properly control an electric motor of a hybrid vehicle.
An arctangent may only be calculated over a 45° segment of a 360° degree circle. Accordingly, any x-value input and y-value input used to calculate an arctangent value must be adjusted from an initial octant (i.e., an initial 45° segment) to the segment of the circle corresponding to the 45° segment in which the arctangent may be calculated. After the numerical value of the arctangent is calculated, it must be adjusted back into the initial octant of the circle.
A method of calculating an arctangent for a point located on a plane by a y-value and an x-value of a Cartesian coordinate system is provided. The method includes coding a y-bit of an index to indicate a sign of the y-value, and coding an x-bit of the index to indicate a sign of the x-value. A sign bit of a binary value representing the y-value is removed to define a cleared y-value, and a sign bit of a binary value representing the x-value is removed to define a cleared x-value. When the cleared x-value is equal to or less than the cleared y-value, an arctangent calculation input argument is calculated by dividing the cleared x-value by the cleared y-value. The arctangent of the input argument is then calculated to define a calculated arctangent output value. When the cleared x-value is greater than the cleared y-value, the cleared x-value is swapped for the cleared y-value to define a swapped x-value, and the cleared y-value is swapped for the cleared x-value to define a swapped y-value. A swap bit of the index is coded to indicate that the cleared x-value and the cleared y-value were swapped to define the swapped x-value and the swapped y-value respectively. When the cleared x-value is greater than the cleared y-value, an arctangent calculation input argument is calculated by dividing the swapped x-value by the swapped y-value. The arctangent of the input argument is then calculated to define a calculated arctangent output value. A segment base angle is looked up from an angle adjustment table, based on the coded x-bit, the coded y-bit, and the coded swap bit of the index. A numerical sign is extracted from a reverse bit coded into the extracted segment base angle. The numerical sign of the reverse bit from the segment base angle is applied to the calculated arctangent output value to define a corrected arctangent value, and the reverse bit of the segment base angle is cleared to define a cleared segment base angle. The corrected arctangent value is added to the cleared segment base angle to define the arctangent of the point.
Accordingly, the three Boolean bits that are coded into the index, i.e., the y-bit, the x-bit, and the swap bit, are used to fetch an encoded value from a look-up table, i.e., the angle adjustment table, which is used to reverse and adjust the calculated numerical output from the arctangent calculation. This minimizes pre-processing and post-processing time associated with the arctangent calculation, thereby improving processing speed.
The above features and advantages and other features and advantages of the present invention are readily apparent from the following detailed description of the best modes for carrying out the invention when taken in connection with the accompanying drawings.
Those having ordinary skill in the art will recognize that terms such as “above,” “below,” “upward,” “downward,” “top,” “bottom,” etc., are used descriptively for the figures, and do not represent limitations on the scope of the invention, as defined by the appended claims. Furthermore, the invention may be described herein in terms of functional and/or logical block components and/or various processing steps. It should be realized that such block components may be comprised of any number of hardware, software, and/or firmware components configured to perform the specified functions.
Referring to the Figures, wherein like numerals indicate like parts throughout the several views, a method of calculating an arctangent for a point is provided. The method may be performed by a controller, such as but not limited to a computer or control module. The controller is operable to perform the various processes necessary to calculate the arctangent in accordance with the method described below. The controller includes all software, hardware, memory, processor, algorithms, coding, etc. necessary to calculate the arctangent as described below.
Referring to
A first point 40 is shown in the first octant. The arctangent of the first point 40 is calculated in Example 1 below. A second point 42 is shown in the sixth octant. The arctangent of the second point 42 is calculated in Example 2 below.
The method includes establishing an index in the memory of the controller. The index is a 3-bit binary value, which is used to save Boolean information of three different input parameters (the y-value, the x-value, and a swap bit). The index is later used to retrieve a value from a look-up table, which is used to reverse and/or adjust a calculated arctangent output value. Although the index only uses 3-bits to save the Boolean information of the input parameters, the index is encoded in the number of bits used by the controller, such as but not limited to a 32-bit, IEEE-754 standard floating point number format, shown and described herein. These three bits of the index include a y-bit, an x-bit, and the swap bit. For example, the index may be represented as syx00, wherein “s” represents the swap bit, “y” represents the y-bit, and “x” represents the x-bit. The x-bit, the y-bit, and the swap bit are pre-shifted for use as a table look-up index for a table containing 32-bit data. Preferably, and as represented above, the x-bit, the y-bit, and the swap bit are coded into a second bit, a third bit, and a fourth bit of the index, respectively.
As is known, a single precision floating point number is 32-bits in size. In most processors, the fundamental unit of memory is 8 bits (1 byte). In most processors, which use a 32-bit system, the 32-bit value takes up 4 bytes of memory, so in order to point to one contiguous group of a 32-bit number, the address must increment in steps of 4. Pre-shifting the index left by two bits multiplies it by 4. Since the bits are inserted into the index individually, the shift is free, i.e., does not require processing time. If the index were right justified, i.e., not pre-shifted, then the processor/controller would have to shift the index left by two bits before using the index to reference a look-up table. Accordingly, by pre-shifting the index, the speed of the look-up is increased.
In order to calculate the arctangent for any given point, the sign of the y-value is coded into the y-bit of the index, which is used to indicate the sign of the y-value. The sign is either a positive (+) sign or a negative (−) sign. As is generally appreciated, a positive sign is generally represented in binary code by a “0”, and a negative sign is generally represented in binary code by a “1”. Accordingly, if the y-value includes a negative sign, i.e., if the y-value is a negative number, the y-bit may be coded with a “1”, whereas if the y-value includes a positive number, i.e., if the y-value is a positive number, the y-bit may be coded with a “0”. The sign of the x-value is coded into the x-bit of the index, which is used to indicate the sign of the x-value. The sign is either a positive (+) sign or a negative (−) sign. Accordingly, if the x-value includes a negative sign, i.e., if the x-value is a negative number, the x-bit may be coded with a “1”, whereas if the x-value includes a positive number, i.e., if the x-value is a positive number, the x-bit may be coded with a “0”.
Once the signs of the y-value and the x-value of the point have been coded into the y-bit and the x-bit of the index, the signs are cleared or masked from the x-value and the y-value to define a cleared x-value and a cleared y-value respectively. The cleared x-value and the cleared y-value effectively define an absolute value of the x-value and the y-value respectively. The signs of the y-value and the x-value are cleared or masked using integer operations, instead of floating point operations. Integer operations include mathematical and logical operations that are performed on integers, as understood and appreciated by those skilled in the art. The x-value and the y-value may be saved in the memory of the controller in a 32-bit binary floating point form, and represented by a 32-bit floating point number. Accordingly, clearing or masking the sign from the x-value and the sign from the y-value to define the absolute value of the x-value and the absolute value of the y-value may include clearing a sign bit in the 32-bit floating point number of the x-value and the y-value respectively, to define the y-value and the x-value as a positive number. As is generally appreciated, the sign of a number represented by a 32-bit floating point number is indicated by the sign bit, which is the most significant, typically the leftmost bit of the 32-bit floating point number. As is generally appreciated, a positive sign is generally represented in binary code by a “0”, and a negative sign is generally represented in binary code by a “1”. As such, clearing the sign bit includes changing the sign bit to “0”.
After the sign of the y-value and the x-value are cleared to define the cleared x-value and the cleared y-value respectively, which effectively defines the absolute value of the x-value and the y-value respectively, the cleared x-value (the effective absolute value of the x-value) is compared to the cleared y-value (the effective absolute value of the y-value) to determine if the cleared x-value is greater than the cleared y-value, or if the cleared x-value is equal to or less than the cleared y-value. The cleared x-value is compared to the cleared y-value using integer operations, instead of floating point operations. Accordingly, the cleared x-value is compared to the cleared y-value using an integer compare, instead of a floating point compare. Because the arctangent of an angle is calculated from a quotient of an x-value divided by a y-value, the y-value, being the denominator, must include a non-zero number. Therefore, if the cleared y-value (the effective absolute value of the y-value) is equal to zero, a value equal to zero is returned as the calculated arctangent output value of the point. When the cleared x-value (the effective absolute value of the x-value) is equal to or less than the cleared y-value (the effective absolute value of the y-value), an arctangent calculation input argument is calculated by dividing the cleared x-value by the cleared y-value. The arctangent of the input argument is then calculated to define the calculated arctangent output value. As such, the arctangent of the quotient of the cleared x-value divided by the cleared y-value is calculated to define the calculated arctangent output value.
When the cleared x-value (the effective absolute value of the x-value) is greater than the cleared y-value (the effective absolute value of the y-value), then the cleared x-value is swapped for the cleared y-value to define a swapped x-value, and the cleared y-value is swapped for the cleared x-value to define a swapped y-value. Accordingly, the swapped x-value is equal to the cleared y-value (the effective absolute value of the y-value), and the swapped y-value is equal to the cleared x-value (the effective absolute value of the x-value). If the point falls within the second octant 26, the fourth octant 30, the sixth octant 34, or the eighth octant 38, then the cleared values of the x-value and the y-value are swapped.
When the cleared x-value and the cleared y-value are swapped, the swap bit of the index is coded to indicate that the cleared x-value and the cleared y-value were swapped to define the swapped x-value and the swapped y-value respectively. In the exemplary embodiment, the swap bit is coded with a “0” to indicate that the cleared x-value and the cleared y-value have not been swapped. In contrast, in the exemplary embodiment, the swap bit is coded with a “1” to indicate that the cleared x-value and the cleared y-value have been swapped. For example, if the point includes an x-value equal to −4, and a y-value equal to −3, then the cleared x-value (4) is greater than the cleared y-value (3), and the cleared values of the x-value and the y-value are swapped. As such, the swap bit is coded as “1”. Because both the x-value and the y-value include a negative sign, both the x-bit and the y-bit are coded as “1”. Accordingly, in the exemplary embodiment, the coded index for a point having an x-value equal to −4 and a y-value equal to −3 is represented in binary form in the pre-shifted index by 11100. When the cleared x-value is greater than the cleared y-value, and after the swap bit has been coded into the index, then an arctangent calculation input argument is calculated by dividing the swapped x-value by the swapped y-value. The arctangent of the input argument is then calculated to define the calculated arctangent output value. As such, the arctangent of the quotient of the swapped x-value divided by the swapped y-value is calculated to define the calculated arctangent output value.
In the preceding exemplary embodiment, the swap bit is encoded with a “1” to indicate that the cleared x-value and the cleared y-value have been swapped, and is encoded with a “0” to indicate that the cleared x-value and the cleared y-value have not been swapped. However, it should be appreciated that the polarity of the swap bit may be reversed, with the swap bit coded with a “0” to indicate that the cleared x-value and the cleared y-value have been swapped, and coded with a “1” to indicate that the cleared x-value and the cleared y-value have not been swapped. If the polarity of the swap bit is reversed from the exemplary embodiment described above, it should be appreciated that the various look-up tables described in detail below must be re-ordered appropriately.
The coded index, including the coded values for the x-bit, the y-bit, and the swap bit, is used to look-up a segment base angle from an angle adjustment table. Notably, by combining all information required for post-processing of the calculated arctangent output value into a single element of the angle adjustment table, only one access into a single look-up table (the angle adjustment table) is required to calculate the arctangent of the point, thereby reducing memory usage and increasing processing speed.
The segment base angle may be saved in the memory of the controller in a 32-bit binary floating point form, and represented by a 32-bit floating point number. Referring to
Preferably, the reverse bit is coded into one of bit 12 or bit 13 of the 32-bit floating point number representing the segment base angle. Bit 12 is shown in column 50 of
The angle adjustment table of the exemplary embodiment is shown in
Within
The segment base angle and encoded output angle adjustment sign value for each possible coded combination of the index are provided in column 64 in hexadecimal form. The segment base angle and encoded output angle adjustment sign value for each possible coded combination of the index are provided in column 66 in 32-bit binary floating point form. The segment base angle and encoded output angle adjustment sign value for each possible coded combination of the index are provided in column 68 in floating point decimal form.
Once the segment base angle is looked-up from the angle adjustment table based upon the specific coding of the index, a numerical sign for the calculated arctangent output value is extracted from a reverse bit encoded in the segment base angle. The numerical sign may include either a positive (+) sign for a positive number, or a negative (−) sign for a negative number. As is generally appreciated, a positive sign is generally coded into a 32-bit floating point number as a “0”, and a negative sign is generally coded into a 32-bit floating point number as a “1”. Accordingly, the reverse bit may be coded with a “0” for a positive sign, or with a “1” for a negative sign. As noted above, the reverse bit is coded into one of bit 12 or bit 13 of the 32-bit floating point number representing the segment base angle, because bit 12 and bit 13 all always otherwise zero for all values of the segment base angle. Bit 12 is shown in column 70 of
The numerical sign extracted from the reverse bit is applied to the calculated arctangent output value, calculated above, to define a corrected arctangent value. Accordingly, the corrected arctangent value may include either a negative number or a positive number. The numerical sign is applied to the calculated arctangent output value using integer operations, instead of floating point operations. The reverse bit of the segment base angle is then masked or cleared to define a cleared segment base angle. The cleared segment base angle is the true or actual value of the segment base angle. Accordingly, the cleared segment base angle may include either a positive number or a negative number.
Clearing the reverse bit of the segment base angle to define the cleared segment base angle includes clearing the reverse bit in the 32-bit floating point number representing the segment base angle. The reverse bit is cleared using a basic integer “clearing” operation, as understood by those skilled in the art.
The corrected arctangent value is then added to the cleared segment base angle (i.e., the true or actual value of the segment base angle) to define the arctangent of the point. It should be appreciated that the corrected arctangent value may include either a positive or negative number, and that the cleared segment base angle may also include either a positive or negative number. Accordingly adding the corrected arctangent value to the cleared segment base angle may include adding a positive number to a positive number, a negative number to a positive number, or a negative number to a negative number.
The above described process removes all branches from the pre-processing and post-processing of the input data used to calculate the arctangent of the point. As is understood by those skilled in the art, a branch is an instruction in a computer program that may, when executed by the controller, cause the controller to begin execution of a different instruction sequence. Each time the linear flow of the processor is interrupted by a branch, the processor slows down. The above described method removes all branches from the pre-processing and post-processing of the input data, thereby increasing the speed required to calculate the arctangent of the point.
The process for calculating the arctangent for the first point 40 is herein described below. The first point 40 includes a y-value 80 that is equal to 4, and an x-value 82 that is equal to 3. Because both the y-value 80 and the x-value 82 are positive, both the y-bit and the x-bit of the index are coded with a “0”. The sign bits of the 32-bit floating point numbers representing the x-value and the y-value respectively are then masked or cleared to define the cleared x-value and the cleared y-value respectively. As noted above, the cleared x-value effectively defines the absolute value of the x-value, and the cleared y-value effectively defines the absolute value of the y-value. The cleared x-value is equal to 3, and the cleared y-value is equal to 4. Because the cleared x-value is less than the cleared y-value, the cleared x-value and the cleared y-value are not swapped, and the swap bit is coded into the index with a value of “0”. Accordingly, the swap bit is coded with a “0”, the y-bit is coded with a “0”, and the x-bit is coded with a “0”. Therefore, the shifted index may be represented by a binary value as 00000. The arctangent of the quotient of the cleared x-value divided by the cleared y-value is then calculated to define the calculated arctangent output value. More specifically, an arctangent calculation input argument is calculated by dividing the cleared x-value by the cleared y-value. The arctangent of the input argument, i.e., the quotient of the cleared x-value divided by the cleared y-value, is then calculated to define the calculated arctangent output value. Accordingly, the arctangent calculation input argument is equal to the quotient of 3 divided by 4. The calculated arctangent output value is equal to the arctangent if the input value, i.e., the arctangent of the quotient of 3 divided by 4, (arctan 3/4) which is equal to 36.87°. Referring to
The process for calculating the arctangent for the second point 42 is herein described below. The second point 42 includes a x-value 88 that is equal to −4, and an y-value 90 that is equal to −3. Because both the x-value 88 and the y-value 90 are negative, both the x-bit and the y-bit of the index are coded with a “1”. The sign bits of the 32-bit floating point numbers representing the x-value 88 and the y-value 90 respectively are then cleared to define the cleared x-value and the cleared y-value respectively. As noted above, the cleared x-value and the cleared y-value effectively define the absolute value of the x-value and the y-value respectively. The cleared x-value is equal to 4, and the cleared y-value is equal to 3. Because the cleared x-value is greater than the cleared y-value (4>3), the cleared x-value and the cleared y-value are swapped to define the swapped x-value and the swapped y-value. As such, the swapped x-value is equal to the cleared y-value (swapped x-value=3), and the swapped y-value is equal to the cleared x-value (swapped y-value=4). Because the cleared x-value and the cleared y-value are swapped, the swap bit is coded into the index with a value of “1”. Accordingly, the swap bit is coded with a “1”, the y-bit is coded with a “1”, and the x-bit is coded with a “1”. Therefore, the shifted index may be represented by a binary value as 11100. The arctangent of the quotient of the swapped x-value divided by the swapped y-value is then calculated to define the calculated arctangent output value. More specifically, an arctangent calculation input argument is calculated by dividing the swapped x-value by the swapped y-value. The arctangent of the input argument, i.e., the quotient of the swapped x-value divided by the swapped y-value, is then calculated to define the calculated arctangent output value. Accordingly, the arctangent calculation input argument is equal to the quotient of 3 divided by 4. The calculated arctangent output value is equal to the arctangent of the input value, i.e., the arctangent of the quotient of 3 divided by 4, (arctan 3/4) which is equal to 36.87°. Referring to
The negative sign indicated by the reverse bit, which was extracted from the segment base angle, is then applied to the calculated arctangent output value to define the corrected arctangent value. As noted above, the calculated arctangent output value is equal to 36.87°, and the reverse bit extracted from the segment base angle is negative, so the corrected arctangent value is equal to −36.87°. The corrected arctangent value (−) 36.87° is then added to the cleared segment base angle (270°) to define the arctangent of the second point 42. Accordingly, arctangent of the second point 42 is calculated as 233.13°, and is indicated by angle 94, shown in
The detailed description and the drawings or figures are supportive and descriptive of the invention, but the scope of the invention is defined solely by the claims. While some of the best modes and other embodiments for carrying out the claimed invention have been described in detail, various alternative designs and embodiments exist for practicing the invention defined in the appended claims.