20210203

Dependencies:   mbed FastPWM

main.cpp

Committer:
Lightvalve
Date:
2020-07-06
Revision:
82:d286749256a4
Parent:
81:49a733a3eb9e
Child:
83:b5bba84861d8

File content as of revision 82:d286749256a4:

//200706-1
#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();
  }
}


float x_past[num_array_x_past] = {0.0f};
float f_past[num_array_f_past] = {0.0f};
float f_future[num_array_f_future] = {0.0f};

float input_NN[num_input] = { 0.0f };

const float h1[num_input][32] = { 
   {-0.7324461936950684f,0.7409296631813049f,0.10589459538459778f,-0.38551005721092224f,-3.5221376419067383f,-0.21096330881118774f,-1.892266869544983f,1.1542932987213135f,0.960740327835083f,-0.10343249887228012f,-0.21933841705322266f,-0.4212304353713989f,-0.19318288564682007f,0.2659827470779419f,-0.21103838086128235f,-5.196728229522705f,0.06015032157301903f,0.24935457110404968f,1.6254364252090454f,0.48002195358276367f,-0.33476194739341736f,0.10696089267730713f,-0.3326931595802307f,-0.0792258083820343f,-0.24545392394065857f,0.18374285101890564f,-0.1767144799232483f,-0.9713953733444214f,-0.25970596075057983f,-0.18794111907482147f,-2.653707265853882f,0.379783570766449f},
{-0.7168723344802856f,0.5038977265357971f,0.29864487051963806f,-0.6717129349708557f,-2.562722682952881f,-0.14344151318073273f,-0.9430316686630249f,0.4483363926410675f,0.5943819284439087f,0.1834067404270172f,-0.31018689274787903f,-0.6483522653579712f,0.021212786436080933f,0.1980869472026825f,0.045876264572143555f,-3.5974650382995605f,-0.14573998749256134f,0.06295260787010193f,1.814133644104004f,0.5035092234611511f,-0.03096979856491089f,-0.12077532708644867f,-0.09506890177726746f,0.27714261412620544f,-0.3832830488681793f,0.2936786115169525f,0.12686115503311157f,-0.39219725131988525f,-0.4316449463367462f,0.09809011220932007f,-2.178504705429077f,0.26320457458496094f},
{-0.10389315336942673f,0.19780808687210083f,-0.0042223334312438965f,0.05603449419140816f,-1.7615299224853516f,-0.18307602405548096f,-0.6851590275764465f,-0.2277992069721222f,0.4095189869403839f,0.15345525741577148f,0.2607969343662262f,-0.7411863803863525f,-0.03282937407493591f,0.21980543434619904f,-0.31718650460243225f,-1.882696509361267f,0.2537345588207245f,-0.2658354640007019f,1.3627671003341675f,0.2939450442790985f,0.22159990668296814f,-0.15311938524246216f,0.13552400469779968f,-0.24659553170204163f,-0.4851999580860138f,0.14537742733955383f,-0.15514154732227325f,-0.1821879744529724f,-0.37288668751716614f,0.302763432264328f,-2.0377211570739746f,-0.16412392258644104f},
{-0.02771024964749813f,-0.2862888276576996f,-0.20532599091529846f,-0.35345712304115295f,-1.75230073928833f,0.022886822000145912f,-0.4297986924648285f,-0.8416728377342224f,0.4213387668132782f,0.06797961890697479f,0.09849962592124939f,-0.6748783588409424f,-0.11809042096138f,0.36359959840774536f,-0.12817290425300598f,-0.27267178893089294f,-0.18019500374794006f,-0.23756909370422363f,0.765958845615387f,-0.10520157963037491f,-0.24942269921302795f,-0.08793863654136658f,-0.14710915088653564f,-0.29148542881011963f,-0.4763914942741394f,0.14159366488456726f,0.13380053639411926f,-0.19485153257846832f,0.286895215511322f,0.1323157548904419f,-1.7547060251235962f,0.07635010778903961f},
{0.3236381411552429f,-0.374115526676178f,0.08410122990608215f,0.00805189460515976f,-1.0858323574066162f,0.013666743412613869f,0.41090333461761475f,-1.6205281019210815f,-0.7114768028259277f,0.6650916934013367f,-0.2693946063518524f,-1.0753451585769653f,0.211430162191391f,0.25044503808021545f,-0.25075238943099976f,1.3431744575500488f,-0.43130552768707275f,-0.18436601758003235f,0.4112853407859802f,-0.5496231913566589f,0.1375632882118225f,0.23587647080421448f,-0.24541926383972168f,-0.07457953691482544f,-0.9904118776321411f,-0.34256690740585327f,0.04057341814041138f,-0.22224822640419006f,0.4641643762588501f,-0.16320037841796875f,-0.944225549697876f,-0.21381261944770813f},
{1.011043667793274f,-1.3360116481781006f,0.30513831973075867f,1.141945242881775f,2.0353193283081055f,0.058615077286958694f,3.288475751876831f,-3.8103392124176025f,-2.601891040802002f,2.3005900382995605f,0.26046672463417053f,-2.587841749191284f,-0.03144201636314392f,0.07914004474878311f,0.21901831030845642f,3.6866838932037354f,-0.9526890516281128f,-0.3195672333240509f,-2.679140567779541f,-0.8484804630279541f,0.13521116971969604f,-0.009676039218902588f,0.12587067484855652f,0.3309326469898224f,-3.5203857421875f,0.20814630389213562f,-0.2925715744495392f,0.792920708656311f,0.5511527061462402f,-0.0013132691383361816f,2.330298900604248f,-0.45225751399993896f},
{0.0163522120565176f,-0.09881188720464706f,0.16288265585899353f,-0.07697032392024994f,0.8232660293579102f,-0.028030529618263245f,-2.3201069831848145f,-2.466125011444092f,0.36204829812049866f,-0.8584702014923096f,-0.11520722508430481f,-0.48068881034851074f,0.098605215549469f,-0.18164213001728058f,-0.18274356424808502f,-1.0025761127471924f,-0.10595233738422394f,-0.08166468143463135f,1.664667010307312f,-0.09866604954004288f,-0.16223669052124023f,-0.1771286129951477f,-0.2626192569732666f,-0.24521145224571228f,0.6187260746955872f,-0.15535086393356323f,-0.1126367449760437f,-0.07650785893201828f,-0.3861798048019409f,-0.03486502170562744f,2.691161870956421f,0.0956985354423523f},
{0.1788516640663147f,-0.24759119749069214f,0.16644713282585144f,-0.05095084011554718f,2.630812883377075f,-0.22031055390834808f,-1.0256447792053223f,1.1981055736541748f,-0.20504942536354065f,-0.3157905340194702f,-0.2549065351486206f,-0.5909162759780884f,0.10806858539581299f,-0.18600939214229584f,0.054826945066452026f,2.342839002609253f,-0.09849568456411362f,-0.28619682788848877f,-2.2247517108917236f,-0.1220199465751648f,-0.07430306077003479f,0.022674113512039185f,-0.12776102125644684f,-0.02844339609146118f,1.1212466955184937f,0.0177537202835083f,-0.341458797454834f,-0.017731331288814545f,-0.3365504741668701f,0.004316955804824829f,1.4245057106018066f,0.22477316856384277f},
{0.07522846758365631f,-0.5892174243927002f,0.13710039854049683f,0.13978177309036255f,1.9179061651229858f,-0.2723221182823181f,-0.4519940912723541f,1.1835663318634033f,-0.9499168395996094f,0.4928976893424988f,-0.31565770506858826f,-0.19895850121974945f,0.11499327421188354f,0.1757679432630539f,0.24149659276008606f,2.529064416885376f,-0.24949464201927185f,0.06021764874458313f,-3.2398345470428467f,-0.2815665900707245f,-0.30298954248428345f,-0.022329479455947876f,-0.2963099479675293f,0.08446213603019714f,-0.04937029629945755f,-0.08218517899513245f,-0.08906334638595581f,0.37760984897613525f,0.18576934933662415f,-0.11924156546592712f,1.3386632204055786f,0.03108183480799198f},
{-0.37845155596733093f,-0.08440887182950974f,-0.24446426331996918f,0.13863100111484528f,0.9723972678184509f,-0.16841982305049896f,0.2753590941429138f,1.0015891790390015f,-0.810889720916748f,0.36379313468933105f,-0.37399381399154663f,0.06687716394662857f,0.08400186896324158f,0.06476005911827087f,-0.26784712076187134f,1.0108706951141357f,0.19997769594192505f,0.3319900929927826f,-2.334019422531128f,-0.2126695066690445f,-0.20100386440753937f,0.1638341248035431f,-0.11680193245410919f,-0.321638286113739f,0.3033031225204468f,0.05633258819580078f,0.13587942719459534f,0.21672657132148743f,0.057419002056121826f,0.30370619893074036f,0.9737980365753174f,0.14423033595085144f},
{-0.35210731625556946f,-0.3368627429008484f,-0.06658771634101868f,0.08506923168897629f,0.4059661328792572f,-0.28213056921958923f,0.7525542378425598f,1.247084140777588f,-0.7127273678779602f,0.13458484411239624f,-0.36268576979637146f,-0.4665933847427368f,-0.1789563000202179f,0.22581127285957336f,0.017138004302978516f,0.4740210473537445f,-0.2751494348049164f,-0.24391496181488037f,-1.9174745082855225f,-0.333028644323349f,0.16973629593849182f,-0.25340327620506287f,0.23975160717964172f,0.011333763599395752f,0.3212549090385437f,0.10268864035606384f,0.08055207133293152f,-0.467197448015213f,-0.00708851357921958f,0.029612571001052856f,0.8009700775146484f,0.17681513726711273f},
{-0.0508580319583416f,0.17910829186439514f,-0.3080642819404602f,-0.23870371282100677f,0.402355819940567f,-0.5370544791221619f,0.38948336243629456f,1.4379266500473022f,-0.45911040902137756f,0.5110005736351013f,0.2755727767944336f,-0.29720327258110046f,-0.03757423162460327f,-0.19858907163143158f,-0.2992876172065735f,0.25499260425567627f,0.0008029160089790821f,-0.1821068376302719f,-1.7392560243606567f,-0.2617977559566498f,0.29002442955970764f,0.2275833785533905f,-0.23374228179454803f,0.3280802071094513f,0.1756536066532135f,0.2108195722103119f,0.33828380703926086f,-0.1146455854177475f,0.02285323664546013f,0.1480078399181366f,0.6982804536819458f,-0.012264931574463844f},
{-0.010969211347401142f,-0.571060061454773f,0.19288143515586853f,0.10581295192241669f,0.8521704077720642f,-0.19241008162498474f,0.7120941281318665f,1.1234700679779053f,-0.6035223007202148f,0.9677979946136475f,-0.1646130084991455f,-0.15344341099262238f,-0.2720222473144531f,-0.37935397028923035f,0.06489166617393494f,0.5313313603401184f,-0.33358800411224365f,0.21822473406791687f,-1.0015411376953125f,-0.33920755982398987f,-0.25171342492103577f,0.027203470468521118f,0.012447118759155273f,-0.0029686689376831055f,0.2576296031475067f,0.03022313117980957f,0.3424017131328583f,-0.08670900762081146f,-0.15304048359394073f,-0.34525999426841736f,0.7017830610275269f,0.12626364827156067f},
{-0.34738433361053467f,-0.11634252220392227f,0.06283923983573914f,0.03090018965303898f,0.07186907529830933f,-0.07297156006097794f,1.3323267698287964f,0.9581816792488098f,-0.3099403381347656f,0.7785437107086182f,0.27633070945739746f,-0.1801576465368271f,0.03354460000991821f,0.021888820454478264f,0.1072375476360321f,0.0018482001032680273f,-0.16910125315189362f,0.08871319890022278f,-0.2313135415315628f,0.08976637572050095f,-0.2227887064218521f,-0.01532900333404541f,0.11891454458236694f,-0.33143535256385803f,0.30407238006591797f,-0.1485452950000763f,0.25882163643836975f,-0.23462599515914917f,0.0756872296333313f,0.24182012677192688f,0.24460257589817047f,0.21474632620811462f},
{0.018077274784445763f,-0.24098707735538483f,0.25620588660240173f,-0.05411790311336517f,-0.05704647675156593f,0.05454791709780693f,1.6305421590805054f,1.0214358568191528f,-0.1747228056192398f,0.48898813128471375f,-0.06157088279724121f,-0.041419290006160736f,0.18931713700294495f,-0.3282688856124878f,0.03958520293235779f,0.028830667957663536f,-0.015212026424705982f,-0.07219305634498596f,0.42320799827575684f,0.18164488673210144f,0.31619134545326233f,0.26330670714378357f,0.026431173086166382f,0.237522691488266f,0.1339150369167328f,0.19442179799079895f,0.1992029845714569f,-0.48437225818634033f,-0.23117280006408691f,-0.10433453321456909f,0.046231336891651154f,-0.2575237452983856f},
{-0.48448505997657776f,0.08657374978065491f,-0.13789941370487213f,-0.001528827240690589f,0.18942709267139435f,0.10840918868780136f,1.560402750968933f,0.6214654445648193f,-0.4458804428577423f,0.6866056323051453f,-0.3386222720146179f,0.15203092992305756f,0.06288954615592957f,-0.21535031497478485f,0.28114965558052063f,-0.15658745169639587f,0.038470298051834106f,-0.14027439057826996f,-0.12108602374792099f,-0.21894791722297668f,0.18525317311286926f,0.07025247812271118f,-0.12988737225532532f,0.04382038116455078f,0.37654832005500793f,-0.12333536148071289f,-0.12442702054977417f,-0.15937449038028717f,-0.1718931794166565f,0.27953919768333435f,0.48613524436950684f,0.15145954489707947f},
{-0.0400209054350853f,-0.2747725546360016f,0.23826703429222107f,-0.17471440136432648f,-0.3626048266887665f,-0.19125176966190338f,1.9813239574432373f,0.36226052045822144f,0.035454537719488144f,0.8329153656959534f,-0.36310845613479614f,-0.2834755480289459f,0.012096136808395386f,-0.0497019998729229f,-0.03951779007911682f,-0.2741624414920807f,-0.07435613125562668f,0.2508954107761383f,0.0010071241995319724f,-0.20140500366687775f,0.22447606921195984f,0.30379387736320496f,-0.006355375051498413f,-0.11041811108589172f,-0.06714245676994324f,-0.28886765241622925f,-0.05783697962760925f,0.031109891831874847f,0.08988594263792038f,-0.1556464582681656f,-0.35560739040374756f,-0.16616936028003693f},
};

