在给定数据范围内找到编码器的位置
Finding position of encoder given range of data
我有一个范围从 0 到 +10000 的编码器数据。我需要知道车轮的圆弧位置。我计算出车轮旋转 1 圈为 5570 次计数。
最后,我想知道确切的圆弧位置,并在完成一整圈后将其重置为零。
例如
<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,但我只是想知道通用算法。
有没有通用的方法来完成这个?
谢谢!
您需要记住最后的状态,并在超过 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
我认为您可以通过跟踪完整的转速并使用 if 语句来处理“360 度也是 0 度”的问题。这里有一些公式,它们似乎可以为您提供有关每一行的大量信息:
正文版
+----------+------------------------------------------------------+----------------+----------------------------+----------------------------------------------+----------------------------------+
| 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
您可以用它来计算圆弧一对一的快速公式。例如,要获取上面 table 中 A4 的 arc(),您可以使用: =arc(A4, $A:A3)
可以将其复制下来,它会快速生成弧线。如果当前位置和前一个位置之间的差异小于 100 步后退,那么那里也有一个 backwardthreshold
,它不会重置为下一个 10000。您可以将 100 更改为任何有意义的值。
我有一个范围从 0 到 +10000 的编码器数据。我需要知道车轮的圆弧位置。我计算出车轮旋转 1 圈为 5570 次计数。
最后,我想知道确切的圆弧位置,并在完成一整圈后将其重置为零。
例如
<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,但我只是想知道通用算法。
有没有通用的方法来完成这个?
谢谢!
您需要记住最后的状态,并在超过 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
我认为您可以通过跟踪完整的转速并使用 if 语句来处理“360 度也是 0 度”的问题。这里有一些公式,它们似乎可以为您提供有关每一行的大量信息:
正文版
+----------+------------------------------------------------------+----------------+----------------------------+----------------------------------------------+----------------------------------+
| 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
您可以用它来计算圆弧一对一的快速公式。例如,要获取上面 table 中 A4 的 arc(),您可以使用: =arc(A4, $A:A3)
可以将其复制下来,它会快速生成弧线。如果当前位置和前一个位置之间的差异小于 100 步后退,那么那里也有一个 backwardthreshold
,它不会重置为下一个 10000。您可以将 100 更改为任何有意义的值。