distribution-201229

Dependencies:   mbed FastPWM

main.cpp

Committer:
Lightvalve
Date:
2020-04-14
Revision:
65:a2d7c63419c2
Parent:
64:6f1caa0993d0
Child:
66:a8e6799dbce3

File content as of revision 65:a2d7c63419c2:

#include "mbed.h"
#include "FastPWM.h"
#include "INIT_HW.h"
#include "function_CAN.h"
#include "SPI_EEP_ENC.h"
#include "I2C_AS5510.h"
#include "setting.h"
#include "function_utilities.h"
#include "stm32f4xx_flash.h"
#include "FlashWriter.h"      

using namespace std;
Timer t;

///191008////

// dac & check ///////////////////////////////////////////
DigitalOut check(PC_2);
DigitalOut check_2(PC_3);
AnalogOut dac_1(PA_4);
AnalogOut dac_2(PA_5);
AnalogIn adc1(PC_4); //pressure_1
AnalogIn adc2(PB_0); //pressure_2
AnalogIn adc3(PC_1); //current


// PWM ///////////////////////////////////////////
float dtc_v=0.0f;
float dtc_w=0.0f;

// I2C ///////////////////////////////////////////
I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
const int i2c_slave_addr1 =  0x56;
unsigned int value; // 10bit output of reading sensor AS5510

// SPI ///////////////////////////////////////////
//SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
//DigitalOut eeprom_cs(PB_12);
//FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
SPI enc(PC_12,PC_11,PC_10);
DigitalOut enc_cs(PD_2);
DigitalOut LED(PA_15);

// UART ///////////////////////////////////////////
Serial pc(PA_9,PA_10); //  _ UART

// CAN ///////////////////////////////////////////
CAN can(PB_8, PB_9, 1000000);
CANMessage msg;
void onMsgReceived()
{
    CAN_RX_HANDLER();
}

// Variables ///////////////////////////////////////////
State pos;
State vel;
State Vout;
State torq;
State pres_A;
State pres_B;
State cur;
State valve_pos;

State INIT_Vout;
State INIT_Valve_Pos;
State INIT_Pos;
State INIT_torq;

extern int CID_RX_CMD;
extern int CID_RX_REF_POSITION;
extern int CID_RX_REF_VALVE_POS;
extern int CID_RX_REF_PWM;

extern int CID_TX_INFO;
extern int CID_TX_POSITION;
extern int CID_TX_TORQUE;
extern int CID_TX_PRES;
extern int CID_TX_VOUT;
extern int CID_TX_VALVE_POSITION;




// =============================================================================
// =============================================================================
// =============================================================================

/*******************************************************************************
 *  REFERENCE MODE
 ******************************************************************************/
enum _REFERENCE_MODE {
    MODE_REF_NO_ACT = 0,                                //0
    MODE_REF_DIRECT,                                //1
    MODE_REF_COS_INC,                                  //2
    MODE_REF_LINE_INC,                                 //3
    MODE_REF_SIN_WAVE,                                  //4
    MODE_REF_SQUARE_WAVE,                                  //5
};

/*******************************************************************************
 *  CONTROL MODE
 ******************************************************************************/
enum _CONTROL_MODE {
    //control mode
    MODE_NO_ACT = 0,                                    //0
    MODE_VALVE_POSITION_CONTROL,                        //1
    MODE_JOINT_CONTROL,                                 //2

    MODE_VALVE_OPEN_LOOP,                               //3
    MODE_JOINT_POSITION_TORQUE_CONTROL_VALVE_POSITION,  //4
    MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING,        //5

    MODE_JOINT_POSITION_PRES_CONTROL_PWM,               //6
    MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION,    //7
    MODE_VALVE_POSITION_PRES_CONTROL_LEARNING,          //8

    MODE_TEST_CURRENT_CONTROL,                          //9
    MODE_TEST_PWM_CONTROL,                              //10

    MODE_CURRENT_CONTROL,                               //11
    MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT,         //12
    MODE_JOINT_POSITION_PRES_CONTROL_CURRENT,           //13

    //utility
    MODE_TORQUE_SENSOR_NULLING = 20,                    //20
    MODE_VALVE_NULLING_AND_DEADZONE_SETTING,            //21
    MODE_FIND_HOME,                                     //22
    MODE_VALVE_GAIN_SETTING,                            //23
    MODE_PRESSURE_SENSOR_NULLING,                       //24
    MODE_PRESSURE_SENSOR_CALIB,                         //25
    MODE_ROTARY_FRICTION_TUNING,                        //26

