// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x20, %rsp
leaq 0x239ea8(%rip), %r14    # 0x32963c
movl (%r14), %eax
cmpq $0x4, %rax
ja 0xef960
leaq 0x25c14(%rip), %rcx     # 0x1153bc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd 0x34ff57(%rip), %xmm1   # 0x43f710
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
movsd 0x34ff4f(%rip), %xmm0   # 0x43f718
jbe 0xef7d1
ucomisd %xmm0, %xmm2
ja 0xef7f0
ucomisd %xmm1, %xmm2
leaq 0x30682(%rip), %rbx     # 0x11fe5e
jbe 0xefd3c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xefd3c
movl $0x2, (%r14)
leaq 0x239e62(%rip), %rax    # 0x329660
movsd (%rax), %xmm0
mulsd 0x3936(%rip), %xmm0     # 0xf3140
cvttsd2si %xmm0, %eax
movl %eax, 0x350000(%rip)    # 0x43f814
leaq 0x30643(%rip), %rbx     # 0x11fe5e
jmp 0xefd3c
leaq 0x3064c(%rip), %rbx     # 0x11fe73
pushq $0x1
popq %rax
xorl %ecx, %ecx
jmp 0xef856
movl 0x34ffe0(%rip), %eax    # 0x43f814
testl %eax, %eax
jle 0xefc52
decl %eax
movl %eax, 0x34ffd0(%rip)    # 0x43f814
jmp 0xefc59
leaq 0x30616(%rip), %rbx     # 0x11fe66
pushq $0x7
popq %rax
pushq $0x2
popq %rcx
andq $0x0, 0x34fe8a(%rip)    # 0x43f6e8
andq $0x0, 0x34fe7a(%rip)    # 0x43f6e0
andq $0x0, 0x34fe5a(%rip)    # 0x43f6c8
andq $0x0, 0x34fe4a(%rip)    # 0x43f6c0
andq $0x0, 0x34fe32(%rip)    # 0x43f6b0
andq $0x0, 0x34fe1a(%rip)    # 0x43f6a0
andq $0x0, 0x34fe2a(%rip)    # 0x43f6b8
andq $0x0, 0x34fe12(%rip)    # 0x43f6a8
andq $0x0, 0x34fdda(%rip)    # 0x43f678
andq $0x0, 0x34fdf2(%rip)    # 0x43f698
movl %ecx, 0x34ff6c(%rip)    # 0x43f818
andq $0x0, 0x34fdbc(%rip)    # 0x43f670
movl %eax, 0x34feb6(%rip)    # 0x43f770
andq $0x0, 0x34fda6(%rip)    # 0x43f668
callq 0xad3f
andl $0x0, 0x34fef2(%rip)    # 0x43f7c0
andl $0x0, 0x34ff3b(%rip)    # 0x43f810
andq $0x0, 0x34fea3(%rip)    # 0x43f780
movsd %xmm0, 0x34fdab(%rip)   # 0x43f690
andq $0x0, 0x34fee3(%rip)    # 0x43f7d0
movsd %xmm0, 0x34fd93(%rip)   # 0x43f688
movsd %xmm0, 0x34fd83(%rip)   # 0x43f680
leaq 0x239d44(%rip), %rax    # 0x329648
movsd (%rax), %xmm0
leaq 0x239d69(%rip), %rcx    # 0x329678
andq $0x0, (%rax)
movsd %xmm0, (%rcx)
leaq 0x239d52(%rip), %rax    # 0x329670
movsd (%rax), %xmm0
movapd 0x3886(%rip), %xmm1     # 0xf31b0
xorpd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpltsd 0x34fdd5(%rip), %xmm2   # 0x43f710
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movlpd %xmm2, 0x34fe09(%rip)   # 0x43f758
callq 0xef0e2
movl $0x5, (%r14)
jmp 0xefd3c
movsd 0x34fcb0(%rip), %xmm0   # 0x43f618
movsd %xmm0, 0x34fcb0(%rip)   # 0x43f620
movsd 0x34fc90(%rip), %xmm0   # 0x43f608
leaq 0x23a449(%rip), %rax    # 0x329dc8
subsd (%rax), %xmm0
movsd %xmm0, 0x34fc8d(%rip)   # 0x43f618
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xef9a5
leaq 0x34fd0c(%rip), %rax    # 0x43f6a8
leaq 0x34fcfd(%rip), %rcx    # 0x43f6a0
jmp 0xef9b9
ucomisd %xmm0, %xmm1
jbe 0xef9d5
leaq 0x34fd06(%rip), %rax    # 0x43f6b8
leaq 0x34fcf7(%rip), %rcx    # 0x43f6b0
movsd (%rcx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rcx)
movsd (%rax), %xmm1
addsd 0x369f(%rip), %xmm1     # 0xf3070
movsd %xmm1, (%rax)
movsd 0x34fd33(%rip), %xmm1   # 0x43f710
leaq 0x239c8c(%rip), %rax    # 0x329670
movsd (%rax), %xmm3
movapd 0x37c0(%rip), %xmm4     # 0xf31b0
xorpd %xmm3, %xmm4
xorpd %xmm2, %xmm2
xorpd %xmm5, %xmm5
cmpltsd %xmm1, %xmm5
andpd %xmm5, %xmm3
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
movslq 0x34fe04(%rip), %rax    # 0x43f818
cmpq %rax, 0x34fccd(%rip)    # 0x43f6e8
leaq 0x3045c(%rip), %rbx     # 0x11fe7e
leaq 0x3045c(%rip), %rcx     # 0x11fe85
cmovleq %rcx, %rbx
cmpq %rax, 0x34fcac(%rip)    # 0x43f6e0
cmovleq %rcx, %rbx
movlpd %xmm5, 0x34fd18(%rip)   # 0x43f758
movsd 0x34fcd0(%rip), %xmm3   # 0x43f718
ucomisd %xmm2, %xmm3
jbe 0xefc0c
ucomisd %xmm1, %xmm2
jb 0xefc0c
callq 0xad3f
movapd %xmm0, %xmm3
subsd 0x34fc1b(%rip), %xmm0   # 0x43f688
movsd %xmm3, 0x34fc0b(%rip)   # 0x43f680
movsd 0x35bb(%rip), %xmm1     # 0xf3038
divsd %xmm1, %xmm0
movsd %xmm0, 0x34fbdf(%rip)   # 0x43f668
movapd %xmm3, %xmm2
subsd 0x34fbfb(%rip), %xmm2   # 0x43f690
divsd %xmm1, %xmm2
movsd %xmm2, 0x34fbf7(%rip)   # 0x43f698
movq 0x34fc38(%rip), %r15    # 0x43f6e0
movslq 0x34fd69(%rip), %rax    # 0x43f818
cmpq %rax, %r15
jle 0xefcd9
cmpq %rax, 0x34fc29(%rip)    # 0x43f6e8
jle 0xefcd9
ucomisd 0x35db(%rip), %xmm0     # 0xf30a8
jbe 0xefd09
movsd 0x34fb95(%rip), %xmm1   # 0x43f670
addsd %xmm0, %xmm1
movsd %xmm1, 0x34fb89(%rip)   # 0x43f670
movsd 0x34fb89(%rip), %xmm1   # 0x43f678
addsd 0x3579(%rip), %xmm1     # 0xf3070
movsd %xmm1, 0x34fb79(%rip)   # 0x43f678
movl 0x34fd0a(%rip), %r14d   # 0x43f810
movl 0x34fc63(%rip), %r12d   # 0x43f770
cmpl %r12d, %r14d
movsd %xmm3, -0x28(%rbp)
jg 0xefb34
leaq 0x34fcb2(%rip), %rdi    # 0x43f7d0
movl %r14d, %esi
callq 0xe7291
movsd -0x28(%rbp), %xmm3
movl %eax, %r14d
movl %eax, 0x34fcdc(%rip)    # 0x43f810
movsd 0x34fb6c(%rip), %xmm1   # 0x43f6a8
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0xefb48
jnp 0xefb64
movsd 0x34fb50(%rip), %xmm2   # 0x43f6a0
divsd %xmm1, %xmm2
mulsd 0x26b84(%rip), %xmm2    # 0x1166e0
movsd %xmm2, 0x34fb8c(%rip)   # 0x43f6f0
movsd 0x34fb4c(%rip), %xmm1   # 0x43f6b8
ucomisd %xmm0, %xmm1
jne 0xefb74
jnp 0xefb90
movsd 0x34fb34(%rip), %xmm0   # 0x43f6b0
divsd %xmm1, %xmm0
mulsd 0x26b58(%rip), %xmm0    # 0x1166e0
movsd %xmm0, 0x34fb68(%rip)   # 0x43f6f8
movl 0x34fc2a(%rip), %esi    # 0x43f7c0
cmpl %r12d, %esi
jg 0xefbc2
movsd 0x34fb4d(%rip), %xmm0   # 0x43f6f0
subsd 0x34fb4d(%rip), %xmm0   # 0x43f6f8
leaq 0x34fbce(%rip), %rdi    # 0x43f780
callq 0xe7291
movsd -0x28(%rbp), %xmm3
movl %eax, 0x34fbfe(%rip)    # 0x43f7c0
andq $0x0, 0x34fae6(%rip)    # 0x43f6b0
andq $0x0, 0x34face(%rip)    # 0x43f6a0
andq $0x0, 0x34fade(%rip)    # 0x43f6b8
andq $0x0, 0x34fac6(%rip)    # 0x43f6a8
cmpl %r12d, %r14d
jl 0xefd09
callq 0xef379
movsd 0x34fa88(%rip), %xmm0   # 0x43f680
movsd %xmm0, 0x34fa88(%rip)   # 0x43f688
leaq 0x30289(%rip), %rbx     # 0x11fe90
jmp 0xefd3c
ucomisd %xmm3, %xmm2
jbe 0xefc71
ucomisd %xmm2, %xmm1
jb 0xefc71
callq 0xad3f
movsd %xmm0, 0x34fa6b(%rip)   # 0x43f690
movsd 0x34f9eb(%rip), %xmm0   # 0x43f618
movsd %xmm0, 0x34fa9b(%rip)   # 0x43f6d0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x34fa92(%rip)    # 0x43f6d8
incq 0x34fa9b(%rip)          # 0x43f6e8
jmp 0xefd3c
movl $0x3, (%r14)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x34fa13(%rip)   # 0x43f678
leaq 0x1f9fb(%rip), %rbx     # 0x10f667
jmp 0xefd3c
movsd 0x34fa57(%rip), %xmm1   # 0x43f6d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x34fa4b(%rip)   # 0x43f6d0
movsd 0x34fa4b(%rip), %xmm2   # 0x43f6d8
addsd 0x33db(%rip), %xmm2     # 0xf3070
movsd %xmm2, 0x34fa3b(%rip)   # 0x43f6d8
ucomisd 0x349b(%rip), %xmm2     # 0xf3140
jne 0xefd3c
jp 0xefd3c
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
divsd 0x347f(%rip), %xmm1     # 0xf3140
movsd %xmm1, 0x34fa07(%rip)   # 0x43f6d0
jbe 0xefde1
movsd %xmm1, 0x34f9e9(%rip)   # 0x43f6c0
jmp 0xefd3c
andq $0x0, 0x34f9cf(%rip)    # 0x43f6b0
andq $0x0, 0x34f9b7(%rip)    # 0x43f6a0
andq $0x0, 0x34f9c7(%rip)    # 0x43f6b8
andq $0x0, 0x34f9af(%rip)    # 0x43f6a8
andq $0x0, 0x34f977(%rip)    # 0x43f678
andq $0x0, 0x34f967(%rip)    # 0x43f670
movsd %xmm3, 0x34f977(%rip)   # 0x43f688
movsd 0x34f8ff(%rip), %xmm0   # 0x43f618
movsd %xmm0, 0x34f9af(%rip)   # 0x43f6d0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x34f9a6(%rip)    # 0x43f6d8
incq %r15
movq %r15, 0x34f9a4(%rip)    # 0x43f6e0
movsd 0x34f924(%rip), %xmm3   # 0x43f668
movsd 0x34f92c(%rip), %xmm0   # 0x43f678
movsd 0x34f944(%rip), %xmm2   # 0x43f698
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
movsd 0x34f954(%rip), %xmm4   # 0x43f6b8
movsd 0x34f98c(%rip), %xmm5   # 0x43f6f8
movsd 0x34f934(%rip), %xmm6   # 0x43f6a8
movsd 0x34f974(%rip), %xmm7   # 0x43f6f0
movl 0x34fa8e(%rip), %eax    # 0x43f810
pushq $0x2
popq %rcx
cltd
idivl %ecx
cltq
leaq 0x34fa3f(%rip), %rdx    # 0x43f7d0
movsd (%rdx,%rax,8), %xmm8
movl 0x34fa23(%rip), %eax    # 0x43f7c0
cltd
idivl %ecx
cltq
leaq 0x34f9d7(%rip), %rcx    # 0x43f780
movsd (%rcx,%rax,8), %xmm9
movsd %xmm9, 0x8(%rsp)
movsd %xmm8, (%rsp)
leaq 0x238d2d(%rip), %rdi    # 0x328af0
leaq 0x300cb(%rip), %rsi     # 0x11fe95
movq %rbx, %rdx
movb $0x8, %al
callq 0x9060
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm1, 0x34f8df(%rip)   # 0x43f6c8
jmp 0xefd3c