// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_autotune()
int calc_autotune()
{
double val;

   if(HEAT_OVERSHOOT == 0.0F) return 1;
   if(COOL_UNDERSHOOT == 0.0F) return 2;
   if(ct_samples == 0.0) return 3;
   if(amp_ptr < 2) return 4;
   if(cyc_ptr < 2) return 5;

   val  = tune_amp[amp_ptr/2];
   if(amp_ptr > 2) {
      val += tune_amp[amp_ptr/2-1];
      val += tune_amp[amp_ptr/2+1];
      val /= 3.0;
   }
   if(val == 0.0) return 6;
   P_GAIN = 0.625F * (KL_TUNE_STEP+KL_TUNE_STEP) / (float) val;

   val  = tune_cyc[cyc_ptr/2];
   if(cyc_ptr > 2) {
      val += tune_cyc[cyc_ptr/2-1];
      val += tune_cyc[cyc_ptr/2+1];
      val /= 3.0;
   }
   I_TC   = 0.75F * (float) val;
   D_TC   = 0.1875F * (float) val; 

   k6 = 0.0F;
// integrator = 0.0F;

   calc_k_factors();
   clear_pid_display();
   show_pid_values();

   bang_bang = 0;    // pid autotune analysis done
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movsd 0x34b410(%rip), %xmm0   # 0x4b4ce0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1698e7
jp 0x1698e7
movl $0x1, -0x4(%rbp)
jmp 0x169b49
movsd 0x34b3f9(%rip), %xmm0   # 0x4b4ce8
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x169906
jp 0x169906
movl $0x2, -0x4(%rbp)
jmp 0x169b49
movsd 0x34b35a(%rip), %xmm0   # 0x4b4c68
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x169925
jp 0x169925
movl $0x3, -0x4(%rbp)
jmp 0x169b49
cmpl $0x2, 0x34b484(%rip)    # 0x4b4db0
jge 0x16993a
movl $0x4, -0x4(%rbp)
jmp 0x169b49
cmpl $0x2, 0x34b4bf(%rip)    # 0x4b4e00
jge 0x16994f
movl $0x5, -0x4(%rbp)
jmp 0x169b49
movl 0x34b45b(%rip), %eax    # 0x4b4db0
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x34b409(%rip), %rax    # 0x4b4d70
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x10(%rbp)
cmpl $0x2, 0x34b438(%rip)    # 0x4b4db0
jle 0x1699e4
movl 0x34b430(%rip), %eax    # 0x4b4db0
movl $0x2, %ecx
cltd
idivl %ecx
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x34b3db(%rip), %rax    # 0x4b4d70
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl 0x34b406(%rip), %eax    # 0x4b4db0
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x34b3b1(%rip), %rax    # 0x4b4d70
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x5805(%rip), %xmm1     # 0x16f1e0
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x169a00
jp 0x169a00
movl $0x6, -0x4(%rbp)
jmp 0x169b49
leaq 0x234e49(%rip), %rax    # 0x39e850
movsd (%rax), %xmm1
leaq 0x234e3e(%rip), %rax    # 0x39e850
addsd (%rax), %xmm1
movsd 0x2575a(%rip), %xmm0    # 0x18f178
mulsd %xmm1, %xmm0
movsd -0x10(%rbp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
divsd %xmm1, %xmm0
leaq 0x234dee(%rip), %rax    # 0x39e828
movsd %xmm0, (%rax)
movl 0x34b3bc(%rip), %eax    # 0x4b4e00
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x34b36a(%rip), %rax    # 0x4b4dc0
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x10(%rbp)
cmpl $0x2, 0x34b399(%rip)    # 0x4b4e00
jle 0x169ad3
movl 0x34b391(%rip), %eax    # 0x4b4e00
movl $0x2, %ecx
cltd
idivl %ecx
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x34b33c(%rip), %rax    # 0x4b4dc0
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl 0x34b367(%rip), %eax    # 0x4b4e00
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x34b312(%rip), %rax    # 0x4b4dc0
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x5716(%rip), %xmm1     # 0x16f1e0
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm1
movss 0x5840(%rip), %xmm0     # 0x16f324
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x234d4d(%rip), %rax    # 0x39e840
movsd %xmm0, (%rax)
movsd -0x10(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm1
movss 0x257bc(%rip), %xmm0    # 0x18f2c4
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x234d19(%rip), %rax    # 0x39e830
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x34b222(%rip)   # 0x4b4d48
callq 0x169430
callq 0x48c90
callq 0x1697c0
leaq 0x234ce0(%rip), %rax    # 0x39e81c
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)