    MODE_DDV_POS_VS_PWM_ID = 30,                           //30
    MODE_DDV_DEADZONE_AND_CENTER,                       //31
    MODE_DDV_POS_VS_FLOWRATE,                           //32
    MODE_SYSTEM_ID,                                     //33
};

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;//8
  RCC_OscInitStruct.PLL.PLLN = 180; //180
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    //Error_Handler();
  }
  /** Activate the Over-Drive mode 
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    //Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    //Error_Handler();
  }
}

const float input[22] = { 0.052414f, 0.066907f, 0.085407f, 0.109023f,
        0.139169f, 0.177651f, 0.052414f, 0.066907f, 0.085407f, 0.109023f,
        0.139169f, 0.177651f, 0.052414f, 0.066907f, 0.085407f, 0.109023f,
        0.139169f, 0.177651f, 0.052414f, 0.066907f, 0.085407f, 0.109023f };

const float h1[22][32] = { { -0.02454003319144249f, 0.13071660697460175f,
        0.08675816655158997f, 0.1402530074119568f, -0.24982963502407074f,
        0.12202739715576172f, -0.13030944764614105f, -0.11353740841150284f,
        -0.2542628347873688f, -0.04588286578655243f, -0.14282149076461792f,
        -0.2201535552740097f, -0.18691661953926086f, 0.30389177799224854f,
        -0.1729012429714203f, -0.12240276485681534f, -0.11355798691511154f,
        0.2349482625722885f, -0.24878336489200592f, 0.07184267044067383f,
        -0.3221011161804199f, 0.08763179183006287f, -0.27257153391838074f,
        -0.06490874290466309f, -0.06682667881250381f, 0.1505383551120758f,
        -0.14478006958961487f, -0.07367490231990814f, 0.23569869995117188f,
        -0.1539779156446457f, -0.1400003880262375f, 0.051764924079179764f }, {
        -0.19842876493930817f, 0.18337926268577576f, 0.24467620253562927f,
        -0.29177823662757874f, -0.08481606096029282f, 0.09983056783676147f,
        0.17972804605960846f, -0.07474377751350403f, -0.1711931675672531f,
        0.12607872486114502f, -0.21832025051116943f, -0.1755477339029312f,
        -0.010855759494006634f, 0.2378554791212082f, 0.03758585453033447f,
        -0.13797838985919952f, -0.15582887828350067f, 0.08006728440523148f,
        0.280606746673584f, 0.28338202834129333f, -0.06711727380752563f,
        -0.09894979000091553f, -0.07788881659507751f, 0.22705963253974915f,
        -0.0012174372095614672f, 0.240607351064682f, 0.1039358377456665f,
        0.18501906096935272f, -0.03155122324824333f, 0.08036407828330994f,
        -0.20284514129161835f, 0.06699042022228241f }, { 0.14068114757537842f,
        0.13828633725643158f, -0.0034593045711517334f, 0.1392701119184494f,
        0.01057615876197815f, -0.007689331658184528f, 0.07328304648399353f,
        -0.14162643253803253f, -0.04415355995297432f, 0.09164466708898544f,
        0.24845272302627563f, -0.05588914453983307f, -0.054479751735925674f,
        0.24456734955310822f, -0.2598671317100525f, -0.15801946818828583f,
        0.2817283868789673f, -0.19191598892211914f, 0.15788163244724274f,
        0.28144943714141846f, 0.14735294878482819f, -0.12544888257980347f,
        0.11103320121765137f, -0.20203278958797455f, 0.0017974432557821274f,
        0.1191059947013855f, -0.12710560858249664f, 0.1702873408794403f,
        -0.09684925526380539f, 0.2480504810810089f, -0.28676652908325195f,
        -0.17398564517498016f }, { 0.04844924807548523f, -0.06973694264888763f,
        -0.1682211458683014f, -0.3513043522834778f, -0.22794683277606964f,
        0.08840374648571014f, 0.026485737413167953f, -0.3492684066295624f,
        0.1813228577375412f, 0.06481858342885971f, 0.11471711844205856f,
        0.1854662299156189f, -0.12381665408611298f, 0.35136616230010986f,
        -0.10501053929328918f, -0.016016438603401184f, 0.03148011490702629f,
        -0.17074286937713623f, -0.1689852625131607f, 0.1171770691871643f,
        -0.23076696693897247f, -0.07204706966876984f, -0.12052477896213531f,
        -0.23881052434444427f, -0.004284285008907318f, 0.11600598692893982f,
        0.10962119698524475f, -0.012203623540699482f, 0.33345603942871094f,
        0.1084047257900238f, 0.015447823330760002f, 0.1315833479166031f }, {
        0.12315890938043594f, 0.16742177307605743f, 0.0689031183719635f,
        -0.29923000931739807f, -0.10478553920984268f, 0.0034702986013144255f,
        -0.06286507844924927f, -0.3965417146682739f, -0.24317654967308044f,
        0.2535353899002075f, -0.18702425062656403f, 0.2042631357908249f,
        0.14613179862499237f, 0.2476590871810913f, -0.20543844997882843f,
        0.15613335371017456f, -0.024847690016031265f, -0.12820857763290405f,
        0.13289833068847656f, -0.033144671469926834f, 0.09313736855983734f,
        0.19325080513954163f, -0.20106908679008484f, -0.061102136969566345f,
        -0.028910696506500244f, -0.2806609869003296f, 0.033241331577301025f,
        -0.26521533727645874f, 0.33587634563446045f, -0.13370811939239502f,
        0.21700626611709595f, 0.008141759783029556f }, { 0.3156174421310425f,
        0.033245913684368134f, 0.24999621510505676f, 0.14565978944301605f,
        0.02309553697705269f, -0.05169738829135895f, -0.15119729936122894f,
        -0.10350330173969269f, -0.3457512855529785f, 0.03809545189142227f,
        0.25342249870300293f, -0.19578632712364197f, -0.05333413928747177f,
        0.09492939710617065f, 0.17943906784057617f, -0.21144242584705353f,
        -0.1867227405309677f, -0.23938390612602234f, -0.2632395327091217f,
        0.06656297296285629f, 0.0961865782737732f, -0.00792744755744934f,
        0.10312435030937195f, 0.27112916111946106f, -0.12544961273670197f,
        0.17053177952766418f, -0.23970040678977966f, 0.16830605268478394f,
        0.18481910228729248f, -0.0010759234428405762f, 0.049939122051000595f,
        -0.059725455939769745f }, { 0.021065108478069305f,
        -0.17225059866905212f, 0.1334478259086609f, 0.09036531299352646f,
        0.22687482833862305f, 0.03710803762078285f, -0.1765347123146057f,
        0.0036233423743396997f, -0.17170532047748566f, -0.11777474731206894f,
        -0.054618652909994125f, -0.17656885087490082f, 0.052830975502729416f,
        0.10441931337118149f, -0.14971962571144104f, -0.24781031906604767f,
        -0.023929525166749954f, -0.04490154981613159f, 0.2847473621368408f,
        0.014796234667301178f, -0.14429597556591034f, -0.14511936902999878f,
        -0.21516084671020508f, -0.20089882612228394f, -0.10562898963689804f,
        -0.12727710604667664f, -0.09228193759918213f, -0.08893769234418869f,
        -0.12074583023786545f, -0.028564512729644775f, 0.008117200806736946f,
        0.08297917991876602f }, { 0.19532811641693115f, -0.05014912784099579f,
        0.13636812567710876f, 0.048916492611169815f, 0.26100409030914307f,
        -0.17881926894187927f, 0.012918300926685333f, 0.06905974447727203f,
        0.16022373735904694f, 0.06287040561437607f, -0.1698886901140213f,
        -0.3164767324924469f, 0.06106283515691757f, 0.05603136122226715f,
        0.04491907358169556f, 0.16258767247200012f, 0.06191227212548256f,
        -0.2078513652086258f, -0.26503077149391174f, 0.019191648811101913f,
        -0.06990422308444977f, 0.018576622009277344f, -0.10467308759689331f,
        -0.023303329944610596f, 0.11910765618085861f, 0.014545410871505737f,
        -0.2797531187534332f, -0.014093500562012196f, -0.09087783098220825f,
        0.003536820411682129f, -0.08422576636075974f, 0.20006318390369415f }, {
        0.03522654250264168f, -0.15291047096252441f, 0.11232474446296692f,
        0.08317171782255173f, 0.01103570032864809f, -0.23718038201332092f,
        -0.043577663600444794f, -0.10673496872186661f, -0.08802048116922379f,
        0.06057487800717354f, -0.2207462638616562f, 0.21330848336219788f,
        0.06867198646068573f, 0.31874582171440125f, 0.19785529375076294f,
        0.11854913830757141f, 0.0048002502880990505f, 0.07107115536928177f,
        0.1471722573041916f, -0.021459000185132027f, -0.25434109568595886f,
        -0.01829427480697632f, -0.2427632212638855f, 0.06919881701469421f,
        -0.2938772141933441f, -0.06733335554599762f, -0.07296852767467499f,
        0.21985474228858948f, 0.27857765555381775f, -0.09769320487976074f,
        -0.2202656865119934f, 0.06659334897994995f }, { -0.18176554143428802f,
        0.05808980390429497f, -0.2002866566181183f, 0.2209930419921875f,
        0.22003944218158722f, -0.12194468080997467f, 0.16832226514816284f,
        -0.32419517636299133f, -0.31987497210502625f, 0.020202845335006714f,
        -0.2701215445995331f, 0.15670055150985718f, 0.04660305753350258f,
        0.2134874314069748f, -0.21944394707679749f, -0.25474902987480164f,
        0.27780312299728394f, 0.28777045011520386f, 0.10909196734428406f,
        -0.06471110135316849f, -0.1670646369457245f, 0.13422733545303345f,
        -0.09569443762302399f, -0.2635143995285034f, -0.14163701236248016f,
        0.04615262150764465f, 0.11132439970970154f, 0.23280498385429382f,
        0.23840151727199554f, 0.24882283806800842f, 0.0529453419148922f,
        0.13884255290031433f }, { -0.10154614597558975f, -0.21454781293869019f,
        -0.05455450713634491f, 0.2060181051492691f, -0.24331730604171753f,
        -0.18322165310382843f, 0.08804970234632492f, -0.09168499708175659f,
        -0.3127654492855072f, -0.20708662271499634f, -0.26258623600006104f,
        -0.2734098434448242f, -0.1649116575717926f, 0.33747056126594543f,
        0.014040976762771606f, 0.23592090606689453f, -0.13581429421901703f,
        -0.1899891048669815f, 0.19671878218650818f, -0.23027168214321136f,
        0.14086498320102692f, -0.20761027932167053f, 0.19642561674118042f,
        0.00928562879562378f, -0.07803404331207275f, 0.08413159847259521f,
        0.06599533557891846f, -0.2540142834186554f, 0.209262415766716f,
        0.0242612361907959f, -0.008453809656202793f, 0.13509076833724976f }, {
        0.19965791702270508f, 0.15909932553768158f, -0.2523933947086334f,
        -0.024008844047784805f, -0.3288625478744507f, -0.3593894839286804f,
        -0.10659854859113693f, -0.09668480604887009f, -0.25964510440826416f,
        0.24553023278713226f, 0.2584691643714905f, -0.13047806918621063f,
        -0.045685749500989914f, -0.027066625654697418f, -0.24520277976989746f,
        0.2635043263435364f, 0.06614753603935242f, -0.14302769303321838f,
        -0.22128811478614807f, -0.2489294409751892f, 0.2430250197649002f,
        0.1864563524723053f, -0.19150227308273315f, 0.26879218220710754f,
        -0.3449307382106781f, 0.1727219521999359f, 0.2771519124507904f,
        0.08954040706157684f, 0.2631568908691406f, 0.12126106023788452f,
        0.12375504523515701f, -0.054388467222452164f }, { 0.19592446088790894f,
        -0.34142163395881653f, 0.15802547335624695f, 0.18388450145721436f,
        0.031184343621134758f, -0.08831250667572021f, -0.20661622285842896f,
        0.06958860903978348f, -0.161506786942482f, 0.25045934319496155f,
        -0.10389973968267441f, 0.21004287898540497f, -0.2358747273683548f,
        -0.1947477161884308f, 0.05316495895385742f, -0.0015946561470627785f,
        -0.15815018117427826f, 0.1855827420949936f, 0.01572786457836628f,
        -0.2823503911495209f, -0.19912849366664886f, 0.022287487983703613f,
        0.01019778847694397f, -0.002432197332382202f, 0.1125744953751564f,
        0.024761438369750977f, 0.28052565455436707f, 0.06848252564668655f,
        0.07588296383619308f, -0.2828674018383026f, -0.1949537843465805f,
        0.0682394951581955f }, { -0.06400725990533829f, -0.039301756769418716f,
        0.05148342251777649f, 0.15645751357078552f, -0.015306415967643261f,
        -0.027875764295458794f, 0.19478058815002441f, -0.1143186166882515f,
        -0.11950938403606415f, 0.21234765648841858f, 0.2555554211139679f,
        -0.05989915132522583f, 0.014815526083111763f, 0.12010202556848526f,
        0.08785843849182129f, 0.0018573424313217402f, -0.014310447499155998f,
        0.08359912782907486f, -0.05963675677776337f, 0.08984222263097763f,
        -0.17661209404468536f, -0.01255887746810913f, 0.09742525219917297f,
        -0.2715410590171814f, 0.019773131236433983f, -0.12170138955116272f,
        0.21204951405525208f, -0.0392717681825161f, 0.22050541639328003f,
        0.1981203556060791f, -0.12718753516674042f, 0.18615314364433289f }, {
        0.207803875207901f, -0.10612393915653229f, 0.2099064588546753f,
        0.048610176891088486f, -0.18356028199195862f, 0.05922550708055496f,
        0.09807947278022766f, 0.05448151379823685f, 0.00759892538189888f,
        0.008583632297813892f, -0.02326703630387783f, 0.03895118832588196f,
        0.142264261841774f, -0.17807096242904663f, 0.03243169188499451f,
        0.29156458377838135f, 0.16580337285995483f, -0.04512497037649155f,
        0.24243946373462677f, 0.19388025999069214f, 0.2613176107406616f,
        0.21572408080101013f, 0.021654754877090454f, 0.19459950923919678f,
        -0.12517954409122467f, 0.1592874825000763f, 0.1632046103477478f,
        -0.2606440782546997f, -0.08548226952552795f, -0.08548004925251007f,
        -0.13906358182430267f, -0.16921734809875488f }, { -0.15630532801151276f,
        0.13156533241271973f, -0.11297933757305145f, 0.12861967086791992f,
        -0.19767631590366364f, 0.1394348293542862f, -0.23108699917793274f,
        0.0800437331199646f, -0.21714667975902557f, 0.08518000692129135f,
        -0.25191619992256165f, 0.23131662607192993f, 0.03946883976459503f,
        -0.09874357283115387f, 0.2303425371646881f, -0.19213639199733734f,
        0.1962396651506424f, -0.10008331388235092f, -0.19810836017131805f,
        -0.2060118019580841f, 0.14884798228740692f, 0.05755701661109924f,
        -0.10641518235206604f, 0.0359015166759491f, 0.1892276555299759f,
        -0.10104718804359436f, -0.10194158554077148f, 0.0636373832821846f,
        -0.0030684147495776415f, 0.22902312874794006f, 0.13050015270709991f,
        0.13367223739624023f }, { 0.17304399609565735f, -0.11803605407476425f,
        0.1952093541622162f, -0.058706384152173996f, 0.07460742443799973f,
        -0.08487367630004883f, 0.2371104210615158f, -0.27557069063186646f,
        0.20663458108901978f, 0.1713942438364029f, -0.27339255809783936f,
        -0.041794922202825546f, 0.0006373148644343019f, 0.022903289645910263f,
        -0.03237643837928772f, 0.1001778319478035f, 0.1372859627008438f,
        0.2195289433002472f, 0.1444065421819687f, -0.16924221813678741f,
        0.17550763487815857f, 0.24889472126960754f, -0.005206882953643799f,
        -0.09046423435211182f, -0.19790777564048767f, -0.23666583001613617f,
        -0.04738515615463257f, 0.19731944799423218f, 0.18761177361011505f,
        -0.12751927971839905f, -0.307704359292984f, -0.1543550342321396f }, {
        0.32603657245635986f, -0.3041064739227295f, 0.25856587290763855f,
        -0.2557964324951172f, -0.3466465473175049f, -0.3196622133255005f,
        0.12455102801322937f, -0.11209218949079514f, 0.001982415560632944f,
        0.06973601132631302f, 0.14987079799175262f, -0.20265568792819977f,
        0.21144899725914001f, -0.10064118355512619f, 0.09263011813163757f,
        0.2236812561750412f, 0.11661216616630554f, 0.06638764590024948f,
        -0.09710270911455154f, -0.1529141068458557f, 0.2093910574913025f,
        -0.21584033966064453f, 0.008751243352890015f, 0.06805345416069031f,
        -0.2336517572402954f, 0.01942119002342224f, 0.08388552069664001f,
        0.07689709961414337f, -0.1510092169046402f, 0.2813257873058319f,
        -0.2768191695213318f, -0.037080224603414536f }, {
        -0.003318269969895482f, 0.08421637862920761f, -0.016360998153686523f,
        -0.12425577640533447f, -0.35029125213623047f, 0.16315333545207977f,
        0.1867244839668274f, 0.04439976066350937f, -0.1956385225057602f,
        0.12546296417713165f, 0.14696259796619415f, 0.031027860939502716f,
        0.13122601807117462f, 0.28269803524017334f, 0.09633612632751465f,
        -0.15478374063968658f, 0.22323313355445862f, -0.0710759162902832f,
        -0.27780935168266296f, 0.053979892283678055f, -0.015637023374438286f,
        -0.1954222172498703f, -0.0505031943321228f, 0.24318388104438782f,
        -0.2976081073284149f, -0.10191759467124939f, -0.07833482325077057f,
        -0.288814902305603f, 0.040884505957365036f, 0.138712078332901f,
        0.20733070373535156f, -0.29824215173721313f }, { -0.18242500722408295f,
        -0.3011151850223541f, -0.18937499821186066f, 0.21996764838695526f,
        0.08294257521629333f, 0.19795776903629303f, -0.2721400856971741f,
        -0.2665696442127228f, -0.1749664843082428f, -0.11208948493003845f,
        -0.12227600067853928f, 0.13757038116455078f, 0.15364645421504974f,
        -0.1376083493232727f, 0.14986392855644226f, 0.27994680404663086f,
        0.11594970524311066f, -0.0993688553571701f, 0.08206108957529068f,
        -0.30797436833381653f, 0.1478293389081955f, 0.029855877161026f,
        0.037324488162994385f, -0.16034488379955292f, -0.22942794859409332f,
        -0.07402129471302032f, 0.01556810736656189f, 0.07635907083749771f,
        0.1468832641839981f, 0.2229032814502716f, 0.2030598223209381f,
        0.011082095094025135f }, { -0.031403448432683945f, -0.179839089512825f,
        -0.061174169182777405f, 0.08824600279331207f, -0.05152688920497894f,
        0.09737619012594223f, -0.019338518381118774f, -0.149160236120224f,
        -0.17775475978851318f, 0.20135465264320374f, -0.26330751180648804f,
        -0.20573703944683075f, -0.18354615569114685f, 0.2306104153394699f,
        -0.12902851402759552f, -0.32519760727882385f, -0.18613660335540771f,
        -0.04431162402033806f, 0.07692430913448334f, 0.2852676808834076f,
        0.18445321917533875f, -0.1602698266506195f, 0.1428212821483612f,
        0.24115940928459167f, -0.15170736610889435f, 0.2710312306880951f,
        -0.08893628418445587f, 0.08192634582519531f, 0.1068575456738472f,
        -0.2417295277118683f, 0.018182098865509033f, 0.011811523698270321f }, {
        0.1185283288359642f, -0.1314961016178131f, -0.2746659219264984f,
        0.018920723348855972f, 0.11404602974653244f, 0.14093975722789764f,
        -0.1613728106021881f, 0.010870097205042839f, 0.05537481606006622f,
        -0.1315172165632248f, -0.18558558821678162f, -0.12937135994434357f,
        -0.20973187685012817f, 0.1692427098751068f, -0.045904770493507385f,
        0.1710028052330017f, 0.01777065359055996f, 0.29331231117248535f,
        -0.12049029022455215f, 0.04678349569439888f, -0.25235429406166077f,
        -0.033619701862335205f, -0.042226120829582214f, -0.27914759516716003f,
        0.006846277974545956f, 0.08922392129898071f, 0.171451598405838f,
        0.050000399351119995f, -0.04939000681042671f, -0.2795959711074829f,
        0.26425138115882874f, -0.2566368877887726f }, };

const float h2[32][32] = { { -0.13349971175193787f, -0.10295046865940094f,
        -0.04925942420959473f, 0.005893866065889597f, -0.12596699595451355f,
        0.2987118661403656f, 0.07975713163614273f, 0.06396704912185669f,
        -0.30411389470100403f, -0.10008957982063293f, 0.014623122289776802f,
        -0.0538821816444397f, 0.048359375447034836f, 0.24325567483901978f,
        0.10184130072593689f, -0.24775750935077667f, 0.07803884148597717f,
        -0.21483641862869263f, 0.17120853066444397f, 0.00830860622227192f,
        -0.29617825150489807f, -0.22132611274719238f, -0.23983746767044067f,
        -0.28275105357170105f, -0.3873054087162018f, 0.30998873710632324f,
        -0.4226490557193756f, -0.11779281497001648f, 0.23828622698783875f,
        0.11771553754806519f, -0.37041905522346497f, -0.30605563521385193f }, {
        -0.16083313524723053f, 0.002147972583770752f, -0.04321768879890442f,
        0.20690490305423737f, 0.022892558947205544f, 0.008302271366119385f,
        -0.20543429255485535f, -0.08731026947498322f, 0.24677053093910217f,
        0.048597872257232666f, 0.20651067793369293f, 0.16381694376468658f,
        0.28672969341278076f, -0.3506166338920593f, 0.06659439951181412f,
        0.04734180495142937f, 0.03125026077032089f, -0.2737269401550293f,
        -0.21843840181827545f, 0.3144419193267822f, 0.10113385319709778f,
        0.14034439623355865f, -0.16832955181598663f, 0.33271604776382446f,
        0.19579239189624786f, 0.14696596562862396f, 0.06076221913099289f,
        0.24354417622089386f, -0.28620439767837524f, -0.0511949360370636f,
        0.2096933126449585f, 0.040742650628089905f }, { -0.1579037755727768f,
        0.28447648882865906f, -0.1789936125278473f, 0.20799604058265686f,
        0.025723248720169067f, 0.17740526795387268f, -0.1666354387998581f,
        -0.06060470640659332f, -0.017253071069717407f, -0.2790656089782715f,
        -0.16800916194915771f, -0.2990717589855194f, 0.07126197218894958f,
        -0.08032660186290741f, -0.1799015998840332f, -0.1891922950744629f,
        0.2556300461292267f, 0.2080206573009491f, -0.24888339638710022f,
        0.27691522240638733f, -0.21319638192653656f, -0.14012537896633148f,
        -0.30466505885124207f, -0.013728171586990356f, -0.1629670113325119f,
        0.2530798614025116f, 0.0633285641670227f, 0.0011726915836334229f,
        -0.14820529520511627f, 0.2823629677295685f, 0.10884168744087219f,
        -0.2637729346752167f }, { -0.12927524745464325f, -0.01806354522705078f,
        -0.23058539628982544f, -0.030255556106567383f, -0.6437712907791138f,
        -0.17983144521713257f, 0.12746267020702362f, 0.08151327818632126f,
        -0.06944303214550018f, 0.09271347522735596f, -0.0808296874165535f,
        -0.24855418503284454f, -0.11665885150432587f, 0.12946322560310364f,
        -0.0930287167429924f, -0.3423658609390259f, 0.10996055603027344f,
        0.24023392796516418f, 0.2942558825016022f, -0.17709596455097198f,
        -0.1255558878183365f, -0.2134709507226944f, -0.06394433975219727f,
        -0.24457648396492004f, 0.10029968619346619f, -0.2400282919406891f,
        0.11666780710220337f, -0.14067168533802032f, -0.22396358847618103f,
        0.2546447217464447f, -0.002586635062471032f, 0.04970479756593704f }, {
        -0.18258020281791687f, 0.2506926953792572f, -0.0804666131734848f,
        0.05040470138192177f, 0.02259594015777111f, -0.23578980565071106f,
        -0.09264150261878967f, 0.0013416205765679479f, 0.043158918619155884f,
        -0.2690317630767822f, 0.04383622854948044f, -0.25736406445503235f,
        -0.21347089111804962f, -0.19318343698978424f, -0.21244710683822632f,
        -0.13572444021701813f, -0.1285906583070755f, 0.15626338124275208f,
        -0.050212472677230835f, 0.0982573851943016f, 0.11135826259851456f,
        0.06453899294137955f, 0.19172045588493347f, -0.22890380024909973f,
        -0.12237071990966797f, 0.09743352234363556f, 0.10070990025997162f,
        0.28772199153900146f, 0.2695164978504181f, -0.16498444974422455f,
        0.014173826202750206f, -0.2047310173511505f }, { -0.19158349931240082f,
        -0.004258692264556885f, 0.032371193170547485f, -0.3141036927700043f,
        -0.026762403547763824f, -0.29336631298065186f, 0.08645252138376236f,
        -0.16367173194885254f, -0.03548547625541687f, 0.08825305104255676f,
        -0.16509909927845f, 0.22309480607509613f, -0.22282296419143677f,
        0.2915789783000946f, -0.24179422855377197f, -0.19140881299972534f,
        0.21163100004196167f, 0.24713221192359924f, 0.28102827072143555f,
        0.25016462802886963f, 0.020191945135593414f, 0.19882827997207642f,
        -0.1307595670223236f, -0.059169698506593704f, 0.2300402969121933f,
        -0.261514276266098f, 0.22954094409942627f, -0.12279488891363144f,
        0.05800282955169678f, -0.26823562383651733f, 0.16349054872989655f,
        0.1337476223707199f }, { -0.21433322131633759f, -0.1279313862323761f,
        -0.11171227693557739f, 0.27164509892463684f, -0.26441383361816406f,
        0.2845393121242523f, 0.07359254360198975f, -0.20844537019729614f,
        -0.26620519161224365f, 0.17144301533699036f, -0.061109013855457306f,
        -0.19284695386886597f, 0.29584869742393494f, -0.02723398245871067f,
        -0.11244286596775055f, 0.0967603325843811f, -0.16153597831726074f,
        0.18742787837982178f, -0.13470113277435303f, 0.12359786033630371f,
        0.0469309501349926f, 0.02421507239341736f, -0.07988794147968292f,
        -0.15143592655658722f, -0.18177983164787292f, 0.1452380120754242f,
        0.2337561547756195f, -0.17263798415660858f, -0.11822041869163513f,
        0.19546762108802795f, -0.16657039523124695f, -0.033323969691991806f }, {
        0.29264822602272034f, 0.11346611380577087f, 0.04012581706047058f,
        -0.0461408905684948f, 0.225418359041214f, -0.05009126663208008f,
        0.013211707584559917f, 0.004197537899017334f, 0.2864855229854584f,
        0.01006925106048584f, -0.010690944269299507f, 0.11204484105110168f,
        -0.16305525600910187f, -0.09700464457273483f, 0.08624142408370972f,
        -0.3024343252182007f, -0.29751643538475037f, 0.2706601917743683f,
        0.19159749150276184f, -0.1787676364183426f, 0.07430826127529144f,
        0.05496254935860634f, -0.07979777455329895f, 0.16874255239963531f,
        -0.37327951192855835f, 0.09258365631103516f, 0.44089770317077637f,
        -0.042961325496435165f, -0.14135785400867462f, 0.07403197884559631f,
        0.09825335443019867f, 0.07322394847869873f }, { 0.28564146161079407f,
        0.2890130579471588f, -0.021617114543914795f, 0.08250059932470322f,
        0.3030112683773041f, 0.07065775990486145f, -0.236245796084404f,
        -0.2608001232147217f, 0.29470762610435486f, -0.06864601373672485f,
        0.2483109086751938f, -0.20344071090221405f, 0.2957691252231598f,
        -0.13390012085437775f, 0.19158922135829926f, 0.3726794719696045f,
        0.02077532187104225f, 0.06025278568267822f, 0.05459544435143471f,
        0.41055813431739807f, 0.32145416736602783f, 0.030043581500649452f,
        0.28743305802345276f, 0.19736182689666748f, 0.1656898558139801f,
        -0.28868207335472107f, -0.039911627769470215f, -0.006442106794565916f,
        0.2501918375492096f, -0.14010676741600037f, -0.10169651359319687f,
        -0.15317825973033905f }, { 0.2615049183368683f, 0.08643358945846558f,
        -0.29237738251686096f, -0.2458128184080124f, 0.20376496016979218f,
        -0.0771239846944809f, 0.1555452048778534f, -0.15540927648544312f,
        0.18755429983139038f, -0.07031691074371338f, -0.06268419325351715f,
        -0.041090548038482666f, 0.042330075055360794f, 0.27333545684814453f,
        -0.008807391859591007f, 0.1133832335472107f, -0.040624819695949554f,
        -0.2859399914741516f, -0.26647016406059265f, -0.2497035712003708f,
        0.09534543007612228f, 0.09418291598558426f, -0.2307620644569397f,
        0.055859606713056564f, -0.07496815174818039f, -0.19647599756717682f,
        0.02131301537156105f, 0.09323979169130325f, 0.2113950550556183f,
        0.0796298086643219f, -0.06491973996162415f, -0.3819006681442261f }, {
        0.19981321692466736f, 0.1956864297389984f, -0.033508092164993286f,
        -0.22838544845581055f, -0.12505969405174255f, 0.026693344116210938f,
        0.07720576971769333f, 0.14285165071487427f, -0.17670965194702148f,
        0.17403557896614075f, -0.22244372963905334f, 0.1680217683315277f,
        -0.1060299277305603f, 0.1071399599313736f, -0.11092887073755264f,
        -0.05349590629339218f, 0.03990337252616882f, 0.0716913640499115f,
        0.22807641327381134f, -0.13356927037239075f, 0.1965365707874298f,
        0.1245347261428833f, 0.07470300793647766f, 0.1796993613243103f,
        -0.004812136758118868f, 0.10351252555847168f, -0.057905394583940506f,
        -0.18869787454605103f, 0.13817378878593445f, 0.17853263020515442f,
        -0.027914874255657196f, -0.02365732006728649f }, { -0.2546439468860626f,
        -0.09556084871292114f, 0.12328919768333435f, 0.03998832777142525f,
        -0.30052247643470764f, 0.24699172377586365f, -0.32009536027908325f,
        -0.11670328676700592f, 0.17735600471496582f, 0.009893029928207397f,
        0.26776403188705444f, 0.19816415011882782f, -0.05558810010552406f,
        -0.19641350209712982f, 0.3241870105266571f, -0.0002136280236300081f,
        -0.015536589547991753f, 0.10842388868331909f, 0.2418537586927414f,
        0.25024867057800293f, -0.0643463209271431f, -0.06727208197116852f,
        0.09996896982192993f, 0.08140590041875839f, -0.12102364748716354f,
        0.09566594660282135f, 0.026901600882411003f, 0.24559487402439117f,
        -0.288028746843338f, -0.2434448003768921f, 0.1651056408882141f,
        0.23462900519371033f }, { -0.08816954493522644f, 0.2728963792324066f,
        -0.07170480489730835f, 0.08861888200044632f, -0.08407828211784363f,
        0.20119628310203552f, -0.021976735442876816f, 0.020944571122527122f,
        -0.15294449031352997f, -0.038666754961013794f, -0.1334909051656723f,
        0.2502269148826599f, 0.20107106864452362f, -0.2309870719909668f,
        0.26465773582458496f, 0.09385055303573608f, -0.0016800533048808575f,
        0.21900424361228943f, 0.22734272480010986f, 0.15115073323249817f,
        -0.3155216872692108f, 0.03708222880959511f, -0.19714993238449097f,
        0.18521378934383392f, 0.10345728695392609f, 0.21989409625530243f,
        -0.008119909092783928f, -0.27566710114479065f, 0.18573936820030212f,
        0.15478622913360596f, 0.03387163206934929f, -0.09682217240333557f }, {
        0.14700931310653687f, 0.1833711564540863f, -0.0858893096446991f,
        0.03548716753721237f, -0.03290480375289917f, 0.2733006179332733f,
        -0.00011049837485188618f, -0.1723647564649582f, 0.000652611255645752f,
        -0.22920364141464233f, 0.03385178744792938f, -0.11219978332519531f,
        0.23820827901363373f, 0.33258119225502014f, 0.05578869208693504f,
        -0.21240083873271942f, -0.07963520288467407f, 0.14483880996704102f,
        -0.18910400569438934f, -0.1327388882637024f, -0.024066906422376633f,
        0.2018621563911438f, -0.2228696048259735f, 0.2577541768550873f,
        -0.16621330380439758f, 0.06491447985172272f, -0.09694764763116837f,
        0.19547560811042786f, -0.011228948831558228f, 0.26896968483924866f,
        -0.360990971326828f, -0.2201414704322815f }, { -0.23982827365398407f,
        0.03880670666694641f, -0.013682037591934204f, -0.11685100197792053f,
        -0.020662933588027954f, 0.30190005898475647f, -0.14934301376342773f,
        -0.15993990004062653f, 0.1152651309967041f, -0.11991468071937561f,
        -0.15497799217700958f, 0.11116543412208557f, -0.2045515924692154f,
        0.2403949797153473f, -0.21275603771209717f, 0.05732181668281555f,
        -0.2770107090473175f, 0.088544100522995f, 0.19012078642845154f,
        -0.07119561731815338f, -0.18914666771888733f, -0.10413256287574768f,
        -0.16050675511360168f, 0.11598068475723267f, -0.03151249885559082f,
        -0.1937510371208191f, 0.18474078178405762f, 0.19502457976341248f,
        0.22045472264289856f, -0.29474368691444397f, 0.09864541888237f,
        0.06054645776748657f }, { -0.1617974042892456f, 0.1317739188671112f,
        0.03260180354118347f, -0.19948318600654602f, -0.0928228572010994f,
        -0.1987132430076599f, -0.1755896806716919f, 0.11372503638267517f,
        0.05445769429206848f, 0.07657262682914734f, -0.18663887679576874f,
        -0.23218511044979095f, 0.2056141495704651f, 0.03608625754714012f,
        0.33959874510765076f, -0.3019200563430786f, -0.25386664271354675f,
        -0.18997573852539062f, -0.06610497832298279f, -0.1891406625509262f,
        -0.10423542559146881f, -0.11639183014631271f, -0.3044300675392151f,
        0.2589237689971924f, -0.06821785122156143f, 0.17655706405639648f,
        0.1661142259836197f, -0.24122722446918488f, -0.0006675124168395996f,
        -0.2239055633544922f, -0.0723206177353859f, 0.2102394998073578f }, {
        0.036086976528167725f, -0.062024205923080444f, 0.10704237222671509f,
        -0.1612134575843811f, -0.1631729155778885f, 0.1497061848640442f,
        0.03271482139825821f, 0.27597710490226746f, -0.23304778337478638f,
        -0.035415440797805786f, 0.1086379662156105f, 0.07746559381484985f,
        0.13613636791706085f, 0.09150157123804092f, 0.25167548656463623f,
        -0.24342824518680573f, -0.027234435081481934f, 0.19620582461357117f,
        0.44021302461624146f, -0.23242811858654022f, -0.05763920396566391f,
        -0.09912444651126862f, -0.19671492278575897f, -0.33463647961616516f,
        -0.40228715538978577f, -0.000608850852586329f, 0.06298207491636276f,
        0.39882805943489075f, -0.2531542181968689f, 0.24004116654396057f,
        -0.30925723910331726f, -0.26930177211761475f }, { 0.1379433274269104f,
        -0.16078773140907288f, 0.11784148216247559f, 0.20431241393089294f,
        0.29410573840141296f, 0.19759181141853333f, 0.09952005743980408f,
        0.1544932872056961f, 0.040603965520858765f, -0.2119520902633667f,
        0.18040086328983307f, -0.38904741406440735f, -0.046765703707933426f,
        0.13744039833545685f, -0.3137235641479492f, 0.05679671838879585f,
        0.1565159410238266f, 0.2297056019306183f, 0.032699450850486755f,
        0.20715069770812988f, -0.016263214871287346f, -0.3025153875350952f,
        0.26029130816459656f, 0.42276427149772644f, 0.2875830829143524f,
        -0.12763257324695587f, -0.20330224931240082f, -0.07699788361787796f,
        -0.08671019971370697f, -0.019541800022125244f, 0.4000849425792694f,
        0.17512208223342896f }, { 0.09298902750015259f, 0.2841971218585968f,
        -0.0814327746629715f, -0.010091223753988743f, -0.12308911234140396f,
        0.11057800054550171f, -0.1737426221370697f, 0.20642486214637756f,
        -0.2925014793872833f, 0.022429823875427246f, -0.06304851174354553f,
        -0.07166500389575958f, -0.26361000537872314f, 0.050878144800662994f,
        0.189671128988266f, 0.30893731117248535f, 0.1296386420726776f,
        0.027322769165039062f, 0.29731038212776184f, 0.14744597673416138f,
        0.2998683750629425f, 0.08306887000799179f, 0.06231796741485596f,
        0.3401893973350525f, -0.004573390819132328f, 0.18759925663471222f,
        -0.16945621371269226f, 0.01625938154757023f, 0.14345040917396545f,
        -0.11986468732357025f, -0.21137407422065735f, 0.1280972957611084f }, {
        -0.14775772392749786f, 0.29651907086372375f, 0.02990397810935974f,
        -0.06818585097789764f, 0.1993788629770279f, -0.22813674807548523f,
        0.16850803792476654f, 0.24565419554710388f, -0.2325194776058197f,
        -0.2458522766828537f, -0.017307782545685768f, -0.12818260490894318f,
        -0.17801909148693085f, -0.2504909038543701f, 0.1731644719839096f,
        0.18156693875789642f, 0.23218704760074615f, 0.11064860224723816f,
        -0.15070302784442902f, 0.10314767062664032f, 0.3309069275856018f,
        -0.28518369793891907f, -0.07374785840511322f, 0.05567348375916481f,
        -0.01467915065586567f, -0.3420534133911133f, -0.21914657950401306f,
        0.23488031327724457f, -0.2205982804298401f, 0.28094157576560974f,
        -0.15506510436534882f, 0.07852166146039963f }, { 0.2703488767147064f,
        -0.16041381657123566f, 0.12738299369812012f, -0.045484237372875214f,
        0.06815514713525772f, 0.1861400008201599f, -0.15391220152378082f,
        0.28177282214164734f, 0.1474648416042328f, -0.23766008019447327f,
        0.025048013776540756f, 0.06622862070798874f, 0.09594401717185974f,
        -0.1663329154253006f, -0.10072105377912521f, -0.044219132512807846f,
        -0.2531753182411194f, 0.08700510859489441f, -0.26066508889198303f,
        0.07304855436086655f, -0.13394378125667572f, 0.22174499928951263f,
        -0.18301571905612946f, -0.16244065761566162f, -0.004656728822737932f,
        0.2568950057029724f, 0.15513287484645844f, -0.2911604344844818f,
        0.08541131019592285f, 0.08213254809379578f, -0.258662611246109f,
        0.04998021572828293f }, { -0.06283275783061981f, -0.26753589510917664f,
        -0.10381917655467987f, -0.13344663381576538f, 0.2644997537136078f,
        0.2204643189907074f, 0.2924788296222687f, -0.17931663990020752f,
        -0.09078609943389893f, -0.17306232452392578f, 0.02247890830039978f,
        -0.26996222138404846f, 0.22075030207633972f, -0.3029964864253998f,
        0.21873190999031067f, -0.0030784308910369873f, 0.18228653073310852f,
        0.09971508383750916f, -0.242676243185997f, -0.1424383968114853f,
        0.19031599164009094f, 0.3004438579082489f, -0.23650792241096497f,
        -0.2683113217353821f, 0.29957786202430725f, 0.08561205863952637f,
        -0.1366746425628662f, -0.15950153768062592f, -0.07675467431545258f,
        -0.2218506634235382f, -0.2795913517475128f, 0.1869203746318817f }, {
        0.2003176510334015f, -0.27792131900787354f, 0.055196613073349f,
        -0.27959588170051575f, 0.18430876731872559f, -0.05316638946533203f,
        0.16156336665153503f, -0.18850930035114288f, 0.09482413530349731f,
        0.021483391523361206f, 0.009039878845214844f, 0.1586727499961853f,
        0.11457142233848572f, 0.2876913249492645f, -0.24802550673484802f,
        0.14089101552963257f, -0.06842575967311859f, -0.10107968747615814f,
        0.06239110231399536f, -0.2528536915779114f, -0.27538973093032837f,
        0.15457189083099365f, -0.15549622476100922f, 0.20245882868766785f,
        0.08544808626174927f, 0.22650876641273499f, -0.05268847942352295f,
        0.20387354493141174f, 0.24724552035331726f, 0.21444126963615417f,
        0.05186706781387329f, -0.11084021627902985f }, { -0.19810965657234192f,
        0.2745245397090912f, 0.1767616868019104f, 0.19752147793769836f,
        0.0650932788848877f, 0.29138168692588806f, -0.29061684012413025f,
        0.010446667671203613f, 0.025284886360168457f, -0.10202452540397644f,
        -0.10358673334121704f, 0.14746251702308655f, 0.15136101841926575f,
        0.04500553011894226f, -0.2497977316379547f, -0.29277902841567993f,
        0.02556520700454712f, -0.09509801864624023f, 0.16731274127960205f,
        -0.25266408920288086f, -0.3004688322544098f, -0.19769597053527832f,
        -0.14847496151924133f, 0.3053832948207855f, 0.07563886046409607f,
        -0.10258816182613373f, -0.060128673911094666f, 0.0016401112079620361f,
        -0.29647189378738403f, -0.18869297206401825f, 0.05497395992279053f,
        0.16092956066131592f }, { -0.011267274618148804f, -0.25281068682670593f,
        -0.10565309226512909f, -0.10787995159626007f, 0.23997481167316437f,
        0.011940985918045044f, 0.10584557056427002f, -0.22430141270160675f,
        -0.06814777851104736f, -0.24531681835651398f, -0.07618588954210281f,
        0.12015970051288605f, 0.0185067281126976f, 0.002306112553924322f,
        0.17718534171581268f, 0.3006735146045685f, 0.1760181039571762f,
        0.04417389631271362f, 0.20160885155200958f, 0.16602091491222382f,
        0.2585296034812927f, -0.027029002085328102f, -0.2520882785320282f,
        0.29521048069000244f, 0.25318053364753723f, 0.05056742578744888f,
        0.22924767434597015f, -0.03404907137155533f, -0.18211489915847778f,
        -0.15127526223659515f, 0.18461528420448303f, 0.2534249424934387f }, {
        -0.10580866038799286f, -0.17215237021446228f, -0.01352420449256897f,
        -0.13443149626255035f, -0.10265451669692993f, -0.2169143706560135f,
        0.30098411440849304f, -0.2986297905445099f, 0.07908004522323608f,
        -0.30061498284339905f, 0.10424068570137024f, -0.018799901008605957f,
        -0.18665757775306702f, 0.005998075008392334f, -0.2969498336315155f,
        0.18525955080986023f, 0.2379322350025177f, 0.15184926986694336f,
        0.215760737657547f, -0.19088758528232574f, 0.08620095252990723f,
        0.019999295473098755f, -0.0752219706773758f, 0.11181813478469849f,
        0.1881926953792572f, 0.2210972011089325f, 0.13236162066459656f,
        -0.06690356135368347f, -0.18345263600349426f, -0.14861416816711426f,
        0.08577704429626465f, -0.09683996438980103f }, { 0.03951254487037659f,
        0.23604217171669006f, 0.012746036052703857f, -0.12561996281147003f,
        -0.1283164620399475f, 0.25823846459388733f, 0.10610169172286987f,
        0.22718152403831482f, 0.1749366819858551f, 0.007794857025146484f,
        0.11289489269256592f, 0.2725127637386322f, -0.13616766035556793f,
        -0.03546282649040222f, -0.1685536652803421f, -0.24492129683494568f,
        0.15760570764541626f, 0.017505377531051636f, -0.273531436920166f,
        -0.005667984485626221f, 0.18370777368545532f, -0.023328542709350586f,
        0.2379932701587677f, -0.048420339822769165f, 0.10376441478729248f,
        -0.295401930809021f, -0.20498433709144592f, 0.22346952557563782f,
        -0.26649031043052673f, -0.08466874063014984f, 0.23347875475883484f,
        -0.03380200266838074f }, { 0.3014208972454071f, -0.11652402579784393f,
        0.24921879172325134f, 0.16919833421707153f, -0.33012309670448303f,
        -0.19414515793323517f, -0.031346261501312256f, -0.11968852579593658f,
        0.23963233828544617f, 0.21321198344230652f, -0.04236738383769989f,
        -0.04909612983465195f, -0.15507397055625916f, 0.041283149272203445f,
        -0.0798816904425621f, -0.16132454574108124f, -0.22342979907989502f,
        0.2487291395664215f, -0.275277704000473f, -0.1761859953403473f,
        -0.1089298352599144f, 0.14958830177783966f, -0.09928475320339203f,
        -0.18516667187213898f, -0.11554347723722458f, 0.2378307580947876f,
        0.042566750198602676f, -0.09927814453840256f, -0.03476092219352722f,
        0.2736774981021881f, 0.01615159772336483f, -0.08927445858716965f }, {
        -0.17075178027153015f, 0.009595632553100586f, 0.2466227114200592f,
        0.033388618379831314f, -0.34659287333488464f, -0.2819134294986725f,
        -0.07840541750192642f, -0.15331926941871643f, -0.2516406178474426f,
        0.012286126613616943f, 0.20758569240570068f, -0.2048848569393158f,
        -0.12051127851009369f, 0.10427423566579819f, 0.0999673530459404f,
        0.028271911665797234f, -0.24087269604206085f, -0.048499464988708496f,
        -0.20961162447929382f, 0.10103815048933029f, -0.2312844693660736f,
        -0.22412340342998505f, -0.031352698802948f, 0.27904242277145386f,
        -0.11300363391637802f, -0.16207841038703918f, -0.33752813935279846f,
        0.3786945641040802f, -0.07090267539024353f, -0.23986908793449402f,
        -0.30680611729621887f, -0.0811222642660141f }, { -0.01348048448562622f,
        0.08233943581581116f, 0.18838337063789368f, 0.21852442622184753f,
        -0.04740855097770691f, 0.19177213311195374f, -0.0060100555419921875f,
        0.20933791995048523f, 0.06356173753738403f, 0.063028484582901f,
        0.09440034627914429f, 0.29559019207954407f, -0.2545689642429352f,
        -0.09652335941791534f, -0.2841038703918457f, -0.03553757071495056f,
        0.02329021692276001f, -0.08720272779464722f, -0.0750999003648758f,
        -0.10468094050884247f, -0.03761094808578491f, -0.030853450298309326f,
        0.2112145721912384f, 0.2599640190601349f, -0.2969062626361847f,
        -0.13667742908000946f, 0.24173608422279358f, 0.09566575288772583f,
        0.021116197109222412f, -0.005982398986816406f, -0.26417285203933716f,
        0.1879688799381256f }, { -0.1763739138841629f, -0.17203155159950256f,
        0.0620841383934021f, -0.04815051332116127f, -0.08886270225048065f,
        -0.19284456968307495f, 0.09443464875221252f, 0.1472807228565216f,
        -0.21029016375541687f, 0.2210421860218048f, -0.26095423102378845f,
        0.23557153344154358f, -0.07677116990089417f, -0.16641905903816223f,
        -0.16725768148899078f, -0.2004043012857437f, 0.08218716084957123f,
        -0.29308468103408813f, -0.057871971279382706f, -0.07429885864257812f,
        0.2029605507850647f, -0.19719278812408447f, -0.22302524745464325f,
        0.2192145586013794f, -0.13908077776432037f, 0.2019682675600052f,
        -0.028201768174767494f, 0.08898906409740448f, 0.17976179718971252f,
        -0.22928372025489807f, 0.3000858724117279f, 0.08501409739255905f }, {
        -0.09888127446174622f, -0.22750279307365417f, -0.22503364086151123f,
        -0.0014225258491933346f, -0.012498408555984497f, -0.24329537153244019f,
        -0.09442851692438126f, 0.08175003528594971f, -0.016126900911331177f,
        0.06972584128379822f, 0.05360379442572594f, 0.22432491183280945f,
        -0.031845152378082275f, -0.2877633273601532f, 0.18349257111549377f,
        0.19327174127101898f, -0.09216172993183136f, 0.04617339372634888f,
        -0.09483018517494202f, -0.07904840260744095f, -0.035641368478536606f,
        -0.2887636721134186f, -0.06047184765338898f, -0.3070217967033386f,
        4.397468364913948e-05f, -0.19100867211818695f, 0.28239238262176514f,
        -0.2758784890174866f, -0.16326025128364563f, 0.11664235591888428f,
        -0.03823475539684296f, 0.11660117655992508f },

};

const float h3[32][32] = { { -0.13349971175193787f, -0.10295046865940094f,
        -0.04925942420959473f, 0.005893866065889597f, -0.12596699595451355f,
        0.2987118661403656f, 0.07975713163614273f, 0.06396704912185669f,
        -0.30411389470100403f, -0.10008957982063293f, 0.014623122289776802f,
        -0.0538821816444397f, 0.048359375447034836f, 0.24325567483901978f,
        0.10184130072593689f, -0.24775750935077667f, 0.07803884148597717f,
        -0.21483641862869263f, 0.17120853066444397f, 0.00830860622227192f,
        -0.29617825150489807f, -0.22132611274719238f, -0.23983746767044067f,
        -0.28275105357170105f, -0.3873054087162018f, 0.30998873710632324f,
        -0.4226490557193756f, -0.11779281497001648f, 0.23828622698783875f,
        0.11771553754806519f, -0.37041905522346497f, -0.30605563521385193f }, {
        -0.16083313524723053f, 0.002147972583770752f, -0.04321768879890442f,
        0.20690490305423737f, 0.022892558947205544f, 0.008302271366119385f,
        -0.20543429255485535f, -0.08731026947498322f, 0.24677053093910217f,
        0.048597872257232666f, 0.20651067793369293f, 0.16381694376468658f,
        0.28672969341278076f, -0.3506166338920593f, 0.06659439951181412f,
        0.04734180495142937f, 0.03125026077032089f, -0.2737269401550293f,
        -0.21843840181827545f, 0.3144419193267822f, 0.10113385319709778f,
        0.14034439623355865f, -0.16832955181598663f, 0.33271604776382446f,
        0.19579239189624786f, 0.14696596562862396f, 0.06076221913099289f,
        0.24354417622089386f, -0.28620439767837524f, -0.0511949360370636f,
        0.2096933126449585f, 0.040742650628089905f }, { -0.1579037755727768f,
        0.28447648882865906f, -0.1789936125278473f, 0.20799604058265686f,
        0.025723248720169067f, 0.17740526795387268f, -0.1666354387998581f,
        -0.06060470640659332f, -0.017253071069717407f, -0.2790656089782715f,
        -0.16800916194915771f, -0.2990717589855194f, 0.07126197218894958f,
        -0.08032660186290741f, -0.1799015998840332f, -0.1891922950744629f,
        0.2556300461292267f, 0.2080206573009491f, -0.24888339638710022f,
        0.27691522240638733f, -0.21319638192653656f, -0.14012537896633148f,
        -0.30466505885124207f, -0.013728171586990356f, -0.1629670113325119f,
        0.2530798614025116f, 0.0633285641670227f, 0.0011726915836334229f,
        -0.14820529520511627f, 0.2823629677295685f, 0.10884168744087219f,
        -0.2637729346752167f }, { -0.12927524745464325f, -0.01806354522705078f,
        -0.23058539628982544f, -0.030255556106567383f, -0.6437712907791138f,
        -0.17983144521713257f, 0.12746267020702362f, 0.08151327818632126f,
        -0.06944303214550018f, 0.09271347522735596f, -0.0808296874165535f,
        -0.24855418503284454f, -0.11665885150432587f, 0.12946322560310364f,
        -0.0930287167429924f, -0.3423658609390259f, 0.10996055603027344f,
        0.24023392796516418f, 0.2942558825016022f, -0.17709596455097198f,
        -0.1255558878183365f, -0.2134709507226944f, -0.06394433975219727f,
        -0.24457648396492004f, 0.10029968619346619f, -0.2400282919406891f,
        0.11666780710220337f, -0.14067168533802032f, -0.22396358847618103f,
        0.2546447217464447f, -0.002586635062471032f, 0.04970479756593704f }, {
        -0.18258020281791687f, 0.2506926953792572f, -0.0804666131734848f,
        0.05040470138192177f, 0.02259594015777111f, -0.23578980565071106f,
        -0.09264150261878967f, 0.0013416205765679479f, 0.043158918619155884f,
        -0.2690317630767822f, 0.04383622854948044f, -0.25736406445503235f,
        -0.21347089111804962f, -0.19318343698978424f, -0.21244710683822632f,
        -0.13572444021701813f, -0.1285906583070755f, 0.15626338124275208f,
        -0.050212472677230835f, 0.0982573851943016f, 0.11135826259851456f,
        0.06453899294137955f, 0.19172045588493347f, -0.22890380024909973f,
        -0.12237071990966797f, 0.09743352234363556f, 0.10070990025997162f,
        0.28772199153900146f, 0.2695164978504181f, -0.16498444974422455f,
        0.014173826202750206f, -0.2047310173511505f }, { -0.19158349931240082f,
        -0.004258692264556885f, 0.032371193170547485f, -0.3141036927700043f,
        -0.026762403547763824f, -0.29336631298065186f, 0.08645252138376236f,
        -0.16367173194885254f, -0.03548547625541687f, 0.08825305104255676f,
        -0.16509909927845f, 0.22309480607509613f, -0.22282296419143677f,
        0.2915789783000946f, -0.24179422855377197f, -0.19140881299972534f,
        0.21163100004196167f, 0.24713221192359924f, 0.28102827072143555f,
        0.25016462802886963f, 0.020191945135593414f, 0.19882827997207642f,
        -0.1307595670223236f, -0.059169698506593704f, 0.2300402969121933f,
        -0.261514276266098f, 0.22954094409942627f, -0.12279488891363144f,
        0.05800282955169678f, -0.26823562383651733f, 0.16349054872989655f,
        0.1337476223707199f }, { -0.21433322131633759f, -0.1279313862323761f,
        -0.11171227693557739f, 0.27164509892463684f, -0.26441383361816406f,
        0.2845393121242523f, 0.07359254360198975f, -0.20844537019729614f,
        -0.26620519161224365f, 0.17144301533699036f, -0.061109013855457306f,
        -0.19284695386886597f, 0.29584869742393494f, -0.02723398245871067f,
        -0.11244286596775055f, 0.0967603325843811f, -0.16153597831726074f,
        0.18742787837982178f, -0.13470113277435303f, 0.12359786033630371f,
        0.0469309501349926f, 0.02421507239341736f, -0.07988794147968292f,
        -0.15143592655658722f, -0.18177983164787292f, 0.1452380120754242f,
        0.2337561547756195f, -0.17263798415660858f, -0.11822041869163513f,
        0.19546762108802795f, -0.16657039523124695f, -0.033323969691991806f }, {
        0.29264822602272034f, 0.11346611380577087f, 0.04012581706047058f,
        -0.0461408905684948f, 0.225418359041214f, -0.05009126663208008f,
        0.013211707584559917f, 0.004197537899017334f, 0.2864855229854584f,
        0.01006925106048584f, -0.010690944269299507f, 0.11204484105110168f,
        -0.16305525600910187f, -0.09700464457273483f, 0.08624142408370972f,
        -0.3024343252182007f, -0.29751643538475037f, 0.2706601917743683f,
        0.19159749150276184f, -0.1787676364183426f, 0.07430826127529144f,
        0.05496254935860634f, -0.07979777455329895f, 0.16874255239963531f,
        -0.37327951192855835f, 0.09258365631103516f, 0.44089770317077637f,
        -0.042961325496435165f, -0.14135785400867462f, 0.07403197884559631f,
        0.09825335443019867f, 0.07322394847869873f }, { 0.28564146161079407f,
        0.2890130579471588f, -0.021617114543914795f, 0.08250059932470322f,
        0.3030112683773041f, 0.07065775990486145f, -0.236245796084404f,
        -0.2608001232147217f, 0.29470762610435486f, -0.06864601373672485f,
        0.2483109086751938f, -0.20344071090221405f, 0.2957691252231598f,
        -0.13390012085437775f, 0.19158922135829926f, 0.3726794719696045f,
        0.02077532187104225f, 0.06025278568267822f, 0.05459544435143471f,
        0.41055813431739807f, 0.32145416736602783f, 0.030043581500649452f,
        0.28743305802345276f, 0.19736182689666748f, 0.1656898558139801f,
        -0.28868207335472107f, -0.039911627769470215f, -0.006442106794565916f,
        0.2501918375492096f, -0.14010676741600037f, -0.10169651359319687f,
        -0.15317825973033905f }, { 0.2615049183368683f, 0.08643358945846558f,
        -0.29237738251686096f, -0.2458128184080124f, 0.20376496016979218f,
        -0.0771239846944809f, 0.1555452048778534f, -0.15540927648544312f,
        0.18755429983139038f, -0.07031691074371338f, -0.06268419325351715f,
        -0.041090548038482666f, 0.042330075055360794f, 0.27333545684814453f,
        -0.008807391859591007f, 0.1133832335472107f, -0.040624819695949554f,
        -0.2859399914741516f, -0.26647016406059265f, -0.2497035712003708f,
        0.09534543007612228f, 0.09418291598558426f, -0.2307620644569397f,
        0.055859606713056564f, -0.07496815174818039f, -0.19647599756717682f,
        0.02131301537156105f, 0.09323979169130325f, 0.2113950550556183f,
        0.0796298086643219f, -0.06491973996162415f, -0.3819006681442261f }, {
        0.19981321692466736f, 0.1956864297389984f, -0.033508092164993286f,
        -0.22838544845581055f, -0.12505969405174255f, 0.026693344116210938f,
        0.07720576971769333f, 0.14285165071487427f, -0.17670965194702148f,
        0.17403557896614075f, -0.22244372963905334f, 0.1680217683315277f,
        -0.1060299277305603f, 0.1071399599313736f, -0.11092887073755264f,
        -0.05349590629339218f, 0.03990337252616882f, 0.0716913640499115f,
        0.22807641327381134f, -0.13356927037239075f, 0.1965365707874298f,
        0.1245347261428833f, 0.07470300793647766f, 0.1796993613243103f,
        -0.004812136758118868f, 0.10351252555847168f, -0.057905394583940506f,
        -0.18869787454605103f, 0.13817378878593445f, 0.17853263020515442f,
        -0.027914874255657196f, -0.02365732006728649f }, { -0.2546439468860626f,
        -0.09556084871292114f, 0.12328919768333435f, 0.03998832777142525f,
        -0.30052247643470764f, 0.24699172377586365f, -0.32009536027908325f,
        -0.11670328676700592f, 0.17735600471496582f, 0.009893029928207397f,
        0.26776403188705444f, 0.19816415011882782f, -0.05558810010552406f,
        -0.19641350209712982f, 0.3241870105266571f, -0.0002136280236300081f,
        -0.015536589547991753f, 0.10842388868331909f, 0.2418537586927414f,
        0.25024867057800293f, -0.0643463209271431f, -0.06727208197116852f,
        0.09996896982192993f, 0.08140590041875839f, -0.12102364748716354f,
        0.09566594660282135f, 0.026901600882411003f, 0.24559487402439117f,
        -0.288028746843338f, -0.2434448003768921f, 0.1651056408882141f,
        0.23462900519371033f }, { -0.08816954493522644f, 0.2728963792324066f,
        -0.07170480489730835f, 0.08861888200044632f, -0.08407828211784363f,
        0.20119628310203552f, -0.021976735442876816f, 0.020944571122527122f,
        -0.15294449031352997f, -0.038666754961013794f, -0.1334909051656723f,
        0.2502269148826599f, 0.20107106864452362f, -0.2309870719909668f,
        0.26465773582458496f, 0.09385055303573608f, -0.0016800533048808575f,
        0.21900424361228943f, 0.22734272480010986f, 0.15115073323249817f,
        -0.3155216872692108f, 0.03708222880959511f, -0.19714993238449097f,
        0.18521378934383392f, 0.10345728695392609f, 0.21989409625530243f,
        -0.008119909092783928f, -0.27566710114479065f, 0.18573936820030212f,
        0.15478622913360596f, 0.03387163206934929f, -0.09682217240333557f }, {
        0.14700931310653687f, 0.1833711564540863f, -0.0858893096446991f,
        0.03548716753721237f, -0.03290480375289917f, 0.2733006179332733f,
        -0.00011049837485188618f, -0.1723647564649582f, 0.000652611255645752f,
        -0.22920364141464233f, 0.03385178744792938f, -0.11219978332519531f,
        0.23820827901363373f, 0.33258119225502014f, 0.05578869208693504f,
        -0.21240083873271942f, -0.07963520288467407f, 0.14483880996704102f,
        -0.18910400569438934f, -0.1327388882637024f, -0.024066906422376633f,
        0.2018621563911438f, -0.2228696048259735f, 0.2577541768550873f,
        -0.16621330380439758f, 0.06491447985172272f, -0.09694764763116837f,
        0.19547560811042786f, -0.011228948831558228f, 0.26896968483924866f,
        -0.360990971326828f, -0.2201414704322815f }, { -0.23982827365398407f,
        0.03880670666694641f, -0.013682037591934204f, -0.11685100197792053f,
        -0.020662933588027954f, 0.30190005898475647f, -0.14934301376342773f,
        -0.15993990004062653f, 0.1152651309967041f, -0.11991468071937561f,
        -0.15497799217700958f, 0.11116543412208557f, -0.2045515924692154f,
        0.2403949797153473f, -0.21275603771209717f, 0.05732181668281555f,
        -0.2770107090473175f, 0.088544100522995f, 0.19012078642845154f,
        -0.07119561731815338f, -0.18914666771888733f, -0.10413256287574768f,
        -0.16050675511360168f, 0.11598068475723267f, -0.03151249885559082f,
        -0.1937510371208191f, 0.18474078178405762f, 0.19502457976341248f,
        0.22045472264289856f, -0.29474368691444397f, 0.09864541888237f,
        0.06054645776748657f }, { -0.1617974042892456f, 0.1317739188671112f,
        0.03260180354118347f, -0.19948318600654602f, -0.0928228572010994f,
        -0.1987132430076599f, -0.1755896806716919f, 0.11372503638267517f,
        0.05445769429206848f, 0.07657262682914734f, -0.18663887679576874f,
        -0.23218511044979095f, 0.2056141495704651f, 0.03608625754714012f,
        0.33959874510765076f, -0.3019200563430786f, -0.25386664271354675f,
        -0.18997573852539062f, -0.06610497832298279f, -0.1891406625509262f,
        -0.10423542559146881f, -0.11639183014631271f, -0.3044300675392151f,
        0.2589237689971924f, -0.06821785122156143f, 0.17655706405639648f,
        0.1661142259836197f, -0.24122722446918488f, -0.0006675124168395996f,
        -0.2239055633544922f, -0.0723206177353859f, 0.2102394998073578f }, {
        0.036086976528167725f, -0.062024205923080444f, 0.10704237222671509f,
        -0.1612134575843811f, -0.1631729155778885f, 0.1497061848640442f,
        0.03271482139825821f, 0.27597710490226746f, -0.23304778337478638f,
        -0.035415440797805786f, 0.1086379662156105f, 0.07746559381484985f,
        0.13613636791706085f, 0.09150157123804092f, 0.25167548656463623f,
        -0.24342824518680573f, -0.027234435081481934f, 0.19620582461357117f,
        0.44021302461624146f, -0.23242811858654022f, -0.05763920396566391f,
        -0.09912444651126862f, -0.19671492278575897f, -0.33463647961616516f,
        -0.40228715538978577f, -0.000608850852586329f, 0.06298207491636276f,
        0.39882805943489075f, -0.2531542181968689f, 0.24004116654396057f,
        -0.30925723910331726f, -0.26930177211761475f }, { 0.1379433274269104f,
        -0.16078773140907288f, 0.11784148216247559f, 0.20431241393089294f,
        0.29410573840141296f, 0.19759181141853333f, 0.09952005743980408f,
        0.1544932872056961f, 0.040603965520858765f, -0.2119520902633667f,
        0.18040086328983307f, -0.38904741406440735f, -0.046765703707933426f,
        0.13744039833545685f, -0.3137235641479492f, 0.05679671838879585f,
        0.1565159410238266f, 0.2297056019306183f, 0.032699450850486755f,
        0.20715069770812988f, -0.016263214871287346f, -0.3025153875350952f,
        0.26029130816459656f, 0.42276427149772644f, 0.2875830829143524f,
        -0.12763257324695587f, -0.20330224931240082f, -0.07699788361787796f,
        -0.08671019971370697f, -0.019541800022125244f, 0.4000849425792694f,
        0.17512208223342896f }, { 0.09298902750015259f, 0.2841971218585968f,
        -0.0814327746629715f, -0.010091223753988743f, -0.12308911234140396f,
        0.11057800054550171f, -0.1737426221370697f, 0.20642486214637756f,
        -0.2925014793872833f, 0.022429823875427246f, -0.06304851174354553f,
        -0.07166500389575958f, -0.26361000537872314f, 0.050878144800662994f,
        0.189671128988266f, 0.30893731117248535f, 0.1296386420726776f,
        0.027322769165039062f, 0.29731038212776184f, 0.14744597673416138f,
        0.2998683750629425f, 0.08306887000799179f, 0.06231796741485596f,
        0.3401893973350525f, -0.004573390819132328f, 0.18759925663471222f,
        -0.16945621371269226f, 0.01625938154757023f, 0.14345040917396545f,
        -0.11986468732357025f, -0.21137407422065735f, 0.1280972957611084f }, {
        -0.14775772392749786f, 0.29651907086372375f, 0.02990397810935974f,
        -0.06818585097789764f, 0.1993788629770279f, -0.22813674807548523f,
        0.16850803792476654f, 0.24565419554710388f, -0.2325194776058197f,
        -0.2458522766828537f, -0.017307782545685768f, -0.12818260490894318f,
        -0.17801909148693085f, -0.2504909038543701f, 0.1731644719839096f,
        0.18156693875789642f, 0.23218704760074615f, 0.11064860224723816f,
        -0.15070302784442902f, 0.10314767062664032f, 0.3309069275856018f,
        -0.28518369793891907f, -0.07374785840511322f, 0.05567348375916481f,
        -0.01467915065586567f, -0.3420534133911133f, -0.21914657950401306f,
        0.23488031327724457f, -0.2205982804298401f, 0.28094157576560974f,
        -0.15506510436534882f, 0.07852166146039963f }, { 0.2703488767147064f,
        -0.16041381657123566f, 0.12738299369812012f, -0.045484237372875214f,
        0.06815514713525772f, 0.1861400008201599f, -0.15391220152378082f,
        0.28177282214164734f, 0.1474648416042328f, -0.23766008019447327f,
        0.025048013776540756f, 0.06622862070798874f, 0.09594401717185974f,
        -0.1663329154253006f, -0.10072105377912521f, -0.044219132512807846f,
        -0.2531753182411194f, 0.08700510859489441f, -0.26066508889198303f,
        0.07304855436086655f, -0.13394378125667572f, 0.22174499928951263f,
        -0.18301571905612946f, -0.16244065761566162f, -0.004656728822737932f,
        0.2568950057029724f, 0.15513287484645844f, -0.2911604344844818f,
        0.08541131019592285f, 0.08213254809379578f, -0.258662611246109f,
        0.04998021572828293f }, { -0.06283275783061981f, -0.26753589510917664f,
        -0.10381917655467987f, -0.13344663381576538f, 0.2644997537136078f,
        0.2204643189907074f, 0.2924788296222687f, -0.17931663990020752f,
        -0.09078609943389893f, -0.17306232452392578f, 0.02247890830039978f,
        -0.26996222138404846f, 0.22075030207633972f, -0.3029964864253998f,
        0.21873190999031067f, -0.0030784308910369873f, 0.18228653073310852f,
        0.09971508383750916f, -0.242676243185997f, -0.1424383968114853f,
        0.19031599164009094f, 0.3004438579082489f, -0.23650792241096497f,
        -0.2683113217353821f, 0.29957786202430725f, 0.08561205863952637f,
        -0.1366746425628662f, -0.15950153768062592f, -0.07675467431545258f,
        -0.2218506634235382f, -0.2795913517475128f, 0.1869203746318817f }, {
        0.2003176510334015f, -0.27792131900787354f, 0.055196613073349f,
        -0.27959588170051575f, 0.18430876731872559f, -0.05316638946533203f,
        0.16156336665153503f, -0.18850930035114288f, 0.09482413530349731f,
        0.021483391523361206f, 0.009039878845214844f, 0.1586727499961853f,
        0.11457142233848572f, 0.2876913249492645f, -0.24802550673484802f,
        0.14089101552963257f, -0.06842575967311859f, -0.10107968747615814f,
        0.06239110231399536f, -0.2528536915779114f, -0.27538973093032837f,
        0.15457189083099365f, -0.15549622476100922f, 0.20245882868766785f,
        0.08544808626174927f, 0.22650876641273499f, -0.05268847942352295f,
        0.20387354493141174f, 0.24724552035331726f, 0.21444126963615417f,
        0.05186706781387329f, -0.11084021627902985f }, { -0.19810965657234192f,
        0.2745245397090912f, 0.1767616868019104f, 0.19752147793769836f,
        0.0650932788848877f, 0.29138168692588806f, -0.29061684012413025f,
        0.010446667671203613f, 0.025284886360168457f, -0.10202452540397644f,
        -0.10358673334121704f, 0.14746251702308655f, 0.15136101841926575f,
        0.04500553011894226f, -0.2497977316379547f, -0.29277902841567993f,
        0.02556520700454712f, -0.09509801864624023f, 0.16731274127960205f,
        -0.25266408920288086f, -0.3004688322544098f, -0.19769597053527832f,
        -0.14847496151924133f, 0.3053832948207855f, 0.07563886046409607f,
        -0.10258816182613373f, -0.060128673911094666f, 0.0016401112079620361f,
        -0.29647189378738403f, -0.18869297206401825f, 0.05497395992279053f,
        0.16092956066131592f }, { -0.011267274618148804f, -0.25281068682670593f,
        -0.10565309226512909f, -0.10787995159626007f, 0.23997481167316437f,
        0.011940985918045044f, 0.10584557056427002f, -0.22430141270160675f,
        -0.06814777851104736f, -0.24531681835651398f, -0.07618588954210281f,
        0.12015970051288605f, 0.0185067281126976f, 0.002306112553924322f,
        0.17718534171581268f, 0.3006735146045685f, 0.1760181039571762f,
        0.04417389631271362f, 0.20160885155200958f, 0.16602091491222382f,
        0.2585296034812927f, -0.027029002085328102f, -0.2520882785320282f,
        0.29521048069000244f, 0.25318053364753723f, 0.05056742578744888f,
        0.22924767434597015f, -0.03404907137155533f, -0.18211489915847778f,
        -0.15127526223659515f, 0.18461528420448303f, 0.2534249424934387f }, {
        -0.10580866038799286f, -0.17215237021446228f, -0.01352420449256897f,
        -0.13443149626255035f, -0.10265451669692993f, -0.2169143706560135f,
        0.30098411440849304f, -0.2986297905445099f, 0.07908004522323608f,
        -0.30061498284339905f, 0.10424068570137024f, -0.018799901008605957f,
        -0.18665757775306702f, 0.005998075008392334f, -0.2969498336315155f,
        0.18525955080986023f, 0.2379322350025177f, 0.15184926986694336f,
        0.215760737657547f, -0.19088758528232574f, 0.08620095252990723f,
        0.019999295473098755f, -0.0752219706773758f, 0.11181813478469849f,
        0.1881926953792572f, 0.2210972011089325f, 0.13236162066459656f,
        -0.06690356135368347f, -0.18345263600349426f, -0.14861416816711426f,
        0.08577704429626465f, -0.09683996438980103f }, { 0.03951254487037659f,
        0.23604217171669006f, 0.012746036052703857f, -0.12561996281147003f,
        -0.1283164620399475f, 0.25823846459388733f, 0.10610169172286987f,
        0.22718152403831482f, 0.1749366819858551f, 0.007794857025146484f,
        0.11289489269256592f, 0.2725127637386322f, -0.13616766035556793f,
        -0.03546282649040222f, -0.1685536652803421f, -0.24492129683494568f,
        0.15760570764541626f, 0.017505377531051636f, -0.273531436920166f,
        -0.005667984485626221f, 0.18370777368545532f, -0.023328542709350586f,
        0.2379932701587677f, -0.048420339822769165f, 0.10376441478729248f,
        -0.295401930809021f, -0.20498433709144592f, 0.22346952557563782f,
        -0.26649031043052673f, -0.08466874063014984f, 0.23347875475883484f,
        -0.03380200266838074f }, { 0.3014208972454071f, -0.11652402579784393f,
        0.24921879172325134f, 0.16919833421707153f, -0.33012309670448303f,
        -0.19414515793323517f, -0.031346261501312256f, -0.11968852579593658f,
        0.23963233828544617f, 0.21321198344230652f, -0.04236738383769989f,
        -0.04909612983465195f, -0.15507397055625916f, 0.041283149272203445f,
        -0.0798816904425621f, -0.16132454574108124f, -0.22342979907989502f,
        0.2487291395664215f, -0.275277704000473f, -0.1761859953403473f,
        -0.1089298352599144f, 0.14958830177783966f, -0.09928475320339203f,
        -0.18516667187213898f, -0.11554347723722458f, 0.2378307580947876f,
        0.042566750198602676f, -0.09927814453840256f, -0.03476092219352722f,
        0.2736774981021881f, 0.01615159772336483f, -0.08927445858716965f }, {
        -0.17075178027153015f, 0.009595632553100586f, 0.2466227114200592f,
        0.033388618379831314f, -0.34659287333488464f, -0.2819134294986725f,
        -0.07840541750192642f, -0.15331926941871643f, -0.2516406178474426f,
        0.012286126613616943f, 0.20758569240570068f, -0.2048848569393158f,
        -0.12051127851009369f, 0.10427423566579819f, 0.0999673530459404f,
        0.028271911665797234f, -0.24087269604206085f, -0.048499464988708496f,
        -0.20961162447929382f, 0.10103815048933029f, -0.2312844693660736f,
        -0.22412340342998505f, -0.031352698802948f, 0.27904242277145386f,
        -0.11300363391637802f, -0.16207841038703918f, -0.33752813935279846f,
        0.3786945641040802f, -0.07090267539024353f, -0.23986908793449402f,
        -0.30680611729621887f, -0.0811222642660141f }, { -0.01348048448562622f,
        0.08233943581581116f, 0.18838337063789368f, 0.21852442622184753f,
        -0.04740855097770691f, 0.19177213311195374f, -0.0060100555419921875f,
        0.20933791995048523f, 0.06356173753738403f, 0.063028484582901f,
        0.09440034627914429f, 0.29559019207954407f, -0.2545689642429352f,
        -0.09652335941791534f, -0.2841038703918457f, -0.03553757071495056f,
        0.02329021692276001f, -0.08720272779464722f, -0.0750999003648758f,
        -0.10468094050884247f, -0.03761094808578491f, -0.030853450298309326f,
        0.2112145721912384f, 0.2599640190601349f, -0.2969062626361847f,
        -0.13667742908000946f, 0.24173608422279358f, 0.09566575288772583f,
        0.021116197109222412f, -0.005982398986816406f, -0.26417285203933716f,
        0.1879688799381256f }, { -0.1763739138841629f, -0.17203155159950256f,
        0.0620841383934021f, -0.04815051332116127f, -0.08886270225048065f,
        -0.19284456968307495f, 0.09443464875221252f, 0.1472807228565216f,
        -0.21029016375541687f, 0.2210421860218048f, -0.26095423102378845f,
        0.23557153344154358f, -0.07677116990089417f, -0.16641905903816223f,
        -0.16725768148899078f, -0.2004043012857437f, 0.08218716084957123f,
        -0.29308468103408813f, -0.057871971279382706f, -0.07429885864257812f,
        0.2029605507850647f, -0.19719278812408447f, -0.22302524745464325f,
        0.2192145586013794f, -0.13908077776432037f, 0.2019682675600052f,
        -0.028201768174767494f, 0.08898906409740448f, 0.17976179718971252f,
        -0.22928372025489807f, 0.3000858724117279f, 0.08501409739255905f }, {
        -0.09888127446174622f, -0.22750279307365417f, -0.22503364086151123f,
        -0.0014225258491933346f, -0.012498408555984497f, -0.24329537153244019f,
        -0.09442851692438126f, 0.08175003528594971f, -0.016126900911331177f,
        0.06972584128379822f, 0.05360379442572594f, 0.22432491183280945f,
        -0.031845152378082275f, -0.2877633273601532f, 0.18349257111549377f,
        0.19327174127101898f, -0.09216172993183136f, 0.04617339372634888f,
        -0.09483018517494202f, -0.07904840260744095f, -0.035641368478536606f,
        -0.2887636721134186f, -0.06047184765338898f, -0.3070217967033386f,
        4.397468364913948e-05f, -0.19100867211818695f, 0.28239238262176514f,
        -0.2758784890174866f, -0.16326025128364563f, 0.11664235591888428f,
        -0.03823475539684296f, 0.11660117655992508f },

};

const float hout[32] = { 0.3285340666770935f, 0.2858171761035919f,
        -0.36366742849349976f, -0.2632009983062744f, -0.25631460547447205f,
        0.33468252420425415f, 0.002162247896194458f, 0.1081397533416748f,
        -0.19566167891025543f, -0.007266071625053883f, 0.1744336485862732f,
        -0.19491273164749146f, -0.16939763724803925f, 0.1573415994644165f,
        0.2356991171836853f, -0.1462659239768982f, 0.09851181507110596f,
        -0.21499931812286377f, -0.013007339090108871f, -0.26934152841567993f,
        -0.10125408321619034f, 0.16949352622032166f, -0.2570975124835968f,
        -0.21437735855579376f, 0.2278641164302826f, 0.2520971894264221f,
        -0.38587644696235657f, -0.3990364372730255f, 0.24998146295547485f,
        -0.3257754445075989f, -0.41329723596572876f, -0.26551106572151184f };

const float b1[32] = { 0.6920363306999207f, 0.9509658217430115f,
        -1.7145336866378784f, 0.4119643270969391f, 1.0741286277770996f,
        0.8546265363693237f, -0.07467586547136307f, 0.93145751953125f,
        0.5193935036659241f, -0.8871728777885437f, 0.593039333820343f,
        1.6031073331832886f, -0.33494266867637634f, -0.8010779023170471f,
        -1.087764859199524f, 1.1554406881332397f, 0.6632958650588989f,
        -0.224932461977005f, 0.26702749729156494f, 0.5859951972961426f,
        -0.31409698724746704f, -1.1118980646133423f, -1.6770726442337036f,
        -1.2263047695159912f, 2.018333911895752f, -2.0880801677703857f,
        -1.0489267110824585f, 0.7445360422134399f, 0.4031659960746765f,
        -1.0887231826782227f, 1.5438745021820068f, -0.4563482999801636f };

const float b2[32] = { -0.10466054826974869f, -0.7642630338668823f,
        -1.4564176797866821f, -0.528777003288269f, -0.7297643423080444f,
        0.2625555098056793f, 0.389851450920105f, -0.33048292994499207f,
        -1.20063054561615f, -1.912178635597229f, 0.31253743171691895f,
        0.6883504390716553f, -0.04988760128617287f, 2.26581072807312f,
        0.13501279056072235f, 1.132785677909851f, -1.8237234354019165f,
        -1.5844906568527222f, 0.26375624537467957f, -0.23789723217487335f,
        0.17283980548381805f, 0.3692796230316162f, 0.09335066378116608f,
        -1.0231001377105713f, 1.2738454341888428f, 1.44853675365448f,
        0.9071134924888611f, 8.9274239144288e-05f, -0.6323830485343933f,
        -0.4208582937717438f, -1.1864360570907593f, -1.3916414976119995f };

const float b3[32] = { -1.963319182395935f, -0.4028509557247162f,
        0.3184174597263336f, 0.1579766422510147f, 0.1897633820772171f,
        0.866980791091919f, -0.45353031158447266f, -0.6562485098838806f,
        0.5865635275840759f, 0.5033581852912903f, -0.14371006190776825f,
        0.050810977816581726f, -0.5131177306175232f, 0.5688209533691406f,
        -1.1428403854370117f, -0.3598397970199585f, -1.3594286441802979f,
        -1.0192245244979858f, -0.2365882694721222f, 0.5294732451438904f,
        1.530857801437378f, 1.2039998769760132f, -1.0573883056640625f,
        -0.7962622046470642f, 0.7248114347457886f, -0.36519983410835266f,
        -0.07752024382352829f, 0.767806887626648f, 0.2635451555252075f,
        -2.3236680030822754f, 0.06150589883327484f, 1.0375205278396606f };

const float bout[1] = { -0.3124803602695465f };


int main()
{
    //F401_init84 myinit(0);
    //F401_init84 SystemClock_Config_84MHz_internal();
    HAL_Init();
    SystemClock_Config();
    __HAL_RCC_SYSCFG_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();
    HAL_IncTick();
    
    /*********************************
    ***     Initialization
    *********************************/
    LED = 1;
    pc.baud(9600);

    // i2c init
    i2c.frequency(400 * 1000);          // 0.4 mHz
    wait_ms(2);                         // Power Up wait
    look_for_hardware_i2c();            // Hardware present
    init_as5510(i2c_slave_addr1);
    make_delay();