const float h2[32][32] = { 
    {-0.3233126699924469f,-0.10295046865940094f,-0.04925942420959473f,0.028001029044389725f,-0.15490937232971191f,0.2402254343032837f,-0.009092477150261402f,-0.09383764117956161f,-0.30411389470100403f,-0.10008957982063293f,0.12772084772586823f,-0.3111713230609894f,0.012173167429864407f,-0.2510131895542145f,-0.20470082759857178f,-0.4046356678009033f,0.07803884148597717f,-0.21483641862869263f,-0.06435013562440872f,0.0016537902411073446f,-0.5275964736938477f,-0.37845200300216675f,-0.23983746767044067f,-0.29126548767089844f,-0.4172869324684143f,0.05886608734726906f,-0.4203137159347534f,-0.3401240110397339f,0.23828622698783875f,0.11771553754806519f,-0.24613501131534576f,-0.27408090233802795f},
{-0.7742806077003479f,0.002147972583770752f,-0.04321768879890442f,0.2295239269733429f,0.039372533559799194f,-0.025343723595142365f,-0.6232535243034363f,-0.2030182033777237f,0.24677053093910217f,0.048597872257232666f,-0.29219910502433777f,0.18143370747566223f,0.17267045378684998f,-1.1579262018203735f,0.029419085010886192f,0.42441555857658386f,0.056778669357299805f,-0.2737269401550293f,-0.12574294209480286f,0.21240612864494324f,-0.06682362407445908f,-0.17620179057121277f,-0.16832955181598663f,0.17329838871955872f,0.5303869247436523f,-0.10751811414957047f,-0.476502925157547f,0.12136103957891464f,-0.28620439767837524f,-0.0511949360370636f,0.05717116594314575f,-0.0838746577501297f},
{-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.30145350098609924f,-0.01806354522705078f,-0.23058539628982544f,0.036581140011548996f,-0.27190759778022766f,-0.25008028745651245f,-0.43496081233024597f,-0.054719261825084686f,-0.06944303214550018f,0.09271347522735596f,-0.5551741123199463f,-0.47675442695617676f,-0.28422942757606506f,-0.9006205797195435f,-0.37641534209251404f,-0.2267972081899643f,0.10996055603027344f,0.24023392796516418f,0.12340197712182999f,-0.20969530940055847f,-0.3575495779514313f,-0.48650968074798584f,-0.06394433975219727f,-0.3389127850532532f,-0.23638612031936646f,0.055264804512262344f,-0.1449035406112671f,-0.3893917202949524f,-0.22396358847618103f,0.2546447217464447f,-0.09510714560747147f,-0.03329534828662872f},
{-0.5843220353126526f,0.2506926953792572f,-0.0804666131734848f,0.059204310178756714f,-0.11763454228639603f,-0.40854984521865845f,-1.593937873840332f,-0.1407748907804489f,0.043158918619155884f,-0.2690317630767822f,-0.9261438250541687f,-2.550259828567505f,-0.3895283639431f,-1.4387625455856323f,-0.6720712184906006f,-4.8835954666137695f,-0.10307048261165619f,0.15626338124275208f,3.0023770332336426f,0.009725779294967651f,-0.24121296405792236f,-0.2457580715417862f,0.19172045588493347f,-0.5267027616500854f,0.17413386702537537f,-0.26919829845428467f,-0.4927682876586914f,0.10982076078653336f,0.11138012260198593f,-0.05381976068019867f,-0.14540907740592957f,-0.33406519889831543f},
{-0.19158349931240082f,-0.004258692264556885f,0.032371193170547485f,-0.2922333776950836f,-0.023485273122787476f,-0.21373715996742249f,0.0017751825507730246f,-0.14440131187438965f,-0.03548547625541687f,0.08825305104255676f,-0.16768305003643036f,0.1999492049217224f,-0.2578849494457245f,0.1645275205373764f,-0.20099258422851562f,-0.14849749207496643f,0.2371552288532257f,0.24713221192359924f,0.2731606066226959f,0.16465769708156586f,-0.01827855035662651f,0.1846475601196289f,-0.1307595670223236f,-0.0736805647611618f,0.13213670253753662f,-0.279893159866333f,0.20242686569690704f,-0.13792051374912262f,0.05800282955169678f,-0.26823562383651733f,0.17187702655792236f,0.15163694322109222f},
{-0.6624926328659058f,-0.30315548181533813f,-0.11171227693557739f,0.24856127798557281f,-0.2269049882888794f,0.10569709539413452f,-1.1323813199996948f,-0.35414770245552063f,-0.26620519161224365f,0.17144301533699036f,-0.5042814612388611f,-1.0794178247451782f,0.09224560111761093f,-0.9259583353996277f,1.6006993055343628f,-0.5644950270652771f,-0.16153597831726074f,0.18742787837982178f,-4.573171615600586f,0.0647876188158989f,-0.06991176307201385f,-0.30434662103652954f,-0.07988794147968292f,-0.4169270992279053f,-3.2402853965759277f,-1.0891244411468506f,-0.5646524429321289f,-0.36112168431282043f,-0.27477923035621643f,0.10233109444379807f,-0.28240182995796204f,-0.15972262620925903f},
{-0.3740357756614685f,-0.23163960874080658f,0.04012581706047058f,0.00230496097356081f,0.08348319679498672f,-0.2912064492702484f,-1.748984932899475f,-0.13795287907123566f,0.2864855229854584f,0.01006925106048584f,-1.6497565507888794f,1.2684836387634277f,-0.2492595613002777f,-2.1554017066955566f,0.19397760927677155f,0.4792504906654358f,-0.2721019387245178f,0.2706601917743683f,-4.52908992767334f,-0.1432938575744629f,-0.09787218272686005f,-0.25326859951019287f,-0.07979777455329895f,-0.047466348856687546f,-1.0241167545318604f,0.8771169185638428f,-0.9902152419090271f,-0.2526286244392395f,-0.29799938201904297f,0.009232791140675545f,0.006896080914884806f,-0.10395918786525726f},
{-0.3416319191455841f,0.3232463300228119f,-0.021617114543914795f,0.09345877170562744f,0.1531517654657364f,-0.05689593032002449f,-1.218935251235962f,-0.3824131190776825f,0.29470762610435486f,-0.06864601373672485f,-0.8985402584075928f,-0.01473225373774767f,0.008866318501532078f,-1.410807728767395f,1.7128677368164062f,0.6200125217437744f,0.046292245388031006f,0.06025278568267822f,-0.026382790878415108f,0.22570835053920746f,-0.13317528367042542f,-0.2663436830043793f,0.28743305802345276f,-0.12336486577987671f,-0.6454019546508789f,0.22416429221630096f,-0.6884293556213379f,-0.05984973907470703f,0.09702112525701523f,-0.18618243932724f,-0.3938767910003662f,-0.37032800912857056f},
{0.06367537379264832f,-0.1944228857755661f,-0.29237738251686096f,-0.24619391560554504f,0.03965183347463608f,-0.3089410066604614f,0.028653467074036598f,-0.30879589915275574f,0.18755429983139038f,-0.07031691074371338f,0.053658001124858856f,0.4843670129776001f,-0.16650398075580597f,-0.7546054124832153f,-0.2978924810886383f,-0.7792365550994873f,-0.015168905258178711f,-0.2859399914741516f,-0.03901958465576172f,-0.22692938148975372f,-0.2877960801124573f,-0.21215878427028656f,-0.2899571657180786f,-0.12436526268720627f,0.3370738923549652f,-0.6824218034744263f,-0.8391618132591248f,-0.22130538523197174f,0.052816230803728104f,-0.04029931128025055f,-0.0010054409503936768f,-0.27258118987083435f},
{0.19981321692466736f,0.1956864297389984f,-0.033508092164993286f,-0.22838544845581055f,-0.12505969405174255f,0.026693344116210938f,0.08343051373958588f,0.14285165071487427f,-0.17670965194702148f,0.17403557896614075f,-0.18852870166301727f,0.10983473062515259f,-0.1060299277305603f,-0.0033903091680258512f,-0.16456294059753418f,0.05858567729592323f,0.03990337252616882f,0.0716913640499115f,0.1532106250524521f,-0.08638858795166016f,0.25297799706459045f,0.09457655996084213f,0.07470300793647766f,0.30162063241004944f,0.0566137433052063f,0.0595703087747097f,-0.009822680614888668f,-0.2516980767250061f,0.13817378878593445f,0.17853263020515442f,-0.009185433387756348f,-0.02637580782175064f},
{-1.4907565116882324f,-0.09556084871292114f,0.12328919768333435f,0.07313191890716553f,-0.4092276990413666f,0.09345024824142456f,-0.7848790884017944f,-0.24536018073558807f,0.17735600471496582f,0.009893029928207397f,-0.7946430444717407f,0.026100417599081993f,-0.30592235922813416f,-1.028707504272461f,-0.4708610773086548f,-0.2440120428800583f,0.009990394115447998f,0.10842388868331909f,-0.07803342491388321f,0.1765805184841156f,-0.3464567959308624f,-0.23086123168468475f,0.09996896982192993f,-0.05010664090514183f,-0.17763382196426392f,0.3989022970199585f,-0.38085007667541504f,0.06118621677160263f,-0.4420720636844635f,-0.30588340759277344f,0.023392902687191963f,0.11829513311386108f},
{-0.08816954493522644f,0.2728963792324066f,-0.07170480489730835f,0.1482785940170288f,0.07727676630020142f,0.20119628310203552f,0.03849402070045471f,0.02873826026916504f,-0.15294449031352997f,-0.038666754961013794f,-0.1574239432811737f,0.27019134163856506f,0.1312139928340912f,-0.26498496532440186f,0.1620480716228485f,0.12264445424079895f,0.023397088050842285f,0.21900424361228943f,0.25839516520500183f,0.2068612277507782f,-0.29656288027763367f,0.045920878648757935f,-0.19714993238449097f,0.22523340582847595f,0.07844719290733337f,0.22350957989692688f,-0.04062908887863159f,-0.26378923654556274f,0.18573936820030212f,0.15478622913360596f,0.013760745525360107f,0.010002970695495605f},
{0.14700931310653687f,0.1833711564540863f,-0.0858893096446991f,0.0614105761051178f,-0.25116482377052307f,0.2733006179332733f,0.6875689029693604f,-0.16134384274482727f,0.000652611255645752f,-0.22920364141464233f,0.7882081270217896f,-0.7014076709747314f,0.26723334193229675f,0.7679355144500732f,-0.435951292514801f,-0.6919644474983215f,-0.07963520288467407f,0.14483880996704102f,-0.28649914264678955f,-0.20337045192718506f,-0.0700601264834404f,0.20099303126335144f,-0.2228696048259735f,0.2573260962963104f,-0.06171654537320137f,-0.14574120938777924f,-0.5463026762008667f,0.08074890822172165f,-0.011228948831558228f,0.26896968483924866f,-0.28556761145591736f,-0.0726936087012291f},
{-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.7255915999412537f,0.1317739188671112f,0.03260180354118347f,-0.19140556454658508f,-0.2063623070716858f,-0.3916719853878021f,-1.2569708824157715f,-0.028363805264234543f,0.05445769429206848f,0.07657262682914734f,-1.261732578277588f,-4.106509208679199f,-0.05181446298956871f,-2.1050660610198975f,-1.7625566720962524f,1.258276104927063f,-0.25386664271354675f,-0.18997573852539062f,1.4711048603057861f,-0.26434141397476196f,-0.2588711082935333f,-0.41244617104530334f,-0.3044300675392151f,-0.021670076996088028f,-3.0161335468292236f,-2.141369342803955f,-1.062567114830017f,-0.5045756101608276f,-0.1588822603225708f,-0.2239055633544922f,-0.1912272870540619f,0.10602492839097977f},
{-0.6162709593772888f,-0.062024205923080444f,0.10704237222671509f,-0.18120908737182617f,-0.04875987768173218f,-0.0061126272194087505f,-0.474213570356369f,0.1336868852376938f,-0.23304778337478638f,-0.035415440797805786f,-0.5262596011161804f,0.054680559784173965f,0.08294253796339035f,-0.6178011298179626f,-0.02124152146279812f,0.041992392390966415f,-0.027234435081481934f,0.19620582461357117f,-0.030048342421650887f,-0.2688896954059601f,-0.14993496239185333f,-0.26845231652259827f,-0.19671492278575897f,-0.4124130606651306f,-0.24481038749217987f,0.01430027186870575f,-0.11357278376817703f,0.22084195911884308f,-0.2531542181968689f,0.24004116654396057f,-0.21812304854393005f,-0.3099365830421448f},
{0.1379433274269104f,-0.16078773140907288f,0.11784148216247559f,0.2671110928058624f,0.2693568766117096f,0.19759181141853333f,0.08533552289009094f,0.17868870496749878f,0.040603965520858765f,-0.2119520902633667f,0.1136421263217926f,-0.24326792359352112f,-0.07898660004138947f,0.2795462906360626f,-0.22247430682182312f,-0.0708579272031784f,0.18204066157341003f,0.2297056019306183f,0.034212857484817505f,0.05284380912780762f,-0.15357805788516998f,-0.2940097451210022f,0.26029130816459656f,0.2647087275981903f,0.1594548225402832f,0.04613301157951355f,-0.22981436550617218f,0.10197556018829346f,-0.08671019971370697f,-0.019541800022125244f,0.30332449078559875f,0.15016543865203857f},
{0.037222720682621f,0.11332181841135025f,-0.0814327746629715f,-0.027964258566498756f,-0.1585979014635086f,-0.15052323043346405f,-1.8530080318450928f,0.06395404040813446f,-0.2925014793872833f,0.022429823875427246f,-1.443596601486206f,1.7770520448684692f,-0.3932042717933655f,-0.7809745669364929f,-0.2989632189273834f,-0.01567240059375763f,0.1296386420726776f,0.027322769165039062f,0.7680656909942627f,-0.0349733941257f,-0.10412891209125519f,-0.2298865169286728f,-0.03486085683107376f,-0.11272889375686646f,-3.057006359100342f,0.4281110167503357f,-1.4503432512283325f,-0.2588704228401184f,-0.012737486511468887f,-0.22627900540828705f,-0.3389904797077179f,-0.0320734977722168f},
{-0.5281551480293274f,0.29651907086372375f,0.02990397810935974f,-0.05468868464231491f,0.1826970875263214f,-0.2695440948009491f,-0.017192618921399117f,0.1249740794301033f,-0.2325194776058197f,-0.2458522766828537f,0.02350984886288643f,0.21151113510131836f,-0.2042773962020874f,-0.7171024084091187f,0.2226519137620926f,0.5389895439147949f,0.2577105462551117f,0.11064860224723816f,-0.0508388914167881f,-0.016880039125680923f,0.22808200120925903f,-0.44513463973999023f,-0.07374785840511322f,-0.10630907118320465f,-0.3302907645702362f,-0.3197507858276367f,-0.4939371645450592f,0.22595663368701935f,-0.2205982804298401f,0.28094157576560974f,-0.1669490486383438f,0.08341114223003387f},
{0.2703488767147064f,-0.16041381657123566f,0.12738299369812012f,-0.03611093759536743f,0.1169431209564209f,0.1861400008201599f,-0.1765512377023697f,0.28177282214164734f,0.1474648416042328f,-0.23766008019447327f,-0.005803406238555908f,0.08728843927383423f,0.09594401717185974f,-0.17028304934501648f,-0.16805587708950043f,-0.058941617608070374f,-0.22846683859825134f,0.08700510859489441f,-0.2656046748161316f,0.07154610753059387f,-0.1559111475944519f,0.22832396626472473f,-0.18301571905612946f,-0.17816279828548431f,-0.04200500249862671f,0.2835671007633209f,0.1830621361732483f,-0.26011598110198975f,0.08541131019592285f,0.08213254809379578f,-0.29332536458969116f,0.08477649092674255f},
{-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},
{-1.0259166955947876f,-0.4249313771724701f,-0.10565309226512909f,-0.07732077687978745f,0.12454111874103546f,-0.17488564550876617f,-1.0551177263259888f,-0.34218570590019226f,-0.06814777851104736f,-0.24531681835651398f,-1.5096275806427002f,3.3561782836914062f,-0.25797709822654724f,-2.601508140563965f,-3.0165209770202637f,4.079505443572998f,0.2015441358089447f,0.04417389631271362f,-1.5205823183059692f,0.09596772491931915f,-0.0331561416387558f,-0.33675655722618103f,-0.2520882785320282f,0.014313658699393272f,-1.512328028678894f,4.057154178619385f,-0.9460916519165039f,-0.1974325180053711f,-0.32825788855552673f,-0.24132376909255981f,0.04739285260438919f,0.12632279098033905f},
{-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.047657690942287445f,-0.11652402579784393f,0.24921879172325134f,0.18867287039756775f,-0.2817712128162384f,-0.27453500032424927f,-0.2238679677248001f,-0.2621168792247772f,0.23963233828544617f,0.21321198344230652f,-0.17865566909313202f,-0.09800297766923904f,-0.16443446278572083f,-0.11671604216098785f,-0.2924588620662689f,0.005072008352726698f,-0.19791248440742493f,0.2487291395664215f,-0.20952290296554565f,-0.29542285203933716f,-0.20094189047813416f,-0.009510638192296028f,-0.09928475320339203f,-0.35661420226097107f,-0.31114643812179565f,-0.15508677065372467f,-0.24212686717510223f,-0.1551239937543869f,-0.03476092219352722f,0.2736774981021881f,0.004408210515975952f,-0.18763792514801025f},
{-0.17075178027153015f,0.009595632553100586f,0.2466227114200592f,0.055009979754686356f,-0.29828932881355286f,-0.33059003949165344f,-0.056320156902074814f,-0.2985670268535614f,-0.2516406178474426f,0.012286126613616943f,0.21675583720207214f,-0.3378230333328247f,-0.09437626600265503f,-0.2456921488046646f,-0.0856185108423233f,-0.05789673328399658f,-0.24087269604206085f,-0.048499464988708496f,-0.29421383142471313f,0.01438645739108324f,-0.33456161618232727f,-0.3776583969593048f,-0.031352698802948f,0.2876163423061371f,-0.36141401529312134f,-0.2468460351228714f,-0.4790426194667816f,0.22929580509662628f,-0.07090267539024353f,-0.23986908793449402f,-0.2543276250362396f,-0.007315821945667267f},
{-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.6206833124160767f,-0.36007484793663025f,0.0620841383934021f,-0.02594662643969059f,-0.22267669439315796f,-0.31157833337783813f,-0.35370802879333496f,0.03218350186944008f,-0.21029016375541687f,0.2210421860218048f,-0.14142879843711853f,-8.121310234069824f,-0.108514204621315f,-1.20806884765625f,3.2017698287963867f,0.6867505311965942f,0.10771593451499939f,-0.29308468103408813f,-1.1648472547531128f,-0.18391931056976318f,-0.12236563861370087f,-0.3634614646434784f,-0.22392597794532776f,-0.09029920399188995f,-20.889820098876953f,-2.2144384384155273f,0.24900056421756744f,-0.07976706326007843f,0.17976179718971252f,-0.08812034130096436f,0.13261021673679352f,-0.051259275525808334f},
{-0.09888127446174622f,-0.22750279307365417f,-0.22503364086151123f,-0.031065694987773895f,-0.012498408555984497f,-0.24329537153244019f,-0.12042153626680374f,0.08175003528594971f,-0.016126900911331177f,0.06972584128379822f,-0.020760251209139824f,0.4404478967189789f,-0.031845152378082275f,-0.5313631892204285f,0.4538247883319855f,0.4567507803440094f,-0.09216172993183136f,0.04617339372634888f,0.07478172332048416f,-0.12741152942180634f,-0.042085327208042145f,-0.31553542613983154f,-0.06047184765338898f,-0.2932652533054352f,-0.0905158594250679f,-0.12291274219751358f,-0.14609231054782867f,-0.3254922330379486f,-0.16326025128364563f,0.11664235591888428f,-0.02675291895866394f,0.1203904077410698f},
};

