20210203

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Wed Dec 23 11:22:31 2020 +0000
Revision:
212:ec41f1449ef9
Parent:
211:6fe9886cdb6e
Child:
213:3e7bee70a0b4
Child:
214:231d27008443
201223-4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 212:ec41f1449ef9 1 //201223_4
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 179:d5377766d7ea 14 #include <cmath>
Lightvalve 61:bc8c8270f0ab 15
Lightvalve 61:bc8c8270f0ab 16 using namespace std;
Lightvalve 61:bc8c8270f0ab 17 Timer t;
GiJeongKim 0:51c43836c1d7 18
Lightvalve 31:66738bfecec5 19 ///191008////
Lightvalve 31:66738bfecec5 20
jobuuu 7:e9086c72bb22 21 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 22 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 23 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 24 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 25 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 26 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 27 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 28 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 29
Lightvalve 24:ef6e1092e9e6 30
jobuuu 7:e9086c72bb22 31 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 32 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 33 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 34
jobuuu 7:e9086c72bb22 35 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 36 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
GiJeongKim 0:51c43836c1d7 37 const int i2c_slave_addr1 = 0x56;
GiJeongKim 0:51c43836c1d7 38 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 39
jobuuu 7:e9086c72bb22 40 // SPI ///////////////////////////////////////////
Lightvalve 170:42c938a40313 41 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 170:42c938a40313 42 DigitalOut eeprom_cs(PB_12);
Lightvalve 16:903b5a4433b4 43 //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
GiJeongKim 0:51c43836c1d7 44 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 45 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 46 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 47
jobuuu 7:e9086c72bb22 48 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 49 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 50
jobuuu 7:e9086c72bb22 51 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 52 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 53 CANMessage msg;
Lightvalve 11:82d8768d7351 54 void onMsgReceived()
Lightvalve 11:82d8768d7351 55 {
Lightvalve 11:82d8768d7351 56 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 57 }
jobuuu 2:a1c0a37df760 58
jobuuu 7:e9086c72bb22 59 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 60 State pos;
jobuuu 7:e9086c72bb22 61 State vel;
jobuuu 7:e9086c72bb22 62 State Vout;
jobuuu 7:e9086c72bb22 63 State torq;
Lightvalve 207:c70c5a9f17dd 64 State torq_dot;
jobuuu 7:e9086c72bb22 65 State pres_A;
jobuuu 7:e9086c72bb22 66 State pres_B;
jobuuu 7:e9086c72bb22 67 State cur;
Lightvalve 14:8e7590227d22 68 State valve_pos;
Lightvalve 14:8e7590227d22 69
Lightvalve 14:8e7590227d22 70 State INIT_Vout;
Lightvalve 14:8e7590227d22 71 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 72 State INIT_Pos;
Lightvalve 14:8e7590227d22 73 State INIT_torq;
jobuuu 5:a4319f79457b 74
Lightvalve 19:23b7c1ad8683 75 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 76 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 77 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 78 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 79
Lightvalve 19:23b7c1ad8683 80 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 84 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 85 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 86
Lightvalve 61:bc8c8270f0ab 87
Lightvalve 61:bc8c8270f0ab 88
Lightvalve 61:bc8c8270f0ab 89
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91 // =============================================================================
jobuuu 7:e9086c72bb22 92 // =============================================================================
jobuuu 7:e9086c72bb22 93
Lightvalve 12:6f2531038ea4 94 /*******************************************************************************
Lightvalve 12:6f2531038ea4 95 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 96 ******************************************************************************/
Lightvalve 13:747daba9cf59 97 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 98 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 99 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 100 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 101 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 102 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 103 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 104 };
Lightvalve 12:6f2531038ea4 105
Lightvalve 12:6f2531038ea4 106 /*******************************************************************************
Lightvalve 12:6f2531038ea4 107 * CONTROL MODE
Lightvalve 12:6f2531038ea4 108 ******************************************************************************/
Lightvalve 13:747daba9cf59 109 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 110 //control mode
Lightvalve 12:6f2531038ea4 111 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 112 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 113 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 114
Lightvalve 47:fdcb8bd86fd6 115 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 116 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 117 MODE_RL, //5
Lightvalve 13:747daba9cf59 118
Lightvalve 12:6f2531038ea4 119 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 120 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 121 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 122
Lightvalve 12:6f2531038ea4 123 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 124 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 125
Lightvalve 14:8e7590227d22 126 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 127 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 128 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 171:bfc1fd2629d8 129 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 130
Lightvalve 12:6f2531038ea4 131 //utility
Lightvalve 12:6f2531038ea4 132 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 133 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 134 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 135 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 136 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 137 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 138 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 139
Lightvalve 12:6f2531038ea4 140 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 141 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 142 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 143 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 144 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 145 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 146 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 147 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 148 };
Lightvalve 12:6f2531038ea4 149
Lightvalve 65:a2d7c63419c2 150 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 151 {
Lightvalve 169:645207e160ca 152 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 153 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 154
Lightvalve 169:645207e160ca 155 /** Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 156 */
Lightvalve 169:645207e160ca 157 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 158 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 169:645207e160ca 159 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 160 */
Lightvalve 169:645207e160ca 161 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 162 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 163 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 164 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 165 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 166 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 167 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 168 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 169 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 170 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 171 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 172 //Error_Handler();
Lightvalve 169:645207e160ca 173 }
Lightvalve 169:645207e160ca 174 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 175 */
Lightvalve 169:645207e160ca 176 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 177 //Error_Handler();
Lightvalve 169:645207e160ca 178 }
Lightvalve 169:645207e160ca 179 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 180 */
Lightvalve 169:645207e160ca 181 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 182 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 183 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 184 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 185 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 186 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 187
Lightvalve 169:645207e160ca 188 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 189 //Error_Handler();
Lightvalve 169:645207e160ca 190 }
Lightvalve 65:a2d7c63419c2 191 }
Lightvalve 65:a2d7c63419c2 192
Lightvalve 99:7bbcb3c0fb06 193 float u_past[num_array_u_past] = {0.0f};
Lightvalve 68:328e1be06f5d 194 float x_past[num_array_x_past] = {0.0f};
Lightvalve 85:a3b46118b5cd 195 float x_future[num_array_x_future] = {0.0f};
Lightvalve 68:328e1be06f5d 196 float f_past[num_array_f_past] = {0.0f};
Lightvalve 68:328e1be06f5d 197 float f_future[num_array_f_future] = {0.0f};
Lightvalve 66:a8e6799dbce3 198
Lightvalve 73:f80dc3970c99 199 float input_NN[num_input] = { 0.0f };
Lightvalve 65:a2d7c63419c2 200
Lightvalve 169:645207e160ca 201 const float h1[num_input][16] = {
Lightvalve 212:ec41f1449ef9 202 {-1.6070621013641357f,1.4444793462753296f,0.12903714179992676f,0.45971256494522095f,-1.8895007371902466f,1.5912665128707886f,-0.17472904920578003f,1.3318427801132202f,-2.261326551437378f,-0.03385619446635246f,-0.23437689244747162f,-1.2534494400024414f,-0.26408806443214417f,-1.1718051433563232f,-0.2571594715118408f,-1.0255193710327148f},
Lightvalve 212:ec41f1449ef9 203 {-1.0846422910690308f,0.8761099576950073f,-0.3927857577800751f,0.10885071754455566f,-1.2603814601898193f,0.7932608127593994f,-0.4054011106491089f,0.5438631772994995f,-1.546224594116211f,0.22130662202835083f,-0.27116647362709045f,-0.7526845335960388f,0.23553097248077393f,-1.0961291790008545f,-0.20967772603034973f,-0.659416675567627f},
Lightvalve 212:ec41f1449ef9 204 {-0.8406979441642761f,0.5655145645141602f,0.36391180753707886f,-0.2829425036907196f,-0.6366292238235474f,0.6242642402648926f,0.2872133255004883f,0.39269590377807617f,-1.4623386859893799f,0.22947895526885986f,-0.34503698348999023f,-0.4441416561603546f,-0.00272137182764709f,-0.3268943130970001f,0.05590221285820007f,-1.1436342000961304f},
Lightvalve 212:ec41f1449ef9 205 {-0.35294806957244873f,-0.09107200056314468f,0.39742106199264526f,0.33738622069358826f,-0.11074129492044449f,-0.19795556366443634f,-0.11584559082984924f,0.2239270806312561f,-1.016740083694458f,0.367963969707489f,0.09880797564983368f,0.34261059761047363f,-0.16485224664211273f,-0.02963976189494133f,-0.2961459457874298f,-0.9800482988357544f},
Lightvalve 212:ec41f1449ef9 206 {1.1172692775726318f,-0.881708025932312f,-0.0051451027393341064f,0.24785789847373962f,0.949159562587738f,-0.8879446387290955f,0.1294034719467163f,-1.1183830499649048f,-0.18216757476329803f,0.18784251809120178f,0.35077041387557983f,0.5736857056617737f,-0.06842327117919922f,1.0481723546981812f,-0.3865056037902832f,-0.9325311183929443f},
Lightvalve 212:ec41f1449ef9 207 {3.2859668731689453f,-3.373445987701416f,0.21773302555084229f,0.23583510518074036f,3.153277635574341f,-3.0842037200927734f,0.16514194011688232f,-3.330359697341919f,1.6308770179748535f,0.20284788310527802f,-0.24478337168693542f,1.4629696607589722f,-0.26506540179252625f,3.084402084350586f,-0.4132833778858185f,0.3235807716846466f},
Lightvalve 212:ec41f1449ef9 208 {-0.16239909827709198f,0.652294397354126f,-0.2501986622810364f,-0.7905404567718506f,-0.6188644766807556f,0.8528907299041748f,0.06014183163642883f,-0.4905660152435303f,1.5433074235916138f,-0.1533675193786621f,0.15192021429538727f,-0.10490059852600098f,-0.1730978935956955f,0.015356072224676609f,-0.15618428587913513f,0.5344299077987671f},
Lightvalve 212:ec41f1449ef9 209 {-0.8749822974205017f,0.6992738246917725f,-0.26619744300842285f,-0.08359992504119873f,-1.2196704149246216f,0.7197252511978149f,-0.17925891280174255f,0.24655599892139435f,0.32290568947792053f,0.030707592144608498f,0.10629666596651077f,-0.48497965931892395f,0.37155985832214355f,-0.5724732875823975f,0.04303058981895447f,0.5185656547546387f},
Lightvalve 212:ec41f1449ef9 210 {0.44704651832580566f,0.0694582387804985f,0.10248100757598877f,-0.6408140659332275f,0.08685804158449173f,-0.24289385974407196f,-0.07246989011764526f,-0.8823809623718262f,0.975390613079071f,0.2915976345539093f,-0.2963060140609741f,0.3272884786128998f,0.22921693325042725f,0.7061342597007751f,-0.30555272102355957f,1.1997355222702026f},
Lightvalve 212:ec41f1449ef9 211 {0.10269708186388016f,-0.4119151830673218f,0.18462657928466797f,-0.4237739145755768f,0.28614145517349243f,-0.0384453721344471f,-0.2990540862083435f,-0.3690754175186157f,1.0338808298110962f,-0.506875216960907f,-0.007301787845790386f,-0.4488030672073364f,0.372331827878952f,0.18725666403770447f,0.3484981060028076f,0.7741178274154663f},
Lightvalve 212:ec41f1449ef9 212 {0.4530973434448242f,0.06094556301832199f,0.37182438373565674f,-0.03273813799023628f,0.0016666745068505406f,-0.13683553040027618f,-0.20339298248291016f,0.0006952928379178047f,0.36829644441604614f,0.007492598611861467f,0.3493041396141052f,-0.34462884068489075f,-0.06654797494411469f,0.2495005875825882f,0.26688337326049805f,0.2787923216819763f},
Lightvalve 212:ec41f1449ef9 213 {-0.41628196835517883f,-0.34683796763420105f,-0.40320003032684326f,-0.26510435342788696f,0.01427370309829712f,-0.12881697714328766f,0.1533789038658142f,0.6213264465332031f,0.43982186913490295f,0.1924997866153717f,-0.41328826546669006f,-0.0027057332918047905f,0.14470605552196503f,0.13542859256267548f,0.10493969917297363f,0.358428418636322f},
Lightvalve 212:ec41f1449ef9 214 {-0.09863808006048203f,-0.15524423122406006f,0.19847965240478516f,-0.08667934685945511f,0.2108737826347351f,0.0608486607670784f,-0.2404318004846573f,0.5305993556976318f,0.29834941029548645f,-0.20526979863643646f,-0.10813693702220917f,-0.45609328150749207f,0.09201034903526306f,0.16512572765350342f,-0.22268100082874298f,-0.07933637499809265f},
Lightvalve 212:ec41f1449ef9 215 {-0.1803528368473053f,-0.07565825432538986f,0.4127817749977112f,-0.29987478256225586f,-0.3406386971473694f,-0.3658057749271393f,-0.32001304626464844f,0.18756037950515747f,0.23431532084941864f,-0.23265855014324188f,-0.19404397904872894f,-0.44801175594329834f,-0.31276315450668335f,0.09663949906826019f,0.0479682981967926f,0.23482243716716766f},
Lightvalve 212:ec41f1449ef9 216 {0.09787440299987793f,0.05535588413476944f,0.20282304286956787f,-0.1285126656293869f,0.21852988004684448f,-0.23995305597782135f,0.04202890396118164f,0.899287223815918f,0.4933943748474121f,0.07827243953943253f,-0.2784428298473358f,-0.7169914841651917f,0.10353262722492218f,0.041469767689704895f,0.0668090283870697f,0.10634815692901611f},
Lightvalve 212:ec41f1449ef9 217 {-0.17166218161582947f,-0.24739637970924377f,-0.40439701080322266f,-0.278349369764328f,-0.34712228178977966f,-0.047724951058626175f,-0.15568238496780396f,0.7170681357383728f,0.2422538846731186f,-0.006808132864534855f,-0.47287464141845703f,-0.4041852056980133f,-0.2711547017097473f,0.0345175676047802f,-0.08245879411697388f,-0.017156904563307762f},
Lightvalve 212:ec41f1449ef9 218 {0.10945872962474823f,-0.3796606957912445f,0.16706281900405884f,-0.1058274433016777f,0.09715140610933304f,-0.6114601492881775f,-0.041414469480514526f,0.459582656621933f,0.15684588253498077f,0.09002777934074402f,-0.35252657532691956f,0.13107328116893768f,0.11200001835823059f,0.6847966313362122f,0.29427415132522583f,-0.10886158049106598f},
Lightvalve 65:a2d7c63419c2 219 };
Lightvalve 65:a2d7c63419c2 220
Lightvalve 169:645207e160ca 221 const float h2[16][16] = {
Lightvalve 212:ec41f1449ef9 222 {-0.2100285440683365f,0.5450449585914612f,-0.06966331601142883f,-0.16327691078186035f,-0.21907491981983185f,-0.7174352407455444f,-0.08997400104999542f,0.03496444225311279f,-0.43008196353912354f,-0.1415480375289917f,-1.024130940437317f,-0.43296945095062256f,0.12968102097511292f,-1.543792724609375f,-0.4490607678890228f,-4.010191440582275f},
Lightvalve 212:ec41f1449ef9 223 {0.09335885941982269f,-2.328054904937744f,0.057057321071624756f,-0.4136889576911926f,-0.35503754019737244f,-1.8548954725265503f,-0.5476264953613281f,-0.3163917064666748f,-0.33757925033569336f,0.2895788848400116f,-3.989751100540161f,-0.2958875000476837f,0.33698758482933044f,2.3762283325195312f,-0.10209953784942627f,-1.2367063760757446f},
Lightvalve 179:d5377766d7ea 224 {-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 212:ec41f1449ef9 225 {0.05469902977347374f,-0.25110578536987305f,-0.2894435524940491f,0.5345750451087952f,0.08946844935417175f,0.4574865400791168f,-0.23805393278598785f,0.4122363030910492f,0.26211628317832947f,0.2953031361103058f,0.3205234110355377f,0.3810727894306183f,-0.404754102230072f,-0.2590928077697754f,0.2257760912179947f,0.16342028975486755f},
Lightvalve 212:ec41f1449ef9 226 {-0.25391754508018494f,0.49998918175697327f,-0.25313520431518555f,0.0678805261850357f,0.036378175020217896f,-0.5025586485862732f,-0.58262699842453f,-0.12088710814714432f,-0.024399548768997192f,-0.39465832710266113f,-1.5418189764022827f,-0.7374540567398071f,0.10077962279319763f,-0.42483294010162354f,-0.49100643396377563f,-0.64886075258255f},
Lightvalve 212:ec41f1449ef9 227 {0.34239134192466736f,-1.3530895709991455f,-0.3519742488861084f,0.014243300072848797f,-0.3015052080154419f,-1.4964323043823242f,-0.6469179391860962f,-0.04728340730071068f,-0.23047015070915222f,0.3579089343547821f,-2.080329179763794f,-0.22948884963989258f,-0.20959392189979553f,1.1190712451934814f,0.13593792915344238f,-2.15841007232666f},
Lightvalve 208:408f9f15c486 228 {-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 212:ec41f1449ef9 229 {0.1319282501935959f,-1.5630793571472168f,0.33083590865135193f,-0.6722083687782288f,-0.14358049631118774f,-1.5424034595489502f,-0.5113024115562439f,-0.32745271921157837f,0.2600560486316681f,-0.37898191809654236f,-2.461042642593384f,-0.40288063883781433f,-0.31673234701156616f,1.4697182178497314f,0.17047874629497528f,-0.9254405498504639f},
Lightvalve 212:ec41f1449ef9 230 {-0.2827617824077606f,-0.5835872888565063f,-0.11379697918891907f,-0.2174888700246811f,0.041274964809417725f,-1.1300400495529175f,-0.5878999829292297f,-0.08471067994832993f,0.06103590130805969f,-0.38046833872795105f,-2.1991589069366455f,-0.5746544599533081f,-0.37024784088134766f,-1.505262017250061f,-0.564139187335968f,-0.7886471748352051f},
Lightvalve 212:ec41f1449ef9 231 {-0.16934998333454132f,0.5294079184532166f,-0.13502129912376404f,0.6688047051429749f,0.12987366318702698f,0.48973479866981506f,0.2711336314678192f,-0.3632148504257202f,-0.13619378209114075f,0.16938945651054382f,0.5563372373580933f,0.3686026632785797f,0.3811538517475128f,-0.608641505241394f,-0.3150292634963989f,-0.07790349423885345f},
Lightvalve 212:ec41f1449ef9 232 {-0.290787935256958f,0.008599113672971725f,0.0457797646522522f,-0.3533823788166046f,-0.03321319818496704f,-0.4156119227409363f,0.17873415350914001f,-0.20421427488327026f,-0.050184011459350586f,0.12480869889259338f,-0.2018822431564331f,0.3545852601528168f,-0.3647043704986572f,0.40289223194122314f,-0.40987616777420044f,-0.2687363028526306f},
Lightvalve 212:ec41f1449ef9 233 {0.31293871998786926f,0.514428973197937f,0.3954955041408539f,0.03152908384799957f,0.0033026933670043945f,0.11348362267017365f,-0.25357672572135925f,-0.1509854942560196f,0.3487861454486847f,-0.32520344853401184f,0.08041279017925262f,-0.32230615615844727f,0.0820283591747284f,-0.3886100947856903f,0.11279337108135223f,-0.043686479330062866f},
Lightvalve 212:ec41f1449ef9 234 {-0.26997238397598267f,-0.21165083348751068f,-0.15798500180244446f,0.37889501452445984f,-0.37393757700920105f,0.3643323481082916f,0.11292675137519836f,-0.2947862446308136f,-0.3764709532260895f,0.2424570620059967f,-0.11038123071193695f,-0.2837170362472534f,0.41839322447776794f,-0.016996487975120544f,-0.12759913504123688f,0.10896846652030945f},
Lightvalve 212:ec41f1449ef9 235 {-0.2488415539264679f,0.5050635933876038f,-0.2018718123435974f,0.4112447202205658f,0.07545611262321472f,0.41179272532463074f,-0.39304429292678833f,-0.21989363431930542f,-0.24509364366531372f,0.19522181153297424f,0.8638243079185486f,-0.5810434818267822f,-0.16718891263008118f,-0.8570426106452942f,-0.6993795037269592f,0.26117852330207825f},
Lightvalve 179:d5377766d7ea 236 {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 212:ec41f1449ef9 237 {-0.4079033434391022f,0.0030517925042659044f,0.12081471085548401f,-0.5565865635871887f,0.29976895451545715f,-1.0711815357208252f,-0.3759289085865021f,0.23442097008228302f,-0.3893685042858124f,-0.02222958207130432f,-3.00561785697937f,-0.32067564129829407f,-0.19991017878055573f,-0.5698347091674805f,0.07654765248298645f,-2.1375231742858887f},
Lightvalve 65:a2d7c63419c2 238 };
Lightvalve 65:a2d7c63419c2 239
Lightvalve 169:645207e160ca 240 const float h3[16][16] = {
Lightvalve 212:ec41f1449ef9 241 {-0.36079341173171997f,-0.08991171419620514f,-0.07854580879211426f,-0.3761775493621826f,-0.19077321887016296f,0.04002965986728668f,0.3332441747188568f,0.3036012351512909f,-0.2405819296836853f,0.04891335964202881f,-0.11870327591896057f,0.4001283347606659f,0.023342788219451904f,0.3089201748371124f,-0.3160521388053894f,0.14951297640800476f},
Lightvalve 212:ec41f1449ef9 242 {0.047732532024383545f,-0.09652574360370636f,0.8443890810012817f,-0.2737945318222046f,-0.6023720502853394f,0.0635899230837822f,-0.3198729455471039f,0.06247803568840027f,-0.14267313480377197f,-0.025146597996354103f,0.22248788177967072f,-0.3128277659416199f,-0.1967906951904297f,2.094522714614868f,-0.3103681206703186f,-0.29817160964012146f},
Lightvalve 205:f3ffa0306a53 243 {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 212:ec41f1449ef9 244 {-0.30922991037368774f,0.09327439963817596f,0.05971873924136162f,-0.3845849633216858f,-0.5093491673469543f,0.33769017457962036f,0.20021501183509827f,0.07002416253089905f,-0.33915144205093384f,-0.365601509809494f,-0.03443625569343567f,0.41021624207496643f,-0.2978057265281677f,0.35003378987312317f,0.26804131269454956f,-0.023121735081076622f},
Lightvalve 205:f3ffa0306a53 245 {-0.39607733488082886f,-0.05481579899787903f,0.1976260244846344f,0.022423356771469116f,0.16892847418785095f,-0.27518749237060547f,0.16012099385261536f,0.3626593053340912f,-0.08640444278717041f,-0.11053556203842163f,-0.10529157519340515f,-0.31317979097366333f,-0.1530032455921173f,-0.1336749792098999f,0.22959044575691223f,0.19986507296562195f},
Lightvalve 212:ec41f1449ef9 246 {-0.37449589371681213f,1.8457472324371338f,-1.8165240287780762f,0.09289615601301193f,-1.1789792776107788f,1.3625030517578125f,-0.18732719123363495f,0.22384825348854065f,-0.5127489566802979f,-1.1904394626617432f,-0.154531329870224f,0.19005391001701355f,-0.5971469283103943f,-0.36133918166160583f,0.4544094502925873f,-0.4715000092983246f},
Lightvalve 212:ec41f1449ef9 247 {0.4110594093799591f,0.2230953872203827f,-0.48654863238334656f,0.37246426939964294f,-0.24602459371089935f,0.1548190414905548f,-0.08670487999916077f,-0.25336313247680664f,-0.030661463737487793f,0.22344467043876648f,-0.1344406008720398f,0.35313835740089417f,0.21155259013175964f,0.10930821299552917f,0.3126353323459625f,-0.3391006588935852f},
Lightvalve 212:ec41f1449ef9 248 {-0.40892091393470764f,0.04582450911402702f,-0.3875610828399658f,0.25968697667121887f,0.3424709737300873f,-0.05059037357568741f,-0.23312048614025116f,-0.390264093875885f,0.28059282898902893f,-0.15431912243366241f,-0.14134526252746582f,-0.0003446042537689209f,-0.2742875814437866f,-0.3649594783782959f,0.07994696497917175f,0.005298197269439697f},
Lightvalve 208:408f9f15c486 249 {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 208:408f9f15c486 250 {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 212:ec41f1449ef9 251 {0.15459725260734558f,0.8427432179450989f,0.679609477519989f,-0.1838339865207672f,-0.8048290014266968f,1.4805269241333008f,-0.08742031455039978f,0.05785742402076721f,-0.4665476381778717f,0.49658796191215515f,-0.08510879427194595f,-0.309722363948822f,-0.2896781861782074f,-0.9483605027198792f,0.4396055340766907f,0.17858834564685822f},
Lightvalve 212:ec41f1449ef9 252 {-0.408692330121994f,0.15604840219020844f,-0.11896948516368866f,0.0922636091709137f,-0.1451295167207718f,0.10412421077489853f,0.03285527229309082f,0.38763079047203064f,-0.20705322921276093f,-0.07742909342050552f,0.12809070944786072f,0.03996849060058594f,-0.4220041334629059f,-0.2886850833892822f,-0.3350064158439636f,0.3153386116027832f},
Lightvalve 212:ec41f1449ef9 253 {-0.2991822361946106f,0.3794580399990082f,-0.07897943258285522f,-0.05932474136352539f,0.08582660555839539f,0.24227938055992126f,-0.11253207921981812f,0.34576353430747986f,0.04814547300338745f,-0.35770976543426514f,-0.044228196144104004f,-0.36229726672172546f,0.015840977430343628f,-0.13475483655929565f,0.36124154925346375f,-0.16869547963142395f},
Lightvalve 212:ec41f1449ef9 254 {-0.2675279378890991f,0.4225318431854248f,-1.3180500268936157f,0.011379898525774479f,1.3076831102371216f,0.03549259528517723f,-0.32875844836235046f,-0.31893211603164673f,-0.9009249806404114f,-0.3250093460083008f,-0.5623511672019958f,-0.42054229974746704f,0.48087817430496216f,2.155684232711792f,0.3168305456638336f,0.050804875791072845f},
Lightvalve 212:ec41f1449ef9 255 {-0.015470266342163086f,-0.5833328366279602f,0.6117988228797913f,0.3354269862174988f,0.33325907588005066f,-0.5840275883674622f,0.05196094512939453f,-0.008358269929885864f,-0.5232630372047424f,-0.37522807717323303f,0.06831542402505875f,-0.11511552333831787f,0.5202909708023071f,-0.2629256248474121f,-0.06220770627260208f,-0.4585099518299103f},
Lightvalve 212:ec41f1449ef9 256 {0.1863725483417511f,1.084182858467102f,-1.0181115865707397f,-0.4724007248878479f,-0.33689597249031067f,0.9457219839096069f,0.04860696196556091f,0.17769548296928406f,-0.12625347077846527f,0.3378079831600189f,-0.4645434319972992f,0.19592127203941345f,-0.12818381190299988f,1.0687506198883057f,-0.028336618095636368f,-0.11500971764326096f},
Lightvalve 66:a8e6799dbce3 257 };
Lightvalve 65:a2d7c63419c2 258
Lightvalve 212:ec41f1449ef9 259 const float hout[16] = { 0.45773375034332275f,1.3250397443771362f,-0.4248747229576111f,-0.16762784123420715f,-0.8701521158218384f,-0.003051317995414138f,0.0030125975608825684f,0.15066689252853394f,-0.0765313059091568f,0.410363107919693f,0.11355479061603546f,-0.2710728645324707f,-1.0137888193130493f,0.31872233748435974f,0.46201157569885254f,-0.1275034099817276f };
Lightvalve 212:ec41f1449ef9 260
Lightvalve 212:ec41f1449ef9 261 const float b1[16] = { 0.34279701113700867f,1.5129735469818115f,-1.7145336866378784f,0.27451324462890625f,0.7754346132278442f,1.2727166414260864f,-0.058932315558195114f,0.9473782777786255f,1.5622917413711548f,-1.0377662181854248f,0.5732393264770508f,1.2818049192428589f,-0.37127330899238586f,-1.0851714611053467f,-1.087764859199524f,2.291581153869629f };
Lightvalve 212:ec41f1449ef9 262
Lightvalve 212:ec41f1449ef9 263 const float b2[16] = { -0.1286313831806183f,-1.2512075901031494f,-1.4564176797866821f,-0.8030261397361755f,-0.6939148902893066f,-0.16376319527626038f,0.1311706006526947f,-0.3665841221809387f,-1.20063054561615f,-1.912178635597229f,-0.45517241954803467f,0.411864310503006f,-0.07918518036603928f,1.8873447179794312f,-0.0411815345287323f,0.7242259383201599f };
Lightvalve 212:ec41f1449ef9 264
Lightvalve 212:ec41f1449ef9 265 const float b3[16] = { -1.963319182395935f,-0.7351341247558594f,0.32978370785713196f,-0.1386529505252838f,0.019260885193943977f,-0.056516703218221664f,-0.45353031158447266f,-0.6562485098838806f,-0.015211169607937336f,0.6103503704071045f,-0.34831100702285767f,-0.014407366514205933f,-0.753882646560669f,-1.2588518857955933f,-1.226777195930481f,-0.5482154488563538f };
Lightvalve 212:ec41f1449ef9 266
Lightvalve 212:ec41f1449ef9 267 const float bout[1] = { 0.2236381620168686f };
Lightvalve 65:a2d7c63419c2 268
Lightvalve 170:42c938a40313 269 /////////////////////////////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 270 float input_RL[num_input_RL] = { 0.0f };
Lightvalve 170:42c938a40313 271
Lightvalve 170:42c938a40313 272 //Critic Networks
Lightvalve 173:68c7914679ec 273 float hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 274 float bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 275 float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 276 float bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 277 float hc3[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 278 float bc3 = 0.0f;
Lightvalve 170:42c938a40313 279
Lightvalve 170:42c938a40313 280 //Critic Networks Temporary
Lightvalve 173:68c7914679ec 281 float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 282 float bc1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 283 float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 284 float bc2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 285 float hc3_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 286 float bc3_temp = 0.0f;
Lightvalve 170:42c938a40313 287
Lightvalve 170:42c938a40313 288 //Actor Networks
Lightvalve 173:68c7914679ec 289 float ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 290 float ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 291 float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 292 float ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 293 float ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 294 float ba3[2] = {0.0f};
Lightvalve 170:42c938a40313 295
Lightvalve 170:42c938a40313 296 //Actor Networks Temporary
Lightvalve 173:68c7914679ec 297 float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 298 float ba1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 299 float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 300 float ba2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 301 float ha3_temp[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 302 float ba3_temp[2] = {0.0f};
Lightvalve 66:a8e6799dbce3 303
Lightvalve 87:471334725012 304 float VALVE_POS_RAW_NN = 0.0f;
Lightvalve 87:471334725012 305 float DDV_JOINT_POS_FF(float REF_JOINT_VEL);
Lightvalve 61:bc8c8270f0ab 306
Lightvalve 179:d5377766d7ea 307 /////////////////////////////////////////////RL tuning
Lightvalve 179:d5377766d7ea 308 float Gradient_Limit = 0.5f;
Lightvalve 179:d5377766d7ea 309 float gradient_rate_actor = 0.001f;
Lightvalve 179:d5377766d7ea 310 float gradient_rate_critic = 0.001f;
Lightvalve 179:d5377766d7ea 311 //////////////////////////////////////////////////////////////////////////////
Lightvalve 170:42c938a40313 312
Lightvalve 170:42c938a40313 313 float Critic_Network_Temp(float *arr)
Lightvalve 170:42c938a40313 314 {
Lightvalve 173:68c7914679ec 315 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 316 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 317 float output = 0.0f;
Lightvalve 173:68c7914679ec 318 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 319 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 320 output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 321 }
Lightvalve 173:68c7914679ec 322 //ReLU
Lightvalve 173:68c7914679ec 323 output1[index2] = output1[index2] + bc1_temp[index2];
Lightvalve 173:68c7914679ec 324 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 325 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 326 output1[index2] = 0;
Lightvalve 173:68c7914679ec 327 }
Lightvalve 173:68c7914679ec 328 //tanh
Lightvalve 173:68c7914679ec 329 //output1[index2] = tanh(output1[index2] + bc1_temp[index2]);
Lightvalve 173:68c7914679ec 330 }
Lightvalve 173:68c7914679ec 331 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 332 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 333 output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 334 }
Lightvalve 173:68c7914679ec 335 //ReLU
Lightvalve 173:68c7914679ec 336 output2[index2] = output2[index2] + bc2_temp[index2];
Lightvalve 173:68c7914679ec 337 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 338 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 339 output2[index2] = 0;
Lightvalve 173:68c7914679ec 340 }
Lightvalve 173:68c7914679ec 341 //tanh
Lightvalve 173:68c7914679ec 342 //output2[index2] = tanh(output2[index2] + bc2_temp[index2]);
Lightvalve 170:42c938a40313 343 }
Lightvalve 170:42c938a40313 344 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 345 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 346 output = output + hc3_temp[index1] * output2[index1];
Lightvalve 170:42c938a40313 347 }
Lightvalve 173:68c7914679ec 348 output = output + bc3_temp;
Lightvalve 173:68c7914679ec 349 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 350 }
Lightvalve 170:42c938a40313 351 return output;
Lightvalve 170:42c938a40313 352 }
Lightvalve 170:42c938a40313 353
Lightvalve 170:42c938a40313 354
Lightvalve 170:42c938a40313 355 void Actor_Network(float *arr)
Lightvalve 170:42c938a40313 356 {
Lightvalve 173:68c7914679ec 357 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 358 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 170:42c938a40313 359 float output[2] = {0.0f};
Lightvalve 170:42c938a40313 360
Lightvalve 173:68c7914679ec 361 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 362 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 178:1074553d2f6f 363 output1[index2] = output1[index2] + ha1_temp[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 364 }
Lightvalve 178:1074553d2f6f 365 output1[index2] = output1[index2] + ba1_temp[index2];
Lightvalve 173:68c7914679ec 366 hx_a_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 367 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 368 output1[index2] = 0;
Lightvalve 173:68c7914679ec 369 }
Lightvalve 173:68c7914679ec 370 }
Lightvalve 173:68c7914679ec 371 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 372 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 178:1074553d2f6f 373 output2[index2] = output2[index2] + ha2_temp[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 374 }
Lightvalve 178:1074553d2f6f 375 output2[index2] = output2[index2] + ba2_temp[index2];
Lightvalve 173:68c7914679ec 376 hxh_a_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 377 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 378 output2[index2] = 0;
Lightvalve 173:68c7914679ec 379 }
Lightvalve 173:68c7914679ec 380 }
Lightvalve 173:68c7914679ec 381 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 382 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 178:1074553d2f6f 383 output[index2] = output[index2] + ha3_temp[index1][index2] * output2[index1];
Lightvalve 173:68c7914679ec 384 }
Lightvalve 178:1074553d2f6f 385 hxhh_a_sum[index2] = output[index2] + ba3_temp[index2];
Lightvalve 173:68c7914679ec 386 }
Lightvalve 178:1074553d2f6f 387
Lightvalve 178:1074553d2f6f 388 mean_before_SP = output[0] + ba3_temp[0]; //SP = softplus
Lightvalve 178:1074553d2f6f 389 deviation_before_SP = output[1] + ba3_temp[1];
Lightvalve 173:68c7914679ec 390 //Softplus
Lightvalve 173:68c7914679ec 391 mean = log(1.0f+exp(mean_before_SP));
Lightvalve 173:68c7914679ec 392 deviation = log(1.0f+exp(deviation_before_SP));
Lightvalve 179:d5377766d7ea 393 logging2 = mean;
Lightvalve 179:d5377766d7ea 394 logging4 = deviation;
Lightvalve 173:68c7914679ec 395 }
Lightvalve 173:68c7914679ec 396
Lightvalve 173:68c7914679ec 397
Lightvalve 173:68c7914679ec 398 void Actor_Network_Old(float *arr)
Lightvalve 173:68c7914679ec 399 {
Lightvalve 173:68c7914679ec 400 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 401 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 402 float output[2] = {0.0f};
Lightvalve 173:68c7914679ec 403
Lightvalve 173:68c7914679ec 404 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 405 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 406 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 407 }
Lightvalve 170:42c938a40313 408 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 170:42c938a40313 409 if (output1[index2] < 0) {
Lightvalve 170:42c938a40313 410 output1[index2] = 0;
Lightvalve 170:42c938a40313 411 }
Lightvalve 170:42c938a40313 412 }
Lightvalve 173:68c7914679ec 413 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 414 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 415 output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1];
Lightvalve 170:42c938a40313 416 }
Lightvalve 173:68c7914679ec 417 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 418 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 419 output2[index2] = 0;
Lightvalve 170:42c938a40313 420 }
Lightvalve 170:42c938a40313 421 }
Lightvalve 170:42c938a40313 422 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 423 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 424 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 170:42c938a40313 425 }
Lightvalve 170:42c938a40313 426 }
Lightvalve 173:68c7914679ec 427 mean_old = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 428 deviation_old = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 429 //Softplus
Lightvalve 173:68c7914679ec 430 mean_old = log(1.0f+exp(mean_old));
Lightvalve 173:68c7914679ec 431 deviation_old = log(1.0f+exp(deviation_old));
Lightvalve 170:42c938a40313 432 }
Lightvalve 170:42c938a40313 433
Lightvalve 170:42c938a40313 434 float Grad_Normal_Dist_Mean(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 435 {
Lightvalve 170:42c938a40313 436 float grad_mean = 0.0f;
Lightvalve 170:42c938a40313 437 grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation);
Lightvalve 170:42c938a40313 438 return grad_mean;
Lightvalve 170:42c938a40313 439 }
Lightvalve 170:42c938a40313 440
Lightvalve 170:42c938a40313 441 float Grad_Normal_Dist_Deviation(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 442 {
Lightvalve 170:42c938a40313 443 float grad_dev = 0.0f;
Lightvalve 170:42c938a40313 444 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 445 return grad_dev;
Lightvalve 170:42c938a40313 446 }
Lightvalve 170:42c938a40313 447
Lightvalve 173:68c7914679ec 448 float ReLU(float x)
Lightvalve 173:68c7914679ec 449 {
Lightvalve 173:68c7914679ec 450 if (x >= 0) {
Lightvalve 173:68c7914679ec 451 return x;
Lightvalve 173:68c7914679ec 452 } else {
Lightvalve 173:68c7914679ec 453 return 0.0f;
Lightvalve 173:68c7914679ec 454 }
Lightvalve 173:68c7914679ec 455 }
Lightvalve 173:68c7914679ec 456
Lightvalve 170:42c938a40313 457 void update_Critic_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 458 {
Lightvalve 173:68c7914679ec 459 float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 177:8e9cf31d63f4 460 float G_bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 461 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 462 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 463 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 464 float d_V_d_hc1 = 0.0f;
Lightvalve 173:68c7914679ec 465 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 466 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 467 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 468 d_V_d_hc1 = d_V_d_hc1 + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 469 }
Lightvalve 173:68c7914679ec 470 }
Lightvalve 170:42c938a40313 471 }
Lightvalve 177:8e9cf31d63f4 472 G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1);
Lightvalve 170:42c938a40313 473 }
Lightvalve 170:42c938a40313 474 G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 475 if(G_hc1[index1][index2] > Gradient_Limit) G_hc1[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 476 else if (G_hc1[index1][index2] < -Gradient_Limit) G_hc1[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 477 //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2];
Lightvalve 173:68c7914679ec 478 }
Lightvalve 173:68c7914679ec 479 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 480 float d_V_d_bc1 = 0.0f;
Lightvalve 173:68c7914679ec 481 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 482 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 483 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 484 d_V_d_bc1 = d_V_d_bc1 + hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 485 }
Lightvalve 173:68c7914679ec 486 }
Lightvalve 173:68c7914679ec 487 }
Lightvalve 177:8e9cf31d63f4 488 G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1);
Lightvalve 173:68c7914679ec 489 }
Lightvalve 173:68c7914679ec 490 G_bc1[index2] = G_bc1[index2] / batch_size;
Lightvalve 179:d5377766d7ea 491 if(G_bc1[index2] > Gradient_Limit) G_bc1[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 492 else if (G_bc1[index2] < -Gradient_Limit) G_bc1[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 493 //bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2];
Lightvalve 173:68c7914679ec 494 }
Lightvalve 173:68c7914679ec 495
Lightvalve 175:2f7289dbd488 496
Lightvalve 173:68c7914679ec 497 float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 498 float G_bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 499 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 500 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 501 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 502 float d_V_d_hc2 = 0.0f;
Lightvalve 173:68c7914679ec 503 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 504 if (hx_c_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 505 d_V_d_hc2 = hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 173:68c7914679ec 506 }
Lightvalve 173:68c7914679ec 507 }
Lightvalve 177:8e9cf31d63f4 508 G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2);
Lightvalve 173:68c7914679ec 509 }
Lightvalve 173:68c7914679ec 510 G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 511 if(G_hc2[index1][index2] > Gradient_Limit) G_hc2[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 512 else if (G_hc2[index1][index2] < -Gradient_Limit) G_hc2[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 513 //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 514 }
Lightvalve 173:68c7914679ec 515 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 516 float d_V_d_bc2 = 0.0f;
Lightvalve 173:68c7914679ec 517 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 177:8e9cf31d63f4 518 d_V_d_bc2 = hc3_temp[index2];
Lightvalve 173:68c7914679ec 519 }
Lightvalve 177:8e9cf31d63f4 520 G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2);
Lightvalve 173:68c7914679ec 521 }
Lightvalve 173:68c7914679ec 522 G_bc2[index2] = G_bc2[index2] / batch_size;
Lightvalve 179:d5377766d7ea 523 if(G_bc2[index2] > Gradient_Limit) G_bc2[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 524 else if (G_bc2[index2] < -Gradient_Limit) G_bc2[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 525 //bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2];
Lightvalve 173:68c7914679ec 526 }
Lightvalve 173:68c7914679ec 527
Lightvalve 173:68c7914679ec 528 float G_hc3[num_hidden_unit2]= {0.0f};
Lightvalve 173:68c7914679ec 529 float G_bc3 = 0.0f;
Lightvalve 173:68c7914679ec 530 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 531 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 532 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 533 float d_V_d_hc3 = 0.0f;
Lightvalve 173:68c7914679ec 534 if (hxh_c_sum_array[n][index1] >= 0) {
Lightvalve 177:8e9cf31d63f4 535 d_V_d_hc3 = d_V_d_hc3 + hxh_c_sum_array[n][index1];
Lightvalve 173:68c7914679ec 536 }
Lightvalve 177:8e9cf31d63f4 537 G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3);
Lightvalve 173:68c7914679ec 538 }
Lightvalve 173:68c7914679ec 539 G_hc3[index1] = G_hc3[index1] / batch_size;
Lightvalve 179:d5377766d7ea 540 if(G_hc3[index1] > Gradient_Limit) G_hc3[index1] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 541 else if (G_hc3[index1] < -Gradient_Limit) G_hc3[index1] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 542 //hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1];
Lightvalve 173:68c7914679ec 543 }
Lightvalve 173:68c7914679ec 544 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 545 float d_V_d_bc3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 546 d_V_d_bc3 = 1.0f;
Lightvalve 177:8e9cf31d63f4 547 G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3);
Lightvalve 173:68c7914679ec 548 }
Lightvalve 173:68c7914679ec 549 G_bc3 = G_bc3 / batch_size;
Lightvalve 179:d5377766d7ea 550 if(G_bc3 > Gradient_Limit) G_bc3 = Gradient_Limit;
Lightvalve 179:d5377766d7ea 551 else if (G_bc3 < -Gradient_Limit) G_bc3 = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 552 //bc3_temp = bc3_temp - gradient_rate_critic * G_bc3;
Lightvalve 173:68c7914679ec 553 }
Lightvalve 173:68c7914679ec 554
Lightvalve 173:68c7914679ec 555 // Simultaneous Update
Lightvalve 173:68c7914679ec 556 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 557 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 558 hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2];
Lightvalve 170:42c938a40313 559 }
Lightvalve 179:d5377766d7ea 560 bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2];
Lightvalve 170:42c938a40313 561 }
Lightvalve 173:68c7914679ec 562 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 563 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 564 hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 565 }
Lightvalve 179:d5377766d7ea 566 bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2];
Lightvalve 173:68c7914679ec 567 }
Lightvalve 170:42c938a40313 568 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 569 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 570 hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1];
Lightvalve 170:42c938a40313 571 }
Lightvalve 179:d5377766d7ea 572 bc3_temp = bc3_temp - gradient_rate_critic * G_bc3;
Lightvalve 170:42c938a40313 573 }
Lightvalve 170:42c938a40313 574 }
Lightvalve 170:42c938a40313 575
Lightvalve 173:68c7914679ec 576 ///////////////////////////Softplus//////////////////////////////////
Lightvalve 170:42c938a40313 577 void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 578 {
Lightvalve 179:d5377766d7ea 579
Lightvalve 173:68c7914679ec 580
Lightvalve 173:68c7914679ec 581 float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 582 float G_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 583
Lightvalve 173:68c7914679ec 584 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 585 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 586 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 587 float d_x_d_ha1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 588 float d_y_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 589 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 590 G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 170:42c938a40313 591 } else {
Lightvalve 173:68c7914679ec 592 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 593 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 594 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 595 d_x_d_ha1 = d_x_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 177:8e9cf31d63f4 596 d_y_d_ha1 = d_y_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 597 }
Lightvalve 171:bfc1fd2629d8 598 }
Lightvalve 170:42c938a40313 599 }
Lightvalve 170:42c938a40313 600 float d_mean_d_ha1 = 0.0f;
Lightvalve 170:42c938a40313 601 float d_dev_d_ha1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 602 d_mean_d_ha1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha1;
Lightvalve 177:8e9cf31d63f4 603 d_dev_d_ha1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha1;
Lightvalve 173:68c7914679ec 604
Lightvalve 173:68c7914679ec 605 G_ha1[index1][index2] = G_ha1[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 606 }
Lightvalve 170:42c938a40313 607 }
Lightvalve 176:589ea3edcf3c 608 G_ha1[index1][index2] = -G_ha1[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 609 if(G_ha1[index1][index2] > Gradient_Limit) G_ha1[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 610 else if (G_ha1[index1][index2] < -Gradient_Limit) G_ha1[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 611 //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2];
Lightvalve 170:42c938a40313 612 }
Lightvalve 173:68c7914679ec 613
Lightvalve 173:68c7914679ec 614 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 615 float d_x_d_ba1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 616 float d_y_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 617 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 618 G_ba1[index2] = G_ba1[index2];
Lightvalve 170:42c938a40313 619 } else {
Lightvalve 173:68c7914679ec 620 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 621 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 622 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 623 d_x_d_ba1 = d_x_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 177:8e9cf31d63f4 624 d_y_d_ba1 = d_y_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 625 }
Lightvalve 171:bfc1fd2629d8 626 }
Lightvalve 170:42c938a40313 627 }
Lightvalve 170:42c938a40313 628 float d_mean_d_ba1 = 0.0f;
Lightvalve 170:42c938a40313 629 float d_dev_d_ba1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 630 d_mean_d_ba1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba1;
Lightvalve 177:8e9cf31d63f4 631 d_dev_d_ba1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba1;
Lightvalve 173:68c7914679ec 632
Lightvalve 173:68c7914679ec 633 G_ba1[index2] = G_ba1[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 634 }
Lightvalve 170:42c938a40313 635 }
Lightvalve 176:589ea3edcf3c 636 G_ba1[index2] = -G_ba1[index2] / batch_size;
Lightvalve 179:d5377766d7ea 637 if(G_ba1[index2] > Gradient_Limit) G_ba1[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 638 else if (G_ba1[index2] < -Gradient_Limit) G_ba1[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 639 //ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2];
Lightvalve 170:42c938a40313 640 }
Lightvalve 170:42c938a40313 641
Lightvalve 173:68c7914679ec 642 float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 643 float G_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 644
Lightvalve 173:68c7914679ec 645 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 646 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 647 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 648 float d_x_d_ha2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 649 float d_y_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 650 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 651 G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 170:42c938a40313 652 } else {
Lightvalve 173:68c7914679ec 653 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 654 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 655 d_x_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][0];
Lightvalve 177:8e9cf31d63f4 656 d_y_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][1];
Lightvalve 171:bfc1fd2629d8 657 }
Lightvalve 170:42c938a40313 658 }
Lightvalve 173:68c7914679ec 659
Lightvalve 170:42c938a40313 660 float d_mean_d_ha2 = 0.0f;
Lightvalve 170:42c938a40313 661 float d_dev_d_ha2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 662 d_mean_d_ha2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha2;
Lightvalve 177:8e9cf31d63f4 663 d_dev_d_ha2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha2;
Lightvalve 173:68c7914679ec 664
Lightvalve 173:68c7914679ec 665 G_ha2[index1][index2] = G_ha2[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 666 }
Lightvalve 170:42c938a40313 667 }
Lightvalve 176:589ea3edcf3c 668 G_ha2[index1][index2] = -G_ha2[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 669 if(G_ha2[index1][index2] > Gradient_Limit) G_ha2[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 670 else if (G_ha2[index1][index2] < -Gradient_Limit) G_ha2[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 671 //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2];
Lightvalve 170:42c938a40313 672 }
Lightvalve 173:68c7914679ec 673
Lightvalve 173:68c7914679ec 674 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 675 float d_x_d_ba2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 676 float d_y_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 677 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 678 G_ba2[index2] = G_ba2[index2];
Lightvalve 170:42c938a40313 679 } else {
Lightvalve 170:42c938a40313 680
Lightvalve 173:68c7914679ec 681 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 177:8e9cf31d63f4 682 d_x_d_ba2 = ha3_temp[index2][0];
Lightvalve 177:8e9cf31d63f4 683 d_y_d_ba2 = ha3_temp[index2][1];
Lightvalve 173:68c7914679ec 684 }
Lightvalve 173:68c7914679ec 685 float d_mean_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 686 float d_dev_d_ba2= 0.0f;
Lightvalve 177:8e9cf31d63f4 687 d_mean_d_ba2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba2;
Lightvalve 177:8e9cf31d63f4 688 d_dev_d_ba2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba2;
Lightvalve 173:68c7914679ec 689
Lightvalve 173:68c7914679ec 690 G_ba2[index2] = G_ba2[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 691 }
Lightvalve 170:42c938a40313 692 }
Lightvalve 176:589ea3edcf3c 693 G_ba2[index2] = -G_ba2[index2] / batch_size;
Lightvalve 179:d5377766d7ea 694 if(G_ba2[index2] > Gradient_Limit) G_ba2[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 695 else if (G_ba2[index2] < -Gradient_Limit) G_ba2[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 696 //ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2];
Lightvalve 170:42c938a40313 697 }
Lightvalve 173:68c7914679ec 698
Lightvalve 173:68c7914679ec 699 float G_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 700 float G_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 701
Lightvalve 173:68c7914679ec 702 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 703 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 704 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 705 float d_x_d_ha3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 706 float d_y_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 707 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 708 G_ha3[index1][index2] = G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 709 } else {
Lightvalve 173:68c7914679ec 710 if (hxh_a_sum_array[n][index1] >= 0) {
Lightvalve 173:68c7914679ec 711 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 712 d_x_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 177:8e9cf31d63f4 713 d_y_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 714 }
Lightvalve 173:68c7914679ec 715 }
Lightvalve 173:68c7914679ec 716 float d_mean_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 717 float d_dev_d_ha3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 718 d_mean_d_ha3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha3;
Lightvalve 177:8e9cf31d63f4 719 d_dev_d_ha3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha3;
Lightvalve 173:68c7914679ec 720
Lightvalve 173:68c7914679ec 721 G_ha3[index1][index2] = G_ha3[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 722 }
Lightvalve 173:68c7914679ec 723 }
Lightvalve 176:589ea3edcf3c 724 G_ha3[index1][index2] = -G_ha3[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 725 if(G_ha3[index1][index2] > Gradient_Limit) G_ha3[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 726 else if (G_ha3[index1][index2] < -Gradient_Limit) G_ha3[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 727 //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 728 }
Lightvalve 173:68c7914679ec 729
Lightvalve 173:68c7914679ec 730 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 731 float d_x_d_ba3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 732 float d_y_d_ba3 = 0.0f;
Lightvalve 173:68c7914679ec 733 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 734 G_ba3[index2] = G_ba3[index2];
Lightvalve 173:68c7914679ec 735 } else {
Lightvalve 173:68c7914679ec 736
Lightvalve 177:8e9cf31d63f4 737 d_x_d_ba3 = 1.0f;
Lightvalve 177:8e9cf31d63f4 738 d_y_d_ba3 = 1.0f;
Lightvalve 173:68c7914679ec 739
Lightvalve 173:68c7914679ec 740 float d_mean_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 741 float d_dev_d_ba3= 0.0f;
Lightvalve 177:8e9cf31d63f4 742 d_mean_d_ba3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba3;
Lightvalve 177:8e9cf31d63f4 743 d_dev_d_ba3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba3;
Lightvalve 173:68c7914679ec 744
Lightvalve 173:68c7914679ec 745 G_ba3[index2] = G_ba3[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 746 }
Lightvalve 173:68c7914679ec 747 }
Lightvalve 176:589ea3edcf3c 748 G_ba3[index2] = -G_ba3[index2] / batch_size;
Lightvalve 179:d5377766d7ea 749 if(G_ba3[index2] > Gradient_Limit) G_ba3[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 750 else if (G_ba3[index2] < -Gradient_Limit) G_ba3[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 751 //ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2];
Lightvalve 175:2f7289dbd488 752 }
Lightvalve 177:8e9cf31d63f4 753
Lightvalve 175:2f7289dbd488 754 // Simultaneous Update
Lightvalve 175:2f7289dbd488 755 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 175:2f7289dbd488 756 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 757 ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2];
Lightvalve 175:2f7289dbd488 758 }
Lightvalve 179:d5377766d7ea 759 ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2];
Lightvalve 175:2f7289dbd488 760 }
Lightvalve 175:2f7289dbd488 761 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 175:2f7289dbd488 762 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 763 ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2];
Lightvalve 175:2f7289dbd488 764 }
Lightvalve 179:d5377766d7ea 765 ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2];
Lightvalve 175:2f7289dbd488 766 }
Lightvalve 175:2f7289dbd488 767 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 175:2f7289dbd488 768 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 769 ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2];
Lightvalve 175:2f7289dbd488 770 }
Lightvalve 179:d5377766d7ea 771 ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2];
Lightvalve 173:68c7914679ec 772 }
Lightvalve 170:42c938a40313 773 }
Lightvalve 170:42c938a40313 774
Lightvalve 170:42c938a40313 775 float rand_normal(double mean, double stddev)
Lightvalve 170:42c938a40313 776 {
Lightvalve 170:42c938a40313 777 //Box muller method
Lightvalve 170:42c938a40313 778 static double n2 = 0.0f;
Lightvalve 170:42c938a40313 779 static int n2_cached = 0;
Lightvalve 170:42c938a40313 780 if (!n2_cached) {
Lightvalve 170:42c938a40313 781 double x, y, r;
Lightvalve 170:42c938a40313 782 do {
Lightvalve 170:42c938a40313 783 x = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 784 y = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 785
Lightvalve 170:42c938a40313 786 r = x*x + y*y;
Lightvalve 170:42c938a40313 787 } while (r == 0.0f || r > 1.0f);
Lightvalve 170:42c938a40313 788 {
Lightvalve 170:42c938a40313 789 double d = sqrt(-2.0f*log(r)/r);
Lightvalve 170:42c938a40313 790 double n1 = x*d;
Lightvalve 170:42c938a40313 791 n2 = y*d;
Lightvalve 170:42c938a40313 792 double result = n1*stddev + mean;
Lightvalve 170:42c938a40313 793 n2_cached = 1;
Lightvalve 170:42c938a40313 794 return result;
Lightvalve 170:42c938a40313 795 }
Lightvalve 170:42c938a40313 796 } else {
Lightvalve 170:42c938a40313 797 n2_cached = 0;
Lightvalve 170:42c938a40313 798 return n2*stddev + mean;
Lightvalve 170:42c938a40313 799 }
Lightvalve 170:42c938a40313 800 }
Lightvalve 170:42c938a40313 801
Lightvalve 179:d5377766d7ea 802 float mean_adv(float x[], int size)
Lightvalve 179:d5377766d7ea 803 {
Lightvalve 179:d5377766d7ea 804 float add = 0.0f;
Lightvalve 179:d5377766d7ea 805 float result;
Lightvalve 179:d5377766d7ea 806
Lightvalve 179:d5377766d7ea 807 for (int i=0; i<size; i++)
Lightvalve 179:d5377766d7ea 808 {
Lightvalve 179:d5377766d7ea 809 add += x[i];
Lightvalve 179:d5377766d7ea 810 }
Lightvalve 179:d5377766d7ea 811 result = (float) add/size;
Lightvalve 179:d5377766d7ea 812 return result;
Lightvalve 179:d5377766d7ea 813 }
Lightvalve 179:d5377766d7ea 814 float deviation_adv(float x[], int size)
Lightvalve 179:d5377766d7ea 815 {
Lightvalve 179:d5377766d7ea 816 float sigma = 0.0f;
Lightvalve 179:d5377766d7ea 817 float resultDeb = 0.0f;
Lightvalve 179:d5377766d7ea 818
Lightvalve 179:d5377766d7ea 819 for (int k=0; k<size; k++)
Lightvalve 179:d5377766d7ea 820 {
Lightvalve 179:d5377766d7ea 821 sigma = pow((float)x[k]-mean_adv(x,size), (float)2.0f)/(size-1);
Lightvalve 179:d5377766d7ea 822 resultDeb += sqrt(sigma);
Lightvalve 179:d5377766d7ea 823 }
Lightvalve 179:d5377766d7ea 824 return resultDeb;
Lightvalve 179:d5377766d7ea 825 }
Lightvalve 179:d5377766d7ea 826
Lightvalve 173:68c7914679ec 827
Lightvalve 170:42c938a40313 828 void Overwirte_Critic_Networks()
Lightvalve 170:42c938a40313 829 {
Lightvalve 173:68c7914679ec 830 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 831 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 832 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 170:42c938a40313 833 }
Lightvalve 170:42c938a40313 834 bc1[index2] = bc1_temp[index2];
Lightvalve 170:42c938a40313 835 }
Lightvalve 173:68c7914679ec 836 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 837 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 838 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 173:68c7914679ec 839 }
Lightvalve 173:68c7914679ec 840 bc2[index2] = bc2_temp[index2];
Lightvalve 173:68c7914679ec 841 hc3[index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 842 }
Lightvalve 173:68c7914679ec 843 bc3 = bc3_temp;
Lightvalve 170:42c938a40313 844 }
Lightvalve 170:42c938a40313 845 void Overwirte_Actor_Networks()
Lightvalve 170:42c938a40313 846 {
Lightvalve 173:68c7914679ec 847 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 848 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 849 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 170:42c938a40313 850 }
Lightvalve 170:42c938a40313 851 ba1[index2] = ba1_temp[index2];
Lightvalve 170:42c938a40313 852 }
Lightvalve 173:68c7914679ec 853 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 854 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:42c938a40313 855 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 170:42c938a40313 856 }
Lightvalve 170:42c938a40313 857 ba2[index2] = ba2_temp[index2];
Lightvalve 170:42c938a40313 858 }
Lightvalve 173:68c7914679ec 859 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 860 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 861 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 173:68c7914679ec 862 }
Lightvalve 173:68c7914679ec 863 ba3[index2] = ba3_temp[index2];
Lightvalve 173:68c7914679ec 864 }
Lightvalve 170:42c938a40313 865 }
Lightvalve 170:42c938a40313 866
Lightvalve 170:42c938a40313 867
GiJeongKim 0:51c43836c1d7 868 int main()
GiJeongKim 0:51c43836c1d7 869 {
Lightvalve 66:a8e6799dbce3 870
Lightvalve 65:a2d7c63419c2 871 HAL_Init();
Lightvalve 65:a2d7c63419c2 872 SystemClock_Config();
Lightvalve 169:645207e160ca 873
jobuuu 6:df07d3491e3a 874 /*********************************
jobuuu 1:e04e563be5ce 875 *** Initialization
jobuuu 6:df07d3491e3a 876 *********************************/
Lightvalve 69:3995ffeaa786 877 LED = 0;
Lightvalve 61:bc8c8270f0ab 878 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 879
GiJeongKim 0:51c43836c1d7 880 // i2c init
Lightvalve 8:5d2eebdad025 881 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 882 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 883 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 884 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 885 make_delay();
jobuuu 2:a1c0a37df760 886
GiJeongKim 0:51c43836c1d7 887 // // spi init
Lightvalve 170:42c938a40313 888 eeprom.format(8,3);
Lightvalve 170:42c938a40313 889 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 890 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 891 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 892 make_delay();
Lightvalve 21:e5f1a43ea6f9 893
Lightvalve 16:903b5a4433b4 894 //rom
Lightvalve 19:23b7c1ad8683 895 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 896 make_delay();
Lightvalve 13:747daba9cf59 897
GiJeongKim 0:51c43836c1d7 898 // ADC init
jobuuu 5:a4319f79457b 899 Init_ADC();
Lightvalve 11:82d8768d7351 900 make_delay();
jobuuu 2:a1c0a37df760 901
GiJeongKim 0:51c43836c1d7 902 // Pwm init
GiJeongKim 0:51c43836c1d7 903 Init_PWM();
GiJeongKim 0:51c43836c1d7 904 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 905 make_delay();
Lightvalve 13:747daba9cf59 906
Lightvalve 11:82d8768d7351 907 // TMR3 init
Lightvalve 11:82d8768d7351 908 Init_TMR3();
Lightvalve 11:82d8768d7351 909 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 910 make_delay();
Lightvalve 21:e5f1a43ea6f9 911
Lightvalve 50:3c630b5eba9f 912 // TMR2 init
Lightvalve 56:6f50d9d3bfee 913 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 914 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 915 // make_delay();
Lightvalve 21:e5f1a43ea6f9 916
GiJeongKim 0:51c43836c1d7 917 // CAN
jobuuu 2:a1c0a37df760 918 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 919 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 920 make_delay();
Lightvalve 34:bb2ca2fc2a8e 921
Lightvalve 23:59218d4a256d 922 //Timer priority
Lightvalve 23:59218d4a256d 923 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 57:f4819de54e7a 924 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 925 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 926
Lightvalve 23:59218d4a256d 927 //can.reset();
Lightvalve 19:23b7c1ad8683 928 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 929
GiJeongKim 0:51c43836c1d7 930 // spi _ enc
GiJeongKim 0:51c43836c1d7 931 spi_enc_set_init();
Lightvalve 11:82d8768d7351 932 make_delay();
Lightvalve 13:747daba9cf59 933
Lightvalve 11:82d8768d7351 934 //DAC init
Lightvalve 58:2eade98630e2 935 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 936 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 937 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 938 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 939 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 58:2eade98630e2 940 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 58:2eade98630e2 941 }
Lightvalve 11:82d8768d7351 942 make_delay();
Lightvalve 13:747daba9cf59 943
Lightvalve 19:23b7c1ad8683 944 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 945 if(i%2==0)
Lightvalve 38:118df027d851 946 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 947 else
Lightvalve 38:118df027d851 948 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 949 }
Lightvalve 169:645207e160ca 950
Lightvalve 173:68c7914679ec 951 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 952 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 953 hc1_temp[index1][index2] = (float) (rand()%100) * 0.007f ;
Lightvalve 170:42c938a40313 954 }
Lightvalve 179:d5377766d7ea 955 bc1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 956 }
Lightvalve 173:68c7914679ec 957 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 958 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 959 hc2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 960 }
Lightvalve 179:d5377766d7ea 961 bc2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 179:d5377766d7ea 962 hc3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 963 }
Lightvalve 179:d5377766d7ea 964 bc3_temp = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 965
Lightvalve 173:68c7914679ec 966 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 967 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 968 ha1_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 969 }
Lightvalve 179:d5377766d7ea 970 ba1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 971 }
Lightvalve 173:68c7914679ec 972 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 973 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 974 ha2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 975 }
Lightvalve 179:d5377766d7ea 976 ba2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 977 }
Lightvalve 170:42c938a40313 978 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 979 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 980 ha3_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 981 }
Lightvalve 179:d5377766d7ea 982 ba3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 983 }
Lightvalve 171:bfc1fd2629d8 984
Lightvalve 170:42c938a40313 985 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 986 Overwirte_Actor_Networks();
Lightvalve 61:bc8c8270f0ab 987
jobuuu 6:df07d3491e3a 988 /************************************
jobuuu 1:e04e563be5ce 989 *** Program is operating!
jobuuu 6:df07d3491e3a 990 *************************************/
GiJeongKim 0:51c43836c1d7 991 while(1) {
Lightvalve 169:645207e160ca 992
Lightvalve 171:bfc1fd2629d8 993 // if(timer_while==27491) {
Lightvalve 171:bfc1fd2629d8 994 // timer_while = 0;
Lightvalve 171:bfc1fd2629d8 995 // 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 996 // //pc.printf("%f\n", virt_pos);
Lightvalve 171:bfc1fd2629d8 997 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 171:bfc1fd2629d8 998 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 171:bfc1fd2629d8 999 // }
Lightvalve 169:645207e160ca 1000
Lightvalve 171:bfc1fd2629d8 1001
Lightvalve 171:bfc1fd2629d8 1002 //i2c
Lightvalve 180:02be1711ee0b 1003 read_field(i2c_slave_addr1);
Lightvalve 180:02be1711ee0b 1004 if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 171:bfc1fd2629d8 1005
Lightvalve 177:8e9cf31d63f4 1006 //timer_while ++;
Lightvalve 169:645207e160ca 1007
Lightvalve 170:42c938a40313 1008 ///////////////////////////////////////////////////////Neural Network
Lightvalve 169:645207e160ca 1009
Lightvalve 73:f80dc3970c99 1010 if(NN_Control_Flag == 0) {
Lightvalve 73:f80dc3970c99 1011 LED = 0;
Lightvalve 73:f80dc3970c99 1012 }
Lightvalve 169:645207e160ca 1013
Lightvalve 73:f80dc3970c99 1014 else if(NN_Control_Flag == 1) {
Lightvalve 169:645207e160ca 1015
Lightvalve 162:9dd4f35e9de8 1016 int ind = 0;
Lightvalve 162:9dd4f35e9de8 1017 for(int i=0; i<numpast_u; i++) {
Lightvalve 162:9dd4f35e9de8 1018 input_NN[ind] = u_past[time_interval*i];
Lightvalve 162:9dd4f35e9de8 1019 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1020 }
Lightvalve 162:9dd4f35e9de8 1021
Lightvalve 162:9dd4f35e9de8 1022 for(int i=0; i<numpast_x; i++) {
Lightvalve 162:9dd4f35e9de8 1023 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 162:9dd4f35e9de8 1024 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1025 }
Lightvalve 162:9dd4f35e9de8 1026 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 162:9dd4f35e9de8 1027 ind = ind + 1;
Lightvalve 169:645207e160ca 1028
Lightvalve 162:9dd4f35e9de8 1029 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 162:9dd4f35e9de8 1030 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 122:dcb3ce3056a0 1031 // ind = ind + 1;
Lightvalve 122:dcb3ce3056a0 1032 // }
Lightvalve 162:9dd4f35e9de8 1033
Lightvalve 162:9dd4f35e9de8 1034 for(int i=0; i<numpast_f; i++) {
Lightvalve 208:408f9f15c486 1035 // input_NN[ind] = f_past[time_interval*i] / 10000.0f * 8.0f + 0.5f;
Lightvalve 208:408f9f15c486 1036 input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1037 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1038 }
Lightvalve 208:408f9f15c486 1039 // input_NN[ind] = torq.sen / 10000.0f * 8.0f + 0.5f;
Lightvalve 208:408f9f15c486 1040 input_NN[ind] = torq.sen / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1041 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1042 for(int i=0; i<numfuture_f; i++) {
Lightvalve 208:408f9f15c486 1043 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f * 8.0f + 0.5f;
Lightvalve 212:ec41f1449ef9 1044 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f + 0.5f;
Lightvalve 206:2e4d0c287578 1045 // input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f*8.0f+0.5f;
Lightvalve 212:ec41f1449ef9 1046 input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1047 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1048 }
Lightvalve 169:645207e160ca 1049
Lightvalve 112:8dcb1600cb90 1050 float output1[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1051 float output2[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1052 float output3[16] = { 0.0f };
Lightvalve 66:a8e6799dbce3 1053 float output = 0.0f;
Lightvalve 65:a2d7c63419c2 1054
Lightvalve 112:8dcb1600cb90 1055 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 68:328e1be06f5d 1056 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 66:a8e6799dbce3 1057 output1[index2] = output1[index2]
Lightvalve 73:f80dc3970c99 1058 + h1[index1][index2] * input_NN[index1];
Lightvalve 66:a8e6799dbce3 1059 }
Lightvalve 66:a8e6799dbce3 1060 output1[index2] = output1[index2] + b1[index2];
Lightvalve 66:a8e6799dbce3 1061 if (output1[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1062 output1[index2] = 0;
Lightvalve 66:a8e6799dbce3 1063 }
Lightvalve 66:a8e6799dbce3 1064 }
Lightvalve 65:a2d7c63419c2 1065
Lightvalve 112:8dcb1600cb90 1066 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1067 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1068 output2[index2] = output2[index2]
Lightvalve 66:a8e6799dbce3 1069 + h2[index1][index2] * output1[index1];
Lightvalve 66:a8e6799dbce3 1070 }
Lightvalve 66:a8e6799dbce3 1071 output2[index2] = output2[index2] + b2[index2];
Lightvalve 66:a8e6799dbce3 1072 if (output2[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1073 output2[index2] = 0;
Lightvalve 66:a8e6799dbce3 1074 }
Lightvalve 66:a8e6799dbce3 1075 }
Lightvalve 65:a2d7c63419c2 1076
Lightvalve 112:8dcb1600cb90 1077 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1078 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1079 output3[index2] = output3[index2]
Lightvalve 66:a8e6799dbce3 1080 + h3[index1][index2] * output2[index1];
Lightvalve 66:a8e6799dbce3 1081 }
Lightvalve 66:a8e6799dbce3 1082 output3[index2] = output3[index2] + b3[index2];
Lightvalve 66:a8e6799dbce3 1083 if (output3[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1084 output3[index2] = 0;
Lightvalve 66:a8e6799dbce3 1085 }
Lightvalve 65:a2d7c63419c2 1086 }
Lightvalve 66:a8e6799dbce3 1087
Lightvalve 66:a8e6799dbce3 1088 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 112:8dcb1600cb90 1089 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 73:f80dc3970c99 1090 output = output + hout[index1] * output3[index1];
Lightvalve 66:a8e6799dbce3 1091 }
Lightvalve 66:a8e6799dbce3 1092 output = output + bout[index2];
Lightvalve 169:645207e160ca 1093
Lightvalve 66:a8e6799dbce3 1094 }
Lightvalve 73:f80dc3970c99 1095 output = 1.0f/(1.0f+exp(-output));
Lightvalve 101:50159049a518 1096 output_normalized = output;
Lightvalve 68:328e1be06f5d 1097 output = output * 20000.0f - 10000.0f;
Lightvalve 169:645207e160ca 1098
Lightvalve 66:a8e6799dbce3 1099 if(output>=0) {
Lightvalve 66:a8e6799dbce3 1100 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 66:a8e6799dbce3 1101 } else {
Lightvalve 66:a8e6799dbce3 1102 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 65:a2d7c63419c2 1103 }
Lightvalve 87:471334725012 1104
Lightvalve 169:645207e160ca 1105
Lightvalve 69:3995ffeaa786 1106 if(LED==1) {
Lightvalve 69:3995ffeaa786 1107 LED=0;
Lightvalve 69:3995ffeaa786 1108 } else
Lightvalve 69:3995ffeaa786 1109 LED = 1;
Lightvalve 169:645207e160ca 1110
Lightvalve 65:a2d7c63419c2 1111 }
Lightvalve 171:bfc1fd2629d8 1112
Lightvalve 171:bfc1fd2629d8 1113
Lightvalve 170:42c938a40313 1114 /////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 1115 switch (Update_Case) {
Lightvalve 170:42c938a40313 1116 case 0: {
Lightvalve 170:42c938a40313 1117 break;
Lightvalve 170:42c938a40313 1118 }
Lightvalve 170:42c938a40313 1119 case 1: {
Lightvalve 170:42c938a40313 1120 //Network Update(just update and hold network)
Lightvalve 170:42c938a40313 1121 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 170:42c938a40313 1122 float loss_sum = 0.0f;
Lightvalve 175:2f7289dbd488 1123 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 170:42c938a40313 1124 //Calculate Estimated V
Lightvalve 175:2f7289dbd488 1125 //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]};
Lightvalve 175:2f7289dbd488 1126 float temp_array[2] = {state_array[n][0], state_array[n][1]};
Lightvalve 175:2f7289dbd488 1127 V[n] = Critic_Network_Temp(temp_array);
Lightvalve 173:68c7914679ec 1128 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 175:2f7289dbd488 1129 hx_c_sum_array[n][i] = hx_c_sum[i];
Lightvalve 173:68c7914679ec 1130 }
Lightvalve 173:68c7914679ec 1131 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 175:2f7289dbd488 1132 hxh_c_sum_array[n][i] = hxh_c_sum[i];
Lightvalve 173:68c7914679ec 1133 }
Lightvalve 175:2f7289dbd488 1134 hxhh_c_sum_array[n] = hxhh_c_sum;
Lightvalve 177:8e9cf31d63f4 1135
Lightvalve 175:2f7289dbd488 1136 pi[n] = exp(-(action_array[n]-mean_array[n])*(action_array[n]-mean_array[n])/(2.0f*deviation_array[n]*deviation_array[n]))/(sqrt(2.0f*PI)*deviation_array[n]);
Lightvalve 170:42c938a40313 1137 Actor_Network_Old(temp_array);
Lightvalve 175:2f7289dbd488 1138 pi_old[n] = exp(-(action_array[n]-mean_old)*(action_array[n]-mean_old)/(2.0f*deviation_old*deviation_old))/(sqrt(2.0f*PI)*deviation_old);
Lightvalve 179:d5377766d7ea 1139 r[n] = exp(-0.25f * 5.0f * state_array[n][1] * state_array[n][1]);
Lightvalve 175:2f7289dbd488 1140 if(n == batch_size-1) return_G[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1141 else return_G[n] = gamma * return_G[n+1] + r[n];
Lightvalve 175:2f7289dbd488 1142 if(n == batch_size-1) td_target[n] = r[n];
Lightvalve 175:2f7289dbd488 1143 else td_target[n] = r[n] + gamma * V[n+1];
Lightvalve 175:2f7289dbd488 1144 delta[n] = td_target[n] - V[n];
Lightvalve 175:2f7289dbd488 1145 if(n == batch_size-1) advantage[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1146 else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n];
Lightvalve 179:d5377766d7ea 1147 // return_G[n] = advantage[n] + V[n];
Lightvalve 175:2f7289dbd488 1148 ratio[n] = pi[n]/pi_old[n];
Lightvalve 179:d5377766d7ea 1149 }
Lightvalve 179:d5377766d7ea 1150 float mean_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1151 float dev_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1152 mean_advantage = mean_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1153 dev_advantage = deviation_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1154 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 179:d5377766d7ea 1155 //advantage[n] = (advantage[n]-mean_advantage)/dev_advantage;
Lightvalve 175:2f7289dbd488 1156 surr1[n] = ratio[n] * advantage[n];
Lightvalve 175:2f7289dbd488 1157 if (ratio[n] > 1.0f + epsilon) {
Lightvalve 175:2f7289dbd488 1158 surr2[n] = (1.0f + epsilon)*advantage[n];
Lightvalve 175:2f7289dbd488 1159 } else if( ratio[n] < 1.0f - epsilon) {
Lightvalve 175:2f7289dbd488 1160 surr2[n] = (1.0f - epsilon)*advantage[n];
Lightvalve 170:42c938a40313 1161 } else {
Lightvalve 175:2f7289dbd488 1162 surr2[n] = ratio[n]*advantage[n];
Lightvalve 170:42c938a40313 1163 }
Lightvalve 175:2f7289dbd488 1164 loss[n] = -min(surr1[n], surr2[n]);
Lightvalve 175:2f7289dbd488 1165 loss_sum = loss_sum + loss[n];
Lightvalve 170:42c938a40313 1166 }
Lightvalve 170:42c938a40313 1167 reward_sum = 0.0f;
Lightvalve 170:42c938a40313 1168 for (int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 1169 reward_sum = reward_sum + r[i];
Lightvalve 170:42c938a40313 1170 }
Lightvalve 170:42c938a40313 1171 logging5 = reward_sum;
Lightvalve 171:bfc1fd2629d8 1172
Lightvalve 171:bfc1fd2629d8 1173
Lightvalve 170:42c938a40313 1174 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 170:42c938a40313 1175 loss_batch = loss_sum;
Lightvalve 170:42c938a40313 1176 //Update Networks
Lightvalve 170:42c938a40313 1177 update_Critic_Networks(state_array);
Lightvalve 170:42c938a40313 1178 update_Actor_Networks(state_array);
Lightvalve 170:42c938a40313 1179 }
Lightvalve 170:42c938a40313 1180 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1181 Update_Case = 0;
Lightvalve 170:42c938a40313 1182 //logging1 = V[0];
Lightvalve 171:bfc1fd2629d8 1183
Lightvalve 170:42c938a40313 1184 break;
Lightvalve 170:42c938a40313 1185 }
Lightvalve 170:42c938a40313 1186 case 2: {
Lightvalve 170:42c938a40313 1187 //Network apply to next Network
Lightvalve 170:42c938a40313 1188 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1189 Overwirte_Actor_Networks();
Lightvalve 170:42c938a40313 1190 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1191 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1192 Update_Case = 0;
Lightvalve 170:42c938a40313 1193 break;
Lightvalve 170:42c938a40313 1194 }
Lightvalve 169:645207e160ca 1195
Lightvalve 170:42c938a40313 1196 }
GiJeongKim 0:51c43836c1d7 1197 }
jobuuu 1:e04e563be5ce 1198 }
jobuuu 1:e04e563be5ce 1199
Lightvalve 33:91b17819ec30 1200 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1201 {
Lightvalve 14:8e7590227d22 1202
Lightvalve 13:747daba9cf59 1203 int i = 0;
Lightvalve 48:889798ff9329 1204 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1205 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1206 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 1207 if(i==0) {
Lightvalve 50:3c630b5eba9f 1208 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 1209 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1210 } else {
Lightvalve 57:f4819de54e7a 1211 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 1212 }
Lightvalve 14:8e7590227d22 1213 } else {
Lightvalve 50:3c630b5eba9f 1214 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 1215 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1216 } else {
Lightvalve 57:f4819de54e7a 1217 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 1218 }
Lightvalve 13:747daba9cf59 1219 }
Lightvalve 13:747daba9cf59 1220 break;
Lightvalve 13:747daba9cf59 1221 }
Lightvalve 13:747daba9cf59 1222 }
Lightvalve 14:8e7590227d22 1223 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1224 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1225 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1226 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1227 }
Lightvalve 36:a46e63505ed8 1228
Lightvalve 57:f4819de54e7a 1229 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1230 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 1231
Lightvalve 13:747daba9cf59 1232 }
jobuuu 6:df07d3491e3a 1233
jobuuu 6:df07d3491e3a 1234
Lightvalve 30:8d561f16383b 1235 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1236 {
Lightvalve 13:747daba9cf59 1237 int i = 0;
Lightvalve 13:747daba9cf59 1238
Lightvalve 38:118df027d851 1239 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1240 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1241 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1242 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1243 }
Lightvalve 38:118df027d851 1244
Lightvalve 89:a7b45368ea0f 1245 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1246 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1247 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1248 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 1249 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 1250 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1251
Lightvalve 13:747daba9cf59 1252 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 1253
Lightvalve 18:b8adf1582ea3 1254 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 1255 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 1256 if(i==0) {
Lightvalve 48:889798ff9329 1257 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 1258 } else {
Lightvalve 48:889798ff9329 1259 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 1260 }
Lightvalve 13:747daba9cf59 1261 break;
Lightvalve 13:747daba9cf59 1262 }
Lightvalve 13:747daba9cf59 1263 }
Lightvalve 59:f308b1656d9c 1264 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1265 }
Lightvalve 13:747daba9cf59 1266
Lightvalve 14:8e7590227d22 1267 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1268 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 1269 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1270 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1271 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1272 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1273 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1274 }; // duty
Lightvalve 67:c2812cf26c38 1275 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 1276 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 1277 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 1278 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 1279 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 1280 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1281 }; // mV
Lightvalve 13:747daba9cf59 1282
Lightvalve 30:8d561f16383b 1283 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1284 {
Lightvalve 30:8d561f16383b 1285 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1286 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1287 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1288 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1289 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1290 } else {
Lightvalve 13:747daba9cf59 1291 int idx = 0;
Lightvalve 13:747daba9cf59 1292 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1293 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1294 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1295 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1296 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1297 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1298 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1299 break;
Lightvalve 13:747daba9cf59 1300 }
Lightvalve 13:747daba9cf59 1301 }
Lightvalve 13:747daba9cf59 1302 }
Lightvalve 14:8e7590227d22 1303
Lightvalve 13:747daba9cf59 1304 return PWM_duty;
Lightvalve 13:747daba9cf59 1305 }
jobuuu 6:df07d3491e3a 1306
Lightvalve 57:f4819de54e7a 1307
Lightvalve 57:f4819de54e7a 1308
Lightvalve 57:f4819de54e7a 1309
Lightvalve 57:f4819de54e7a 1310
jobuuu 2:a1c0a37df760 1311 /*******************************************************************************
jobuuu 2:a1c0a37df760 1312 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1313 *******************************************************************************/
jobuuu 2:a1c0a37df760 1314
Lightvalve 51:b46bed7fec80 1315 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 1316 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 1317 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 1318 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1319 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1320 {
Lightvalve 19:23b7c1ad8683 1321 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1322
Lightvalve 21:e5f1a43ea6f9 1323 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1324 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1325 ********************************************************/
Lightvalve 13:747daba9cf59 1326
Lightvalve 57:f4819de54e7a 1327 //Encoder
Lightvalve 57:f4819de54e7a 1328 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 1329 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 1330 }
Lightvalve 61:bc8c8270f0ab 1331
Lightvalve 61:bc8c8270f0ab 1332 ADC1->CR2 |= 0x40000000;
Lightvalve 58:2eade98630e2 1333 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1334 // Torque Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1335 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 58:2eade98630e2 1336 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 1337 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 1338 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 1339
Lightvalve 67:c2812cf26c38 1340
Lightvalve 67:c2812cf26c38 1341 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 67:c2812cf26c38 1342 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 67:c2812cf26c38 1343 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 67:c2812cf26c38 1344 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 67:c2812cf26c38 1345 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 67:c2812cf26c38 1346
Lightvalve 17:1865016ca2e7 1347
Lightvalve 58:2eade98630e2 1348 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1349 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1350 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 58:2eade98630e2 1351 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 58:2eade98630e2 1352 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 58:2eade98630e2 1353 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 1354 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 1355 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 1356 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 1357
Lightvalve 58:2eade98630e2 1358 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 1359 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 1360 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 1361 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 1362 }
Lightvalve 58:2eade98630e2 1363 }
Lightvalve 61:bc8c8270f0ab 1364
Lightvalve 58:2eade98630e2 1365 // //Pressure sensor A
Lightvalve 58:2eade98630e2 1366 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 58:2eade98630e2 1367 // //while((ADC1->SR & 0b10));
Lightvalve 58:2eade98630e2 1368 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1369 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 58:2eade98630e2 1370 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 58:2eade98630e2 1371 // 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 1372 //
Lightvalve 58:2eade98630e2 1373 //
Lightvalve 58:2eade98630e2 1374 // //Pressure sensor B
Lightvalve 58:2eade98630e2 1375 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1376 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 58:2eade98630e2 1377 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 58:2eade98630e2 1378 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1379
Lightvalve 17:1865016ca2e7 1380
Lightvalve 21:e5f1a43ea6f9 1381 //Current
Lightvalve 21:e5f1a43ea6f9 1382 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1383 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1384 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 1385 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1386 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1387 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1388 //cur.sen = raw_cur;
Lightvalve 58:2eade98630e2 1389
Lightvalve 57:f4819de54e7a 1390 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1391 }
Lightvalve 11:82d8768d7351 1392 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1393 }
Lightvalve 19:23b7c1ad8683 1394
Lightvalve 19:23b7c1ad8683 1395
Lightvalve 18:b8adf1582ea3 1396 int j =0;
Lightvalve 54:647072f5307a 1397 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 1398 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 1399 int cnt_trans = 0;
Lightvalve 48:889798ff9329 1400 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 1401 int can_rest =0;
Lightvalve 48:889798ff9329 1402
Lightvalve 11:82d8768d7351 1403 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1404 {
Lightvalve 19:23b7c1ad8683 1405 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 1406
Lightvalve 57:f4819de54e7a 1407 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 1408 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1409 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 1410 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1411 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 1412 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 1413 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1414 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 1415 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1416 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 1417 }
Lightvalve 50:3c630b5eba9f 1418
Lightvalve 50:3c630b5eba9f 1419 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 1420 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1421 cnt_trans++;
Lightvalve 46:2694daea349b 1422 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1423 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 1424 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 1425 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 1426 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1427 cnt_trans++;
Lightvalve 46:2694daea349b 1428 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1429 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 1430 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 1431 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 1432 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 1433 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 1434 } else {
Lightvalve 58:2eade98630e2 1435 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 1436 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 1437 }
Lightvalve 45:35fa6884d0c6 1438
Lightvalve 50:3c630b5eba9f 1439
Lightvalve 57:f4819de54e7a 1440 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 1441 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 1442
Lightvalve 57:f4819de54e7a 1443 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 1444 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1445 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1446 } else {
Lightvalve 57:f4819de54e7a 1447 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1448 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1449 }
Lightvalve 56:6f50d9d3bfee 1450
Lightvalve 56:6f50d9d3bfee 1451
Lightvalve 56:6f50d9d3bfee 1452
Lightvalve 57:f4819de54e7a 1453 // UTILITY MODE ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1454
Lightvalve 57:f4819de54e7a 1455 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 1456 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1457 break;
Lightvalve 13:747daba9cf59 1458 }
Lightvalve 14:8e7590227d22 1459
Lightvalve 14:8e7590227d22 1460 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1461 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1462 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1463 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1464
Lightvalve 14:8e7590227d22 1465 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1466 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1467 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1468
Lightvalve 84:c355d3e52bf1 1469 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1470
Lightvalve 30:8d561f16383b 1471 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 1472 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 50:3c630b5eba9f 1473
Lightvalve 16:903b5a4433b4 1474 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1475 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1476 }
Lightvalve 13:747daba9cf59 1477 } else {
Lightvalve 58:2eade98630e2 1478 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1479 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1480 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1481 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1482
Lightvalve 170:42c938a40313 1483 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1484 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1485
Lightvalve 30:8d561f16383b 1486 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1487
Lightvalve 13:747daba9cf59 1488 }
Lightvalve 14:8e7590227d22 1489 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1490 break;
Lightvalve 19:23b7c1ad8683 1491 }
Lightvalve 14:8e7590227d22 1492
Lightvalve 50:3c630b5eba9f 1493 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 1494 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 1495 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1496 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 1497 // }
Lightvalve 50:3c630b5eba9f 1498 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1499 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1500 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1501 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1502 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1503 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1504 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1505 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1506 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1507 // }
Lightvalve 50:3c630b5eba9f 1508 //
Lightvalve 50:3c630b5eba9f 1509 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1510 // 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 1511 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 1512 //
Lightvalve 50:3c630b5eba9f 1513 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 1514 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1515 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1516 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1517 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1518 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1519 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 1520 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1521 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1522 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1523 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1524 //
Lightvalve 50:3c630b5eba9f 1525 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 1526 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 1527 // // | / | / |/
Lightvalve 50:3c630b5eba9f 1528 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 1529 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 1530 // // /| / | / |
Lightvalve 50:3c630b5eba9f 1531 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 1532 //
Lightvalve 50:3c630b5eba9f 1533 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 1534 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 1535 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1536 // } else {
Lightvalve 50:3c630b5eba9f 1537 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 1538 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1539 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1540 // }
Lightvalve 50:3c630b5eba9f 1541 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 1542 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1543 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1544 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 1545 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1546 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1547 // }
Lightvalve 50:3c630b5eba9f 1548 // } else {
Lightvalve 50:3c630b5eba9f 1549 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 1550 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 1551 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 1552 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1553 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1554 // }
Lightvalve 50:3c630b5eba9f 1555 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1556 //
Lightvalve 50:3c630b5eba9f 1557 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1558 //
Lightvalve 50:3c630b5eba9f 1559 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 1560 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 1561 //
Lightvalve 50:3c630b5eba9f 1562 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1563 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1564 // }
Lightvalve 50:3c630b5eba9f 1565 // }
Lightvalve 50:3c630b5eba9f 1566 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 1567 // break;
Lightvalve 50:3c630b5eba9f 1568 // }
Lightvalve 14:8e7590227d22 1569
Lightvalve 14:8e7590227d22 1570 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1571 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1572 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1573 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1574 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 59:f308b1656d9c 1575 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 1576 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1577 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1578 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 1579 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1580 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1581 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1582 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1583 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1584 }
Lightvalve 29:69f3f5445d6d 1585 cnt_findhome++;
Lightvalve 14:8e7590227d22 1586
Lightvalve 29:69f3f5445d6d 1587 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1588 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1589 } else {
Lightvalve 29:69f3f5445d6d 1590 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1591 }
Lightvalve 19:23b7c1ad8683 1592
Lightvalve 57:f4819de54e7a 1593 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1594 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 67:c2812cf26c38 1595 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 67:c2812cf26c38 1596 else pos.ref = pos.ref - 12.0f;
Lightvalve 61:bc8c8270f0ab 1597
Lightvalve 59:f308b1656d9c 1598 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 59:f308b1656d9c 1599 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1600 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 59:f308b1656d9c 1601 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1602 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 1603
Lightvalve 59:f308b1656d9c 1604 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 1605 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1606
Lightvalve 34:bb2ca2fc2a8e 1607
Lightvalve 29:69f3f5445d6d 1608 } else {
Lightvalve 29:69f3f5445d6d 1609 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 67:c2812cf26c38 1610 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1611 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1612 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1613 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1614 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1615 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1616 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1617 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1618 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1619 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 1620
Lightvalve 67:c2812cf26c38 1621
Lightvalve 67:c2812cf26c38 1622 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 1623 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1624 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1625 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1626 vel.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1627 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 67:c2812cf26c38 1628 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 169:645207e160ca 1629
Lightvalve 169:645207e160ca 1630
Lightvalve 29:69f3f5445d6d 1631 }
Lightvalve 29:69f3f5445d6d 1632 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1633 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 1634 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 1635 //pos.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1636 vel.ref = 0.0f;
Lightvalve 169:645207e160ca 1637
Lightvalve 29:69f3f5445d6d 1638 // input for position control
Lightvalve 169:645207e160ca 1639
Lightvalve 67:c2812cf26c38 1640 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 1641 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1642
Lightvalve 67:c2812cf26c38 1643 double torq_ref = 0.0f;
Lightvalve 67:c2812cf26c38 1644 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 1645 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 1646 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 67:c2812cf26c38 1647
Lightvalve 67:c2812cf26c38 1648 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 67:c2812cf26c38 1649
Lightvalve 67:c2812cf26c38 1650 double I_REF_POS = 0.0f;
Lightvalve 67:c2812cf26c38 1651 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 67:c2812cf26c38 1652 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 67:c2812cf26c38 1653
Lightvalve 67:c2812cf26c38 1654 double temp_vel_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1655 double temp_vel_torq = 0.0f;
Lightvalve 67:c2812cf26c38 1656 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 67:c2812cf26c38 1657
Lightvalve 69:3995ffeaa786 1658 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 67:c2812cf26c38 1659 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 1660 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 1661 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 67:c2812cf26c38 1662 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 1663 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:c2812cf26c38 1664 }
Lightvalve 67:c2812cf26c38 1665 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 1666 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1667
Lightvalve 67:c2812cf26c38 1668 I_REF = I_REF_POS;
Lightvalve 67:c2812cf26c38 1669
Lightvalve 67:c2812cf26c38 1670
Lightvalve 67:c2812cf26c38 1671
Lightvalve 67:c2812cf26c38 1672 } else {
Lightvalve 67:c2812cf26c38 1673 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 67:c2812cf26c38 1674 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 1675
Lightvalve 67:c2812cf26c38 1676 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 67:c2812cf26c38 1677 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 67:c2812cf26c38 1678 } else {
Lightvalve 67:c2812cf26c38 1679 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 67:c2812cf26c38 1680 }
Lightvalve 67:c2812cf26c38 1681
Lightvalve 67:c2812cf26c38 1682 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 67:c2812cf26c38 1683
Lightvalve 67:c2812cf26c38 1684 V_out = (float) Vout.ref;
Lightvalve 67:c2812cf26c38 1685
Lightvalve 67:c2812cf26c38 1686 }
Lightvalve 67:c2812cf26c38 1687
Lightvalve 67:c2812cf26c38 1688
Lightvalve 169:645207e160ca 1689
Lightvalve 169:645207e160ca 1690
Lightvalve 59:f308b1656d9c 1691 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 59:f308b1656d9c 1692 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1693 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 1694 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1695 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1696
Lightvalve 29:69f3f5445d6d 1697 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1698 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1699 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1700 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1701 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1702 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1703 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1704 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1705 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 1706 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1707 }
Lightvalve 13:747daba9cf59 1708 }
Lightvalve 19:23b7c1ad8683 1709
Lightvalve 13:747daba9cf59 1710 break;
Lightvalve 13:747daba9cf59 1711 }
Lightvalve 14:8e7590227d22 1712
Lightvalve 50:3c630b5eba9f 1713 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1714 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1715 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1716 // else {
Lightvalve 50:3c630b5eba9f 1717 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1718 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1719 // }
Lightvalve 50:3c630b5eba9f 1720 // }
Lightvalve 50:3c630b5eba9f 1721 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1722 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1723 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1724 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1725 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1726 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1727 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1728 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1729 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1730 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1731 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1732 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1733 // }
Lightvalve 50:3c630b5eba9f 1734 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1735 // }
Lightvalve 50:3c630b5eba9f 1736 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1737 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1738 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1739 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1740 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1741 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1742 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1743 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1744 // 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 1745 // // 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 1746 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1747 // }
Lightvalve 50:3c630b5eba9f 1748 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1749 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1750 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1751 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1752 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1753 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1754 // 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 1755 // // 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 1756 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1757 // }
Lightvalve 50:3c630b5eba9f 1758 // }
Lightvalve 50:3c630b5eba9f 1759 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1760 //
Lightvalve 50:3c630b5eba9f 1761 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1762 //
Lightvalve 50:3c630b5eba9f 1763 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1764 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1765 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1766 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1767 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1768 // }
Lightvalve 50:3c630b5eba9f 1769 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1770 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1771 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1772 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1773 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1774 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1775 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1776 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1777 // }
Lightvalve 50:3c630b5eba9f 1778 //
Lightvalve 50:3c630b5eba9f 1779 // }
Lightvalve 50:3c630b5eba9f 1780 // break;
Lightvalve 50:3c630b5eba9f 1781 // }
Lightvalve 50:3c630b5eba9f 1782 //
Lightvalve 50:3c630b5eba9f 1783 // }
Lightvalve 14:8e7590227d22 1784 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1785 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1786 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1787 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1788 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1789
Lightvalve 14:8e7590227d22 1790 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1791 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1792 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1793 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1794 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1795
Lightvalve 38:118df027d851 1796 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 1797 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 1798 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1799
Lightvalve 30:8d561f16383b 1800 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1801 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1802 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1803 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1804
Lightvalve 30:8d561f16383b 1805 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1806 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1807 }
Lightvalve 13:747daba9cf59 1808 } else {
Lightvalve 57:f4819de54e7a 1809 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1810 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1811 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1812 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1813 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1814 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1815
Lightvalve 170:42c938a40313 1816 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1817 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 1818 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1819
Lightvalve 30:8d561f16383b 1820 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1821 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1822 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1823 }
Lightvalve 14:8e7590227d22 1824 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1825 break;
Lightvalve 13:747daba9cf59 1826 }
Lightvalve 14:8e7590227d22 1827
Lightvalve 50:3c630b5eba9f 1828 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1829 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1830 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1831 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1832 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1833 // }
Lightvalve 50:3c630b5eba9f 1834 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1835 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1836 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1837 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1838 // }
Lightvalve 50:3c630b5eba9f 1839 // } else {
Lightvalve 50:3c630b5eba9f 1840 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1841 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1842 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1843 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1844 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1845 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1846 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1847 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1848 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1849 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1850 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1851 //
Lightvalve 50:3c630b5eba9f 1852 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1853 //
Lightvalve 50:3c630b5eba9f 1854 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1855 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1856 // }
Lightvalve 50:3c630b5eba9f 1857 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1858 // break;
Lightvalve 50:3c630b5eba9f 1859 // }
Lightvalve 19:23b7c1ad8683 1860
Lightvalve 50:3c630b5eba9f 1861 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1862 // 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 1863 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1864 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1865 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1866 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1867 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1868 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 1869 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 1870 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1871 // }
Lightvalve 50:3c630b5eba9f 1872 // break;
Lightvalve 50:3c630b5eba9f 1873 // }
Lightvalve 14:8e7590227d22 1874
Lightvalve 14:8e7590227d22 1875 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 59:f308b1656d9c 1876 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1877 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1878
Lightvalve 14:8e7590227d22 1879 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 60:64181f1d3e60 1880 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1881 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 59:f308b1656d9c 1882 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1883 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1884 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1885 data_num = 0;
Lightvalve 14:8e7590227d22 1886 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1887 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1888 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1889 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 59:f308b1656d9c 1890 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1891 } else {
Lightvalve 13:747daba9cf59 1892 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1893 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1894 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1895 }
Lightvalve 14:8e7590227d22 1896
Lightvalve 17:1865016ca2e7 1897 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1898 int i;
Lightvalve 13:747daba9cf59 1899 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1900 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1901 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1902 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1903 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1904 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1905 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1906 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1907 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1908 }
Lightvalve 13:747daba9cf59 1909 }
Lightvalve 170:42c938a40313 1910 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1911 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 1912 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 170:42c938a40313 1913 for(int i=0; i<25; i++) {
Lightvalve 170:42c938a40313 1914 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 170:42c938a40313 1915 }
Lightvalve 13:747daba9cf59 1916 ID_index = 0;
Lightvalve 57:f4819de54e7a 1917 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1918 }
Lightvalve 14:8e7590227d22 1919
Lightvalve 14:8e7590227d22 1920
Lightvalve 13:747daba9cf59 1921 break;
Lightvalve 13:747daba9cf59 1922 }
Lightvalve 14:8e7590227d22 1923
Lightvalve 14:8e7590227d22 1924 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 59:f308b1656d9c 1925 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1926 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 1927 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1928 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1929 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1930 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1931 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1932 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 1933 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1934 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1935 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1936 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1937 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 1938 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1939 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 1940 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1941 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 1942 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1943 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 1944 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1945 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 1946 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 1947 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 1948 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1949 data_num = 0;
Lightvalve 14:8e7590227d22 1950
Lightvalve 30:8d561f16383b 1951 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1952 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1953 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1954
Lightvalve 30:8d561f16383b 1955 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1956 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1957 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1958
Lightvalve 30:8d561f16383b 1959 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1960 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1961 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1962 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1963
Lightvalve 60:64181f1d3e60 1964 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 1965 DZ_case = 1;
Lightvalve 60:64181f1d3e60 1966 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 1967 DZ_case = -1;
Lightvalve 14:8e7590227d22 1968 } else {
Lightvalve 13:747daba9cf59 1969 DZ_case = 0;
Lightvalve 13:747daba9cf59 1970 }
Lightvalve 61:bc8c8270f0ab 1971
Lightvalve 60:64181f1d3e60 1972 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 1973
Lightvalve 13:747daba9cf59 1974 first_check = 1;
Lightvalve 13:747daba9cf59 1975 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1976 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1977 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 1978 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 1979 DZ_index = 1;
Lightvalve 14:8e7590227d22 1980
Lightvalve 13:747daba9cf59 1981 }
Lightvalve 19:23b7c1ad8683 1982 } else {
Lightvalve 14:8e7590227d22 1983 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 1984 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 1985 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 1986 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 1987 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 1988 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1989 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1990 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1991 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1992 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1993 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1994 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1995 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1996 }
Lightvalve 14:8e7590227d22 1997 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1998
Lightvalve 30:8d561f16383b 1999 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2000 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2001 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2002
Lightvalve 14:8e7590227d22 2003 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2004 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2005 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2006 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2007 } else {
Lightvalve 13:747daba9cf59 2008 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 2009 }
Lightvalve 14:8e7590227d22 2010
Lightvalve 13:747daba9cf59 2011 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2012 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2013 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2014 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2015 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2016 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2017 DZ_index = 1;
Lightvalve 13:747daba9cf59 2018 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2019 }
Lightvalve 13:747daba9cf59 2020 }
Lightvalve 14:8e7590227d22 2021 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 2022 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2023 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 2024 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2025 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2026 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2027 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2028 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2029 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2030 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2031 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2032 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2033 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2034 }
Lightvalve 14:8e7590227d22 2035 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2036
Lightvalve 30:8d561f16383b 2037 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2038 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2039 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2040 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2041 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2042
Lightvalve 14:8e7590227d22 2043 if((FINAL_POS - START_POS)>100) {
Lightvalve 60:64181f1d3e60 2044 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2045 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2046 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2047 } else {
Lightvalve 60:64181f1d3e60 2048 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2049 }
Lightvalve 14:8e7590227d22 2050
Lightvalve 13:747daba9cf59 2051 VALVE_DZ_timer = 0;
Lightvalve 60:64181f1d3e60 2052 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2053 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2054 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2055 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2056 first_check = 0;
Lightvalve 33:91b17819ec30 2057 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2058 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2059
Lightvalve 170:42c938a40313 2060 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2061 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2062 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2063 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2064
Lightvalve 60:64181f1d3e60 2065 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2066 DZ_index = 1;
Lightvalve 13:747daba9cf59 2067 }
Lightvalve 13:747daba9cf59 2068 }
Lightvalve 14:8e7590227d22 2069 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2070 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2071 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 2072 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2073 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2074 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2075 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2076 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2077 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2078 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2079 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2080 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2081 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2082 }
Lightvalve 14:8e7590227d22 2083 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2084
Lightvalve 30:8d561f16383b 2085 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2086 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2087 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2088
Lightvalve 14:8e7590227d22 2089 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2090 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2091 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2092 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2093 } else {
Lightvalve 13:747daba9cf59 2094 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2095 }
Lightvalve 13:747daba9cf59 2096 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2097 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2098 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2099 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2100 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2101 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2102 DZ_index = 1;
Lightvalve 13:747daba9cf59 2103 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2104 }
Lightvalve 13:747daba9cf59 2105 }
Lightvalve 14:8e7590227d22 2106 } else {
Lightvalve 30:8d561f16383b 2107 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2108 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 2109 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2110 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2111 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2112 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2113 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2114 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2115 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2116 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2117 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2118 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2119 }
Lightvalve 14:8e7590227d22 2120 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2121
Lightvalve 30:8d561f16383b 2122 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2123 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2124 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2125 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2126 FINAL_POS = pos.sen;
Lightvalve 61:bc8c8270f0ab 2127
Lightvalve 60:64181f1d3e60 2128 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2129 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2130 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2131 DZ_DIRECTION = -1;
Lightvalve 60:64181f1d3e60 2132 } else {
Lightvalve 13:747daba9cf59 2133 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2134 }
Lightvalve 14:8e7590227d22 2135
Lightvalve 13:747daba9cf59 2136 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2137 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2138 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2139 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2140 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2141 first_check = 0;
Lightvalve 33:91b17819ec30 2142 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2143 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 61:bc8c8270f0ab 2144
Lightvalve 170:42c938a40313 2145 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2146 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2147 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2148 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2149
Lightvalve 57:f4819de54e7a 2150 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2151 DZ_index = 1;
Lightvalve 13:747daba9cf59 2152 }
Lightvalve 13:747daba9cf59 2153 }
Lightvalve 13:747daba9cf59 2154 }
Lightvalve 14:8e7590227d22 2155 }
Lightvalve 13:747daba9cf59 2156 break;
Lightvalve 13:747daba9cf59 2157 }
Lightvalve 14:8e7590227d22 2158
Lightvalve 14:8e7590227d22 2159 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 59:f308b1656d9c 2160 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2161 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2162 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2163 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2164 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2165 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2166 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2167 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2168 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2169 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2170 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2171 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2172 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2173 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 59:f308b1656d9c 2174 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2175 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2176 first_check = 1;
Lightvalve 13:747daba9cf59 2177 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2178 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2179 ID_index = 0;
Lightvalve 13:747daba9cf59 2180 max_check = 0;
Lightvalve 13:747daba9cf59 2181 min_check = 0;
Lightvalve 13:747daba9cf59 2182 }
Lightvalve 14:8e7590227d22 2183 } else {
Lightvalve 30:8d561f16383b 2184 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2185 //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 2186 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 59:f308b1656d9c 2187 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2188 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2189 data_num = 0;
Lightvalve 57:f4819de54e7a 2190 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2191
Lightvalve 14:8e7590227d22 2192 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2193 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2194 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f4819de54e7a 2195 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2196 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2197 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2198 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2199 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2200 one_period_end = 1;
Lightvalve 13:747daba9cf59 2201 }
Lightvalve 30:8d561f16383b 2202 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2203 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2204 one_period_end = 1;
Lightvalve 59:f308b1656d9c 2205 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2206 }
Lightvalve 14:8e7590227d22 2207
Lightvalve 14:8e7590227d22 2208 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2209 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2210 max_check = 1;
Lightvalve 14:8e7590227d22 2211 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2212 min_check = 1;
Lightvalve 13:747daba9cf59 2213 }
Lightvalve 13:747daba9cf59 2214 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2215
Lightvalve 13:747daba9cf59 2216 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2217 one_period_end = 0;
Lightvalve 13:747daba9cf59 2218 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2219 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2220 }
Lightvalve 14:8e7590227d22 2221
Lightvalve 14:8e7590227d22 2222 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2223
Lightvalve 13:747daba9cf59 2224 VALVE_POS_NUM = ID_index;
Lightvalve 170:42c938a40313 2225 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2226 for(int i=0; i<100; i++) {
Lightvalve 170:42c938a40313 2227 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 170:42c938a40313 2228 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 170:42c938a40313 2229 }
Lightvalve 13:747daba9cf59 2230 ID_index = 0;
Lightvalve 13:747daba9cf59 2231 first_check = 0;
Lightvalve 13:747daba9cf59 2232 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2233 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 2234 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2235 }
Lightvalve 13:747daba9cf59 2236 }
Lightvalve 13:747daba9cf59 2237 break;
Lightvalve 13:747daba9cf59 2238 }
Lightvalve 58:2eade98630e2 2239
Lightvalve 57:f4819de54e7a 2240 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 2241 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 2242 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 2243 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 2244 cnt_sysid++;
Lightvalve 57:f4819de54e7a 2245 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 2246 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 2247 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 2248 }
Lightvalve 57:f4819de54e7a 2249 break;
Lightvalve 57:f4819de54e7a 2250 }
Lightvalve 58:2eade98630e2 2251
Lightvalve 169:645207e160ca 2252 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 2253 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 2254 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2255 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2256 } else {
Lightvalve 169:645207e160ca 2257 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2258 }
Lightvalve 169:645207e160ca 2259 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2260 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2261 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2262 } else {
Lightvalve 169:645207e160ca 2263 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2264 }
Lightvalve 58:2eade98630e2 2265
Lightvalve 169:645207e160ca 2266 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2267 cnt_freq_test++;
Lightvalve 169:645207e160ca 2268 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 2269 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 2270 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 2271 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2272 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 2273 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 2274 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2275 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2276 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2277 }
Lightvalve 169:645207e160ca 2278 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2279 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 2280
Lightvalve 169:645207e160ca 2281 }
Lightvalve 169:645207e160ca 2282 break;
Lightvalve 169:645207e160ca 2283 }
Lightvalve 169:645207e160ca 2284 case MODE_SEND_BUFFER: {
Lightvalve 169:645207e160ca 2285 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 169:645207e160ca 2286 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 169:645207e160ca 2287 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 169:645207e160ca 2288 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 169:645207e160ca 2289 // }
Lightvalve 169:645207e160ca 2290 // cnt_send_buffer++;
Lightvalve 169:645207e160ca 2291 // }
Lightvalve 169:645207e160ca 2292
Lightvalve 169:645207e160ca 2293 break;
Lightvalve 169:645207e160ca 2294 }
Lightvalve 169:645207e160ca 2295 case MODE_SEND_OVER: {
Lightvalve 169:645207e160ca 2296 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 169:645207e160ca 2297 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2298 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2299 break;
Lightvalve 169:645207e160ca 2300 }
Lightvalve 170:42c938a40313 2301
Lightvalve 169:645207e160ca 2302 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 2303 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2304 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2305 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2306 } else {
Lightvalve 169:645207e160ca 2307 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 2308 }
Lightvalve 169:645207e160ca 2309 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2310 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2311 } else {
Lightvalve 169:645207e160ca 2312 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2313 }
Lightvalve 169:645207e160ca 2314 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2315 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2316 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2317 } else {
Lightvalve 169:645207e160ca 2318 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2319 }
Lightvalve 169:645207e160ca 2320
Lightvalve 169:645207e160ca 2321 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2322 cnt_step_test++;
Lightvalve 170:42c938a40313 2323 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2324 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 2325 cnt_step_test = 0;
Lightvalve 169:645207e160ca 2326 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2327 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 2328 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2329 }
Lightvalve 169:645207e160ca 2330 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 169:645207e160ca 2331 // {
Lightvalve 169:645207e160ca 2332 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2333 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2334 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2335 // }
Lightvalve 170:42c938a40313 2336
Lightvalve 169:645207e160ca 2337 break;
Lightvalve 169:645207e160ca 2338 }
Lightvalve 57:f4819de54e7a 2339
Lightvalve 57:f4819de54e7a 2340 default:
Lightvalve 57:f4819de54e7a 2341 break;
Lightvalve 57:f4819de54e7a 2342 }
Lightvalve 57:f4819de54e7a 2343
Lightvalve 57:f4819de54e7a 2344 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 2345
Lightvalve 57:f4819de54e7a 2346 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 2347 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 2348 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 2349 break;
Lightvalve 57:f4819de54e7a 2350 }
Lightvalve 57:f4819de54e7a 2351
Lightvalve 57:f4819de54e7a 2352 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 2353 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 2354 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 2355 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 2356 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 2357 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 2358 } else {
Lightvalve 67:c2812cf26c38 2359 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 2360 }
Lightvalve 57:f4819de54e7a 2361 break;
Lightvalve 57:f4819de54e7a 2362 }
Lightvalve 57:f4819de54e7a 2363
Lightvalve 57:f4819de54e7a 2364 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 2365
Lightvalve 57:f4819de54e7a 2366 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 2367 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 2368 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 2369 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2370
Lightvalve 67:c2812cf26c38 2371 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 2372 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 2373 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 2374 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 2375
Lightvalve 139:15621998925b 2376 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 139:15621998925b 2377 torq_ref = torq.ref;
Lightvalve 57:f4819de54e7a 2378
Lightvalve 57:f4819de54e7a 2379 // torque feedback
Lightvalve 67:c2812cf26c38 2380 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 2381 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 2382
Lightvalve 57:f4819de54e7a 2383 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 2384
Lightvalve 57:f4819de54e7a 2385 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 2386 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 2387 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 2388
Lightvalve 57:f4819de54e7a 2389 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 2390 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 2391 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 2392
Lightvalve 69:3995ffeaa786 2393 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2394 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 2395 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 2396 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2397 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 2398 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 2399 }
Lightvalve 57:f4819de54e7a 2400 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 2401 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2402
Lightvalve 57:f4819de54e7a 2403 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 2404 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2405 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 2406 // 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 2407 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2408 // L feedforward velocity
Lightvalve 69:3995ffeaa786 2409 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2410 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 2411 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2412 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2413 // L feedforward velocity
Lightvalve 57:f4819de54e7a 2414 }
Lightvalve 57:f4819de54e7a 2415 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 2416 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2417 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 2418 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 2419 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 2420
Lightvalve 57:f4819de54e7a 2421 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 2422
Lightvalve 67:c2812cf26c38 2423 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 2424 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 2425 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 2426 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 2427 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 2428 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 2429 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2430 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 2431 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2432 } else if (I_REF < -I_MAX) {
Lightvalve 67:c2812cf26c38 2433 double I_rem = I_REF - (-I_MAX);
Lightvalve 67:c2812cf26c38 2434 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2435 I_REF = -I_MAX;
Lightvalve 67:c2812cf26c38 2436 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2437 }
Lightvalve 67:c2812cf26c38 2438 }
Lightvalve 57:f4819de54e7a 2439
Lightvalve 57:f4819de54e7a 2440 } else {
Lightvalve 57:f4819de54e7a 2441 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 2442 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 2443 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 2444
Lightvalve 209:ebc69d6ee6f1 2445 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * (torq.ref_diff - torq_dot.sen)) * 0.01f + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 57:f4819de54e7a 2446
Lightvalve 72:3436ce769b1e 2447 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 2448
Lightvalve 72:3436ce769b1e 2449 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 2450
Lightvalve 169:645207e160ca 2451
Lightvalve 72:3436ce769b1e 2452 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 2453 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 2454 } else {
Lightvalve 72:3436ce769b1e 2455 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 2456 }
Lightvalve 57:f4819de54e7a 2457
Lightvalve 57:f4819de54e7a 2458 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 2459 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 2460 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 2461 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2462 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2463 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2464 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2465 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 2466 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2467 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2468 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2469 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2470 }
Lightvalve 57:f4819de54e7a 2471 }
Lightvalve 61:bc8c8270f0ab 2472
Lightvalve 57:f4819de54e7a 2473 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2474
Lightvalve 67:c2812cf26c38 2475 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 60:64181f1d3e60 2476 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2477
Lightvalve 57:f4819de54e7a 2478 }
Lightvalve 169:645207e160ca 2479
Lightvalve 72:3436ce769b1e 2480 torq_ref_past = torq_ref;
Lightvalve 58:2eade98630e2 2481
Lightvalve 133:22ab22818e01 2482
Lightvalve 57:f4819de54e7a 2483 break;
Lightvalve 57:f4819de54e7a 2484 }
Lightvalve 58:2eade98630e2 2485
Lightvalve 57:f4819de54e7a 2486 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 2487 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2488 break;
Lightvalve 57:f4819de54e7a 2489 }
Lightvalve 169:645207e160ca 2490
Lightvalve 138:a843f32ced33 2491 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 2492
Lightvalve 169:645207e160ca 2493
Lightvalve 139:15621998925b 2494 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 2495 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 210:efc3a92cc6be 2496
Lightvalve 170:42c938a40313 2497 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 2498
Lightvalve 169:645207e160ca 2499
Lightvalve 170:42c938a40313 2500 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 2501
Lightvalve 138:a843f32ced33 2502 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 2503 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2504 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 2505 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2506 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 2507 } else {
Lightvalve 138:a843f32ced33 2508 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 2509 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 2510 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2511 } else {
Lightvalve 138:a843f32ced33 2512 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 2513 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2514 }
Lightvalve 138:a843f32ced33 2515 }
Lightvalve 138:a843f32ced33 2516 float tau = 0.01f;
Lightvalve 142:43026242815a 2517 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 2518
Lightvalve 138:a843f32ced33 2519 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 2520 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 2521 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2522 } else {
Lightvalve 138:a843f32ced33 2523 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2524 }
Lightvalve 138:a843f32ced33 2525 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 2526 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 2527
Lightvalve 139:15621998925b 2528 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 2529
Lightvalve 138:a843f32ced33 2530 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 2531 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 2532 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2533
Lightvalve 138:a843f32ced33 2534 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 2535 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 2536
Lightvalve 212:ec41f1449ef9 2537 float k3 = 2000.0f; //2000 //20000
Lightvalve 142:43026242815a 2538 float k4 = 10.0f;
Lightvalve 142:43026242815a 2539 float rho3 = 3.2f;
Lightvalve 212:ec41f1449ef9 2540 float rho4 = 10000000.0f; //25000000.0f;
Lightvalve 139:15621998925b 2541 float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 2542 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 2543 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 2544
Lightvalve 139:15621998925b 2545 if (x_4_des > 0) {
Lightvalve 139:15621998925b 2546 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2547 } else {
Lightvalve 139:15621998925b 2548 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2549 }
Lightvalve 169:645207e160ca 2550
Lightvalve 138:a843f32ced33 2551 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 2552 x_4_des_old = x_4_des;
Lightvalve 212:ec41f1449ef9 2553 float V_input = 0.0f;
Lightvalve 139:15621998925b 2554 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 212:ec41f1449ef9 2555 // //V_out LPF
Lightvalve 212:ec41f1449ef9 2556 // float alpha_V_out = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*50.0f)); // f_cutoff : 50Hz
Lightvalve 212:ec41f1449ef9 2557 // V_out = V_out*(1.0f-alpha_V_out)+V_input*(alpha_V_out);
Lightvalve 212:ec41f1449ef9 2558
Lightvalve 212:ec41f1449ef9 2559 float rho_gamma = 5000.0f;//5000 for change //50000 for not change
Lightvalve 139:15621998925b 2560 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 2561 gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 212:ec41f1449ef9 2562
Lightvalve 212:ec41f1449ef9 2563 if(gamma_hat > 10000.0f) gamma_hat = 10000.0f;
Lightvalve 212:ec41f1449ef9 2564 else if(gamma_hat < 100.0f) gamma_hat = 100.0f;
Lightvalve 212:ec41f1449ef9 2565
Lightvalve 169:645207e160ca 2566 break;
Lightvalve 138:a843f32ced33 2567 }
Lightvalve 171:bfc1fd2629d8 2568
Lightvalve 170:42c938a40313 2569 case MODE_RL: {
Lightvalve 170:42c938a40313 2570 //t.reset();
Lightvalve 170:42c938a40313 2571 //t.start();
Lightvalve 170:42c938a40313 2572
Lightvalve 170:42c938a40313 2573 // if(LED == 0) LED = 1;
Lightvalve 170:42c938a40313 2574 // else LED = 0;
Lightvalve 170:42c938a40313 2575
Lightvalve 170:42c938a40313 2576 if (Update_Done_Flag == 1) {
Lightvalve 170:42c938a40313 2577 //Gather Data on each loop
Lightvalve 170:42c938a40313 2578 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 170:42c938a40313 2579 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2580 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2581 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2582 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2583 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2584 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2585 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 170:42c938a40313 2586 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 170:42c938a40313 2587 Actor_Network(temp_array);
Lightvalve 173:68c7914679ec 2588 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 2589 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 173:68c7914679ec 2590 }
Lightvalve 173:68c7914679ec 2591 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 2592 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 173:68c7914679ec 2593 }
Lightvalve 173:68c7914679ec 2594 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 173:68c7914679ec 2595 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 170:42c938a40313 2596 mean_array[RL_timer] = mean;
Lightvalve 170:42c938a40313 2597 deviation_array[RL_timer] = deviation;
Lightvalve 170:42c938a40313 2598 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 170:42c938a40313 2599
Lightvalve 179:d5377766d7ea 2600 virt_pos = virt_pos + (action_array[RL_timer] - 5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2601 if (virt_pos > 70 ) {
Lightvalve 170:42c938a40313 2602 virt_pos = 70.0f;
Lightvalve 179:d5377766d7ea 2603 }else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2604 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2605 }
Lightvalve 171:bfc1fd2629d8 2606
Lightvalve 170:42c938a40313 2607 RL_timer++;
Lightvalve 170:42c938a40313 2608
Lightvalve 170:42c938a40313 2609
Lightvalve 170:42c938a40313 2610 if (RL_timer >= batch_size) {
Lightvalve 170:42c938a40313 2611 RL_timer = 0;
Lightvalve 170:42c938a40313 2612 batch++;
Lightvalve 170:42c938a40313 2613 for(int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 2614 state_array[i][0] = train_set_x[i];
Lightvalve 170:42c938a40313 2615 state_array[i][1] = train_set_error[i];
Lightvalve 170:42c938a40313 2616 //state_array[i][2] = train_set_count[i];
Lightvalve 170:42c938a40313 2617 }
Lightvalve 170:42c938a40313 2618 Update_Case = 1;
Lightvalve 170:42c938a40313 2619 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2620 logging1 = virt_pos;
Lightvalve 179:d5377766d7ea 2621
Lightvalve 170:42c938a40313 2622 if(batch >= num_batch) {
Lightvalve 170:42c938a40313 2623 batch = 0;
Lightvalve 170:42c938a40313 2624 RL_timer = 0;
Lightvalve 170:42c938a40313 2625 Update_Case = 2;
Lightvalve 170:42c938a40313 2626 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2627 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2628 }
Lightvalve 170:42c938a40313 2629 }
Lightvalve 170:42c938a40313 2630 }
Lightvalve 170:42c938a40313 2631
Lightvalve 170:42c938a40313 2632 else {
Lightvalve 170:42c938a40313 2633 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2634 float temp_array[3] = {0.0f};
Lightvalve 170:42c938a40313 2635 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2636 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2637 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2638 Actor_Network(temp_array);
Lightvalve 170:42c938a40313 2639 action = rand_normal(mean, deviation);
Lightvalve 170:42c938a40313 2640 //logging1 = action;
Lightvalve 179:d5377766d7ea 2641 //logging2 = mean;
Lightvalve 179:d5377766d7ea 2642 //logging4 = deviation;
Lightvalve 179:d5377766d7ea 2643 virt_pos = virt_pos + (action-5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2644 if (virt_pos > 70) {
Lightvalve 170:42c938a40313 2645 virt_pos = 70.0f;
Lightvalve 179:d5377766d7ea 2646 }else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2647 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2648 }
Lightvalve 170:42c938a40313 2649
Lightvalve 170:42c938a40313 2650 logging3 = virt_pos;
Lightvalve 170:42c938a40313 2651 }
Lightvalve 170:42c938a40313 2652
Lightvalve 170:42c938a40313 2653 //t.stop();
Lightvalve 170:42c938a40313 2654 //logging1 = t.read()*1000.0f; //msec
Lightvalve 170:42c938a40313 2655
Lightvalve 170:42c938a40313 2656 break;
Lightvalve 170:42c938a40313 2657 }
Lightvalve 14:8e7590227d22 2658
Lightvalve 12:6f2531038ea4 2659 default:
Lightvalve 12:6f2531038ea4 2660 break;
Lightvalve 12:6f2531038ea4 2661 }
Lightvalve 14:8e7590227d22 2662
Lightvalve 57:f4819de54e7a 2663
Lightvalve 57:f4819de54e7a 2664 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 2665
Lightvalve 57:f4819de54e7a 2666 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2667 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 2668 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2669 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 2670 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 2671 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 2672
Lightvalve 57:f4819de54e7a 2673 I_ERR = I_REF_fil - cur.sen;
Lightvalve 67:c2812cf26c38 2674 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 2675
Lightvalve 57:f4819de54e7a 2676
Lightvalve 57:f4819de54e7a 2677 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 2678 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 2679 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 2680 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 2681 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 2682 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 2683
Lightvalve 57:f4819de54e7a 2684 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 2685 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 2686 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 2687 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 2688 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 2689 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 2690 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 2691 }
Lightvalve 57:f4819de54e7a 2692
Lightvalve 57:f4819de54e7a 2693 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 2694
Lightvalve 67:c2812cf26c38 2695 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 57:f4819de54e7a 2696 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 57:f4819de54e7a 2697 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 57:f4819de54e7a 2698 I_REF_fil_old = I_REF_fil;
Lightvalve 67:c2812cf26c38 2699 // 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 2700 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 67:c2812cf26c38 2701 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 2702
Lightvalve 57:f4819de54e7a 2703 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 2704 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 2705 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 2706 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2707 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 2708 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2709 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 2710 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 2711 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2712 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 2713 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2714 }
Lightvalve 57:f4819de54e7a 2715 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2716 } else {
Lightvalve 57:f4819de54e7a 2717 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 2718 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2719 }
Lightvalve 57:f4819de54e7a 2720
Lightvalve 57:f4819de54e7a 2721 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2722 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 2723 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2724 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 57:f4819de54e7a 2725 if (FLAG_VALVE_DEADZONE) {
Lightvalve 57:f4819de54e7a 2726 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2727 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2728
Lightvalve 57:f4819de54e7a 2729 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2730
Lightvalve 57:f4819de54e7a 2731 } else {
Lightvalve 57:f4819de54e7a 2732 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 57:f4819de54e7a 2733 }
Lightvalve 57:f4819de54e7a 2734
Lightvalve 57:f4819de54e7a 2735 // Output Voltage Linearization
Lightvalve 57:f4819de54e7a 2736 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 67:c2812cf26c38 2737 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 2738
Lightvalve 57:f4819de54e7a 2739 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 2740 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 2741 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 2742 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 2743 } else { //////////////////////////sw valve
Lightvalve 89:a7b45368ea0f 2744 // Output Voltage Linearization
Lightvalve 135:79885a39c161 2745 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 135:79885a39c161 2746 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 89:a7b45368ea0f 2747
Lightvalve 89:a7b45368ea0f 2748 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 135:79885a39c161 2749 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 135:79885a39c161 2750 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 135:79885a39c161 2751 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 135:79885a39c161 2752
Lightvalve 135:79885a39c161 2753 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 2754 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 2755 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 2756 }
Lightvalve 169:645207e160ca 2757
Lightvalve 67:c2812cf26c38 2758 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 67:c2812cf26c38 2759 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 67:c2812cf26c38 2760 // else V_out = V_out;
Lightvalve 169:645207e160ca 2761
jobuuu 7:e9086c72bb22 2762 /*******************************************************
jobuuu 7:e9086c72bb22 2763 *** PWM
jobuuu 7:e9086c72bb22 2764 ********************************************************/
Lightvalve 169:645207e160ca 2765 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 2766 V_out = -V_out;
Lightvalve 67:c2812cf26c38 2767 }
Lightvalve 169:645207e160ca 2768
Lightvalve 49:e7bcfc244d40 2769 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2770 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 2771 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2772 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2773 }
Lightvalve 49:e7bcfc244d40 2774 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2775
Lightvalve 19:23b7c1ad8683 2776 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2777 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2778 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2779
Lightvalve 30:8d561f16383b 2780 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2781 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2782 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2783 } else {
jobuuu 2:a1c0a37df760 2784 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2785 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2786 }
Lightvalve 13:747daba9cf59 2787
jobuuu 1:e04e563be5ce 2788 //pwm
Lightvalve 30:8d561f16383b 2789 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2790 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 2791
Lightvalve 61:bc8c8270f0ab 2792
Lightvalve 57:f4819de54e7a 2793 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2794
Lightvalve 54:647072f5307a 2795 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 2796 if (flag_data_request[0] == HIGH) {
Lightvalve 57:f4819de54e7a 2797 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 2798 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2799 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 2800 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 2801 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 2802 }
Lightvalve 57:f4819de54e7a 2803 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 2804 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2805 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 2806 } else if (SENSING_MODE == 1) {
Lightvalve 67:c2812cf26c38 2807 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 2808 }
Lightvalve 52:8ea76864368a 2809 }
Lightvalve 52:8ea76864368a 2810 }
Lightvalve 56:6f50d9d3bfee 2811 if (flag_data_request[1] == HIGH) {
Lightvalve 179:d5377766d7ea 2812 CAN_TX_TORQUE((int16_t) (return_G[0]*100.0f)); //1300
Lightvalve 54:647072f5307a 2813 }
Lightvalve 58:2eade98630e2 2814
Lightvalve 58:2eade98630e2 2815
Lightvalve 171:bfc1fd2629d8 2816 if (flag_data_request[2] == HIGH) {
Lightvalve 169:645207e160ca 2817 double t_value = 0.0f;
Lightvalve 171:bfc1fd2629d8 2818 if(value>=(float) VALVE_CENTER) {
Lightvalve 131:d08121ac87ba 2819 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2820 } else {
Lightvalve 131:d08121ac87ba 2821 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2822 }
Lightvalve 169:645207e160ca 2823 double t_value_ref = 0.0f;
Lightvalve 169:645207e160ca 2824 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2825 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2826 } else {
Lightvalve 169:645207e160ca 2827 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2828 }
Lightvalve 169:645207e160ca 2829
Lightvalve 169:645207e160ca 2830
Lightvalve 169:645207e160ca 2831 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 55:b25725257569 2832 }
Lightvalve 58:2eade98630e2 2833
Lightvalve 57:f4819de54e7a 2834 //If it doesn't rest, below can can not work.
Lightvalve 57:f4819de54e7a 2835 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 57:f4819de54e7a 2836 ;
Lightvalve 57:f4819de54e7a 2837 }
Lightvalve 58:2eade98630e2 2838
Lightvalve 169:645207e160ca 2839 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 2840 //PWM
Lightvalve 209:ebc69d6ee6f1 2841 CAN_TX_PWM((int16_t) (torq.ref)); //1500
Lightvalve 209:ebc69d6ee6f1 2842 // CAN_TX_PWM((int16_t) (f_future[1])); //1500
Lightvalve 54:647072f5307a 2843 }
Lightvalve 179:d5377766d7ea 2844
Lightvalve 56:6f50d9d3bfee 2845 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 2846 //valve position
Lightvalve 212:ec41f1449ef9 2847 //CAN_TX_VALVE_POSITION((int16_t) pos.sen/(float)(ENC_PULSE_PER_POSITION), (int16_t) virt_pos, (int16_t) (logging2*1000.0f), (int16_t) (logging4*1000.0f)); //1600
Lightvalve 212:ec41f1449ef9 2848 CAN_TX_VALVE_POSITION((int16_t) gamma_hat, (int16_t) 0, (int16_t) 0, (int16_t) 0); //1600
Lightvalve 54:647072f5307a 2849 }
Lightvalve 20:806196fda269 2850
Lightvalve 54:647072f5307a 2851 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 2852 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 2853 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 2854 // }
Lightvalve 54:647072f5307a 2855 //if (flag_delay_test == 1){
Lightvalve 58:2eade98630e2 2856 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 2857 //}
Lightvalve 52:8ea76864368a 2858
Lightvalve 54:647072f5307a 2859 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 2860 }
Lightvalve 54:647072f5307a 2861 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 2862
Lightvalve 20:806196fda269 2863 }
Lightvalve 52:8ea76864368a 2864 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 2865
Lightvalve 58:2eade98630e2 2866 }