//    // spi init
    //eeprom.format(8,3);
    //eeprom.frequency(5000000); //5M
    enc.format(8,0);
    enc.frequency(5000000); //5M
    make_delay();

    //rom
    ROM_CALL_DATA();
    make_delay();

    // ADC init
    Init_ADC();
    make_delay();

    // Pwm init
    Init_PWM();
    TIM4->CR1 ^= TIM_CR1_UDIS;
    make_delay();

    // TMR3 init
    Init_TMR3();
    TIM3->CR1 ^= TIM_CR1_UDIS;
    make_delay();

    // TMR2 init
//    Init_TMR2();
//    TIM2->CR1 ^= TIM_CR1_UDIS;
//    make_delay();

    // CAN
    can.attach(&CAN_RX_HANDLER);
    CAN_ID_INIT();
    make_delay();

    //Timer priority
    NVIC_SetPriority(TIM3_IRQn, 2);
    //NVIC_SetPriority(TIM2_IRQn, 3);
    NVIC_SetPriority(TIM4_IRQn, 3);

    //can.reset();
    can.filter(msg.id, 0xFFFFF000, CANStandard);

    // spi _ enc
    spi_enc_set_init();
    make_delay();

    //DAC init
    if (SENSING_MODE == 0) {
        dac_1 = TORQUE_VREF / 3.3f;
        dac_2 = 0.0f;
    } else if (SENSING_MODE == 1) {
        dac_1 = PRES_A_VREF / 3.3f;
        dac_2 = PRES_B_VREF / 3.3f;
    }
    make_delay();

    for (int i=0; i<50; i++) {
        if(i%2==0)
            ID_index_array[i] = - i * 0.5f;
        else
            ID_index_array[i] =  (i+1) * 0.5f;
    }
    

    /************************************
    ***     Program is operating!
    *************************************/
    while(1) {
        //if(timer_while==1000 && OPERATING_MODE==5) {
        if(timer_while==1000) {
            //i2c
            read_field(i2c_slave_addr1);
            if(DIR_VALVE_ENC < 0) value = 1023 - value;
            //            if(LED==1) {
            //                LED=0;
            //            } else
            //                LED = 1;
            timer_while = 0;
        }

        float output1[32] = { 0.0f };
        float output2[32] = { 0.0f };
        float output3[32] = { 0.0f };
        float output = 0.0f;

        timer_while ++;

//        t.reset();
//        t.start();

        for (int index2 = 0; index2 < 32; index2++) {
            for (int index1 = 0; index1 < 22; index1++) {
                output1[index2] = output1[index2]
                                  + h1[index1][index2] * input[index1];
            }
            output1[index2] = output1[index2] + b1[index2];
            if (output1[index2] < 0) {
                output1[index2] = 0;
            }
        }

        for (int index2 = 0; index2 < 32; index2++) {
            for (int index1 = 0; index1 < 32; index1++) {
                output2[index2] = output2[index2]
                                  + h2[index1][index2] * output1[index1];
            }
            output2[index2] = output2[index2] + b2[index2];
            if (output2[index2] < 0) {
                output2[index2] = 0;
            }
        }

        for (int index2 = 0; index2 < 32; index2++) {
            for (int index1 = 0; index1 < 32; index1++) {
                output3[index2] = output3[index2]
                                  + h3[index1][index2] * output2[index1];
            }
            output3[index2] = output3[index2] + b3[index2];
            if (output3[index2] < 0) {
                output3[index2] = 0;
            }
        }

        for (int index2 = 0; index2 < 1; index2++) {
            for (int index1 = 0; index1 < 32; index1++) {
                output = output + hout[index2] * output3[index1];
            }
            output = output + bout[index2];
            output = 1.0f/(1.0f+exp(-output));
        }
        
//        t.stop();
        
        //printf("NNoutput : %f\n", NNoutput);
        //printf("time : %f\n", t.read());
        
        //wait(1);
        
        //if(timer_while%10000==0)
        if(LED==1) {
             LED=0;
        } else
            LED = 1;
            
//        CAN_TX_PWM((int16_t) (t.read() * 1000000.0f)); //1500
        
//        PWM_out = NNoutput * 24000.0f - 12000.0f;
//
//
//        if (PWM_out>0.0f) {
//            dtc_v=0.0f;
//            dtc_w=PWM_out;
//        } else {
//            dtc_v=-PWM_out;
//            dtc_w=0.0f;
//        }
//
//        //pwm
//        TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
//        TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
        
        
        
    }
}

