// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_k_factors()
void calc_k_factors()
{
   // This routine converts the user friendly input PID constants into the
   // "k" values used by the PID filter algorithm.  It is called whenever
   // a PID filter constant is changed by the user.
   //
   // P_GAIN        -> k1   (PID gain)
   // D_TC          -> k2   (derivative time constant)
   // FILTER_TC     -> k3   (filter time constant)
   // I_TC          -> k4   (integrator time constant)
   // FILTER_OFFSET         (temperature setpoint offset)

   if(I_TC) k4 = ((DATA_SIZE)1.0/I_TC) / (DATA_SIZE) PID_UPDATE_RATE * P_GAIN;
   else     k4 = (DATA_SIZE) 0.0;

   if(FILTER_TC > 0.0) {
      k3 = (DATA_SIZE) 1.0 - ((DATA_SIZE) 1.0/((FILTER_TC*(DATA_SIZE) PID_UPDATE_RATE)+(DATA_SIZE) 1.0));
      k2 = ((DATA_SIZE) -1.0 * D_TC * P_GAIN) / (FILTER_TC*(DATA_SIZE) PID_UPDATE_RATE+(DATA_SIZE) 1.0) * ((DATA_SIZE) PID_UPDATE_RATE/(DATA_SIZE) SCALE_FACTOR);
   }
   else {
      k3 = FILTER_TC;
      k2 = ((DATA_SIZE) -1.0 * D_TC * P_GAIN) * ((DATA_SIZE) PID_UPDATE_RATE/(DATA_SIZE) SCALE_FACTOR);
   }

   k1 = ((P_GAIN/(DATA_SIZE) SCALE_FACTOR) * ((DATA_SIZE) 1.0 - k3)) - k2;

   if(pid_debug || bang_bang) {  // set a marker each time a PID value changes
      if(++test_marker >= MAX_MARKER) test_marker = 1;
      if(test_marker < 0) test_marker = 1;
      mark_q_entry[test_marker] = plot_q_in;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
leaq 0x235405(%rip), %rax    # 0x39e840
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x16944c
jp 0x16944c
jmp 0x169480
movsd 0x5c14(%rip), %xmm0     # 0x16f068
leaq 0x2353e5(%rip), %rax    # 0x39e840
divsd (%rax), %xmm0
movsd 0x5c01(%rip), %xmm1     # 0x16f068
divsd %xmm1, %xmm0
leaq 0x2353b6(%rip), %rax    # 0x39e828
mulsd (%rax), %xmm0
movsd %xmm0, 0x34b8c2(%rip)   # 0x4b4d40
jmp 0x16948b
xorps %xmm0, %xmm0
movsd %xmm0, 0x34b8b5(%rip)   # 0x4b4d40
leaq 0x2353a6(%rip), %rax    # 0x39e838
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x169529
leaq 0x23538e(%rip), %rax    # 0x39e838
movsd (%rax), %xmm0
movsd 0x5bb2(%rip), %xmm1     # 0x16f068
addsd %xmm1, %xmm0
movsd 0x5ba6(%rip), %xmm1     # 0x16f068
divsd %xmm0, %xmm1
movsd 0x5b9a(%rip), %xmm0     # 0x16f068
subsd %xmm1, %xmm0
movsd %xmm0, 0x34b85e(%rip)   # 0x4b4d38
movsd 0x5c36(%rip), %xmm0     # 0x16f118
leaq 0x235347(%rip), %rax    # 0x39e830
mulsd (%rax), %xmm0
leaq 0x235334(%rip), %rax    # 0x39e828
mulsd (%rax), %xmm0
leaq 0x235339(%rip), %rax    # 0x39e838
movsd (%rax), %xmm1
movsd 0x5b5d(%rip), %xmm2     # 0x16f068
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd 0x5b4d(%rip), %xmm1     # 0x16f068
mulsd %xmm1, %xmm0
movsd %xmm0, 0x34b809(%rip)   # 0x4b4d30
jmp 0x16956e
leaq 0x235308(%rip), %rax    # 0x39e838
movsd (%rax), %xmm0
movsd %xmm0, 0x34b7fc(%rip)   # 0x4b4d38
movsd 0x5bd4(%rip), %xmm0     # 0x16f118
leaq 0x2352e5(%rip), %rax    # 0x39e830
mulsd (%rax), %xmm0
leaq 0x2352d2(%rip), %rax    # 0x39e828
mulsd (%rax), %xmm0
movsd 0x5b06(%rip), %xmm1     # 0x16f068
mulsd %xmm1, %xmm0
movsd %xmm0, 0x34b7c2(%rip)   # 0x4b4d30
leaq 0x2352b3(%rip), %rax    # 0x39e828
movsd (%rax), %xmm0
movsd 0x5ae7(%rip), %xmm1     # 0x16f068
divsd %xmm1, %xmm0
movsd 0x5adb(%rip), %xmm2     # 0x16f068
subsd 0x34b7a3(%rip), %xmm2   # 0x4b4d38
movsd 0x34b793(%rip), %xmm1   # 0x4b4d30
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x34b764(%rip)   # 0x4b4d28
leaq 0x235255(%rip), %rax    # 0x39e820
cmpb $0x0, (%rax)
jne 0x1695dc
leaq 0x235245(%rip), %rax    # 0x39e81c
cmpl $0x0, (%rax)
je 0x16963b
leaq 0x235235(%rip), %rax    # 0x39e818
movl (%rax), %eax
addl $0x1, %eax
leaq 0x235229(%rip), %rcx    # 0x39e818
movl %eax, (%rcx)
cmpl $0xa, %eax
jl 0x169603
leaq 0x23521b(%rip), %rax    # 0x39e818
movl $0x1, (%rax)
leaq 0x23520e(%rip), %rax    # 0x39e818
cmpl $0x0, (%rax)
jge 0x16961c
leaq 0x235202(%rip), %rax    # 0x39e818
movl $0x1, (%rax)
leaq 0x110ee5(%rip), %rax    # 0x27a508
movq (%rax), %rdx
leaq 0x2351eb(%rip), %rax    # 0x39e818
movslq (%rax), %rcx
leaq 0x110e59(%rip), %rax    # 0x27a490
movq %rdx, (%rax,%rcx,8)
popq %rbp
retq
nopl (%rax)