LIGHT2

Dependencies:   FastPWM mbed

Committer:
Lightvalve
Date:
Sat Nov 21 07:15:47 2020 +0000
Revision:
171:bfc1fd2629d8
Parent:
170:42c938a40313
Child:
172:63af34265fe9
201121-1

Who changed what in which revision?

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