const float h3[32][32] = { 
    {-0.2551194727420807f,-0.09101890027523041f,-0.026477575302124023f,-0.15196433663368225f,-0.2843017578125f,0.016633404418826103f,0.23563924431800842f,0.21467849612236023f,-0.23768503963947296f,-0.16189225018024445f,0.01949387975037098f,0.15432734787464142f,0.5416690707206726f,0.05566665902733803f,-0.2476848065853119f,0.13452908396720886f,0.033751994371414185f,0.2061004936695099f,0.026342852041125298f,-0.10083381831645966f,-0.7394891381263733f,-0.29829099774360657f,-0.22618433833122253f,0.044178664684295654f,0.17592744529247284f,-0.1258748173713684f,0.2930828630924225f,-0.26859936118125916f,0.1685795933008194f,0.2922307550907135f,-0.21407078206539154f,-0.051871590316295624f},
{0.310348242521286f,0.19731858372688293f,-0.055143773555755615f,0.030042022466659546f,-0.13887283205986023f,0.01627178117632866f,-0.2855546772480011f,0.011247634887695312f,0.1085631251335144f,0.021339505910873413f,0.17599248886108398f,0.09762296080589294f,0.030526816844940186f,-0.14660483598709106f,-0.21469902992248535f,0.03876742720603943f,-0.21865856647491455f,-0.12651903927326202f,0.04666612297296524f,-0.23926785588264465f,-0.14443452656269073f,0.22025445103645325f,0.14157342910766602f,-0.06798144429922104f,-0.1251431256532669f,-0.25851932168006897f,0.0932874083518982f,0.2900666892528534f,-0.023782014846801758f,0.050485759973526f,0.09447437524795532f,0.0058679282665252686f},
{-0.2800689935684204f,-0.038760632276535034f,0.13974270224571228f,0.01585569977760315f,0.11945047974586487f,-0.19458696246147156f,0.11322265863418579f,0.25643888115882874f,-0.061097174882888794f,-0.07816044986248016f,-0.07445238530635834f,-0.22145158052444458f,-0.1081896424293518f,-0.09452249109745026f,0.16234496235847473f,0.1413259506225586f,-0.2648085951805115f,-0.054869145154953f,-0.05491623282432556f,0.17325228452682495f,0.23639705777168274f,0.0748760998249054f,-0.1324603259563446f,0.15828460454940796f,-0.22871175408363342f,-0.027338534593582153f,-0.06307074427604675f,0.13438841700553894f,0.13834497332572937f,0.1633671224117279f,0.259930282831192f,-0.21565306186676025f},
{0.2906629145145416f,0.2196197658777237f,-0.2048129439353943f,0.2633720338344574f,0.07553021609783173f,-0.032275788486003876f,-0.061309605836868286f,-0.17915479838848114f,-0.048149947077035904f,-0.044261813163757324f,-0.09506386518478394f,0.24970653653144836f,0.12309885770082474f,0.09685163199901581f,0.22106656432151794f,-0.23978039622306824f,-0.28915077447891235f,0.030949801206588745f,-0.29950326681137085f,0.1836264431476593f,0.21585485339164734f,-0.009739493951201439f,-0.16484108567237854f,-0.27595841884613037f,0.22493940591812134f,-0.11024686694145203f,-0.09994620084762573f,-0.02688438631594181f,-0.1672891527414322f,-0.25852420926094055f,0.05653107166290283f,0.0037463903427124023f},
{0.07563930749893188f,-0.0864691436290741f,-0.2677825689315796f,0.1497999131679535f,-0.2632053792476654f,-0.12118913233280182f,-0.09772239625453949f,0.2470131814479828f,-0.2551872134208679f,-0.2740214169025421f,0.2979947626590729f,-0.24627932906150818f,-0.09607142210006714f,-0.1546318233013153f,0.06802645325660706f,-0.048776447772979736f,0.20131775736808777f,0.013118326663970947f,0.2238367348909378f,0.2585393488407135f,-0.03326711058616638f,0.21529874205589294f,0.1718432903289795f,0.20571765303611755f,-0.19813477993011475f,0.12758469581604004f,0.2959180772304535f,-0.20305807888507843f,0.17249789834022522f,-0.09959287941455841f,-0.07189898192882538f,0.007399916648864746f},
{0.12596839666366577f,0.14568550884723663f,0.12925085425376892f,-0.1691121906042099f,-0.14090044796466827f,0.24331282079219818f,-0.061815500259399414f,0.04091137647628784f,0.032660406082868576f,-0.2484673708677292f,0.12724275887012482f,-0.21529507637023926f,-0.014116826467216015f,0.019264258444309235f,0.2971965968608856f,0.25175121426582336f,-0.2889891266822815f,-0.13035915791988373f,0.12640652060508728f,0.06524023413658142f,0.06571434438228607f,-0.08267568051815033f,0.023232191801071167f,0.256379634141922f,0.026501981541514397f,-0.18302330374717712f,0.09057381749153137f,-0.004888213239610195f,-0.27729424834251404f,-0.29547566175460815f,-0.25800079107284546f,0.40328702330589294f},
{-0.21155381202697754f,0.6564063429832458f,-0.2628793716430664f,-1.0410994291305542f,-0.5127164125442505f,0.3808554708957672f,-0.07957221567630768f,0.2444917857646942f,-0.16960306465625763f,0.5487350821495056f,0.7583959102630615f,-0.25618287920951843f,0.40037423372268677f,0.25831204652786255f,0.25543639063835144f,-0.1192857176065445f,-0.18917083740234375f,-0.11552592366933823f,-0.163506880402565f,0.03761041909456253f,0.6083343625068665f,0.2957093119621277f,-0.26556405425071716f,-0.2255190908908844f,0.4039100408554077f,-0.2910519242286682f,-0.32265353202819824f,-0.6550114154815674f,0.4487787187099457f,-0.09532724320888519f,0.023906931281089783f,0.26367390155792236f},
{-0.01093912124633789f,-0.23583689332008362f,0.28153905272483826f,0.13032647967338562f,-0.07508793473243713f,-0.11246801167726517f,0.036741942167282104f,-0.0059101879596710205f,-0.4039776921272278f,-0.11471008509397507f,0.09319102764129639f,-0.08139897882938385f,0.03910289704799652f,0.13743947446346283f,0.0685860812664032f,-0.29903873801231384f,0.1317853033542633f,0.20550194382667542f,-0.12251400947570801f,-0.28424710035324097f,0.08722569048404694f,0.2752329111099243f,0.0343703031539917f,0.12564969062805176f,0.18976923823356628f,0.23886635899543762f,-0.2778390347957611f,-0.019107135012745857f,0.28547611832618713f,0.17161712050437927f,-0.017215043306350708f,-0.05991865694522858f},
{-0.20420776307582855f,0.1970202624797821f,0.23591408133506775f,-0.282694011926651f,0.09019026160240173f,0.20012763142585754f,0.028453975915908813f,0.23323127627372742f,0.15246456861495972f,-0.1270071268081665f,0.20025727152824402f,0.13077861070632935f,-0.2238168716430664f,-0.20709244906902313f,-0.0045506954193115234f,-0.16448761522769928f,0.28163614869117737f,0.1971360743045807f,0.30405697226524353f,-0.28959211707115173f,-0.2255709171295166f,0.15360668301582336f,0.24163606762886047f,0.19554737210273743f,-0.1804599016904831f,0.07604315876960754f,-0.1459394246339798f,-0.14081311225891113f,-0.29656586050987244f,0.19529584050178528f,-0.05168652534484863f,-0.03415781259536743f},
{0.27550336718559265f,-0.14170598983764648f,-0.21542268991470337f,-0.2159879207611084f,0.06500789523124695f,-0.21155211329460144f,-0.01575431227684021f,0.0617731511592865f,0.13126209378242493f,-0.04922589659690857f,-0.23877641558647156f,-0.06292802095413208f,0.01433342695236206f,0.14592674374580383f,-0.23886999487876892f,0.08878573775291443f,0.1853090226650238f,-0.05102238059043884f,-0.13667166233062744f,-0.24401302635669708f,0.25155118107795715f,-0.20339438319206238f,0.03535670042037964f,0.048473477363586426f,-0.13871997594833374f,-0.15382859110832214f,-0.13837768137454987f,0.26085415482521057f,0.2491457164287567f,0.157204270362854f,0.14108964800834656f,0.12518465518951416f},
{-0.023678362369537354f,0.5319188237190247f,-0.24700739979743958f,0.2642342746257782f,-0.21380269527435303f,0.5601517558097839f,0.0393199622631073f,0.196751207113266f,-0.537047266960144f,0.10823977738618851f,0.8011711835861206f,-0.06168833374977112f,0.6660637259483337f,0.2502623200416565f,0.080221027135849f,-0.2471318244934082f,0.026521950960159302f,0.16163073480129242f,-0.1901899129152298f,-0.43416815996170044f,-0.36087092757225037f,0.6420853734016418f,0.05331581085920334f,0.2614522874355316f,0.63054358959198f,-0.25962090492248535f,-0.10450930893421173f,-0.9246357679367065f,-0.1607130914926529f,-0.2912617325782776f,-0.32391759753227234f,-0.44677719473838806f},
{0.3407846689224243f,-0.15367960929870605f,-0.348749577999115f,-2.1230273246765137f,-0.06339437514543533f,-2.1875176429748535f,-0.0018706796690821648f,-0.17794576287269592f,0.08020078390836716f,-1.166637897491455f,-0.9363284707069397f,0.013093339279294014f,0.8193063735961914f,-0.06932337582111359f,0.23152390122413635f,-0.07731422781944275f,0.05630389228463173f,-0.15284278988838196f,0.2577855587005615f,0.28169524669647217f,0.05924757942557335f,0.20316094160079956f,-0.2750208377838135f,-0.021603485569357872f,-0.3904852867126465f,0.025629352778196335f,-0.2948634922504425f,0.21841423213481903f,0.07357483357191086f,-0.1739533692598343f,-0.47626638412475586f,-0.3502570390701294f},
{0.10771462321281433f,0.1270637810230255f,-0.1647956818342209f,-0.15007510781288147f,0.10816431045532227f,0.28367120027542114f,-0.2502448558807373f,-0.05063694715499878f,-0.2787182927131653f,-0.11501708626747131f,0.24353471398353577f,0.11519002914428711f,-0.1879904866218567f,0.1970348358154297f,-0.0055807530879974365f,0.05870962142944336f,-0.0561012327671051f,-0.1419317126274109f,-0.24982497096061707f,-0.05947013199329376f,-0.21285000443458557f,-0.11344525218009949f,0.2986377775669098f,-0.2888573110103607f,0.002569987205788493f,-0.03866580128669739f,0.09892630577087402f,0.1957591027021408f,0.1267586350440979f,0.024399906396865845f,-0.264041006565094f,-0.037910252809524536f},
{0.13293679058551788f,0.0799899622797966f,-0.11364158242940903f,0.607576310634613f,-0.07702409476041794f,-0.050367020070552826f,0.19400060176849365f,-0.09019742906093597f,-0.0878034457564354f,-0.03290947154164314f,0.46468329429626465f,-0.12769456207752228f,0.12451063096523285f,-0.04707755520939827f,0.13272255659103394f,-0.0684008002281189f,-0.13351139426231384f,0.11981581896543503f,0.01592940464615822f,0.05004088208079338f,-0.498890221118927f,0.2979670763015747f,-0.07797680050134659f,0.3322955369949341f,0.3608349859714508f,-0.1976800560951233f,-0.40144115686416626f,-0.1867043673992157f,0.5227740406990051f,-0.29330405592918396f,-0.035194240510463715f,0.28220516443252563f},
{4.256609916687012f,-0.25469231605529785f,-0.05855978652834892f,0.4824114739894867f,0.02334796078503132f,-0.9133865833282471f,-0.25158730149269104f,-0.2674597501754761f,-0.2487834095954895f,0.14748555421829224f,-0.6689738631248474f,0.04456409811973572f,-2.1510491371154785f,-0.3838811218738556f,-0.02410009503364563f,0.06722107529640198f,0.22686699032783508f,-0.30486053228378296f,-0.5974616408348083f,0.8017420172691345f,0.16174809634685516f,1.0218971967697144f,-0.24753881990909576f,1.5039350986480713f,-0.5278223752975464f,-0.025903940200805664f,-0.20675314962863922f,2.5720057487487793f,-0.6852219104766846f,0.06508117914199829f,-0.04787150025367737f,-0.01761513203382492f},
{0.8870363235473633f,0.008923593908548355f,-0.4825742840766907f,-3.2156248092651367f,-0.15642116963863373f,-3.9962704181671143f,-0.2901684641838074f,-0.4319564402103424f,-0.9348295331001282f,-3.552518606185913f,-0.7782331705093384f,-0.2941525876522064f,-1.2979642152786255f,-1.5691884756088257f,0.062144935131073f,-0.40031808614730835f,-0.12148397415876389f,-0.12814576923847198f,0.6662481427192688f,-1.3290021419525146f,-0.7299875617027283f,-2.9367856979370117f,-0.31825554370880127f,0.02476561814546585f,-1.401256799697876f,-0.26416075229644775f,-0.3001742660999298f,-3.532200813293457f,-0.6654595136642456f,0.017086774110794067f,-0.6349568963050842f,-0.4410650432109833f},
{-0.06136500835418701f,-0.11579577624797821f,-0.23311558365821838f,-0.020844489336013794f,0.161813884973526f,0.22929790616035461f,0.11958792805671692f,-0.0384809672832489f,-0.10647237300872803f,-0.09010881185531616f,0.22322741150856018f,-0.1071339100599289f,0.05349445343017578f,-0.23458370566368103f,0.040097206830978394f,0.08616101741790771f,-0.15468145906925201f,-0.21275553107261658f,-0.21210846304893494f,-0.23840199410915375f,0.12352427840232849f,0.282552570104599f,0.14114680886268616f,0.09007054567337036f,-0.05948364734649658f,0.12031787633895874f,-0.184563547372818f,0.07786750793457031f,-0.2169322669506073f,-0.24735762178897858f,-0.0892629474401474f,-0.11765947937965393f},
{0.1169951856136322f,0.06741076707839966f,0.014288008213043213f,-0.1894388198852539f,0.2306886613368988f,0.08970087766647339f,0.29362860321998596f,0.2666439712047577f,-0.1488228738307953f,0.22382596135139465f,-0.1313140094280243f,0.02602824568748474f,0.029441386461257935f,-0.1546926349401474f,0.10762849450111389f,0.06951633095741272f,0.0639207661151886f,0.27696695923805237f,-0.2749756872653961f,0.2684275209903717f,-0.240950807929039f,-0.08405844867229462f,0.26762983202934265f,0.09600964188575745f,-0.20394444465637207f,0.012219250202178955f,-0.09225042164325714f,-0.12637756764888763f,-0.21354423463344574f,-0.29393887519836426f,-0.056166648864746094f,0.002250969409942627f},
{0.614778995513916f,-1.5159648656845093f,-0.03144310787320137f,-0.9700314402580261f,-0.09183521568775177f,-1.6705561876296997f,-0.06413496285676956f,-0.17396175861358643f,0.09930171072483063f,-1.203069806098938f,-0.563704788684845f,-0.13357871770858765f,-1.771638035774231f,-0.14552509784698486f,-0.2592318058013916f,0.09155987948179245f,0.02301185578107834f,0.16387297213077545f,0.5510402321815491f,-0.23720161616802216f,-0.2707703709602356f,-0.878989577293396f,0.2998003363609314f,-0.2551230788230896f,-0.1881304383277893f,-0.28873488306999207f,-0.17016147077083588f,-0.9637672901153564f,-0.4259791672229767f,0.18439257144927979f,-0.03995918482542038f,-0.20910827815532684f},
{0.261911004781723f,-0.26230135560035706f,-0.2993849217891693f,-0.02090282365679741f,0.0858701542019844f,0.040831949561834335f,-0.06670857965946198f,-0.030254334211349487f,-0.26490429043769836f,0.1197996437549591f,0.01667046546936035f,-0.2924177646636963f,0.2057911902666092f,-0.14392434060573578f,-0.20773646235466003f,-0.10781967639923096f,-0.15712574124336243f,0.16112294793128967f,0.2421790212392807f,0.1918656975030899f,0.21191515028476715f,0.11302537471055984f,-0.3001303970813751f,0.14906415343284607f,0.13874544203281403f,0.0025720298290252686f,-0.08662295341491699f,-0.20260775089263916f,0.08291058987379074f,0.21125611662864685f,-0.05751854181289673f,-0.22727590799331665f},
{0.14277681708335876f,-0.20598182082176208f,0.14885470271110535f,0.07422748953104019f,-0.07730758190155029f,-0.243596151471138f,0.1624150574207306f,-0.27022814750671387f,0.1641770750284195f,-0.3630456030368805f,-0.07460077852010727f,0.18551066517829895f,0.5706867575645447f,-0.13496075570583344f,-0.03100520372390747f,-0.2484396994113922f,0.06366744637489319f,-0.2764144539833069f,0.4148780405521393f,0.20108410716056824f,-0.2951240837574005f,0.0057419296354055405f,0.22367028892040253f,-0.0716068297624588f,-0.1542186290025711f,-0.19159743189811707f,0.16949120163917542f,-0.04074159264564514f,-0.23831413686275482f,-0.10488687455654144f,-0.21563708782196045f,0.19791725277900696f},
{0.29069945216178894f,-0.23835359513759613f,0.019009560346603394f,-0.1545778214931488f,0.2683386504650116f,-0.455144464969635f,0.045349299907684326f,-0.17138783633708954f,0.002351903822273016f,0.028545528650283813f,-0.16238126158714294f,-0.15074089169502258f,0.03172455355525017f,-0.5331412553787231f,0.09875330328941345f,0.08164769411087036f,0.2294725477695465f,0.10683679580688477f,-0.16084524989128113f,-0.11490018665790558f,0.29465681314468384f,-0.019545430317521095f,0.16651621460914612f,-0.15362010896205902f,0.004259847570210695f,0.023651927709579468f,-0.1729920208454132f,0.4100888669490814f,-0.023760994896292686f,0.19045329093933105f,0.02994692325592041f,-0.3275023102760315f},
{0.20895303785800934f,0.12534403800964355f,-0.04112154245376587f,0.23123982548713684f,-0.17588619887828827f,-0.302817702293396f,-0.1610087752342224f,-0.031804412603378296f,0.10274738073348999f,-0.25018924474716187f,0.15975651144981384f,-0.057292819023132324f,0.1464652419090271f,0.08016204833984375f,0.2151305377483368f,0.2952744662761688f,-0.21319252252578735f,0.04970458149909973f,0.09217280894517899f,0.3050673305988312f,0.21167120337486267f,-0.15151944756507874f,0.13887640833854675f,0.23811738193035126f,-0.11117732524871826f,-0.07015113532543182f,-0.287551611661911f,-0.025779157876968384f,-0.07337476313114166f,-0.24420931935310364f,0.015819430351257324f,-0.10595546662807465f},
{0.12202468514442444f,0.12833252549171448f,-0.10034829378128052f,-0.4877680838108063f,0.188173308968544f,0.07598204165697098f,-0.06113804876804352f,-0.11568751931190491f,0.14684881269931793f,-0.23565168678760529f,-0.06917986273765564f,-0.11160935461521149f,-0.2788645625114441f,-0.04787037521600723f,-0.06735922396183014f,0.05864924192428589f,-0.25733181834220886f,-0.17565661668777466f,0.07044778764247894f,-0.11984686553478241f,-0.2329980432987213f,0.32391929626464844f,0.15173940360546112f,-0.05681343376636505f,0.21799437701702118f,0.1795230209827423f,0.17492660880088806f,-0.12970009446144104f,-0.189169242978096f,0.2425754964351654f,-0.22172409296035767f,-0.009572982788085938f},
{-0.3027309775352478f,-0.2744956612586975f,-0.07827287912368774f,-0.5441405177116394f,-0.24296191334724426f,-0.882329523563385f,-0.0639423206448555f,0.21086838841438293f,0.03557892516255379f,-0.1612216830253601f,-0.24169281125068665f,-0.004238337278366089f,1.2720441818237305f,-1.6536059379577637f,-0.023131370544433594f,0.036719828844070435f,0.16260939836502075f,-0.07431402057409286f,-0.3205522894859314f,-0.20333026349544525f,0.1351436972618103f,-0.335978239774704f,0.10831346362829208f,0.002637058962136507f,-1.9716442823410034f,-0.18189698457717896f,-0.28665629029273987f,-0.9414716958999634f,-0.8309455513954163f,0.1865309774875641f,0.0585930161178112f,-2.7983274459838867f},
{0.5289771556854248f,-0.6546974182128906f,-0.432040274143219f,0.11496585607528687f,-0.8455396890640259f,0.9780278205871582f,-0.430982768535614f,0.021168958395719528f,-0.8947069644927979f,0.14622509479522705f,-0.7853243947029114f,-0.47896459698677063f,-2.179903268814087f,-1.0347936153411865f,-0.2527656555175781f,-0.5106107592582703f,-0.12364529818296432f,-0.3783210217952728f,1.0325160026550293f,-0.5455983281135559f,-1.887567162513733f,-3.8462014198303223f,-0.6145559549331665f,1.1528420448303223f,-4.034075736999512f,-0.19434598088264465f,0.010721833445131779f,-0.2404177039861679f,-2.6473023891448975f,0.1616479754447937f,-0.22846700251102448f,-4.5056233406066895f},
{0.2051050215959549f,-0.6538046598434448f,-0.3615240454673767f,0.3128914535045624f,0.016421372070908546f,-0.2689581513404846f,-0.13985206186771393f,-0.1761629432439804f,-0.40701162815093994f,-0.33677518367767334f,0.06714833527803421f,-0.27169856429100037f,0.1540621519088745f,0.11222054064273834f,-0.19851773977279663f,-0.10762731730937958f,0.14198419451713562f,-0.02367377281188965f,-0.34345048666000366f,-0.14947156608104706f,-0.24543680250644684f,-0.2330523580312729f,0.14932160079479218f,0.15952368080615997f,0.13089370727539062f,-0.2665709853172302f,0.09590979665517807f,0.36886048316955566f,-0.19728988409042358f,-0.1200934648513794f,-0.11093162000179291f,-0.20550590753555298f},
{-0.12546227872371674f,-0.02762400545179844f,-0.08090300858020782f,0.13270385563373566f,-0.17950619757175446f,-0.0937546119093895f,-0.28310835361480713f,-0.020981580018997192f,0.010336830280721188f,-0.43992236256599426f,-0.5115480422973633f,0.046816736459732056f,-0.20826157927513123f,0.05989319831132889f,-0.0649729073047638f,0.2468220293521881f,0.060016900300979614f,-0.16779978573322296f,-0.09818467497825623f,0.08315295726060867f,-0.16647054255008698f,0.10796991735696793f,-0.3018942177295685f,0.22389158606529236f,-0.06431164592504501f,0.020356476306915283f,-0.27565521001815796f,0.1382385939359665f,0.19752755761146545f,-7.510185241699219e-06f,0.094448983669281f,-0.08219978213310242f},
{0.07555973529815674f,-0.0029259920120239258f,-0.08789914846420288f,0.06678077578544617f,-0.11083845794200897f,0.24179935455322266f,-0.14982634782791138f,0.12075316905975342f,-0.2766430675983429f,0.013024389743804932f,0.18159812688827515f,-0.20200279355049133f,0.2637675702571869f,-0.033522289246320724f,0.2882818281650543f,0.05304515361785889f,-0.15297894179821014f,0.07341468334197998f,0.05268138647079468f,0.1067940890789032f,-0.2661813795566559f,-0.2009473443031311f,0.1843600869178772f,0.2517976462841034f,-0.07343396544456482f,-0.051110416650772095f,-0.23432010412216187f,0.16746872663497925f,-0.2205146849155426f,-0.017525076866149902f,0.20926335453987122f,-0.2141496241092682f},
{0.06582912802696228f,0.2796666920185089f,-0.2556927502155304f,-0.1495107114315033f,0.2043982446193695f,0.14944858849048615f,0.06556430459022522f,0.29767414927482605f,0.24966773390769958f,-0.16670267283916473f,-0.1203770637512207f,-0.06570860743522644f,-0.015925198793411255f,0.17675906419754028f,-0.014884740114212036f,-0.05300724506378174f,0.26490333676338196f,0.15243005752563477f,-0.13416795432567596f,0.2353842556476593f,0.23596063256263733f,0.16653820872306824f,-0.04174882173538208f,-0.11559568345546722f,-0.2429179847240448f,0.21069613099098206f,-0.19532959163188934f,-0.2486409693956375f,0.032513976097106934f,-0.2712400257587433f,-0.19435247778892517f,-0.23200613260269165f},
{-0.1299392580986023f,-0.06208275258541107f,-0.18067686259746552f,0.062329649925231934f,-0.22588664293289185f,-0.07687954604625702f,-0.19175797700881958f,0.02903696894645691f,-0.2614779472351074f,0.016345679759979248f,-0.24767392873764038f,0.2033931314945221f,-0.28187739849090576f,0.05922310799360275f,0.13206014037132263f,0.15829482674598694f,-0.21012049913406372f,0.2088232934474945f,0.07014858722686768f,-0.19175665080547333f,0.28649166226387024f,0.05041047930717468f,-0.27260470390319824f,-0.025072306394577026f,0.2722422182559967f,-0.007939159870147705f,-0.22979341447353363f,0.12208101153373718f,0.1298319399356842f,0.18135693669319153f,-0.008151531219482422f,0.15906798839569092f},
{0.07638216018676758f,0.068531833589077f,-0.30373215675354004f,-0.2750092148780823f,0.12976641952991486f,0.011675742454826832f,0.2795381247997284f,-0.25668495893478394f,-0.07923202216625214f,0.09863324463367462f,-0.026776641607284546f,0.02524423599243164f,0.1101275235414505f,0.24877052009105682f,-0.029521822929382324f,-0.19500072300434113f,0.06506577134132385f,-0.011876672506332397f,0.12230639904737473f,0.24428009986877441f,0.1352696567773819f,-0.0009022368467412889f,-0.19084952771663666f,-0.17324526607990265f,0.21801449358463287f,0.1901901364326477f,-0.2824017107486725f,-0.06062716245651245f,-0.1096503958106041f,0.22636333107948303f,-0.2677184045314789f,0.21909776329994202f},
};