float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
{

    int i = 0;
    float Ref_Valve_Pos_FF = 0.0f;
    for(i=0; i<VALVE_POS_NUM; i++) {
        if(REF_JOINT_VEL >= min(JOINT_VEL[i],JOINT_VEL[i+1]) && REF_JOINT_VEL <=  max(JOINT_VEL[i],JOINT_VEL[i+1])) {
            if(i==0) {
                if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
                    Ref_Valve_Pos_FF = (float) VALVE_CENTER;
                } else {
                    Ref_Valve_Pos_FF = ((float) 10/(JOINT_VEL[i+1] - JOINT_VEL[i]) * (REF_JOINT_VEL - JOINT_VEL[i])) + (float) VALVE_CENTER;
                }
            } else {
                if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
                    Ref_Valve_Pos_FF = (float) VALVE_CENTER;
                } else {
                    Ref_Valve_Pos_FF = ((float) 10*(ID_index_array[i+1] - ID_index_array[i-1])/(JOINT_VEL[i+1] - JOINT_VEL[i-1]) * (REF_JOINT_VEL - JOINT_VEL[i-1])) + (float) VALVE_CENTER + (float) (10*ID_index_array[i-1]);
                }
            }
            break;
        }
    }
    if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
        Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
    } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
        Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
    }

    Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
    return Ref_Valve_Pos_FF;

}


