// File: /workspace/github_cpp_makefile_star2_O1/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 0x239c64(%rip), %r14    # 0x33f63c
movl (%r14), %eax
cmpq $0x4, %rax
ja 0x105b9e
leaq 0x25ab0(%rip), %rcx     # 0x12b49c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd 0x34fdf3(%rip), %xmm1   # 0x4557f0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
movsd 0x34fdeb(%rip), %xmm0   # 0x4557f8
jbe 0x105a15
ucomisd %xmm0, %xmm2
ja 0x105a34
ucomisd %xmm1, %xmm2
leaq 0x30439(%rip), %rbx     # 0x135e59
jbe 0x105fc4
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x105fc4
movl $0x2, (%r14)
leaq 0x239c1e(%rip), %rax    # 0x33f660
movsd (%rax), %xmm0
mulsd 0x36ea(%rip), %xmm0     # 0x109138
cvttsd2si %xmm0, %eax
movl %eax, 0x34fe9c(%rip)    # 0x4558f4
leaq 0x303fa(%rip), %rbx     # 0x135e59
jmp 0x105fc4
leaq 0x30403(%rip), %rbx     # 0x135e6e
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x105aa0
movl 0x34fe7a(%rip), %eax    # 0x4558f4
testl %eax, %eax
jle 0x105ede
decl %eax
movl %eax, 0x34fe6a(%rip)    # 0x4558f4
jmp 0x105ee5
leaq 0x303cb(%rip), %rbx     # 0x135e61
movl $0x7, %eax
movl $0x2, %ecx
movl %ecx, 0x34fe52(%rip)    # 0x4558f8
movl %eax, 0x34fda4(%rip)    # 0x455850
xorl %r15d, %r15d
movq %r15, 0x34fd12(%rip)    # 0x4557c8
movq %r15, 0x34fd03(%rip)    # 0x4557c0
movq %r15, 0x34fce4(%rip)    # 0x4557a8
movq %r15, 0x34fcd5(%rip)    # 0x4557a0
movq %r15, 0x34fcbe(%rip)    # 0x455790
movq %r15, 0x34fca7(%rip)    # 0x455780
movq %r15, 0x34fcb8(%rip)    # 0x455798
movq %r15, 0x34fca1(%rip)    # 0x455788
movq %r15, 0x34fc6a(%rip)    # 0x455758
movq %r15, 0x34fc83(%rip)    # 0x455778
movq %r15, 0x34fc54(%rip)    # 0x455750
movq %r15, 0x34fc45(%rip)    # 0x455748
callq 0xaea1
movsd %xmm0, 0x34fc60(%rip)   # 0x455770
movsd %xmm0, 0x34fc50(%rip)   # 0x455768
movsd %xmm0, 0x34fc40(%rip)   # 0x455760
movl %r15d, 0x34fd79(%rip)   # 0x4558a0
movl %r15d, 0x34fdc2(%rip)   # 0x4558f0
movq %r15, 0x34fd2b(%rip)    # 0x455860
movq %r15, 0x34fd74(%rip)    # 0x4558b0
leaq 0x239b05(%rip), %rax    # 0x33f648
movsd (%rax), %xmm0
leaq 0x239b2a(%rip), %rcx    # 0x33f678
movsd %xmm0, (%rcx)
movq %r15, (%rax)
leaq 0x239b14(%rip), %rax    # 0x33f670
movsd (%rax), %xmm0
movapd 0x3638(%rip), %xmm1     # 0x1091a0
xorpd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpltsd 0x34fc77(%rip), %xmm2   # 0x4557f0
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movlpd %xmm2, 0x34fcab(%rip)   # 0x455838
callq 0x1052ff
movl $0x5, (%r14)
jmp 0x105fc4
movsd 0x34fb52(%rip), %xmm0   # 0x4556f8
movsd %xmm0, 0x34fb52(%rip)   # 0x455700
movsd 0x34fb32(%rip), %xmm0   # 0x4556e8
leaq 0x23a20b(%rip), %rax    # 0x33fdc8
subsd (%rax), %xmm0
movsd %xmm0, 0x34fb2f(%rip)   # 0x4556f8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x105be3
leaq 0x34fbae(%rip), %rax    # 0x455788
leaq 0x34fb9f(%rip), %rcx    # 0x455780
jmp 0x105bf7
ucomisd %xmm0, %xmm1
jbe 0x105c13
leaq 0x34fba8(%rip), %rax    # 0x455798
leaq 0x34fb99(%rip), %rcx    # 0x455790
movsd (%rcx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rcx)
movsd (%rax), %xmm1
addsd 0x3461(%rip), %xmm1     # 0x109070
movsd %xmm1, (%rax)
movsd 0x34fbd5(%rip), %xmm1   # 0x4557f0
leaq 0x239a4e(%rip), %rax    # 0x33f670
movsd (%rax), %xmm3
movapd 0x3572(%rip), %xmm4     # 0x1091a0
xorpd %xmm3, %xmm4
xorpd %xmm2, %xmm2
xorpd %xmm5, %xmm5
cmpltsd %xmm1, %xmm5
andpd %xmm5, %xmm3
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
movslq 0x34fca6(%rip), %rax    # 0x4558f8
cmpq %rax, 0x34fb6f(%rip)    # 0x4557c8
leaq 0x30219(%rip), %rbx     # 0x135e79
leaq 0x30219(%rip), %rcx     # 0x135e80
cmovleq %rcx, %rbx
cmpq %rax, 0x34fb4e(%rip)    # 0x4557c0
cmovleq %rcx, %rbx
movlpd %xmm5, 0x34fbba(%rip)   # 0x455838
movsd 0x34fb72(%rip), %xmm3   # 0x4557f8
ucomisd %xmm2, %xmm3
jbe 0x105e98
ucomisd %xmm1, %xmm2
jb 0x105e98
callq 0xaea1
movapd %xmm0, %xmm1
subsd 0x34fabd(%rip), %xmm1   # 0x455768
movsd %xmm0, 0x34faad(%rip)   # 0x455760
movsd 0x337d(%rip), %xmm2     # 0x109038
divsd %xmm2, %xmm1
movsd %xmm1, 0x34fa81(%rip)   # 0x455748
movapd %xmm0, %xmm3
subsd 0x34fa9d(%rip), %xmm3   # 0x455770
divsd %xmm2, %xmm3
movsd %xmm3, 0x34fa99(%rip)   # 0x455778
movq 0x34fada(%rip), %rax    # 0x4557c0
movslq 0x34fc0b(%rip), %rcx    # 0x4558f8
cmpq %rcx, %rax
jle 0x105f65
cmpq %rcx, 0x34facb(%rip)    # 0x4557c8
jle 0x105f65
ucomisd 0x339d(%rip), %xmm1     # 0x1090a8
jbe 0x105f91
movsd 0x34fa37(%rip), %xmm2   # 0x455750
addsd %xmm1, %xmm2
movsd %xmm2, 0x34fa2b(%rip)   # 0x455750
movsd 0x34fa2b(%rip), %xmm2   # 0x455758
addsd 0x333b(%rip), %xmm2     # 0x109070
movsd %xmm2, 0x34fa1b(%rip)   # 0x455758
movl 0x34fbad(%rip), %edx    # 0x4558f0
movl 0x34fb07(%rip), %ecx    # 0x455850
cmpl %ecx, %edx
jg 0x105d92
cmpl $0x5f, %edx
jg 0x105d8c
movslq %edx, %rdi
leaq 0x34fb54(%rip), %rsi    # 0x4558b0
movsd %xmm1, (%rsi,%rdi,8)
testl %edx, %edx
jle 0x105d8a
leaq 0x1(%rdx), %rdi
movsd -0x10(%rsi,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x105d8a
movsd %xmm2, -0x8(%rsi,%rdi,8)
movsd %xmm1, -0x10(%rsi,%rdi,8)
decq %rdi
cmpq $0x1, %rdi
jg 0x105d69
incl %edx
movl %edx, 0x34fb5e(%rip)    # 0x4558f0
movsd 0x34f9ee(%rip), %xmm2   # 0x455788
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jne 0x105da6
jnp 0x105dc2
movsd 0x34f9d2(%rip), %xmm3   # 0x455780
divsd %xmm2, %xmm3
mulsd 0x2691e(%rip), %xmm3    # 0x12c6d8
movsd %xmm3, 0x34fa0e(%rip)   # 0x4557d0
movsd 0x34f9ce(%rip), %xmm2   # 0x455798
ucomisd %xmm1, %xmm2
jne 0x105dd2
jnp 0x105dee
movsd 0x34f9b6(%rip), %xmm1   # 0x455790
divsd %xmm2, %xmm1
mulsd 0x268f2(%rip), %xmm1    # 0x12c6d8
movsd %xmm1, 0x34f9ea(%rip)   # 0x4557d8
movl 0x34faac(%rip), %edx    # 0x4558a0
cmpl %ecx, %edx
jg 0x105e4d
cmpl $0x5f, %edx
jg 0x105e47
movsd 0x34f9cb(%rip), %xmm1   # 0x4557d0
subsd 0x34f9cb(%rip), %xmm1   # 0x4557d8
movslq %edx, %rdi
leaq 0x34fa49(%rip), %rsi    # 0x455860
movsd %xmm1, (%rsi,%rdi,8)
testl %edx, %edx
jle 0x105e45
leaq 0x1(%rdx), %rdi
movsd -0x10(%rsi,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x105e45
movsd %xmm2, -0x8(%rsi,%rdi,8)
movsd %xmm1, -0x10(%rsi,%rdi,8)
decq %rdi
cmpq $0x1, %rdi
jg 0x105e24
incl %edx
movl %edx, 0x34fa53(%rip)    # 0x4558a0
xorl %edx, %edx
movq %rdx, 0x34f93a(%rip)    # 0x455790
movq %rdx, 0x34f923(%rip)    # 0x455780
movq %rdx, 0x34f934(%rip)    # 0x455798
movq %rdx, 0x34f91d(%rip)    # 0x455788
cmpl %ecx, 0x34fa7f(%rip)    # 0x4558f0
jl 0x105f91
callq 0x1055ab
movsd 0x34f8dc(%rip), %xmm0   # 0x455760
movsd %xmm0, 0x34f8dc(%rip)   # 0x455768
leaq 0x2fff8(%rip), %rbx     # 0x135e8b
jmp 0x105fc4
ucomisd %xmm3, %xmm2
jbe 0x105f01
ucomisd %xmm2, %xmm1
jb 0x105f01
callq 0xaea1
movsd %xmm0, 0x34f8bf(%rip)   # 0x455770
movsd 0x34f83f(%rip), %xmm0   # 0x4556f8
movsd %xmm0, 0x34f8ef(%rip)   # 0x4557b0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x34f8e6(%rip)    # 0x4557b8
incq 0x34f8ef(%rip)          # 0x4557c8
jmp 0x105fc4
movl $0x3, (%r14)
cvtsi2sdl 0x34fa07(%rip), %xmm0 # 0x4558f4
movsd %xmm0, 0x34f863(%rip)   # 0x455758
leaq 0x1f743(%rip), %rbx     # 0x12563f
jmp 0x105fc4
movsd 0x34f8a7(%rip), %xmm1   # 0x4557b0
addsd %xmm0, %xmm1
movsd %xmm1, 0x34f89b(%rip)   # 0x4557b0
movsd 0x34f89b(%rip), %xmm2   # 0x4557b8
addsd 0x314b(%rip), %xmm2     # 0x109070
movsd %xmm2, 0x34f88b(%rip)   # 0x4557b8
ucomisd 0x3203(%rip), %xmm2     # 0x109138
jne 0x105fc4
jp 0x105fc4
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
divsd %xmm2, %xmm1
movsd %xmm1, 0x34f85b(%rip)   # 0x4557b0
jbe 0x106072
movsd %xmm1, 0x34f83d(%rip)   # 0x4557a0
jmp 0x105fc4
xorl %ecx, %ecx
movq %rcx, 0x34f822(%rip)    # 0x455790
movq %rcx, 0x34f80b(%rip)    # 0x455780
movq %rcx, 0x34f81c(%rip)    # 0x455798
movq %rcx, 0x34f805(%rip)    # 0x455788
movq %rcx, 0x34f7ce(%rip)    # 0x455758
movq %rcx, 0x34f7bf(%rip)    # 0x455750
movsd %xmm0, 0x34f7cf(%rip)   # 0x455768
movsd 0x34f757(%rip), %xmm0   # 0x4556f8
movsd %xmm0, 0x34f807(%rip)   # 0x4557b0
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x34f7fe(%rip)    # 0x4557b8
incq %rax
movq %rax, 0x34f7fc(%rip)    # 0x4557c0
movsd 0x34f77c(%rip), %xmm3   # 0x455748
movsd 0x34f784(%rip), %xmm0   # 0x455758
movsd 0x34f79c(%rip), %xmm2   # 0x455778
movapd %xmm3, %xmm1
subsd %xmm2, %xmm1
movsd 0x34f7ac(%rip), %xmm4   # 0x455798
movsd 0x34f7e4(%rip), %xmm5   # 0x4557d8
movsd 0x34f78c(%rip), %xmm6   # 0x455788
movsd 0x34f7cc(%rip), %xmm7   # 0x4557d0
movl 0x34f8e6(%rip), %eax    # 0x4558f0
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x34f893(%rip), %rcx    # 0x4558b0
movsd (%rcx,%rax,8), %xmm8
movl 0x34f877(%rip), %eax    # 0x4558a0
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rax
leaq 0x34f824(%rip), %rcx    # 0x455860
movsd (%rcx,%rax,8), %xmm9
movsd %xmm9, 0x8(%rsp)
movsd %xmm8, (%rsp)
leaq 0x238a9a(%rip), %rdi    # 0x33eaf0
leaq 0x2fe33(%rip), %rsi     # 0x135e90
movq %rbx, %rdx
movb $0x8, %al
callq 0x9060
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm1, 0x34f72e(%rip)   # 0x4557a8
jmp 0x105fc4