const float hout[32] = { 0.21326908469200134f,2.2586073875427246f,0.1476738303899765f,-1.677074909210205f,-0.07610172778367996f,0.9997888207435608f,0.04738113284111023f,-0.0647246316075325f,0.05791274830698967f,-0.07491426914930344f,1.5706628561019897f,0.13482078909873962f,0.4233318865299225f,0.18541938066482544f,0.2356991171836853f,-0.1045510470867157f,-0.04238883778452873f,-0.11297602951526642f,-0.45068806409835815f,-0.458795428276062f,-0.03062211349606514f,-0.8815718293190002f,0.13232038915157318f,0.1551399677991867f,-0.03634444996714592f,0.023228900507092476f,-0.2141081690788269f,1.0693230628967285f,1.344179391860962f,-0.3257754445075989f,-0.01295095682144165f,0.10170384496450424f };

const float b1[32] = { 0.44476959109306335f,1.0069347620010376f,-1.7145336866378784f,0.018564485013484955f,4.172628402709961f,0.7625464200973511f,-1.8725342750549316f,1.3031364679336548f,0.8175675868988037f,-2.7146263122558594f,0.5570551156997681f,2.0560190677642822f,-0.3425353467464447f,-1.0188425779342651f,-1.087764859199524f,3.0741117000579834f,0.654374897480011f,-0.26377663016319275f,1.8588004112243652f,0.6049700975418091f,-0.284685879945755f,-1.1118980646133423f,-1.6770726442337036f,-1.2263047695159912f,4.202861309051514f,-2.0880801677703857f,-1.0489267110824585f,0.643108069896698f,0.13291175663471222f,-1.0887231826782227f,2.6434223651885986f,-0.4184800088405609f };