void VALVE_POS_CONTROL(float REF_VALVE_POS)
{
    int i = 0;

    if(REF_VALVE_POS > VALVE_MAX_POS) {
        REF_VALVE_POS = VALVE_MAX_POS;
    } else if(REF_VALVE_POS < VALVE_MIN_POS) {
        REF_VALVE_POS = VALVE_MIN_POS;
    }

    valve_pos_err = REF_VALVE_POS - value;
    valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
    valve_pos_err_old = valve_pos_err;
    valve_pos_err_sum += valve_pos_err;
    if (valve_pos_err_sum > 1000) valve_pos_err_sum = 1000;
    if (valve_pos_err_sum<-1000) valve_pos_err_sum = -1000;

    VALVE_PWM_RAW_FB = P_GAIN_VALVE_POSITION * valve_pos_err + I_GAIN_VALVE_POSITION * valve_pos_err_sum + D_GAIN_VALVE_POSITION * valve_pos_err_diff;

    for(i=0; i<24; i++) {
        if(REF_VALVE_POS >= min(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1]) && REF_VALVE_POS <=  max(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1])) {
            if(i==0) {
                VALVE_PWM_RAW_FF = (float) 1000.0f / (float) (VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i]);
            } else {
                VALVE_PWM_RAW_FF = (float) 1000.0f* (float) (ID_index_array[i+1] - ID_index_array[i-1])/(VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i-1]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i-1]) + 1000.0f * (float) ID_index_array[i-1];
            }
            break;
        }
    }
    Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
}

