2011

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Wed Nov 25 06:19:12 2020 +0000
Revision:
173:1074553d2f6f
Parent:
172:8e9cf31d63f4
Child:
174:d5377766d7ea
201125-1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 173:1074553d2f6f 1 //201125_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 164:645207e160ca 11 #include "FlashWriter.h"
Lightvalve 165:42c938a40313 12 #include <string>
Lightvalve 165:42c938a40313 13 #include <iostream>
Lightvalve 59:bc8c8270f0ab 14
Lightvalve 59:bc8c8270f0ab 15 using namespace std;
Lightvalve 59: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 165:42c938a40313 40 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 165: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 48:e7bcfc244d40 75 extern int CID_RX_REF_VALVE_POS;
Lightvalve 44: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 59:bc8c8270f0ab 85
Lightvalve 59:bc8c8270f0ab 86
Lightvalve 59: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 46:fdcb8bd86fd6 110 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 46:fdcb8bd86fd6 111 MODE_JOINT_CONTROL, //2
Lightvalve 49:3c630b5eba9f 112
Lightvalve 46:fdcb8bd86fd6 113 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 136:a843f32ced33 114 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 166: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 166: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 55: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 55:f4819de54e7a 141 MODE_SYSTEM_ID, //33
Lightvalve 164:645207e160ca 142 MODE_FREQ_TEST, //34
Lightvalve 164:645207e160ca 143 MODE_SEND_BUFFER, //35
Lightvalve 164:645207e160ca 144 MODE_SEND_OVER, //36
Lightvalve 164:645207e160ca 145 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 146 };
Lightvalve 12:6f2531038ea4 147
Lightvalve 63:a2d7c63419c2 148 void SystemClock_Config(void)
Lightvalve 63:a2d7c63419c2 149 {
Lightvalve 164:645207e160ca 150 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 164:645207e160ca 151 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 63:a2d7c63419c2 152
Lightvalve 164:645207e160ca 153 /** Configure the main internal regulator output voltage
Lightvalve 164:645207e160ca 154 */
Lightvalve 164:645207e160ca 155 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 164:645207e160ca 156 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 164:645207e160ca 157 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 164:645207e160ca 158 */
Lightvalve 164:645207e160ca 159 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 164:645207e160ca 160 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 164:645207e160ca 161 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 164:645207e160ca 162 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 164:645207e160ca 163 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 164:645207e160ca 164 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 164:645207e160ca 165 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 164:645207e160ca 166 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 164:645207e160ca 167 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 164:645207e160ca 168 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 164:645207e160ca 169 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 164:645207e160ca 170 //Error_Handler();
Lightvalve 164:645207e160ca 171 }
Lightvalve 164:645207e160ca 172 /** Activate the Over-Drive mode
Lightvalve 164:645207e160ca 173 */
Lightvalve 164:645207e160ca 174 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 164:645207e160ca 175 //Error_Handler();
Lightvalve 164:645207e160ca 176 }
Lightvalve 164:645207e160ca 177 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 164:645207e160ca 178 */
Lightvalve 164:645207e160ca 179 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 164:645207e160ca 180 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 164:645207e160ca 181 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 164:645207e160ca 182 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 164:645207e160ca 183 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 164:645207e160ca 184 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 63:a2d7c63419c2 185
Lightvalve 164:645207e160ca 186 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 164:645207e160ca 187 //Error_Handler();
Lightvalve 164:645207e160ca 188 }
Lightvalve 63:a2d7c63419c2 189 }
Lightvalve 63:a2d7c63419c2 190
Lightvalve 97:7bbcb3c0fb06 191 float u_past[num_array_u_past] = {0.0f};
Lightvalve 66:328e1be06f5d 192 float x_past[num_array_x_past] = {0.0f};
Lightvalve 83:a3b46118b5cd 193 float x_future[num_array_x_future] = {0.0f};
Lightvalve 66:328e1be06f5d 194 float f_past[num_array_f_past] = {0.0f};
Lightvalve 66:328e1be06f5d 195 float f_future[num_array_f_future] = {0.0f};
Lightvalve 64:a8e6799dbce3 196
Lightvalve 71:f80dc3970c99 197 float input_NN[num_input] = { 0.0f };
Lightvalve 63:a2d7c63419c2 198
Lightvalve 164:645207e160ca 199 const float h1[num_input][16] = {
Lightvalve 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 63:a2d7c63419c2 217 };
Lightvalve 63:a2d7c63419c2 218
Lightvalve 164:645207e160ca 219 const float h2[16][16] = {
Lightvalve 163: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 63:a2d7c63419c2 236 };
Lightvalve 63:a2d7c63419c2 237
Lightvalve 164:645207e160ca 238 const float h3[16][16] = {
Lightvalve 163: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 164: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 64:a8e6799dbce3 255 };
Lightvalve 63:a2d7c63419c2 256
Lightvalve 163: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 64:a8e6799dbce3 258
Lightvalve 163: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 85:471334725012 260
Lightvalve 163: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 63:a2d7c63419c2 262
Lightvalve 163: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 85:471334725012 264
Lightvalve 163:f05a45d5f318 265 const float bout[1] = { -0.10839174687862396f };
Lightvalve 63:a2d7c63419c2 266
Lightvalve 165:42c938a40313 267 /////////////////////////////////////////////////////////////////////////////////////////////RL
Lightvalve 165:42c938a40313 268 float input_RL[num_input_RL] = { 0.0f };
Lightvalve 165:42c938a40313 269
Lightvalve 165:42c938a40313 270 //Critic Networks
Lightvalve 168:68c7914679ec 271 float hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 272 float bc1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 273 float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 274 float bc2[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 275 float hc3[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 276 float bc3 = 0.0f;
Lightvalve 165:42c938a40313 277
Lightvalve 165:42c938a40313 278 //Critic Networks Temporary
Lightvalve 168:68c7914679ec 279 float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 280 float bc1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 281 float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 282 float bc2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 283 float hc3_temp[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 284 float bc3_temp = 0.0f;
Lightvalve 165:42c938a40313 285
Lightvalve 165:42c938a40313 286 //Actor Networks
Lightvalve 168:68c7914679ec 287 float ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 288 float ba1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 289 float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 290 float ba2[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 291 float ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 168:68c7914679ec 292 float ba3[2] = {0.0f};
Lightvalve 165:42c938a40313 293
Lightvalve 165:42c938a40313 294 //Actor Networks Temporary
Lightvalve 168:68c7914679ec 295 float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 296 float ba1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 297 float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 298 float ba2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 299 float ha3_temp[num_hidden_unit2][2] = {0.0f};
Lightvalve 168:68c7914679ec 300 float ba3_temp[2] = {0.0f};
Lightvalve 64:a8e6799dbce3 301
Lightvalve 85:471334725012 302 float VALVE_POS_RAW_NN = 0.0f;
Lightvalve 85:471334725012 303 float DDV_JOINT_POS_FF(float REF_JOINT_VEL);
Lightvalve 59:bc8c8270f0ab 304
Lightvalve 165:42c938a40313 305
Lightvalve 165:42c938a40313 306 float Critic_Network(float *arr)
Lightvalve 165:42c938a40313 307 {
Lightvalve 168:68c7914679ec 308 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 168:68c7914679ec 309 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 165:42c938a40313 310 float output = 0.0f;
Lightvalve 168:68c7914679ec 311 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 312 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 313 output1[index2] = output1[index2] + hc1[index1][index2] * arr[index1];
Lightvalve 165:42c938a40313 314 }
Lightvalve 168:68c7914679ec 315 //ReLU
Lightvalve 168:68c7914679ec 316 output1[index2] = output1[index2] + bc1[index2];
Lightvalve 168:68c7914679ec 317 hx_c_sum[index2] = output1[index2];
Lightvalve 168:68c7914679ec 318 if (output1[index2] < 0) {
Lightvalve 168:68c7914679ec 319 output1[index2] = 0;
Lightvalve 168:68c7914679ec 320 }
Lightvalve 168:68c7914679ec 321 //tanh
Lightvalve 168:68c7914679ec 322 //output1[index2] = tanh(output1[index2] + bc1[index2]);
Lightvalve 168:68c7914679ec 323 }
Lightvalve 168:68c7914679ec 324 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 325 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 169:c828479f53f9 326 output2[index2] = output2[index2] + hc2[index1][index2] * output1[index1];
Lightvalve 168:68c7914679ec 327 }
Lightvalve 168:68c7914679ec 328 //ReLU
Lightvalve 168:68c7914679ec 329 output2[index2] = output2[index2] + bc2[index2];
Lightvalve 168:68c7914679ec 330 hxh_c_sum[index2] = output2[index2];
Lightvalve 168:68c7914679ec 331 if (output2[index2] < 0) {
Lightvalve 168:68c7914679ec 332 output2[index2] = 0;
Lightvalve 168:68c7914679ec 333 }
Lightvalve 168:68c7914679ec 334 //tanh
Lightvalve 168:68c7914679ec 335 //output2[index2] = tanh(output2[index2] + bc2[index2]);
Lightvalve 165:42c938a40313 336 }
Lightvalve 165:42c938a40313 337 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 168:68c7914679ec 338 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 339 output = output + hc3[index1] * output2[index1];
Lightvalve 165:42c938a40313 340 }
Lightvalve 168:68c7914679ec 341 output = output + bc3;
Lightvalve 168:68c7914679ec 342 hxhh_c_sum = output;
Lightvalve 165:42c938a40313 343 }
Lightvalve 165:42c938a40313 344 return output;
Lightvalve 165:42c938a40313 345 }
Lightvalve 165:42c938a40313 346
Lightvalve 165:42c938a40313 347 float Critic_Network_Temp(float *arr)
Lightvalve 165:42c938a40313 348 {
Lightvalve 168:68c7914679ec 349 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 168:68c7914679ec 350 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 165:42c938a40313 351 float output = 0.0f;
Lightvalve 168:68c7914679ec 352 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 353 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 354 output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1];
Lightvalve 165:42c938a40313 355 }
Lightvalve 168:68c7914679ec 356 //ReLU
Lightvalve 168:68c7914679ec 357 output1[index2] = output1[index2] + bc1_temp[index2];
Lightvalve 168:68c7914679ec 358 hx_c_sum[index2] = output1[index2];
Lightvalve 168:68c7914679ec 359 if (output1[index2] < 0) {
Lightvalve 168:68c7914679ec 360 output1[index2] = 0;
Lightvalve 168:68c7914679ec 361 }
Lightvalve 168:68c7914679ec 362 //tanh
Lightvalve 168:68c7914679ec 363 //output1[index2] = tanh(output1[index2] + bc1_temp[index2]);
Lightvalve 168:68c7914679ec 364 }
Lightvalve 168:68c7914679ec 365 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 366 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 169:c828479f53f9 367 output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1];
Lightvalve 168:68c7914679ec 368 }
Lightvalve 168:68c7914679ec 369 //ReLU
Lightvalve 168:68c7914679ec 370 output2[index2] = output2[index2] + bc2_temp[index2];
Lightvalve 168:68c7914679ec 371 hxh_c_sum[index2] = output2[index2];
Lightvalve 168:68c7914679ec 372 if (output2[index2] < 0) {
Lightvalve 168:68c7914679ec 373 output2[index2] = 0;
Lightvalve 168:68c7914679ec 374 }
Lightvalve 168:68c7914679ec 375 //tanh
Lightvalve 168:68c7914679ec 376 //output2[index2] = tanh(output2[index2] + bc2_temp[index2]);
Lightvalve 165:42c938a40313 377 }
Lightvalve 165:42c938a40313 378 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 168:68c7914679ec 379 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 380 output = output + hc3_temp[index1] * output2[index1];
Lightvalve 165:42c938a40313 381 }
Lightvalve 168:68c7914679ec 382 output = output + bc3_temp;
Lightvalve 168:68c7914679ec 383 hxhh_c_sum = output;
Lightvalve 165:42c938a40313 384 }
Lightvalve 165:42c938a40313 385 return output;
Lightvalve 165:42c938a40313 386 }
Lightvalve 165:42c938a40313 387
Lightvalve 165:42c938a40313 388
Lightvalve 165:42c938a40313 389 void Actor_Network(float *arr)
Lightvalve 165:42c938a40313 390 {
Lightvalve 168:68c7914679ec 391 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 392 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 165:42c938a40313 393 float output[2] = {0.0f};
Lightvalve 165:42c938a40313 394
Lightvalve 168:68c7914679ec 395 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 168:68c7914679ec 396 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:1074553d2f6f 397 output1[index2] = output1[index2] + ha1_temp[index1][index2] * arr[index1];
Lightvalve 168:68c7914679ec 398 }
Lightvalve 173:1074553d2f6f 399 output1[index2] = output1[index2] + ba1_temp[index2];
Lightvalve 168:68c7914679ec 400 hx_a_sum[index2] = output1[index2];
Lightvalve 168:68c7914679ec 401 if (output1[index2] < 0) {
Lightvalve 168:68c7914679ec 402 output1[index2] = 0;
Lightvalve 168:68c7914679ec 403 }
Lightvalve 168:68c7914679ec 404 }
Lightvalve 168:68c7914679ec 405 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 406 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:1074553d2f6f 407 output2[index2] = output2[index2] + ha2_temp[index1][index2] * output1[index1];
Lightvalve 168:68c7914679ec 408 }
Lightvalve 173:1074553d2f6f 409 output2[index2] = output2[index2] + ba2_temp[index2];
Lightvalve 168:68c7914679ec 410 hxh_a_sum[index2] = output2[index2];
Lightvalve 168:68c7914679ec 411 if (output2[index2] < 0) {
Lightvalve 168:68c7914679ec 412 output2[index2] = 0;
Lightvalve 168:68c7914679ec 413 }
Lightvalve 168:68c7914679ec 414 }
Lightvalve 168:68c7914679ec 415 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 168:68c7914679ec 416 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:1074553d2f6f 417 output[index2] = output[index2] + ha3_temp[index1][index2] * output2[index1];
Lightvalve 168:68c7914679ec 418 }
Lightvalve 173:1074553d2f6f 419 hxhh_a_sum[index2] = output[index2] + ba3_temp[index2];
Lightvalve 168:68c7914679ec 420 }
Lightvalve 173:1074553d2f6f 421
Lightvalve 173:1074553d2f6f 422 mean_before_SP = output[0] + ba3_temp[0]; //SP = softplus
Lightvalve 173:1074553d2f6f 423 deviation_before_SP = output[1] + ba3_temp[1];
Lightvalve 168:68c7914679ec 424 //Softplus
Lightvalve 168:68c7914679ec 425 mean = log(1.0f+exp(mean_before_SP));
Lightvalve 168:68c7914679ec 426 deviation = log(1.0f+exp(deviation_before_SP));
Lightvalve 168:68c7914679ec 427 }
Lightvalve 168:68c7914679ec 428
Lightvalve 168:68c7914679ec 429
Lightvalve 168:68c7914679ec 430 void Actor_Network_Old(float *arr)
Lightvalve 168:68c7914679ec 431 {
Lightvalve 168:68c7914679ec 432 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 433 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 434 float output[2] = {0.0f};
Lightvalve 168:68c7914679ec 435
Lightvalve 168:68c7914679ec 436 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 437 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 438 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 165:42c938a40313 439 }
Lightvalve 165:42c938a40313 440 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 165:42c938a40313 441 if (output1[index2] < 0) {
Lightvalve 165:42c938a40313 442 output1[index2] = 0;
Lightvalve 165:42c938a40313 443 }
Lightvalve 165:42c938a40313 444 }
Lightvalve 168:68c7914679ec 445 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 446 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 169:c828479f53f9 447 output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1];
Lightvalve 165:42c938a40313 448 }
Lightvalve 168:68c7914679ec 449 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 168:68c7914679ec 450 if (output2[index2] < 0) {
Lightvalve 168:68c7914679ec 451 output2[index2] = 0;
Lightvalve 165:42c938a40313 452 }
Lightvalve 165:42c938a40313 453 }
Lightvalve 165:42c938a40313 454 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 168:68c7914679ec 455 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 456 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 165:42c938a40313 457 }
Lightvalve 165:42c938a40313 458 }
Lightvalve 168:68c7914679ec 459 mean_old = output[0] + ba3[0];
Lightvalve 168:68c7914679ec 460 deviation_old = output[1] + ba3[1];
Lightvalve 168:68c7914679ec 461 //Softplus
Lightvalve 168:68c7914679ec 462 mean_old = log(1.0f+exp(mean_old));
Lightvalve 168:68c7914679ec 463 deviation_old = log(1.0f+exp(deviation_old));
Lightvalve 165:42c938a40313 464 }
Lightvalve 165:42c938a40313 465
Lightvalve 165:42c938a40313 466 float Grad_Normal_Dist_Mean(float mean, float deviation, float action)
Lightvalve 165:42c938a40313 467 {
Lightvalve 165:42c938a40313 468 float grad_mean = 0.0f;
Lightvalve 165:42c938a40313 469 grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation);
Lightvalve 165:42c938a40313 470 return grad_mean;
Lightvalve 165:42c938a40313 471 }
Lightvalve 165:42c938a40313 472
Lightvalve 165:42c938a40313 473 float Grad_Normal_Dist_Deviation(float mean, float deviation, float action)
Lightvalve 165:42c938a40313 474 {
Lightvalve 165:42c938a40313 475 float grad_dev = 0.0f;
Lightvalve 165:42c938a40313 476 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 165:42c938a40313 477 return grad_dev;
Lightvalve 165:42c938a40313 478 }
Lightvalve 165:42c938a40313 479
Lightvalve 168:68c7914679ec 480 float ReLU(float x)
Lightvalve 168:68c7914679ec 481 {
Lightvalve 168:68c7914679ec 482 if (x >= 0) {
Lightvalve 168:68c7914679ec 483 return x;
Lightvalve 168:68c7914679ec 484 } else {
Lightvalve 168:68c7914679ec 485 return 0.0f;
Lightvalve 168:68c7914679ec 486 }
Lightvalve 168:68c7914679ec 487 }
Lightvalve 168:68c7914679ec 488
Lightvalve 165:42c938a40313 489 void update_Critic_Networks(float (*arr)[num_input_RL])
Lightvalve 165:42c938a40313 490 {
Lightvalve 172:8e9cf31d63f4 491 float gradient_rate = 0.1f;
Lightvalve 168:68c7914679ec 492 float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 172:8e9cf31d63f4 493 float G_bc1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 494 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 495 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 168:68c7914679ec 496 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 497 float d_V_d_hc1 = 0.0f;
Lightvalve 168:68c7914679ec 498 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 168:68c7914679ec 499 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 168:68c7914679ec 500 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 172:8e9cf31d63f4 501 d_V_d_hc1 = d_V_d_hc1 + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 168:68c7914679ec 502 }
Lightvalve 168:68c7914679ec 503 }
Lightvalve 165:42c938a40313 504 }
Lightvalve 172:8e9cf31d63f4 505 G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1);
Lightvalve 165:42c938a40313 506 }
Lightvalve 165:42c938a40313 507 G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size;
Lightvalve 168:68c7914679ec 508 //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 168:68c7914679ec 509 }
Lightvalve 168:68c7914679ec 510 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 511 float d_V_d_bc1 = 0.0f;
Lightvalve 168:68c7914679ec 512 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 168:68c7914679ec 513 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 168:68c7914679ec 514 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 172:8e9cf31d63f4 515 d_V_d_bc1 = d_V_d_bc1 + hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 168:68c7914679ec 516 }
Lightvalve 168:68c7914679ec 517 }
Lightvalve 168:68c7914679ec 518 }
Lightvalve 172:8e9cf31d63f4 519 G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1);
Lightvalve 168:68c7914679ec 520 }
Lightvalve 168:68c7914679ec 521 G_bc1[index2] = G_bc1[index2] / batch_size;
Lightvalve 168:68c7914679ec 522 //bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 168:68c7914679ec 523 }
Lightvalve 168:68c7914679ec 524
Lightvalve 170:2f7289dbd488 525
Lightvalve 168:68c7914679ec 526 float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 527 float G_bc2[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 528 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 529 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 530 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 531 float d_V_d_hc2 = 0.0f;
Lightvalve 168:68c7914679ec 532 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 168:68c7914679ec 533 if (hx_c_sum_array[n][index1] > 0) {
Lightvalve 172:8e9cf31d63f4 534 d_V_d_hc2 = hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 168:68c7914679ec 535 }
Lightvalve 168:68c7914679ec 536 }
Lightvalve 172:8e9cf31d63f4 537 G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2);
Lightvalve 168:68c7914679ec 538 }
Lightvalve 168:68c7914679ec 539 G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size;
Lightvalve 168:68c7914679ec 540 //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 168:68c7914679ec 541 }
Lightvalve 168:68c7914679ec 542 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 543 float d_V_d_bc2 = 0.0f;
Lightvalve 168:68c7914679ec 544 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 172:8e9cf31d63f4 545 d_V_d_bc2 = hc3_temp[index2];
Lightvalve 168:68c7914679ec 546 }
Lightvalve 172:8e9cf31d63f4 547 G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2);
Lightvalve 168:68c7914679ec 548 }
Lightvalve 168:68c7914679ec 549 G_bc2[index2] = G_bc2[index2] / batch_size;
Lightvalve 168:68c7914679ec 550 //bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 168:68c7914679ec 551 }
Lightvalve 168:68c7914679ec 552
Lightvalve 168:68c7914679ec 553 float G_hc3[num_hidden_unit2]= {0.0f};
Lightvalve 168:68c7914679ec 554 float G_bc3 = 0.0f;
Lightvalve 168:68c7914679ec 555 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 168:68c7914679ec 556 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 557 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 558 float d_V_d_hc3 = 0.0f;
Lightvalve 168:68c7914679ec 559 if (hxh_c_sum_array[n][index1] >= 0) {
Lightvalve 172:8e9cf31d63f4 560 d_V_d_hc3 = d_V_d_hc3 + hxh_c_sum_array[n][index1];
Lightvalve 168:68c7914679ec 561 }
Lightvalve 172:8e9cf31d63f4 562 G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3);
Lightvalve 168:68c7914679ec 563 }
Lightvalve 168:68c7914679ec 564 G_hc3[index1] = G_hc3[index1] / batch_size;
Lightvalve 168:68c7914679ec 565 //hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 168:68c7914679ec 566 }
Lightvalve 168:68c7914679ec 567 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 568 float d_V_d_bc3 = 0.0f;
Lightvalve 172:8e9cf31d63f4 569 d_V_d_bc3 = 1.0f;
Lightvalve 172:8e9cf31d63f4 570 G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3);
Lightvalve 168:68c7914679ec 571 }
Lightvalve 168:68c7914679ec 572 G_bc3 = G_bc3 / batch_size;
Lightvalve 168:68c7914679ec 573 //bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 168:68c7914679ec 574 }
Lightvalve 168:68c7914679ec 575
Lightvalve 168:68c7914679ec 576 // Simultaneous Update
Lightvalve 168:68c7914679ec 577 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 168:68c7914679ec 578 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 579 hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 165:42c938a40313 580 }
Lightvalve 165:42c938a40313 581 bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 165:42c938a40313 582 }
Lightvalve 168:68c7914679ec 583 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 584 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 585 hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 168:68c7914679ec 586 }
Lightvalve 168:68c7914679ec 587 bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 168:68c7914679ec 588 }
Lightvalve 165:42c938a40313 589 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 168:68c7914679ec 590 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 591 hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 165:42c938a40313 592 }
Lightvalve 168:68c7914679ec 593 bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 165:42c938a40313 594 }
Lightvalve 165:42c938a40313 595 }
Lightvalve 165:42c938a40313 596
Lightvalve 168:68c7914679ec 597 ///////////////////////////Softplus//////////////////////////////////
Lightvalve 165:42c938a40313 598 void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 165:42c938a40313 599 {
Lightvalve 172:8e9cf31d63f4 600 float gradient_rate = 0.1f;
Lightvalve 168:68c7914679ec 601
Lightvalve 168:68c7914679ec 602 float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 603 float G_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 168:68c7914679ec 604
Lightvalve 168:68c7914679ec 605 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 606 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 168:68c7914679ec 607 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 608 float d_x_d_ha1 = 0.0f;
Lightvalve 172:8e9cf31d63f4 609 float d_y_d_ha1 = 0.0f;
Lightvalve 168:68c7914679ec 610 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 165:42c938a40313 611 G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 165:42c938a40313 612 } else {
Lightvalve 168:68c7914679ec 613 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 168:68c7914679ec 614 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 168:68c7914679ec 615 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 172:8e9cf31d63f4 616 d_x_d_ha1 = d_x_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 172:8e9cf31d63f4 617 d_y_d_ha1 = d_y_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 168:68c7914679ec 618 }
Lightvalve 166:bfc1fd2629d8 619 }
Lightvalve 165:42c938a40313 620 }
Lightvalve 165:42c938a40313 621 float d_mean_d_ha1 = 0.0f;
Lightvalve 165:42c938a40313 622 float d_dev_d_ha1 = 0.0f;
Lightvalve 172:8e9cf31d63f4 623 d_mean_d_ha1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha1;
Lightvalve 172:8e9cf31d63f4 624 d_dev_d_ha1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha1;
Lightvalve 168:68c7914679ec 625
Lightvalve 168:68c7914679ec 626 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 165:42c938a40313 627 }
Lightvalve 165:42c938a40313 628 }
Lightvalve 171:589ea3edcf3c 629 G_ha1[index1][index2] = -G_ha1[index1][index2] / batch_size;
Lightvalve 170:2f7289dbd488 630 //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 165:42c938a40313 631 }
Lightvalve 168:68c7914679ec 632
Lightvalve 168:68c7914679ec 633 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 634 float d_x_d_ba1 = 0.0f;
Lightvalve 172:8e9cf31d63f4 635 float d_y_d_ba1 = 0.0f;
Lightvalve 168:68c7914679ec 636 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 165:42c938a40313 637 G_ba1[index2] = G_ba1[index2];
Lightvalve 165:42c938a40313 638 } else {
Lightvalve 168:68c7914679ec 639 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 168:68c7914679ec 640 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 168:68c7914679ec 641 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 172:8e9cf31d63f4 642 d_x_d_ba1 = d_x_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 172:8e9cf31d63f4 643 d_y_d_ba1 = d_y_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 168:68c7914679ec 644 }
Lightvalve 166:bfc1fd2629d8 645 }
Lightvalve 165:42c938a40313 646 }
Lightvalve 165:42c938a40313 647 float d_mean_d_ba1 = 0.0f;
Lightvalve 165:42c938a40313 648 float d_dev_d_ba1 = 0.0f;
Lightvalve 172:8e9cf31d63f4 649 d_mean_d_ba1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba1;
Lightvalve 172:8e9cf31d63f4 650 d_dev_d_ba1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba1;
Lightvalve 168:68c7914679ec 651
Lightvalve 168:68c7914679ec 652 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 165:42c938a40313 653 }
Lightvalve 165:42c938a40313 654 }
Lightvalve 171:589ea3edcf3c 655 G_ba1[index2] = -G_ba1[index2] / batch_size;
Lightvalve 170:2f7289dbd488 656 //ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 165:42c938a40313 657 }
Lightvalve 165:42c938a40313 658
Lightvalve 168:68c7914679ec 659 float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 660 float G_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 168:68c7914679ec 661
Lightvalve 168:68c7914679ec 662 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 663 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 664 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 665 float d_x_d_ha2 = 0.0f;
Lightvalve 172:8e9cf31d63f4 666 float d_y_d_ha2 = 0.0f;
Lightvalve 168:68c7914679ec 667 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 165:42c938a40313 668 G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 165:42c938a40313 669 } else {
Lightvalve 168:68c7914679ec 670 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 168:68c7914679ec 671 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 172:8e9cf31d63f4 672 d_x_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][0];
Lightvalve 172:8e9cf31d63f4 673 d_y_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][1];
Lightvalve 166:bfc1fd2629d8 674 }
Lightvalve 165:42c938a40313 675 }
Lightvalve 168:68c7914679ec 676
Lightvalve 165:42c938a40313 677 float d_mean_d_ha2 = 0.0f;
Lightvalve 165:42c938a40313 678 float d_dev_d_ha2 = 0.0f;
Lightvalve 172:8e9cf31d63f4 679 d_mean_d_ha2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha2;
Lightvalve 172:8e9cf31d63f4 680 d_dev_d_ha2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha2;
Lightvalve 168:68c7914679ec 681
Lightvalve 168:68c7914679ec 682 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 165:42c938a40313 683 }
Lightvalve 165:42c938a40313 684 }
Lightvalve 171:589ea3edcf3c 685 G_ha2[index1][index2] = -G_ha2[index1][index2] / batch_size;
Lightvalve 170:2f7289dbd488 686 //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 165:42c938a40313 687 }
Lightvalve 168:68c7914679ec 688
Lightvalve 168:68c7914679ec 689 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 690 float d_x_d_ba2 = 0.0f;
Lightvalve 172:8e9cf31d63f4 691 float d_y_d_ba2 = 0.0f;
Lightvalve 168:68c7914679ec 692 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 165:42c938a40313 693 G_ba2[index2] = G_ba2[index2];
Lightvalve 165:42c938a40313 694 } else {
Lightvalve 165:42c938a40313 695
Lightvalve 168:68c7914679ec 696 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 172:8e9cf31d63f4 697 d_x_d_ba2 = ha3_temp[index2][0];
Lightvalve 172:8e9cf31d63f4 698 d_y_d_ba2 = ha3_temp[index2][1];
Lightvalve 168:68c7914679ec 699 }
Lightvalve 168:68c7914679ec 700 float d_mean_d_ba2= 0.0f;
Lightvalve 168:68c7914679ec 701 float d_dev_d_ba2= 0.0f;
Lightvalve 172:8e9cf31d63f4 702 d_mean_d_ba2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba2;
Lightvalve 172:8e9cf31d63f4 703 d_dev_d_ba2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba2;
Lightvalve 168:68c7914679ec 704
Lightvalve 168:68c7914679ec 705 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 165:42c938a40313 706 }
Lightvalve 165:42c938a40313 707 }
Lightvalve 171:589ea3edcf3c 708 G_ba2[index2] = -G_ba2[index2] / batch_size;
Lightvalve 170:2f7289dbd488 709 //ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 165:42c938a40313 710 }
Lightvalve 168:68c7914679ec 711
Lightvalve 168:68c7914679ec 712 float G_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 168:68c7914679ec 713 float G_ba3[2] = {0.0f};
Lightvalve 168:68c7914679ec 714
Lightvalve 168:68c7914679ec 715 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 168:68c7914679ec 716 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 717 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 718 float d_x_d_ha3 = 0.0f;
Lightvalve 172:8e9cf31d63f4 719 float d_y_d_ha3 = 0.0f;
Lightvalve 168:68c7914679ec 720 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 168:68c7914679ec 721 G_ha3[index1][index2] = G_ha3[index1][index2];
Lightvalve 168:68c7914679ec 722 } else {
Lightvalve 168:68c7914679ec 723 if (hxh_a_sum_array[n][index1] >= 0) {
Lightvalve 168:68c7914679ec 724 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 172:8e9cf31d63f4 725 d_x_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 172:8e9cf31d63f4 726 d_y_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 168:68c7914679ec 727 }
Lightvalve 168:68c7914679ec 728 }
Lightvalve 168:68c7914679ec 729 float d_mean_d_ha3 = 0.0f;
Lightvalve 168:68c7914679ec 730 float d_dev_d_ha3 = 0.0f;
Lightvalve 172:8e9cf31d63f4 731 d_mean_d_ha3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha3;
Lightvalve 172:8e9cf31d63f4 732 d_dev_d_ha3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha3;
Lightvalve 168:68c7914679ec 733
Lightvalve 168:68c7914679ec 734 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 168:68c7914679ec 735 }
Lightvalve 168:68c7914679ec 736 }
Lightvalve 171:589ea3edcf3c 737 G_ha3[index1][index2] = -G_ha3[index1][index2] / batch_size;
Lightvalve 170:2f7289dbd488 738 //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate * G_ha3[index1][index2];
Lightvalve 168:68c7914679ec 739 }
Lightvalve 168:68c7914679ec 740
Lightvalve 168:68c7914679ec 741 for (int n=0; n<batch_size; n++) {
Lightvalve 172:8e9cf31d63f4 742 float d_x_d_ba3 = 0.0f;
Lightvalve 172:8e9cf31d63f4 743 float d_y_d_ba3 = 0.0f;
Lightvalve 168:68c7914679ec 744 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 168:68c7914679ec 745 G_ba3[index2] = G_ba3[index2];
Lightvalve 168:68c7914679ec 746 } else {
Lightvalve 168:68c7914679ec 747
Lightvalve 172:8e9cf31d63f4 748 d_x_d_ba3 = 1.0f;
Lightvalve 172:8e9cf31d63f4 749 d_y_d_ba3 = 1.0f;
Lightvalve 168:68c7914679ec 750
Lightvalve 168:68c7914679ec 751 float d_mean_d_ba3= 0.0f;
Lightvalve 168:68c7914679ec 752 float d_dev_d_ba3= 0.0f;
Lightvalve 172:8e9cf31d63f4 753 d_mean_d_ba3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba3;
Lightvalve 172:8e9cf31d63f4 754 d_dev_d_ba3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba3;
Lightvalve 168:68c7914679ec 755
Lightvalve 168:68c7914679ec 756 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 168:68c7914679ec 757 }
Lightvalve 168:68c7914679ec 758 }
Lightvalve 171:589ea3edcf3c 759 G_ba3[index2] = -G_ba3[index2] / batch_size;
Lightvalve 170:2f7289dbd488 760 //ba3_temp[index2] = ba3_temp[index2] - gradient_rate * G_ba3[index2];
Lightvalve 170:2f7289dbd488 761 }
Lightvalve 172:8e9cf31d63f4 762
Lightvalve 170:2f7289dbd488 763 // Simultaneous Update
Lightvalve 170:2f7289dbd488 764 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:2f7289dbd488 765 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:2f7289dbd488 766 ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 170:2f7289dbd488 767 }
Lightvalve 170:2f7289dbd488 768 ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 170:2f7289dbd488 769 }
Lightvalve 170:2f7289dbd488 770 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 170:2f7289dbd488 771 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:2f7289dbd488 772 ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 170:2f7289dbd488 773 }
Lightvalve 170:2f7289dbd488 774 ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 170:2f7289dbd488 775 }
Lightvalve 170:2f7289dbd488 776 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 170:2f7289dbd488 777 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 170:2f7289dbd488 778 ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate * G_ha3[index1][index2];
Lightvalve 170:2f7289dbd488 779 }
Lightvalve 168:68c7914679ec 780 ba3_temp[index2] = ba3_temp[index2] - gradient_rate * G_ba3[index2];
Lightvalve 168:68c7914679ec 781 }
Lightvalve 172:8e9cf31d63f4 782
Lightvalve 165:42c938a40313 783 }
Lightvalve 165:42c938a40313 784
Lightvalve 165:42c938a40313 785 float rand_normal(double mean, double stddev)
Lightvalve 165:42c938a40313 786 {
Lightvalve 165:42c938a40313 787 //Box muller method
Lightvalve 165:42c938a40313 788 static double n2 = 0.0f;
Lightvalve 165:42c938a40313 789 static int n2_cached = 0;
Lightvalve 165:42c938a40313 790 if (!n2_cached) {
Lightvalve 165:42c938a40313 791 double x, y, r;
Lightvalve 165:42c938a40313 792 do {
Lightvalve 165:42c938a40313 793 x = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 165:42c938a40313 794 y = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 165:42c938a40313 795
Lightvalve 165:42c938a40313 796 r = x*x + y*y;
Lightvalve 165:42c938a40313 797 } while (r == 0.0f || r > 1.0f);
Lightvalve 165:42c938a40313 798 {
Lightvalve 165:42c938a40313 799 double d = sqrt(-2.0f*log(r)/r);
Lightvalve 165:42c938a40313 800 double n1 = x*d;
Lightvalve 165:42c938a40313 801 n2 = y*d;
Lightvalve 165:42c938a40313 802 double result = n1*stddev + mean;
Lightvalve 165:42c938a40313 803 n2_cached = 1;
Lightvalve 165:42c938a40313 804 return result;
Lightvalve 165:42c938a40313 805 }
Lightvalve 165:42c938a40313 806 } else {
Lightvalve 165:42c938a40313 807 n2_cached = 0;
Lightvalve 165:42c938a40313 808 return n2*stddev + mean;
Lightvalve 165:42c938a40313 809 }
Lightvalve 165:42c938a40313 810 }
Lightvalve 165:42c938a40313 811
Lightvalve 168:68c7914679ec 812
Lightvalve 165:42c938a40313 813 void Overwirte_Critic_Networks()
Lightvalve 165:42c938a40313 814 {
Lightvalve 168:68c7914679ec 815 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 816 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 817 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 165:42c938a40313 818 }
Lightvalve 165:42c938a40313 819 bc1[index2] = bc1_temp[index2];
Lightvalve 165:42c938a40313 820 }
Lightvalve 168:68c7914679ec 821 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 822 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 823 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 168:68c7914679ec 824 }
Lightvalve 168:68c7914679ec 825 bc2[index2] = bc2_temp[index2];
Lightvalve 168:68c7914679ec 826 hc3[index2] = hc3_temp[index2];
Lightvalve 168:68c7914679ec 827 }
Lightvalve 168:68c7914679ec 828 bc3 = bc3_temp;
Lightvalve 165:42c938a40313 829 }
Lightvalve 165:42c938a40313 830 void Overwirte_Actor_Networks()
Lightvalve 165:42c938a40313 831 {
Lightvalve 168:68c7914679ec 832 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 833 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 834 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 165:42c938a40313 835 }
Lightvalve 165:42c938a40313 836 ba1[index2] = ba1_temp[index2];
Lightvalve 165:42c938a40313 837 }
Lightvalve 168:68c7914679ec 838 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 839 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 165:42c938a40313 840 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 165:42c938a40313 841 }
Lightvalve 165:42c938a40313 842 ba2[index2] = ba2_temp[index2];
Lightvalve 165:42c938a40313 843 }
Lightvalve 168:68c7914679ec 844 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 168:68c7914679ec 845 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 846 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 168:68c7914679ec 847 }
Lightvalve 168:68c7914679ec 848 ba3[index2] = ba3_temp[index2];
Lightvalve 168:68c7914679ec 849 }
Lightvalve 165:42c938a40313 850 }
Lightvalve 165:42c938a40313 851
Lightvalve 165:42c938a40313 852
GiJeongKim 0:51c43836c1d7 853 int main()
GiJeongKim 0:51c43836c1d7 854 {
Lightvalve 64:a8e6799dbce3 855
Lightvalve 63:a2d7c63419c2 856 HAL_Init();
Lightvalve 63:a2d7c63419c2 857 SystemClock_Config();
Lightvalve 164:645207e160ca 858
jobuuu 6:df07d3491e3a 859 /*********************************
jobuuu 1:e04e563be5ce 860 *** Initialization
jobuuu 6:df07d3491e3a 861 *********************************/
Lightvalve 67:3995ffeaa786 862 LED = 0;
Lightvalve 59:bc8c8270f0ab 863 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 864
GiJeongKim 0:51c43836c1d7 865 // i2c init
Lightvalve 8:5d2eebdad025 866 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 867 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 868 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 869 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 870 make_delay();
jobuuu 2:a1c0a37df760 871
GiJeongKim 0:51c43836c1d7 872 // // spi init
Lightvalve 165:42c938a40313 873 eeprom.format(8,3);
Lightvalve 165:42c938a40313 874 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 875 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 876 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 877 make_delay();
Lightvalve 21:e5f1a43ea6f9 878
Lightvalve 16:903b5a4433b4 879 //rom
Lightvalve 19:23b7c1ad8683 880 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 881 make_delay();
Lightvalve 13:747daba9cf59 882
GiJeongKim 0:51c43836c1d7 883 // ADC init
jobuuu 5:a4319f79457b 884 Init_ADC();
Lightvalve 11:82d8768d7351 885 make_delay();
jobuuu 2:a1c0a37df760 886
GiJeongKim 0:51c43836c1d7 887 // Pwm init
GiJeongKim 0:51c43836c1d7 888 Init_PWM();
GiJeongKim 0:51c43836c1d7 889 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 890 make_delay();
Lightvalve 13:747daba9cf59 891
Lightvalve 11:82d8768d7351 892 // TMR3 init
Lightvalve 11:82d8768d7351 893 Init_TMR3();
Lightvalve 11:82d8768d7351 894 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 895 make_delay();
Lightvalve 21:e5f1a43ea6f9 896
Lightvalve 49:3c630b5eba9f 897 // TMR2 init
Lightvalve 54:6f50d9d3bfee 898 // Init_TMR2();
Lightvalve 54:6f50d9d3bfee 899 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 54:6f50d9d3bfee 900 // make_delay();
Lightvalve 21:e5f1a43ea6f9 901
GiJeongKim 0:51c43836c1d7 902 // CAN
jobuuu 2:a1c0a37df760 903 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 904 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 905 make_delay();
Lightvalve 34:bb2ca2fc2a8e 906
Lightvalve 23:59218d4a256d 907 //Timer priority
Lightvalve 23:59218d4a256d 908 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 55:f4819de54e7a 909 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 52:647072f5307a 910 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 911
Lightvalve 23:59218d4a256d 912 //can.reset();
Lightvalve 19:23b7c1ad8683 913 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 914
GiJeongKim 0:51c43836c1d7 915 // spi _ enc
GiJeongKim 0:51c43836c1d7 916 spi_enc_set_init();
Lightvalve 11:82d8768d7351 917 make_delay();
Lightvalve 13:747daba9cf59 918
Lightvalve 11:82d8768d7351 919 //DAC init
Lightvalve 56:2eade98630e2 920 if (SENSING_MODE == 0) {
Lightvalve 56:2eade98630e2 921 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 56:2eade98630e2 922 dac_2 = 0.0f;
Lightvalve 56:2eade98630e2 923 } else if (SENSING_MODE == 1) {
Lightvalve 56:2eade98630e2 924 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 56:2eade98630e2 925 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 56:2eade98630e2 926 }
Lightvalve 11:82d8768d7351 927 make_delay();
Lightvalve 13:747daba9cf59 928
Lightvalve 19:23b7c1ad8683 929 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 930 if(i%2==0)
Lightvalve 38:118df027d851 931 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 932 else
Lightvalve 38:118df027d851 933 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 934 }
Lightvalve 164:645207e160ca 935
Lightvalve 168:68c7914679ec 936 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 937 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 165:42c938a40313 938 hc1_temp[index1][index2] = (float) (rand()%100) * 0.01f ;
Lightvalve 165:42c938a40313 939 }
Lightvalve 165:42c938a40313 940 bc1_temp[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 941 }
Lightvalve 168:68c7914679ec 942 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 943 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 944 hc2_temp[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 945 }
Lightvalve 168:68c7914679ec 946 bc2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 947 hc3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 165:42c938a40313 948 }
Lightvalve 168:68c7914679ec 949 bc3 = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 950
Lightvalve 168:68c7914679ec 951 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 165:42c938a40313 952 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 168:68c7914679ec 953 ha1[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 165:42c938a40313 954 }
Lightvalve 168:68c7914679ec 955 ba1[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 956 }
Lightvalve 168:68c7914679ec 957 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 168:68c7914679ec 958 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 168:68c7914679ec 959 ha2[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 168:68c7914679ec 960 }
Lightvalve 168:68c7914679ec 961 ba2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 165:42c938a40313 962 }
Lightvalve 165:42c938a40313 963 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 168:68c7914679ec 964 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 168:68c7914679ec 965 ha3[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 165:42c938a40313 966 }
Lightvalve 168:68c7914679ec 967 ba3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 165:42c938a40313 968 }
Lightvalve 166:bfc1fd2629d8 969
Lightvalve 165:42c938a40313 970 Overwirte_Critic_Networks();
Lightvalve 165:42c938a40313 971 Overwirte_Actor_Networks();
Lightvalve 59:bc8c8270f0ab 972
jobuuu 6:df07d3491e3a 973 /************************************
jobuuu 1:e04e563be5ce 974 *** Program is operating!
jobuuu 6:df07d3491e3a 975 *************************************/
GiJeongKim 0:51c43836c1d7 976 while(1) {
Lightvalve 164:645207e160ca 977
Lightvalve 166:bfc1fd2629d8 978 // if(timer_while==27491) {
Lightvalve 166:bfc1fd2629d8 979 // timer_while = 0;
Lightvalve 166:bfc1fd2629d8 980 // 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 166:bfc1fd2629d8 981 // //pc.printf("%f\n", virt_pos);
Lightvalve 166:bfc1fd2629d8 982 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 166:bfc1fd2629d8 983 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 166:bfc1fd2629d8 984 // }
Lightvalve 164:645207e160ca 985
Lightvalve 166:bfc1fd2629d8 986
Lightvalve 166:bfc1fd2629d8 987 //i2c
Lightvalve 166:bfc1fd2629d8 988 //read_field(i2c_slave_addr1);
Lightvalve 166:bfc1fd2629d8 989 //if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 166:bfc1fd2629d8 990
Lightvalve 172:8e9cf31d63f4 991 //timer_while ++;
Lightvalve 164:645207e160ca 992
Lightvalve 165:42c938a40313 993 ///////////////////////////////////////////////////////Neural Network
Lightvalve 164:645207e160ca 994
Lightvalve 71:f80dc3970c99 995 if(NN_Control_Flag == 0) {
Lightvalve 71:f80dc3970c99 996 LED = 0;
Lightvalve 71:f80dc3970c99 997 }
Lightvalve 164:645207e160ca 998
Lightvalve 71:f80dc3970c99 999 else if(NN_Control_Flag == 1) {
Lightvalve 164:645207e160ca 1000
Lightvalve 158:9dd4f35e9de8 1001 int ind = 0;
Lightvalve 158:9dd4f35e9de8 1002 for(int i=0; i<numpast_u; i++) {
Lightvalve 158:9dd4f35e9de8 1003 input_NN[ind] = u_past[time_interval*i];
Lightvalve 158:9dd4f35e9de8 1004 ind = ind + 1;
Lightvalve 158:9dd4f35e9de8 1005 }
Lightvalve 158:9dd4f35e9de8 1006
Lightvalve 158:9dd4f35e9de8 1007 for(int i=0; i<numpast_x; i++) {
Lightvalve 158:9dd4f35e9de8 1008 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 158:9dd4f35e9de8 1009 ind = ind + 1;
Lightvalve 158:9dd4f35e9de8 1010 }
Lightvalve 158:9dd4f35e9de8 1011 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 158:9dd4f35e9de8 1012 ind = ind + 1;
Lightvalve 164:645207e160ca 1013
Lightvalve 158:9dd4f35e9de8 1014 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 158:9dd4f35e9de8 1015 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 120:dcb3ce3056a0 1016 // ind = ind + 1;
Lightvalve 120:dcb3ce3056a0 1017 // }
Lightvalve 158:9dd4f35e9de8 1018
Lightvalve 158:9dd4f35e9de8 1019 for(int i=0; i<numpast_f; i++) {
Lightvalve 158:9dd4f35e9de8 1020 input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f;
Lightvalve 158:9dd4f35e9de8 1021 ind = ind + 1;
Lightvalve 158:9dd4f35e9de8 1022 }
Lightvalve 158:9dd4f35e9de8 1023 input_NN[ind] = torq.sen / 10000.0f + 0.5f;
Lightvalve 158:9dd4f35e9de8 1024 ind = ind + 1;
Lightvalve 158:9dd4f35e9de8 1025 for(int i=0; i<numfuture_f; i++) {
Lightvalve 163:f05a45d5f318 1026 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f+0.5f;
Lightvalve 163:f05a45d5f318 1027 input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f+0.5f;
Lightvalve 158:9dd4f35e9de8 1028 ind = ind + 1;
Lightvalve 158:9dd4f35e9de8 1029 }
Lightvalve 164:645207e160ca 1030
Lightvalve 110:8dcb1600cb90 1031 float output1[16] = { 0.0f };
Lightvalve 110:8dcb1600cb90 1032 float output2[16] = { 0.0f };
Lightvalve 110:8dcb1600cb90 1033 float output3[16] = { 0.0f };
Lightvalve 64:a8e6799dbce3 1034 float output = 0.0f;
Lightvalve 63:a2d7c63419c2 1035
Lightvalve 110:8dcb1600cb90 1036 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 66:328e1be06f5d 1037 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 64:a8e6799dbce3 1038 output1[index2] = output1[index2]
Lightvalve 71:f80dc3970c99 1039 + h1[index1][index2] * input_NN[index1];
Lightvalve 64:a8e6799dbce3 1040 }
Lightvalve 64:a8e6799dbce3 1041 output1[index2] = output1[index2] + b1[index2];
Lightvalve 64:a8e6799dbce3 1042 if (output1[index2] < 0) {
Lightvalve 64:a8e6799dbce3 1043 output1[index2] = 0;
Lightvalve 64:a8e6799dbce3 1044 }
Lightvalve 64:a8e6799dbce3 1045 }
Lightvalve 63:a2d7c63419c2 1046
Lightvalve 110:8dcb1600cb90 1047 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 110:8dcb1600cb90 1048 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 64:a8e6799dbce3 1049 output2[index2] = output2[index2]
Lightvalve 64:a8e6799dbce3 1050 + h2[index1][index2] * output1[index1];
Lightvalve 64:a8e6799dbce3 1051 }
Lightvalve 64:a8e6799dbce3 1052 output2[index2] = output2[index2] + b2[index2];
Lightvalve 64:a8e6799dbce3 1053 if (output2[index2] < 0) {
Lightvalve 64:a8e6799dbce3 1054 output2[index2] = 0;
Lightvalve 64:a8e6799dbce3 1055 }
Lightvalve 64:a8e6799dbce3 1056 }
Lightvalve 63:a2d7c63419c2 1057
Lightvalve 110:8dcb1600cb90 1058 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 110:8dcb1600cb90 1059 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 64:a8e6799dbce3 1060 output3[index2] = output3[index2]
Lightvalve 64:a8e6799dbce3 1061 + h3[index1][index2] * output2[index1];
Lightvalve 64:a8e6799dbce3 1062 }
Lightvalve 64:a8e6799dbce3 1063 output3[index2] = output3[index2] + b3[index2];
Lightvalve 64:a8e6799dbce3 1064 if (output3[index2] < 0) {
Lightvalve 64:a8e6799dbce3 1065 output3[index2] = 0;
Lightvalve 64:a8e6799dbce3 1066 }
Lightvalve 63:a2d7c63419c2 1067 }
Lightvalve 64:a8e6799dbce3 1068
Lightvalve 64:a8e6799dbce3 1069 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 110:8dcb1600cb90 1070 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 71:f80dc3970c99 1071 output = output + hout[index1] * output3[index1];
Lightvalve 64:a8e6799dbce3 1072 }
Lightvalve 64:a8e6799dbce3 1073 output = output + bout[index2];
Lightvalve 164:645207e160ca 1074
Lightvalve 64:a8e6799dbce3 1075 }
Lightvalve 71:f80dc3970c99 1076 output = 1.0f/(1.0f+exp(-output));
Lightvalve 99:50159049a518 1077 output_normalized = output;
Lightvalve 66:328e1be06f5d 1078 output = output * 20000.0f - 10000.0f;
Lightvalve 164:645207e160ca 1079
Lightvalve 64:a8e6799dbce3 1080 if(output>=0) {
Lightvalve 64:a8e6799dbce3 1081 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 64:a8e6799dbce3 1082 } else {
Lightvalve 64:a8e6799dbce3 1083 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 63:a2d7c63419c2 1084 }
Lightvalve 85:471334725012 1085
Lightvalve 164:645207e160ca 1086
Lightvalve 67:3995ffeaa786 1087 if(LED==1) {
Lightvalve 67:3995ffeaa786 1088 LED=0;
Lightvalve 67:3995ffeaa786 1089 } else
Lightvalve 67:3995ffeaa786 1090 LED = 1;
Lightvalve 164:645207e160ca 1091
Lightvalve 63:a2d7c63419c2 1092 }
Lightvalve 166:bfc1fd2629d8 1093
Lightvalve 166:bfc1fd2629d8 1094
Lightvalve 165:42c938a40313 1095 /////////////////////////////////////////////////////////////////////RL
Lightvalve 165:42c938a40313 1096 switch (Update_Case) {
Lightvalve 165:42c938a40313 1097 case 0: {
Lightvalve 165:42c938a40313 1098 break;
Lightvalve 165:42c938a40313 1099 }
Lightvalve 165:42c938a40313 1100 case 1: {
Lightvalve 165:42c938a40313 1101 //Network Update(just update and hold network)
Lightvalve 165:42c938a40313 1102 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 165:42c938a40313 1103 float loss_sum = 0.0f;
Lightvalve 170:2f7289dbd488 1104 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 165:42c938a40313 1105 //Calculate Estimated V
Lightvalve 170:2f7289dbd488 1106 //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]};
Lightvalve 170:2f7289dbd488 1107 float temp_array[2] = {state_array[n][0], state_array[n][1]};
Lightvalve 170:2f7289dbd488 1108 V[n] = Critic_Network_Temp(temp_array);
Lightvalve 168:68c7914679ec 1109 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 170:2f7289dbd488 1110 hx_c_sum_array[n][i] = hx_c_sum[i];
Lightvalve 168:68c7914679ec 1111 }
Lightvalve 168:68c7914679ec 1112 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 170:2f7289dbd488 1113 hxh_c_sum_array[n][i] = hxh_c_sum[i];
Lightvalve 168:68c7914679ec 1114 }
Lightvalve 170:2f7289dbd488 1115 hxhh_c_sum_array[n] = hxhh_c_sum;
Lightvalve 172:8e9cf31d63f4 1116
Lightvalve 170:2f7289dbd488 1117 pi[n] = exp(-(action_array[n]-mean_array[n])*(action_array[n]-mean_array[n])/(2.0f*deviation_array[n]*deviation_array[n]))/(sqrt(2.0f*PI)*deviation_array[n]);
Lightvalve 165:42c938a40313 1118 Actor_Network_Old(temp_array);
Lightvalve 170:2f7289dbd488 1119 pi_old[n] = exp(-(action_array[n]-mean_old)*(action_array[n]-mean_old)/(2.0f*deviation_old*deviation_old))/(sqrt(2.0f*PI)*deviation_old);
Lightvalve 170:2f7289dbd488 1120 r[n] = exp(-0.25f * state_array[n][1] * state_array[n][1]);
Lightvalve 170:2f7289dbd488 1121 if(n == batch_size-1) return_G[n] = 0.0f;
Lightvalve 170:2f7289dbd488 1122 else return_G[n] = gamma * return_G[n+1] + r[n];
Lightvalve 170:2f7289dbd488 1123 if(n == batch_size-1) td_target[n] = r[n];
Lightvalve 170:2f7289dbd488 1124 else td_target[n] = r[n] + gamma * V[n+1];
Lightvalve 170:2f7289dbd488 1125 delta[n] = td_target[n] - V[n];
Lightvalve 170:2f7289dbd488 1126 if(n == batch_size-1) advantage[n] = 0.0f;
Lightvalve 170:2f7289dbd488 1127 else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n];
Lightvalve 170:2f7289dbd488 1128 ratio[n] = pi[n]/pi_old[n];
Lightvalve 170:2f7289dbd488 1129 surr1[n] = ratio[n] * advantage[n];
Lightvalve 170:2f7289dbd488 1130 if (ratio[n] > 1.0f + epsilon) {
Lightvalve 170:2f7289dbd488 1131 surr2[n] = (1.0f + epsilon)*advantage[n];
Lightvalve 170:2f7289dbd488 1132 } else if( ratio[n] < 1.0f - epsilon) {
Lightvalve 170:2f7289dbd488 1133 surr2[n] = (1.0f - epsilon)*advantage[n];
Lightvalve 165:42c938a40313 1134 } else {
Lightvalve 170:2f7289dbd488 1135 surr2[n] = ratio[n]*advantage[n];
Lightvalve 165:42c938a40313 1136 }
Lightvalve 170:2f7289dbd488 1137 loss[n] = -min(surr1[n], surr2[n]);
Lightvalve 170:2f7289dbd488 1138 loss_sum = loss_sum + loss[n];
Lightvalve 165:42c938a40313 1139 }
Lightvalve 165:42c938a40313 1140 reward_sum = 0.0f;
Lightvalve 165:42c938a40313 1141 for (int i=0; i<batch_size; i++) {
Lightvalve 165:42c938a40313 1142 reward_sum = reward_sum + r[i];
Lightvalve 165:42c938a40313 1143 }
Lightvalve 165:42c938a40313 1144 logging5 = reward_sum;
Lightvalve 166:bfc1fd2629d8 1145
Lightvalve 166:bfc1fd2629d8 1146
Lightvalve 165:42c938a40313 1147 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 165:42c938a40313 1148 loss_batch = loss_sum;
Lightvalve 165:42c938a40313 1149 //Update Networks
Lightvalve 165:42c938a40313 1150 update_Critic_Networks(state_array);
Lightvalve 165:42c938a40313 1151 update_Actor_Networks(state_array);
Lightvalve 165:42c938a40313 1152 }
Lightvalve 165:42c938a40313 1153 Update_Done_Flag = 1;
Lightvalve 165:42c938a40313 1154 Update_Case = 0;
Lightvalve 165:42c938a40313 1155 //logging1 = V[0];
Lightvalve 166:bfc1fd2629d8 1156
Lightvalve 165:42c938a40313 1157 break;
Lightvalve 165:42c938a40313 1158 }
Lightvalve 165:42c938a40313 1159 case 2: {
Lightvalve 165:42c938a40313 1160 //Network apply to next Network
Lightvalve 165:42c938a40313 1161 Overwirte_Critic_Networks();
Lightvalve 165:42c938a40313 1162 Overwirte_Actor_Networks();
Lightvalve 165:42c938a40313 1163 virt_pos = 10.0f;
Lightvalve 165:42c938a40313 1164 Update_Done_Flag = 1;
Lightvalve 165:42c938a40313 1165 Update_Case = 0;
Lightvalve 165:42c938a40313 1166 break;
Lightvalve 165:42c938a40313 1167 }
Lightvalve 164:645207e160ca 1168
Lightvalve 165:42c938a40313 1169 }
GiJeongKim 0:51c43836c1d7 1170 }
jobuuu 1:e04e563be5ce 1171 }
jobuuu 1:e04e563be5ce 1172
Lightvalve 33:91b17819ec30 1173 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1174 {
Lightvalve 14:8e7590227d22 1175
Lightvalve 13:747daba9cf59 1176 int i = 0;
Lightvalve 47:889798ff9329 1177 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1178 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1179 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 1180 if(i==0) {
Lightvalve 49:3c630b5eba9f 1181 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 55:f4819de54e7a 1182 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 49:3c630b5eba9f 1183 } else {
Lightvalve 55:f4819de54e7a 1184 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 1185 }
Lightvalve 14:8e7590227d22 1186 } else {
Lightvalve 49:3c630b5eba9f 1187 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 55:f4819de54e7a 1188 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 49:3c630b5eba9f 1189 } else {
Lightvalve 55:f4819de54e7a 1190 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 1191 }
Lightvalve 13:747daba9cf59 1192 }
Lightvalve 13:747daba9cf59 1193 break;
Lightvalve 13:747daba9cf59 1194 }
Lightvalve 13:747daba9cf59 1195 }
Lightvalve 14:8e7590227d22 1196 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1197 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1198 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1199 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1200 }
Lightvalve 36:a46e63505ed8 1201
Lightvalve 55:f4819de54e7a 1202 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1203 return Ref_Valve_Pos_FF;
Lightvalve 49:3c630b5eba9f 1204
Lightvalve 13:747daba9cf59 1205 }
jobuuu 6:df07d3491e3a 1206
jobuuu 6:df07d3491e3a 1207
Lightvalve 30:8d561f16383b 1208 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1209 {
Lightvalve 13:747daba9cf59 1210 int i = 0;
Lightvalve 13:747daba9cf59 1211
Lightvalve 38:118df027d851 1212 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1213 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1214 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1215 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1216 }
Lightvalve 38:118df027d851 1217
Lightvalve 87:a7b45368ea0f 1218 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1219 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1220 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1221 valve_pos_err_sum += valve_pos_err;
Lightvalve 87:a7b45368ea0f 1222 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 87:a7b45368ea0f 1223 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1224
Lightvalve 13:747daba9cf59 1225 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 1226
Lightvalve 18:b8adf1582ea3 1227 for(i=0; i<24; i++) {
Lightvalve 87:a7b45368ea0f 1228 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 1229 if(i==0) {
Lightvalve 47:889798ff9329 1230 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 1231 } else {
Lightvalve 47:889798ff9329 1232 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 1233 }
Lightvalve 13:747daba9cf59 1234 break;
Lightvalve 13:747daba9cf59 1235 }
Lightvalve 13:747daba9cf59 1236 }
Lightvalve 57:f308b1656d9c 1237 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1238 }
Lightvalve 13:747daba9cf59 1239
Lightvalve 14:8e7590227d22 1240 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1241 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 1242 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1243 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1244 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1245 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1246 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1247 }; // duty
Lightvalve 65:c2812cf26c38 1248 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 65:c2812cf26c38 1249 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 65:c2812cf26c38 1250 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 65:c2812cf26c38 1251 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 65:c2812cf26c38 1252 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 65:c2812cf26c38 1253 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1254 }; // mV
Lightvalve 13:747daba9cf59 1255
Lightvalve 30:8d561f16383b 1256 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1257 {
Lightvalve 30:8d561f16383b 1258 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1259 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1260 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1261 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1262 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1263 } else {
Lightvalve 13:747daba9cf59 1264 int idx = 0;
Lightvalve 13:747daba9cf59 1265 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1266 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1267 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1268 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1269 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1270 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1271 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1272 break;
Lightvalve 13:747daba9cf59 1273 }
Lightvalve 13:747daba9cf59 1274 }
Lightvalve 13:747daba9cf59 1275 }
Lightvalve 14:8e7590227d22 1276
Lightvalve 13:747daba9cf59 1277 return PWM_duty;
Lightvalve 13:747daba9cf59 1278 }
jobuuu 6:df07d3491e3a 1279
Lightvalve 55:f4819de54e7a 1280
Lightvalve 55:f4819de54e7a 1281
Lightvalve 55:f4819de54e7a 1282
Lightvalve 55:f4819de54e7a 1283
jobuuu 2:a1c0a37df760 1284 /*******************************************************************************
jobuuu 2:a1c0a37df760 1285 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1286 *******************************************************************************/
jobuuu 2:a1c0a37df760 1287
Lightvalve 50:b46bed7fec80 1288 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 50:b46bed7fec80 1289 float DT_TMR4 = (float)DT_20k;
Lightvalve 55:f4819de54e7a 1290 long CNT_TMR4 = 0;
Lightvalve 55:f4819de54e7a 1291 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1292 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1293 {
Lightvalve 19:23b7c1ad8683 1294 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1295
Lightvalve 21:e5f1a43ea6f9 1296 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1297 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1298 ********************************************************/
Lightvalve 13:747daba9cf59 1299
Lightvalve 55:f4819de54e7a 1300 //Encoder
Lightvalve 55:f4819de54e7a 1301 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 55:f4819de54e7a 1302 ENC_UPDATE();
Lightvalve 55:f4819de54e7a 1303 }
Lightvalve 59:bc8c8270f0ab 1304
Lightvalve 59:bc8c8270f0ab 1305 ADC1->CR2 |= 0x40000000;
Lightvalve 56:2eade98630e2 1306 if (SENSING_MODE == 0) {
Lightvalve 56:2eade98630e2 1307 // Torque Sensing (0~210)bar =============================================
Lightvalve 56:2eade98630e2 1308 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 56:2eade98630e2 1309 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 56:2eade98630e2 1310 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 65:c2812cf26c38 1311 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 65:c2812cf26c38 1312
Lightvalve 65:c2812cf26c38 1313
Lightvalve 65:c2812cf26c38 1314 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 65:c2812cf26c38 1315 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 65:c2812cf26c38 1316 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 65:c2812cf26c38 1317 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 65:c2812cf26c38 1318 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 65:c2812cf26c38 1319
Lightvalve 17:1865016ca2e7 1320
Lightvalve 56:2eade98630e2 1321 } else if (SENSING_MODE == 1) {
Lightvalve 56:2eade98630e2 1322 // Pressure Sensing (0~210)bar =============================================
Lightvalve 56:2eade98630e2 1323 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 56:2eade98630e2 1324 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 56:2eade98630e2 1325 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 56:2eade98630e2 1326 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 56:2eade98630e2 1327 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 56:2eade98630e2 1328 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 56:2eade98630e2 1329 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 49:3c630b5eba9f 1330
Lightvalve 56:2eade98630e2 1331 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 56:2eade98630e2 1332 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm
Lightvalve 56:2eade98630e2 1333 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 56:2eade98630e2 1334 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N
Lightvalve 56:2eade98630e2 1335 }
Lightvalve 56:2eade98630e2 1336 }
Lightvalve 59:bc8c8270f0ab 1337
Lightvalve 56:2eade98630e2 1338 // //Pressure sensor A
Lightvalve 56:2eade98630e2 1339 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 56:2eade98630e2 1340 // //while((ADC1->SR & 0b10));
Lightvalve 56:2eade98630e2 1341 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 56:2eade98630e2 1342 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 56:2eade98630e2 1343 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 56:2eade98630e2 1344 // 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 56:2eade98630e2 1345 //
Lightvalve 56:2eade98630e2 1346 //
Lightvalve 56:2eade98630e2 1347 // //Pressure sensor B
Lightvalve 56:2eade98630e2 1348 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 56:2eade98630e2 1349 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 56:2eade98630e2 1350 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 56:2eade98630e2 1351 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1352
Lightvalve 17:1865016ca2e7 1353
Lightvalve 21:e5f1a43ea6f9 1354 //Current
Lightvalve 21:e5f1a43ea6f9 1355 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1356 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1357 //while((ADC3->SR & 0b10));
Lightvalve 50:b46bed7fec80 1358 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1359 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1360 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1361 //cur.sen = raw_cur;
Lightvalve 56:2eade98630e2 1362
Lightvalve 55:f4819de54e7a 1363 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1364 }
Lightvalve 11:82d8768d7351 1365 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1366 }
Lightvalve 19:23b7c1ad8683 1367
Lightvalve 19:23b7c1ad8683 1368
Lightvalve 18:b8adf1582ea3 1369 int j =0;
Lightvalve 52:647072f5307a 1370 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 47:889798ff9329 1371 float DT_TMR3 = (float)DT_5k;
Lightvalve 44:35fa6884d0c6 1372 int cnt_trans = 0;
Lightvalve 47:889798ff9329 1373 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 55:f4819de54e7a 1374 int can_rest =0;
Lightvalve 47:889798ff9329 1375
Lightvalve 11:82d8768d7351 1376 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1377 {
Lightvalve 19:23b7c1ad8683 1378 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 55:f4819de54e7a 1379
Lightvalve 55:f4819de54e7a 1380 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 55:f4819de54e7a 1381 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 55:f4819de54e7a 1382 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 55:f4819de54e7a 1383 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 55:f4819de54e7a 1384 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 55:f4819de54e7a 1385 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 55:f4819de54e7a 1386 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 55:f4819de54e7a 1387 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 55:f4819de54e7a 1388 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 55:f4819de54e7a 1389 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 55:f4819de54e7a 1390 }
Lightvalve 49:3c630b5eba9f 1391
Lightvalve 49:3c630b5eba9f 1392 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 47:889798ff9329 1393 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 44:35fa6884d0c6 1394 cnt_trans++;
Lightvalve 45:2694daea349b 1395 torq.err_sum = 0;
Lightvalve 47:889798ff9329 1396 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 44:35fa6884d0c6 1397 MODE_POS_FT_TRANS = 2;
Lightvalve 49:3c630b5eba9f 1398 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 47:889798ff9329 1399 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 44:35fa6884d0c6 1400 cnt_trans++;
Lightvalve 45:2694daea349b 1401 torq.err_sum = 0;
Lightvalve 47:889798ff9329 1402 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 44:35fa6884d0c6 1403 MODE_POS_FT_TRANS = 0;
Lightvalve 49:3c630b5eba9f 1404 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 56:2eade98630e2 1405 alpha_trans = 1.0f;
Lightvalve 44:35fa6884d0c6 1406 cnt_trans = 0;
Lightvalve 49:3c630b5eba9f 1407 } else {
Lightvalve 56:2eade98630e2 1408 alpha_trans = 0.0f;
Lightvalve 44:35fa6884d0c6 1409 cnt_trans = 0;
Lightvalve 44:35fa6884d0c6 1410 }
Lightvalve 44:35fa6884d0c6 1411
Lightvalve 49:3c630b5eba9f 1412
Lightvalve 55:f4819de54e7a 1413 int UTILITY_MODE = 0;
Lightvalve 55:f4819de54e7a 1414 int CONTROL_MODE = 0;
Lightvalve 56:2eade98630e2 1415
Lightvalve 55:f4819de54e7a 1416 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 55:f4819de54e7a 1417 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 55:f4819de54e7a 1418 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 55:f4819de54e7a 1419 } else {
Lightvalve 55:f4819de54e7a 1420 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 55:f4819de54e7a 1421 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 55:f4819de54e7a 1422 }
Lightvalve 54:6f50d9d3bfee 1423
Lightvalve 54:6f50d9d3bfee 1424
Lightvalve 54:6f50d9d3bfee 1425
Lightvalve 55:f4819de54e7a 1426 // UTILITY MODE ------------------------------------------------------------
Lightvalve 54:6f50d9d3bfee 1427
Lightvalve 55:f4819de54e7a 1428 switch (UTILITY_MODE) {
Lightvalve 55:f4819de54e7a 1429 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1430 break;
Lightvalve 13:747daba9cf59 1431 }
Lightvalve 14:8e7590227d22 1432
Lightvalve 14:8e7590227d22 1433 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1434 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1435 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1436 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1437
Lightvalve 14:8e7590227d22 1438 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1439 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1440 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1441
Lightvalve 82:c355d3e52bf1 1442 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1443
Lightvalve 30:8d561f16383b 1444 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 57:f308b1656d9c 1445 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 49:3c630b5eba9f 1446
Lightvalve 16:903b5a4433b4 1447 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1448 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1449 }
Lightvalve 13:747daba9cf59 1450 } else {
Lightvalve 56:2eade98630e2 1451 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1452 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1453 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1454 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1455
Lightvalve 165:42c938a40313 1456 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 1457 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1458
Lightvalve 30:8d561f16383b 1459 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1460
Lightvalve 13:747daba9cf59 1461 }
Lightvalve 14:8e7590227d22 1462 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1463 break;
Lightvalve 19:23b7c1ad8683 1464 }
Lightvalve 14:8e7590227d22 1465
Lightvalve 49:3c630b5eba9f 1466 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 49:3c630b5eba9f 1467 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 49:3c630b5eba9f 1468 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 49:3c630b5eba9f 1469 // else temp_time = 0;
Lightvalve 49:3c630b5eba9f 1470 // }
Lightvalve 49:3c630b5eba9f 1471 // if (need_enc_init) {
Lightvalve 49:3c630b5eba9f 1472 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 49:3c630b5eba9f 1473 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1474 // pos_plus_end = pos.sen;
Lightvalve 49:3c630b5eba9f 1475 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1476 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1477 // pos_minus_end = pos.sen;
Lightvalve 49:3c630b5eba9f 1478 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 49:3c630b5eba9f 1479 // temp_time = TMR_FREQ_5k;
Lightvalve 49:3c630b5eba9f 1480 // }
Lightvalve 49:3c630b5eba9f 1481 //
Lightvalve 49:3c630b5eba9f 1482 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 49:3c630b5eba9f 1483 // V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen);
Lightvalve 49:3c630b5eba9f 1484 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 49:3c630b5eba9f 1485 //
Lightvalve 49:3c630b5eba9f 1486 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 49:3c630b5eba9f 1487 // V_out = 0;
Lightvalve 49:3c630b5eba9f 1488 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 49:3c630b5eba9f 1489 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 49:3c630b5eba9f 1490 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 49:3c630b5eba9f 1491 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 49:3c630b5eba9f 1492 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 49:3c630b5eba9f 1493 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 49:3c630b5eba9f 1494 // CUR_VELOCITY_sum = 0;
Lightvalve 49:3c630b5eba9f 1495 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 49:3c630b5eba9f 1496 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 49:3c630b5eba9f 1497 //
Lightvalve 49:3c630b5eba9f 1498 // // Position of Dead Zone
Lightvalve 49:3c630b5eba9f 1499 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 49:3c630b5eba9f 1500 // // | / | / |/
Lightvalve 49:3c630b5eba9f 1501 // // | ______/ ___|___/ ______/|
Lightvalve 49:3c630b5eba9f 1502 // // |/ / | / |
Lightvalve 49:3c630b5eba9f 1503 // // /| / | / |
Lightvalve 49:3c630b5eba9f 1504 // // 0V 0V 0V
Lightvalve 49:3c630b5eba9f 1505 //
Lightvalve 49:3c630b5eba9f 1506 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 49:3c630b5eba9f 1507 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 49:3c630b5eba9f 1508 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 49:3c630b5eba9f 1509 // } else {
Lightvalve 49:3c630b5eba9f 1510 // V_out -= DZ_dir;
Lightvalve 49:3c630b5eba9f 1511 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 49:3c630b5eba9f 1512 // CUR_VELOCITY_sum = 0;
Lightvalve 49:3c630b5eba9f 1513 // }
Lightvalve 49:3c630b5eba9f 1514 // if (DZ_temp_cnt == 5) {
Lightvalve 49:3c630b5eba9f 1515 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 49:3c630b5eba9f 1516 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 49:3c630b5eba9f 1517 // DZ_dir = -DZ_dir;
Lightvalve 49:3c630b5eba9f 1518 // DZ_temp_cnt = 0;
Lightvalve 49:3c630b5eba9f 1519 // DZ_temp_cnt2++;
Lightvalve 49:3c630b5eba9f 1520 // }
Lightvalve 49:3c630b5eba9f 1521 // } else {
Lightvalve 49:3c630b5eba9f 1522 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 49:3c630b5eba9f 1523 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 49:3c630b5eba9f 1524 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 49:3c630b5eba9f 1525 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 49:3c630b5eba9f 1526 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 49:3c630b5eba9f 1527 // }
Lightvalve 49:3c630b5eba9f 1528 // V_out = 0;
Lightvalve 49:3c630b5eba9f 1529 //
Lightvalve 49:3c630b5eba9f 1530 // ROM_RESET_DATA();
Lightvalve 49:3c630b5eba9f 1531 //
Lightvalve 49:3c630b5eba9f 1532 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 49:3c630b5eba9f 1533 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 49:3c630b5eba9f 1534 //
Lightvalve 49:3c630b5eba9f 1535 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 49:3c630b5eba9f 1536 // DZ_temp_cnt2 = 0;
Lightvalve 49:3c630b5eba9f 1537 // }
Lightvalve 49:3c630b5eba9f 1538 // }
Lightvalve 49:3c630b5eba9f 1539 // TMR3_COUNT_DEADZONE++;
Lightvalve 49:3c630b5eba9f 1540 // break;
Lightvalve 49:3c630b5eba9f 1541 // }
Lightvalve 14:8e7590227d22 1542
Lightvalve 14:8e7590227d22 1543 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1544 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1545 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1546 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1547 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 57:f308b1656d9c 1548 pos.ref = pos.sen;
Lightvalve 57:f308b1656d9c 1549 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1550 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1551 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 65:c2812cf26c38 1552 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1553 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1554 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1555 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1556 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1557 }
Lightvalve 29:69f3f5445d6d 1558 cnt_findhome++;
Lightvalve 14:8e7590227d22 1559
Lightvalve 29:69f3f5445d6d 1560 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1561 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1562 } else {
Lightvalve 29:69f3f5445d6d 1563 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1564 }
Lightvalve 19:23b7c1ad8683 1565
Lightvalve 55:f4819de54e7a 1566 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1567 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 65:c2812cf26c38 1568 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 65:c2812cf26c38 1569 else pos.ref = pos.ref - 12.0f;
Lightvalve 59:bc8c8270f0ab 1570
Lightvalve 57:f308b1656d9c 1571 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 57:f308b1656d9c 1572 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 57:f308b1656d9c 1573 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 57:f308b1656d9c 1574 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 57:f308b1656d9c 1575 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:bc8c8270f0ab 1576
Lightvalve 57:f308b1656d9c 1577 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 57:f308b1656d9c 1578 alpha_trans = 0.0f;
Lightvalve 59:bc8c8270f0ab 1579
Lightvalve 34:bb2ca2fc2a8e 1580
Lightvalve 29:69f3f5445d6d 1581 } else {
Lightvalve 29:69f3f5445d6d 1582 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 65:c2812cf26c38 1583 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1584 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1585 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1586 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1587 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1588 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1589 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1590 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1591 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1592 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 65:c2812cf26c38 1593
Lightvalve 65:c2812cf26c38 1594
Lightvalve 65:c2812cf26c38 1595 cnt_findhome = 0;
Lightvalve 65:c2812cf26c38 1596 pos.ref = 0.0f;
Lightvalve 65:c2812cf26c38 1597 vel.ref = 0.0f;
Lightvalve 65:c2812cf26c38 1598 pos.ref_home_pos = 0.0f;
Lightvalve 65:c2812cf26c38 1599 vel.ref_home_pos = 0.0f;
Lightvalve 65:c2812cf26c38 1600 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 65:c2812cf26c38 1601 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 164:645207e160ca 1602
Lightvalve 164:645207e160ca 1603
Lightvalve 29:69f3f5445d6d 1604 }
Lightvalve 29:69f3f5445d6d 1605 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1606 int T_move = 2*TMR_FREQ_5k;
Lightvalve 57:f308b1656d9c 1607 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 65:c2812cf26c38 1608 //pos.ref = 0.0f;
Lightvalve 57:f308b1656d9c 1609 vel.ref = 0.0f;
Lightvalve 164:645207e160ca 1610
Lightvalve 29:69f3f5445d6d 1611 // input for position control
Lightvalve 164:645207e160ca 1612
Lightvalve 65:c2812cf26c38 1613 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 65:c2812cf26c38 1614 alpha_trans = 0.0f;
Lightvalve 59:bc8c8270f0ab 1615
Lightvalve 65:c2812cf26c38 1616 double torq_ref = 0.0f;
Lightvalve 65:c2812cf26c38 1617 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 65:c2812cf26c38 1618 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 65:c2812cf26c38 1619 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 65:c2812cf26c38 1620
Lightvalve 65:c2812cf26c38 1621 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 65:c2812cf26c38 1622
Lightvalve 65:c2812cf26c38 1623 double I_REF_POS = 0.0f;
Lightvalve 65:c2812cf26c38 1624 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 65:c2812cf26c38 1625 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 65:c2812cf26c38 1626
Lightvalve 65:c2812cf26c38 1627 double temp_vel_pos = 0.0f;
Lightvalve 65:c2812cf26c38 1628 double temp_vel_torq = 0.0f;
Lightvalve 65:c2812cf26c38 1629 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 65:c2812cf26c38 1630
Lightvalve 67:3995ffeaa786 1631 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 65:c2812cf26c38 1632 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 65:c2812cf26c38 1633 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:3995ffeaa786 1634 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 65:c2812cf26c38 1635 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 65:c2812cf26c38 1636 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 65:c2812cf26c38 1637 }
Lightvalve 65:c2812cf26c38 1638 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 65:c2812cf26c38 1639 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 65:c2812cf26c38 1640
Lightvalve 65:c2812cf26c38 1641 I_REF = I_REF_POS;
Lightvalve 65:c2812cf26c38 1642
Lightvalve 65:c2812cf26c38 1643
Lightvalve 65:c2812cf26c38 1644
Lightvalve 65:c2812cf26c38 1645 } else {
Lightvalve 65:c2812cf26c38 1646 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 65:c2812cf26c38 1647 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 65:c2812cf26c38 1648
Lightvalve 65:c2812cf26c38 1649 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 65:c2812cf26c38 1650 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 65:c2812cf26c38 1651 } else {
Lightvalve 65:c2812cf26c38 1652 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 65:c2812cf26c38 1653 }
Lightvalve 65:c2812cf26c38 1654
Lightvalve 65:c2812cf26c38 1655 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 65:c2812cf26c38 1656
Lightvalve 65:c2812cf26c38 1657 V_out = (float) Vout.ref;
Lightvalve 65:c2812cf26c38 1658
Lightvalve 65:c2812cf26c38 1659 }
Lightvalve 65:c2812cf26c38 1660
Lightvalve 65:c2812cf26c38 1661
Lightvalve 164:645207e160ca 1662
Lightvalve 164:645207e160ca 1663
Lightvalve 57:f308b1656d9c 1664 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 57:f308b1656d9c 1665 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 57:f308b1656d9c 1666 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 57:f308b1656d9c 1667 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 57:f308b1656d9c 1668 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1669
Lightvalve 29:69f3f5445d6d 1670 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1671 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1672 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1673 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1674 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1675 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1676 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1677 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1678 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 55:f4819de54e7a 1679 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1680 }
Lightvalve 13:747daba9cf59 1681 }
Lightvalve 19:23b7c1ad8683 1682
Lightvalve 13:747daba9cf59 1683 break;
Lightvalve 13:747daba9cf59 1684 }
Lightvalve 14:8e7590227d22 1685
Lightvalve 49:3c630b5eba9f 1686 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 49:3c630b5eba9f 1687 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 49:3c630b5eba9f 1688 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 49:3c630b5eba9f 1689 // else {
Lightvalve 49:3c630b5eba9f 1690 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1691 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 49:3c630b5eba9f 1692 // }
Lightvalve 49:3c630b5eba9f 1693 // }
Lightvalve 49:3c630b5eba9f 1694 // if (need_enc_init) {
Lightvalve 49:3c630b5eba9f 1695 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 49:3c630b5eba9f 1696 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1697 // pos_plus_end = pos.sen;
Lightvalve 49:3c630b5eba9f 1698 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1699 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1700 // pos_minus_end = pos.sen;
Lightvalve 49:3c630b5eba9f 1701 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1702 // need_enc_init = false;
Lightvalve 49:3c630b5eba9f 1703 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 49:3c630b5eba9f 1704 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 49:3c630b5eba9f 1705 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 49:3c630b5eba9f 1706 // }
Lightvalve 49:3c630b5eba9f 1707 // temp_time = TMR_FREQ_5k;
Lightvalve 49:3c630b5eba9f 1708 // }
Lightvalve 49:3c630b5eba9f 1709 // TMR3_COUNT_FLOWRATE++;
Lightvalve 49:3c630b5eba9f 1710 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 49:3c630b5eba9f 1711 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 49:3c630b5eba9f 1712 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 49:3c630b5eba9f 1713 // V_out = VALVE_VOLTAGE;
Lightvalve 49:3c630b5eba9f 1714 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 49:3c630b5eba9f 1715 // fl_temp_cnt++;
Lightvalve 49:3c630b5eba9f 1716 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 49:3c630b5eba9f 1717 // 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 49:3c630b5eba9f 1718 // // 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 49:3c630b5eba9f 1719 // fl_temp_cnt2++;
Lightvalve 49:3c630b5eba9f 1720 // }
Lightvalve 49:3c630b5eba9f 1721 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 49:3c630b5eba9f 1722 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 49:3c630b5eba9f 1723 // V_out = VALVE_VOLTAGE;
Lightvalve 49:3c630b5eba9f 1724 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 49:3c630b5eba9f 1725 // fl_temp_cnt++;
Lightvalve 49:3c630b5eba9f 1726 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 49:3c630b5eba9f 1727 // 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 49:3c630b5eba9f 1728 // // 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 49:3c630b5eba9f 1729 // fl_temp_cnt2++;
Lightvalve 49:3c630b5eba9f 1730 // }
Lightvalve 49:3c630b5eba9f 1731 // }
Lightvalve 49:3c630b5eba9f 1732 // if (fl_temp_cnt2 == 100) {
Lightvalve 49:3c630b5eba9f 1733 //
Lightvalve 49:3c630b5eba9f 1734 // ROM_RESET_DATA();
Lightvalve 49:3c630b5eba9f 1735 //
Lightvalve 49:3c630b5eba9f 1736 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 49:3c630b5eba9f 1737 // cur_vel_sum = 0;
Lightvalve 49:3c630b5eba9f 1738 // fl_temp_cnt = 0;
Lightvalve 49:3c630b5eba9f 1739 // fl_temp_cnt2 = 0;
Lightvalve 49:3c630b5eba9f 1740 // flag_flowrate++;
Lightvalve 49:3c630b5eba9f 1741 // }
Lightvalve 49:3c630b5eba9f 1742 // if (flag_flowrate == 10) {
Lightvalve 49:3c630b5eba9f 1743 // V_out = 0;
Lightvalve 49:3c630b5eba9f 1744 // flag_flowrate = 0;
Lightvalve 49:3c630b5eba9f 1745 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 49:3c630b5eba9f 1746 // valve_gain_repeat_cnt++;
Lightvalve 49:3c630b5eba9f 1747 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 49:3c630b5eba9f 1748 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 49:3c630b5eba9f 1749 // valve_gain_repeat_cnt = 0;
Lightvalve 49:3c630b5eba9f 1750 // }
Lightvalve 49:3c630b5eba9f 1751 //
Lightvalve 49:3c630b5eba9f 1752 // }
Lightvalve 49:3c630b5eba9f 1753 // break;
Lightvalve 49:3c630b5eba9f 1754 // }
Lightvalve 49:3c630b5eba9f 1755 //
Lightvalve 49:3c630b5eba9f 1756 // }
Lightvalve 14:8e7590227d22 1757 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1758 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1759 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1760 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1761 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1762
Lightvalve 14:8e7590227d22 1763 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1764 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1765 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1766 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1767 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1768
Lightvalve 38:118df027d851 1769 float VREF_NullingGain = 0.0003f;
Lightvalve 56:2eade98630e2 1770 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 56:2eade98630e2 1771 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1772
Lightvalve 30:8d561f16383b 1773 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1774 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1775 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1776 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1777
Lightvalve 30:8d561f16383b 1778 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1779 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1780 }
Lightvalve 13:747daba9cf59 1781 } else {
Lightvalve 55:f4819de54e7a 1782 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1783 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1784 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1785 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1786 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1787 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1788
Lightvalve 165:42c938a40313 1789 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 1790 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 165:42c938a40313 1791 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1792
Lightvalve 30:8d561f16383b 1793 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1794 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1795 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1796 }
Lightvalve 14:8e7590227d22 1797 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1798 break;
Lightvalve 13:747daba9cf59 1799 }
Lightvalve 14:8e7590227d22 1800
Lightvalve 49:3c630b5eba9f 1801 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 49:3c630b5eba9f 1802 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1803 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1804 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1805 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 49:3c630b5eba9f 1806 // }
Lightvalve 49:3c630b5eba9f 1807 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1808 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1809 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1810 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 49:3c630b5eba9f 1811 // }
Lightvalve 49:3c630b5eba9f 1812 // } else {
Lightvalve 49:3c630b5eba9f 1813 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 49:3c630b5eba9f 1814 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 49:3c630b5eba9f 1815 // V_out = 0;
Lightvalve 49:3c630b5eba9f 1816 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 49:3c630b5eba9f 1817 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 49:3c630b5eba9f 1818 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 49:3c630b5eba9f 1819 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 49:3c630b5eba9f 1820 // CUR_PRES_A_sum = 0;
Lightvalve 49:3c630b5eba9f 1821 // CUR_PRES_B_sum = 0;
Lightvalve 49:3c630b5eba9f 1822 // CUR_PRES_A_mean = 0;
Lightvalve 49:3c630b5eba9f 1823 // CUR_PRES_B_mean = 0;
Lightvalve 49:3c630b5eba9f 1824 //
Lightvalve 49:3c630b5eba9f 1825 // ROM_RESET_DATA();
Lightvalve 49:3c630b5eba9f 1826 //
Lightvalve 49:3c630b5eba9f 1827 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 49:3c630b5eba9f 1828 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 49:3c630b5eba9f 1829 // }
Lightvalve 49:3c630b5eba9f 1830 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 49:3c630b5eba9f 1831 // break;
Lightvalve 49:3c630b5eba9f 1832 // }
Lightvalve 19:23b7c1ad8683 1833
Lightvalve 49:3c630b5eba9f 1834 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 49:3c630b5eba9f 1835 // 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 49:3c630b5eba9f 1836 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 49:3c630b5eba9f 1837 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1838 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 49:3c630b5eba9f 1839 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 49:3c630b5eba9f 1840 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 49:3c630b5eba9f 1841 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 49:3c630b5eba9f 1842 // V_out = 0.0f;
Lightvalve 49:3c630b5eba9f 1843 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 49:3c630b5eba9f 1844 // }
Lightvalve 49:3c630b5eba9f 1845 // break;
Lightvalve 49:3c630b5eba9f 1846 // }
Lightvalve 14:8e7590227d22 1847
Lightvalve 14:8e7590227d22 1848 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 57:f308b1656d9c 1849 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1850 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1851
Lightvalve 14:8e7590227d22 1852 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 58:64181f1d3e60 1853 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1854 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 57:f308b1656d9c 1855 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1856 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1857 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1858 data_num = 0;
Lightvalve 14:8e7590227d22 1859 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1860 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1861 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1862 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 57:f308b1656d9c 1863 Vout.ref = 0.0f;
Lightvalve 57:f308b1656d9c 1864 } else {
Lightvalve 13:747daba9cf59 1865 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1866 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1867 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1868 }
Lightvalve 14:8e7590227d22 1869
Lightvalve 17:1865016ca2e7 1870 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1871 int i;
Lightvalve 13:747daba9cf59 1872 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1873 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1874 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1875 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1876 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1877 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1878 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1879 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1880 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1881 }
Lightvalve 13:747daba9cf59 1882 }
Lightvalve 165:42c938a40313 1883 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 1884 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 165:42c938a40313 1885 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 165:42c938a40313 1886 for(int i=0; i<25; i++) {
Lightvalve 165:42c938a40313 1887 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 165:42c938a40313 1888 }
Lightvalve 13:747daba9cf59 1889 ID_index = 0;
Lightvalve 55:f4819de54e7a 1890 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1891 }
Lightvalve 14:8e7590227d22 1892
Lightvalve 14:8e7590227d22 1893
Lightvalve 13:747daba9cf59 1894 break;
Lightvalve 13:747daba9cf59 1895 }
Lightvalve 14:8e7590227d22 1896
Lightvalve 14:8e7590227d22 1897 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 57:f308b1656d9c 1898 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1899 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 1900 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1901 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1902 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1903 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1904 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1905 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 1906 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1907 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1908 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1909 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1910 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 1911 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1912 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 1913 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1914 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 1915 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1916 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 1917 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 1918 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 1919 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 1920 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 1921 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1922 data_num = 0;
Lightvalve 14:8e7590227d22 1923
Lightvalve 30:8d561f16383b 1924 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1925 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1926 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1927
Lightvalve 30:8d561f16383b 1928 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1929 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1930 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 58:64181f1d3e60 1931
Lightvalve 30:8d561f16383b 1932 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1933 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1934 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1935 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1936
Lightvalve 58:64181f1d3e60 1937 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 1938 DZ_case = 1;
Lightvalve 58:64181f1d3e60 1939 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 1940 DZ_case = -1;
Lightvalve 14:8e7590227d22 1941 } else {
Lightvalve 13:747daba9cf59 1942 DZ_case = 0;
Lightvalve 13:747daba9cf59 1943 }
Lightvalve 59:bc8c8270f0ab 1944
Lightvalve 58:64181f1d3e60 1945 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 49:3c630b5eba9f 1946
Lightvalve 13:747daba9cf59 1947 first_check = 1;
Lightvalve 13:747daba9cf59 1948 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1949 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1950 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 1951 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 1952 DZ_index = 1;
Lightvalve 14:8e7590227d22 1953
Lightvalve 13:747daba9cf59 1954 }
Lightvalve 19:23b7c1ad8683 1955 } else {
Lightvalve 14:8e7590227d22 1956 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 1957 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 58:64181f1d3e60 1958 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 58:64181f1d3e60 1959 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 58:64181f1d3e60 1960 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 1961 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1962 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1963 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1964 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1965 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1966 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1967 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1968 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1969 }
Lightvalve 14:8e7590227d22 1970 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 58:64181f1d3e60 1971
Lightvalve 30:8d561f16383b 1972 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1973 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1974 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1975
Lightvalve 14:8e7590227d22 1976 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 1977 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 1978 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 1979 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 1980 } else {
Lightvalve 13:747daba9cf59 1981 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 1982 }
Lightvalve 14:8e7590227d22 1983
Lightvalve 13:747daba9cf59 1984 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1985 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 1986 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 1987 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 1988 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 1989 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 1990 DZ_index = 1;
Lightvalve 13:747daba9cf59 1991 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1992 }
Lightvalve 13:747daba9cf59 1993 }
Lightvalve 14:8e7590227d22 1994 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 1995 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 58:64181f1d3e60 1996 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 58:64181f1d3e60 1997 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 58:64181f1d3e60 1998 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 1999 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2000 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2001 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 58:64181f1d3e60 2002 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2003 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2004 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2005 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2006 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2007 }
Lightvalve 14:8e7590227d22 2008 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:bc8c8270f0ab 2009
Lightvalve 30:8d561f16383b 2010 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2011 Vout.ref = 0.0f;
Lightvalve 57:f308b1656d9c 2012 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2013 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2014 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2015
Lightvalve 14:8e7590227d22 2016 if((FINAL_POS - START_POS)>100) {
Lightvalve 58:64181f1d3e60 2017 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2018 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 58:64181f1d3e60 2019 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2020 } else {
Lightvalve 58:64181f1d3e60 2021 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2022 }
Lightvalve 14:8e7590227d22 2023
Lightvalve 13:747daba9cf59 2024 VALVE_DZ_timer = 0;
Lightvalve 58:64181f1d3e60 2025 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2026 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2027 SECOND_DZ = valve_pos.ref;
Lightvalve 55:f4819de54e7a 2028 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2029 first_check = 0;
Lightvalve 33:91b17819ec30 2030 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2031 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2032
Lightvalve 165:42c938a40313 2033 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 2034 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 165:42c938a40313 2035 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 165:42c938a40313 2036 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2037
Lightvalve 58:64181f1d3e60 2038 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2039 DZ_index = 1;
Lightvalve 13:747daba9cf59 2040 }
Lightvalve 13:747daba9cf59 2041 }
Lightvalve 14:8e7590227d22 2042 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2043 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 58:64181f1d3e60 2044 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 58:64181f1d3e60 2045 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 58:64181f1d3e60 2046 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2047 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2048 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2049 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2050 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2051 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2052 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2053 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2054 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2055 }
Lightvalve 14:8e7590227d22 2056 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:bc8c8270f0ab 2057
Lightvalve 30:8d561f16383b 2058 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2059 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2060 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2061
Lightvalve 14:8e7590227d22 2062 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2063 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2064 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2065 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2066 } else {
Lightvalve 13:747daba9cf59 2067 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2068 }
Lightvalve 13:747daba9cf59 2069 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2070 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2071 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2072 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2073 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2074 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2075 DZ_index = 1;
Lightvalve 13:747daba9cf59 2076 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2077 }
Lightvalve 13:747daba9cf59 2078 }
Lightvalve 14:8e7590227d22 2079 } else {
Lightvalve 30:8d561f16383b 2080 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 58:64181f1d3e60 2081 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 58:64181f1d3e60 2082 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 58:64181f1d3e60 2083 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2084 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2085 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2086 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2087 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2088 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2089 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2090 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2091 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2092 }
Lightvalve 14:8e7590227d22 2093 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 58:64181f1d3e60 2094
Lightvalve 30:8d561f16383b 2095 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2096 Vout.ref = 0.0f;
Lightvalve 57:f308b1656d9c 2097 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2098 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2099 FINAL_POS = pos.sen;
Lightvalve 59:bc8c8270f0ab 2100
Lightvalve 58:64181f1d3e60 2101 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2102 DZ_DIRECTION = 1;
Lightvalve 58:64181f1d3e60 2103 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 58:64181f1d3e60 2104 DZ_DIRECTION = -1;
Lightvalve 58:64181f1d3e60 2105 } else {
Lightvalve 13:747daba9cf59 2106 DZ_DIRECTION = 1;
Lightvalve 58:64181f1d3e60 2107 }
Lightvalve 14:8e7590227d22 2108
Lightvalve 13:747daba9cf59 2109 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2110 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2111 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2112 SECOND_DZ = valve_pos.ref;
Lightvalve 55:f4819de54e7a 2113 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2114 first_check = 0;
Lightvalve 33:91b17819ec30 2115 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2116 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 59:bc8c8270f0ab 2117
Lightvalve 165:42c938a40313 2118 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 2119 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 165:42c938a40313 2120 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 165:42c938a40313 2121 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2122
Lightvalve 55:f4819de54e7a 2123 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2124 DZ_index = 1;
Lightvalve 13:747daba9cf59 2125 }
Lightvalve 13:747daba9cf59 2126 }
Lightvalve 13:747daba9cf59 2127 }
Lightvalve 14:8e7590227d22 2128 }
Lightvalve 13:747daba9cf59 2129 break;
Lightvalve 13:747daba9cf59 2130 }
Lightvalve 14:8e7590227d22 2131
Lightvalve 14:8e7590227d22 2132 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 57:f308b1656d9c 2133 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2134 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2135 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2136 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2137 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2138 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2139 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2140 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2141 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2142 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2143 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2144 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2145 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2146 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 57:f308b1656d9c 2147 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2148 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2149 first_check = 1;
Lightvalve 13:747daba9cf59 2150 VALVE_FR_timer = 0;
Lightvalve 55:f4819de54e7a 2151 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2152 ID_index = 0;
Lightvalve 13:747daba9cf59 2153 max_check = 0;
Lightvalve 13:747daba9cf59 2154 min_check = 0;
Lightvalve 13:747daba9cf59 2155 }
Lightvalve 14:8e7590227d22 2156 } else {
Lightvalve 30:8d561f16383b 2157 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f308b1656d9c 2158 //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 57:f308b1656d9c 2159 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 57:f308b1656d9c 2160 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2161 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2162 data_num = 0;
Lightvalve 55:f4819de54e7a 2163 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2164
Lightvalve 14:8e7590227d22 2165 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2166 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2167 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 55:f4819de54e7a 2168 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2169 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2170 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2171 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2172 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2173 one_period_end = 1;
Lightvalve 13:747daba9cf59 2174 }
Lightvalve 30:8d561f16383b 2175 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2176 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2177 one_period_end = 1;
Lightvalve 57:f308b1656d9c 2178 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2179 }
Lightvalve 14:8e7590227d22 2180
Lightvalve 14:8e7590227d22 2181 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2182 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2183 max_check = 1;
Lightvalve 14:8e7590227d22 2184 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2185 min_check = 1;
Lightvalve 13:747daba9cf59 2186 }
Lightvalve 13:747daba9cf59 2187 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2188
Lightvalve 13:747daba9cf59 2189 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2190 one_period_end = 0;
Lightvalve 13:747daba9cf59 2191 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2192 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2193 }
Lightvalve 14:8e7590227d22 2194
Lightvalve 14:8e7590227d22 2195 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2196
Lightvalve 13:747daba9cf59 2197 VALVE_POS_NUM = ID_index;
Lightvalve 165:42c938a40313 2198 // ROM_RESET_DATA();
Lightvalve 165:42c938a40313 2199 for(int i=0; i<100; i++) {
Lightvalve 165:42c938a40313 2200 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 165:42c938a40313 2201 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 165:42c938a40313 2202 }
Lightvalve 13:747daba9cf59 2203 ID_index = 0;
Lightvalve 13:747daba9cf59 2204 first_check = 0;
Lightvalve 13:747daba9cf59 2205 VALVE_FR_timer = 0;
Lightvalve 55:f4819de54e7a 2206 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 48:e7bcfc244d40 2207 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2208 }
Lightvalve 13:747daba9cf59 2209 }
Lightvalve 13:747daba9cf59 2210 break;
Lightvalve 13:747daba9cf59 2211 }
Lightvalve 56:2eade98630e2 2212
Lightvalve 55:f4819de54e7a 2213 case MODE_SYSTEM_ID: {
Lightvalve 164:645207e160ca 2214 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 55:f4819de54e7a 2215 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 55:f4819de54e7a 2216 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 55:f4819de54e7a 2217 cnt_sysid++;
Lightvalve 55:f4819de54e7a 2218 if (freq_sysid_Iref >= 300) {
Lightvalve 55:f4819de54e7a 2219 cnt_sysid = 0;
Lightvalve 55:f4819de54e7a 2220 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 55:f4819de54e7a 2221 }
Lightvalve 55:f4819de54e7a 2222 break;
Lightvalve 55:f4819de54e7a 2223 }
Lightvalve 56:2eade98630e2 2224
Lightvalve 164:645207e160ca 2225 case MODE_FREQ_TEST: {
Lightvalve 164:645207e160ca 2226 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 164:645207e160ca 2227 if(valve_pos_ref >= 0) {
Lightvalve 164:645207e160ca 2228 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 164:645207e160ca 2229 } else {
Lightvalve 164:645207e160ca 2230 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 164:645207e160ca 2231 }
Lightvalve 164:645207e160ca 2232 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 164:645207e160ca 2233 if(value>=(float) VALVE_CENTER) {
Lightvalve 164:645207e160ca 2234 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2235 } else {
Lightvalve 164:645207e160ca 2236 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2237 }
Lightvalve 56:2eade98630e2 2238
Lightvalve 164:645207e160ca 2239 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 164:645207e160ca 2240 cnt_freq_test++;
Lightvalve 164:645207e160ca 2241 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 164:645207e160ca 2242 buffer_data_size = cnt_freq_test;
Lightvalve 164:645207e160ca 2243 cnt_freq_test = 0;
Lightvalve 164:645207e160ca 2244 cnt_send_buffer = 0;
Lightvalve 164:645207e160ca 2245 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 164:645207e160ca 2246 if (freq_test_valve_ref >= 400) {
Lightvalve 164:645207e160ca 2247 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2248 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2249 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 164:645207e160ca 2250 }
Lightvalve 164:645207e160ca 2251 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2252 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 165:42c938a40313 2253
Lightvalve 164:645207e160ca 2254 }
Lightvalve 164:645207e160ca 2255 break;
Lightvalve 164:645207e160ca 2256 }
Lightvalve 164:645207e160ca 2257 case MODE_SEND_BUFFER: {
Lightvalve 164:645207e160ca 2258 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 164:645207e160ca 2259 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 164:645207e160ca 2260 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 164:645207e160ca 2261 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 164:645207e160ca 2262 // }
Lightvalve 164:645207e160ca 2263 // cnt_send_buffer++;
Lightvalve 164:645207e160ca 2264 // }
Lightvalve 164:645207e160ca 2265
Lightvalve 164:645207e160ca 2266 break;
Lightvalve 164:645207e160ca 2267 }
Lightvalve 164:645207e160ca 2268 case MODE_SEND_OVER: {
Lightvalve 164:645207e160ca 2269 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 164:645207e160ca 2270 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2271 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2272 break;
Lightvalve 164:645207e160ca 2273 }
Lightvalve 165:42c938a40313 2274
Lightvalve 164:645207e160ca 2275 case MODE_STEP_TEST: {
Lightvalve 164:645207e160ca 2276 float valve_pos_ref = 0.0f;
Lightvalve 165:42c938a40313 2277 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 164:645207e160ca 2278 valve_pos_ref = 0.0f;
Lightvalve 165:42c938a40313 2279 } else {
Lightvalve 164:645207e160ca 2280 valve_pos_ref = 10000.0f;
Lightvalve 164:645207e160ca 2281 }
Lightvalve 164:645207e160ca 2282 if(valve_pos_ref >= 0) {
Lightvalve 164:645207e160ca 2283 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 164:645207e160ca 2284 } else {
Lightvalve 164:645207e160ca 2285 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 164:645207e160ca 2286 }
Lightvalve 164:645207e160ca 2287 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 164:645207e160ca 2288 if(value>=(float) VALVE_CENTER) {
Lightvalve 164:645207e160ca 2289 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2290 } else {
Lightvalve 164:645207e160ca 2291 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2292 }
Lightvalve 164:645207e160ca 2293
Lightvalve 164:645207e160ca 2294 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 164:645207e160ca 2295 cnt_step_test++;
Lightvalve 165:42c938a40313 2296 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 164:645207e160ca 2297 buffer_data_size = cnt_step_test;
Lightvalve 164:645207e160ca 2298 cnt_step_test = 0;
Lightvalve 164:645207e160ca 2299 cnt_send_buffer = 0;
Lightvalve 164:645207e160ca 2300 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 164:645207e160ca 2301 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2302 }
Lightvalve 164:645207e160ca 2303 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 164:645207e160ca 2304 // {
Lightvalve 164:645207e160ca 2305 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2306 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 164:645207e160ca 2307 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 164:645207e160ca 2308 // }
Lightvalve 165:42c938a40313 2309
Lightvalve 164:645207e160ca 2310 break;
Lightvalve 164:645207e160ca 2311 }
Lightvalve 55:f4819de54e7a 2312
Lightvalve 55:f4819de54e7a 2313 default:
Lightvalve 55:f4819de54e7a 2314 break;
Lightvalve 55:f4819de54e7a 2315 }
Lightvalve 55:f4819de54e7a 2316
Lightvalve 55:f4819de54e7a 2317 // CONTROL MODE ------------------------------------------------------------
Lightvalve 55:f4819de54e7a 2318
Lightvalve 55:f4819de54e7a 2319 switch (CONTROL_MODE) {
Lightvalve 55:f4819de54e7a 2320 case MODE_NO_ACT: {
Lightvalve 55:f4819de54e7a 2321 V_out = 0.0f;
Lightvalve 55:f4819de54e7a 2322 break;
Lightvalve 55:f4819de54e7a 2323 }
Lightvalve 55:f4819de54e7a 2324
Lightvalve 55:f4819de54e7a 2325 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 65:c2812cf26c38 2326 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 55:f4819de54e7a 2327 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 57:f308b1656d9c 2328 V_out = Vout.ref;
Lightvalve 65:c2812cf26c38 2329 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 65:c2812cf26c38 2330 V_out = valve_pos.ref;
Lightvalve 164:645207e160ca 2331 } else {
Lightvalve 65:c2812cf26c38 2332 I_REF = valve_pos.ref * 0.001f;
Lightvalve 55:f4819de54e7a 2333 }
Lightvalve 55:f4819de54e7a 2334 break;
Lightvalve 55:f4819de54e7a 2335 }
Lightvalve 55:f4819de54e7a 2336
Lightvalve 55:f4819de54e7a 2337 case MODE_JOINT_CONTROL: {
Lightvalve 164:645207e160ca 2338
Lightvalve 55:f4819de54e7a 2339 double torq_ref = 0.0f;
Lightvalve 55:f4819de54e7a 2340 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 65:c2812cf26c38 2341 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 55:f4819de54e7a 2342 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 164:645207e160ca 2343
Lightvalve 65:c2812cf26c38 2344 //K & D Low Pass Filter
Lightvalve 65:c2812cf26c38 2345 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 65:c2812cf26c38 2346 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 65:c2812cf26c38 2347 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 164:645207e160ca 2348
Lightvalve 137:15621998925b 2349 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 137:15621998925b 2350 torq_ref = torq.ref;
Lightvalve 55:f4819de54e7a 2351
Lightvalve 55:f4819de54e7a 2352 // torque feedback
Lightvalve 65:c2812cf26c38 2353 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 55:f4819de54e7a 2354 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 55:f4819de54e7a 2355
Lightvalve 55:f4819de54e7a 2356 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 56:2eade98630e2 2357
Lightvalve 55:f4819de54e7a 2358 double I_REF_POS = 0.0f;
Lightvalve 55:f4819de54e7a 2359 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 55:f4819de54e7a 2360 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 55:f4819de54e7a 2361
Lightvalve 55:f4819de54e7a 2362 double temp_vel_pos = 0.0f;
Lightvalve 55:f4819de54e7a 2363 double temp_vel_torq = 0.0f;
Lightvalve 55:f4819de54e7a 2364 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 56:2eade98630e2 2365
Lightvalve 67:3995ffeaa786 2366 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 55:f4819de54e7a 2367 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 55:f4819de54e7a 2368 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:3995ffeaa786 2369 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 55:f4819de54e7a 2370 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 55:f4819de54e7a 2371 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 55:f4819de54e7a 2372 }
Lightvalve 55:f4819de54e7a 2373 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 55:f4819de54e7a 2374 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 55:f4819de54e7a 2375
Lightvalve 55:f4819de54e7a 2376 // velocity compensation for torque control
Lightvalve 67:3995ffeaa786 2377 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 55:f4819de54e7a 2378 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 55:f4819de54e7a 2379 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s
Lightvalve 55:f4819de54e7a 2380 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 55:f4819de54e7a 2381 // L feedforward velocity
Lightvalve 67:3995ffeaa786 2382 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 55:f4819de54e7a 2383 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 55:f4819de54e7a 2384 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 55:f4819de54e7a 2385 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 55:f4819de54e7a 2386 // L feedforward velocity
Lightvalve 55:f4819de54e7a 2387 }
Lightvalve 55:f4819de54e7a 2388 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 55:f4819de54e7a 2389 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 55:f4819de54e7a 2390 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 55:f4819de54e7a 2391 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 55:f4819de54e7a 2392 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 55:f4819de54e7a 2393
Lightvalve 55:f4819de54e7a 2394 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 164:645207e160ca 2395
Lightvalve 65:c2812cf26c38 2396 // Anti-windup for FT
Lightvalve 65:c2812cf26c38 2397 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 65:c2812cf26c38 2398 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 65:c2812cf26c38 2399 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 65:c2812cf26c38 2400 if (I_REF > I_MAX) {
Lightvalve 65:c2812cf26c38 2401 double I_rem = I_REF - I_MAX;
Lightvalve 65:c2812cf26c38 2402 I_rem = Ka*I_rem;
Lightvalve 65:c2812cf26c38 2403 I_REF = I_MAX;
Lightvalve 65:c2812cf26c38 2404 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 65:c2812cf26c38 2405 } else if (I_REF < -I_MAX) {
Lightvalve 65:c2812cf26c38 2406 double I_rem = I_REF - (-I_MAX);
Lightvalve 65:c2812cf26c38 2407 I_rem = Ka*I_rem;
Lightvalve 65:c2812cf26c38 2408 I_REF = -I_MAX;
Lightvalve 65:c2812cf26c38 2409 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 65:c2812cf26c38 2410 }
Lightvalve 65:c2812cf26c38 2411 }
Lightvalve 55:f4819de54e7a 2412
Lightvalve 55:f4819de54e7a 2413 } else {
Lightvalve 55:f4819de54e7a 2414 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 70:3436ce769b1e 2415 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 70:3436ce769b1e 2416 float VALVE_POS_RAW = 0.0f;
Lightvalve 164:645207e160ca 2417
Lightvalve 55:f4819de54e7a 2418 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 55:f4819de54e7a 2419 + 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 55:f4819de54e7a 2420
Lightvalve 70:3436ce769b1e 2421 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 164:645207e160ca 2422
Lightvalve 70:3436ce769b1e 2423 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 164:645207e160ca 2424
Lightvalve 164:645207e160ca 2425
Lightvalve 70:3436ce769b1e 2426 if (VALVE_POS_RAW >= 0) {
Lightvalve 70:3436ce769b1e 2427 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 55:f4819de54e7a 2428 } else {
Lightvalve 70:3436ce769b1e 2429 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 55:f4819de54e7a 2430 }
Lightvalve 55:f4819de54e7a 2431
Lightvalve 55:f4819de54e7a 2432 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 137:15621998925b 2433 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 55:f4819de54e7a 2434 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 55:f4819de54e7a 2435 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 55:f4819de54e7a 2436 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 55:f4819de54e7a 2437 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 55:f4819de54e7a 2438 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 55:f4819de54e7a 2439 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 55:f4819de54e7a 2440 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 55:f4819de54e7a 2441 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 55:f4819de54e7a 2442 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 55:f4819de54e7a 2443 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 55:f4819de54e7a 2444 }
Lightvalve 55:f4819de54e7a 2445 }
Lightvalve 59:bc8c8270f0ab 2446
Lightvalve 55:f4819de54e7a 2447 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:bc8c8270f0ab 2448
Lightvalve 65:c2812cf26c38 2449 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 58:64181f1d3e60 2450 V_out = (float) Vout.ref;
Lightvalve 55:f4819de54e7a 2451
Lightvalve 55:f4819de54e7a 2452 }
Lightvalve 164:645207e160ca 2453
Lightvalve 70:3436ce769b1e 2454 torq_ref_past = torq_ref;
Lightvalve 56:2eade98630e2 2455
Lightvalve 131:22ab22818e01 2456
Lightvalve 55:f4819de54e7a 2457 break;
Lightvalve 55:f4819de54e7a 2458 }
Lightvalve 56:2eade98630e2 2459
Lightvalve 55:f4819de54e7a 2460 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 55:f4819de54e7a 2461 V_out = (float) Vout.ref;
Lightvalve 55:f4819de54e7a 2462 break;
Lightvalve 55:f4819de54e7a 2463 }
Lightvalve 164:645207e160ca 2464
Lightvalve 136:a843f32ced33 2465 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 164:645207e160ca 2466
Lightvalve 164:645207e160ca 2467
Lightvalve 137:15621998925b 2468 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 137:15621998925b 2469 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 164:645207e160ca 2470
Lightvalve 137:15621998925b 2471 // float Va = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 137:15621998925b 2472 // float Vb = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 165:42c938a40313 2473 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 164:645207e160ca 2474
Lightvalve 164:645207e160ca 2475
Lightvalve 165:42c938a40313 2476 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 164:645207e160ca 2477
Lightvalve 136:a843f32ced33 2478 float g3_prime = 0.0f;
Lightvalve 164:645207e160ca 2479 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 136:a843f32ced33 2480 g3_prime = 1.0f;
Lightvalve 136:a843f32ced33 2481 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 136:a843f32ced33 2482 g3_prime = -1.0f;
Lightvalve 136:a843f32ced33 2483 } else {
Lightvalve 136:a843f32ced33 2484 if ((value-VALVE_CENTER) > 0) {
Lightvalve 164:645207e160ca 2485 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 164:645207e160ca 2486 // g3_prime = sqrt(Ps-Pt);
Lightvalve 136:a843f32ced33 2487 } else {
Lightvalve 136:a843f32ced33 2488 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 137:15621998925b 2489 // g3_prime = sqrt(Ps-Pt);
Lightvalve 136:a843f32ced33 2490 }
Lightvalve 136:a843f32ced33 2491 }
Lightvalve 136:a843f32ced33 2492 float tau = 0.01f;
Lightvalve 140:43026242815a 2493 float K_valve = 0.0004f;
Lightvalve 164:645207e160ca 2494
Lightvalve 136:a843f32ced33 2495 float x_v = 0.0f; //x_v : -1~1
Lightvalve 136:a843f32ced33 2496 if(value>=VALVE_CENTER) {
Lightvalve 136:a843f32ced33 2497 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 136:a843f32ced33 2498 } else {
Lightvalve 136:a843f32ced33 2499 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 136:a843f32ced33 2500 }
Lightvalve 136:a843f32ced33 2501 float f4 = -x_v/tau;
Lightvalve 136:a843f32ced33 2502 float g4 = K_valve/tau;
Lightvalve 164:645207e160ca 2503
Lightvalve 137:15621998925b 2504 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 164:645207e160ca 2505
Lightvalve 136:a843f32ced33 2506 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 136:a843f32ced33 2507 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 136:a843f32ced33 2508 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 164:645207e160ca 2509
Lightvalve 136:a843f32ced33 2510 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 136:a843f32ced33 2511 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 164:645207e160ca 2512
Lightvalve 140:43026242815a 2513 float k3 = 20000.0f; //2000
Lightvalve 140:43026242815a 2514 float k4 = 10.0f;
Lightvalve 140:43026242815a 2515 float rho3 = 3.2f;
Lightvalve 140:43026242815a 2516 float rho4 = 25000000.0f;
Lightvalve 137:15621998925b 2517 float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 136:a843f32ced33 2518 if (x_4_des > 1) x_4_des = 1;
Lightvalve 136:a843f32ced33 2519 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 164:645207e160ca 2520
Lightvalve 137:15621998925b 2521 if (x_4_des > 0) {
Lightvalve 137:15621998925b 2522 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 137:15621998925b 2523 } else {
Lightvalve 137:15621998925b 2524 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 137:15621998925b 2525 }
Lightvalve 164:645207e160ca 2526
Lightvalve 164:645207e160ca 2527
Lightvalve 136:a843f32ced33 2528 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 136:a843f32ced33 2529 x_4_des_old = x_4_des;
Lightvalve 164:645207e160ca 2530
Lightvalve 137:15621998925b 2531 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 164:645207e160ca 2532
Lightvalve 140:43026242815a 2533 float rho_gamma = 50000.0f;//5000
Lightvalve 137:15621998925b 2534 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 136:a843f32ced33 2535 gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 164:645207e160ca 2536 break;
Lightvalve 136:a843f32ced33 2537 }
Lightvalve 166:bfc1fd2629d8 2538
Lightvalve 165:42c938a40313 2539 case MODE_RL: {
Lightvalve 165:42c938a40313 2540 //t.reset();
Lightvalve 165:42c938a40313 2541 //t.start();
Lightvalve 165:42c938a40313 2542
Lightvalve 165:42c938a40313 2543 // if(LED == 0) LED = 1;
Lightvalve 165:42c938a40313 2544 // else LED = 0;
Lightvalve 165:42c938a40313 2545
Lightvalve 165:42c938a40313 2546 if (Update_Done_Flag == 1) {
Lightvalve 165:42c938a40313 2547 //Gather Data on each loop
Lightvalve 165:42c938a40313 2548 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 165:42c938a40313 2549 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2550 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2551 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 165:42c938a40313 2552 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2553 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2554 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 165:42c938a40313 2555 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 165:42c938a40313 2556 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 165:42c938a40313 2557 Actor_Network(temp_array);
Lightvalve 168:68c7914679ec 2558 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 168:68c7914679ec 2559 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 168:68c7914679ec 2560 }
Lightvalve 168:68c7914679ec 2561 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 168:68c7914679ec 2562 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 168:68c7914679ec 2563 }
Lightvalve 168:68c7914679ec 2564 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 168:68c7914679ec 2565 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 165:42c938a40313 2566 mean_array[RL_timer] = mean;
Lightvalve 165:42c938a40313 2567 deviation_array[RL_timer] = deviation;
Lightvalve 165:42c938a40313 2568 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 165:42c938a40313 2569
Lightvalve 165:42c938a40313 2570 virt_pos = virt_pos + (action_array[RL_timer] - 3.0f) * 1000.0f * 0.0002f;
Lightvalve 165:42c938a40313 2571 if (virt_pos > 70.0f ) {
Lightvalve 165:42c938a40313 2572 virt_pos = 70.0f;
Lightvalve 165:42c938a40313 2573 } else if(virt_pos < -70.0f) {
Lightvalve 165:42c938a40313 2574 virt_pos = -70.0f;
Lightvalve 165:42c938a40313 2575 }
Lightvalve 166:bfc1fd2629d8 2576
Lightvalve 165:42c938a40313 2577 RL_timer++;
Lightvalve 165:42c938a40313 2578
Lightvalve 165:42c938a40313 2579
Lightvalve 165:42c938a40313 2580 if (RL_timer >= batch_size) {
Lightvalve 165:42c938a40313 2581 RL_timer = 0;
Lightvalve 165:42c938a40313 2582 batch++;
Lightvalve 165:42c938a40313 2583 for(int i=0; i<batch_size; i++) {
Lightvalve 165:42c938a40313 2584 state_array[i][0] = train_set_x[i];
Lightvalve 165:42c938a40313 2585 state_array[i][1] = train_set_error[i];
Lightvalve 165:42c938a40313 2586 //state_array[i][2] = train_set_count[i];
Lightvalve 165:42c938a40313 2587 }
Lightvalve 165:42c938a40313 2588 Update_Case = 1;
Lightvalve 165:42c938a40313 2589 Update_Done_Flag = 0;
Lightvalve 165:42c938a40313 2590 logging1 = virt_pos;
Lightvalve 173:1074553d2f6f 2591
Lightvalve 165:42c938a40313 2592 if(batch >= num_batch) {
Lightvalve 165:42c938a40313 2593 batch = 0;
Lightvalve 165:42c938a40313 2594 RL_timer = 0;
Lightvalve 165:42c938a40313 2595 Update_Case = 2;
Lightvalve 165:42c938a40313 2596 Update_Done_Flag = 0;
Lightvalve 165:42c938a40313 2597 virt_pos = 10.0f;
Lightvalve 165:42c938a40313 2598 }
Lightvalve 165:42c938a40313 2599 }
Lightvalve 165:42c938a40313 2600 }
Lightvalve 165:42c938a40313 2601
Lightvalve 165:42c938a40313 2602 else {
Lightvalve 165:42c938a40313 2603
Lightvalve 165:42c938a40313 2604 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 165:42c938a40313 2605 float temp_array[3] = {0.0f};
Lightvalve 165:42c938a40313 2606 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2607 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 165:42c938a40313 2608 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 165:42c938a40313 2609 Actor_Network(temp_array);
Lightvalve 165:42c938a40313 2610 action = rand_normal(mean, deviation);
Lightvalve 165:42c938a40313 2611 //logging1 = action;
Lightvalve 165:42c938a40313 2612 logging2 = mean;
Lightvalve 165:42c938a40313 2613 logging4 = deviation;
Lightvalve 165:42c938a40313 2614 virt_pos = virt_pos + (action-3.0f) * 1000.0f * 0.0002f;
Lightvalve 165:42c938a40313 2615 if (virt_pos > 70.0f) {
Lightvalve 165:42c938a40313 2616 virt_pos = 70.0f;
Lightvalve 165:42c938a40313 2617 } else if(virt_pos < -70.0f) {
Lightvalve 165:42c938a40313 2618 virt_pos = -70.0f;
Lightvalve 165:42c938a40313 2619 }
Lightvalve 165:42c938a40313 2620
Lightvalve 165:42c938a40313 2621 logging3 = virt_pos;
Lightvalve 165:42c938a40313 2622 }
Lightvalve 165:42c938a40313 2623
Lightvalve 165:42c938a40313 2624 //t.stop();
Lightvalve 165:42c938a40313 2625 //logging1 = t.read()*1000.0f; //msec
Lightvalve 165:42c938a40313 2626
Lightvalve 165:42c938a40313 2627 break;
Lightvalve 165:42c938a40313 2628 }
Lightvalve 14:8e7590227d22 2629
Lightvalve 12:6f2531038ea4 2630 default:
Lightvalve 12:6f2531038ea4 2631 break;
Lightvalve 12:6f2531038ea4 2632 }
Lightvalve 14:8e7590227d22 2633
Lightvalve 55:f4819de54e7a 2634
Lightvalve 55:f4819de54e7a 2635 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 56:2eade98630e2 2636
Lightvalve 55:f4819de54e7a 2637 ////////////////////////////////////////////////////////////////////////////
Lightvalve 55:f4819de54e7a 2638 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 55:f4819de54e7a 2639 ////////////////////////////////////////////////////////////////////////////
Lightvalve 55:f4819de54e7a 2640 if (CURRENT_CONTROL_MODE) {
Lightvalve 65:c2812cf26c38 2641 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 55:f4819de54e7a 2642 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 55:f4819de54e7a 2643
Lightvalve 55:f4819de54e7a 2644 I_ERR = I_REF_fil - cur.sen;
Lightvalve 65:c2812cf26c38 2645 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 55:f4819de54e7a 2646
Lightvalve 55:f4819de54e7a 2647
Lightvalve 55:f4819de54e7a 2648 // Moog Valve Current Control Gain
Lightvalve 65:c2812cf26c38 2649 double R_model = 500.0f; // ohm
Lightvalve 55:f4819de54e7a 2650 double L_model = 1.2f;
Lightvalve 55:f4819de54e7a 2651 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 55:f4819de54e7a 2652 double KP_I = 0.1f * L_model*w0;
Lightvalve 55:f4819de54e7a 2653 double KI_I = 0.1f * R_model*w0;
Lightvalve 55:f4819de54e7a 2654
Lightvalve 55:f4819de54e7a 2655 // KNR Valve Current Control Gain
Lightvalve 55:f4819de54e7a 2656 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 55:f4819de54e7a 2657 R_model = 163.0f; // ohm
Lightvalve 55:f4819de54e7a 2658 L_model = 1.0f;
Lightvalve 55:f4819de54e7a 2659 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 55:f4819de54e7a 2660 KP_I = 1.0f * L_model*w0;
Lightvalve 55:f4819de54e7a 2661 KI_I = 0.08f * R_model*w0;
Lightvalve 55:f4819de54e7a 2662 }
Lightvalve 55:f4819de54e7a 2663
Lightvalve 55:f4819de54e7a 2664 double FF_gain = 1.0f;
Lightvalve 55:f4819de54e7a 2665
Lightvalve 65:c2812cf26c38 2666 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 55:f4819de54e7a 2667 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 55:f4819de54e7a 2668 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 55:f4819de54e7a 2669 I_REF_fil_old = I_REF_fil;
Lightvalve 65:c2812cf26c38 2670 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV
Lightvalve 65:c2812cf26c38 2671 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 65:c2812cf26c38 2672 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 55:f4819de54e7a 2673
Lightvalve 55:f4819de54e7a 2674 double Ka = 3.0f / KP_I;
Lightvalve 55:f4819de54e7a 2675 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 55:f4819de54e7a 2676 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 55:f4819de54e7a 2677 V_rem = Ka*V_rem;
Lightvalve 55:f4819de54e7a 2678 VALVE_PWM_RAW = V_MAX;
Lightvalve 65:c2812cf26c38 2679 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 55:f4819de54e7a 2680 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 55:f4819de54e7a 2681 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 55:f4819de54e7a 2682 V_rem = Ka*V_rem;
Lightvalve 55:f4819de54e7a 2683 VALVE_PWM_RAW = -V_MAX;
Lightvalve 65:c2812cf26c38 2684 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 55:f4819de54e7a 2685 }
Lightvalve 55:f4819de54e7a 2686 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 55:f4819de54e7a 2687 } else {
Lightvalve 55:f4819de54e7a 2688 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 55:f4819de54e7a 2689 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 55:f4819de54e7a 2690 }
Lightvalve 55:f4819de54e7a 2691
Lightvalve 55:f4819de54e7a 2692 ////////////////////////////////////////////////////////////////////////////
Lightvalve 55:f4819de54e7a 2693 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 55:f4819de54e7a 2694 ////////////////////////////////////////////////////////////////////////////
Lightvalve 55:f4819de54e7a 2695 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 55:f4819de54e7a 2696 if (FLAG_VALVE_DEADZONE) {
Lightvalve 55:f4819de54e7a 2697 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 55:f4819de54e7a 2698 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 55:f4819de54e7a 2699
Lightvalve 55:f4819de54e7a 2700 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 55:f4819de54e7a 2701
Lightvalve 55:f4819de54e7a 2702 } else {
Lightvalve 55:f4819de54e7a 2703 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 55:f4819de54e7a 2704 }
Lightvalve 55:f4819de54e7a 2705
Lightvalve 55:f4819de54e7a 2706 // Output Voltage Linearization
Lightvalve 55:f4819de54e7a 2707 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 65:c2812cf26c38 2708 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 55:f4819de54e7a 2709
Lightvalve 55:f4819de54e7a 2710 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 65:c2812cf26c38 2711 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 65:c2812cf26c38 2712 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 65:c2812cf26c38 2713 else V_out = (float) (CUR_PWM_lin);
Lightvalve 164:645207e160ca 2714 } else { //////////////////////////sw valve
Lightvalve 87:a7b45368ea0f 2715 // Output Voltage Linearization
Lightvalve 133:79885a39c161 2716 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 133:79885a39c161 2717 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 87:a7b45368ea0f 2718
Lightvalve 87:a7b45368ea0f 2719 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 133:79885a39c161 2720 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 133:79885a39c161 2721 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 133:79885a39c161 2722 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 133:79885a39c161 2723
Lightvalve 133:79885a39c161 2724 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 133:79885a39c161 2725 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 133:79885a39c161 2726 else V_out = 0.0f;
Lightvalve 87:a7b45368ea0f 2727 }
Lightvalve 164:645207e160ca 2728
Lightvalve 65:c2812cf26c38 2729 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 65:c2812cf26c38 2730 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 65:c2812cf26c38 2731 // else V_out = V_out;
Lightvalve 164:645207e160ca 2732
jobuuu 7:e9086c72bb22 2733 /*******************************************************
jobuuu 7:e9086c72bb22 2734 *** PWM
jobuuu 7:e9086c72bb22 2735 ********************************************************/
Lightvalve 164:645207e160ca 2736 if(DIR_VALVE<0) {
Lightvalve 65:c2812cf26c38 2737 V_out = -V_out;
Lightvalve 65:c2812cf26c38 2738 }
Lightvalve 164:645207e160ca 2739
Lightvalve 48:e7bcfc244d40 2740 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 48:e7bcfc244d40 2741 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 48:e7bcfc244d40 2742 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 48:e7bcfc244d40 2743 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2744 }
Lightvalve 48:e7bcfc244d40 2745 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2746
Lightvalve 19:23b7c1ad8683 2747 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2748 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2749 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2750
Lightvalve 30:8d561f16383b 2751 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2752 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2753 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2754 } else {
jobuuu 2:a1c0a37df760 2755 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2756 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2757 }
Lightvalve 13:747daba9cf59 2758
jobuuu 1:e04e563be5ce 2759 //pwm
Lightvalve 30:8d561f16383b 2760 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2761 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 65:c2812cf26c38 2762
Lightvalve 59:bc8c8270f0ab 2763
Lightvalve 55:f4819de54e7a 2764 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2765
Lightvalve 52:647072f5307a 2766 // Position, Velocity, and Torque (ID:1200)
Lightvalve 54:6f50d9d3bfee 2767 if (flag_data_request[0] == HIGH) {
Lightvalve 55:f4819de54e7a 2768 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 52:647072f5307a 2769 if (SENSING_MODE == 0) {
Lightvalve 65:c2812cf26c38 2770 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 52:647072f5307a 2771 } else if (SENSING_MODE == 1) {
Lightvalve 56:2eade98630e2 2772 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 52:647072f5307a 2773 }
Lightvalve 55:f4819de54e7a 2774 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 52:647072f5307a 2775 if (SENSING_MODE == 0) {
Lightvalve 65:c2812cf26c38 2776 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 52:647072f5307a 2777 } else if (SENSING_MODE == 1) {
Lightvalve 65:c2812cf26c38 2778 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 52:647072f5307a 2779 }
Lightvalve 51:8ea76864368a 2780 }
Lightvalve 51:8ea76864368a 2781 }
Lightvalve 54:6f50d9d3bfee 2782 if (flag_data_request[1] == HIGH) {
Lightvalve 52:647072f5307a 2783 //valve position
Lightvalve 164:645207e160ca 2784 double t_value = 0.0f;
Lightvalve 95:d71c57e3515e 2785 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 95:d71c57e3515e 2786 t_value = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 95:d71c57e3515e 2787 } else {
Lightvalve 95:d71c57e3515e 2788 t_value = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 95:d71c57e3515e 2789 }
Lightvalve 65:c2812cf26c38 2790 CAN_TX_TORQUE((int16_t) (t_value)); //1300
Lightvalve 52:647072f5307a 2791 }
Lightvalve 56:2eade98630e2 2792
Lightvalve 56:2eade98630e2 2793
Lightvalve 166:bfc1fd2629d8 2794 if (flag_data_request[2] == HIGH) {
Lightvalve 164:645207e160ca 2795 double t_value = 0.0f;
Lightvalve 166:bfc1fd2629d8 2796 if(value>=(float) VALVE_CENTER) {
Lightvalve 129:d08121ac87ba 2797 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 87:a7b45368ea0f 2798 } else {
Lightvalve 129:d08121ac87ba 2799 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 87:a7b45368ea0f 2800 }
Lightvalve 164:645207e160ca 2801 double t_value_ref = 0.0f;
Lightvalve 164:645207e160ca 2802 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 164:645207e160ca 2803 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2804 } else {
Lightvalve 164:645207e160ca 2805 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 164:645207e160ca 2806 }
Lightvalve 164:645207e160ca 2807
Lightvalve 164:645207e160ca 2808
Lightvalve 164:645207e160ca 2809 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 53:b25725257569 2810 }
Lightvalve 56:2eade98630e2 2811
Lightvalve 55:f4819de54e7a 2812 //If it doesn't rest, below can can not work.
Lightvalve 55:f4819de54e7a 2813 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 55:f4819de54e7a 2814 ;
Lightvalve 55:f4819de54e7a 2815 }
Lightvalve 56:2eade98630e2 2816
Lightvalve 164:645207e160ca 2817 if (flag_data_request[3] == HIGH) {
Lightvalve 52:647072f5307a 2818 //PWM
Lightvalve 71:f80dc3970c99 2819 //CAN_TX_PWM((int16_t) value); //1500
Lightvalve 137:15621998925b 2820 CAN_TX_PWM((int16_t) gamma_hat); //1500
Lightvalve 52:647072f5307a 2821 }
Lightvalve 55:f4819de54e7a 2822 //for (i = 0; i < 10000; i++) {
Lightvalve 55:f4819de54e7a 2823 // ;
Lightvalve 55:f4819de54e7a 2824 // }
Lightvalve 54:6f50d9d3bfee 2825 if (flag_data_request[4] == HIGH) {
Lightvalve 52:647072f5307a 2826 //valve position
Lightvalve 166:bfc1fd2629d8 2827 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 52:647072f5307a 2828 }
Lightvalve 20:806196fda269 2829
Lightvalve 52:647072f5307a 2830 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 51:8ea76864368a 2831 // if (flag_data_request[1] == HIGH) {
Lightvalve 51:8ea76864368a 2832 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 51:8ea76864368a 2833 // }
Lightvalve 52:647072f5307a 2834 //if (flag_delay_test == 1){
Lightvalve 56:2eade98630e2 2835 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 52:647072f5307a 2836 //}
Lightvalve 51:8ea76864368a 2837
Lightvalve 52:647072f5307a 2838 TMR2_COUNT_CAN_TX = 0;
Lightvalve 52:647072f5307a 2839 }
Lightvalve 52:647072f5307a 2840 TMR2_COUNT_CAN_TX++;
Lightvalve 51:8ea76864368a 2841
Lightvalve 20:806196fda269 2842 }
Lightvalve 51:8ea76864368a 2843 TIM3->SR = 0x0; // reset the status register
Lightvalve 51:8ea76864368a 2844
Lightvalve 56:2eade98630e2 2845 }