const float b2[32] = { -0.40674254298210144f,-1.0293166637420654f,-1.4564176797866821f,-0.4969722330570221f,-0.8316564559936523f,-0.018703091889619827f,-0.45245102047920227f,-0.452020525932312f,-1.20063054561615f,-1.912178635597229f,-0.3487573564052582f,1.9788821935653687f,-0.3680809736251831f,0.8134458661079407f,-1.4347866773605347f,1.9550145864486694f,-1.798204779624939f,-1.5844906568527222f,2.8308825492858887f,-0.3278801739215851f,-0.24611428380012512f,0.05525890365242958f,0.03126738220453262f,-1.3197581768035889f,1.3092838525772095f,2.5383434295654297f,0.09729897975921631f,-0.1865679919719696f,-0.7906461358070374f,-0.5617371797561646f,-1.3292487859725952f,-1.5089067220687866f };

const float b3[32] = { -1.5173410177230835f,-0.6792654991149902f,-0.3339633345603943f,1.1060025691986084f,-0.6123055219650269f,1.3233104944229126f,-0.6508695483207703f,-0.785393238067627f,-0.43242132663726807f,-0.34026646614074707f,-0.5721292495727539f,-0.49279072880744934f,0.4067545533180237f,-0.03992043435573578f,-1.1428403854370117f,-0.44154849648475647f,-1.5117244720458984f,-1.1461795568466187f,-0.3880360722541809f,-0.2065272331237793f,-0.627239465713501f,1.5729435682296753f,-1.4130381345748901f,-1.1953978538513184f,-0.014376070350408554f,-0.6644327044487f,-0.26526084542274475f,1.8583635091781616f,-0.32082390785217285f,-2.3236680030822754f,-0.40476295351982117f,-0.004477785434573889f };