#define LT_MAX_IDX  57
float LT_PWM_duty[LT_MAX_IDX] = {-100.0f, -80.0f, -60.0f, -50.0f, -40.0f, -35.0f, -30.0f, -25.0f, -20.0f,
                                 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
                                 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
                                 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                                 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
                                 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
                                };  // duty
float LT_Voltage_Output[LT_MAX_IDX] = {-321.4f, -291.3f, -261.5f, -246.8f, -231.7f, -223.9f, -216.1f, -207.9f, -198.8f,
                                       -196.9f, -195.0f, -192.5f, -188.8f, -184.5f, -180.2f, -175.9f, -171.5f, -166.3f, -161.0f,
                                       -156.0f, -149.5f, -139.0f, -126.0f, -107.0f, -87.5f, -64.0f, -38.5f, -9.4f, 0.0f,
                                       12.0f, 43.5f, 69.0f, 94.0f, 114.0f, 132.0f, 146.0f, 155.5f, 162.3f, 168.2f,
                                       173.1f, 178.2f, 182.8f, 187.4f, 191.8f, 196.0f, 199.7f, 201.9f, 203.8f, 205.6f,
                                       214.6f, 222.5f, 230.4f, 238.2f, 253.3f, 268.0f, 297.6f, 327.7f
                                      }; // mV

float PWM_duty_byLT(float Ref_V)
{
    float PWM_duty = 0.0f;
    if(Ref_V<LT_Voltage_Output[0]) {
        PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
    } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
        PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
    } else {
        int idx = 0;
        for(idx=0; idx<LT_MAX_IDX-1; idx++) {
            float ini_x = LT_Voltage_Output[idx];
            float fin_x = LT_Voltage_Output[idx+1];
            float ini_y = LT_PWM_duty[idx];
            float fin_y = LT_PWM_duty[idx+1];
            if(Ref_V>=ini_x && Ref_V<fin_x) {
                PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
                break;
            }
        }
    }

    return PWM_duty;
}





/*******************************************************************************
                            TIMER INTERRUPT
*******************************************************************************/

float FREQ_TMR4 = (float)FREQ_20k;
float DT_TMR4 = (float)DT_20k;
long  CNT_TMR4 = 0;
int   TMR4_FREQ_10k = (int)FREQ_10k;
extern "C" void TIM4_IRQHandler(void)
{
    if (TIM4->SR & TIM_SR_UIF ) {

        /*******************************************************
        ***     Sensor Read & Data Handling
        ********************************************************/

        //Encoder
        if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
            ENC_UPDATE();
        }
        
//        for(int i=0; i<10; i++){
//            position_array[i] = position_array[i+1];
//        }
//        position_array[10] = (pos.ref/(float)(ENC_PULSE_PER_POSITION)+10.0f)/90.0f;
//        
//        for(int i=0; i<5; i++){
//            input_array[i] = position_array[2*i+2] - (pos.ref/(float)(ENC_PULSE_PER_POSITION)+10.0f)/90.0f;
//        } 

        ADC1->CR2  |= 0x40000000;
        if (SENSING_MODE == 0) {
            // Torque Sensing (0~210)bar =============================================
            float pres_A_new = (((float) ADC1->DR) - 2047.5f);
            double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
            pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
            torq.sen = pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;

        } else if (SENSING_MODE == 1) {
            // Pressure Sensing (0~210)bar =============================================
            float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
            float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
            double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
            pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
            pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
            CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
            CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;

            if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
                torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm
            } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
                torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N
            }
        }

//        //Pressure sensor A
//        ADC1->CR2  |= 0x40000000;                        // adc _ 12bit
//        //while((ADC1->SR & 0b10));
//        float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
//        float pres_A_new = ((float)ADC1->DR);
//        pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
//        torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047    //SW just changed the sign to correct the direction of loadcell on LIGHT. Correct later.
//
//
//        //Pressure sensor B
//        float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
//        float pres_B_new = ((float)ADC2->DR);
//        pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
//        //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;


        //Current
        //ADC3->CR2  |= 0x40000000;                        // adc _ 12bit
        //int raw_cur = ADC3->DR;
        //while((ADC3->SR & 0b10));
        float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
        float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
        cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
        //cur.sen = raw_cur;

        CNT_TMR4++;
    }
    TIM4->SR = 0x0;  // reset the status register
}


int j =0;
float FREQ_TMR3 = (float)FREQ_5k;
float DT_TMR3 = (float)DT_5k;
int cnt_trans = 0;
double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
int can_rest =0;

