kFS Helper Script Development and Discussion

pteranodog

New Member
Messages
1
Reaction score
1
Hey everyone! I've made a quick-and-dirty optimized-kASM helper script for PID controllers, so I figured I'd start a thread here for sharing and discussing the design/optimization of kFS scripts for common tasks, at least until there's a more "standardized" way of sharing useful scripts.

Here's the fast-PID script I wrote! It uses pre-initialized global variables (you can call "init_pid" in an init task, or whenever you need to change the globals) to speed up the function calling. The script also assumed it will be called at a constant rate to remove a floating point division by delta-time; the delta-time multiplication/division is, in effect, distributed across the tuning variables, which are the only three multiplications this implementation actually does.
Code
; pid_fast
.include <kfs.kinc>

.global KP, 4
.global KI, 4
.global KD, 4
.global STPT, 4
.global KI_ACC, 4
.global PREV_ERR, 4

.proto "void init_pid(const float setpoint, const float kp, const float ki, const float kd)"
.func init_pid
    .args setpoint, kp, ki, kd
    LDL setpoint
    STG STPT
    LDL kp
    STG KP
    LDL ki
    STG KI
    LDL kd
    STG KD
    RET 0
.endfunc

.proto "float pid_fast(const float process_var)"
.func pid_fast
    .args process_var
    LDG STPT
    LDL process_var
    FSUB
    DUP
    LDG KP
    FMUL
    SWAP
    DUP
    LDG KI_ACC
    FADD
    DUP
    STG KI_ACC
    LDG KI
    FMUL
    SWAP
    DUP
    LDG PREV_ERR
    FSUB
    LDG KD
    FMUL
    SWAP
    STG PREV_ERR
    FADD
    FADD   
    RET 1
.endfunc
 
Back
Top