2011

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Tue Nov 24 05:19:59 2020 +0000
Revision:
173:68c7914679ec
Parent:
172:63af34265fe9
Child:
174:c828479f53f9
201124-1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 173:68c7914679ec 1 //201124_1
GiJeongKim 0:51c43836c1d7 2 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 3 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 4 #include "INIT_HW.h"
jobuuu 2:a1c0a37df760 5 #include "function_CAN.h"
GiJeongKim 0:51c43836c1d7 6 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 7 #include "I2C_AS5510.h"
GiJeongKim 0:51c43836c1d7 8 #include "setting.h"
Lightvalve 11:82d8768d7351 9 #include "function_utilities.h"
Lightvalve 16:903b5a4433b4 10 #include "stm32f4xx_flash.h"
Lightvalve 169:645207e160ca 11 #include "FlashWriter.h"
Lightvalve 170:42c938a40313 12 #include <string>
Lightvalve 170:42c938a40313 13 #include <iostream>
Lightvalve 61:bc8c8270f0ab 14
Lightvalve 61:bc8c8270f0ab 15 using namespace std;
Lightvalve 61:bc8c8270f0ab 16 Timer t;
GiJeongKim 0:51c43836c1d7 17
Lightvalve 31:66738bfecec5 18 ///191008////
Lightvalve 31:66738bfecec5 19
jobuuu 7:e9086c72bb22 20 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 21 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 22 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 23 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 24 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 25 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 26 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 27 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 28
Lightvalve 24:ef6e1092e9e6 29
jobuuu 7:e9086c72bb22 30 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 31 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 32 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 33
jobuuu 7:e9086c72bb22 34 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 35 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
GiJeongKim 0:51c43836c1d7 36 const int i2c_slave_addr1 = 0x56;
GiJeongKim 0:51c43836c1d7 37 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 38
jobuuu 7:e9086c72bb22 39 // SPI ///////////////////////////////////////////
Lightvalve 170:42c938a40313 40 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 170:42c938a40313 41 DigitalOut eeprom_cs(PB_12);
Lightvalve 16:903b5a4433b4 42 //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
GiJeongKim 0:51c43836c1d7 43 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 44 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 45 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 46
jobuuu 7:e9086c72bb22 47 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 48 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 49
jobuuu 7:e9086c72bb22 50 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 51 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 52 CANMessage msg;
Lightvalve 11:82d8768d7351 53 void onMsgReceived()
Lightvalve 11:82d8768d7351 54 {
Lightvalve 11:82d8768d7351 55 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 56 }
jobuuu 2:a1c0a37df760 57
jobuuu 7:e9086c72bb22 58 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 59 State pos;
jobuuu 7:e9086c72bb22 60 State vel;
jobuuu 7:e9086c72bb22 61 State Vout;
jobuuu 7:e9086c72bb22 62 State torq;
jobuuu 7:e9086c72bb22 63 State pres_A;
jobuuu 7:e9086c72bb22 64 State pres_B;
jobuuu 7:e9086c72bb22 65 State cur;
Lightvalve 14:8e7590227d22 66 State valve_pos;
Lightvalve 14:8e7590227d22 67
Lightvalve 14:8e7590227d22 68 State INIT_Vout;
Lightvalve 14:8e7590227d22 69 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 70 State INIT_Pos;
Lightvalve 14:8e7590227d22 71 State INIT_torq;
jobuuu 5:a4319f79457b 72
Lightvalve 19:23b7c1ad8683 73 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 74 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 75 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 76 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 77
Lightvalve 19:23b7c1ad8683 78 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 79 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 80 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 84
Lightvalve 61:bc8c8270f0ab 85
Lightvalve 61:bc8c8270f0ab 86
Lightvalve 61:bc8c8270f0ab 87
jobuuu 7:e9086c72bb22 88 // =============================================================================
jobuuu 7:e9086c72bb22 89 // =============================================================================
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91
Lightvalve 12:6f2531038ea4 92 /*******************************************************************************
Lightvalve 12:6f2531038ea4 93 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 94 ******************************************************************************/
Lightvalve 13:747daba9cf59 95 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 96 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 97 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 98 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 99 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 100 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 101 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 102 };
Lightvalve 12:6f2531038ea4 103
Lightvalve 12:6f2531038ea4 104 /*******************************************************************************
Lightvalve 12:6f2531038ea4 105 * CONTROL MODE
Lightvalve 12:6f2531038ea4 106 ******************************************************************************/
Lightvalve 13:747daba9cf59 107 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 108 //control mode
Lightvalve 12:6f2531038ea4 109 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 110 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 111 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 112
Lightvalve 47:fdcb8bd86fd6 113 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 114 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 115 MODE_RL, //5
Lightvalve 13:747daba9cf59 116
Lightvalve 12:6f2531038ea4 117 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 118 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 119 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 120
Lightvalve 12:6f2531038ea4 121 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 122 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 123
Lightvalve 14:8e7590227d22 124 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 125 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 126 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 171:bfc1fd2629d8 127 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 128
Lightvalve 12:6f2531038ea4 129 //utility
Lightvalve 12:6f2531038ea4 130 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 131 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 132 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 133 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 134 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 135 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 136 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 137
Lightvalve 12:6f2531038ea4 138 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 139 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 140 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 141 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 142 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 143 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 144 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 145 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 146 };
Lightvalve 12:6f2531038ea4 147
Lightvalve 65:a2d7c63419c2 148 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 149 {
Lightvalve 169:645207e160ca 150 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 151 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 152
Lightvalve 169:645207e160ca 153 /** Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 154 */
Lightvalve 169:645207e160ca 155 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 156 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 169:645207e160ca 157 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 158 */
Lightvalve 169:645207e160ca 159 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 160 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 161 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 162 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 163 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 164 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 165 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 166 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 167 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 168 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 169 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 170 //Error_Handler();
Lightvalve 169:645207e160ca 171 }
Lightvalve 169:645207e160ca 172 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 173 */
Lightvalve 169:645207e160ca 174 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 175 //Error_Handler();
Lightvalve 169:645207e160ca 176 }
Lightvalve 169:645207e160ca 177 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 178 */
Lightvalve 169:645207e160ca 179 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 180 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 181 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 182 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 183 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 184 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 185
Lightvalve 169:645207e160ca 186 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 187 //Error_Handler();
Lightvalve 169:645207e160ca 188 }
Lightvalve 65:a2d7c63419c2 189 }
Lightvalve 65:a2d7c63419c2 190
Lightvalve 99:7bbcb3c0fb06 191 float u_past[num_array_u_past] = {0.0f};
Lightvalve 68:328e1be06f5d 192 float x_past[num_array_x_past] = {0.0f};
Lightvalve 85:a3b46118b5cd 193 float x_future[num_array_x_future] = {0.0f};
Lightvalve 68:328e1be06f5d 194 float f_past[num_array_f_past] = {0.0f};
Lightvalve 68:328e1be06f5d 195 float f_future[num_array_f_future] = {0.0f};
Lightvalve 66:a8e6799dbce3 196
Lightvalve 73:f80dc3970c99 197 float input_NN[num_input] = { 0.0f };
Lightvalve 65:a2d7c63419c2 198
Lightvalve 169:645207e160ca 199 const float h1[num_input][16] = {
Lightvalve 169:645207e160ca 200 {-1.3752100467681885f,0.9850414395332336f,0.12903714179992676f,-0.23237967491149902f,-1.5664141178131104f,0.36565715074539185f,-0.17472904920578003f,-0.3780243396759033f,-0.5318458676338196f,-0.6931207776069641f,-0.24376395344734192f,-3.9280478954315186f,-0.259793758392334f,-0.5339483022689819f,-0.2571594715118408f,-0.4960939884185791f},
Lightvalve 169:645207e160ca 201 {-1.184814453125f,0.8704654574394226f,-0.3927857577800751f,-0.40050771832466125f,-1.4921096563339233f,0.5157514810562134f,-0.4054011106491089f,-0.3723553717136383f,-0.264297217130661f,-0.4598728120326996f,-0.2805407643318176f,-2.543855905532837f,0.24001067876815796f,-0.9143947958946228f,-0.20967772603034973f,-0.46271881461143494f},
Lightvalve 169:645207e160ca 202 {-0.9112814664840698f,0.6714562773704529f,0.36391180753707886f,-0.6117674708366394f,-1.1069424152374268f,0.9712548851966858f,0.2872133255004883f,-0.059537652879953384f,-0.28272250294685364f,-0.4464949667453766f,-0.3544096052646637f,-1.0609019994735718f,0.0019771659281104803f,-0.2590618431568146f,0.05590221285820007f,-1.0201798677444458f},
Lightvalve 169:645207e160ca 203 {-0.2895396053791046f,-0.01403216365724802f,0.39742106199264526f,0.20069332420825958f,-0.6715657114982605f,0.5361601710319519f,-0.11584559082984924f,0.3479021489620209f,-0.01779201813042164f,-0.017457854002714157f,0.08943979442119598f,0.845181941986084f,-0.15994226932525635f,-0.14522784948349f,-0.2961459457874298f,-0.7345172166824341f},
Lightvalve 169:645207e160ca 204 {1.1968307495117188f,-0.8280011415481567f,-0.0051451027393341064f,0.39627134799957275f,0.3181920051574707f,-0.05303953215479851f,0.1294034719467163f,0.16528914868831635f,0.07129872590303421f,0.7101694941520691f,0.34140077233314514f,1.623598575592041f,-0.06323867291212082f,0.27155500650405884f,-0.3865056037902832f,-0.30631402134895325f},
Lightvalve 169:645207e160ca 205 {3.009206533432007f,-3.122997522354126f,0.21773302555084229f,0.7692314386367798f,2.249300003051758f,-2.275317907333374f,0.16514194011688232f,0.11058115214109421f,0.17132356762886047f,2.417614459991455f,-0.25413116812705994f,2.2743594646453857f,-0.25959381461143494f,0.8003392219543457f,-0.4132833778858185f,1.4499995708465576f},
Lightvalve 169:645207e160ca 206 {-0.7179104089736938f,0.8526995182037354f,-0.2501986622810364f,-0.875395655632019f,-0.5985659956932068f,2.8744397163391113f,0.06014183163642883f,-0.8439451456069946f,-0.05617062747478485f,-0.640657901763916f,0.14294663071632385f,3.381211519241333f,-0.1638868898153305f,-0.2370987981557846f,-0.15618428587913513f,-0.7710326910018921f},
Lightvalve 169:645207e160ca 207 {-0.5504567623138428f,0.25199615955352783f,-0.26619744300842285f,-0.08971554040908813f,-0.3043712079524994f,0.903326153755188f,-0.17925891280174255f,-0.6943695545196533f,-0.25526830554008484f,0.20558907091617584f,0.09723648428916931f,2.3618743419647217f,0.38060376048088074f,-0.1769549399614334f,0.04303058981895447f,-0.12222103029489517f},
Lightvalve 169:645207e160ca 208 {0.06810358166694641f,0.17962348461151123f,0.10248100757598877f,-0.30372604727745056f,0.4354613721370697f,-0.7276828289031982f,-0.07246989011764526f,-0.6118704676628113f,-0.42743170261383057f,1.453460693359375f,-0.30540645122528076f,1.6834946870803833f,0.23858578503131866f,0.21455851197242737f,-0.30555272102355957f,0.8063939213752747f},
Lightvalve 169:645207e160ca 209 {-0.0713198110461235f,-0.4495846927165985f,0.18462657928466797f,0.05500239133834839f,0.7719969153404236f,-1.2848507165908813f,-0.2990540862083435f,-0.22474081814289093f,-0.1015893891453743f,0.7663454413414001f,-0.016399379819631577f,-0.03539150580763817f,0.38201630115509033f,-0.09081173688173294f,0.3484981060028076f,0.7820040583610535f},
Lightvalve 169:645207e160ca 210 {0.33533185720443726f,0.13371603190898895f,0.37182438373565674f,0.39484986662864685f,0.35771995782852173f,-1.3015228509902954f,-0.20339298248291016f,-0.14145439863204956f,-0.5059540271759033f,0.8015591502189636f,0.34025880694389343f,-0.6485148072242737f,-0.05657690018415451f,0.09265313297510147f,0.26688337326049805f,0.2284013032913208f},
Lightvalve 169:645207e160ca 211 {-0.4380135238170624f,-0.15535405278205872f,-0.40320003032684326f,0.0452105812728405f,0.23505118489265442f,-0.8950125575065613f,0.1533789038658142f,0.1997886300086975f,-0.24218705296516418f,0.45601460337638855f,-0.42232224345207214f,-0.5756524205207825f,0.15488487482070923f,-0.013335001654922962f,0.10493969917297363f,0.23898905515670776f},
Lightvalve 169:645207e160ca 212 {-0.02198643982410431f,0.009633726440370083f,0.19847965240478516f,0.0860355794429779f,0.336037278175354f,-0.277248352766037f,-0.2404318004846573f,-0.07731665670871735f,-0.2765587270259857f,-0.1708865910768509f,-0.11712735891342163f,-0.7335506677627563f,0.10241620987653732f,-0.06552530825138092f,-0.22268100082874298f,-0.13819573819637299f},
Lightvalve 169:645207e160ca 213 {0.020149538293480873f,-0.03840681165456772f,0.4127817749977112f,-0.2523573637008667f,-0.21765978634357452f,-0.4540342092514038f,-0.32001304626464844f,-0.5396589040756226f,-0.21919101476669312f,-0.2550864815711975f,-0.20302052795886993f,-0.2918916642665863f,-0.3021690249443054f,-0.19007131457328796f,0.0479682981967926f,0.28083792328834534f},
Lightvalve 169:645207e160ca 214 {0.19955825805664062f,0.13080951571464539f,0.20282304286956787f,-0.21126452088356018f,0.15339018404483795f,-0.06708966940641403f,0.04202890396118164f,0.06752092391252518f,0.25217023491859436f,-0.20238792896270752f,-0.2873592674732208f,-0.42253321409225464f,0.11435109376907349f,-0.2376958578824997f,0.0668090283870697f,0.04261681064963341f},
Lightvalve 169:645207e160ca 215 {-0.03747318685054779f,-0.29164087772369385f,-0.40439701080322266f,-0.3783135414123535f,-0.40124887228012085f,-0.026558339595794678f,-0.15568238496780396f,-0.08766567707061768f,0.21709603071212769f,-0.35802650451660156f,-0.4817592203617096f,-0.3809756338596344f,-0.2601439654827118f,-0.1273980438709259f,-0.08245879411697388f,0.014104350470006466f},
Lightvalve 169:645207e160ca 216 {0.16147102415561676f,-0.5628581643104553f,0.16706281900405884f,-0.1172998696565628f,-0.03297983109951019f,-0.9104568362236023f,-0.041414469480514526f,0.1465616226196289f,0.04248951002955437f,0.007101915311068296f,-0.3614332377910614f,-0.48312774300575256f,0.12316523492336273f,0.3939219117164612f,0.29427415132522583f,0.3038717210292816f},
Lightvalve 65:a2d7c63419c2 217 };
Lightvalve 65:a2d7c63419c2 218
Lightvalve 169:645207e160ca 219 const float h2[16][16] = {
Lightvalve 168:f05a45d5f318 220 {0.33418041467666626f,2.9079723358154297f,-0.06966331601142883f,-0.009902028366923332f,2.812035322189331f,4.195849418640137f,-0.038234543055295944f,0.0904630720615387f,-0.43008196353912354f,-0.1415480375289917f,0.10707370936870575f,-0.20333009958267212f,-0.031739287078380585f,-3.289416551589966f,-1.172767996788025f,-0.6248859763145447f},
Lightvalve 169:645207e160ca 221 {0.11070519685745239f,-2.3348469734191895f,0.057057321071624756f,-0.10134492069482803f,-5.252432346343994f,-1.3715343475341797f,-0.8426902890205383f,-0.2990124225616455f,-0.33757925033569336f,0.2895788848400116f,-7.361403465270996f,-0.858361005783081f,0.0845860168337822f,2.2169580459594727f,-3.0816471576690674f,-1.0190033912658691f},
Lightvalve 169:645207e160ca 222 {-0.22745239734649658f,0.003037691116333008f,-0.061119019985198975f,0.35696902871131897f,0.05568113923072815f,0.011741191148757935f,-0.20225946605205536f,-0.08465918898582458f,0.3489862382411957f,0.0687277615070343f,0.31964078545570374f,0.3004753887653351f,0.36063823103904724f,-0.42892736196517944f,0.08652284741401672f,0.027493387460708618f},
Lightvalve 169:645207e160ca 223 {-0.1391567885875702f,0.03293241187930107f,-0.2894435524940491f,0.25554025173187256f,0.08946844935417175f,0.1340968757867813f,-0.39087218046188354f,0.4122363030910492f,0.26211628317832947f,0.2953031361103058f,0.7025435566902161f,0.05798906087875366f,-0.5013425946235657f,-0.030901746824383736f,-0.06675297021865845f,-0.0446302704513073f},
Lightvalve 169:645207e160ca 224 {-2.245600938796997f,1.6008015871047974f,-0.25313520431518555f,0.1469619870185852f,1.6072996854782104f,-1.3637473583221436f,-0.5957030653953552f,-0.08570799231529236f,-0.024399548768997192f,-0.39465832710266113f,-6.811410427093506f,-0.7977238893508911f,-0.022602174431085587f,-1.6454986333847046f,0.5441725850105286f,-0.7979243397712708f},
Lightvalve 169:645207e160ca 225 {0.5210384130477905f,-0.11346126347780228f,-0.3519742488861084f,0.29306867718696594f,-0.3572634160518646f,1.75344979763031f,-0.744201123714447f,-0.019414573907852173f,-0.23047015070915222f,0.3579089343547821f,-2.398893117904663f,-0.48980283737182617f,-0.45957082509994507f,0.5487804412841797f,-0.7850930690765381f,-0.7663227319717407f},
Lightvalve 169:645207e160ca 226 {-0.1828227937221527f,-0.02554568648338318f,-0.3260969817638397f,0.08422836661338806f,-0.38453540205955505f,-0.25432005524635315f,0.285016268491745f,0.12387624382972717f,-0.0982072651386261f,0.13111665844917297f,-0.03692615032196045f,-0.32796353101730347f,-0.21546880900859833f,0.049302369356155396f,-0.27088475227355957f,-0.4124959409236908f},
Lightvalve 169:645207e160ca 227 {-0.007747650612145662f,0.3667842745780945f,0.33083590865135193f,-0.38213321566581726f,-0.14358049631118774f,-0.2546055316925049f,-0.09043094515800476f,-0.3003333508968353f,0.2600560486316681f,-0.37898191809654236f,0.49078798294067383f,-0.3058888614177704f,-0.31673234701156616f,0.44664251804351807f,-0.9339061975479126f,-0.1061311885714531f},
Lightvalve 169:645207e160ca 228 {-0.17694538831710815f,0.1767565757036209f,-0.11379697918891907f,-0.07116051763296127f,0.041274964809417725f,-0.517110288143158f,-0.10284432768821716f,0.0019084513187408447f,0.06103590130805969f,-0.38046833872795105f,-0.3456110656261444f,-0.24793067574501038f,-0.4166972041130066f,-0.30618393421173096f,-0.33565962314605713f,-0.48154377937316895f},
Lightvalve 169:645207e160ca 229 {0.5043365359306335f,0.1850307136774063f,-0.13502129912376404f,-0.25706031918525696f,-0.8451530337333679f,-0.9415315389633179f,-0.2777014970779419f,-0.3632148504257202f,-0.13619378209114075f,0.16938945651054382f,1.5622152090072632f,-0.19312366843223572f,0.1250869333744049f,-0.1843482404947281f,1.0270578861236572f,-0.5840676426887512f},
Lightvalve 169:645207e160ca 230 {-0.25693249702453613f,-0.010147050023078918f,0.0457797646522522f,-0.3549601435661316f,-0.03321319818496704f,-0.4391511380672455f,0.17873415350914001f,-0.20421427488327026f,-0.050184011459350586f,0.12480869889259338f,-0.22192376852035522f,0.3545852601528168f,-0.3647043704986572f,0.4194667637348175f,-0.3910166025161743f,-0.2837793529033661f},
Lightvalve 169:645207e160ca 231 {2.3816981315612793f,0.4564768075942993f,0.3954955041408539f,0.11254638433456421f,1.1973353624343872f,1.804193377494812f,-0.8583166599273682f,-0.11795541644096375f,0.3487861454486847f,-0.32520344853401184f,-1.3897980451583862f,-0.5921090841293335f,-0.14213289320468903f,-0.26851770281791687f,1.597784161567688f,-0.5877130031585693f},
Lightvalve 169:645207e160ca 232 {-0.267646849155426f,-0.20429572463035583f,-0.15798500180244446f,0.3999568819999695f,-0.37393757700920105f,0.35770177841186523f,0.11292675137519836f,-0.2947862446308136f,-0.3764709532260895f,0.2424570620059967f,-0.10294663906097412f,-0.2837170362472534f,0.41839322447776794f,-0.02792874164879322f,-0.13706544041633606f,0.11999254673719406f},
Lightvalve 169:645207e160ca 233 {0.2282707840204239f,-0.13254989683628082f,-0.2018718123435974f,-0.07019486278295517f,0.07545611262321472f,-0.33204764127731323f,-0.4035329520702362f,-0.21416273713111877f,-0.24509364366531372f,0.19522181153297424f,0.47974857687950134f,-0.8069085478782654f,-0.40297332406044006f,0.23708327114582062f,0.5259239673614502f,-0.3548051416873932f},
Lightvalve 169:645207e160ca 234 {0.4138670265674591f,0.1604653298854828f,0.056746453046798706f,0.036025404930114746f,0.3228367865085602f,-0.07083973288536072f,0.018455177545547485f,0.0059362053871154785f,0.40515169501304626f,0.014240056276321411f,-0.07738298177719116f,0.1407785713672638f,-0.13024571537971497f,-0.29546058177948f,-0.11976784467697144f,-0.35825538635253906f},
Lightvalve 169:645207e160ca 235 {-0.5818981528282166f,0.014110059477388859f,0.12081471085548401f,-0.2973254919052124f,0.5392167568206787f,0.3773144781589508f,-0.39351940155029297f,0.31089308857917786f,-0.3893685042858124f,-0.02222958207130432f,-6.493835926055908f,-0.12242847681045532f,-0.2689415514469147f,-0.2518419325351715f,-0.1694900244474411f,-0.652117908000946f},
Lightvalve 65:a2d7c63419c2 236 };
Lightvalve 65:a2d7c63419c2 237
Lightvalve 169:645207e160ca 238 const float h3[16][16] = {
Lightvalve 168:f05a45d5f318 239 {-0.36079341173171997f,-1.5671656131744385f,-0.870830237865448f,-0.4786093831062317f,-0.5575059056282043f,-0.773746132850647f,0.29977259039878845f,0.1743643879890442f,-0.5892294645309448f,-0.19605471193790436f,-0.27379146218299866f,0.06893575936555862f,2.2012624740600586f,-2.7650468349456787f,-1.8527313470840454f,1.5724681615829468f},
Lightvalve 169:645207e160ca 240 {0.047732532024383545f,-0.23476845026016235f,-2.8836917877197266f,-0.42602431774139404f,-2.397148370742798f,1.254254937171936f,-0.3198729455471039f,-0.25123724341392517f,0.013695899397134781f,-0.5729482173919678f,0.09775999933481216f,-0.5587971806526184f,-0.9381403923034668f,-1.532671570777893f,-0.6944977045059204f,1.4258947372436523f},
Lightvalve 169:645207e160ca 241 {0.07903262972831726f,0.2790505588054657f,-0.07798504829406738f,0.04248586297035217f,-0.1963958442211151f,-0.19260792434215546f,-0.4038352966308594f,0.015906542539596558f,0.15353140234947205f,0.030178606510162354f,0.2488909661769867f,0.13805970549583435f,-0.0816211998462677f,-0.20733052492141724f,-0.3036302626132965f,0.054825395345687866f},
Lightvalve 169:645207e160ca 242 {-0.30922991037368774f,-0.16969707608222961f,0.3056027889251709f,-0.380797415971756f,-0.17742466926574707f,0.10660405457019806f,0.20021501183509827f,0.07002416253089905f,-0.25412267446517944f,-0.365601509809494f,0.13192829489707947f,0.41021624207496643f,-0.13397035002708435f,0.0815882533788681f,0.15073642134666443f,0.10944337397813797f},
Lightvalve 169:645207e160ca 243 {-0.39607733488082886f,-0.05481579899787903f,0.1976260244846344f,0.022423356771469116f,0.16892847418785095f,-0.6999471187591553f,0.16012099385261536f,0.21129152178764343f,-0.08640444278717041f,-0.11053556203842163f,-0.2634495496749878f,-0.31317979097366333f,-0.1530032455921173f,-0.24679358303546906f,0.22959044575691223f,-3.0342295169830322f},
Lightvalve 169:645207e160ca 244 {-0.37449589371681213f,-0.10077743977308273f,-4.51036262512207f,-0.04963836818933487f,-0.8192825317382812f,-0.6951172947883606f,-0.19780586659908295f,-0.09049295634031296f,-0.9114404916763306f,-0.8623005151748657f,-0.4059421718120575f,-0.16295623779296875f,-5.017462730407715f,-1.1079373359680176f,0.34683844447135925f,0.6427209377288818f},
Lightvalve 169:645207e160ca 245 {0.4110594093799591f,0.2715781033039093f,-0.38537508249282837f,0.37246426939964294f,-0.05490662157535553f,-0.009114405140280724f,-0.08670487999916077f,-0.25336313247680664f,-0.030661463737487793f,-0.06259563565254211f,-0.1344406008720398f,0.35313835740089417f,-0.13377737998962402f,0.25604528188705444f,0.3126353323459625f,-0.1528691202402115f},
Lightvalve 169:645207e160ca 246 {-0.40892091393470764f,0.043769627809524536f,-0.3867315948009491f,0.25968697667121887f,0.3424709737300873f,-0.051169753074645996f,-0.23312048614025116f,-0.390264093875885f,0.28059282898902893f,-0.1559126079082489f,-0.14134526252746582f,-0.0003446042537689209f,-0.2742875814437866f,-0.36560842394828796f,0.07994696497917175f,0.005298197269439697f},
Lightvalve 169:645207e160ca 247 {0.10697010159492493f,-0.12228584289550781f,-0.37870171666145325f,0.21184906363487244f,-0.37222859263420105f,-0.17138728499412537f,-0.1382003128528595f,0.3493293821811676f,-0.360889196395874f,-0.3875247836112976f,0.42142823338508606f,-0.3482915461063385f,-0.3289247751235962f,-0.2186824083328247f,0.09620395302772522f,-0.06898030638694763f},
Lightvalve 169:645207e160ca 248 {0.2847062647342682f,0.018552124500274658f,0.11435768008232117f,0.36562982201576233f,-0.047046810388565063f,0.30447837710380554f,0.2430230677127838f,0.2909286320209503f,-0.2802048921585083f,0.18043199181556702f,0.41849127411842346f,-0.287167489528656f,0.24394884705543518f,-0.14084559679031372f,-0.10168051719665527f,0.010465055704116821f},
Lightvalve 169:645207e160ca 249 {0.15459725260734558f,0.22762465476989746f,1.5232031345367432f,-0.2233445942401886f,1.524779200553894f,2.6997625827789307f,-0.08742031455039978f,0.05785742402076721f,-0.1277361363172531f,-0.37371426820755005f,-0.03133596479892731f,-0.30447322130203247f,-1.9101834297180176f,0.6954238414764404f,0.46117544174194336f,1.1762653589248657f},
Lightvalve 169:645207e160ca 250 {-0.408692330121994f,0.07260357588529587f,-0.02147701010107994f,0.0922636091709137f,-0.1529182642698288f,-0.05657944083213806f,0.03285527229309082f,0.38763079047203064f,-0.20705322921276093f,-0.25883403420448303f,0.12809070944786072f,0.03996849060058594f,-0.6609845757484436f,-0.1087266057729721f,-0.10636871308088303f,-0.0133456289768219f},
Lightvalve 169:645207e160ca 251 {-0.2991822361946106f,0.3794580399990082f,-0.08715943992137909f,-0.05932474136352539f,0.11478022485971451f,0.3007120192050934f,-0.11253207921981812f,0.34576353430747986f,0.04814547300338745f,-0.35770976543426514f,-0.044228196144104004f,-0.36229726672172546f,0.05380958318710327f,-0.13672849535942078f,0.35829514265060425f,-0.10585878044366837f},
Lightvalve 169:645207e160ca 252 {-0.2675279378890991f,-0.1429223269224167f,-0.9267327189445496f,-0.1256239265203476f,0.49980443716049194f,-0.843601405620575f,-0.33532586693763733f,-0.31893211603164673f,-1.478386402130127f,-1.1926257610321045f,-0.5260127782821655f,-0.8384293913841248f,0.9772797226905823f,0.1813446283340454f,0.17612296342849731f,1.590701699256897f},
Lightvalve 169:645207e160ca 253 {-0.015470266342163086f,-0.4976375699043274f,2.497199058532715f,0.14489558339118958f,0.11334121972322464f,0.3805958926677704f,0.02394937537610531f,-0.16103173792362213f,-0.677362859249115f,-0.8498573303222656f,-0.026575788855552673f,-0.427055299282074f,0.17177484929561615f,1.67837655544281f,0.08538639545440674f,-1.3552721738815308f},
Lightvalve 169:645207e160ca 254 {0.1863725483417511f,0.13157431781291962f,-0.047772910445928574f,-0.49708226323127747f,0.7275430560112f,-0.145163431763649f,0.04860696196556091f,0.17769548296928406f,-0.050254471600055695f,0.19876523315906525f,-0.5504136085510254f,0.19592127203941345f,0.23928996920585632f,-0.12190091609954834f,-0.1250786930322647f,0.041962411254644394f},
Lightvalve 66:a8e6799dbce3 255 };
Lightvalve 65:a2d7c63419c2 256
Lightvalve 168:f05a45d5f318 257 const float hout[16] = { 0.45773375034332275f,0.5733839869499207f,-0.5152473449707031f,-0.035915032029151917f,-0.306951105594635f,0.30648893117904663f,-0.1005084440112114f,-0.08898112922906876f,-0.2034129500389099f,-0.05844772607088089f,-0.07081260532140732f,0.08596939593553543f,0.5426119565963745f,0.5678461194038391f,1.0715678930282593f,-0.25178417563438416f };
Lightvalve 66:a8e6799dbce3 258
Lightvalve 168:f05a45d5f318 259 const float b1[16] = { 0.5956194400787354f,1.4903173446655273f,-1.7145336866378784f,0.2382747083902359f,1.812330722808838f,1.1310549974441528f,-0.058932315558195114f,1.0475828647613525f,0.5750831365585327f,-1.1691874265670776f,0.564017653465271f,0.8430382013320923f,-0.3627738058567047f,-0.8757394552230835f,-1.087764859199524f,1.8534502983093262f };
Lightvalve 87:471334725012 260
Lightvalve 168:f05a45d5f318 261 const float b2[16] = { -0.19254711270332336f,-1.2538373470306396f,-1.4564176797866821f,-0.6360846757888794f,-2.2159981727600098f,-0.5284036993980408f,0.07729385793209076f,-0.30969977378845215f,-1.20063054561615f,-1.912178635597229f,0.16195619106292725f,0.17741809785366058f,-0.3301374316215515f,1.8553483486175537f,-0.1654871553182602f,0.5861462950706482f };
Lightvalve 65:a2d7c63419c2 262
Lightvalve 168:f05a45d5f318 263 const float b3[16] = { -1.963319182395935f,-0.5293647646903992f,-0.10495924949645996f,-0.23769626021385193f,1.5550658702850342f,1.2147407531738281f,-0.47398853302001953f,-0.9554895758628845f,-0.067354217171669f,-0.006115084979683161f,-0.45877549052238464f,-0.290895938873291f,-1.0047727823257446f,1.1215981245040894f,-1.407442569732666f,-1.6007274389266968f };
Lightvalve 87:471334725012 264
Lightvalve 168:f05a45d5f318 265 const float bout[1] = { -0.10839174687862396f };
Lightvalve 65:a2d7c63419c2 266
Lightvalve 170:42c938a40313 267 /////////////////////////////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 268 float input_RL[num_input_RL] = { 0.0f };
Lightvalve 170:42c938a40313 269
Lightvalve 170:42c938a40313 270 //Critic Networks
Lightvalve 173:68c7914679ec 271 float hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 272 float bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 273 float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 274 float bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 275 float hc3[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 276 float bc3 = 0.0f;
Lightvalve 170:42c938a40313 277
Lightvalve 170:42c938a40313 278 //Critic Networks Temporary
Lightvalve 173:68c7914679ec 279 float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 280 float bc1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 281 float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 282 float bc2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 283 float hc3_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 284 float bc3_temp = 0.0f;
Lightvalve 170:42c938a40313 285
Lightvalve 170:42c938a40313 286 //Actor Networks
Lightvalve 173:68c7914679ec 287 float ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 288 float ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 289 float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 290 float ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 291 float ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 292 float ba3[2] = {0.0f};
Lightvalve 170:42c938a40313 293
Lightvalve 170:42c938a40313 294 //Actor Networks Temporary
Lightvalve 173:68c7914679ec 295 float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 296 float ba1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 297 float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 298 float ba2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 299 float ha3_temp[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 300 float ba3_temp[2] = {0.0f};
Lightvalve 66:a8e6799dbce3 301
Lightvalve 87:471334725012 302 float VALVE_POS_RAW_NN = 0.0f;
Lightvalve 87:471334725012 303 float DDV_JOINT_POS_FF(float REF_JOINT_VEL);
Lightvalve 61:bc8c8270f0ab 304
Lightvalve 170:42c938a40313 305
Lightvalve 170:42c938a40313 306 float Critic_Network(float *arr)
Lightvalve 170:42c938a40313 307 {
Lightvalve 173:68c7914679ec 308 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 309 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 310 float output = 0.0f;
Lightvalve 173:68c7914679ec 311 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 312 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 313 output1[index2] = output1[index2] + hc1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 314 }
Lightvalve 173:68c7914679ec 315 //ReLU
Lightvalve 173:68c7914679ec 316 output1[index2] = output1[index2] + bc1[index2];
Lightvalve 173:68c7914679ec 317 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 318 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 319 output1[index2] = 0;
Lightvalve 173:68c7914679ec 320 }
Lightvalve 173:68c7914679ec 321 //tanh
Lightvalve 173:68c7914679ec 322 //output1[index2] = tanh(output1[index2] + bc1[index2]);
Lightvalve 173:68c7914679ec 323 }
Lightvalve 173:68c7914679ec 324 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 325 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 326 output2[index2] = output2[index2] + hc2[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 327 }
Lightvalve 173:68c7914679ec 328 //ReLU
Lightvalve 173:68c7914679ec 329 output2[index2] = output2[index2] + bc2[index2];
Lightvalve 173:68c7914679ec 330 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 331 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 332 output2[index2] = 0;
Lightvalve 173:68c7914679ec 333 }
Lightvalve 173:68c7914679ec 334 //tanh
Lightvalve 173:68c7914679ec 335 //output2[index2] = tanh(output2[index2] + bc2[index2]);
Lightvalve 170:42c938a40313 336 }
Lightvalve 170:42c938a40313 337 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 338 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 339 output = output + hc3[index1] * output2[index1];
Lightvalve 170:42c938a40313 340 }
Lightvalve 173:68c7914679ec 341 output = output + bc3;
Lightvalve 173:68c7914679ec 342 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 343 }
Lightvalve 170:42c938a40313 344 return output;
Lightvalve 170:42c938a40313 345 }
Lightvalve 170:42c938a40313 346
Lightvalve 170:42c938a40313 347 float Critic_Network_Temp(float *arr)
Lightvalve 170:42c938a40313 348 {
Lightvalve 173:68c7914679ec 349 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 350 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 351 float output = 0.0f;
Lightvalve 173:68c7914679ec 352 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 353 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 354 output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 355 }
Lightvalve 173:68c7914679ec 356 //ReLU
Lightvalve 173:68c7914679ec 357 output1[index2] = output1[index2] + bc1_temp[index2];
Lightvalve 173:68c7914679ec 358 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 359 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 360 output1[index2] = 0;
Lightvalve 173:68c7914679ec 361 }
Lightvalve 173:68c7914679ec 362 //tanh
Lightvalve 173:68c7914679ec 363 //output1[index2] = tanh(output1[index2] + bc1_temp[index2]);
Lightvalve 173:68c7914679ec 364 }
Lightvalve 173:68c7914679ec 365 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 366 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 367 output2[index2] = output2[index2] + hc2_temp[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 368 }
Lightvalve 173:68c7914679ec 369 //ReLU
Lightvalve 173:68c7914679ec 370 output2[index2] = output2[index2] + bc2_temp[index2];
Lightvalve 173:68c7914679ec 371 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 372 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 373 output2[index2] = 0;
Lightvalve 173:68c7914679ec 374 }
Lightvalve 173:68c7914679ec 375 //tanh
Lightvalve 173:68c7914679ec 376 //output2[index2] = tanh(output2[index2] + bc2_temp[index2]);
Lightvalve 170:42c938a40313 377 }
Lightvalve 170:42c938a40313 378 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 379 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 380 output = output + hc3_temp[index1] * output2[index1];
Lightvalve 170:42c938a40313 381 }
Lightvalve 173:68c7914679ec 382 output = output + bc3_temp;
Lightvalve 173:68c7914679ec 383 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 384 }
Lightvalve 170:42c938a40313 385 return output;
Lightvalve 170:42c938a40313 386 }
Lightvalve 170:42c938a40313 387
Lightvalve 170:42c938a40313 388
Lightvalve 170:42c938a40313 389 void Actor_Network(float *arr)
Lightvalve 170:42c938a40313 390 {
Lightvalve 173:68c7914679ec 391 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 392 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 170:42c938a40313 393 float output[2] = {0.0f};
Lightvalve 170:42c938a40313 394
Lightvalve 173:68c7914679ec 395 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 396 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 397 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 398 }
Lightvalve 173:68c7914679ec 399 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 173:68c7914679ec 400 hx_a_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 401 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 402 output1[index2] = 0;
Lightvalve 173:68c7914679ec 403 }
Lightvalve 173:68c7914679ec 404 }
Lightvalve 173:68c7914679ec 405 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 406 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 407 output2[index2] = output2[index2] + ha2[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 408 }
Lightvalve 173:68c7914679ec 409 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 410 hxh_a_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 411 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 412 output2[index2] = 0;
Lightvalve 173:68c7914679ec 413 }
Lightvalve 173:68c7914679ec 414 }
Lightvalve 173:68c7914679ec 415 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 416 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 417 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 173:68c7914679ec 418 }
Lightvalve 173:68c7914679ec 419 }
Lightvalve 173:68c7914679ec 420 hxhh_a_sum[0] = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 421 hxhh_a_sum[1] = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 422
Lightvalve 173:68c7914679ec 423 mean_before_SP = output[0] + ba3[0]; //SP = softplus
Lightvalve 173:68c7914679ec 424 deviation_before_SP = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 425 //Softplus
Lightvalve 173:68c7914679ec 426 mean = log(1.0f+exp(mean_before_SP));
Lightvalve 173:68c7914679ec 427 deviation = log(1.0f+exp(deviation_before_SP));
Lightvalve 173:68c7914679ec 428 }
Lightvalve 173:68c7914679ec 429
Lightvalve 173:68c7914679ec 430
Lightvalve 173:68c7914679ec 431 void Actor_Network_Old(float *arr)
Lightvalve 173:68c7914679ec 432 {
Lightvalve 173:68c7914679ec 433 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 434 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 435 float output[2] = {0.0f};
Lightvalve 173:68c7914679ec 436
Lightvalve 173:68c7914679ec 437 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 438 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 439 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 440 }
Lightvalve 170:42c938a40313 441 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 170:42c938a40313 442 if (output1[index2] < 0) {
Lightvalve 170:42c938a40313 443 output1[index2] = 0;
Lightvalve 170:42c938a40313 444 }
Lightvalve 170:42c938a40313 445 }
Lightvalve 173:68c7914679ec 446 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 447 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 448 output2[index2] = output2[index2] + ha2[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 449 }
Lightvalve 173:68c7914679ec 450 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 451 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 452 output2[index2] = 0;
Lightvalve 170:42c938a40313 453 }
Lightvalve 170:42c938a40313 454 }
Lightvalve 170:42c938a40313 455 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 456 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 457 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 170:42c938a40313 458 }
Lightvalve 170:42c938a40313 459 }
Lightvalve 173:68c7914679ec 460 mean_old = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 461 deviation_old = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 462 //Softplus
Lightvalve 173:68c7914679ec 463 mean_old = log(1.0f+exp(mean_old));
Lightvalve 173:68c7914679ec 464 deviation_old = log(1.0f+exp(deviation_old));
Lightvalve 170:42c938a40313 465 }
Lightvalve 170:42c938a40313 466
Lightvalve 170:42c938a40313 467 float Grad_Normal_Dist_Mean(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 468 {
Lightvalve 170:42c938a40313 469 float grad_mean = 0.0f;
Lightvalve 170:42c938a40313 470 grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation);
Lightvalve 170:42c938a40313 471 return grad_mean;
Lightvalve 170:42c938a40313 472 }
Lightvalve 170:42c938a40313 473
Lightvalve 170:42c938a40313 474 float Grad_Normal_Dist_Deviation(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 475 {
Lightvalve 170:42c938a40313 476 float grad_dev = 0.0f;
Lightvalve 170:42c938a40313 477 grad_dev = exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))*(-1.0f/(sqrt(2.0f*PI)*deviation*deviation) + (action-mean)*(action-mean)/(sqrt(2.0f*PI)*deviation*deviation*deviation*deviation));
Lightvalve 170:42c938a40313 478 return grad_dev;
Lightvalve 170:42c938a40313 479 }
Lightvalve 170:42c938a40313 480
Lightvalve 173:68c7914679ec 481 float ReLU(float x)
Lightvalve 173:68c7914679ec 482 {
Lightvalve 173:68c7914679ec 483 if (x >= 0) {
Lightvalve 173:68c7914679ec 484 return x;
Lightvalve 173:68c7914679ec 485 } else {
Lightvalve 173:68c7914679ec 486 return 0.0f;
Lightvalve 173:68c7914679ec 487 }
Lightvalve 173:68c7914679ec 488 }
Lightvalve 173:68c7914679ec 489
Lightvalve 170:42c938a40313 490 void update_Critic_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 491 {
Lightvalve 172:63af34265fe9 492 float gradient_rate = 0.001f;
Lightvalve 173:68c7914679ec 493 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 494
Lightvalve 173:68c7914679ec 495
Lightvalve 173:68c7914679ec 496 ///////////////////////////////////////////////////////////CRITIC
Lightvalve 173:68c7914679ec 497 float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 498 float G_bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 499 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 500 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 501 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 502 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 503 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 504 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 505 G_hc1[index1][index2] = G_hc1[index1][index2] + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 506 }
Lightvalve 173:68c7914679ec 507 }
Lightvalve 170:42c938a40313 508 }
Lightvalve 170:42c938a40313 509 }
Lightvalve 170:42c938a40313 510 G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 511 //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 173:68c7914679ec 512 }
Lightvalve 173:68c7914679ec 513 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 514 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 515 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 516 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 517 G_bc1[index2] = G_bc1[index2] + hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 518 }
Lightvalve 173:68c7914679ec 519
Lightvalve 173:68c7914679ec 520 }
Lightvalve 173:68c7914679ec 521 }
Lightvalve 173:68c7914679ec 522 }
Lightvalve 173:68c7914679ec 523 G_bc1[index2] = G_bc1[index2] / batch_size;
Lightvalve 173:68c7914679ec 524 //bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 173:68c7914679ec 525 }
Lightvalve 173:68c7914679ec 526
Lightvalve 173:68c7914679ec 527 float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 528 float G_bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 529 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 530 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 531 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 532 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 533 if (hx_c_sum_array[n][index1] > 0) {
Lightvalve 173:68c7914679ec 534 G_hc2[index1][index2] = G_hc2[index1][index2] + hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 173:68c7914679ec 535 }
Lightvalve 173:68c7914679ec 536 }
Lightvalve 173:68c7914679ec 537 }
Lightvalve 173:68c7914679ec 538 G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 539 //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 540 }
Lightvalve 173:68c7914679ec 541 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 542 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 543 G_bc2[index2] = G_bc2[index2] + hc3_temp[index2];
Lightvalve 173:68c7914679ec 544 }
Lightvalve 173:68c7914679ec 545 }
Lightvalve 173:68c7914679ec 546 G_bc2[index2] = G_bc2[index2] / batch_size;
Lightvalve 173:68c7914679ec 547 //bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 173:68c7914679ec 548 }
Lightvalve 173:68c7914679ec 549
Lightvalve 173:68c7914679ec 550 float G_hc3[num_hidden_unit2]= {0.0f};
Lightvalve 173:68c7914679ec 551 float G_bc3 = 0.0f;
Lightvalve 173:68c7914679ec 552 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 553 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 554 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 555 if (hxh_c_sum_array[n][index1] >= 0) {
Lightvalve 173:68c7914679ec 556 G_hc3[index1] = G_hc3[index1] + hxh_c_sum_array[n][index1];
Lightvalve 173:68c7914679ec 557 }
Lightvalve 173:68c7914679ec 558 }
Lightvalve 173:68c7914679ec 559 G_hc3[index1] = G_hc3[index1] / batch_size;
Lightvalve 173:68c7914679ec 560 //hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 173:68c7914679ec 561 }
Lightvalve 173:68c7914679ec 562 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 563 G_bc2[index2] = G_bc2[index2] + 1.0f;
Lightvalve 173:68c7914679ec 564 }
Lightvalve 173:68c7914679ec 565 G_bc3 = G_bc3 / batch_size;
Lightvalve 173:68c7914679ec 566 //bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 173:68c7914679ec 567 }
Lightvalve 173:68c7914679ec 568
Lightvalve 173:68c7914679ec 569 // Simultaneous Update
Lightvalve 173:68c7914679ec 570 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 571 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 572 hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 170:42c938a40313 573 }
Lightvalve 170:42c938a40313 574 bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 170:42c938a40313 575 }
Lightvalve 173:68c7914679ec 576 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 577 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 578 hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 579 }
Lightvalve 173:68c7914679ec 580 bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 173:68c7914679ec 581 }
Lightvalve 170:42c938a40313 582 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 583 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 584 hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 170:42c938a40313 585 }
Lightvalve 173:68c7914679ec 586 bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 170:42c938a40313 587 }
Lightvalve 173:68c7914679ec 588
Lightvalve 170:42c938a40313 589 }
Lightvalve 170:42c938a40313 590
Lightvalve 173:68c7914679ec 591 ///////////////////////////Softplus//////////////////////////////////
Lightvalve 170:42c938a40313 592 void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 593 {
Lightvalve 173:68c7914679ec 594 float gradient_rate = 0.001f;
Lightvalve 173:68c7914679ec 595
Lightvalve 173:68c7914679ec 596 float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 597 float G_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 598 float d_x_d_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 599 float d_x_d_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 600 float d_y_d_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 601 float d_y_d_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 602
Lightvalve 173:68c7914679ec 603 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 604 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 605 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 606 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 607 G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 170:42c938a40313 608 } else {
Lightvalve 173:68c7914679ec 609 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 610 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 611 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 612 d_x_d_ha1[index1][index2] = d_x_d_ha1[index1][index2] + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 173:68c7914679ec 613 d_y_d_ha1[index1][index2] = d_y_d_ha1[index1][index2] + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 614 }
Lightvalve 171:bfc1fd2629d8 615 }
Lightvalve 170:42c938a40313 616 }
Lightvalve 170:42c938a40313 617 float d_mean_d_ha1 = 0.0f;
Lightvalve 170:42c938a40313 618 float d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 619 d_mean_d_ha1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha1[index1][index2];
Lightvalve 173:68c7914679ec 620 d_dev_d_ha1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha1[index1][index2];
Lightvalve 173:68c7914679ec 621
Lightvalve 173:68c7914679ec 622 G_ha1[index1][index2] = G_ha1[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 623 }
Lightvalve 170:42c938a40313 624 }
Lightvalve 170:42c938a40313 625 G_ha1[index1][index2] = G_ha1[index1][index2] / batch_size;
Lightvalve 170:42c938a40313 626 ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 170:42c938a40313 627 }
Lightvalve 173:68c7914679ec 628
Lightvalve 173:68c7914679ec 629 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 630 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 631 G_ba1[index2] = G_ba1[index2];
Lightvalve 170:42c938a40313 632 } else {
Lightvalve 173:68c7914679ec 633 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 634 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 635 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 636 d_x_d_ba1[index2] = d_x_d_ba1[index2] + ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 173:68c7914679ec 637 d_y_d_ba1[index2] = d_y_d_ba1[index2] + ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 638 }
Lightvalve 171:bfc1fd2629d8 639 }
Lightvalve 170:42c938a40313 640 }
Lightvalve 170:42c938a40313 641 float d_mean_d_ba1 = 0.0f;
Lightvalve 170:42c938a40313 642 float d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 643 d_mean_d_ba1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba1[index2];
Lightvalve 173:68c7914679ec 644 d_dev_d_ba1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba1[index2];
Lightvalve 173:68c7914679ec 645
Lightvalve 173:68c7914679ec 646 G_ba1[index2] = G_ba1[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 647 }
Lightvalve 170:42c938a40313 648 }
Lightvalve 170:42c938a40313 649 G_ba1[index2] = G_ba1[index2] / batch_size;
Lightvalve 170:42c938a40313 650 ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 170:42c938a40313 651 }
Lightvalve 170:42c938a40313 652
Lightvalve 173:68c7914679ec 653 float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 654 float G_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 655 float d_x_d_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 656 float d_x_d_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 657 float d_y_d_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 658 float d_y_d_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 659
Lightvalve 173:68c7914679ec 660 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 661 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 662 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 663 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 664 G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 170:42c938a40313 665 } else {
Lightvalve 171:bfc1fd2629d8 666
Lightvalve 173:68c7914679ec 667 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 668 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 173:68c7914679ec 669 d_x_d_ha2[index1][index2] = d_x_d_ha2[index1][index2] + hx_a_sum_array[n][index1]*ha3_temp[index2][0];
Lightvalve 173:68c7914679ec 670 d_y_d_ha2[index1][index2] = d_y_d_ha2[index1][index2] + hx_a_sum_array[n][index1]*ha3_temp[index2][1];
Lightvalve 171:bfc1fd2629d8 671 }
Lightvalve 170:42c938a40313 672 }
Lightvalve 173:68c7914679ec 673
Lightvalve 170:42c938a40313 674 float d_mean_d_ha2 = 0.0f;
Lightvalve 170:42c938a40313 675 float d_dev_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 676 d_mean_d_ha2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha2[index1][index2];
Lightvalve 173:68c7914679ec 677 d_dev_d_ha2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha2[index1][index2];
Lightvalve 173:68c7914679ec 678
Lightvalve 173:68c7914679ec 679 G_ha2[index1][index2] = G_ha2[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 680 }
Lightvalve 170:42c938a40313 681 }
Lightvalve 170:42c938a40313 682 G_ha2[index1][index2] = G_ha2[index1][index2] / batch_size;
Lightvalve 170:42c938a40313 683 ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 170:42c938a40313 684 }
Lightvalve 173:68c7914679ec 685
Lightvalve 173:68c7914679ec 686 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 687 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 688 G_ba2[index2] = G_ba2[index2];
Lightvalve 170:42c938a40313 689 } else {
Lightvalve 170:42c938a40313 690
Lightvalve 173:68c7914679ec 691 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 692 d_x_d_ba2[index2] = d_x_d_ba2[index2] + ha3_temp[index2][0];
Lightvalve 173:68c7914679ec 693 d_y_d_ba2[index2] = d_y_d_ba2[index2] + ha3_temp[index2][1];
Lightvalve 173:68c7914679ec 694 }
Lightvalve 173:68c7914679ec 695 float d_mean_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 696 float d_dev_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 697 d_mean_d_ba2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba2[index2];
Lightvalve 173:68c7914679ec 698 d_dev_d_ba2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba2[index2];
Lightvalve 173:68c7914679ec 699
Lightvalve 173:68c7914679ec 700 G_ba2[index2] = G_ba2[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 701 }
Lightvalve 170:42c938a40313 702 }
Lightvalve 170:42c938a40313 703 G_ba2[index2] = G_ba2[index2] / batch_size;
Lightvalve 170:42c938a40313 704 ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 170:42c938a40313 705 }
Lightvalve 173:68c7914679ec 706
Lightvalve 173:68c7914679ec 707 float G_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 708 float G_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 709 float d_x_d_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 710 float d_x_d_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 711 float d_y_d_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 712 float d_y_d_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 713
Lightvalve 173:68c7914679ec 714 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 715 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 716 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 717 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 718 G_ha3[index1][index2] = G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 719 } else {
Lightvalve 173:68c7914679ec 720
Lightvalve 173:68c7914679ec 721 if (hxh_a_sum_array[n][index1] >= 0) {
Lightvalve 173:68c7914679ec 722 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 173:68c7914679ec 723 d_x_d_ha3[index1][index2] = d_x_d_ha3[index1][index2] + hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 724 d_y_d_ha3[index1][index2] = d_y_d_ha3[index1][index2] + hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 725
Lightvalve 173:68c7914679ec 726 }
Lightvalve 173:68c7914679ec 727 }
Lightvalve 173:68c7914679ec 728 float d_mean_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 729 float d_dev_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 730 d_mean_d_ha3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha3[index1][index2];
Lightvalve 173:68c7914679ec 731 d_dev_d_ha3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha3[index1][index2];
Lightvalve 173:68c7914679ec 732
Lightvalve 173:68c7914679ec 733 G_ha3[index1][index2] = G_ha3[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 734 }
Lightvalve 173:68c7914679ec 735 }
Lightvalve 173:68c7914679ec 736 G_ha3[index1][index2] = G_ha3[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 737 ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate * G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 738 }
Lightvalve 173:68c7914679ec 739
Lightvalve 173:68c7914679ec 740 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 741 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 742 G_ba3[index2] = G_ba3[index2];
Lightvalve 173:68c7914679ec 743 } else {
Lightvalve 173:68c7914679ec 744
Lightvalve 173:68c7914679ec 745 d_x_d_ba3[index2] = d_x_d_ba3[index2] + 1.0f;
Lightvalve 173:68c7914679ec 746 d_y_d_ba3[index2] = d_y_d_ba3[index2] + 1.0f;
Lightvalve 173:68c7914679ec 747
Lightvalve 173:68c7914679ec 748 float d_mean_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 749 float d_dev_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 750 d_mean_d_ba3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba3[index2];
Lightvalve 173:68c7914679ec 751 d_dev_d_ba3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba3[index2];
Lightvalve 173:68c7914679ec 752
Lightvalve 173:68c7914679ec 753 G_ba3[index2] = G_ba3[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 754 }
Lightvalve 173:68c7914679ec 755 }
Lightvalve 173:68c7914679ec 756 G_ba3[index2] = G_ba3[index2] / batch_size;
Lightvalve 173:68c7914679ec 757 ba3_temp[index2] = ba3_temp[index2] - gradient_rate * G_ba3[index2];
Lightvalve 173:68c7914679ec 758 }
Lightvalve 170:42c938a40313 759 }
Lightvalve 170:42c938a40313 760
Lightvalve 173:68c7914679ec 761 ///////////////////////////ReLU - Bad performance//////////////////////////////////
Lightvalve 173:68c7914679ec 762 //void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 173:68c7914679ec 763 //{
Lightvalve 173:68c7914679ec 764 // float gradient_rate = 0.001f; //-0.01f
Lightvalve 173:68c7914679ec 765 //
Lightvalve 173:68c7914679ec 766 // float G_ha1[num_input_RL][num_hidden_unit] = {0.0f};
Lightvalve 173:68c7914679ec 767 // float G_ba1[num_hidden_unit] = {0.0f};
Lightvalve 173:68c7914679ec 768 // for (int index2 = 0; index2 < num_hidden_unit; index2++) {
Lightvalve 173:68c7914679ec 769 // for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 770 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 771 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 772 // G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 173:68c7914679ec 773 // } else {
Lightvalve 173:68c7914679ec 774 //
Lightvalve 173:68c7914679ec 775 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 776 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 777 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 778 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 779 // }
Lightvalve 173:68c7914679ec 780 // }
Lightvalve 173:68c7914679ec 781 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 782 // float d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 783 // float d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 784 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 785 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 786 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 787 // hx_sum = hx_sum + ha1_temp[j][index2]*arr[i][j];
Lightvalve 173:68c7914679ec 788 // }
Lightvalve 173:68c7914679ec 789 // hx_sum = hx_sum + bc1_temp[index2];
Lightvalve 173:68c7914679ec 790 // if (hx_sum >= 0) {
Lightvalve 173:68c7914679ec 791 // d_mean_d_ha1 = ha2_temp[index2][0]*arr[i][index1];
Lightvalve 173:68c7914679ec 792 // d_dev_d_ha1 = ha2_temp[index2][1]*arr[i][index1];
Lightvalve 173:68c7914679ec 793 // } else {
Lightvalve 173:68c7914679ec 794 // d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 795 // d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 796 // }
Lightvalve 173:68c7914679ec 797 // } else {
Lightvalve 173:68c7914679ec 798 // d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 799 // d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 800 // }
Lightvalve 173:68c7914679ec 801 // G_ha1[index1][index2] = G_ha1[index1][index2] + advantage[i]/pi_old[i]*(d_mean_d_ha1*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ha1*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 802 // }
Lightvalve 173:68c7914679ec 803 // }
Lightvalve 173:68c7914679ec 804 // G_ha1[index1][index2] = G_ha1[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 805 // ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 173:68c7914679ec 806 // }
Lightvalve 173:68c7914679ec 807 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 808 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 809 // G_ba1[index2] = G_ba1[index2];
Lightvalve 173:68c7914679ec 810 // } else {
Lightvalve 173:68c7914679ec 811 //
Lightvalve 173:68c7914679ec 812 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 813 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 814 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 815 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 816 // }
Lightvalve 173:68c7914679ec 817 // }
Lightvalve 173:68c7914679ec 818 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 819 // float d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 820 // float d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 821 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 822 //
Lightvalve 173:68c7914679ec 823 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 824 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 825 // hx_sum = hx_sum + ha1_temp[j][index2]*arr[i][j];
Lightvalve 173:68c7914679ec 826 // }
Lightvalve 173:68c7914679ec 827 // hx_sum = hx_sum + bc1_temp[index2];
Lightvalve 173:68c7914679ec 828 //
Lightvalve 173:68c7914679ec 829 // if(hx_sum >=0) {
Lightvalve 173:68c7914679ec 830 // d_mean_d_ba1 = ha2_temp[index2][0];
Lightvalve 173:68c7914679ec 831 // d_dev_d_ba1 = ha2_temp[index2][1];
Lightvalve 173:68c7914679ec 832 // } else {
Lightvalve 173:68c7914679ec 833 // d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 834 // d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 835 // }
Lightvalve 173:68c7914679ec 836 // } else {
Lightvalve 173:68c7914679ec 837 // d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 838 // d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 839 // }
Lightvalve 173:68c7914679ec 840 // G_ba1[index2] = G_ba1[index2] + advantage[i]/pi_old[i]*(d_mean_d_ba1*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ba1*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 841 // }
Lightvalve 173:68c7914679ec 842 // }
Lightvalve 173:68c7914679ec 843 // G_ba1[index2] = G_ba1[index2] / batch_size;
Lightvalve 173:68c7914679ec 844 // ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 173:68c7914679ec 845 // }
Lightvalve 173:68c7914679ec 846 //
Lightvalve 173:68c7914679ec 847 // float G_ha2[num_hidden_unit][2] = {0.0f};
Lightvalve 173:68c7914679ec 848 // float G_ba2[2] = {0.0f};
Lightvalve 173:68c7914679ec 849 // for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 850 // for (int index1 = 0; index1 < num_hidden_unit; index1++) {
Lightvalve 173:68c7914679ec 851 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 852 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 853 // G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 173:68c7914679ec 854 // } else {
Lightvalve 173:68c7914679ec 855 //
Lightvalve 173:68c7914679ec 856 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 857 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 858 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 859 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 860 // }
Lightvalve 173:68c7914679ec 861 // }
Lightvalve 173:68c7914679ec 862 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 863 // float d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 864 // float d_dev_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 865 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 866 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 867 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 868 // hx_sum = hx_sum + ha1_temp[j][index1]*arr[i][j];
Lightvalve 173:68c7914679ec 869 // }
Lightvalve 173:68c7914679ec 870 // hx_sum = hx_sum + bc1_temp[index1];
Lightvalve 173:68c7914679ec 871 // if (hx_sum >= 0) {
Lightvalve 173:68c7914679ec 872 // d_mean_d_ha2 = hx_sum;
Lightvalve 173:68c7914679ec 873 // d_dev_d_ha2 = hx_sum;
Lightvalve 173:68c7914679ec 874 // } else {
Lightvalve 173:68c7914679ec 875 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 876 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 877 // }
Lightvalve 173:68c7914679ec 878 // } else {
Lightvalve 173:68c7914679ec 879 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 880 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 881 // }
Lightvalve 173:68c7914679ec 882 // G_ha2[index1][index2] = G_ha2[index1][index2] + advantage[i]/pi_old[i]*(d_mean_d_ha2*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ha2*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 883 // }
Lightvalve 173:68c7914679ec 884 // }
Lightvalve 173:68c7914679ec 885 // G_ha2[index1][index2] = G_ha2[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 886 // ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 173:68c7914679ec 887 // }
Lightvalve 173:68c7914679ec 888 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 889 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 890 // G_ba2[index2] = G_ba2[index2];
Lightvalve 173:68c7914679ec 891 // } else {
Lightvalve 173:68c7914679ec 892 //
Lightvalve 173:68c7914679ec 893 // float d_mean_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 894 // float d_dev_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 895 // d_mean_d_ba2 = 1.0f;
Lightvalve 173:68c7914679ec 896 // d_dev_d_ba2 = 1.0f;
Lightvalve 173:68c7914679ec 897 // G_ba1[index2] = G_ba1[index2] + advantage[i]/pi_old[i]*(d_mean_d_ba2*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ba2*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 898 // }
Lightvalve 173:68c7914679ec 899 // }
Lightvalve 173:68c7914679ec 900 // G_ba2[index2] = G_ba2[index2] / batch_size;
Lightvalve 173:68c7914679ec 901 // ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 173:68c7914679ec 902 // }
Lightvalve 173:68c7914679ec 903 //}
Lightvalve 173:68c7914679ec 904
Lightvalve 170:42c938a40313 905
Lightvalve 170:42c938a40313 906
Lightvalve 170:42c938a40313 907 float rand_normal(double mean, double stddev)
Lightvalve 170:42c938a40313 908 {
Lightvalve 170:42c938a40313 909 //Box muller method
Lightvalve 170:42c938a40313 910 static double n2 = 0.0f;
Lightvalve 170:42c938a40313 911 static int n2_cached = 0;
Lightvalve 170:42c938a40313 912 if (!n2_cached) {
Lightvalve 170:42c938a40313 913 double x, y, r;
Lightvalve 170:42c938a40313 914 do {
Lightvalve 170:42c938a40313 915 x = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 916 y = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 917
Lightvalve 170:42c938a40313 918 r = x*x + y*y;
Lightvalve 170:42c938a40313 919 } while (r == 0.0f || r > 1.0f);
Lightvalve 170:42c938a40313 920 {
Lightvalve 170:42c938a40313 921 double d = sqrt(-2.0f*log(r)/r);
Lightvalve 170:42c938a40313 922 double n1 = x*d;
Lightvalve 170:42c938a40313 923 n2 = y*d;
Lightvalve 170:42c938a40313 924 double result = n1*stddev + mean;
Lightvalve 170:42c938a40313 925 n2_cached = 1;
Lightvalve 170:42c938a40313 926 return result;
Lightvalve 170:42c938a40313 927 }
Lightvalve 170:42c938a40313 928 } else {
Lightvalve 170:42c938a40313 929 n2_cached = 0;
Lightvalve 170:42c938a40313 930 return n2*stddev + mean;
Lightvalve 170:42c938a40313 931 }
Lightvalve 170:42c938a40313 932 }
Lightvalve 170:42c938a40313 933
Lightvalve 173:68c7914679ec 934
Lightvalve 170:42c938a40313 935 void Overwirte_Critic_Networks()
Lightvalve 170:42c938a40313 936 {
Lightvalve 173:68c7914679ec 937 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 938 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 939 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 170:42c938a40313 940 }
Lightvalve 170:42c938a40313 941 bc1[index2] = bc1_temp[index2];
Lightvalve 170:42c938a40313 942 }
Lightvalve 173:68c7914679ec 943 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 944 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 945 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 173:68c7914679ec 946 }
Lightvalve 173:68c7914679ec 947 bc2[index2] = bc2_temp[index2];
Lightvalve 173:68c7914679ec 948 hc3[index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 949 }
Lightvalve 173:68c7914679ec 950 bc3 = bc3_temp;
Lightvalve 170:42c938a40313 951 }
Lightvalve 170:42c938a40313 952 void Overwirte_Actor_Networks()
Lightvalve 170:42c938a40313 953 {
Lightvalve 173:68c7914679ec 954 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 955 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 956 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 170:42c938a40313 957 }
Lightvalve 170:42c938a40313 958 ba1[index2] = ba1_temp[index2];
Lightvalve 170:42c938a40313 959 }
Lightvalve 173:68c7914679ec 960 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 961 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:42c938a40313 962 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 170:42c938a40313 963 }
Lightvalve 170:42c938a40313 964 ba2[index2] = ba2_temp[index2];
Lightvalve 170:42c938a40313 965 }
Lightvalve 173:68c7914679ec 966 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 967 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 968 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 173:68c7914679ec 969 }
Lightvalve 173:68c7914679ec 970 ba3[index2] = ba3_temp[index2];
Lightvalve 173:68c7914679ec 971 }
Lightvalve 170:42c938a40313 972 }
Lightvalve 170:42c938a40313 973
Lightvalve 170:42c938a40313 974
GiJeongKim 0:51c43836c1d7 975 int main()
GiJeongKim 0:51c43836c1d7 976 {
Lightvalve 66:a8e6799dbce3 977
Lightvalve 65:a2d7c63419c2 978 HAL_Init();
Lightvalve 65:a2d7c63419c2 979 SystemClock_Config();
Lightvalve 169:645207e160ca 980
jobuuu 6:df07d3491e3a 981 /*********************************
jobuuu 1:e04e563be5ce 982 *** Initialization
jobuuu 6:df07d3491e3a 983 *********************************/
Lightvalve 69:3995ffeaa786 984 LED = 0;
Lightvalve 61:bc8c8270f0ab 985 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 986
GiJeongKim 0:51c43836c1d7 987 // i2c init
Lightvalve 8:5d2eebdad025 988 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 989 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 990 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 991 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 992 make_delay();
jobuuu 2:a1c0a37df760 993
GiJeongKim 0:51c43836c1d7 994 // // spi init
Lightvalve 170:42c938a40313 995 eeprom.format(8,3);
Lightvalve 170:42c938a40313 996 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 997 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 998 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 999 make_delay();
Lightvalve 21:e5f1a43ea6f9 1000
Lightvalve 16:903b5a4433b4 1001 //rom
Lightvalve 19:23b7c1ad8683 1002 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 1003 make_delay();
Lightvalve 13:747daba9cf59 1004
GiJeongKim 0:51c43836c1d7 1005 // ADC init
jobuuu 5:a4319f79457b 1006 Init_ADC();
Lightvalve 11:82d8768d7351 1007 make_delay();
jobuuu 2:a1c0a37df760 1008
GiJeongKim 0:51c43836c1d7 1009 // Pwm init
GiJeongKim 0:51c43836c1d7 1010 Init_PWM();
GiJeongKim 0:51c43836c1d7 1011 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 1012 make_delay();
Lightvalve 13:747daba9cf59 1013
Lightvalve 11:82d8768d7351 1014 // TMR3 init
Lightvalve 11:82d8768d7351 1015 Init_TMR3();
Lightvalve 11:82d8768d7351 1016 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 1017 make_delay();
Lightvalve 21:e5f1a43ea6f9 1018
Lightvalve 50:3c630b5eba9f 1019 // TMR2 init
Lightvalve 56:6f50d9d3bfee 1020 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 1021 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 1022 // make_delay();
Lightvalve 21:e5f1a43ea6f9 1023
GiJeongKim 0:51c43836c1d7 1024 // CAN
jobuuu 2:a1c0a37df760 1025 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 1026 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 1027 make_delay();
Lightvalve 34:bb2ca2fc2a8e 1028
Lightvalve 23:59218d4a256d 1029 //Timer priority
Lightvalve 23:59218d4a256d 1030 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 57:f4819de54e7a 1031 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 1032 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 1033
Lightvalve 23:59218d4a256d 1034 //can.reset();
Lightvalve 19:23b7c1ad8683 1035 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 1036
GiJeongKim 0:51c43836c1d7 1037 // spi _ enc
GiJeongKim 0:51c43836c1d7 1038 spi_enc_set_init();
Lightvalve 11:82d8768d7351 1039 make_delay();
Lightvalve 13:747daba9cf59 1040
Lightvalve 11:82d8768d7351 1041 //DAC init
Lightvalve 58:2eade98630e2 1042 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1043 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1044 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 1045 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1046 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1047 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1048 }
Lightvalve 11:82d8768d7351 1049 make_delay();
Lightvalve 13:747daba9cf59 1050
Lightvalve 19:23b7c1ad8683 1051 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 1052 if(i%2==0)
Lightvalve 38:118df027d851 1053 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 1054 else
Lightvalve 38:118df027d851 1055 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 1056 }
Lightvalve 169:645207e160ca 1057
Lightvalve 173:68c7914679ec 1058 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 1059 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 1060 hc1_temp[index1][index2] = (float) (rand()%100) * 0.01f ;
Lightvalve 170:42c938a40313 1061 }
Lightvalve 170:42c938a40313 1062 bc1_temp[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1063 }
Lightvalve 173:68c7914679ec 1064 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 1065 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 1066 hc2_temp[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1067 }
Lightvalve 173:68c7914679ec 1068 bc2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1069 hc3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1070 }
Lightvalve 173:68c7914679ec 1071 bc3 = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1072
Lightvalve 173:68c7914679ec 1073 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 1074 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 1075 ha1[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1076 }
Lightvalve 173:68c7914679ec 1077 ba1[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1078 }
Lightvalve 173:68c7914679ec 1079 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 1080 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 1081 ha2[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1082 }
Lightvalve 173:68c7914679ec 1083 ba2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1084 }
Lightvalve 170:42c938a40313 1085 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 1086 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 1087 ha3[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1088 }
Lightvalve 173:68c7914679ec 1089 ba3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1090 }
Lightvalve 171:bfc1fd2629d8 1091
Lightvalve 170:42c938a40313 1092 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1093 Overwirte_Actor_Networks();
Lightvalve 61:bc8c8270f0ab 1094
jobuuu 6:df07d3491e3a 1095 /************************************
jobuuu 1:e04e563be5ce 1096 *** Program is operating!
jobuuu 6:df07d3491e3a 1097 *************************************/
GiJeongKim 0:51c43836c1d7 1098 while(1) {
Lightvalve 169:645207e160ca 1099
Lightvalve 171:bfc1fd2629d8 1100 // if(timer_while==27491) {
Lightvalve 171:bfc1fd2629d8 1101 // timer_while = 0;
Lightvalve 171:bfc1fd2629d8 1102 // pc.printf("ref : %f virt_pos : %f mean : %f deviation : %f Last_pos_of_batch : %f reward_sum : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), logging3, logging2, logging4, logging1, logging5);
Lightvalve 171:bfc1fd2629d8 1103 // //pc.printf("%f\n", virt_pos);
Lightvalve 171:bfc1fd2629d8 1104 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 171:bfc1fd2629d8 1105 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 171:bfc1fd2629d8 1106 // }
Lightvalve 169:645207e160ca 1107
Lightvalve 171:bfc1fd2629d8 1108
Lightvalve 171:bfc1fd2629d8 1109 //i2c
Lightvalve 171:bfc1fd2629d8 1110 //read_field(i2c_slave_addr1);
Lightvalve 171:bfc1fd2629d8 1111 //if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 171:bfc1fd2629d8 1112
Lightvalve 171:bfc1fd2629d8 1113 // timer_while ++;
Lightvalve 169:645207e160ca 1114
Lightvalve 170:42c938a40313 1115 ///////////////////////////////////////////////////////Neural Network
Lightvalve 169:645207e160ca 1116
Lightvalve 73:f80dc3970c99 1117 if(NN_Control_Flag == 0) {
Lightvalve 73:f80dc3970c99 1118 LED = 0;
Lightvalve 73:f80dc3970c99 1119 }
Lightvalve 169:645207e160ca 1120
Lightvalve 73:f80dc3970c99 1121 else if(NN_Control_Flag == 1) {
Lightvalve 169:645207e160ca 1122
Lightvalve 162:9dd4f35e9de8 1123 int ind = 0;
Lightvalve 162:9dd4f35e9de8 1124 for(int i=0; i<numpast_u; i++) {
Lightvalve 162:9dd4f35e9de8 1125 input_NN[ind] = u_past[time_interval*i];
Lightvalve 162:9dd4f35e9de8 1126 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1127 }
Lightvalve 162:9dd4f35e9de8 1128
Lightvalve 162:9dd4f35e9de8 1129 for(int i=0; i<numpast_x; i++) {
Lightvalve 162:9dd4f35e9de8 1130 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 162:9dd4f35e9de8 1131 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1132 }
Lightvalve 162:9dd4f35e9de8 1133 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 162:9dd4f35e9de8 1134 ind = ind + 1;
Lightvalve 169:645207e160ca 1135
Lightvalve 162:9dd4f35e9de8 1136 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 162:9dd4f35e9de8 1137 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 122:dcb3ce3056a0 1138 // ind = ind + 1;
Lightvalve 122:dcb3ce3056a0 1139 // }
Lightvalve 162:9dd4f35e9de8 1140
Lightvalve 162:9dd4f35e9de8 1141 for(int i=0; i<numpast_f; i++) {
Lightvalve 162:9dd4f35e9de8 1142 input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1143 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1144 }
Lightvalve 162:9dd4f35e9de8 1145 input_NN[ind] = torq.sen / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1146 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1147 for(int i=0; i<numfuture_f; i++) {
Lightvalve 168:f05a45d5f318 1148 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f+0.5f;
Lightvalve 168:f05a45d5f318 1149 input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f+0.5f;
Lightvalve 162:9dd4f35e9de8 1150 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1151 }
Lightvalve 169:645207e160ca 1152
Lightvalve 112:8dcb1600cb90 1153 float output1[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1154 float output2[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1155 float output3[16] = { 0.0f };
Lightvalve 66:a8e6799dbce3 1156 float output = 0.0f;
Lightvalve 65:a2d7c63419c2 1157
Lightvalve 112:8dcb1600cb90 1158 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 68:328e1be06f5d 1159 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 66:a8e6799dbce3 1160 output1[index2] = output1[index2]
Lightvalve 73:f80dc3970c99 1161 + h1[index1][index2] * input_NN[index1];
Lightvalve 66:a8e6799dbce3 1162 }
Lightvalve 66:a8e6799dbce3 1163 output1[index2] = output1[index2] + b1[index2];
Lightvalve 66:a8e6799dbce3 1164 if (output1[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1165 output1[index2] = 0;
Lightvalve 66:a8e6799dbce3 1166 }
Lightvalve 66:a8e6799dbce3 1167 }
Lightvalve 65:a2d7c63419c2 1168
Lightvalve 112:8dcb1600cb90 1169 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1170 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1171 output2[index2] = output2[index2]
Lightvalve 66:a8e6799dbce3 1172 + h2[index1][index2] * output1[index1];
Lightvalve 66:a8e6799dbce3 1173 }
Lightvalve 66:a8e6799dbce3 1174 output2[index2] = output2[index2] + b2[index2];
Lightvalve 66:a8e6799dbce3 1175 if (output2[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1176 output2[index2] = 0;
Lightvalve 66:a8e6799dbce3 1177 }
Lightvalve 66:a8e6799dbce3 1178 }
Lightvalve 65:a2d7c63419c2 1179
Lightvalve 112:8dcb1600cb90 1180 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1181 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1182 output3[index2] = output3[index2]
Lightvalve 66:a8e6799dbce3 1183 + h3[index1][index2] * output2[index1];
Lightvalve 66:a8e6799dbce3 1184 }
Lightvalve 66:a8e6799dbce3 1185 output3[index2] = output3[index2] + b3[index2];
Lightvalve 66:a8e6799dbce3 1186 if (output3[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1187 output3[index2] = 0;
Lightvalve 66:a8e6799dbce3 1188 }
Lightvalve 65:a2d7c63419c2 1189 }
Lightvalve 66:a8e6799dbce3 1190
Lightvalve 66:a8e6799dbce3 1191 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 112:8dcb1600cb90 1192 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 73:f80dc3970c99 1193 output = output + hout[index1] * output3[index1];
Lightvalve 66:a8e6799dbce3 1194 }
Lightvalve 66:a8e6799dbce3 1195 output = output + bout[index2];
Lightvalve 169:645207e160ca 1196
Lightvalve 66:a8e6799dbce3 1197 }
Lightvalve 73:f80dc3970c99 1198 output = 1.0f/(1.0f+exp(-output));
Lightvalve 101:50159049a518 1199 output_normalized = output;
Lightvalve 68:328e1be06f5d 1200 output = output * 20000.0f - 10000.0f;
Lightvalve 169:645207e160ca 1201
Lightvalve 66:a8e6799dbce3 1202 if(output>=0) {
Lightvalve 66:a8e6799dbce3 1203 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 66:a8e6799dbce3 1204 } else {
Lightvalve 66:a8e6799dbce3 1205 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 65:a2d7c63419c2 1206 }
Lightvalve 87:471334725012 1207
Lightvalve 169:645207e160ca 1208
Lightvalve 69:3995ffeaa786 1209 if(LED==1) {
Lightvalve 69:3995ffeaa786 1210 LED=0;
Lightvalve 69:3995ffeaa786 1211 } else
Lightvalve 69:3995ffeaa786 1212 LED = 1;
Lightvalve 169:645207e160ca 1213
Lightvalve 65:a2d7c63419c2 1214 }
Lightvalve 171:bfc1fd2629d8 1215
Lightvalve 171:bfc1fd2629d8 1216
Lightvalve 170:42c938a40313 1217 /////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 1218 switch (Update_Case) {
Lightvalve 170:42c938a40313 1219 case 0: {
Lightvalve 170:42c938a40313 1220 break;
Lightvalve 170:42c938a40313 1221 }
Lightvalve 170:42c938a40313 1222 case 1: {
Lightvalve 170:42c938a40313 1223 //Network Update(just update and hold network)
Lightvalve 170:42c938a40313 1224 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 170:42c938a40313 1225 float loss_sum = 0.0f;
Lightvalve 170:42c938a40313 1226 for (int i=batch_size-1; i>=0; i--) {
Lightvalve 170:42c938a40313 1227 //Calculate Estimated V
Lightvalve 170:42c938a40313 1228 //float temp_array[3] = {state_array[i][0], state_array[i][1], state_array[i][2]};
Lightvalve 170:42c938a40313 1229 float temp_array[2] = {state_array[i][0], state_array[i][1]};
Lightvalve 170:42c938a40313 1230 V[i] = Critic_Network_Temp(temp_array);
Lightvalve 173:68c7914679ec 1231 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 1232 hx_c_sum_array[RL_timer][i] = hx_c_sum[i];
Lightvalve 173:68c7914679ec 1233 }
Lightvalve 173:68c7914679ec 1234 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 1235 hxh_c_sum_array[RL_timer][i] = hxh_c_sum[i];
Lightvalve 173:68c7914679ec 1236 }
Lightvalve 173:68c7914679ec 1237 hxhh_c_sum_array[RL_timer] = hxhh_c_sum;
Lightvalve 170:42c938a40313 1238 pi[i] = exp(-(action_array[i]-mean_array[i])*(action_array[i]-mean_array[i])/(2.0f*deviation_array[i]*deviation_array[i]))/(sqrt(2.0f*PI)*deviation_array[i]);
Lightvalve 170:42c938a40313 1239 Actor_Network_Old(temp_array);
Lightvalve 170:42c938a40313 1240 pi_old[i] = exp(-(action_array[i]-mean_old)*(action_array[i]-mean_old)/(2.0f*deviation_old*deviation_old))/(sqrt(2.0f*PI)*deviation_old);
Lightvalve 173:68c7914679ec 1241 r[i] = exp(-0.25f * state_array[i][1] * state_array[i][1]);
Lightvalve 173:68c7914679ec 1242 if(i == batch_size-1) return_G[i] = 0.0f;
Lightvalve 173:68c7914679ec 1243 else return_G[i] = gamma * return_G[i+1] + r[i];
Lightvalve 170:42c938a40313 1244 if(i == batch_size-1) td_target[i] = r[i];
Lightvalve 170:42c938a40313 1245 else td_target[i] = r[i] + gamma * V[i+1];
Lightvalve 170:42c938a40313 1246 delta[i] = td_target[i] - V[i];
Lightvalve 170:42c938a40313 1247 if(i == batch_size-1) advantage[i] = 0.0f;
Lightvalve 170:42c938a40313 1248 else advantage[i] = gamma * lmbda * advantage[i+1] + delta[i];
Lightvalve 170:42c938a40313 1249 ratio[i] = pi[i]/pi_old[i];
Lightvalve 170:42c938a40313 1250 surr1[i] = ratio[i] * advantage[i];
Lightvalve 170:42c938a40313 1251 if (ratio[i] > 1.0f + epsilon) {
Lightvalve 170:42c938a40313 1252 surr2[i] = (1.0f + epsilon)*advantage[i];
Lightvalve 170:42c938a40313 1253 } else if( ratio[i] < 1.0f - epsilon) {
Lightvalve 170:42c938a40313 1254 surr2[i] = (1.0f - epsilon)*advantage[i];
Lightvalve 170:42c938a40313 1255 } else {
Lightvalve 170:42c938a40313 1256 surr2[i] = ratio[i]*advantage[i];
Lightvalve 170:42c938a40313 1257 }
Lightvalve 170:42c938a40313 1258 loss[i] = -min(surr1[i], surr2[i]);
Lightvalve 170:42c938a40313 1259 loss_sum = loss_sum + loss[i];
Lightvalve 170:42c938a40313 1260 }
Lightvalve 170:42c938a40313 1261 reward_sum = 0.0f;
Lightvalve 170:42c938a40313 1262 for (int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 1263 reward_sum = reward_sum + r[i];
Lightvalve 170:42c938a40313 1264 }
Lightvalve 170:42c938a40313 1265 logging5 = reward_sum;
Lightvalve 171:bfc1fd2629d8 1266
Lightvalve 171:bfc1fd2629d8 1267
Lightvalve 170:42c938a40313 1268 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 170:42c938a40313 1269 loss_batch = loss_sum;
Lightvalve 170:42c938a40313 1270 //Update Networks
Lightvalve 170:42c938a40313 1271 update_Critic_Networks(state_array);
Lightvalve 170:42c938a40313 1272 update_Actor_Networks(state_array);
Lightvalve 170:42c938a40313 1273 }
Lightvalve 170:42c938a40313 1274 //virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1275 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1276 Update_Case = 0;
Lightvalve 170:42c938a40313 1277 //logging1 = V[0];
Lightvalve 171:bfc1fd2629d8 1278
Lightvalve 170:42c938a40313 1279 break;
Lightvalve 170:42c938a40313 1280 }
Lightvalve 170:42c938a40313 1281 case 2: {
Lightvalve 170:42c938a40313 1282 //Network apply to next Network
Lightvalve 170:42c938a40313 1283 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1284 Overwirte_Actor_Networks();
Lightvalve 170:42c938a40313 1285 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1286 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1287 Update_Case = 0;
Lightvalve 170:42c938a40313 1288 break;
Lightvalve 170:42c938a40313 1289 }
Lightvalve 169:645207e160ca 1290
Lightvalve 170:42c938a40313 1291 }
GiJeongKim 0:51c43836c1d7 1292 }
jobuuu 1:e04e563be5ce 1293 }
jobuuu 1:e04e563be5ce 1294
Lightvalve 33:91b17819ec30 1295 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1296 {
Lightvalve 14:8e7590227d22 1297
Lightvalve 13:747daba9cf59 1298 int i = 0;
Lightvalve 48:889798ff9329 1299 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1300 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1301 if(REF_JOINT_VEL >= min(JOINT_VEL[i],JOINT_VEL[i+1]) && REF_JOINT_VEL <= max(JOINT_VEL[i],JOINT_VEL[i+1])) {
Lightvalve 14:8e7590227d22 1302 if(i==0) {
Lightvalve 50:3c630b5eba9f 1303 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 1304 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1305 } else {
Lightvalve 57:f4819de54e7a 1306 Ref_Valve_Pos_FF = ((float) 10/(JOINT_VEL[i+1] - JOINT_VEL[i]) * (REF_JOINT_VEL - JOINT_VEL[i])) + (float) VALVE_CENTER;
Lightvalve 38:118df027d851 1307 }
Lightvalve 14:8e7590227d22 1308 } else {
Lightvalve 50:3c630b5eba9f 1309 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 1310 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1311 } else {
Lightvalve 57:f4819de54e7a 1312 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]);
Lightvalve 38:118df027d851 1313 }
Lightvalve 13:747daba9cf59 1314 }
Lightvalve 13:747daba9cf59 1315 break;
Lightvalve 13:747daba9cf59 1316 }
Lightvalve 13:747daba9cf59 1317 }
Lightvalve 14:8e7590227d22 1318 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1319 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1320 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1321 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1322 }
Lightvalve 36:a46e63505ed8 1323
Lightvalve 57:f4819de54e7a 1324 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1325 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 1326
Lightvalve 13:747daba9cf59 1327 }
jobuuu 6:df07d3491e3a 1328
jobuuu 6:df07d3491e3a 1329
Lightvalve 30:8d561f16383b 1330 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1331 {
Lightvalve 13:747daba9cf59 1332 int i = 0;
Lightvalve 13:747daba9cf59 1333
Lightvalve 38:118df027d851 1334 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1335 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1336 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1337 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1338 }
Lightvalve 38:118df027d851 1339
Lightvalve 89:a7b45368ea0f 1340 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1341 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1342 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1343 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 1344 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 1345 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1346
Lightvalve 13:747daba9cf59 1347 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;
Lightvalve 14:8e7590227d22 1348
Lightvalve 18:b8adf1582ea3 1349 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 1350 if(REF_VALVE_POS >= min(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1]) && (float) REF_VALVE_POS <= max(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1])) {
Lightvalve 14:8e7590227d22 1351 if(i==0) {
Lightvalve 48:889798ff9329 1352 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]);
Lightvalve 14:8e7590227d22 1353 } else {
Lightvalve 48:889798ff9329 1354 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];
Lightvalve 13:747daba9cf59 1355 }
Lightvalve 13:747daba9cf59 1356 break;
Lightvalve 13:747daba9cf59 1357 }
Lightvalve 13:747daba9cf59 1358 }
Lightvalve 59:f308b1656d9c 1359 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1360 }
Lightvalve 13:747daba9cf59 1361
Lightvalve 14:8e7590227d22 1362 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1363 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,
Lightvalve 34:bb2ca2fc2a8e 1364 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1365 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1366 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1367 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1368 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1369 }; // duty
Lightvalve 67:c2812cf26c38 1370 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,
Lightvalve 67:c2812cf26c38 1371 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 1372 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 1373 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 1374 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 1375 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1376 }; // mV
Lightvalve 13:747daba9cf59 1377
Lightvalve 30:8d561f16383b 1378 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1379 {
Lightvalve 30:8d561f16383b 1380 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1381 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1382 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1383 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1384 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1385 } else {
Lightvalve 13:747daba9cf59 1386 int idx = 0;
Lightvalve 13:747daba9cf59 1387 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1388 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1389 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1390 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1391 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1392 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1393 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1394 break;
Lightvalve 13:747daba9cf59 1395 }
Lightvalve 13:747daba9cf59 1396 }
Lightvalve 13:747daba9cf59 1397 }
Lightvalve 14:8e7590227d22 1398
Lightvalve 13:747daba9cf59 1399 return PWM_duty;
Lightvalve 13:747daba9cf59 1400 }
jobuuu 6:df07d3491e3a 1401
Lightvalve 57:f4819de54e7a 1402
Lightvalve 57:f4819de54e7a 1403
Lightvalve 57:f4819de54e7a 1404
Lightvalve 57:f4819de54e7a 1405
jobuuu 2:a1c0a37df760 1406 /*******************************************************************************
jobuuu 2:a1c0a37df760 1407 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1408 *******************************************************************************/
jobuuu 2:a1c0a37df760 1409
Lightvalve 51:b46bed7fec80 1410 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 1411 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 1412 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 1413 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1414 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1415 {
Lightvalve 19:23b7c1ad8683 1416 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1417
Lightvalve 21:e5f1a43ea6f9 1418 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1419 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1420 ********************************************************/
Lightvalve 13:747daba9cf59 1421
Lightvalve 57:f4819de54e7a 1422 //Encoder
Lightvalve 57:f4819de54e7a 1423 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 1424 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 1425 }
Lightvalve 61:bc8c8270f0ab 1426
Lightvalve 61:bc8c8270f0ab 1427 ADC1->CR2 |= 0x40000000;
Lightvalve 58:2eade98630e2 1428 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1429 // Torque Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1430 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 58:2eade98630e2 1431 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 1432 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 1433 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 1434
Lightvalve 67:c2812cf26c38 1435
Lightvalve 67:c2812cf26c38 1436 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 67:c2812cf26c38 1437 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 67:c2812cf26c38 1438 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 67:c2812cf26c38 1439 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 67:c2812cf26c38 1440 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 67:c2812cf26c38 1441
Lightvalve 17:1865016ca2e7 1442
Lightvalve 58:2eade98630e2 1443 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1444 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1445 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 58:2eade98630e2 1446 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 58:2eade98630e2 1447 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 58:2eade98630e2 1448 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 1449 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 1450 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 1451 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 1452
Lightvalve 58:2eade98630e2 1453 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 1454 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm
Lightvalve 58:2eade98630e2 1455 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 1456 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N
Lightvalve 58:2eade98630e2 1457 }
Lightvalve 58:2eade98630e2 1458 }
Lightvalve 61:bc8c8270f0ab 1459
Lightvalve 58:2eade98630e2 1460 // //Pressure sensor A
Lightvalve 58:2eade98630e2 1461 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 58:2eade98630e2 1462 // //while((ADC1->SR & 0b10));
Lightvalve 58:2eade98630e2 1463 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1464 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 58:2eade98630e2 1465 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 58:2eade98630e2 1466 // 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.
Lightvalve 58:2eade98630e2 1467 //
Lightvalve 58:2eade98630e2 1468 //
Lightvalve 58:2eade98630e2 1469 // //Pressure sensor B
Lightvalve 58:2eade98630e2 1470 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1471 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 58:2eade98630e2 1472 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 58:2eade98630e2 1473 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1474
Lightvalve 17:1865016ca2e7 1475
Lightvalve 21:e5f1a43ea6f9 1476 //Current
Lightvalve 21:e5f1a43ea6f9 1477 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1478 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1479 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 1480 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1481 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1482 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1483 //cur.sen = raw_cur;
Lightvalve 58:2eade98630e2 1484
Lightvalve 57:f4819de54e7a 1485 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1486 }
Lightvalve 11:82d8768d7351 1487 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1488 }
Lightvalve 19:23b7c1ad8683 1489
Lightvalve 19:23b7c1ad8683 1490
Lightvalve 18:b8adf1582ea3 1491 int j =0;
Lightvalve 54:647072f5307a 1492 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 1493 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 1494 int cnt_trans = 0;
Lightvalve 48:889798ff9329 1495 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 1496 int can_rest =0;
Lightvalve 48:889798ff9329 1497
Lightvalve 11:82d8768d7351 1498 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1499 {
Lightvalve 19:23b7c1ad8683 1500 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 1501
Lightvalve 57:f4819de54e7a 1502 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 1503 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1504 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 1505 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1506 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 1507 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 1508 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1509 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 1510 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1511 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 1512 }
Lightvalve 50:3c630b5eba9f 1513
Lightvalve 50:3c630b5eba9f 1514 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 1515 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1516 cnt_trans++;
Lightvalve 46:2694daea349b 1517 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1518 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 1519 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 1520 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 1521 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1522 cnt_trans++;
Lightvalve 46:2694daea349b 1523 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1524 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 1525 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 1526 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 1527 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 1528 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 1529 } else {
Lightvalve 58:2eade98630e2 1530 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 1531 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 1532 }
Lightvalve 45:35fa6884d0c6 1533
Lightvalve 50:3c630b5eba9f 1534
Lightvalve 57:f4819de54e7a 1535 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 1536 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 1537
Lightvalve 57:f4819de54e7a 1538 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 1539 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1540 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1541 } else {
Lightvalve 57:f4819de54e7a 1542 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1543 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1544 }
Lightvalve 56:6f50d9d3bfee 1545
Lightvalve 56:6f50d9d3bfee 1546
Lightvalve 56:6f50d9d3bfee 1547
Lightvalve 57:f4819de54e7a 1548 // UTILITY MODE ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1549
Lightvalve 57:f4819de54e7a 1550 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 1551 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1552 break;
Lightvalve 13:747daba9cf59 1553 }
Lightvalve 14:8e7590227d22 1554
Lightvalve 14:8e7590227d22 1555 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1556 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1557 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1558 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1559
Lightvalve 14:8e7590227d22 1560 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1561 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1562 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1563
Lightvalve 84:c355d3e52bf1 1564 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1565
Lightvalve 30:8d561f16383b 1566 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 1567 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 50:3c630b5eba9f 1568
Lightvalve 16:903b5a4433b4 1569 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1570 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1571 }
Lightvalve 13:747daba9cf59 1572 } else {
Lightvalve 58:2eade98630e2 1573 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1574 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1575 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1576 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1577
Lightvalve 170:42c938a40313 1578 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1579 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1580
Lightvalve 30:8d561f16383b 1581 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1582
Lightvalve 13:747daba9cf59 1583 }
Lightvalve 14:8e7590227d22 1584 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1585 break;
Lightvalve 19:23b7c1ad8683 1586 }
Lightvalve 14:8e7590227d22 1587
Lightvalve 50:3c630b5eba9f 1588 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 1589 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 1590 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1591 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 1592 // }
Lightvalve 50:3c630b5eba9f 1593 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1594 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1595 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1596 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1597 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1598 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1599 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1600 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1601 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1602 // }
Lightvalve 50:3c630b5eba9f 1603 //
Lightvalve 50:3c630b5eba9f 1604 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1605 // V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen);
Lightvalve 50:3c630b5eba9f 1606 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 1607 //
Lightvalve 50:3c630b5eba9f 1608 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 1609 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1610 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1611 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1612 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1613 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1614 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 1615 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1616 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1617 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1618 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1619 //
Lightvalve 50:3c630b5eba9f 1620 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 1621 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 1622 // // | / | / |/
Lightvalve 50:3c630b5eba9f 1623 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 1624 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 1625 // // /| / | / |
Lightvalve 50:3c630b5eba9f 1626 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 1627 //
Lightvalve 50:3c630b5eba9f 1628 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 1629 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 1630 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1631 // } else {
Lightvalve 50:3c630b5eba9f 1632 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 1633 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1634 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1635 // }
Lightvalve 50:3c630b5eba9f 1636 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 1637 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1638 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1639 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 1640 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1641 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1642 // }
Lightvalve 50:3c630b5eba9f 1643 // } else {
Lightvalve 50:3c630b5eba9f 1644 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 1645 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 1646 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 1647 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1648 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1649 // }
Lightvalve 50:3c630b5eba9f 1650 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1651 //
Lightvalve 50:3c630b5eba9f 1652 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1653 //
Lightvalve 50:3c630b5eba9f 1654 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 1655 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 1656 //
Lightvalve 50:3c630b5eba9f 1657 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1658 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1659 // }
Lightvalve 50:3c630b5eba9f 1660 // }
Lightvalve 50:3c630b5eba9f 1661 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 1662 // break;
Lightvalve 50:3c630b5eba9f 1663 // }
Lightvalve 14:8e7590227d22 1664
Lightvalve 14:8e7590227d22 1665 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1666 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1667 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1668 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1669 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 59:f308b1656d9c 1670 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 1671 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1672 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1673 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 1674 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1675 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1676 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1677 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1678 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1679 }
Lightvalve 29:69f3f5445d6d 1680 cnt_findhome++;
Lightvalve 14:8e7590227d22 1681
Lightvalve 29:69f3f5445d6d 1682 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1683 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1684 } else {
Lightvalve 29:69f3f5445d6d 1685 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1686 }
Lightvalve 19:23b7c1ad8683 1687
Lightvalve 57:f4819de54e7a 1688 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1689 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 67:c2812cf26c38 1690 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 67:c2812cf26c38 1691 else pos.ref = pos.ref - 12.0f;
Lightvalve 61:bc8c8270f0ab 1692
Lightvalve 59:f308b1656d9c 1693 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 59:f308b1656d9c 1694 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1695 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 59:f308b1656d9c 1696 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1697 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 1698
Lightvalve 59:f308b1656d9c 1699 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 1700 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1701
Lightvalve 34:bb2ca2fc2a8e 1702
Lightvalve 29:69f3f5445d6d 1703 } else {
Lightvalve 29:69f3f5445d6d 1704 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 67:c2812cf26c38 1705 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1706 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1707 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1708 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1709 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1710 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1711 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1712 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1713 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1714 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 1715
Lightvalve 67:c2812cf26c38 1716
Lightvalve 67:c2812cf26c38 1717 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 1718 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1719 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1720 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1721 vel.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1722 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 67:c2812cf26c38 1723 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 169:645207e160ca 1724
Lightvalve 169:645207e160ca 1725
Lightvalve 29:69f3f5445d6d 1726 }
Lightvalve 29:69f3f5445d6d 1727 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1728 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 1729 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;
Lightvalve 67:c2812cf26c38 1730 //pos.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1731 vel.ref = 0.0f;
Lightvalve 169:645207e160ca 1732
Lightvalve 29:69f3f5445d6d 1733 // input for position control
Lightvalve 169:645207e160ca 1734
Lightvalve 67:c2812cf26c38 1735 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 1736 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1737
Lightvalve 67:c2812cf26c38 1738 double torq_ref = 0.0f;
Lightvalve 67:c2812cf26c38 1739 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 1740 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 1741 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 67:c2812cf26c38 1742
Lightvalve 67:c2812cf26c38 1743 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 67:c2812cf26c38 1744
Lightvalve 67:c2812cf26c38 1745 double I_REF_POS = 0.0f;
Lightvalve 67:c2812cf26c38 1746 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 67:c2812cf26c38 1747 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 67:c2812cf26c38 1748
Lightvalve 67:c2812cf26c38 1749 double temp_vel_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1750 double temp_vel_torq = 0.0f;
Lightvalve 67:c2812cf26c38 1751 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 67:c2812cf26c38 1752
Lightvalve 69:3995ffeaa786 1753 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 67:c2812cf26c38 1754 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
Lightvalve 67:c2812cf26c38 1755 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 1756 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 67:c2812cf26c38 1757 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
Lightvalve 67:c2812cf26c38 1758 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:c2812cf26c38 1759 }
Lightvalve 67:c2812cf26c38 1760 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))));
Lightvalve 67:c2812cf26c38 1761 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1762
Lightvalve 67:c2812cf26c38 1763 I_REF = I_REF_POS;
Lightvalve 67:c2812cf26c38 1764
Lightvalve 67:c2812cf26c38 1765
Lightvalve 67:c2812cf26c38 1766
Lightvalve 67:c2812cf26c38 1767 } else {
Lightvalve 67:c2812cf26c38 1768 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 67:c2812cf26c38 1769 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));
Lightvalve 67:c2812cf26c38 1770
Lightvalve 67:c2812cf26c38 1771 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 67:c2812cf26c38 1772 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 67:c2812cf26c38 1773 } else {
Lightvalve 67:c2812cf26c38 1774 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 67:c2812cf26c38 1775 }
Lightvalve 67:c2812cf26c38 1776
Lightvalve 67:c2812cf26c38 1777 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 67:c2812cf26c38 1778
Lightvalve 67:c2812cf26c38 1779 V_out = (float) Vout.ref;
Lightvalve 67:c2812cf26c38 1780
Lightvalve 67:c2812cf26c38 1781 }
Lightvalve 67:c2812cf26c38 1782
Lightvalve 67:c2812cf26c38 1783
Lightvalve 169:645207e160ca 1784
Lightvalve 169:645207e160ca 1785
Lightvalve 59:f308b1656d9c 1786 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 59:f308b1656d9c 1787 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1788 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 1789 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1790 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1791
Lightvalve 29:69f3f5445d6d 1792 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1793 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1794 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1795 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1796 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1797 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1798 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1799 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1800 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 1801 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1802 }
Lightvalve 13:747daba9cf59 1803 }
Lightvalve 19:23b7c1ad8683 1804
Lightvalve 13:747daba9cf59 1805 break;
Lightvalve 13:747daba9cf59 1806 }
Lightvalve 14:8e7590227d22 1807
Lightvalve 50:3c630b5eba9f 1808 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1809 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1810 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1811 // else {
Lightvalve 50:3c630b5eba9f 1812 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1813 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1814 // }
Lightvalve 50:3c630b5eba9f 1815 // }
Lightvalve 50:3c630b5eba9f 1816 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1817 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1818 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1819 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1820 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1821 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1822 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1823 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1824 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1825 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1826 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1827 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1828 // }
Lightvalve 50:3c630b5eba9f 1829 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1830 // }
Lightvalve 50:3c630b5eba9f 1831 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1832 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1833 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1834 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1835 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1836 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1837 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1838 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1839 // 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
Lightvalve 50:3c630b5eba9f 1840 // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // PULSE/sec
Lightvalve 50:3c630b5eba9f 1841 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1842 // }
Lightvalve 50:3c630b5eba9f 1843 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1844 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1845 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1846 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1847 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1848 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1849 // 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);
Lightvalve 50:3c630b5eba9f 1850 // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); // PULSE/sec
Lightvalve 50:3c630b5eba9f 1851 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1852 // }
Lightvalve 50:3c630b5eba9f 1853 // }
Lightvalve 50:3c630b5eba9f 1854 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1855 //
Lightvalve 50:3c630b5eba9f 1856 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1857 //
Lightvalve 50:3c630b5eba9f 1858 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1859 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1860 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1861 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1862 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1863 // }
Lightvalve 50:3c630b5eba9f 1864 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1865 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1866 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1867 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1868 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1869 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1870 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1871 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1872 // }
Lightvalve 50:3c630b5eba9f 1873 //
Lightvalve 50:3c630b5eba9f 1874 // }
Lightvalve 50:3c630b5eba9f 1875 // break;
Lightvalve 50:3c630b5eba9f 1876 // }
Lightvalve 50:3c630b5eba9f 1877 //
Lightvalve 50:3c630b5eba9f 1878 // }
Lightvalve 14:8e7590227d22 1879 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1880 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1881 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1882 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1883 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1884
Lightvalve 14:8e7590227d22 1885 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1886 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1887 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1888 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1889 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1890
Lightvalve 38:118df027d851 1891 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 1892 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 1893 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1894
Lightvalve 30:8d561f16383b 1895 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1896 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1897 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1898 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1899
Lightvalve 30:8d561f16383b 1900 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1901 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1902 }
Lightvalve 13:747daba9cf59 1903 } else {
Lightvalve 57:f4819de54e7a 1904 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1905 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1906 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1907 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1908 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1909 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1910
Lightvalve 170:42c938a40313 1911 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1912 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 1913 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1914
Lightvalve 30:8d561f16383b 1915 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1916 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1917 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1918 }
Lightvalve 14:8e7590227d22 1919 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1920 break;
Lightvalve 13:747daba9cf59 1921 }
Lightvalve 14:8e7590227d22 1922
Lightvalve 50:3c630b5eba9f 1923 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1924 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1925 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1926 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1927 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1928 // }
Lightvalve 50:3c630b5eba9f 1929 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1930 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1931 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1932 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1933 // }
Lightvalve 50:3c630b5eba9f 1934 // } else {
Lightvalve 50:3c630b5eba9f 1935 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1936 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1937 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1938 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1939 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1940 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1941 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1942 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1943 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1944 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1945 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1946 //
Lightvalve 50:3c630b5eba9f 1947 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1948 //
Lightvalve 50:3c630b5eba9f 1949 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1950 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1951 // }
Lightvalve 50:3c630b5eba9f 1952 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1953 // break;
Lightvalve 50:3c630b5eba9f 1954 // }
Lightvalve 19:23b7c1ad8683 1955
Lightvalve 50:3c630b5eba9f 1956 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1957 // 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);
Lightvalve 50:3c630b5eba9f 1958 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1959 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1960 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1961 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1962 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1963 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 1964 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 1965 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1966 // }
Lightvalve 50:3c630b5eba9f 1967 // break;
Lightvalve 50:3c630b5eba9f 1968 // }
Lightvalve 14:8e7590227d22 1969
Lightvalve 14:8e7590227d22 1970 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 59:f308b1656d9c 1971 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1972 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1973
Lightvalve 14:8e7590227d22 1974 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 60:64181f1d3e60 1975 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1976 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 59:f308b1656d9c 1977 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1978 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1979 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1980 data_num = 0;
Lightvalve 14:8e7590227d22 1981 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1982 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1983 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1984 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 59:f308b1656d9c 1985 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1986 } else {
Lightvalve 13:747daba9cf59 1987 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1988 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1989 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1990 }
Lightvalve 14:8e7590227d22 1991
Lightvalve 17:1865016ca2e7 1992 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1993 int i;
Lightvalve 13:747daba9cf59 1994 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1995 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1996 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1997 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1998 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1999 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 2000 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 2001 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 2002 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 2003 }
Lightvalve 13:747daba9cf59 2004 }
Lightvalve 170:42c938a40313 2005 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2006 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2007 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 170:42c938a40313 2008 for(int i=0; i<25; i++) {
Lightvalve 170:42c938a40313 2009 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 170:42c938a40313 2010 }
Lightvalve 13:747daba9cf59 2011 ID_index = 0;
Lightvalve 57:f4819de54e7a 2012 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2013 }
Lightvalve 14:8e7590227d22 2014
Lightvalve 14:8e7590227d22 2015
Lightvalve 13:747daba9cf59 2016 break;
Lightvalve 13:747daba9cf59 2017 }
Lightvalve 14:8e7590227d22 2018
Lightvalve 14:8e7590227d22 2019 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 59:f308b1656d9c 2020 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2021 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 2022 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2023 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2024 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2025 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2026 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2027 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 2028 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2029 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2030 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2031 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2032 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 2033 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2034 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;
Lightvalve 30:8d561f16383b 2035 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2036 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;
Lightvalve 13:747daba9cf59 2037 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 2038 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 2039 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2040 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;
Lightvalve 13:747daba9cf59 2041 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 2042 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 2043 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 2044 data_num = 0;
Lightvalve 14:8e7590227d22 2045
Lightvalve 30:8d561f16383b 2046 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2047 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2048 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2049
Lightvalve 30:8d561f16383b 2050 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2051 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2052 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2053
Lightvalve 30:8d561f16383b 2054 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2055 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2056 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2057 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2058
Lightvalve 60:64181f1d3e60 2059 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 2060 DZ_case = 1;
Lightvalve 60:64181f1d3e60 2061 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 2062 DZ_case = -1;
Lightvalve 14:8e7590227d22 2063 } else {
Lightvalve 13:747daba9cf59 2064 DZ_case = 0;
Lightvalve 13:747daba9cf59 2065 }
Lightvalve 61:bc8c8270f0ab 2066
Lightvalve 60:64181f1d3e60 2067 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 2068
Lightvalve 13:747daba9cf59 2069 first_check = 1;
Lightvalve 13:747daba9cf59 2070 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2071 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2072 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 2073 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 2074 DZ_index = 1;
Lightvalve 14:8e7590227d22 2075
Lightvalve 13:747daba9cf59 2076 }
Lightvalve 19:23b7c1ad8683 2077 } else {
Lightvalve 14:8e7590227d22 2078 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 2079 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2080 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;
Lightvalve 60:64181f1d3e60 2081 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2082 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2083 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2084 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2085 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2086 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2087 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2088 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2089 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2090 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2091 }
Lightvalve 14:8e7590227d22 2092 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2093
Lightvalve 30:8d561f16383b 2094 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2095 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2096 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2097
Lightvalve 14:8e7590227d22 2098 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2099 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2100 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2101 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2102 } else {
Lightvalve 13:747daba9cf59 2103 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 2104 }
Lightvalve 14:8e7590227d22 2105
Lightvalve 13:747daba9cf59 2106 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2107 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2108 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2109 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2110 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2111 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2112 DZ_index = 1;
Lightvalve 13:747daba9cf59 2113 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2114 }
Lightvalve 13:747daba9cf59 2115 }
Lightvalve 14:8e7590227d22 2116 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 2117 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2118 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;
Lightvalve 60:64181f1d3e60 2119 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2120 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2121 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2122 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2123 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2124 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2125 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2126 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2127 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2128 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2129 }
Lightvalve 14:8e7590227d22 2130 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2131
Lightvalve 30:8d561f16383b 2132 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2133 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2134 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2135 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2136 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2137
Lightvalve 14:8e7590227d22 2138 if((FINAL_POS - START_POS)>100) {
Lightvalve 60:64181f1d3e60 2139 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2140 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2141 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2142 } else {
Lightvalve 60:64181f1d3e60 2143 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2144 }
Lightvalve 14:8e7590227d22 2145
Lightvalve 13:747daba9cf59 2146 VALVE_DZ_timer = 0;
Lightvalve 60:64181f1d3e60 2147 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2148 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2149 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2150 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2151 first_check = 0;
Lightvalve 33:91b17819ec30 2152 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2153 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2154
Lightvalve 170:42c938a40313 2155 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2156 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2157 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2158 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2159
Lightvalve 60:64181f1d3e60 2160 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2161 DZ_index = 1;
Lightvalve 13:747daba9cf59 2162 }
Lightvalve 13:747daba9cf59 2163 }
Lightvalve 14:8e7590227d22 2164 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2165 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2166 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;
Lightvalve 60:64181f1d3e60 2167 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2168 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2169 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2170 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2171 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2172 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2173 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2174 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2175 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2176 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2177 }
Lightvalve 14:8e7590227d22 2178 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2179
Lightvalve 30:8d561f16383b 2180 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2181 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2182 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2183
Lightvalve 14:8e7590227d22 2184 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2185 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2186 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2187 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2188 } else {
Lightvalve 13:747daba9cf59 2189 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2190 }
Lightvalve 13:747daba9cf59 2191 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2192 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2193 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2194 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2195 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2196 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2197 DZ_index = 1;
Lightvalve 13:747daba9cf59 2198 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2199 }
Lightvalve 13:747daba9cf59 2200 }
Lightvalve 14:8e7590227d22 2201 } else {
Lightvalve 30:8d561f16383b 2202 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2203 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;
Lightvalve 60:64181f1d3e60 2204 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2205 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2206 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2207 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2208 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2209 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2210 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2211 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2212 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2213 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2214 }
Lightvalve 14:8e7590227d22 2215 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2216
Lightvalve 30:8d561f16383b 2217 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2218 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2219 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2220 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2221 FINAL_POS = pos.sen;
Lightvalve 61:bc8c8270f0ab 2222
Lightvalve 60:64181f1d3e60 2223 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2224 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2225 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2226 DZ_DIRECTION = -1;
Lightvalve 60:64181f1d3e60 2227 } else {
Lightvalve 13:747daba9cf59 2228 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2229 }
Lightvalve 14:8e7590227d22 2230
Lightvalve 13:747daba9cf59 2231 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2232 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2233 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2234 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2235 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2236 first_check = 0;
Lightvalve 33:91b17819ec30 2237 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2238 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 61:bc8c8270f0ab 2239
Lightvalve 170:42c938a40313 2240 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2241 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2242 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2243 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2244
Lightvalve 57:f4819de54e7a 2245 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2246 DZ_index = 1;
Lightvalve 13:747daba9cf59 2247 }
Lightvalve 13:747daba9cf59 2248 }
Lightvalve 13:747daba9cf59 2249 }
Lightvalve 14:8e7590227d22 2250 }
Lightvalve 13:747daba9cf59 2251 break;
Lightvalve 13:747daba9cf59 2252 }
Lightvalve 14:8e7590227d22 2253
Lightvalve 14:8e7590227d22 2254 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 59:f308b1656d9c 2255 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2256 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2257 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2258 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2259 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2260 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2261 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2262 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2263 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2264 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2265 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2266 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2267 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2268 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 59:f308b1656d9c 2269 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2270 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2271 first_check = 1;
Lightvalve 13:747daba9cf59 2272 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2273 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2274 ID_index = 0;
Lightvalve 13:747daba9cf59 2275 max_check = 0;
Lightvalve 13:747daba9cf59 2276 min_check = 0;
Lightvalve 13:747daba9cf59 2277 }
Lightvalve 14:8e7590227d22 2278 } else {
Lightvalve 30:8d561f16383b 2279 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2280 //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;
Lightvalve 59:f308b1656d9c 2281 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 59:f308b1656d9c 2282 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2283 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2284 data_num = 0;
Lightvalve 57:f4819de54e7a 2285 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2286
Lightvalve 14:8e7590227d22 2287 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2288 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2289 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f4819de54e7a 2290 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2291 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2292 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2293 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2294 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2295 one_period_end = 1;
Lightvalve 13:747daba9cf59 2296 }
Lightvalve 30:8d561f16383b 2297 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2298 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2299 one_period_end = 1;
Lightvalve 59:f308b1656d9c 2300 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2301 }
Lightvalve 14:8e7590227d22 2302
Lightvalve 14:8e7590227d22 2303 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2304 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2305 max_check = 1;
Lightvalve 14:8e7590227d22 2306 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2307 min_check = 1;
Lightvalve 13:747daba9cf59 2308 }
Lightvalve 13:747daba9cf59 2309 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2310
Lightvalve 13:747daba9cf59 2311 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2312 one_period_end = 0;
Lightvalve 13:747daba9cf59 2313 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2314 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2315 }
Lightvalve 14:8e7590227d22 2316
Lightvalve 14:8e7590227d22 2317 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2318
Lightvalve 13:747daba9cf59 2319 VALVE_POS_NUM = ID_index;
Lightvalve 170:42c938a40313 2320 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2321 for(int i=0; i<100; i++) {
Lightvalve 170:42c938a40313 2322 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 170:42c938a40313 2323 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 170:42c938a40313 2324 }
Lightvalve 13:747daba9cf59 2325 ID_index = 0;
Lightvalve 13:747daba9cf59 2326 first_check = 0;
Lightvalve 13:747daba9cf59 2327 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2328 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 2329 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2330 }
Lightvalve 13:747daba9cf59 2331 }
Lightvalve 13:747daba9cf59 2332 break;
Lightvalve 13:747daba9cf59 2333 }
Lightvalve 58:2eade98630e2 2334
Lightvalve 57:f4819de54e7a 2335 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 2336 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 2337 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 2338 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 2339 cnt_sysid++;
Lightvalve 57:f4819de54e7a 2340 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 2341 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 2342 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 2343 }
Lightvalve 57:f4819de54e7a 2344 break;
Lightvalve 57:f4819de54e7a 2345 }
Lightvalve 58:2eade98630e2 2346
Lightvalve 169:645207e160ca 2347 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 2348 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 2349 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2350 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2351 } else {
Lightvalve 169:645207e160ca 2352 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2353 }
Lightvalve 169:645207e160ca 2354 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2355 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2356 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2357 } else {
Lightvalve 169:645207e160ca 2358 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2359 }
Lightvalve 58:2eade98630e2 2360
Lightvalve 169:645207e160ca 2361 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2362 cnt_freq_test++;
Lightvalve 169:645207e160ca 2363 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 2364 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 2365 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 2366 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2367 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 2368 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 2369 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2370 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2371 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2372 }
Lightvalve 169:645207e160ca 2373 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2374 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 2375
Lightvalve 169:645207e160ca 2376 }
Lightvalve 169:645207e160ca 2377 break;
Lightvalve 169:645207e160ca 2378 }
Lightvalve 169:645207e160ca 2379 case MODE_SEND_BUFFER: {
Lightvalve 169:645207e160ca 2380 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 169:645207e160ca 2381 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 169:645207e160ca 2382 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 169:645207e160ca 2383 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 169:645207e160ca 2384 // }
Lightvalve 169:645207e160ca 2385 // cnt_send_buffer++;
Lightvalve 169:645207e160ca 2386 // }
Lightvalve 169:645207e160ca 2387
Lightvalve 169:645207e160ca 2388 break;
Lightvalve 169:645207e160ca 2389 }
Lightvalve 169:645207e160ca 2390 case MODE_SEND_OVER: {
Lightvalve 169:645207e160ca 2391 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 169:645207e160ca 2392 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2393 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2394 break;
Lightvalve 169:645207e160ca 2395 }
Lightvalve 170:42c938a40313 2396
Lightvalve 169:645207e160ca 2397 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 2398 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2399 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2400 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2401 } else {
Lightvalve 169:645207e160ca 2402 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 2403 }
Lightvalve 169:645207e160ca 2404 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2405 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2406 } else {
Lightvalve 169:645207e160ca 2407 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2408 }
Lightvalve 169:645207e160ca 2409 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2410 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2411 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2412 } else {
Lightvalve 169:645207e160ca 2413 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2414 }
Lightvalve 169:645207e160ca 2415
Lightvalve 169:645207e160ca 2416 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2417 cnt_step_test++;
Lightvalve 170:42c938a40313 2418 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2419 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 2420 cnt_step_test = 0;
Lightvalve 169:645207e160ca 2421 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2422 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 2423 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2424 }
Lightvalve 169:645207e160ca 2425 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 169:645207e160ca 2426 // {
Lightvalve 169:645207e160ca 2427 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2428 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2429 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2430 // }
Lightvalve 170:42c938a40313 2431
Lightvalve 169:645207e160ca 2432 break;
Lightvalve 169:645207e160ca 2433 }
Lightvalve 57:f4819de54e7a 2434
Lightvalve 57:f4819de54e7a 2435 default:
Lightvalve 57:f4819de54e7a 2436 break;
Lightvalve 57:f4819de54e7a 2437 }
Lightvalve 57:f4819de54e7a 2438
Lightvalve 57:f4819de54e7a 2439 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 2440
Lightvalve 57:f4819de54e7a 2441 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 2442 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 2443 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 2444 break;
Lightvalve 57:f4819de54e7a 2445 }
Lightvalve 57:f4819de54e7a 2446
Lightvalve 57:f4819de54e7a 2447 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 2448 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 2449 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 2450 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 2451 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 2452 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 2453 } else {
Lightvalve 67:c2812cf26c38 2454 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 2455 }
Lightvalve 57:f4819de54e7a 2456 break;
Lightvalve 57:f4819de54e7a 2457 }
Lightvalve 57:f4819de54e7a 2458
Lightvalve 57:f4819de54e7a 2459 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 2460
Lightvalve 57:f4819de54e7a 2461 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 2462 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 2463 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 2464 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2465
Lightvalve 67:c2812cf26c38 2466 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 2467 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 2468 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 2469 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 2470
Lightvalve 139:15621998925b 2471 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 139:15621998925b 2472 torq_ref = torq.ref;
Lightvalve 57:f4819de54e7a 2473
Lightvalve 57:f4819de54e7a 2474 // torque feedback
Lightvalve 67:c2812cf26c38 2475 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 2476 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 2477
Lightvalve 57:f4819de54e7a 2478 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 2479
Lightvalve 57:f4819de54e7a 2480 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 2481 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 2482 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 2483
Lightvalve 57:f4819de54e7a 2484 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 2485 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 2486 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 2487
Lightvalve 69:3995ffeaa786 2488 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2489 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
Lightvalve 57:f4819de54e7a 2490 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 2491 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2492 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
Lightvalve 57:f4819de54e7a 2493 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 2494 }
Lightvalve 57:f4819de54e7a 2495 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))));
Lightvalve 57:f4819de54e7a 2496 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2497
Lightvalve 57:f4819de54e7a 2498 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 2499 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2500 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 2501 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s
Lightvalve 57:f4819de54e7a 2502 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2503 // L feedforward velocity
Lightvalve 69:3995ffeaa786 2504 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2505 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.
Lightvalve 57:f4819de54e7a 2506 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2507 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2508 // L feedforward velocity
Lightvalve 57:f4819de54e7a 2509 }
Lightvalve 57:f4819de54e7a 2510 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))));
Lightvalve 57:f4819de54e7a 2511 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2512 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 2513 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 2514 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 2515
Lightvalve 57:f4819de54e7a 2516 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 2517
Lightvalve 67:c2812cf26c38 2518 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 2519 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 2520 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 2521 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 2522 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 2523 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 2524 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2525 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 2526 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2527 } else if (I_REF < -I_MAX) {
Lightvalve 67:c2812cf26c38 2528 double I_rem = I_REF - (-I_MAX);
Lightvalve 67:c2812cf26c38 2529 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2530 I_REF = -I_MAX;
Lightvalve 67:c2812cf26c38 2531 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2532 }
Lightvalve 67:c2812cf26c38 2533 }
Lightvalve 57:f4819de54e7a 2534
Lightvalve 57:f4819de54e7a 2535 } else {
Lightvalve 57:f4819de54e7a 2536 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 2537 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 2538 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 2539
Lightvalve 57:f4819de54e7a 2540 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) * 0.01f
Lightvalve 57:f4819de54e7a 2541 + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 57:f4819de54e7a 2542
Lightvalve 72:3436ce769b1e 2543 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;
Lightvalve 169:645207e160ca 2544
Lightvalve 72:3436ce769b1e 2545 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 2546
Lightvalve 169:645207e160ca 2547
Lightvalve 72:3436ce769b1e 2548 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 2549 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 2550 } else {
Lightvalve 72:3436ce769b1e 2551 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 2552 }
Lightvalve 57:f4819de54e7a 2553
Lightvalve 57:f4819de54e7a 2554 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 2555 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 2556 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 2557 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2558 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2559 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2560 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2561 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 2562 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2563 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2564 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2565 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2566 }
Lightvalve 57:f4819de54e7a 2567 }
Lightvalve 61:bc8c8270f0ab 2568
Lightvalve 57:f4819de54e7a 2569 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2570
Lightvalve 67:c2812cf26c38 2571 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 60:64181f1d3e60 2572 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2573
Lightvalve 57:f4819de54e7a 2574 }
Lightvalve 169:645207e160ca 2575
Lightvalve 72:3436ce769b1e 2576 torq_ref_past = torq_ref;
Lightvalve 58:2eade98630e2 2577
Lightvalve 133:22ab22818e01 2578
Lightvalve 57:f4819de54e7a 2579 break;
Lightvalve 57:f4819de54e7a 2580 }
Lightvalve 58:2eade98630e2 2581
Lightvalve 57:f4819de54e7a 2582 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 2583 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2584 break;
Lightvalve 57:f4819de54e7a 2585 }
Lightvalve 169:645207e160ca 2586
Lightvalve 138:a843f32ced33 2587 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 2588
Lightvalve 169:645207e160ca 2589
Lightvalve 139:15621998925b 2590 float Va = (1256.6f + Amm * pos.sen/(float)(ENC_PULSE_PER_POSITION)) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 139:15621998925b 2591 float Vb = (1256.6f + Amm * (79.0f - pos.sen/(float)(ENC_PULSE_PER_POSITION))) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 169:645207e160ca 2592
Lightvalve 139:15621998925b 2593 // float Va = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 139:15621998925b 2594 // float Vb = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 170:42c938a40313 2595 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 2596
Lightvalve 169:645207e160ca 2597
Lightvalve 170:42c938a40313 2598 float f3 = -Amm*Amm*beta*0.000001f*0.000001f/V_adapt * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076
Lightvalve 169:645207e160ca 2599
Lightvalve 138:a843f32ced33 2600 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 2601 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2602 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 2603 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2604 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 2605 } else {
Lightvalve 138:a843f32ced33 2606 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 2607 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 2608 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2609 } else {
Lightvalve 138:a843f32ced33 2610 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 2611 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2612 }
Lightvalve 138:a843f32ced33 2613 }
Lightvalve 138:a843f32ced33 2614 float tau = 0.01f;
Lightvalve 142:43026242815a 2615 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 2616
Lightvalve 138:a843f32ced33 2617 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 2618 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 2619 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2620 } else {
Lightvalve 138:a843f32ced33 2621 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2622 }
Lightvalve 138:a843f32ced33 2623 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 2624 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 2625
Lightvalve 139:15621998925b 2626 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 2627
Lightvalve 138:a843f32ced33 2628 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 2629 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 2630 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2631
Lightvalve 138:a843f32ced33 2632 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 2633 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 2634
Lightvalve 142:43026242815a 2635 float k3 = 20000.0f; //2000
Lightvalve 142:43026242815a 2636 float k4 = 10.0f;
Lightvalve 142:43026242815a 2637 float rho3 = 3.2f;
Lightvalve 142:43026242815a 2638 float rho4 = 25000000.0f;
Lightvalve 139:15621998925b 2639 float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 2640 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 2641 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 2642
Lightvalve 139:15621998925b 2643 if (x_4_des > 0) {
Lightvalve 139:15621998925b 2644 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2645 } else {
Lightvalve 139:15621998925b 2646 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2647 }
Lightvalve 169:645207e160ca 2648
Lightvalve 169:645207e160ca 2649
Lightvalve 138:a843f32ced33 2650 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 2651 x_4_des_old = x_4_des;
Lightvalve 169:645207e160ca 2652
Lightvalve 139:15621998925b 2653 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 169:645207e160ca 2654
Lightvalve 142:43026242815a 2655 float rho_gamma = 50000.0f;//5000
Lightvalve 139:15621998925b 2656 float gamma_hat_dot = rho3*(-torq.err)/rho_gamma*((-f3+torq_ref_dot-k3*(-torq.err))/gamma_hat + g3_prime*(x_v-x_4_des));
Lightvalve 138:a843f32ced33 2657 gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 169:645207e160ca 2658 break;
Lightvalve 138:a843f32ced33 2659 }
Lightvalve 171:bfc1fd2629d8 2660
Lightvalve 170:42c938a40313 2661 case MODE_RL: {
Lightvalve 170:42c938a40313 2662 //t.reset();
Lightvalve 170:42c938a40313 2663 //t.start();
Lightvalve 170:42c938a40313 2664
Lightvalve 170:42c938a40313 2665 // if(LED == 0) LED = 1;
Lightvalve 170:42c938a40313 2666 // else LED = 0;
Lightvalve 170:42c938a40313 2667
Lightvalve 170:42c938a40313 2668 if (Update_Done_Flag == 1) {
Lightvalve 170:42c938a40313 2669 //Gather Data on each loop
Lightvalve 170:42c938a40313 2670 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 170:42c938a40313 2671 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2672 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2673 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2674 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2675 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2676 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2677 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 170:42c938a40313 2678 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 170:42c938a40313 2679 Actor_Network(temp_array);
Lightvalve 173:68c7914679ec 2680 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 2681 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 173:68c7914679ec 2682 }
Lightvalve 173:68c7914679ec 2683 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 2684 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 173:68c7914679ec 2685 }
Lightvalve 173:68c7914679ec 2686 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 173:68c7914679ec 2687 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 170:42c938a40313 2688 mean_array[RL_timer] = mean;
Lightvalve 170:42c938a40313 2689 deviation_array[RL_timer] = deviation;
Lightvalve 170:42c938a40313 2690 mean_before_SP_array[RL_timer] = mean_before_SP;
Lightvalve 170:42c938a40313 2691 deviation_before_SP_array[RL_timer] = deviation_before_SP;
Lightvalve 170:42c938a40313 2692 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 170:42c938a40313 2693
Lightvalve 170:42c938a40313 2694
Lightvalve 170:42c938a40313 2695 virt_pos = virt_pos + (action_array[RL_timer] - 3.0f) * 1000.0f * 0.0002f;
Lightvalve 170:42c938a40313 2696 if (virt_pos > 70.0f ) {
Lightvalve 170:42c938a40313 2697 virt_pos = 70.0f;
Lightvalve 170:42c938a40313 2698 } else if(virt_pos < -70.0f) {
Lightvalve 170:42c938a40313 2699 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2700 }
Lightvalve 171:bfc1fd2629d8 2701
Lightvalve 170:42c938a40313 2702 RL_timer++;
Lightvalve 170:42c938a40313 2703
Lightvalve 170:42c938a40313 2704
Lightvalve 170:42c938a40313 2705 if (RL_timer >= batch_size) {
Lightvalve 170:42c938a40313 2706 RL_timer = 0;
Lightvalve 170:42c938a40313 2707 batch++;
Lightvalve 170:42c938a40313 2708 for(int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 2709 state_array[i][0] = train_set_x[i];
Lightvalve 170:42c938a40313 2710 state_array[i][1] = train_set_error[i];
Lightvalve 170:42c938a40313 2711 //state_array[i][2] = train_set_count[i];
Lightvalve 170:42c938a40313 2712 }
Lightvalve 170:42c938a40313 2713 Update_Case = 1;
Lightvalve 170:42c938a40313 2714 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2715 logging1 = virt_pos;
Lightvalve 170:42c938a40313 2716 //virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2717
Lightvalve 170:42c938a40313 2718 if(batch >= num_batch) {
Lightvalve 170:42c938a40313 2719 batch = 0;
Lightvalve 170:42c938a40313 2720 RL_timer = 0;
Lightvalve 170:42c938a40313 2721 Update_Case = 2;
Lightvalve 170:42c938a40313 2722 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2723 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2724 }
Lightvalve 170:42c938a40313 2725 }
Lightvalve 170:42c938a40313 2726 }
Lightvalve 170:42c938a40313 2727
Lightvalve 170:42c938a40313 2728 else {
Lightvalve 170:42c938a40313 2729
Lightvalve 170:42c938a40313 2730 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2731 float temp_array[3] = {0.0f};
Lightvalve 170:42c938a40313 2732 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2733 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2734 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2735 Actor_Network(temp_array);
Lightvalve 170:42c938a40313 2736 action = rand_normal(mean, deviation);
Lightvalve 170:42c938a40313 2737 //logging1 = action;
Lightvalve 170:42c938a40313 2738 logging2 = mean;
Lightvalve 170:42c938a40313 2739 logging4 = deviation;
Lightvalve 170:42c938a40313 2740 virt_pos = virt_pos + (action-3.0f) * 1000.0f * 0.0002f;
Lightvalve 170:42c938a40313 2741 if (virt_pos > 70.0f) {
Lightvalve 170:42c938a40313 2742 virt_pos = 70.0f;
Lightvalve 170:42c938a40313 2743 } else if(virt_pos < -70.0f) {
Lightvalve 170:42c938a40313 2744 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2745 }
Lightvalve 170:42c938a40313 2746
Lightvalve 170:42c938a40313 2747 logging3 = virt_pos;
Lightvalve 170:42c938a40313 2748 }
Lightvalve 170:42c938a40313 2749
Lightvalve 170:42c938a40313 2750 //t.stop();
Lightvalve 170:42c938a40313 2751 //logging1 = t.read()*1000.0f; //msec
Lightvalve 170:42c938a40313 2752
Lightvalve 170:42c938a40313 2753 break;
Lightvalve 170:42c938a40313 2754 }
Lightvalve 14:8e7590227d22 2755
Lightvalve 12:6f2531038ea4 2756 default:
Lightvalve 12:6f2531038ea4 2757 break;
Lightvalve 12:6f2531038ea4 2758 }
Lightvalve 14:8e7590227d22 2759
Lightvalve 57:f4819de54e7a 2760
Lightvalve 57:f4819de54e7a 2761 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 2762
Lightvalve 57:f4819de54e7a 2763 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2764 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 2765 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2766 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 2767 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 2768 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 2769
Lightvalve 57:f4819de54e7a 2770 I_ERR = I_REF_fil - cur.sen;
Lightvalve 67:c2812cf26c38 2771 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 2772
Lightvalve 57:f4819de54e7a 2773
Lightvalve 57:f4819de54e7a 2774 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 2775 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 2776 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 2777 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 2778 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 2779 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 2780
Lightvalve 57:f4819de54e7a 2781 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 2782 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 2783 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 2784 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 2785 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 2786 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 2787 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 2788 }
Lightvalve 57:f4819de54e7a 2789
Lightvalve 57:f4819de54e7a 2790 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 2791
Lightvalve 67:c2812cf26c38 2792 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 57:f4819de54e7a 2793 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 57:f4819de54e7a 2794 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 57:f4819de54e7a 2795 I_REF_fil_old = I_REF_fil;
Lightvalve 67:c2812cf26c38 2796 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV
Lightvalve 67:c2812cf26c38 2797 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 67:c2812cf26c38 2798 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 2799
Lightvalve 57:f4819de54e7a 2800 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 2801 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 2802 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 2803 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2804 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 2805 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2806 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 2807 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 2808 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2809 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 2810 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2811 }
Lightvalve 57:f4819de54e7a 2812 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2813 } else {
Lightvalve 57:f4819de54e7a 2814 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 2815 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2816 }
Lightvalve 57:f4819de54e7a 2817
Lightvalve 57:f4819de54e7a 2818 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2819 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 2820 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2821 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 57:f4819de54e7a 2822 if (FLAG_VALVE_DEADZONE) {
Lightvalve 57:f4819de54e7a 2823 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2824 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2825
Lightvalve 57:f4819de54e7a 2826 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2827
Lightvalve 57:f4819de54e7a 2828 } else {
Lightvalve 57:f4819de54e7a 2829 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 57:f4819de54e7a 2830 }
Lightvalve 57:f4819de54e7a 2831
Lightvalve 57:f4819de54e7a 2832 // Output Voltage Linearization
Lightvalve 57:f4819de54e7a 2833 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 67:c2812cf26c38 2834 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 2835
Lightvalve 57:f4819de54e7a 2836 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 2837 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 2838 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 2839 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 2840 } else { //////////////////////////sw valve
Lightvalve 89:a7b45368ea0f 2841 // Output Voltage Linearization
Lightvalve 135:79885a39c161 2842 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 135:79885a39c161 2843 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 89:a7b45368ea0f 2844
Lightvalve 89:a7b45368ea0f 2845 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 135:79885a39c161 2846 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 135:79885a39c161 2847 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 135:79885a39c161 2848 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 135:79885a39c161 2849
Lightvalve 135:79885a39c161 2850 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 2851 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 2852 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 2853 }
Lightvalve 169:645207e160ca 2854
Lightvalve 67:c2812cf26c38 2855 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 67:c2812cf26c38 2856 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 67:c2812cf26c38 2857 // else V_out = V_out;
Lightvalve 169:645207e160ca 2858
jobuuu 7:e9086c72bb22 2859 /*******************************************************
jobuuu 7:e9086c72bb22 2860 *** PWM
jobuuu 7:e9086c72bb22 2861 ********************************************************/
Lightvalve 169:645207e160ca 2862 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 2863 V_out = -V_out;
Lightvalve 67:c2812cf26c38 2864 }
Lightvalve 169:645207e160ca 2865
Lightvalve 49:e7bcfc244d40 2866 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2867 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 2868 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2869 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2870 }
Lightvalve 49:e7bcfc244d40 2871 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2872
Lightvalve 19:23b7c1ad8683 2873 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2874 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2875 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2876
Lightvalve 30:8d561f16383b 2877 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2878 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2879 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2880 } else {
jobuuu 2:a1c0a37df760 2881 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2882 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2883 }
Lightvalve 13:747daba9cf59 2884
jobuuu 1:e04e563be5ce 2885 //pwm
Lightvalve 30:8d561f16383b 2886 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2887 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 2888
Lightvalve 61:bc8c8270f0ab 2889
Lightvalve 57:f4819de54e7a 2890 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2891
Lightvalve 54:647072f5307a 2892 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 2893 if (flag_data_request[0] == HIGH) {
Lightvalve 57:f4819de54e7a 2894 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 2895 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2896 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 2897 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 2898 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));
Lightvalve 54:647072f5307a 2899 }
Lightvalve 57:f4819de54e7a 2900 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 2901 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2902 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)));
Lightvalve 54:647072f5307a 2903 } else if (SENSING_MODE == 1) {
Lightvalve 67:c2812cf26c38 2904 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));
Lightvalve 54:647072f5307a 2905 }
Lightvalve 52:8ea76864368a 2906 }
Lightvalve 52:8ea76864368a 2907 }
Lightvalve 56:6f50d9d3bfee 2908 if (flag_data_request[1] == HIGH) {
Lightvalve 54:647072f5307a 2909 //valve position
Lightvalve 169:645207e160ca 2910 double t_value = 0.0f;
Lightvalve 97:d71c57e3515e 2911 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 97:d71c57e3515e 2912 t_value = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 97:d71c57e3515e 2913 } else {
Lightvalve 97:d71c57e3515e 2914 t_value = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 97:d71c57e3515e 2915 }
Lightvalve 67:c2812cf26c38 2916 CAN_TX_TORQUE((int16_t) (t_value)); //1300
Lightvalve 54:647072f5307a 2917 }
Lightvalve 58:2eade98630e2 2918
Lightvalve 58:2eade98630e2 2919
Lightvalve 171:bfc1fd2629d8 2920 if (flag_data_request[2] == HIGH) {
Lightvalve 169:645207e160ca 2921 double t_value = 0.0f;
Lightvalve 171:bfc1fd2629d8 2922 if(value>=(float) VALVE_CENTER) {
Lightvalve 131:d08121ac87ba 2923 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2924 } else {
Lightvalve 131:d08121ac87ba 2925 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2926 }
Lightvalve 169:645207e160ca 2927 double t_value_ref = 0.0f;
Lightvalve 169:645207e160ca 2928 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2929 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2930 } else {
Lightvalve 169:645207e160ca 2931 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2932 }
Lightvalve 169:645207e160ca 2933
Lightvalve 169:645207e160ca 2934
Lightvalve 169:645207e160ca 2935 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 55:b25725257569 2936 }
Lightvalve 58:2eade98630e2 2937
Lightvalve 57:f4819de54e7a 2938 //If it doesn't rest, below can can not work.
Lightvalve 57:f4819de54e7a 2939 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 57:f4819de54e7a 2940 ;
Lightvalve 57:f4819de54e7a 2941 }
Lightvalve 58:2eade98630e2 2942
Lightvalve 169:645207e160ca 2943 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 2944 //PWM
Lightvalve 73:f80dc3970c99 2945 //CAN_TX_PWM((int16_t) value); //1500
Lightvalve 139:15621998925b 2946 CAN_TX_PWM((int16_t) gamma_hat); //1500
Lightvalve 54:647072f5307a 2947 }
Lightvalve 57:f4819de54e7a 2948 //for (i = 0; i < 10000; i++) {
Lightvalve 57:f4819de54e7a 2949 // ;
Lightvalve 57:f4819de54e7a 2950 // }
Lightvalve 56:6f50d9d3bfee 2951 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 2952 //valve position
Lightvalve 171:bfc1fd2629d8 2953 CAN_TX_VALVE_POSITION((int16_t) pos.sen/(float)(ENC_PULSE_PER_POSITION), (int16_t) virt_pos, (int16_t) 0, (int16_t) 0); //1600
Lightvalve 54:647072f5307a 2954 }
Lightvalve 20:806196fda269 2955
Lightvalve 54:647072f5307a 2956 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 2957 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 2958 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 2959 // }
Lightvalve 54:647072f5307a 2960 //if (flag_delay_test == 1){
Lightvalve 58:2eade98630e2 2961 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 2962 //}
Lightvalve 52:8ea76864368a 2963
Lightvalve 54:647072f5307a 2964 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 2965 }
Lightvalve 54:647072f5307a 2966 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 2967
Lightvalve 20:806196fda269 2968 }
Lightvalve 52:8ea76864368a 2969 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 2970
Lightvalve 58:2eade98630e2 2971 }