const float bout[1] = { 0.48785537481307983f };


int main()
{

    HAL_Init();
    SystemClock_Config();
    
    /*********************************
    ***     Initialization
    *********************************/
    LED = 0;
    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;
        //}
        
        timer_while ++;
        
        if(NN_Control_Flag == 0) {
            LED = 0;
        }
        
        else if(NN_Control_Flag == 1) {
            float output1[32] = { 0.0f };
            float output2[32] = { 0.0f };
            float output3[32] = { 0.0f };
            float output = 0.0f;

            for (int index2 = 0; index2 < 32; index2++) {
                for (int index1 = 0; index1 < num_input; index1++) {
                    output1[index2] = output1[index2]
                                      + h1[index1][index2] * input_NN[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[index1] * output3[index1];
                }
                output = output + bout[index2];
                
            }
            output = 1.0f/(1.0f+exp(-output));
            output = output * 20000.0f - 10000.0f;
            if(output>=0) {
                valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
            } else {
                valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
            }
            
            if(LED==1) {
                LED=0;
            } else
                LED = 1;
            
        }
        
        

    }
}

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] = {-230.0f, -215.0f, -192.5f, -185.0f, -177.5f, -170.0f, -164.0f, -160.0f, -150.0f,
                                       -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
                                       -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
                                       7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
                                       105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
                                       155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
                                      }; // 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();
        }

        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;


