找到给定数据范围的编码器位置

时间:2017-05-17 17:44:43

标签: excel algorithm sensor

我的编码器数据范围为0到+10000。我需要知道车轮的弧形位置。我计算了1轮的转数为5570计数。

最后,我想知道确切的弧形位置,并在它完成一整圈时将其重置为零。

e.g。

<table border=1>
  <tr>
    <th>Raw Count</th>
    <th>Arc Position Count</th>
  </tr>
  <tr>
    <td>0</td>
    <td>0</td>
  </tr>
  <tr>
    <td>5000</td>
    <td>5000</td>
  </tr>
  <tr>
    <td>5569</td>
    <td>5569</td>
  </tr>
  <tr>
    <td>5570</td>
    <td>0</td>
  </tr>
  <tr>
    <td>10000</td>
    <td>4430</td>
  </tr>
  <tr>
    <td>0</td>
    <td>4431</td>
  </tr>
</table>

我尝试过使用MOD(),但是当它从10000回到0时会出现问题。我得到了一些错误的数字。

我正在使用Excel,但我只想知道一般算法。

是否有一般方法可以实现这一目标?

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为您可以通过跟踪完整转速并使用if语句来处理“360度也是0度”的问题。这里有一些公式似乎会给你很多关于每一行的信息:

enter image description here

文字版

+----------+------------------------------------------------------+----------------+----------------------------+----------------------------------------------+----------------------------------+
| Position | Adjusted =IF(A3<A2, 10000, 0) + FLOOR(B2, 10000) +A3 | =MOD(A2, 5569) | degrees  =(B2*360)/5569    | Condensed function =(MOD(A2, 5569)*360)/5569 | Complete Revs =FLOOR(A2/5569, 1) |
+----------+------------------------------------------------------+----------------+----------------------------+----------------------------------------------+----------------------------------+
|        1 |                                                    1 |              1 |                0.064643563 |                                  0.064643563 |                                0 |
|     1200 |                                                 1200 |           1200 |                77.57227509 |                                  77.57227509 |                                0 |
|     3300 |                                                 3300 |           3300 |                213.3237565 |                                  213.3237565 |                                0 |
|     5569 |                                                 5569 |              0 |                          0 |                                            0 |                                1 |
|     5700 |                                                 5700 |            131 |                8.468306698 |                                  8.468306698 |                                1 |
|     1137 |                                                11137 |           5568 |                359.9353564 |                                  359.9353564 |                                1 |
|     1138 |                                                11138 |              0 |                          0 |                                            0 |                                2 |
+----------+------------------------------------------------------+----------------+----------------------------+----------------------------------------------+----------------------------------+

** UDF来解决这个问题

将其粘贴在工作表上的新模块中(在VBE中)

Function arc(currentPos As Integer, prevSeq As Range) As Double
    'Set backwards threshold
    Dim backwardsthreshold As Integer
    backwardsthreshold = 100


    Dim tenThousandths As Integer
    Dim prevPos As Integer
    Dim capturedInitPos As Boolean

    'Loop through previous sequence to determine tenthousandths
    'as well as intitial position
    For Each rngPos In prevSeq.Cells

        'set initial position
        If Not capturedInitPos Then initialPos = rngPos.Value: capturedInitPos = True

        'Increment tenThousandths if we are skipping backwards and less
        'then our backwards threshold
        If prevPos > rngPos.Value2 And Abs(prevPos - rngPos.Value) > backwardsthreshold Then
            tenThousandths = tenThousandths + 1
        End If

        'set prevPos number for next iteration
        prevPos = rngPos.Value
    Next

    'Bump it the adjusted number
    currentPos = currentPos + (tenThousandths * 10000)

    'Subtract the initial position
    currentPos = currentPos - initialPos

    'determine arc
    arc = ((currentPos Mod 5569) * CLng(360)) / CLng(5569)

End Function

您可以使用它来计算一对一快速公式中的弧。例如,要获得上表中A4的arc(),您可以使用:=arc(A4, $A$2:A3)可以向下复制它,它将快速生成弧。如果当前位置和前一个位置之间的差异小于100步之后,那里还有一个backwardthreshold,它将不会重置为下一个10000。你可以将100改为任何有意义的东西。

答案 1 :(得分:0)

当超过10000边界

时,您需要记住最后一个状态并对编码器值进行校正
current = encoder + shift
if last  - current > 5000 then  //too big jump down
     shift = shift + 10001
     current = encoder + shift
arc = current mod 5570  // 10001 mod 5570 = 4431
last = current