extern "C" void TIM3_IRQHandler(void)
{
    if (TIM3->SR & TIM_SR_UIF ) {

        if (((OPERATING_MODE&0b110)>>1) == 0) {
            K_v = 0.4f; // Moog (LPM >> mA) , 100bar
            mV_PER_mA = 500.0f; // 5000mV/10mA
            mV_PER_pulse = 0.5f; // 5000mV/10000pulse
            mA_PER_pulse = 0.001f; // 10mA/10000pulse
        } else if (((OPERATING_MODE&0b110)>>1) == 1) {
            K_v = 0.5f; // KNR (LPM >> mA) , 100bar
            mV_PER_mA = 166.6666f; // 5000mV/30mA
            mV_PER_pulse = 0.5f; // 5000mV/10000pulse
            mA_PER_pulse = 0.003f; // 30mA/10000pulse
        }

        if(MODE_POS_FT_TRANS == 1) {
            alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
            cnt_trans++;
            torq.err_sum = 0;
            if((float)cnt_trans * DT_TMR3 > 3.0f)
                MODE_POS_FT_TRANS = 2;
        } else if(MODE_POS_FT_TRANS == 3) {
            alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
            cnt_trans++;
            torq.err_sum = 0;
            if((float) cnt_trans * DT_TMR3 > 3.0f )
                MODE_POS_FT_TRANS = 0;
        } else if(MODE_POS_FT_TRANS == 2) {
            alpha_trans = 1.0f;
            cnt_trans = 0;
        } else {
            alpha_trans = 0.0f;
            cnt_trans = 0;
        }


        int UTILITY_MODE = 0;
        int CONTROL_MODE = 0;

        if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
            UTILITY_MODE = CONTROL_UTILITY_MODE;
            CONTROL_MODE = MODE_NO_ACT;
        } else {
            CONTROL_MODE = CONTROL_UTILITY_MODE;
            UTILITY_MODE = MODE_NO_ACT;
        }



        // UTILITY MODE ------------------------------------------------------------

        switch (UTILITY_MODE) {
            case MODE_NO_ACT: {
                break;
            }

            case MODE_TORQUE_SENSOR_NULLING: {
                // DAC Voltage reference set
                if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
                    CUR_TORQUE_sum += torq.sen;

                    if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
                        CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
                        CUR_TORQUE_sum = 0;

                        TORQUE_VREF += 0.0003f * (0.0f - CUR_TORQUE_mean);

                        if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
                        if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;

                        //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
                        dac_1 = TORQUE_VREF / 3.3f;
                    }
                } else {
                    CONTROL_UTILITY_MODE = MODE_NO_ACT;
                    TMR3_COUNT_TORQUE_NULL = 0;
                    CUR_TORQUE_sum = 0;
                    CUR_TORQUE_mean = 0;

                    ROM_RESET_DATA();

                    dac_1 = TORQUE_VREF / 3.3f;

                }
                TMR3_COUNT_TORQUE_NULL++;
                break;
            }

//            case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
//                if (TMR3_COUNT_DEADZONE == 0) {
//                    if (pos_plus_end == pos_minus_end) need_enc_init = true;
//                    else temp_time = 0;
//                }
//                if (need_enc_init) {
//                    if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
//                        V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
//                        pos_plus_end = pos.sen;
//                    } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
//                        V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
//                        pos_minus_end = pos.sen;
//                    } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
//                    temp_time = TMR_FREQ_5k;
//                }
//
//                if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
//                    V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen);
//                    VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
//
//                } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
//                    V_out = 0;
//                    CUR_VELOCITY_sum += CUR_VELOCITY;
//                } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
//                    if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
//                    else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
//                    else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
//                    else DZ_temp_cnt2 = DZ_end;
//                    CUR_VELOCITY_sum = 0;
//                } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
//                    if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
//
//                    // Position of Dead Zone
//                    //  (CUR_VELOCITY < 0)  (CUR_VELOCITY == 0)  (CUR_VELOCITY > 0)
//                    //     |        /                 |    /                      |/
//                    //     | ______/               ___|___/                ______/|
//                    //     |/                     /   |                   /       |
//                    //    /|                     /    |                  /        |
//                    //     0V                         0V                          0V
//
//                    if (DZ_temp_cnt2 < DZ_end) {
//                        if (TMR3_COUNT_DEADZONE % 20 != 0) {
//                            CUR_VELOCITY_sum += CUR_VELOCITY;
//                        } else {
//                            V_out -= DZ_dir;
//                            if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
//                            CUR_VELOCITY_sum = 0;
//                        }
//                        if (DZ_temp_cnt == 5) {
//                            if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
//                            else VALVE_DEADZONE_PLUS = (int16_t) V_out;
//                            DZ_dir = -DZ_dir;
//                            DZ_temp_cnt = 0;
//                            DZ_temp_cnt2++;
//                        }
//                    } else {
//                        TMR3_COUNT_DEADZONE = -1;
//                        VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
//                        if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
//                            VALVE_DEADZONE_PLUS = VALVE_CENTER;
//                            VALVE_DEADZONE_MINUS = VALVE_CENTER;
//                        }
//                        V_out = 0;
//
//                        ROM_RESET_DATA();
//
//                        //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
//                        //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
//
//                        CONTROL_MODE = MODE_NO_ACT;
//                        DZ_temp_cnt2 = 0;
//                    }
//                }
//                TMR3_COUNT_DEADZONE++;
//                break;
//            }

            case MODE_FIND_HOME: {
                if (FINDHOME_STAGE == FINDHOME_INIT) {
                    cnt_findhome = 0;
                    cnt_vel_findhome = 0;
                    //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
                    pos.ref = pos.sen;
                    vel.ref = 0.0f;
                    FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
                } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
                    int cnt_check_enc = (TMR_FREQ_5k/500);
                    if(cnt_findhome%cnt_check_enc == 0) {
                        FINDHOME_POSITION = pos.sen;
                        FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
                        FINDHOME_POSITION_OLD = FINDHOME_POSITION;
                    }
                    cnt_findhome++;

                    if (abs(FINDHOME_VELOCITY) <= 1) {
                        cnt_vel_findhome = cnt_vel_findhome + 1;
                    } else {
                        cnt_vel_findhome = 0;
                    }

                    if ((cnt_vel_findhome < 3*TMR_FREQ_5k) &&  cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
                        //REFERENCE_MODE = MODE_REF_NO_ACT;
                        if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 2.0f;
                        else pos.ref = pos.ref - 2.0f;

//                        pos.err = pos.ref_home_pos - pos.sen;
//                        float VALVE_POS_RAW_POS_FB = 0.0f;
//                        VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
//                        valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
//                        VALVE_POS_CONTROL(valve_pos.ref);

                        CONTROL_MODE = MODE_JOINT_CONTROL;
                        alpha_trans = 0.0f;


                    } else {
                        ENC_SET(HOMEPOS_OFFSET);
                        INIT_REF_POS = HOMEPOS_OFFSET;
                        REF_POSITION = 0;
                        REF_VELOCITY = 0;
                        FINDHOME_POSITION = 0;
                        FINDHOME_POSITION_OLD = 0;
                        FINDHOME_VELOCITY = 0;
                        cnt_findhome = 0;
                        cnt_vel_findhome = 0;
                        FINDHOME_STAGE = FINDHOME_ZEROPOSE;
                    }
                } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
                    int T_move = 2*TMR_FREQ_5k;
                    pos.ref = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS;
                    vel.ref = 0.0f;

                    // input for position control

                    CONTROL_MODE = MODE_JOINT_CONTROL;
                    alpha_trans = 0.0f;
//                    pos.err = pos.ref - (float)pos.sen;
//                    float VALVE_POS_RAW_POS_FB = 0.0f;
//                    VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
//                    valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
//                    VALVE_POS_CONTROL(valve_pos.ref);

                    cnt_findhome++;
                    if (cnt_findhome >= T_move) {
                        //REFERENCE_MODE = MODE_REF_DIRECT;
                        cnt_findhome = 0;
                        pos.ref = 0.0f;
                        vel.ref = 0.0f;
                        pos.ref_home_pos = 0.0f;
                        vel.ref_home_pos = 0.0f;
                        FINDHOME_STAGE = FINDHOME_INIT;
                        CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
                    }
                }

                break;
            }

//            case MODE_VALVE_GAIN_SETTING: {
//                if (TMR3_COUNT_FLOWRATE == 0) {
//                    if (pos_plus_end == pos_minus_end) need_enc_init = true;
//                    else {
//                        V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
//                        temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
//                    }
//                }
//                if (need_enc_init) {
//                    if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
//                        V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
//                        pos_plus_end = pos.sen;
//                    } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
//                        V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
//                        pos_minus_end = pos.sen;
//                    } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
//                        need_enc_init = false;
//                        check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
//                        check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
//                        check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
//                    }
//                    temp_time = TMR_FREQ_5k;
//                }
//                TMR3_COUNT_FLOWRATE++;
//                if (TMR3_COUNT_FLOWRATE > temp_time) {
//                    if (flag_flowrate % 2 == 0) { // (+)
//                        VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
//                        V_out = VALVE_VOLTAGE;
//                        if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
//                            fl_temp_cnt++;
//                        } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
//                            VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0 * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // 0.9587=6*pi/65536*10000 0.5757=0.02525*0.02*0.0095*2*60*1000
//                            //                        VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // PULSE/sec
//                            fl_temp_cnt2++;
//                        }
//                    } else if (flag_flowrate % 2 == 1) { // (-)
//                        VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
//                        V_out = VALVE_VOLTAGE;
//                        if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
//                            fl_temp_cnt++;
//                        } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
//                            VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0f * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE);
//                            //                        VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); // PULSE/sec
//                            fl_temp_cnt2++;
//                        }
//                    }
//                    if (fl_temp_cnt2 == 100) {
//
//                        ROM_RESET_DATA();
//
//                        //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
//                        cur_vel_sum = 0;
//                        fl_temp_cnt = 0;
//                        fl_temp_cnt2 = 0;
//                        flag_flowrate++;
//                    }
//                    if (flag_flowrate == 10) {
//                        V_out = 0;
//                        flag_flowrate = 0;
//                        TMR3_COUNT_FLOWRATE = 0;
//                        valve_gain_repeat_cnt++;
//                        if (valve_gain_repeat_cnt >= 1) {
//                            CONTROL_MODE = MODE_NO_ACT;
//                            valve_gain_repeat_cnt = 0;
//                        }
//
//                    }
//                    break;
//                }
//
//            }
            case MODE_PRESSURE_SENSOR_NULLING: {
                // DAC Voltage reference set
                if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
                    CUR_PRES_A_sum += pres_A.sen;
                    CUR_PRES_B_sum += pres_B.sen;

                    if (TMR3_COUNT_PRES_NULL % 10 == 0) {
                        CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
                        CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
                        CUR_PRES_A_sum = 0;
                        CUR_PRES_B_sum = 0;

                        float VREF_NullingGain = 0.0003f;
                        PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
                        PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;

                        if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
                        if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
                        if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
                        if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;

                        dac_1 = PRES_A_VREF / 3.3f;
                        dac_2 = PRES_B_VREF / 3.3f;
                    }
                } else {
                    CONTROL_UTILITY_MODE = MODE_NO_ACT;
                    TMR3_COUNT_PRES_NULL = 0;
                    CUR_PRES_A_sum = 0;
                    CUR_PRES_B_sum = 0;
                    CUR_PRES_A_mean = 0;
                    CUR_PRES_B_mean = 0;

                    ROM_RESET_DATA();

                    dac_1 = PRES_A_VREF / 3.3f;
                    dac_2 = PRES_B_VREF / 3.3f;
                    //pc.printf("nulling end");
                }
                TMR3_COUNT_PRES_NULL++;
                break;
            }

//            case MODE_PRESSURE_SENSOR_CALIB: {
//                if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
//                    V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
//                    if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
//                        CUR_PRES_A_sum += CUR_PRES_A;
//                    }
//                } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
//                    V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
//                    if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
//                        CUR_PRES_B_sum += CUR_PRES_B;
//                    }
//                } else {
//                    CONTROL_MODE = MODE_NO_ACT;
//                    TMR3_COUNT_PRES_CALIB = 0;
//                    V_out = 0;
//                    PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
//                    PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
//                    PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
//                    PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
//                    CUR_PRES_A_sum = 0;
//                    CUR_PRES_B_sum = 0;
//                    CUR_PRES_A_mean = 0;
//                    CUR_PRES_B_mean = 0;
//
//                    ROM_RESET_DATA();
//
//                    //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
//                    //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
//                }
//                TMR3_COUNT_PRES_CALIB++;
//                break;
//            }

