forked from machinekit/machinekit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request machinekit#1373 from zultron/task-pll-correction
Port sittner's add-task-pll-functions.patch to Machinekit
- Loading branch information
Showing
12 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/python | ||
|
||
import sys | ||
import os | ||
|
||
# Params from test.hal | ||
period = 1000000 | ||
# Params from pll_correction.comp | ||
numsamps = 1000 | ||
|
||
os.chdir(os.path.dirname(os.path.realpath(__file__))) | ||
|
||
with open("result", 'r') as f: | ||
with open("stderr", 'a') as log: | ||
for line in f: | ||
line = line.rstrip('[ \n]') | ||
(cycle_count, period_actual, pll_err, samp_avg, phase_diff) = ( | ||
[int(s) for s in line.split(' ')]) | ||
|
||
if cycle_count == 2*numsamps: | ||
log.write("%s\n" % line) | ||
if abs(samp_avg) > period/100: | ||
log.write("0 PLL didn't converge: abs(%d) > %d/100\n" % ( | ||
samp_avg, period)) | ||
sys.exit(-1) | ||
else: | ||
log.write("0 PLL converged: abs(%d) <= %d/100\n" % ( | ||
samp_avg, period)) | ||
if cycle_count == 4*numsamps: | ||
log.write("%s\n" % line) | ||
if abs(samp_avg) > period/100: | ||
log.write("10 PLL didn't converge: abs(%d) > %d/100\n" % ( | ||
samp_avg, period)) | ||
sys.exit(-1) | ||
else: | ||
log.write("10 PLL converged: abs(%d) <= %d/100\n" % ( | ||
samp_avg, period)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
component pll_correction; | ||
|
||
// Cumulative difference between expected and actual period | ||
pin out s32 pll_err = 0; | ||
// Actual period of this cycle | ||
pin out s32 period_actual; | ||
// expected cycle start time | ||
pin out u32 time_base_hi = 0; | ||
pin out u32 time_base_lo = 0; | ||
// cycle start time | ||
pin out u32 now_hi; | ||
pin out u32 now_lo; | ||
// previous cycle start time | ||
pin out u32 prev_hi; | ||
pin out u32 prev_lo; | ||
// PLL reference | ||
pin out u32 ref_hi; | ||
pin out u32 ref_lo; | ||
// Number of cycles | ||
pin out u32 cycle_count = 0; | ||
|
||
// Buffer for average | ||
variable int numsamps = 1000; | ||
variable int samps[1000]; | ||
variable int samp_last; | ||
pin out s32 samp_avg; | ||
|
||
// PLL settings | ||
pin out bit pll_on; | ||
variable int pll_periods = 10; // Lock on to this many phases in the future | ||
// Monitor phase difference | ||
pin out s32 phase_diff; | ||
|
||
function _; | ||
license "GPL"; | ||
;; | ||
|
||
// time_set(foo, bar) splits ull bar into foo_hi and foo_lo | ||
#define time_set(dst, src) do { \ | ||
dst ## _hi = (uint32_t) (src>>32); \ | ||
dst ## _lo = (uint32_t) src; \ | ||
} while (0) | ||
|
||
// time(foo) returns ull of joined foo_hi and foo_lo | ||
#define time(src) \ | ||
((unsigned long long) \ | ||
((((unsigned long long) src ## _hi) << 32) + src ## _lo)) \ | ||
|
||
#define min(x,y) (x<y ? x : y) | ||
|
||
FUNCTION(_) { | ||
// Get current time | ||
time_set(now, rtapi_get_time()); | ||
if (cycle_count == 0) { | ||
// at first cycle, set time_base and prev to now | ||
time_set(time_base, time(now)); | ||
time_set(prev, time(now) - period); | ||
} else | ||
// increment time base by period | ||
time_set(time_base, time(time_base) + period); | ||
|
||
// Difference between expected and actual period start | ||
period_actual = time(now) - time(prev); | ||
|
||
// Phase difference from base | ||
phase_diff = time(now) - time(time_base); | ||
|
||
// Incremental phase error | ||
pll_err = phase_diff - ( | ||
// Maintain 0 err in beginning, period*pll_periods later | ||
!(pll_on = (cycle_count > numsamps*2)) ? 0 : period*pll_periods); | ||
|
||
// Adjust phase | ||
rtapi_task_pll_set_correction(-pll_err); | ||
|
||
// Get reference (unused) | ||
time_set(ref, rtapi_task_pll_get_reference()); | ||
|
||
// Averages | ||
samps[(samp_last++)%numsamps] = pll_err; | ||
samp_avg = 0; | ||
for (int i=0; i<min(numsamps,samp_last); i++) samp_avg += samps[i]; | ||
samp_avg /= min(numsamps,samp_last); | ||
|
||
// Updates for next cycle | ||
time_set(prev, time(now)); | ||
cycle_count++; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
loadrt threads name1=t period1=1000000 | ||
|
||
# Load pc comp | ||
loadrt pll_correction names=pc | ||
addf pc t | ||
|
||
# Load sampler comp | ||
loadrt sampler cfg=ussss depth=2000 | ||
addf sampler.0 t | ||
|
||
# Net pc signals to sampler | ||
net cycle-count pc.cycle-count => sampler.0.pin.0 | ||
net period-actual pc.period-actual => sampler.0.pin.1 | ||
net pll-err pc.pll-err => sampler.0.pin.2 | ||
net samp-avg pc.samp-avg => sampler.0.pin.3 | ||
net phase-diff pc.phase-diff => sampler.0.pin.4 | ||
|
||
# Start threads and wait for userland sampler to exit | ||
start | ||
|
||
# Load userland sampler comp | ||
# - LinuxCNC | ||
#loadusr -Wn halsampler halsampler -N halsampler -n 100 | ||
# - Machinekit | ||
loadusr -w halsampler -n 4000 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
typedef long long int TIME; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash -e | ||
|
||
COMP="$(which halcompile || which comp)" | ||
$COMP --install pll_correction.comp 1>&2 | ||
halrun -f pll_correction.hal |