//        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_NULL)  / PRES_SENSOR_A_PULSE_PER_BAR;
//        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


        } 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/20);
                    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 + 12.0f;
                        else pos.ref = pos.ref - 12.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);
//                        ENC_SET_ZERO();
                        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;


                        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;
                        
                        
                    }
                } 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;
                    //pos.ref = 0.0f;
                    vel.ref = 0.0f;
                    
                    // input for position control
                
//                    CONTROL_MODE = MODE_JOINT_CONTROL;
                    alpha_trans = 0.0f;

                    double torq_ref = 0.0f;
                    pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
                    vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
                    pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]

                    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) * 3.14159f / 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))));

                        I_REF = I_REF_POS;



                    } else {
                        float VALVE_POS_RAW_FORCE_FB = 0.0f;
                        VALVE_POS_RAW_FORCE_FB = DDV_JOINT_POS_FF(vel.sen) + (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;
                        }

                        VALVE_POS_CONTROL(valve_pos.ref);

                        V_out = (float) Vout.ref;

                    }


                    
                    
//                    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 == 5) { //SW Valve
                    VALVE_POS_CONTROL(valve_pos.ref);
                    V_out = Vout.ref;
                } else if (CURRENT_CONTROL_MODE == 0) { //PWM
                    V_out = valve_pos.ref;
                } else { 
                    I_REF = valve_pos.ref * 0.001f;
                }

                break;
            }

            case MODE_JOINT_CONTROL: {
                double torq_ref = 0.0f;
                pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
                vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
                pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
                
                //K & D Low Pass Filter
                float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
                K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
                D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
                
                torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]

                // torque feedback
                torq.err = torq_ref - 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);
                    
                    // Anti-windup for FT
                    if (I_GAIN_JOINT_TORQUE != 0) {
                        double I_MAX = 10.0f; // Maximum Current : 10mV
                        double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
                        if (I_REF > I_MAX) {
                            double I_rem = I_REF - I_MAX;
                            I_rem = Ka*I_rem;
                            I_REF = I_MAX;
                            torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
                        } else if (I_REF < -I_MAX) {
                            double I_rem = I_REF - (-I_MAX);
                            I_rem = Ka*I_rem;
                            I_REF = -I_MAX;
                            torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
                        }
                    }

                } else {
                    float VALVE_POS_RAW_FORCE_FB = 0.0f;
                    float VALVE_POS_RAW_FORCE_FF = 0.0f;
                    float VALVE_POS_RAW = 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));

                    VALVE_POS_RAW_FORCE_FF = P_GAIN_JOINT_TORQUE_FF * torq_ref * 0.001f + D_GAIN_JOINT_TORQUE_FF * (torq_ref - torq_ref_past) * 0.0001f;
                    
                    VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
                    
                    
                    if (VALVE_POS_RAW >= 0) {
                        valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
                    } else {
                        valve_pos.ref = VALVE_POS_RAW + 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;

                }
                
                torq_ref_past = torq_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 + 5000.0f / (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) * 0.0002f;


                // Moog Valve Current Control Gain
                double R_model = 500.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 * 2.0f * I_ERR + KI_I * 2.0f* 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 * 5000.0f); // Unit : mV
                VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
                double V_MAX = 12000.0f; // 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 * 0.0002f;
                } 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 * 0.0002f;
                }
                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);  // -8000~8000

            // Dead Zone Cancellation (Electrical dead-zone)
            if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
            else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
            else V_out = (float) (CUR_PWM_lin);
        }
        