//            case MODE_ROTARY_FRICTION_TUNING: {
//                if (TMR3_COUNT_ROTARY_FRIC_TUNE % (5 * TMR_FREQ_5k) == 0) freq_fric_tune = 4.0f + 3.0f * sin(2 * 3.14159f * 0.5f * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f * 0.05f);
//                V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
//                if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
//                else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
//                TMR3_COUNT_ROTARY_FRIC_TUNE++;
//                if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
//                    TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
//                    V_out = 0.0f;
//                    CONTROL_MODE = MODE_NO_ACT;
//                }
//                break;
//            }

            case MODE_DDV_POS_VS_PWM_ID: {
                CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
                VALVE_ID_timer = VALVE_ID_timer + 1;

                if(VALVE_ID_timer < TMR_FREQ_5k*1) {
                    Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
                } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
                    Vout.ref = 1000.0f*(ID_index_array[ID_index]);
                } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
                    VALVE_POS_TMP = 0;
                    data_num = 0;
                } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
                    data_num = data_num + 1;
                    VALVE_POS_TMP = VALVE_POS_TMP + value;
                } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
                    Vout.ref = 0.0f;
                } else {
                    VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
                    VALVE_ID_timer = 0;
                    ID_index= ID_index +1;
                }

                if(ID_index>=25) {
                    int i;
                    VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
                    for(i=0; i<25; i++) {
                        VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
                        if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
                            VALVE_MAX_POS = VALVE_POS_AVG[i];
                            VALVE_POS_AVG_OLD = VALVE_MAX_POS;
                        } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
                            VALVE_MIN_POS = VALVE_POS_AVG[i];
                            VALVE_POS_AVG_OLD = VALVE_MIN_POS;
                        }
                    }
                    ROM_RESET_DATA();
                    ID_index = 0;
                    CONTROL_UTILITY_MODE = MODE_NO_ACT;
                }


                break;
            }

            case MODE_DDV_DEADZONE_AND_CENTER: {
                CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
                VALVE_DZ_timer = VALVE_DZ_timer + 1;
                if(first_check == 0) {
                    if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
                    } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
                        pos_plus_end = pos.sen;
                    } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
                    } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
                        pos_minus_end = pos.sen;
                    } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                    } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                        data_num = data_num + 1;
                        VALVE_POS_TMP = VALVE_POS_TMP + value;
                    } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                        DDV_POS_AVG = VALVE_POS_TMP / data_num;
                        START_POS = pos.sen;
                        VALVE_POS_TMP = 0;
                        data_num = 0;

                    } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
                        valve_pos.ref = DDV_POS_AVG;
                        VALVE_POS_CONTROL(valve_pos.ref);

                    } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
                        valve_pos.ref = DDV_POS_AVG;
                        VALVE_POS_CONTROL(valve_pos.ref);

                    } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
                        valve_pos.ref = DDV_POS_AVG;
                        VALVE_POS_CONTROL(valve_pos.ref);
                        FINAL_POS = pos.sen;

                        if((FINAL_POS - START_POS)>200) {
                            DZ_case = 1;
                        } else if((FINAL_POS - START_POS)<-200) {
                            DZ_case = -1;
                        } else {
                            DZ_case = 0;
                        }

                        CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));

                        first_check = 1;
                        DZ_DIRECTION = 1;
                        VALVE_DZ_timer = 0;
                        Ref_Valve_Pos_Old = DDV_POS_AVG;
                        DZ_NUM = 1;
                        DZ_index = 1;

                    }
                } else {
                    if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
                        if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
                            Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                            //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
                            //CONTROL_MODE = MODE_JOINT_CONTROL;
                        } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                            START_POS = pos.sen;
                        } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                            valve_pos.ref = Ref_Valve_Pos_Old  - DZ_case * DZ_DIRECTION * 64 / DZ_index;
                            if(valve_pos.ref <= VALVE_MIN_POS) {
                                valve_pos.ref = VALVE_MIN_POS;
                            } else if(valve_pos.ref >= VALVE_MAX_POS) {
                                valve_pos.ref = VALVE_MAX_POS;
                            }
                            VALVE_POS_CONTROL(valve_pos.ref);

                        } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Ref_Valve_Pos_Old = valve_pos.ref;
                            FINAL_POS = pos.sen;

                            if((FINAL_POS - START_POS)>100) {
                                DZ_DIRECTION = 1 * DZ_case;
                            } else if((FINAL_POS - START_POS)<-100) {
                                DZ_DIRECTION = -1 * DZ_case;
                            } else {
                                DZ_DIRECTION = 1 * DZ_case;
                            }

                            VALVE_DZ_timer = 0;
                            DZ_index= DZ_index *2;
                            if(DZ_index >= 128) {
                                FIRST_DZ = valve_pos.ref;
                                DZ_NUM = 2;
                                Ref_Valve_Pos_Old = FIRST_DZ;
                                DZ_index = 1;
                                DZ_DIRECTION = 1;
                            }
                        }
                    } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
                        if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                            Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                            //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
                            //CONTROL_MODE = MODE_JOINT_CONTROL;
                        } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                            START_POS = pos.sen;
                        } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                            valve_pos.ref = Ref_Valve_Pos_Old  - DZ_case * DZ_DIRECTION * 64 / DZ_index;
                            if(valve_pos.ref <= VALVE_MIN_POS) {
                                valve_pos.ref = VALVE_MIN_POS;
                            } else if(valve_pos.ref >= VALVE_MAX_POS) {
                                valve_pos.ref = VALVE_MAX_POS;
                            }
                            VALVE_POS_CONTROL(valve_pos.ref);

                        } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Vout.ref = 0.0f;
                        } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Ref_Valve_Pos_Old = valve_pos.ref;
                            FINAL_POS = pos.sen;

                            if((FINAL_POS - START_POS)>100) {
                                DZ_DIRECTION = 1 * DZ_case;
                            } else if((FINAL_POS - START_POS)<-100) {
                                DZ_DIRECTION = -1 * DZ_case;
                            } else {
                                DZ_DIRECTION = -1 * DZ_case;
                            }

                            VALVE_DZ_timer = 0;
                            DZ_index= DZ_index * 2;
                            if(DZ_index >= 128) {
                                SECOND_DZ = valve_pos.ref;
                                VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
                                first_check = 0;
                                VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
                                VALVE_DEADZONE_PLUS = (float) SECOND_DZ;

                                ROM_RESET_DATA();

                                CONTROL_UTILITY_MODE = MODE_NO_ACT;
                                DZ_index = 1;
                            }
                        }
                    } else if(DZ_case == 0 && DZ_NUM ==1) {
                        if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                            Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                            //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
                            //CONTROL_MODE = MODE_JOINT_CONTROL;
                        } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                            START_POS = pos.sen;
                        } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                            valve_pos.ref = Ref_Valve_Pos_Old  - DZ_DIRECTION * 64 / DZ_index;
                            if(valve_pos.ref <= VALVE_MIN_POS) {
                                valve_pos.ref = VALVE_MIN_POS;
                            } else if(valve_pos.ref >= VALVE_MAX_POS) {
                                valve_pos.ref = VALVE_MAX_POS;
                            }
                            VALVE_POS_CONTROL(valve_pos.ref);

                        } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Ref_Valve_Pos_Old = valve_pos.ref;
                            FINAL_POS = pos.sen;

                            if((FINAL_POS - START_POS)>100) {
                                DZ_DIRECTION = 1;
                            } else if((FINAL_POS - START_POS)<-100) {
                                DZ_DIRECTION = -1;
                            } else {
                                DZ_DIRECTION = 1;
                            }
                            VALVE_DZ_timer = 0;
                            DZ_index= DZ_index *2;
                            if(DZ_index >= 128) {
                                FIRST_DZ = valve_pos.ref;
                                DZ_NUM = 2;
                                Ref_Valve_Pos_Old = FIRST_DZ;
                                DZ_index = 1;
                                DZ_DIRECTION = 1;
                            }
                        }
                    } else {
                        if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                            Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                            //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
                            //CONTROL_MODE = MODE_JOINT_CONTROL;
                        } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                            START_POS = pos.sen;
                        } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                            valve_pos.ref = Ref_Valve_Pos_Old  + DZ_DIRECTION * 64 / DZ_index;
                            if(valve_pos.ref <= VALVE_MIN_POS) {
                                valve_pos.ref = VALVE_MIN_POS;
                            } else if(valve_pos.ref > VALVE_MAX_POS) {
                                valve_pos.ref = VALVE_MAX_POS - 1;
                            }
                            VALVE_POS_CONTROL(valve_pos.ref);

                        } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Vout.ref = 0.0f;
                        } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
                            Ref_Valve_Pos_Old = valve_pos.ref;
                            FINAL_POS = pos.sen;

                            if((FINAL_POS - START_POS)>100) {
                                DZ_DIRECTION = 1;
                            } else if((FINAL_POS - START_POS)<-100) {
                                DZ_DIRECTION = -1;
                            } else {
                                DZ_DIRECTION = 1;
                            }

                            VALVE_DZ_timer = 0;
                            DZ_index= DZ_index *2;
                            if(DZ_index >= 128) {
                                SECOND_DZ = valve_pos.ref;
                                VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
                                first_check = 0;
                                VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
                                VALVE_DEADZONE_PLUS = (float) SECOND_DZ;

                                ROM_RESET_DATA();

                                CONTROL_UTILITY_MODE = MODE_NO_ACT;
                                DZ_index = 1;
                            }
                        }
                    }
                }
                break;
            }

            case MODE_DDV_POS_VS_FLOWRATE: {
                CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
                VALVE_FR_timer = VALVE_FR_timer + 1;
                if(first_check == 0) {
                    if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
                        //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
                    } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
                        pos_plus_end = pos.sen;
                        //                    CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
                    } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
                        Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
                    } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
                        //                    CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
                        Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
                        pos_minus_end = pos.sen;
                        first_check = 1;
                        VALVE_FR_timer = 0;
                        valve_pos.ref = (float) VALVE_CENTER;
                        ID_index = 0;
                        max_check = 0;
                        min_check = 0;
                    }
                } else {
                    if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
                        //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
                        pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
                        CONTROL_MODE = MODE_JOINT_CONTROL;
                    } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
                        data_num = 0;
                        valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;

                        VALVE_POS_CONTROL(valve_pos.ref);
                        START_POS = pos.sen;
                    } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
                        valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
                        VALVE_POS_CONTROL(valve_pos.ref);
                        data_num = data_num + 1;
                        if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
                            FINAL_POS = pos.sen;
                            one_period_end = 1;
                        }
                    } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
                        FINAL_POS = pos.sen;
                        one_period_end = 1;
                        V_out = 0.0f;
                    }

                    if(one_period_end == 1) {
                        if(valve_pos.ref > VALVE_MAX_POS) {
                            max_check = 1;
                        } else if(valve_pos.ref < VALVE_MIN_POS) {
                            min_check = 1;
                        }
                        JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k;   //  pulse/sec

                        VALVE_FR_timer = 0;
                        one_period_end = 0;
                        ID_index= ID_index +1;
                        V_out = 0.0f;
                    }

                    if(max_check == 1 && min_check == 1) {

                        VALVE_POS_NUM = ID_index;
                        ROM_RESET_DATA();
                        ID_index = 0;
                        first_check = 0;
                        VALVE_FR_timer = 0;
                        CONTROL_UTILITY_MODE = MODE_NO_ACT;
//                        CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
                    }
                }
                break;
            }

            case MODE_SYSTEM_ID: {
                freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.;
                valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
                CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
                cnt_sysid++;
                if (freq_sysid_Iref >= 300) {
                    cnt_sysid = 0;
                    CONTROL_UTILITY_MODE = MODE_NO_ACT;
                }
                break;
            }



            default:
                break;
        }

        // CONTROL MODE ------------------------------------------------------------

        switch (CONTROL_MODE) {
            case MODE_NO_ACT: {
                V_out = 0.0f;
                break;
            }

            case MODE_VALVE_POSITION_CONTROL: {
                if (((OPERATING_MODE&0b110)>>1) == 0) { //Moog Valve
                    I_REF = valve_pos.ref;
                } else if (((OPERATING_MODE&0b110)>>1) == 1) { //KNR Valve
                    V_out = valve_pos.ref;
                } else { //SW Valve
                    VALVE_POS_CONTROL(valve_pos.ref);
                    V_out = Vout.ref;
                }

                break;
            }

            case MODE_JOINT_CONTROL: {
                double torq_ref = 0.0f;
                pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
                vel.err = (vel.ref - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
                pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
                torq_ref = torq.ref + (K_SPRING * pos.err * 0.01f + D_DAMPER * vel.err * 0.0001f) / ENC_PULSE_PER_POSITION; //[N]

                // torque feedback
                torq.err = (torq_ref)/(float)(TORQUE_SENSOR_PULSE_PER_TORQUE)  - torq.sen; //[N]
                torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]

                if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {

                    double I_REF_POS = 0.0f;
                    double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
                    double I_REF_VC = 0.0f; // I_REF for velocity compensation

                    double temp_vel_pos = 0.0f;
                    double temp_vel_torq = 0.0f;
                    double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control

                    if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode
                        temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
                        //                            L when P-gain = 100, f_cut = 10Hz                                 L feedforward velocity
                    } else if ((OPERATING_MODE && 0x01) == 1) {
                        temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s
                        //                            L when P-gain = 100, f_cut = 10Hz                                 L feedforward velocity
                    }
                    if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f))));
                    else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));

                    // velocity compensation for torque control
                    if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode
                        I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
                        //                temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s
                        temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
                        //                                                          L feedforward velocity
                    } else if ((OPERATING_MODE && 0x01) == 1) {
                        I_REF_FORCE_FB = 0.001f * 0.01f*((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); // Linear Actuators are more sensitive.
                        //                temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
                        temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
                        //                                                          L feedforward velocity
                    }
                    if (temp_vel_torq > 0.0f) I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f))));
                    else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
                    //                                                  L   velocity(rad/s or mm/s) >> I_ref(mA)
                    //            Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
                    //            Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;

                    I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);

                } else {
                    float VALVE_POS_RAW_FORCE_FB = 0.0f;

                    VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) * 0.01f
                                                          + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));

                    if (VALVE_POS_RAW_FORCE_FB >= 0) {
                        valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
                    } else {
                        valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
                    }

                    if(I_GAIN_JOINT_TORQUE != 0) {
                        double Ka = 1.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
                        if(valve_pos.ref>VALVE_MAX_POS) {
                            double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
                            valve_pos_rem = valve_pos_rem * Ka;
                            valve_pos.ref = VALVE_MAX_POS;
                            torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
                        } else if(valve_pos.ref < VALVE_MIN_POS) {
                            double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
                            valve_pos_rem = valve_pos_rem * Ka;
                            valve_pos.ref = VALVE_MIN_POS;
                            torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
                        }
                    }

                    VALVE_POS_CONTROL(valve_pos.ref);

                    Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
                    V_out = (float) Vout.ref;

                }

                break;
            }

            case MODE_VALVE_OPEN_LOOP: {
                V_out = (float) Vout.ref;
                break;
            }

            default:
                break;
        }


        if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve

            ////////////////////////////////////////////////////////////////////////////
            ////////////////////////////  CURRENT CONTROL //////////////////////////////
            ////////////////////////////////////////////////////////////////////////////
            if (CURRENT_CONTROL_MODE) {
                double alpha_update_Iref = 1.0f / (1.0f + TMR_FREQ_5k / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
                I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;

                I_ERR = I_REF_fil - cur.sen;
                I_ERR_INT = I_ERR_INT + (I_ERR) * DT_TMR3;


                // Moog Valve Current Control Gain
                double R_model = 539.0f; // ohm
                double L_model = 1.2f;
                double w0 = 2.0f * 3.14f * 150.0f;
                double KP_I = 0.1f * L_model*w0;
                double KI_I = 0.1f * R_model*w0;

                // KNR Valve Current Control Gain
                if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
                    R_model = 163.0f; // ohm
                    L_model = 1.0f;
                    w0 = 2.0f * 3.14f * 80.0f;
                    KP_I = 1.0f * L_model*w0;
                    KI_I = 0.08f * R_model*w0;
                }

                double FF_gain = 1.0f;

                VALVE_PWM_RAW = KP_I * I_ERR + KI_I * I_ERR_INT;
                //        VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
                I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
                I_REF_fil_old = I_REF_fil;
                VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * TMR_FREQ_5k); // Unit : mV
                double V_MAX = 12000.; // Maximum Voltage : 12V = 12000mV

                double Ka = 3.0f / KP_I;
                if (VALVE_PWM_RAW > V_MAX) {
                    V_rem = VALVE_PWM_RAW - V_MAX;
                    V_rem = Ka*V_rem;
                    VALVE_PWM_RAW = V_MAX;
                    I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3;
                } else if (VALVE_PWM_RAW < -V_MAX) {
                    V_rem = VALVE_PWM_RAW - (-V_MAX);
                    V_rem = Ka*V_rem;
                    VALVE_PWM_RAW = -V_MAX;
                    I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3;
                }
                Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
            } else {
                VALVE_PWM_RAW = I_REF * mV_PER_mA;
                Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
            }

            ////////////////////////////////////////////////////////////////////////////
            /////////////////  Dead Zone Cancellation & Linearization //////////////////
            ////////////////////////////////////////////////////////////////////////////
            // Dead Zone Cancellation (Mechanical Valve dead-zone)
            if (FLAG_VALVE_DEADZONE) {
                if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
                else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV

                VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV

            } else {
                VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
            }

            // Output Voltage Linearization
            double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
            double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin);

            // Dead Zone Cancellation (Electrical dead-zone)
            if (CUR_PWM_lin > 0) V_out = (int) CUR_PWM_lin + 140;
            else if (CUR_PWM_lin < 0) V_out = (int) CUR_PWM_lin - 140;
            else V_out = CUR_PWM_lin;
        }



        /*******************************************************
        ***     PWM
        ********************************************************/
        if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
            V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
        } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
            V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
        }
        PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV

        // Saturation of output voltage to 12.0V
        if(PWM_out > 1.0f) PWM_out=1.0f;
        else if (PWM_out < -1.0f) PWM_out=-1.0f;

        if (PWM_out>0.0f) {
            dtc_v=0.0f;
            dtc_w=PWM_out;
        } else {
            dtc_v=-PWM_out;
            dtc_w=0.0f;
        }

        //pwm
        TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
        TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
        

        if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {

            // Position, Velocity, and Torque (ID:1200)
            if (flag_data_request[0] == HIGH) {
                if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
                    if (SENSING_MODE == 0) {
                        CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (pres_A.sen*10.0f));
                    } else if (SENSING_MODE == 1) {
                        CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
                    }
                } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
                    if (SENSING_MODE == 0) {
                        CAN_TX_POSITION_FT((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (pres_A.sen));
                    } else if (SENSING_MODE == 1) {
                        CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
                    }
                }
            }
            if (flag_data_request[1] == HIGH) {
                //valve position
                double t_value = 0;
//                if(value>=(float) VALVE_CENTER) {
//                    t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
//                } else {
//                    t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
//                }
                if(OPERATING_MODE==5) {
                    t_value = (double) value;
                } else if(CURRENT_CONTROL_MODE==1) {
                    t_value = cur.sen;
                } else {
                    t_value = V_out;
                }
                CAN_TX_TORQUE((int16_t) (t_value), (int16_t) (V_out)); //1300
            }


            if (flag_data_request[2] == HIGH) {
                //pressure A and B
                CAN_TX_PRES((int16_t) (pres_A.sen), (int16_t) (pres_B.sen)); // CUR_PRES_X : 0(0bar)~4096(210bar) //1400
            }

            //If it doesn't rest, below can can not work.
            for (can_rest = 0; can_rest < 10000; can_rest++) {
                ;
            }

            if (flag_data_request[3] == HIGH) {
                //PWM
                CAN_TX_PWM((int16_t) cur.sen); //1500
            }
            if (flag_data_request[3] == LOW) {
                //PWM
                CAN_TX_PWM((int16_t) 79); //1500
            }
            //for (i = 0; i < 10000; i++) {
//                ;
//            }
            if (flag_data_request[4] == HIGH) {
                //valve position
                CAN_TX_VALVE_POSITION((int16_t) (CAN_FREQ), (int16_t) (D_DAMPER), (int16_t) OPERATING_MODE); //1600
            }

            // Others : Reference position, Reference FT, PWM, Current  (ID:1300)
//        if (flag_data_request[1] == HIGH) {
//            CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
//        }
            //if (flag_delay_test == 1){
            //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
            //}

            TMR2_COUNT_CAN_TX = 0;
        }
        TMR2_COUNT_CAN_TX++;

    }
    TIM3->SR = 0x0;  // reset the status register

}