// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: analyze_bang_bang()
void analyze_bang_bang()
{
char *s;
double ct;
double val;

   ct = 0.0;
   if(bang_bang == 0) {  // we are not banging around
      return;   
   }
   else if(bang_bang == 1) {    // waiting for a temp zero crossing (temp near setpoint)
      if(((PID_error > 0.0) && (last_PID_error < 0.0)) ||
         ((PID_error < 0.0) && (last_PID_error > 0.0))) { 
         bang_bang = 2;         // now wait for pid to settle
         bang_settle_time = (int) (I_TC * 5.0F);  // wait for 5 integrator time constants
      }
      s = "ZEROING";
      goto tuned_up;
   }
   else if(bang_bang == 2) {  // waiting for pid filter to settle
      if(bang_settle_time > 0) --bang_settle_time;
      else bang_bang = 3;     // start analyzing pid disturbance cycles
      ct_samples = bang_settle_time;
      s = "SETTLING";
      goto tuned_up;
   }
   else if(bang_bang == 3) {  // first bang time,  initialize variables
      s = "INITIALIZING";
      BANG_INITS = 2;
      tune_cycles = TUNE_CYCLES;
      goto start_banging;
   }
   else if(bang_bang == 4) { // quick bang test
      s = "QUICK TEST";
      BANG_INITS = 0;
      tune_cycles = 1;

      start_banging:
      low_ok = high_ok = 0;
      heat_rate = cool_rate = 0.0F;
      heat_sum = cool_sum = 0.0F;
      heat_ticks = cool_ticks = 0.0F;
      cycle_time = avg_cycle_time = fall_time = ct_samples = 0.0;
      this_cycle = last_cycle = fall_cycle = GetMsecs();
      cyc_ptr = amp_ptr = 0;
      tune_cyc[0] = tune_amp[0] = 0.0;

      OLD_P_GAIN = P_GAIN;
      P_GAIN = 0.0F;
      if(PID_error > 0.0F) k6 = KL_TUNE_STEP;
      else                 k6 = (-KL_TUNE_STEP);
      calc_k_factors();

      bang_bang = 5;
      goto tuned_up;
   }


   // PID filter should now be near a steady state
   // Start disturbance testing
   last_delta = delta;
   delta = this_temperature - desired_temp;

   if(delta > 0.0F) {
      heat_sum += delta;     // calculate average peak values
      heat_ticks += 1.0F;
   }
   else if(delta < 0.0F) {
      cool_sum += delta;
      cool_ticks += 1.0F;
   }

   if(PID_error > 0.0F) k6 = KL_TUNE_STEP;
   else                 k6 = (-KL_TUNE_STEP);

   if((low_ok > BANG_INITS) && (high_ok > BANG_INITS)) {  // we are past the disturbance startup wobblies
      s = "TUNING";
   }
   else {
      s = "DISTURBING";
   }

   if((last_PID_error > 0.0F) && (PID_error <= 0.0F)) {  // rising zero crossing
      this_cycle = GetMsecs();
      cycle_time = this_cycle - last_cycle;
      cycle_time /= 1000.0;    // waveform cycle time in seconds
      fall_time = this_cycle - fall_cycle;
      fall_time /= 1000.0;

      if((low_ok > BANG_INITS) && (high_ok > BANG_INITS)) { // it's not the first rising crossing
         if(cycle_time > 10.0) {  // crossing is probably not noise
            avg_cycle_time += cycle_time;
            ct_samples += 1.0;
            if(cyc_ptr <= tune_cycles) cyc_ptr = add_to_bin(cycle_time,  tune_cyc,  cyc_ptr);

            if(heat_ticks) HEAT_OVERSHOOT = (heat_sum / heat_ticks) * 1.414F;
            if(cool_ticks) COOL_UNDERSHOOT = (cool_sum / cool_ticks) * 1.414F;
            val = HEAT_OVERSHOOT - COOL_UNDERSHOOT;
            if(amp_ptr <= tune_cycles) amp_ptr = add_to_bin(val,  tune_amp,  amp_ptr);
            heat_sum = cool_sum = 0.0F;
            heat_ticks = cool_ticks = 0.0F;

            if(cyc_ptr >= tune_cycles) {
               calc_autotune();
               s = "DONE";
               last_cycle = this_cycle;
               goto tuned_up;
            }
         }
      }
      else { // first rising zero crossing
         heat_sum = cool_sum = 0.0F;
         heat_ticks = cool_ticks = 0.0F;
         avg_cycle_time = ct_samples = 0.0;
      }

      last_cycle = this_cycle;
      rate_sum = delta;
      rate_count = 1.0F;

      ++low_ok;
   }
   else if((last_PID_error < 0.0F) && (PID_error >= 0.0F)) {  // falling zero crossing
      fall_cycle = GetMsecs();
      rate_sum = delta;     //!!!! should we filter test the cycle time here?
      rate_count = 1.0F;
      ++high_ok;
   }
   else {  // calculate the heating and cooling rates near the zero crossing
      rate_sum += delta;
      if(++rate_count == 5.0) {
         rate_sum /= rate_count;
         if(delta > 0.0) heat_rate = rate_sum;
         else            cool_rate = rate_sum;
      }
   }

   tuned_up:
   #ifdef DEBUG_TEMP_CONTROL
      ct = cycle_time;
      sprintf(debug_text2, "%s: cycle(%.0f)=%.3f+%.3f=%.3f sec   us(%.0f)=%.4f os(%.0f)=%.4f  [%.3f,%.4f]", 
             s, ct_samples, ct-fall_time, fall_time, ct,  
             cool_ticks, COOL_UNDERSHOOT,  heat_ticks, HEAT_OVERSHOOT,
             tune_cyc[cyc_ptr/2], tune_amp[amp_ptr/2]
      );
   #endif
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
leaq 0x23954e(%rip), %r14    # 0x33f63c
movl (%r14), %eax
cmpq $0x4, %rax
ja 0x1062b4
leaq 0x2546a(%rip), %rcx     # 0x12b56c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd 0x34f5fd(%rip), %xmm1   # 0x455710
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
movsd 0x34f5f5(%rip), %xmm0   # 0x455718
jbe 0x10612b
ucomisd %xmm0, %xmm2
ja 0x10614a
ucomisd %xmm1, %xmm2
leaq 0x2fe1c(%rip), %rbx     # 0x135f52
jbe 0x1066da
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1066da
movl $0x2, (%r14)
leaq 0x239508(%rip), %rax    # 0x33f660
movsd (%rax), %xmm0
mulsd 0x2fd4(%rip), %xmm0     # 0x109138
cvttsd2si %xmm0, %eax
movl %eax, 0x34f6a6(%rip)    # 0x455814
leaq 0x2fddd(%rip), %rbx     # 0x135f52
jmp 0x1066da
leaq 0x2fde6(%rip), %rbx     # 0x135f67
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x1061b6
movl 0x34f684(%rip), %eax    # 0x455814
testl %eax, %eax
jle 0x1065f4
decl %eax
movl %eax, 0x34f674(%rip)    # 0x455814
jmp 0x1065fb
leaq 0x2fdae(%rip), %rbx     # 0x135f5a
movl $0x7, %eax
movl $0x2, %ecx
movl %ecx, 0x34f65c(%rip)    # 0x455818
movl %eax, 0x34f5ae(%rip)    # 0x455770
xorl %r15d, %r15d
movq %r15, 0x34f51c(%rip)    # 0x4556e8
movq %r15, 0x34f50d(%rip)    # 0x4556e0
movq %r15, 0x34f4ee(%rip)    # 0x4556c8
movq %r15, 0x34f4df(%rip)    # 0x4556c0
movq %r15, 0x34f4c8(%rip)    # 0x4556b0
movq %r15, 0x34f4b1(%rip)    # 0x4556a0
movq %r15, 0x34f4c2(%rip)    # 0x4556b8
movq %r15, 0x34f4ab(%rip)    # 0x4556a8
movq %r15, 0x34f474(%rip)    # 0x455678
movq %r15, 0x34f48d(%rip)    # 0x455698
movq %r15, 0x34f45e(%rip)    # 0x455670
movq %r15, 0x34f44f(%rip)    # 0x455668
callq 0xae92
movsd %xmm0, 0x34f46a(%rip)   # 0x455690
movsd %xmm0, 0x34f45a(%rip)   # 0x455688
movsd %xmm0, 0x34f44a(%rip)   # 0x455680
movl %r15d, 0x34f583(%rip)   # 0x4557c0
movl %r15d, 0x34f5cc(%rip)   # 0x455810
movq %r15, 0x34f535(%rip)    # 0x455780
movq %r15, 0x34f57e(%rip)    # 0x4557d0
leaq 0x2393ef(%rip), %rax    # 0x33f648
movsd (%rax), %xmm0
leaq 0x239414(%rip), %rcx    # 0x33f678
movsd %xmm0, (%rcx)
movq %r15, (%rax)
leaq 0x2393fe(%rip), %rax    # 0x33f670
movsd (%rax), %xmm0
movapd 0x2f32(%rip), %xmm1     # 0x1091b0
xorpd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpltsd 0x34f481(%rip), %xmm2   # 0x455710
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movlpd %xmm2, 0x34f4b5(%rip)   # 0x455758
callq 0x1059fd
movl $0x5, (%r14)
jmp 0x1066da
movsd 0x34f35c(%rip), %xmm0   # 0x455618
movsd %xmm0, 0x34f35c(%rip)   # 0x455620
movsd 0x34f33c(%rip), %xmm0   # 0x455608
leaq 0x239af5(%rip), %rax    # 0x33fdc8
subsd (%rax), %xmm0
movsd %xmm0, 0x34f339(%rip)   # 0x455618
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1062f9
leaq 0x34f3b8(%rip), %rax    # 0x4556a8
leaq 0x34f3a9(%rip), %rcx    # 0x4556a0
jmp 0x10630d
ucomisd %xmm0, %xmm1
jbe 0x106329
leaq 0x34f3b2(%rip), %rax    # 0x4556b8
leaq 0x34f3a3(%rip), %rcx    # 0x4556b0
movsd (%rcx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rcx)
movsd (%rax), %xmm1
addsd 0x2d4b(%rip), %xmm1     # 0x109070
movsd %xmm1, (%rax)
movsd 0x34f3df(%rip), %xmm1   # 0x455710
leaq 0x239338(%rip), %rax    # 0x33f670
movsd (%rax), %xmm3
movapd 0x2e6c(%rip), %xmm4     # 0x1091b0
xorpd %xmm3, %xmm4
xorpd %xmm2, %xmm2
xorpd %xmm5, %xmm5
cmpltsd %xmm1, %xmm5
andpd %xmm5, %xmm3
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
movslq 0x34f4b0(%rip), %rax    # 0x455818
cmpq %rax, 0x34f379(%rip)    # 0x4556e8
leaq 0x2fbfc(%rip), %rbx     # 0x135f72
leaq 0x2fbfc(%rip), %rcx     # 0x135f79
cmovleq %rcx, %rbx
cmpq %rax, 0x34f358(%rip)    # 0x4556e0
cmovleq %rcx, %rbx
movlpd %xmm5, 0x34f3c4(%rip)   # 0x455758
movsd 0x34f37c(%rip), %xmm3   # 0x455718
ucomisd %xmm2, %xmm3
jbe 0x1065ae
ucomisd %xmm1, %xmm2
jb 0x1065ae
callq 0xae92
movapd %xmm0, %xmm1
subsd 0x34f2c7(%rip), %xmm1   # 0x455688
movsd %xmm0, 0x34f2b7(%rip)   # 0x455680
movsd 0x2c67(%rip), %xmm2     # 0x109038
divsd %xmm2, %xmm1
movsd %xmm1, 0x34f28b(%rip)   # 0x455668
movapd %xmm0, %xmm3
subsd 0x34f2a7(%rip), %xmm3   # 0x455690
divsd %xmm2, %xmm3
movsd %xmm3, 0x34f2a3(%rip)   # 0x455698
movq 0x34f2e4(%rip), %rax    # 0x4556e0
movslq 0x34f415(%rip), %rcx    # 0x455818
cmpq %rcx, %rax
jle 0x10667b
cmpq %rcx, 0x34f2d5(%rip)    # 0x4556e8
jle 0x10667b
ucomisd 0x2c87(%rip), %xmm1     # 0x1090a8
jbe 0x1066a7
movsd 0x34f241(%rip), %xmm2   # 0x455670
addsd %xmm1, %xmm2
movsd %xmm2, 0x34f235(%rip)   # 0x455670
movsd 0x34f235(%rip), %xmm2   # 0x455678
addsd 0x2c25(%rip), %xmm2     # 0x109070
movsd %xmm2, 0x34f225(%rip)   # 0x455678
movl 0x34f3b7(%rip), %ecx    # 0x455810
movl 0x34f311(%rip), %edx    # 0x455770
cmpl %edx, %ecx
jg 0x1064a8
cmpl $0x5f, %ecx
jg 0x1064a2
movslq %ecx, %rdi
leaq 0x34f35e(%rip), %rsi    # 0x4557d0
movsd %xmm1, (%rsi,%rdi,8)
testl %ecx, %ecx
jle 0x1064a0
leaq 0x1(%rcx), %rdi
movsd -0x10(%rsi,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x1064a0
movsd %xmm2, -0x8(%rsi,%rdi,8)
movsd %xmm1, -0x10(%rsi,%rdi,8)
decq %rdi
cmpq $0x1, %rdi
ja 0x10647f
incl %ecx
movl %ecx, 0x34f368(%rip)    # 0x455810
movsd 0x34f1f8(%rip), %xmm2   # 0x4556a8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jne 0x1064bc
jnp 0x1064d8
movsd 0x34f1dc(%rip), %xmm3   # 0x4556a0
divsd %xmm2, %xmm3
mulsd 0x262f0(%rip), %xmm3    # 0x12c7c0
movsd %xmm3, 0x34f218(%rip)   # 0x4556f0
movsd 0x34f1d8(%rip), %xmm2   # 0x4556b8
ucomisd %xmm1, %xmm2
jne 0x1064e8
jnp 0x106504
movsd 0x34f1c0(%rip), %xmm1   # 0x4556b0
divsd %xmm2, %xmm1
mulsd 0x262c4(%rip), %xmm1    # 0x12c7c0
movsd %xmm1, 0x34f1f4(%rip)   # 0x4556f8
movl 0x34f2b6(%rip), %esi    # 0x4557c0
cmpl %edx, %esi
jg 0x106567
cmpl $0x5f, %esi
jg 0x106561
movsd 0x34f1d5(%rip), %xmm1   # 0x4556f0
subsd 0x34f1d5(%rip), %xmm1   # 0x4556f8
movslq %esi, %r8
leaq 0x34f253(%rip), %rdi    # 0x455780
movsd %xmm1, (%rdi,%r8,8)
testl %esi, %esi
jle 0x10655f
leaq 0x1(%rsi), %r8
movsd -0x10(%rdi,%r8,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x10655f
movsd %xmm2, -0x8(%rdi,%r8,8)
movsd %xmm1, -0x10(%rdi,%r8,8)
decq %r8
cmpq $0x1, %r8
ja 0x10653b
incl %esi
movl %esi, 0x34f259(%rip)    # 0x4557c0
xorl %esi, %esi
movq %rsi, 0x34f140(%rip)    # 0x4556b0
movq %rsi, 0x34f129(%rip)    # 0x4556a0
movq %rsi, 0x34f13a(%rip)    # 0x4556b8
movq %rsi, 0x34f123(%rip)    # 0x4556a8
cmpl %edx, %ecx
jl 0x1066a7
callq 0x105c9d
movsd 0x34f0e6(%rip), %xmm0   # 0x455680
movsd %xmm0, 0x34f0e6(%rip)   # 0x455688
leaq 0x2f9db(%rip), %rbx     # 0x135f84
jmp 0x1066da
ucomisd %xmm3, %xmm2
jbe 0x106613
ucomisd %xmm2, %xmm1
jb 0x106613
callq 0xae92
movsd %xmm0, 0x34f0c9(%rip)   # 0x455690
movsd 0x34f049(%rip), %xmm0   # 0x455618
movsd %xmm0, 0x34f0f9(%rip)   # 0x4556d0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x34f0f0(%rip)    # 0x4556d8
incq 0x34f0f9(%rip)          # 0x4556e8
jmp 0x1066da
movl $0x3, (%r14)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x34f071(%rip)   # 0x455678
leaq 0x1f111(%rip), %rbx     # 0x12571f
jmp 0x1066da
movsd 0x34f0b5(%rip), %xmm1   # 0x4556d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x34f0a9(%rip)   # 0x4556d0
movsd 0x34f0a9(%rip), %xmm2   # 0x4556d8
addsd 0x2a39(%rip), %xmm2     # 0x109070
movsd %xmm2, 0x34f099(%rip)   # 0x4556d8
ucomisd 0x2af1(%rip), %xmm2     # 0x109138
jne 0x1066da
jp 0x1066da
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
divsd 0x2ad5(%rip), %xmm1     # 0x109138
movsd %xmm1, 0x34f065(%rip)   # 0x4556d0
jbe 0x106788
movsd %xmm1, 0x34f047(%rip)   # 0x4556c0
jmp 0x1066da
xorl %ecx, %ecx
movq %rcx, 0x34f02c(%rip)    # 0x4556b0
movq %rcx, 0x34f015(%rip)    # 0x4556a0
movq %rcx, 0x34f026(%rip)    # 0x4556b8
movq %rcx, 0x34f00f(%rip)    # 0x4556a8
movq %rcx, 0x34efd8(%rip)    # 0x455678
movq %rcx, 0x34efc9(%rip)    # 0x455670
movsd %xmm0, 0x34efd9(%rip)   # 0x455688
movsd 0x34ef61(%rip), %xmm0   # 0x455618
movsd %xmm0, 0x34f011(%rip)   # 0x4556d0
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x34f008(%rip)    # 0x4556d8
incq %rax
movq %rax, 0x34f006(%rip)    # 0x4556e0
movsd 0x34ef86(%rip), %xmm3   # 0x455668
movsd 0x34ef8e(%rip), %xmm0   # 0x455678
movsd 0x34efa6(%rip), %xmm2   # 0x455698
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
movsd 0x34efb6(%rip), %xmm4   # 0x4556b8
movsd 0x34efee(%rip), %xmm5   # 0x4556f8
movsd 0x34ef96(%rip), %xmm6   # 0x4556a8
movsd 0x34efd6(%rip), %xmm7   # 0x4556f0
movl 0x34f0f0(%rip), %eax    # 0x455810
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x34f09d(%rip), %rcx    # 0x4557d0
movsd (%rcx,%rax,8), %xmm8
movl 0x34f081(%rip), %eax    # 0x4557c0
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x34f02e(%rip), %rcx    # 0x455780
movsd (%rcx,%rax,8), %xmm9
movsd %xmm9, 0x8(%rsp)
movsd %xmm8, (%rsp)
leaq 0x238384(%rip), %rdi    # 0x33eaf0
leaq 0x2f816(%rip), %rsi     # 0x135f89
movq %rbx, %rdx
movb $0x8, %al
callq 0x9060
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm1, 0x34ef38(%rip)   # 0x4556c8
jmp 0x1066da