//        if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
//        else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
//        else V_out = V_out;
    
        /*******************************************************
        ***     PWM
        ********************************************************/
        if(DIR_VALVE<0){
            V_out = -V_out;
        }
        
        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) (torq.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/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) (torq.sen * 10.0f * (float)(TORQUE_SENSOR_PULSE_PER_TORQUE)));
                    } else if (SENSING_MODE == 1) {
                        CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.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(valve_pos.ref>=(float) VALVE_CENTER) {
                    t_value = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
                } else {
                    t_value = -10000.0f*((double)valve_pos.ref - (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)); //1300
                //CAN_TX_TORQUE((int16_t) (cur.sen * 1000.0f)); //1300
                //CAN_TX_TORQUE((int16_t) (I_REF * 1000.0f)); //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) value); //1500
                CAN_TX_PWM((int16_t) input_NN[0] * 100.0f); //1500
            }
            //for (i = 0; i < 10000; i++) {
//                ;
//            }
            if (flag_data_request[4] == HIGH) {
                //valve position
                CAN_TX_VALVE_POSITION((int16_t) (input_NN[1] * 100.0f), (int16_t) (input_NN[2]* 100.0f), (int16_t) (input_NN[3]* 100.0f), (int16_t) (input_NN[4]* 100.0f)); //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

}