///////////////////////////////////////////////////////////////////////////// // Line optimization. ///////////////////////////////////////////////////////////////////////////// double CDiffFunction::LineOpt(const double vx0[], const double vDir[], bool fTrace) { const double Epsilon = 0.00001; const double Big = 10000; // // Normalize vDir for first step // double N2 = 0; for (int i = Dimensions; --i >= 0;) N2 += vDir[i] * vDir[i]; double Scale = 1.0 / std::sqrt(N2); if (Scale == std::numeric_limits<double>::infinity()) return 0.0; // // First, find a bracket, such that: // f(tx[1]) > f(tx[0]) && f(tx[1]) > f(tx[2]) // double tx[3]; double tf[3]; tx[0] = 0; tf[0] = SetLineInput(vx0, vDir, tx[0]); tx[2] = Scale; tf[2] = SetLineInput(vx0, vDir, tx[2]); // // Find a close point that improves the function // while(true) { tx[1] = tx[2] * 0.5; tf[1] = SetLineInput(vx0, vDir, tx[1]); if (tx[1] < Epsilon) return 0.0; if (tf[1] <= tf[0]) { tx[2] = tx[1]; tf[2] = tf[1]; } else break; } // // Find a distant point that is worse // while (tf[1] <= tf[2]) { if (tx[2] > Big) return tx[2]; tx[1] = tx[2]; tf[1] = tf[2]; tx[2] = tx[1] * 2.0; tf[2] = SetLineInput(vx0, vDir, tx[2]); } // // Show the initial bracket // if (fTrace) { for (int i = 0; i < 3; i++) { std::cout << std::setw(12) << vx0[0] + tx[i] * vDir[0]; std::cout << std::setw(12) << tf[i]; } std::cout << '\n'; } // // Do a quadratic regression // double bma = tx[1] - tx[0]; double bmc = tx[1] - tx[2]; double fbmfa = tf[1] - tf[0]; double fbmfc = tf[1] - tf[2]; double x = tx[1] - 0.5 * (bma * bma * fbmfc - bmc * bmc * fbmfa) / (bma * fbmfc - bmc * fbmfa); double f = SetLineInput(vx0, vDir, x); if (fTrace) std::cout << std::setw(12) << vx0[0] + x * vDir[0] << std::setw(12) << f << '\n'; if (f > tf[1]) return x; else return tx[1]; }
//============================================================================== void KeyBoard_Scan(void) { uint8_t i; uint32_t tmp; // if(Sys_Start_Flag == 0)return; if(KeyBoardType.flagfull == 1)return; if(SysTickCnt - KeyBoardType.scanticks < KEYSCANDLY)return; KeyBoardType.scanticks = SysTickCnt; switch(KeyBoardType.status) { case 0: SetRowInput(); KeyBoardType.status++; break; case 1: tmp = GetRowValue(); if(tmp != 0) { KeyBoardType.code = tmp; KeyBoardType.status++; } else { KeyBoardType.code = 0; } KeyBoardType.keycnt = 0; break; case 2: tmp = GetRowValue(); if(tmp == KeyBoardType.code) { if(++KeyBoardType.keycnt > KEYSCANCNT) { KeyBoardType.status++; } } else { KeyBoardType.keycnt = 0; KeyBoardType.code = 0; KeyBoardType.status = 1; } break; case 3: SetLineInput(); KeyBoardType.status++; break; case 4: tmp = GetLineValue(); if(tmp != 0) { KeyBoardType.code += tmp; KeyBoardType.status++; } else { KeyBoardType.code = 0; KeyBoardType.status = 0; } KeyBoardType.keycnt = 0; break; case 5: tmp = GetLineValue(); if(tmp == (KeyBoardType.code&0xf0)) { if(++KeyBoardType.keycnt > KEYSCANCNT) { KeyBoardType.status++; } } else { KeyBoardType.keycnt = 0; KeyBoardType.code = 0; KeyBoardType.status = 0; } break; case 6: for(i = 0;i < sizeof(g_KeyCode);i++) { if(KeyBoardType.code == g_KeyCode[i]) { break; } } if(i >= sizeof(g_KeyCode)) { KeyBoardType.keycnt = 0; KeyBoardType.code = 0; KeyBoardType.status = 0; break; } KeyBoardType.value[KeyBoardType.product] = i; i = (KeyBoardType.product+1)%KEYMAXNUM; if(i == KeyBoardType.consume) { KeyBoardType.flagfull = 1; } else { KeyBoardType.product = i; } KeyBoardType.status++; FlipOutput(LED2); // BUZZER_ON; // SysBlockTick.buzzercnt = BUZZERDLY; break; case 7: if(KeyBoardType.dly++ > KEYRPTDLY/KEYSCANDLY) { KeyBoardType.dly = 0; KeyBoardType.status = 0; KeyBoardType.keycnt = 0; KeyBoardType.code = 0; } break; default:break; } }