rainbow

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Fri Dec 18 13:29:04 2020 +0000
Revision:
193:4b5e617b8481
Parent:
192:637092202815
Child:
194:b31951919b55
201218-3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 193:4b5e617b8481 1 //201218_3
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;
jobuuu 7:e9086c72bb22 64 State pres_A;
jobuuu 7:e9086c72bb22 65 State pres_B;
jobuuu 7:e9086c72bb22 66 State cur;
Lightvalve 14:8e7590227d22 67 State valve_pos;
Lightvalve 14:8e7590227d22 68
Lightvalve 14:8e7590227d22 69 State INIT_Vout;
Lightvalve 14:8e7590227d22 70 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 71 State INIT_Pos;
Lightvalve 14:8e7590227d22 72 State INIT_torq;
jobuuu 5:a4319f79457b 73
Lightvalve 19:23b7c1ad8683 74 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 75 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 76 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 77 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 78
Lightvalve 19:23b7c1ad8683 79 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 80 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 84 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 85
Lightvalve 61:bc8c8270f0ab 86
Lightvalve 61:bc8c8270f0ab 87
Lightvalve 61:bc8c8270f0ab 88
jobuuu 7:e9086c72bb22 89 // =============================================================================
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91 // =============================================================================
jobuuu 7:e9086c72bb22 92
Lightvalve 12:6f2531038ea4 93 /*******************************************************************************
Lightvalve 12:6f2531038ea4 94 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 95 ******************************************************************************/
Lightvalve 13:747daba9cf59 96 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 97 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 98 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 99 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 100 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 101 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 102 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 103 };
Lightvalve 12:6f2531038ea4 104
Lightvalve 12:6f2531038ea4 105 /*******************************************************************************
Lightvalve 12:6f2531038ea4 106 * CONTROL MODE
Lightvalve 12:6f2531038ea4 107 ******************************************************************************/
Lightvalve 13:747daba9cf59 108 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 109 //control mode
Lightvalve 12:6f2531038ea4 110 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 111 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 112 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 113
Lightvalve 47:fdcb8bd86fd6 114 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 115 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 116 MODE_RL, //5
Lightvalve 13:747daba9cf59 117
Lightvalve 12:6f2531038ea4 118 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 119 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 120 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 121
Lightvalve 12:6f2531038ea4 122 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 123 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 124
Lightvalve 14:8e7590227d22 125 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 126 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 127 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 171:bfc1fd2629d8 128 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 129
Lightvalve 12:6f2531038ea4 130 //utility
Lightvalve 12:6f2531038ea4 131 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 132 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 133 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 134 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 135 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 136 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 137 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 138
Lightvalve 12:6f2531038ea4 139 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 140 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 141 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 142 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 143 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 144 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 145 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 146 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 147 };
Lightvalve 12:6f2531038ea4 148
Lightvalve 65:a2d7c63419c2 149 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 150 {
Lightvalve 169:645207e160ca 151 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 152 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 153
Lightvalve 169:645207e160ca 154 /** Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 155 */
Lightvalve 169:645207e160ca 156 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 157 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 169:645207e160ca 158 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 159 */
Lightvalve 169:645207e160ca 160 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 161 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 162 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 163 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 164 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 165 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 166 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 167 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 168 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 169 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 170 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 171 //Error_Handler();
Lightvalve 169:645207e160ca 172 }
Lightvalve 169:645207e160ca 173 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 174 */
Lightvalve 169:645207e160ca 175 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 176 //Error_Handler();
Lightvalve 169:645207e160ca 177 }
Lightvalve 169:645207e160ca 178 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 179 */
Lightvalve 169:645207e160ca 180 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 181 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 182 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 183 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 184 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 185 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 186
Lightvalve 169:645207e160ca 187 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 188 //Error_Handler();
Lightvalve 169:645207e160ca 189 }
Lightvalve 65:a2d7c63419c2 190 }
Lightvalve 65:a2d7c63419c2 191
Lightvalve 99:7bbcb3c0fb06 192 float u_past[num_array_u_past] = {0.0f};
Lightvalve 68:328e1be06f5d 193 float x_past[num_array_x_past] = {0.0f};
Lightvalve 85:a3b46118b5cd 194 float x_future[num_array_x_future] = {0.0f};
Lightvalve 68:328e1be06f5d 195 float f_past[num_array_f_past] = {0.0f};
Lightvalve 68:328e1be06f5d 196 float f_future[num_array_f_future] = {0.0f};
Lightvalve 66:a8e6799dbce3 197
Lightvalve 73:f80dc3970c99 198 float input_NN[num_input] = { 0.0f };
Lightvalve 65:a2d7c63419c2 199
Lightvalve 169:645207e160ca 200 const float h1[num_input][16] = {
Lightvalve 193:4b5e617b8481 201 {-2.0806894302368164f,1.8419220447540283f,0.12903714179992676f,0.3766104280948639f,-2.5691683292388916f,1.3948698043823242f,-1.6932644844055176f,0.47781115770339966f,-0.774815022945404f,-0.14850656688213348f,-0.03650430217385292f,-2.368614435195923f,-2.1139299869537354f,-1.45517098903656f,-0.2571594715118408f,-0.06029130890965462f},
Lightvalve 193:4b5e617b8481 202 {-1.499216914176941f,1.2110093832015991f,-0.3927857577800751f,0.0755331888794899f,-1.912123441696167f,0.634615957736969f,-1.588600993156433f,0.1134842112660408f,-0.4692782759666443f,0.13740988075733185f,-0.028703559190034866f,-1.5429214239120483f,-0.7857987880706787f,-1.4273113012313843f,-0.20967772603034973f,0.22353018820285797f},
Lightvalve 193:4b5e617b8481 203 {-0.9740145802497864f,0.6290917992591858f,0.36391180753707886f,-0.2652323544025421f,-1.0473108291625977f,0.27822592854499817f,-0.6762312054634094f,0.24158823490142822f,-0.48006561398506165f,0.17687489092350006f,0.1041826531291008f,-0.9442924857139587f,-0.3642197847366333f,-0.5978294014930725f,0.05590221285820007f,-0.008679859340190887f},
Lightvalve 193:4b5e617b8481 204 {-0.1346927434206009f,-0.36659669876098633f,0.39742106199264526f,0.4090283513069153f,-0.21661870181560516f,-0.7815824747085571f,-0.6867067217826843f,0.45081475377082825f,-0.22874140739440918f,0.3471550941467285f,0.611111044883728f,0.28319814801216125f,0.14088939130306244f,-0.1510155200958252f,-0.2961459457874298f,0.3566420078277588f},
Lightvalve 193:4b5e617b8481 205 {1.43129301071167f,-1.254888892173767f,-0.0051451027393341064f,0.3767557144165039f,0.8894379734992981f,-1.431206464767456f,0.2549896836280823f,-0.16752402484416962f,-0.12987858057022095f,0.19921472668647766f,0.48584452271461487f,1.1332663297653198f,0.9898359179496765f,0.9567762017250061f,-0.3865056037902832f,0.11671999096870422f},
Lightvalve 193:4b5e617b8481 206 {3.0383973121643066f,-3.2238690853118896f,0.21773302555084229f,0.4259805679321289f,2.487938642501831f,-2.927023410797119f,1.3221275806427002f,-0.9985578656196594f,0.015695471316576004f,0.24681439995765686f,-1.112740397453308f,2.7345285415649414f,1.7078537940979004f,2.5884315967559814f,-0.4132833778858185f,0.1871054768562317f},
Lightvalve 193:4b5e617b8481 207 {-0.08909858763217926f,0.04017534852027893f,-0.2501986622810364f,-1.4312419891357422f,0.10488540679216385f,-0.006775588262826204f,0.036645688116550446f,-0.03338433802127838f,-0.4603580832481384f,-0.04075203463435173f,6.466564655303955f,0.14596475660800934f,-3.76338529586792f,0.8909745216369629f,-0.15618428587913513f,-0.5056508779525757f},
Lightvalve 193:4b5e617b8481 208 {-0.0644197091460228f,-0.0824904516339302f,-0.26619744300842285f,-0.9450392127037048f,0.21956279873847961f,0.5414075255393982f,0.06343743950128555f,0.26915308833122253f,-0.18562251329421997f,0.05249443277716637f,2.5711328983306885f,-0.20299145579338074f,0.32254835963249207f,-0.6894297003746033f,0.04303058981895447f,-2.268955945968628f},
Lightvalve 193:4b5e617b8481 209 {0.010134553536772728f,0.07090140879154205f,0.10248100757598877f,-1.4144065380096436f,0.14498041570186615f,0.6121429800987244f,1.7088721990585327f,-6.707942485809326f,-0.6278461217880249f,0.4125310778617859f,-1.9015189409255981f,-0.17366135120391846f,2.945331573486328f,1.5498052835464478f,-0.30555272102355957f,-2.353926420211792f},
Lightvalve 193:4b5e617b8481 210 {-0.19771908223628998f,-0.37083661556243896f,0.18462657928466797f,-1.2041350603103638f,0.46593061089515686f,0.6668076515197754f,1.1944712400436401f,-3.0236237049102783f,-0.08968208730220795f,-0.4030909240245819f,-0.7150384187698364f,-0.663982093334198f,1.4682765007019043f,0.37283697724342346f,0.3484981060028076f,-1.2506699562072754f},
Lightvalve 193:4b5e617b8481 211 {0.22058553993701935f,0.1321832835674286f,0.37182438373565674f,-0.7691853046417236f,0.06517745554447174f,0.28921499848365784f,0.5419366955757141f,-2.370201587677002f,-0.4239056408405304f,0.043917566537857056f,-0.2696247398853302f,-0.37864330410957336f,0.1648407280445099f,0.5010892748832703f,0.26688337326049805f,-0.7171939015388489f},
Lightvalve 193:4b5e617b8481 212 {-0.31798315048217773f,-0.23621180653572083f,-0.40320003032684326f,-0.8372410535812378f,0.21226218342781067f,0.13394197821617126f,0.0203731507062912f,0.9504984617233276f,-0.27080845832824707f,0.14300179481506348f,-0.6072286367416382f,0.24522404372692108f,-0.6103735566139221f,0.15849997103214264f,0.10493969917297363f,0.48328933119773865f},
Lightvalve 193:4b5e617b8481 213 {-0.019870232790708542f,-0.04535185545682907f,0.19847965240478516f,-0.645010232925415f,0.15441982448101044f,0.19513094425201416f,-0.15949001908302307f,1.0981990098953247f,-0.358957976102829f,-0.2793225646018982f,-0.32054436206817627f,-0.08625413477420807f,-0.525904655456543f,0.22998401522636414f,-0.22268100082874298f,0.5257316827774048f},
Lightvalve 193:4b5e617b8481 214 {-0.011861208826303482f,-0.03636765852570534f,0.4127817749977112f,-0.8922302722930908f,-0.20182070136070251f,-0.12261747568845749f,-0.2627313435077667f,0.48942577838897705f,-0.2826015055179596f,-0.31948286294937134f,-0.30100518465042114f,0.10201402753591537f,-0.18361657857894897f,0.19040097296237946f,0.0479682981967926f,1.3197470903396606f},
Lightvalve 193:4b5e617b8481 215 {0.12351126223802567f,0.0939125269651413f,0.20282304286956787f,-0.734791100025177f,0.0898694396018982f,-0.13186074793338776f,0.02113427221775055f,0.47443610429763794f,0.09890269488096237f,-0.0655839666724205f,-0.04224861413240433f,-0.30557194352149963f,0.7833285927772522f,0.17788979411125183f,0.0668090283870697f,1.285325527191162f},
Lightvalve 193:4b5e617b8481 216 {-0.03807421028614044f,-0.1136704608798027f,-0.40439701080322266f,-1.0595369338989258f,-0.13023366034030914f,0.10300719738006592f,-0.14926490187644958f,1.3389246463775635f,0.05884883925318718f,-0.18226976692676544f,0.31049856543540955f,-0.010614922270178795f,-0.21370303630828857f,-0.021214483305811882f,-0.08245879411697388f,2.3053054809570312f},
Lightvalve 193:4b5e617b8481 217 {-0.16781732439994812f,0.013002467341721058f,0.16706281900405884f,-1.1158241033554077f,-0.045615483075380325f,-0.4337211847305298f,-0.27456235885620117f,1.5922147035598755f,-0.2640377879142761f,-0.04499654844403267f,-1.8151977062225342f,0.38335272669792175f,-2.189336061477661f,0.884656548500061f,0.29427415132522583f,4.138731479644775f},
Lightvalve 65:a2d7c63419c2 218 };
Lightvalve 65:a2d7c63419c2 219
Lightvalve 169:645207e160ca 220 const float h2[16][16] = {
Lightvalve 193:4b5e617b8481 221 {-0.373119056224823f,3.5553245544433594f,-0.18637831509113312f,3.7563912868499756f,-0.3262759745121002f,-2.3471240997314453f,-0.08586140722036362f,-0.0851021334528923f,-0.43008196353912354f,-0.20591317117214203f,-3.0219662189483643f,-2.8412041664123535f,-1.2025442123413086f,-1.6519156694412231f,-1.1901919841766357f,-1.2586970329284668f},
Lightvalve 193:4b5e617b8481 222 {-0.21345800161361694f,-2.3205506801605225f,-0.09147269278764725f,-2.9883475303649902f,-0.38782384991645813f,-0.1081438660621643f,-1.32341730594635f,-0.39894118905067444f,-0.33757925033569336f,0.22082164883613586f,-12.76615047454834f,-10.454728126525879f,-0.8961352109909058f,3.2336769104003906f,1.445095181465149f,-1.3371248245239258f},
Lightvalve 179:d5377766d7ea 223 {-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 193:4b5e617b8481 224 {0.39014673233032227f,-0.39607951045036316f,-0.2894435524940491f,0.41481518745422363f,0.08946844935417175f,-0.22121116518974304f,-0.40275976061820984f,0.2650929391384125f,0.26211628317832947f,0.2953031361103058f,0.8374318480491638f,0.1486164778470993f,0.3700002133846283f,0.7084981799125671f,-0.2438845932483673f,0.17641611397266388f},
Lightvalve 193:4b5e617b8481 225 {-0.6145792603492737f,1.4241276979446411f,-0.38599735498428345f,1.0056519508361816f,-0.130391925573349f,-0.5785019993782043f,-0.8334283828735352f,-0.2552958130836487f,-0.024399548768997192f,-0.4501388370990753f,-10.357773780822754f,-5.493231773376465f,-2.6973838806152344f,-1.543803095817566f,-0.31024715304374695f,-2.4421327114105225f},
Lightvalve 193:4b5e617b8481 226 {-0.4475497901439667f,-0.48771294951438904f,-0.4826374053955078f,-0.7084127068519592f,-0.3307701051235199f,-0.23040759563446045f,-0.5431229472160339f,-0.053084585815668106f,-0.23047015070915222f,0.3046051263809204f,-2.272104501724243f,-6.695068359375f,-0.1596900373697281f,-0.07568152248859406f,-0.6455795168876648f,-1.9376535415649414f},
Lightvalve 193:4b5e617b8481 227 {-0.3334483802318573f,-0.4921075701713562f,-0.3260969817638397f,0.47393539547920227f,-0.4052906930446625f,0.9536952376365662f,-0.18061208724975586f,0.12387624382972717f,-0.0982072651386261f,0.07934291660785675f,-2.9281649589538574f,-6.097873687744141f,0.5353059768676758f,0.21125705540180206f,0.1671367734670639f,-0.6433596014976501f},
Lightvalve 193:4b5e617b8481 228 {0.002462406875565648f,-0.13203328847885132f,0.1748848259449005f,0.062082864344120026f,-0.24962033331394196f,0.3382720649242401f,0.22629152238368988f,-0.4360564351081848f,0.2600560486316681f,-0.37898191809654236f,-1.594963788986206f,-1.8958890438079834f,-1.448304533958435f,0.798218309879303f,0.4798305332660675f,-0.17965641617774963f},
Lightvalve 193:4b5e617b8481 229 {-0.7740408182144165f,-0.00020000309450551867f,-0.2609816789627075f,-0.3370492160320282f,-0.07906099408864975f,-0.018349304795265198f,-0.3007965683937073f,-0.17639212310314178f,0.06103590130805969f,-0.38046833872795105f,-0.23538073897361755f,0.0592195987701416f,-0.40873199701309204f,-0.3732284605503082f,0.07351911813020706f,-0.7348899245262146f},
Lightvalve 193:4b5e617b8481 230 {-0.1242457702755928f,0.10498577356338501f,-0.13502129912376404f,-0.060150690376758575f,0.12987366318702698f,0.3816811740398407f,0.2711336314678192f,-0.37463870644569397f,-0.13619378209114075f,0.16938945651054382f,0.43584179878234863f,0.3007356822490692f,0.1462758630514145f,-0.520169198513031f,-0.17727826535701752f,0.1449422985315323f},
Lightvalve 193:4b5e617b8481 231 {-1.2907301187515259f,-0.015403985045850277f,-0.11076373606920242f,0.21192514896392822f,-0.03321319818496704f,-1.347303032875061f,0.11128412932157516f,-0.20421427488327026f,-0.050184011459350586f,0.12480869889259338f,-18.543546676635742f,-0.28718826174736023f,-0.36511552333831787f,0.2045985460281372f,1.0332635641098022f,-0.9086235761642456f},
Lightvalve 193:4b5e617b8481 232 {0.08386392891407013f,2.098982334136963f,0.2407342940568924f,1.1502493619918823f,-0.10786735266447067f,-0.9310345649719238f,-1.3677669763565063f,-0.19258876144886017f,0.3487861454486847f,-0.3656117618083954f,-2.6828837394714355f,-2.9330313205718994f,-0.13798509538173676f,-0.05980028212070465f,0.9416042566299438f,-0.7318357825279236f},
Lightvalve 193:4b5e617b8481 233 {-0.028731070458889008f,-0.026831166818737984f,-0.15798500180244446f,-0.0034756625536829233f,-0.5573786497116089f,0.357199102640152f,-0.5187659859657288f,-0.46072864532470703f,-0.3764709532260895f,0.19903655350208282f,-1.1018555164337158f,0.6878526210784912f,0.03562719747424126f,-0.45660585165023804f,-0.06941013038158417f,-0.18342538177967072f},
Lightvalve 193:4b5e617b8481 234 {0.08938371390104294f,0.019140882417559624f,-0.25673651695251465f,-0.04428382217884064f,-0.3924296796321869f,1.991448163986206f,-0.9358435273170471f,-0.23740285634994507f,-0.24509364366531372f,0.1368061751127243f,0.5035690069198608f,1.8021210432052612f,-0.11762721836566925f,-2.0946662425994873f,-0.9601436853408813f,-0.21151992678642273f},
Lightvalve 179:d5377766d7ea 235 {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 193:4b5e617b8481 236 {-0.424070805311203f,0.010647752322256565f,0.015124130062758923f,0.033794887363910675f,0.11311512440443039f,-0.28193730115890503f,0.2703526020050049f,0.13120506703853607f,-0.3893685042858124f,-0.02222958207130432f,-1.432602882385254f,-0.9122090935707092f,-1.3930901288986206f,-0.15213778614997864f,-0.04224979132413864f,-1.4842122793197632f},
Lightvalve 65:a2d7c63419c2 237 };
Lightvalve 65:a2d7c63419c2 238
Lightvalve 169:645207e160ca 239 const float h3[16][16] = {
Lightvalve 193:4b5e617b8481 240 {-0.36079341173171997f,0.4573541581630707f,-0.325107216835022f,-0.4272470772266388f,-0.39737746119499207f,0.2834421396255493f,0.3332441747188568f,0.3036012351512909f,-0.2895206809043884f,0.08099097013473511f,-0.11870327591896057f,0.4001283347606659f,0.03681709244847298f,0.5518370866775513f,0.06409662961959839f,0.1620478332042694f},
Lightvalve 193:4b5e617b8481 241 {0.047732532024383545f,-0.300658255815506f,-1.3235893249511719f,-1.084731101989746f,-0.22308531403541565f,0.6925809979438782f,-0.3241617977619171f,-0.34273138642311096f,2.446197986602783f,-0.36785638332366943f,0.2839009761810303f,-0.43809396028518677f,2.1469027996063232f,0.9680188894271851f,0.5854582190513611f,0.2277306318283081f},
Lightvalve 193:4b5e617b8481 242 {0.07903262972831726f,0.2790505588054657f,-0.0448407344520092f,0.04248586297035217f,-0.17811879515647888f,-0.19260792434215546f,-0.4038352966308594f,0.015906542539596558f,0.15353140234947205f,0.030178606510162354f,0.2488909661769867f,0.13805970549583435f,-0.07610863447189331f,-0.20733052492141724f,-0.3036302626132965f,0.054825395345687866f},
Lightvalve 193:4b5e617b8481 243 {-0.30922991037368774f,-1.4248790740966797f,-2.2566752433776855f,-1.8004227876663208f,0.7235559821128845f,0.6167980432510376f,0.17782625555992126f,-0.7147418260574341f,2.1931867599487305f,0.4008365869522095f,-0.2853640913963318f,0.11604917794466019f,3.048943281173706f,3.169879913330078f,0.3572247326374054f,-1.7431739568710327f},
Lightvalve 193:4b5e617b8481 244 {-0.39607733488082886f,0.10962998867034912f,0.023933079093694687f,-0.042653702199459076f,0.10805846750736237f,-0.2995353639125824f,0.16012099385261536f,0.3626593053340912f,-0.32160404324531555f,-0.023096678778529167f,-0.10529157519340515f,-0.31317979097366333f,0.04330437257885933f,-0.16106648743152618f,0.1951502114534378f,0.31701672077178955f},
Lightvalve 193:4b5e617b8481 245 {-0.37449589371681213f,-0.9570617079734802f,-0.18645547330379486f,-0.7889876365661621f,0.17885488271713257f,0.2740497887134552f,-0.18732719123363495f,-0.05049999803304672f,-1.7403619289398193f,0.4164712131023407f,-0.21354427933692932f,-0.1020674780011177f,0.4254971146583557f,-0.030363813042640686f,1.014643907546997f,-0.10146742314100266f},
Lightvalve 193:4b5e617b8481 246 {0.4110594093799591f,0.08816589415073395f,-0.020595824345946312f,0.3703838288784027f,0.2720140516757965f,-0.4842216968536377f,-0.08670487999916077f,-0.25336313247680664f,-0.1550750732421875f,-0.987932562828064f,-0.1344406008720398f,0.35313835740089417f,-0.3957892060279846f,-0.15961946547031403f,0.22525130212306976f,0.3789818584918976f},
Lightvalve 193:4b5e617b8481 247 {-0.40892091393470764f,0.07838445156812668f,-0.3972336947917938f,0.22499339282512665f,0.3223019242286682f,-0.04307904466986656f,-0.23312048614025116f,-0.390264093875885f,0.28059282898902893f,-0.1559126079082489f,-0.14134526252746582f,-0.0003446042537689209f,-0.30871930718421936f,-0.33111128211021423f,0.08064798265695572f,0.08909518271684647f},
Lightvalve 183:55044a4018a9 248 {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 193:4b5e617b8481 249 {0.2847062647342682f,0.018552124500274658f,0.11435768008232117f,0.36562982201576233f,-0.19755424559116364f,0.4533067047595978f,0.2430230677127838f,0.2909286320209503f,-0.2802048921585083f,0.02909536473453045f,0.41849127411842346f,-0.287167489528656f,0.0940583124756813f,-0.14209046959877014f,0.0495246946811676f,-0.0824357196688652f},
Lightvalve 193:4b5e617b8481 250 {0.15459725260734558f,0.4935417175292969f,-0.25264647603034973f,0.11184422671794891f,-0.3341422379016876f,1.1511648893356323f,-0.08742031455039978f,-0.36949634552001953f,1.6829259395599365f,2.233215093612671f,0.07771988958120346f,-0.41695454716682434f,-0.10912178456783295f,1.7060784101486206f,2.532484769821167f,2.331334114074707f},
Lightvalve 193:4b5e617b8481 251 {-0.408692330121994f,0.8799136281013489f,-0.14514438807964325f,-0.20895916223526f,-1.578966498374939f,1.469042181968689f,0.03285527229309082f,0.4112691879272461f,1.0228923559188843f,2.2706377506256104f,0.12809070944786072f,0.03996849060058594f,0.9477834105491638f,1.5862021446228027f,0.10855130106210709f,-2.259545087814331f},
Lightvalve 193:4b5e617b8481 252 {-0.2991822361946106f,-0.6050435900688171f,0.9256192445755005f,-0.08229707181453705f,0.15689420700073242f,0.4238896071910858f,-0.11253207921981812f,0.2177363783121109f,-0.05294230207800865f,-0.5830169916152954f,-1.0703593492507935f,-0.36229726672172546f,-0.8443012237548828f,0.37606582045555115f,0.5651885867118835f,-1.0894941091537476f},
Lightvalve 193:4b5e617b8481 253 {-0.2675279378890991f,-2.22987699508667f,0.15863703191280365f,-0.21841442584991455f,1.3560339212417603f,-0.492367148399353f,-0.32875844836235046f,-0.802202045917511f,-0.1637519896030426f,-4.078615188598633f,-1.2283751964569092f,-0.5240398645401001f,0.19471454620361328f,-0.13531909883022308f,1.8324934244155884f,0.5436633825302124f},
Lightvalve 193:4b5e617b8481 254 {-0.015470266342163086f,-1.0924882888793945f,0.5259896516799927f,0.0900004431605339f,-0.9157431721687317f,-0.07713504880666733f,-0.021036196500062943f,-0.6448765397071838f,-0.3460008203983307f,0.1362907439470291f,-0.37829816341400146f,-0.20573563873767853f,-0.7316341996192932f,-0.5581928491592407f,-1.8910645246505737f,-0.6874275207519531f},
Lightvalve 193:4b5e617b8481 255 {0.1863725483417511f,0.9558444023132324f,-0.8446718454360962f,-0.6416004300117493f,0.08933153003454208f,0.5898428559303284f,0.04860696196556091f,0.17769548296928406f,1.308203935623169f,0.42126715183258057f,-0.4451454281806946f,0.08378204703330994f,0.3650934100151062f,0.6331697702407837f,0.21046006679534912f,0.009809033945202827f},
Lightvalve 66:a8e6799dbce3 256 };
Lightvalve 65:a2d7c63419c2 257
Lightvalve 193:4b5e617b8481 258 const float hout[16] = { 0.45773375034332275f,0.10028111189603806f,-0.15963390469551086f,-0.16705428063869476f,-0.13918212056159973f,0.13862498104572296f,-0.03826538845896721f,-0.08387956768274307f,-0.13896769285202026f,-0.1727340966463089f,0.4263496398925781f,0.039233312010765076f,-0.14129970967769623f,0.21044084429740906f,0.17624303698539734f,-0.1203421801328659f };
Lightvalve 193:4b5e617b8481 259
Lightvalve 193:4b5e617b8481 260 const float b1[16] = { 0.6145862936973572f,1.4653651714324951f,-1.7145336866378784f,-0.8318495154380798f,1.8101054430007935f,1.8614740371704102f,1.4533389806747437f,2.6761667728424072f,0.17127400636672974f,-1.12446928024292f,-1.444535255432129f,1.2159825563430786f,0.3056347072124481f,-1.360192060470581f,-1.087764859199524f,-2.10600209236145f };
Lightvalve 193:4b5e617b8481 261
Lightvalve 193:4b5e617b8481 262 const float b2[16] = { -0.23251420259475708f,-0.42732852697372437f,-1.6084238290786743f,-0.17800576984882355f,-0.9509057402610779f,-0.00474552670493722f,0.10984312742948532f,-0.4811083972454071f,-1.20063054561615f,-1.9749670028686523f,1.7751399278640747f,1.0225811004638672f,-0.07708542048931122f,1.9233418703079224f,0.42136311531066895f,0.17162247002124786f };
Lightvalve 193:4b5e617b8481 263
Lightvalve 193:4b5e617b8481 264 const float b3[16] = { -1.963319182395935f,-0.9337409734725952f,0.4731481671333313f,-0.5163841247558594f,-0.23898737132549286f,0.5903229713439941f,-0.4740028977394104f,-0.8475961685180664f,-0.9789288640022278f,-0.7117742300033569f,-0.2875189781188965f,-0.3560875654220581f,-1.3554514646530151f,-0.16300444304943085f,-0.8108376264572144f,0.2676752209663391f };
Lightvalve 193:4b5e617b8481 265
Lightvalve 193:4b5e617b8481 266 const float bout[1] = { -0.12355829030275345f };
Lightvalve 65:a2d7c63419c2 267
Lightvalve 170:42c938a40313 268 /////////////////////////////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 269 float input_RL[num_input_RL] = { 0.0f };
Lightvalve 170:42c938a40313 270
Lightvalve 170:42c938a40313 271 //Critic Networks
Lightvalve 173:68c7914679ec 272 float hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 273 float bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 274 float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 275 float bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 276 float hc3[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 277 float bc3 = 0.0f;
Lightvalve 170:42c938a40313 278
Lightvalve 170:42c938a40313 279 //Critic Networks Temporary
Lightvalve 173:68c7914679ec 280 float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 281 float bc1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 282 float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 283 float bc2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 284 float hc3_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 285 float bc3_temp = 0.0f;
Lightvalve 170:42c938a40313 286
Lightvalve 170:42c938a40313 287 //Actor Networks
Lightvalve 173:68c7914679ec 288 float ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 289 float ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 290 float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 291 float ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 292 float ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 293 float ba3[2] = {0.0f};
Lightvalve 170:42c938a40313 294
Lightvalve 170:42c938a40313 295 //Actor Networks Temporary
Lightvalve 173:68c7914679ec 296 float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 297 float ba1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 298 float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 299 float ba2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 300 float ha3_temp[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 301 float ba3_temp[2] = {0.0f};
Lightvalve 66:a8e6799dbce3 302
Lightvalve 87:471334725012 303 float VALVE_POS_RAW_NN = 0.0f;
Lightvalve 87:471334725012 304 float DDV_JOINT_POS_FF(float REF_JOINT_VEL);
Lightvalve 61:bc8c8270f0ab 305
Lightvalve 179:d5377766d7ea 306 /////////////////////////////////////////////RL tuning
Lightvalve 179:d5377766d7ea 307 float Gradient_Limit = 0.5f;
Lightvalve 179:d5377766d7ea 308 float gradient_rate_actor = 0.001f;
Lightvalve 179:d5377766d7ea 309 float gradient_rate_critic = 0.001f;
Lightvalve 179:d5377766d7ea 310 //////////////////////////////////////////////////////////////////////////////
Lightvalve 170:42c938a40313 311
Lightvalve 170:42c938a40313 312 float Critic_Network_Temp(float *arr)
Lightvalve 170:42c938a40313 313 {
Lightvalve 173:68c7914679ec 314 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 315 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 316 float output = 0.0f;
Lightvalve 173:68c7914679ec 317 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 318 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 319 output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 320 }
Lightvalve 173:68c7914679ec 321 //ReLU
Lightvalve 173:68c7914679ec 322 output1[index2] = output1[index2] + bc1_temp[index2];
Lightvalve 173:68c7914679ec 323 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 324 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 325 output1[index2] = 0;
Lightvalve 173:68c7914679ec 326 }
Lightvalve 173:68c7914679ec 327 //tanh
Lightvalve 173:68c7914679ec 328 //output1[index2] = tanh(output1[index2] + bc1_temp[index2]);
Lightvalve 173:68c7914679ec 329 }
Lightvalve 173:68c7914679ec 330 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 331 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 332 output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 333 }
Lightvalve 173:68c7914679ec 334 //ReLU
Lightvalve 173:68c7914679ec 335 output2[index2] = output2[index2] + bc2_temp[index2];
Lightvalve 173:68c7914679ec 336 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 337 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 338 output2[index2] = 0;
Lightvalve 173:68c7914679ec 339 }
Lightvalve 173:68c7914679ec 340 //tanh
Lightvalve 173:68c7914679ec 341 //output2[index2] = tanh(output2[index2] + bc2_temp[index2]);
Lightvalve 170:42c938a40313 342 }
Lightvalve 170:42c938a40313 343 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 344 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 345 output = output + hc3_temp[index1] * output2[index1];
Lightvalve 170:42c938a40313 346 }
Lightvalve 173:68c7914679ec 347 output = output + bc3_temp;
Lightvalve 173:68c7914679ec 348 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 349 }
Lightvalve 170:42c938a40313 350 return output;
Lightvalve 170:42c938a40313 351 }
Lightvalve 170:42c938a40313 352
Lightvalve 170:42c938a40313 353
Lightvalve 170:42c938a40313 354 void Actor_Network(float *arr)
Lightvalve 170:42c938a40313 355 {
Lightvalve 173:68c7914679ec 356 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 357 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 170:42c938a40313 358 float output[2] = {0.0f};
Lightvalve 170:42c938a40313 359
Lightvalve 173:68c7914679ec 360 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 361 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 178:1074553d2f6f 362 output1[index2] = output1[index2] + ha1_temp[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 363 }
Lightvalve 178:1074553d2f6f 364 output1[index2] = output1[index2] + ba1_temp[index2];
Lightvalve 173:68c7914679ec 365 hx_a_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 366 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 367 output1[index2] = 0;
Lightvalve 173:68c7914679ec 368 }
Lightvalve 173:68c7914679ec 369 }
Lightvalve 173:68c7914679ec 370 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 371 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 178:1074553d2f6f 372 output2[index2] = output2[index2] + ha2_temp[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 373 }
Lightvalve 178:1074553d2f6f 374 output2[index2] = output2[index2] + ba2_temp[index2];
Lightvalve 173:68c7914679ec 375 hxh_a_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 376 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 377 output2[index2] = 0;
Lightvalve 173:68c7914679ec 378 }
Lightvalve 173:68c7914679ec 379 }
Lightvalve 173:68c7914679ec 380 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 381 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 178:1074553d2f6f 382 output[index2] = output[index2] + ha3_temp[index1][index2] * output2[index1];
Lightvalve 173:68c7914679ec 383 }
Lightvalve 178:1074553d2f6f 384 hxhh_a_sum[index2] = output[index2] + ba3_temp[index2];
Lightvalve 173:68c7914679ec 385 }
Lightvalve 178:1074553d2f6f 386
Lightvalve 178:1074553d2f6f 387 mean_before_SP = output[0] + ba3_temp[0]; //SP = softplus
Lightvalve 178:1074553d2f6f 388 deviation_before_SP = output[1] + ba3_temp[1];
Lightvalve 173:68c7914679ec 389 //Softplus
Lightvalve 173:68c7914679ec 390 mean = log(1.0f+exp(mean_before_SP));
Lightvalve 173:68c7914679ec 391 deviation = log(1.0f+exp(deviation_before_SP));
Lightvalve 179:d5377766d7ea 392 logging2 = mean;
Lightvalve 179:d5377766d7ea 393 logging4 = deviation;
Lightvalve 173:68c7914679ec 394 }
Lightvalve 173:68c7914679ec 395
Lightvalve 173:68c7914679ec 396
Lightvalve 173:68c7914679ec 397 void Actor_Network_Old(float *arr)
Lightvalve 173:68c7914679ec 398 {
Lightvalve 173:68c7914679ec 399 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 400 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 401 float output[2] = {0.0f};
Lightvalve 173:68c7914679ec 402
Lightvalve 173:68c7914679ec 403 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 404 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 405 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 406 }
Lightvalve 170:42c938a40313 407 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 170:42c938a40313 408 if (output1[index2] < 0) {
Lightvalve 170:42c938a40313 409 output1[index2] = 0;
Lightvalve 170:42c938a40313 410 }
Lightvalve 170:42c938a40313 411 }
Lightvalve 173:68c7914679ec 412 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 413 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 414 output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1];
Lightvalve 170:42c938a40313 415 }
Lightvalve 173:68c7914679ec 416 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 417 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 418 output2[index2] = 0;
Lightvalve 170:42c938a40313 419 }
Lightvalve 170:42c938a40313 420 }
Lightvalve 170:42c938a40313 421 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 422 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 423 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 170:42c938a40313 424 }
Lightvalve 170:42c938a40313 425 }
Lightvalve 173:68c7914679ec 426 mean_old = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 427 deviation_old = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 428 //Softplus
Lightvalve 173:68c7914679ec 429 mean_old = log(1.0f+exp(mean_old));
Lightvalve 173:68c7914679ec 430 deviation_old = log(1.0f+exp(deviation_old));
Lightvalve 170:42c938a40313 431 }
Lightvalve 170:42c938a40313 432
Lightvalve 170:42c938a40313 433 float Grad_Normal_Dist_Mean(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 434 {
Lightvalve 170:42c938a40313 435 float grad_mean = 0.0f;
Lightvalve 170:42c938a40313 436 grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation);
Lightvalve 170:42c938a40313 437 return grad_mean;
Lightvalve 170:42c938a40313 438 }
Lightvalve 170:42c938a40313 439
Lightvalve 170:42c938a40313 440 float Grad_Normal_Dist_Deviation(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 441 {
Lightvalve 170:42c938a40313 442 float grad_dev = 0.0f;
Lightvalve 170:42c938a40313 443 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 444 return grad_dev;
Lightvalve 170:42c938a40313 445 }
Lightvalve 170:42c938a40313 446
Lightvalve 173:68c7914679ec 447 float ReLU(float x)
Lightvalve 173:68c7914679ec 448 {
Lightvalve 173:68c7914679ec 449 if (x >= 0) {
Lightvalve 173:68c7914679ec 450 return x;
Lightvalve 173:68c7914679ec 451 } else {
Lightvalve 173:68c7914679ec 452 return 0.0f;
Lightvalve 173:68c7914679ec 453 }
Lightvalve 173:68c7914679ec 454 }
Lightvalve 173:68c7914679ec 455
Lightvalve 170:42c938a40313 456 void update_Critic_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 457 {
Lightvalve 173:68c7914679ec 458 float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 177:8e9cf31d63f4 459 float G_bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 460 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 461 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 462 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 463 float d_V_d_hc1 = 0.0f;
Lightvalve 173:68c7914679ec 464 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 465 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 466 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 467 d_V_d_hc1 = d_V_d_hc1 + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 468 }
Lightvalve 173:68c7914679ec 469 }
Lightvalve 170:42c938a40313 470 }
Lightvalve 177:8e9cf31d63f4 471 G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1);
Lightvalve 170:42c938a40313 472 }
Lightvalve 170:42c938a40313 473 G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 474 if(G_hc1[index1][index2] > Gradient_Limit) G_hc1[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 475 else if (G_hc1[index1][index2] < -Gradient_Limit) G_hc1[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 476 //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2];
Lightvalve 173:68c7914679ec 477 }
Lightvalve 173:68c7914679ec 478 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 479 float d_V_d_bc1 = 0.0f;
Lightvalve 173:68c7914679ec 480 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 481 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 482 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 483 d_V_d_bc1 = d_V_d_bc1 + hc2_temp[index2][k]*hc3_temp[k];
Lightvalve 173:68c7914679ec 484 }
Lightvalve 173:68c7914679ec 485 }
Lightvalve 173:68c7914679ec 486 }
Lightvalve 177:8e9cf31d63f4 487 G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1);
Lightvalve 173:68c7914679ec 488 }
Lightvalve 173:68c7914679ec 489 G_bc1[index2] = G_bc1[index2] / batch_size;
Lightvalve 179:d5377766d7ea 490 if(G_bc1[index2] > Gradient_Limit) G_bc1[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 491 else if (G_bc1[index2] < -Gradient_Limit) G_bc1[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 492 //bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2];
Lightvalve 173:68c7914679ec 493 }
Lightvalve 173:68c7914679ec 494
Lightvalve 175:2f7289dbd488 495
Lightvalve 173:68c7914679ec 496 float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 497 float G_bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 498 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 499 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 500 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 501 float d_V_d_hc2 = 0.0f;
Lightvalve 173:68c7914679ec 502 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 503 if (hx_c_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 504 d_V_d_hc2 = hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 173:68c7914679ec 505 }
Lightvalve 173:68c7914679ec 506 }
Lightvalve 177:8e9cf31d63f4 507 G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2);
Lightvalve 173:68c7914679ec 508 }
Lightvalve 173:68c7914679ec 509 G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 510 if(G_hc2[index1][index2] > Gradient_Limit) G_hc2[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 511 else if (G_hc2[index1][index2] < -Gradient_Limit) G_hc2[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 512 //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 513 }
Lightvalve 173:68c7914679ec 514 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 515 float d_V_d_bc2 = 0.0f;
Lightvalve 173:68c7914679ec 516 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 177:8e9cf31d63f4 517 d_V_d_bc2 = hc3_temp[index2];
Lightvalve 173:68c7914679ec 518 }
Lightvalve 177:8e9cf31d63f4 519 G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2);
Lightvalve 173:68c7914679ec 520 }
Lightvalve 173:68c7914679ec 521 G_bc2[index2] = G_bc2[index2] / batch_size;
Lightvalve 179:d5377766d7ea 522 if(G_bc2[index2] > Gradient_Limit) G_bc2[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 523 else if (G_bc2[index2] < -Gradient_Limit) G_bc2[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 524 //bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2];
Lightvalve 173:68c7914679ec 525 }
Lightvalve 173:68c7914679ec 526
Lightvalve 173:68c7914679ec 527 float G_hc3[num_hidden_unit2]= {0.0f};
Lightvalve 173:68c7914679ec 528 float G_bc3 = 0.0f;
Lightvalve 173:68c7914679ec 529 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 530 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 531 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 532 float d_V_d_hc3 = 0.0f;
Lightvalve 173:68c7914679ec 533 if (hxh_c_sum_array[n][index1] >= 0) {
Lightvalve 177:8e9cf31d63f4 534 d_V_d_hc3 = d_V_d_hc3 + hxh_c_sum_array[n][index1];
Lightvalve 173:68c7914679ec 535 }
Lightvalve 177:8e9cf31d63f4 536 G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3);
Lightvalve 173:68c7914679ec 537 }
Lightvalve 173:68c7914679ec 538 G_hc3[index1] = G_hc3[index1] / batch_size;
Lightvalve 179:d5377766d7ea 539 if(G_hc3[index1] > Gradient_Limit) G_hc3[index1] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 540 else if (G_hc3[index1] < -Gradient_Limit) G_hc3[index1] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 541 //hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1];
Lightvalve 173:68c7914679ec 542 }
Lightvalve 173:68c7914679ec 543 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 544 float d_V_d_bc3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 545 d_V_d_bc3 = 1.0f;
Lightvalve 177:8e9cf31d63f4 546 G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3);
Lightvalve 173:68c7914679ec 547 }
Lightvalve 173:68c7914679ec 548 G_bc3 = G_bc3 / batch_size;
Lightvalve 179:d5377766d7ea 549 if(G_bc3 > Gradient_Limit) G_bc3 = Gradient_Limit;
Lightvalve 179:d5377766d7ea 550 else if (G_bc3 < -Gradient_Limit) G_bc3 = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 551 //bc3_temp = bc3_temp - gradient_rate_critic * G_bc3;
Lightvalve 173:68c7914679ec 552 }
Lightvalve 173:68c7914679ec 553
Lightvalve 173:68c7914679ec 554 // Simultaneous Update
Lightvalve 173:68c7914679ec 555 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 556 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 557 hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2];
Lightvalve 170:42c938a40313 558 }
Lightvalve 179:d5377766d7ea 559 bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2];
Lightvalve 170:42c938a40313 560 }
Lightvalve 173:68c7914679ec 561 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 562 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 563 hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 564 }
Lightvalve 179:d5377766d7ea 565 bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2];
Lightvalve 173:68c7914679ec 566 }
Lightvalve 170:42c938a40313 567 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 568 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 569 hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1];
Lightvalve 170:42c938a40313 570 }
Lightvalve 179:d5377766d7ea 571 bc3_temp = bc3_temp - gradient_rate_critic * G_bc3;
Lightvalve 170:42c938a40313 572 }
Lightvalve 170:42c938a40313 573 }
Lightvalve 170:42c938a40313 574
Lightvalve 173:68c7914679ec 575 ///////////////////////////Softplus//////////////////////////////////
Lightvalve 170:42c938a40313 576 void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 577 {
Lightvalve 179:d5377766d7ea 578
Lightvalve 173:68c7914679ec 579
Lightvalve 173:68c7914679ec 580 float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 581 float G_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 582
Lightvalve 173:68c7914679ec 583 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 584 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 585 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 586 float d_x_d_ha1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 587 float d_y_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 588 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 589 G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 170:42c938a40313 590 } else {
Lightvalve 173:68c7914679ec 591 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 592 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 593 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 594 d_x_d_ha1 = d_x_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 177:8e9cf31d63f4 595 d_y_d_ha1 = d_y_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 596 }
Lightvalve 171:bfc1fd2629d8 597 }
Lightvalve 170:42c938a40313 598 }
Lightvalve 170:42c938a40313 599 float d_mean_d_ha1 = 0.0f;
Lightvalve 170:42c938a40313 600 float d_dev_d_ha1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 601 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 602 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 603
Lightvalve 173:68c7914679ec 604 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 605 }
Lightvalve 170:42c938a40313 606 }
Lightvalve 176:589ea3edcf3c 607 G_ha1[index1][index2] = -G_ha1[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 608 if(G_ha1[index1][index2] > Gradient_Limit) G_ha1[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 609 else if (G_ha1[index1][index2] < -Gradient_Limit) G_ha1[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 610 //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2];
Lightvalve 170:42c938a40313 611 }
Lightvalve 173:68c7914679ec 612
Lightvalve 173:68c7914679ec 613 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 614 float d_x_d_ba1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 615 float d_y_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 616 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 617 G_ba1[index2] = G_ba1[index2];
Lightvalve 170:42c938a40313 618 } else {
Lightvalve 173:68c7914679ec 619 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 620 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 621 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 177:8e9cf31d63f4 622 d_x_d_ba1 = d_x_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 177:8e9cf31d63f4 623 d_y_d_ba1 = d_y_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 624 }
Lightvalve 171:bfc1fd2629d8 625 }
Lightvalve 170:42c938a40313 626 }
Lightvalve 170:42c938a40313 627 float d_mean_d_ba1 = 0.0f;
Lightvalve 170:42c938a40313 628 float d_dev_d_ba1 = 0.0f;
Lightvalve 177:8e9cf31d63f4 629 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 630 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 631
Lightvalve 173:68c7914679ec 632 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 633 }
Lightvalve 170:42c938a40313 634 }
Lightvalve 176:589ea3edcf3c 635 G_ba1[index2] = -G_ba1[index2] / batch_size;
Lightvalve 179:d5377766d7ea 636 if(G_ba1[index2] > Gradient_Limit) G_ba1[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 637 else if (G_ba1[index2] < -Gradient_Limit) G_ba1[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 638 //ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2];
Lightvalve 170:42c938a40313 639 }
Lightvalve 170:42c938a40313 640
Lightvalve 173:68c7914679ec 641 float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 642 float G_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 643
Lightvalve 173:68c7914679ec 644 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 645 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 646 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 647 float d_x_d_ha2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 648 float d_y_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 649 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 650 G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 170:42c938a40313 651 } else {
Lightvalve 173:68c7914679ec 652 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 653 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 654 d_x_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][0];
Lightvalve 177:8e9cf31d63f4 655 d_y_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][1];
Lightvalve 171:bfc1fd2629d8 656 }
Lightvalve 170:42c938a40313 657 }
Lightvalve 173:68c7914679ec 658
Lightvalve 170:42c938a40313 659 float d_mean_d_ha2 = 0.0f;
Lightvalve 170:42c938a40313 660 float d_dev_d_ha2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 661 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 662 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 663
Lightvalve 173:68c7914679ec 664 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 665 }
Lightvalve 170:42c938a40313 666 }
Lightvalve 176:589ea3edcf3c 667 G_ha2[index1][index2] = -G_ha2[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 668 if(G_ha2[index1][index2] > Gradient_Limit) G_ha2[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 669 else if (G_ha2[index1][index2] < -Gradient_Limit) G_ha2[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 670 //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2];
Lightvalve 170:42c938a40313 671 }
Lightvalve 173:68c7914679ec 672
Lightvalve 173:68c7914679ec 673 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 674 float d_x_d_ba2 = 0.0f;
Lightvalve 177:8e9cf31d63f4 675 float d_y_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 676 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 677 G_ba2[index2] = G_ba2[index2];
Lightvalve 170:42c938a40313 678 } else {
Lightvalve 170:42c938a40313 679
Lightvalve 173:68c7914679ec 680 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 177:8e9cf31d63f4 681 d_x_d_ba2 = ha3_temp[index2][0];
Lightvalve 177:8e9cf31d63f4 682 d_y_d_ba2 = ha3_temp[index2][1];
Lightvalve 173:68c7914679ec 683 }
Lightvalve 173:68c7914679ec 684 float d_mean_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 685 float d_dev_d_ba2= 0.0f;
Lightvalve 177:8e9cf31d63f4 686 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 687 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 688
Lightvalve 173:68c7914679ec 689 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 690 }
Lightvalve 170:42c938a40313 691 }
Lightvalve 176:589ea3edcf3c 692 G_ba2[index2] = -G_ba2[index2] / batch_size;
Lightvalve 179:d5377766d7ea 693 if(G_ba2[index2] > Gradient_Limit) G_ba2[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 694 else if (G_ba2[index2] < -Gradient_Limit) G_ba2[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 695 //ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2];
Lightvalve 170:42c938a40313 696 }
Lightvalve 173:68c7914679ec 697
Lightvalve 173:68c7914679ec 698 float G_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 699 float G_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 700
Lightvalve 173:68c7914679ec 701 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 702 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 703 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 704 float d_x_d_ha3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 705 float d_y_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 706 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 707 G_ha3[index1][index2] = G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 708 } else {
Lightvalve 173:68c7914679ec 709 if (hxh_a_sum_array[n][index1] >= 0) {
Lightvalve 173:68c7914679ec 710 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 177:8e9cf31d63f4 711 d_x_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 177:8e9cf31d63f4 712 d_y_d_ha3 = hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 713 }
Lightvalve 173:68c7914679ec 714 }
Lightvalve 173:68c7914679ec 715 float d_mean_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 716 float d_dev_d_ha3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 717 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 718 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 719
Lightvalve 173:68c7914679ec 720 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 721 }
Lightvalve 173:68c7914679ec 722 }
Lightvalve 176:589ea3edcf3c 723 G_ha3[index1][index2] = -G_ha3[index1][index2] / batch_size;
Lightvalve 179:d5377766d7ea 724 if(G_ha3[index1][index2] > Gradient_Limit) G_ha3[index1][index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 725 else if (G_ha3[index1][index2] < -Gradient_Limit) G_ha3[index1][index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 726 //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 727 }
Lightvalve 173:68c7914679ec 728
Lightvalve 173:68c7914679ec 729 for (int n=0; n<batch_size; n++) {
Lightvalve 177:8e9cf31d63f4 730 float d_x_d_ba3 = 0.0f;
Lightvalve 177:8e9cf31d63f4 731 float d_y_d_ba3 = 0.0f;
Lightvalve 173:68c7914679ec 732 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 733 G_ba3[index2] = G_ba3[index2];
Lightvalve 173:68c7914679ec 734 } else {
Lightvalve 173:68c7914679ec 735
Lightvalve 177:8e9cf31d63f4 736 d_x_d_ba3 = 1.0f;
Lightvalve 177:8e9cf31d63f4 737 d_y_d_ba3 = 1.0f;
Lightvalve 173:68c7914679ec 738
Lightvalve 173:68c7914679ec 739 float d_mean_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 740 float d_dev_d_ba3= 0.0f;
Lightvalve 177:8e9cf31d63f4 741 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 742 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 743
Lightvalve 173:68c7914679ec 744 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 745 }
Lightvalve 173:68c7914679ec 746 }
Lightvalve 176:589ea3edcf3c 747 G_ba3[index2] = -G_ba3[index2] / batch_size;
Lightvalve 179:d5377766d7ea 748 if(G_ba3[index2] > Gradient_Limit) G_ba3[index2] = Gradient_Limit;
Lightvalve 179:d5377766d7ea 749 else if (G_ba3[index2] < -Gradient_Limit) G_ba3[index2] = -Gradient_Limit;
Lightvalve 179:d5377766d7ea 750 //ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2];
Lightvalve 175:2f7289dbd488 751 }
Lightvalve 177:8e9cf31d63f4 752
Lightvalve 175:2f7289dbd488 753 // Simultaneous Update
Lightvalve 175:2f7289dbd488 754 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 175:2f7289dbd488 755 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 756 ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2];
Lightvalve 175:2f7289dbd488 757 }
Lightvalve 179:d5377766d7ea 758 ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2];
Lightvalve 175:2f7289dbd488 759 }
Lightvalve 175:2f7289dbd488 760 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 175:2f7289dbd488 761 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 762 ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2];
Lightvalve 175:2f7289dbd488 763 }
Lightvalve 179:d5377766d7ea 764 ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2];
Lightvalve 175:2f7289dbd488 765 }
Lightvalve 175:2f7289dbd488 766 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 175:2f7289dbd488 767 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 768 ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2];
Lightvalve 175:2f7289dbd488 769 }
Lightvalve 179:d5377766d7ea 770 ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2];
Lightvalve 173:68c7914679ec 771 }
Lightvalve 170:42c938a40313 772 }
Lightvalve 170:42c938a40313 773
Lightvalve 170:42c938a40313 774 float rand_normal(double mean, double stddev)
Lightvalve 170:42c938a40313 775 {
Lightvalve 170:42c938a40313 776 //Box muller method
Lightvalve 170:42c938a40313 777 static double n2 = 0.0f;
Lightvalve 170:42c938a40313 778 static int n2_cached = 0;
Lightvalve 170:42c938a40313 779 if (!n2_cached) {
Lightvalve 170:42c938a40313 780 double x, y, r;
Lightvalve 170:42c938a40313 781 do {
Lightvalve 170:42c938a40313 782 x = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 783 y = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 784
Lightvalve 170:42c938a40313 785 r = x*x + y*y;
Lightvalve 170:42c938a40313 786 } while (r == 0.0f || r > 1.0f);
Lightvalve 170:42c938a40313 787 {
Lightvalve 170:42c938a40313 788 double d = sqrt(-2.0f*log(r)/r);
Lightvalve 170:42c938a40313 789 double n1 = x*d;
Lightvalve 170:42c938a40313 790 n2 = y*d;
Lightvalve 170:42c938a40313 791 double result = n1*stddev + mean;
Lightvalve 170:42c938a40313 792 n2_cached = 1;
Lightvalve 170:42c938a40313 793 return result;
Lightvalve 170:42c938a40313 794 }
Lightvalve 170:42c938a40313 795 } else {
Lightvalve 170:42c938a40313 796 n2_cached = 0;
Lightvalve 170:42c938a40313 797 return n2*stddev + mean;
Lightvalve 170:42c938a40313 798 }
Lightvalve 170:42c938a40313 799 }
Lightvalve 170:42c938a40313 800
Lightvalve 179:d5377766d7ea 801 float mean_adv(float x[], int size)
Lightvalve 179:d5377766d7ea 802 {
Lightvalve 179:d5377766d7ea 803 float add = 0.0f;
Lightvalve 179:d5377766d7ea 804 float result;
Lightvalve 179:d5377766d7ea 805
Lightvalve 179:d5377766d7ea 806 for (int i=0; i<size; i++)
Lightvalve 179:d5377766d7ea 807 {
Lightvalve 179:d5377766d7ea 808 add += x[i];
Lightvalve 179:d5377766d7ea 809 }
Lightvalve 179:d5377766d7ea 810 result = (float) add/size;
Lightvalve 179:d5377766d7ea 811 return result;
Lightvalve 179:d5377766d7ea 812 }
Lightvalve 179:d5377766d7ea 813 float deviation_adv(float x[], int size)
Lightvalve 179:d5377766d7ea 814 {
Lightvalve 179:d5377766d7ea 815 float sigma = 0.0f;
Lightvalve 179:d5377766d7ea 816 float resultDeb = 0.0f;
Lightvalve 179:d5377766d7ea 817
Lightvalve 179:d5377766d7ea 818 for (int k=0; k<size; k++)
Lightvalve 179:d5377766d7ea 819 {
Lightvalve 179:d5377766d7ea 820 sigma = pow((float)x[k]-mean_adv(x,size), (float)2.0f)/(size-1);
Lightvalve 179:d5377766d7ea 821 resultDeb += sqrt(sigma);
Lightvalve 179:d5377766d7ea 822 }
Lightvalve 179:d5377766d7ea 823 return resultDeb;
Lightvalve 179:d5377766d7ea 824 }
Lightvalve 179:d5377766d7ea 825
Lightvalve 173:68c7914679ec 826
Lightvalve 170:42c938a40313 827 void Overwirte_Critic_Networks()
Lightvalve 170:42c938a40313 828 {
Lightvalve 173:68c7914679ec 829 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 830 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 831 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 170:42c938a40313 832 }
Lightvalve 170:42c938a40313 833 bc1[index2] = bc1_temp[index2];
Lightvalve 170:42c938a40313 834 }
Lightvalve 173:68c7914679ec 835 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 836 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 837 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 173:68c7914679ec 838 }
Lightvalve 173:68c7914679ec 839 bc2[index2] = bc2_temp[index2];
Lightvalve 173:68c7914679ec 840 hc3[index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 841 }
Lightvalve 173:68c7914679ec 842 bc3 = bc3_temp;
Lightvalve 170:42c938a40313 843 }
Lightvalve 170:42c938a40313 844 void Overwirte_Actor_Networks()
Lightvalve 170:42c938a40313 845 {
Lightvalve 173:68c7914679ec 846 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 847 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 848 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 170:42c938a40313 849 }
Lightvalve 170:42c938a40313 850 ba1[index2] = ba1_temp[index2];
Lightvalve 170:42c938a40313 851 }
Lightvalve 173:68c7914679ec 852 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 853 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:42c938a40313 854 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 170:42c938a40313 855 }
Lightvalve 170:42c938a40313 856 ba2[index2] = ba2_temp[index2];
Lightvalve 170:42c938a40313 857 }
Lightvalve 173:68c7914679ec 858 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 859 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 860 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 173:68c7914679ec 861 }
Lightvalve 173:68c7914679ec 862 ba3[index2] = ba3_temp[index2];
Lightvalve 173:68c7914679ec 863 }
Lightvalve 170:42c938a40313 864 }
Lightvalve 170:42c938a40313 865
Lightvalve 170:42c938a40313 866
GiJeongKim 0:51c43836c1d7 867 int main()
GiJeongKim 0:51c43836c1d7 868 {
Lightvalve 66:a8e6799dbce3 869
Lightvalve 65:a2d7c63419c2 870 HAL_Init();
Lightvalve 65:a2d7c63419c2 871 SystemClock_Config();
Lightvalve 169:645207e160ca 872
jobuuu 6:df07d3491e3a 873 /*********************************
jobuuu 1:e04e563be5ce 874 *** Initialization
jobuuu 6:df07d3491e3a 875 *********************************/
Lightvalve 69:3995ffeaa786 876 LED = 0;
Lightvalve 61:bc8c8270f0ab 877 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 878
GiJeongKim 0:51c43836c1d7 879 // i2c init
Lightvalve 8:5d2eebdad025 880 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 881 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 882 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 883 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 884 make_delay();
jobuuu 2:a1c0a37df760 885
GiJeongKim 0:51c43836c1d7 886 // // spi init
Lightvalve 170:42c938a40313 887 eeprom.format(8,3);
Lightvalve 170:42c938a40313 888 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 889 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 890 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 891 make_delay();
Lightvalve 21:e5f1a43ea6f9 892
Lightvalve 16:903b5a4433b4 893 //rom
Lightvalve 19:23b7c1ad8683 894 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 895 make_delay();
Lightvalve 13:747daba9cf59 896
GiJeongKim 0:51c43836c1d7 897 // ADC init
jobuuu 5:a4319f79457b 898 Init_ADC();
Lightvalve 11:82d8768d7351 899 make_delay();
jobuuu 2:a1c0a37df760 900
GiJeongKim 0:51c43836c1d7 901 // Pwm init
GiJeongKim 0:51c43836c1d7 902 Init_PWM();
GiJeongKim 0:51c43836c1d7 903 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 904 make_delay();
Lightvalve 13:747daba9cf59 905
Lightvalve 11:82d8768d7351 906 // TMR3 init
Lightvalve 11:82d8768d7351 907 Init_TMR3();
Lightvalve 11:82d8768d7351 908 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 909 make_delay();
Lightvalve 21:e5f1a43ea6f9 910
Lightvalve 50:3c630b5eba9f 911 // TMR2 init
Lightvalve 56:6f50d9d3bfee 912 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 913 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 914 // make_delay();
Lightvalve 21:e5f1a43ea6f9 915
GiJeongKim 0:51c43836c1d7 916 // CAN
jobuuu 2:a1c0a37df760 917 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 918 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 919 make_delay();
Lightvalve 34:bb2ca2fc2a8e 920
Lightvalve 23:59218d4a256d 921 //Timer priority
Lightvalve 23:59218d4a256d 922 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 57:f4819de54e7a 923 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 924 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 925
Lightvalve 23:59218d4a256d 926 //can.reset();
Lightvalve 19:23b7c1ad8683 927 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 928
GiJeongKim 0:51c43836c1d7 929 // spi _ enc
GiJeongKim 0:51c43836c1d7 930 spi_enc_set_init();
Lightvalve 11:82d8768d7351 931 make_delay();
Lightvalve 13:747daba9cf59 932
Lightvalve 11:82d8768d7351 933 //DAC init
Lightvalve 58:2eade98630e2 934 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 935 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 936 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 937 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 938 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 58:2eade98630e2 939 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 58:2eade98630e2 940 }
Lightvalve 11:82d8768d7351 941 make_delay();
Lightvalve 13:747daba9cf59 942
Lightvalve 19:23b7c1ad8683 943 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 944 if(i%2==0)
Lightvalve 38:118df027d851 945 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 946 else
Lightvalve 38:118df027d851 947 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 948 }
Lightvalve 169:645207e160ca 949
Lightvalve 173:68c7914679ec 950 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 951 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 952 hc1_temp[index1][index2] = (float) (rand()%100) * 0.007f ;
Lightvalve 170:42c938a40313 953 }
Lightvalve 179:d5377766d7ea 954 bc1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 955 }
Lightvalve 173:68c7914679ec 956 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 957 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 958 hc2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 959 }
Lightvalve 179:d5377766d7ea 960 bc2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 179:d5377766d7ea 961 hc3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 962 }
Lightvalve 179:d5377766d7ea 963 bc3_temp = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 964
Lightvalve 173:68c7914679ec 965 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 966 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 967 ha1_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 968 }
Lightvalve 179:d5377766d7ea 969 ba1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 970 }
Lightvalve 173:68c7914679ec 971 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 972 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 973 ha2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 974 }
Lightvalve 179:d5377766d7ea 975 ba2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 976 }
Lightvalve 170:42c938a40313 977 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 978 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 979 ha3_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 980 }
Lightvalve 179:d5377766d7ea 981 ba3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 982 }
Lightvalve 171:bfc1fd2629d8 983
Lightvalve 170:42c938a40313 984 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 985 Overwirte_Actor_Networks();
Lightvalve 61:bc8c8270f0ab 986
jobuuu 6:df07d3491e3a 987 /************************************
jobuuu 1:e04e563be5ce 988 *** Program is operating!
jobuuu 6:df07d3491e3a 989 *************************************/
GiJeongKim 0:51c43836c1d7 990 while(1) {
Lightvalve 169:645207e160ca 991
Lightvalve 171:bfc1fd2629d8 992 // if(timer_while==27491) {
Lightvalve 171:bfc1fd2629d8 993 // timer_while = 0;
Lightvalve 171:bfc1fd2629d8 994 // 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 995 // //pc.printf("%f\n", virt_pos);
Lightvalve 171:bfc1fd2629d8 996 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 171:bfc1fd2629d8 997 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 171:bfc1fd2629d8 998 // }
Lightvalve 169:645207e160ca 999
Lightvalve 171:bfc1fd2629d8 1000
Lightvalve 171:bfc1fd2629d8 1001 //i2c
Lightvalve 180:02be1711ee0b 1002 read_field(i2c_slave_addr1);
Lightvalve 180:02be1711ee0b 1003 if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 171:bfc1fd2629d8 1004
Lightvalve 177:8e9cf31d63f4 1005 //timer_while ++;
Lightvalve 169:645207e160ca 1006
Lightvalve 170:42c938a40313 1007 ///////////////////////////////////////////////////////Neural Network
Lightvalve 169:645207e160ca 1008
Lightvalve 73:f80dc3970c99 1009 if(NN_Control_Flag == 0) {
Lightvalve 73:f80dc3970c99 1010 LED = 0;
Lightvalve 73:f80dc3970c99 1011 }
Lightvalve 169:645207e160ca 1012
Lightvalve 73:f80dc3970c99 1013 else if(NN_Control_Flag == 1) {
Lightvalve 169:645207e160ca 1014
Lightvalve 162:9dd4f35e9de8 1015 int ind = 0;
Lightvalve 162:9dd4f35e9de8 1016 for(int i=0; i<numpast_u; i++) {
Lightvalve 162:9dd4f35e9de8 1017 input_NN[ind] = u_past[time_interval*i];
Lightvalve 162:9dd4f35e9de8 1018 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1019 }
Lightvalve 162:9dd4f35e9de8 1020
Lightvalve 162:9dd4f35e9de8 1021 for(int i=0; i<numpast_x; i++) {
Lightvalve 162:9dd4f35e9de8 1022 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 162:9dd4f35e9de8 1023 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1024 }
Lightvalve 162:9dd4f35e9de8 1025 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 162:9dd4f35e9de8 1026 ind = ind + 1;
Lightvalve 169:645207e160ca 1027
Lightvalve 162:9dd4f35e9de8 1028 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 162:9dd4f35e9de8 1029 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 122:dcb3ce3056a0 1030 // ind = ind + 1;
Lightvalve 122:dcb3ce3056a0 1031 // }
Lightvalve 162:9dd4f35e9de8 1032
Lightvalve 162:9dd4f35e9de8 1033 for(int i=0; i<numpast_f; i++) {
Lightvalve 193:4b5e617b8481 1034 input_NN[ind] = f_past[time_interval*i] / 10000.0f * 8.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1035 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1036 }
Lightvalve 193:4b5e617b8481 1037 input_NN[ind] = torq.sen / 10000.0f * 8.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1038 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1039 for(int i=0; i<numfuture_f; i++) {
Lightvalve 193:4b5e617b8481 1040 input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f * 8.0f + 0.5f;
Lightvalve 192:637092202815 1041 // input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f+0.5f;
Lightvalve 162:9dd4f35e9de8 1042 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1043 }
Lightvalve 169:645207e160ca 1044
Lightvalve 112:8dcb1600cb90 1045 float output1[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1046 float output2[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1047 float output3[16] = { 0.0f };
Lightvalve 66:a8e6799dbce3 1048 float output = 0.0f;
Lightvalve 65:a2d7c63419c2 1049
Lightvalve 112:8dcb1600cb90 1050 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 68:328e1be06f5d 1051 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 66:a8e6799dbce3 1052 output1[index2] = output1[index2]
Lightvalve 73:f80dc3970c99 1053 + h1[index1][index2] * input_NN[index1];
Lightvalve 66:a8e6799dbce3 1054 }
Lightvalve 66:a8e6799dbce3 1055 output1[index2] = output1[index2] + b1[index2];
Lightvalve 66:a8e6799dbce3 1056 if (output1[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1057 output1[index2] = 0;
Lightvalve 66:a8e6799dbce3 1058 }
Lightvalve 66:a8e6799dbce3 1059 }
Lightvalve 65:a2d7c63419c2 1060
Lightvalve 112:8dcb1600cb90 1061 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1062 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1063 output2[index2] = output2[index2]
Lightvalve 66:a8e6799dbce3 1064 + h2[index1][index2] * output1[index1];
Lightvalve 66:a8e6799dbce3 1065 }
Lightvalve 66:a8e6799dbce3 1066 output2[index2] = output2[index2] + b2[index2];
Lightvalve 66:a8e6799dbce3 1067 if (output2[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1068 output2[index2] = 0;
Lightvalve 66:a8e6799dbce3 1069 }
Lightvalve 66:a8e6799dbce3 1070 }
Lightvalve 65:a2d7c63419c2 1071
Lightvalve 112:8dcb1600cb90 1072 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1073 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1074 output3[index2] = output3[index2]
Lightvalve 66:a8e6799dbce3 1075 + h3[index1][index2] * output2[index1];
Lightvalve 66:a8e6799dbce3 1076 }
Lightvalve 66:a8e6799dbce3 1077 output3[index2] = output3[index2] + b3[index2];
Lightvalve 66:a8e6799dbce3 1078 if (output3[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1079 output3[index2] = 0;
Lightvalve 66:a8e6799dbce3 1080 }
Lightvalve 65:a2d7c63419c2 1081 }
Lightvalve 66:a8e6799dbce3 1082
Lightvalve 66:a8e6799dbce3 1083 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 112:8dcb1600cb90 1084 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 73:f80dc3970c99 1085 output = output + hout[index1] * output3[index1];
Lightvalve 66:a8e6799dbce3 1086 }
Lightvalve 66:a8e6799dbce3 1087 output = output + bout[index2];
Lightvalve 169:645207e160ca 1088
Lightvalve 66:a8e6799dbce3 1089 }
Lightvalve 73:f80dc3970c99 1090 output = 1.0f/(1.0f+exp(-output));
Lightvalve 101:50159049a518 1091 output_normalized = output;
Lightvalve 68:328e1be06f5d 1092 output = output * 20000.0f - 10000.0f;
Lightvalve 169:645207e160ca 1093
Lightvalve 66:a8e6799dbce3 1094 if(output>=0) {
Lightvalve 66:a8e6799dbce3 1095 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 66:a8e6799dbce3 1096 } else {
Lightvalve 66:a8e6799dbce3 1097 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 65:a2d7c63419c2 1098 }
Lightvalve 87:471334725012 1099
Lightvalve 169:645207e160ca 1100
Lightvalve 69:3995ffeaa786 1101 if(LED==1) {
Lightvalve 69:3995ffeaa786 1102 LED=0;
Lightvalve 69:3995ffeaa786 1103 } else
Lightvalve 69:3995ffeaa786 1104 LED = 1;
Lightvalve 169:645207e160ca 1105
Lightvalve 65:a2d7c63419c2 1106 }
Lightvalve 171:bfc1fd2629d8 1107
Lightvalve 171:bfc1fd2629d8 1108
Lightvalve 170:42c938a40313 1109 /////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 1110 switch (Update_Case) {
Lightvalve 170:42c938a40313 1111 case 0: {
Lightvalve 170:42c938a40313 1112 break;
Lightvalve 170:42c938a40313 1113 }
Lightvalve 170:42c938a40313 1114 case 1: {
Lightvalve 170:42c938a40313 1115 //Network Update(just update and hold network)
Lightvalve 170:42c938a40313 1116 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 170:42c938a40313 1117 float loss_sum = 0.0f;
Lightvalve 175:2f7289dbd488 1118 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 170:42c938a40313 1119 //Calculate Estimated V
Lightvalve 175:2f7289dbd488 1120 //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]};
Lightvalve 175:2f7289dbd488 1121 float temp_array[2] = {state_array[n][0], state_array[n][1]};
Lightvalve 175:2f7289dbd488 1122 V[n] = Critic_Network_Temp(temp_array);
Lightvalve 173:68c7914679ec 1123 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 175:2f7289dbd488 1124 hx_c_sum_array[n][i] = hx_c_sum[i];
Lightvalve 173:68c7914679ec 1125 }
Lightvalve 173:68c7914679ec 1126 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 175:2f7289dbd488 1127 hxh_c_sum_array[n][i] = hxh_c_sum[i];
Lightvalve 173:68c7914679ec 1128 }
Lightvalve 175:2f7289dbd488 1129 hxhh_c_sum_array[n] = hxhh_c_sum;
Lightvalve 177:8e9cf31d63f4 1130
Lightvalve 175:2f7289dbd488 1131 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 1132 Actor_Network_Old(temp_array);
Lightvalve 175:2f7289dbd488 1133 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 1134 r[n] = exp(-0.25f * 5.0f * state_array[n][1] * state_array[n][1]);
Lightvalve 175:2f7289dbd488 1135 if(n == batch_size-1) return_G[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1136 else return_G[n] = gamma * return_G[n+1] + r[n];
Lightvalve 175:2f7289dbd488 1137 if(n == batch_size-1) td_target[n] = r[n];
Lightvalve 175:2f7289dbd488 1138 else td_target[n] = r[n] + gamma * V[n+1];
Lightvalve 175:2f7289dbd488 1139 delta[n] = td_target[n] - V[n];
Lightvalve 175:2f7289dbd488 1140 if(n == batch_size-1) advantage[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1141 else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n];
Lightvalve 179:d5377766d7ea 1142 // return_G[n] = advantage[n] + V[n];
Lightvalve 175:2f7289dbd488 1143 ratio[n] = pi[n]/pi_old[n];
Lightvalve 179:d5377766d7ea 1144 }
Lightvalve 179:d5377766d7ea 1145 float mean_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1146 float dev_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1147 mean_advantage = mean_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1148 dev_advantage = deviation_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1149 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 179:d5377766d7ea 1150 //advantage[n] = (advantage[n]-mean_advantage)/dev_advantage;
Lightvalve 175:2f7289dbd488 1151 surr1[n] = ratio[n] * advantage[n];
Lightvalve 175:2f7289dbd488 1152 if (ratio[n] > 1.0f + epsilon) {
Lightvalve 175:2f7289dbd488 1153 surr2[n] = (1.0f + epsilon)*advantage[n];
Lightvalve 175:2f7289dbd488 1154 } else if( ratio[n] < 1.0f - epsilon) {
Lightvalve 175:2f7289dbd488 1155 surr2[n] = (1.0f - epsilon)*advantage[n];
Lightvalve 170:42c938a40313 1156 } else {
Lightvalve 175:2f7289dbd488 1157 surr2[n] = ratio[n]*advantage[n];
Lightvalve 170:42c938a40313 1158 }
Lightvalve 175:2f7289dbd488 1159 loss[n] = -min(surr1[n], surr2[n]);
Lightvalve 175:2f7289dbd488 1160 loss_sum = loss_sum + loss[n];
Lightvalve 170:42c938a40313 1161 }
Lightvalve 170:42c938a40313 1162 reward_sum = 0.0f;
Lightvalve 170:42c938a40313 1163 for (int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 1164 reward_sum = reward_sum + r[i];
Lightvalve 170:42c938a40313 1165 }
Lightvalve 170:42c938a40313 1166 logging5 = reward_sum;
Lightvalve 171:bfc1fd2629d8 1167
Lightvalve 171:bfc1fd2629d8 1168
Lightvalve 170:42c938a40313 1169 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 170:42c938a40313 1170 loss_batch = loss_sum;
Lightvalve 170:42c938a40313 1171 //Update Networks
Lightvalve 170:42c938a40313 1172 update_Critic_Networks(state_array);
Lightvalve 170:42c938a40313 1173 update_Actor_Networks(state_array);
Lightvalve 170:42c938a40313 1174 }
Lightvalve 170:42c938a40313 1175 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1176 Update_Case = 0;
Lightvalve 170:42c938a40313 1177 //logging1 = V[0];
Lightvalve 171:bfc1fd2629d8 1178
Lightvalve 170:42c938a40313 1179 break;
Lightvalve 170:42c938a40313 1180 }
Lightvalve 170:42c938a40313 1181 case 2: {
Lightvalve 170:42c938a40313 1182 //Network apply to next Network
Lightvalve 170:42c938a40313 1183 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1184 Overwirte_Actor_Networks();
Lightvalve 170:42c938a40313 1185 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1186 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1187 Update_Case = 0;
Lightvalve 170:42c938a40313 1188 break;
Lightvalve 170:42c938a40313 1189 }
Lightvalve 169:645207e160ca 1190
Lightvalve 170:42c938a40313 1191 }
GiJeongKim 0:51c43836c1d7 1192 }
jobuuu 1:e04e563be5ce 1193 }
jobuuu 1:e04e563be5ce 1194
Lightvalve 33:91b17819ec30 1195 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1196 {
Lightvalve 14:8e7590227d22 1197
Lightvalve 13:747daba9cf59 1198 int i = 0;
Lightvalve 48:889798ff9329 1199 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1200 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1201 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 1202 if(i==0) {
Lightvalve 50:3c630b5eba9f 1203 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 1204 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1205 } else {
Lightvalve 57:f4819de54e7a 1206 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 1207 }
Lightvalve 14:8e7590227d22 1208 } else {
Lightvalve 50:3c630b5eba9f 1209 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 1210 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1211 } else {
Lightvalve 57:f4819de54e7a 1212 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 1213 }
Lightvalve 13:747daba9cf59 1214 }
Lightvalve 13:747daba9cf59 1215 break;
Lightvalve 13:747daba9cf59 1216 }
Lightvalve 13:747daba9cf59 1217 }
Lightvalve 14:8e7590227d22 1218 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1219 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1220 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1221 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1222 }
Lightvalve 36:a46e63505ed8 1223
Lightvalve 57:f4819de54e7a 1224 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1225 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 1226
Lightvalve 13:747daba9cf59 1227 }
jobuuu 6:df07d3491e3a 1228
jobuuu 6:df07d3491e3a 1229
Lightvalve 30:8d561f16383b 1230 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1231 {
Lightvalve 13:747daba9cf59 1232 int i = 0;
Lightvalve 13:747daba9cf59 1233
Lightvalve 38:118df027d851 1234 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1235 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1236 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1237 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1238 }
Lightvalve 38:118df027d851 1239
Lightvalve 89:a7b45368ea0f 1240 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1241 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1242 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1243 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 1244 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 1245 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1246
Lightvalve 13:747daba9cf59 1247 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 1248
Lightvalve 18:b8adf1582ea3 1249 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 1250 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 1251 if(i==0) {
Lightvalve 48:889798ff9329 1252 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 1253 } else {
Lightvalve 48:889798ff9329 1254 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 1255 }
Lightvalve 13:747daba9cf59 1256 break;
Lightvalve 13:747daba9cf59 1257 }
Lightvalve 13:747daba9cf59 1258 }
Lightvalve 59:f308b1656d9c 1259 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1260 }
Lightvalve 13:747daba9cf59 1261
Lightvalve 14:8e7590227d22 1262 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1263 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 1264 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1265 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1266 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1267 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1268 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1269 }; // duty
Lightvalve 67:c2812cf26c38 1270 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 1271 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 1272 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 1273 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 1274 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 1275 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1276 }; // mV
Lightvalve 13:747daba9cf59 1277
Lightvalve 30:8d561f16383b 1278 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1279 {
Lightvalve 30:8d561f16383b 1280 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1281 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1282 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1283 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1284 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1285 } else {
Lightvalve 13:747daba9cf59 1286 int idx = 0;
Lightvalve 13:747daba9cf59 1287 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1288 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1289 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1290 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1291 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1292 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1293 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1294 break;
Lightvalve 13:747daba9cf59 1295 }
Lightvalve 13:747daba9cf59 1296 }
Lightvalve 13:747daba9cf59 1297 }
Lightvalve 14:8e7590227d22 1298
Lightvalve 13:747daba9cf59 1299 return PWM_duty;
Lightvalve 13:747daba9cf59 1300 }
jobuuu 6:df07d3491e3a 1301
Lightvalve 57:f4819de54e7a 1302
Lightvalve 57:f4819de54e7a 1303
Lightvalve 57:f4819de54e7a 1304
Lightvalve 57:f4819de54e7a 1305
jobuuu 2:a1c0a37df760 1306 /*******************************************************************************
jobuuu 2:a1c0a37df760 1307 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1308 *******************************************************************************/
jobuuu 2:a1c0a37df760 1309
Lightvalve 51:b46bed7fec80 1310 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 1311 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 1312 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 1313 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1314 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1315 {
Lightvalve 19:23b7c1ad8683 1316 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1317
Lightvalve 21:e5f1a43ea6f9 1318 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1319 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1320 ********************************************************/
Lightvalve 13:747daba9cf59 1321
Lightvalve 57:f4819de54e7a 1322 //Encoder
Lightvalve 57:f4819de54e7a 1323 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 1324 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 1325 }
Lightvalve 61:bc8c8270f0ab 1326
Lightvalve 61:bc8c8270f0ab 1327 ADC1->CR2 |= 0x40000000;
Lightvalve 58:2eade98630e2 1328 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1329 // Torque Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1330 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 58:2eade98630e2 1331 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 1332 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 1333 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 1334
Lightvalve 67:c2812cf26c38 1335
Lightvalve 67:c2812cf26c38 1336 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 67:c2812cf26c38 1337 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 67:c2812cf26c38 1338 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 67:c2812cf26c38 1339 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 67:c2812cf26c38 1340 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 67:c2812cf26c38 1341
Lightvalve 17:1865016ca2e7 1342
Lightvalve 58:2eade98630e2 1343 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1344 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1345 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 58:2eade98630e2 1346 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 58:2eade98630e2 1347 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 58:2eade98630e2 1348 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 1349 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 1350 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 1351 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 1352
Lightvalve 58:2eade98630e2 1353 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 1354 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 1355 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 1356 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 1357 }
Lightvalve 58:2eade98630e2 1358 }
Lightvalve 61:bc8c8270f0ab 1359
Lightvalve 58:2eade98630e2 1360 // //Pressure sensor A
Lightvalve 58:2eade98630e2 1361 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 58:2eade98630e2 1362 // //while((ADC1->SR & 0b10));
Lightvalve 58:2eade98630e2 1363 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1364 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 58:2eade98630e2 1365 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 58:2eade98630e2 1366 // 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 1367 //
Lightvalve 58:2eade98630e2 1368 //
Lightvalve 58:2eade98630e2 1369 // //Pressure sensor B
Lightvalve 58:2eade98630e2 1370 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1371 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 58:2eade98630e2 1372 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 58:2eade98630e2 1373 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1374
Lightvalve 17:1865016ca2e7 1375
Lightvalve 21:e5f1a43ea6f9 1376 //Current
Lightvalve 21:e5f1a43ea6f9 1377 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1378 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1379 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 1380 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1381 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1382 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1383 //cur.sen = raw_cur;
Lightvalve 58:2eade98630e2 1384
Lightvalve 57:f4819de54e7a 1385 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1386 }
Lightvalve 11:82d8768d7351 1387 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1388 }
Lightvalve 19:23b7c1ad8683 1389
Lightvalve 19:23b7c1ad8683 1390
Lightvalve 18:b8adf1582ea3 1391 int j =0;
Lightvalve 54:647072f5307a 1392 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 1393 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 1394 int cnt_trans = 0;
Lightvalve 48:889798ff9329 1395 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 1396 int can_rest =0;
Lightvalve 48:889798ff9329 1397
Lightvalve 11:82d8768d7351 1398 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1399 {
Lightvalve 19:23b7c1ad8683 1400 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 1401
Lightvalve 57:f4819de54e7a 1402 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 1403 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1404 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 1405 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1406 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 1407 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 1408 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1409 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 1410 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1411 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 1412 }
Lightvalve 50:3c630b5eba9f 1413
Lightvalve 50:3c630b5eba9f 1414 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 1415 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1416 cnt_trans++;
Lightvalve 46:2694daea349b 1417 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1418 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 1419 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 1420 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 1421 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1422 cnt_trans++;
Lightvalve 46:2694daea349b 1423 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1424 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 1425 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 1426 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 1427 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 1428 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 1429 } else {
Lightvalve 58:2eade98630e2 1430 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 1431 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 1432 }
Lightvalve 45:35fa6884d0c6 1433
Lightvalve 50:3c630b5eba9f 1434
Lightvalve 57:f4819de54e7a 1435 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 1436 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 1437
Lightvalve 57:f4819de54e7a 1438 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 1439 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1440 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1441 } else {
Lightvalve 57:f4819de54e7a 1442 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1443 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1444 }
Lightvalve 56:6f50d9d3bfee 1445
Lightvalve 56:6f50d9d3bfee 1446
Lightvalve 56:6f50d9d3bfee 1447
Lightvalve 57:f4819de54e7a 1448 // UTILITY MODE ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1449
Lightvalve 57:f4819de54e7a 1450 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 1451 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1452 break;
Lightvalve 13:747daba9cf59 1453 }
Lightvalve 14:8e7590227d22 1454
Lightvalve 14:8e7590227d22 1455 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1456 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1457 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1458 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1459
Lightvalve 14:8e7590227d22 1460 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1461 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1462 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1463
Lightvalve 84:c355d3e52bf1 1464 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1465
Lightvalve 30:8d561f16383b 1466 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 1467 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 50:3c630b5eba9f 1468
Lightvalve 16:903b5a4433b4 1469 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1470 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1471 }
Lightvalve 13:747daba9cf59 1472 } else {
Lightvalve 58:2eade98630e2 1473 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1474 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1475 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1476 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1477
Lightvalve 170:42c938a40313 1478 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1479 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1480
Lightvalve 30:8d561f16383b 1481 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1482
Lightvalve 13:747daba9cf59 1483 }
Lightvalve 14:8e7590227d22 1484 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1485 break;
Lightvalve 19:23b7c1ad8683 1486 }
Lightvalve 14:8e7590227d22 1487
Lightvalve 50:3c630b5eba9f 1488 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 1489 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 1490 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1491 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 1492 // }
Lightvalve 50:3c630b5eba9f 1493 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1494 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1495 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1496 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1497 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1498 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1499 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1500 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1501 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1502 // }
Lightvalve 50:3c630b5eba9f 1503 //
Lightvalve 50:3c630b5eba9f 1504 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1505 // 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 1506 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 1507 //
Lightvalve 50:3c630b5eba9f 1508 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 1509 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1510 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1511 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1512 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1513 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1514 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 1515 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1516 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1517 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1518 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1519 //
Lightvalve 50:3c630b5eba9f 1520 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 1521 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 1522 // // | / | / |/
Lightvalve 50:3c630b5eba9f 1523 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 1524 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 1525 // // /| / | / |
Lightvalve 50:3c630b5eba9f 1526 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 1527 //
Lightvalve 50:3c630b5eba9f 1528 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 1529 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 1530 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1531 // } else {
Lightvalve 50:3c630b5eba9f 1532 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 1533 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1534 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1535 // }
Lightvalve 50:3c630b5eba9f 1536 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 1537 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1538 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1539 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 1540 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1541 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1542 // }
Lightvalve 50:3c630b5eba9f 1543 // } else {
Lightvalve 50:3c630b5eba9f 1544 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 1545 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 1546 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 1547 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1548 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1549 // }
Lightvalve 50:3c630b5eba9f 1550 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1551 //
Lightvalve 50:3c630b5eba9f 1552 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1553 //
Lightvalve 50:3c630b5eba9f 1554 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 1555 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 1556 //
Lightvalve 50:3c630b5eba9f 1557 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1558 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1559 // }
Lightvalve 50:3c630b5eba9f 1560 // }
Lightvalve 50:3c630b5eba9f 1561 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 1562 // break;
Lightvalve 50:3c630b5eba9f 1563 // }
Lightvalve 14:8e7590227d22 1564
Lightvalve 14:8e7590227d22 1565 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1566 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1567 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1568 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1569 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 59:f308b1656d9c 1570 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 1571 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1572 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1573 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 1574 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1575 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1576 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1577 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1578 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1579 }
Lightvalve 29:69f3f5445d6d 1580 cnt_findhome++;
Lightvalve 14:8e7590227d22 1581
Lightvalve 29:69f3f5445d6d 1582 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1583 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1584 } else {
Lightvalve 29:69f3f5445d6d 1585 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1586 }
Lightvalve 19:23b7c1ad8683 1587
Lightvalve 57:f4819de54e7a 1588 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1589 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 67:c2812cf26c38 1590 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 67:c2812cf26c38 1591 else pos.ref = pos.ref - 12.0f;
Lightvalve 61:bc8c8270f0ab 1592
Lightvalve 59:f308b1656d9c 1593 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 59:f308b1656d9c 1594 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1595 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 59:f308b1656d9c 1596 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1597 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 1598
Lightvalve 59:f308b1656d9c 1599 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 1600 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1601
Lightvalve 34:bb2ca2fc2a8e 1602
Lightvalve 29:69f3f5445d6d 1603 } else {
Lightvalve 29:69f3f5445d6d 1604 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 67:c2812cf26c38 1605 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1606 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1607 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1608 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1609 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1610 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1611 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1612 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1613 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1614 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 1615
Lightvalve 67:c2812cf26c38 1616
Lightvalve 67:c2812cf26c38 1617 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 1618 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1619 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1620 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1621 vel.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1622 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 67:c2812cf26c38 1623 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 169:645207e160ca 1624
Lightvalve 169:645207e160ca 1625
Lightvalve 29:69f3f5445d6d 1626 }
Lightvalve 29:69f3f5445d6d 1627 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1628 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 1629 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 1630 //pos.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1631 vel.ref = 0.0f;
Lightvalve 169:645207e160ca 1632
Lightvalve 29:69f3f5445d6d 1633 // input for position control
Lightvalve 169:645207e160ca 1634
Lightvalve 67:c2812cf26c38 1635 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 1636 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1637
Lightvalve 67:c2812cf26c38 1638 double torq_ref = 0.0f;
Lightvalve 67:c2812cf26c38 1639 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 1640 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 1641 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 67:c2812cf26c38 1642
Lightvalve 67:c2812cf26c38 1643 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 67:c2812cf26c38 1644
Lightvalve 67:c2812cf26c38 1645 double I_REF_POS = 0.0f;
Lightvalve 67:c2812cf26c38 1646 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 67:c2812cf26c38 1647 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 67:c2812cf26c38 1648
Lightvalve 67:c2812cf26c38 1649 double temp_vel_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1650 double temp_vel_torq = 0.0f;
Lightvalve 67:c2812cf26c38 1651 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 67:c2812cf26c38 1652
Lightvalve 69:3995ffeaa786 1653 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 67:c2812cf26c38 1654 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 1655 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 1656 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 67:c2812cf26c38 1657 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 1658 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:c2812cf26c38 1659 }
Lightvalve 67:c2812cf26c38 1660 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 1661 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1662
Lightvalve 67:c2812cf26c38 1663 I_REF = I_REF_POS;
Lightvalve 67:c2812cf26c38 1664
Lightvalve 67:c2812cf26c38 1665
Lightvalve 67:c2812cf26c38 1666
Lightvalve 67:c2812cf26c38 1667 } else {
Lightvalve 67:c2812cf26c38 1668 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 67:c2812cf26c38 1669 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 1670
Lightvalve 67:c2812cf26c38 1671 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 67:c2812cf26c38 1672 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 67:c2812cf26c38 1673 } else {
Lightvalve 67:c2812cf26c38 1674 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 67:c2812cf26c38 1675 }
Lightvalve 67:c2812cf26c38 1676
Lightvalve 67:c2812cf26c38 1677 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 67:c2812cf26c38 1678
Lightvalve 67:c2812cf26c38 1679 V_out = (float) Vout.ref;
Lightvalve 67:c2812cf26c38 1680
Lightvalve 67:c2812cf26c38 1681 }
Lightvalve 67:c2812cf26c38 1682
Lightvalve 67:c2812cf26c38 1683
Lightvalve 169:645207e160ca 1684
Lightvalve 169:645207e160ca 1685
Lightvalve 59:f308b1656d9c 1686 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 59:f308b1656d9c 1687 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1688 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 1689 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1690 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1691
Lightvalve 29:69f3f5445d6d 1692 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1693 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1694 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1695 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1696 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1697 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1698 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1699 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1700 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 1701 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1702 }
Lightvalve 13:747daba9cf59 1703 }
Lightvalve 19:23b7c1ad8683 1704
Lightvalve 13:747daba9cf59 1705 break;
Lightvalve 13:747daba9cf59 1706 }
Lightvalve 14:8e7590227d22 1707
Lightvalve 50:3c630b5eba9f 1708 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1709 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1710 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1711 // else {
Lightvalve 50:3c630b5eba9f 1712 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1713 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1714 // }
Lightvalve 50:3c630b5eba9f 1715 // }
Lightvalve 50:3c630b5eba9f 1716 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1717 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1718 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1719 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1720 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1721 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1722 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1723 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1724 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1725 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1726 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1727 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1728 // }
Lightvalve 50:3c630b5eba9f 1729 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1730 // }
Lightvalve 50:3c630b5eba9f 1731 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1732 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1733 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1734 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1735 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1736 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1737 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1738 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1739 // 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 1740 // // 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 1741 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1742 // }
Lightvalve 50:3c630b5eba9f 1743 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1744 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1745 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1746 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1747 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1748 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1749 // 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 1750 // // 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 1751 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1752 // }
Lightvalve 50:3c630b5eba9f 1753 // }
Lightvalve 50:3c630b5eba9f 1754 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1755 //
Lightvalve 50:3c630b5eba9f 1756 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1757 //
Lightvalve 50:3c630b5eba9f 1758 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1759 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1760 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1761 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1762 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1763 // }
Lightvalve 50:3c630b5eba9f 1764 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1765 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1766 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1767 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1768 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1769 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1770 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1771 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1772 // }
Lightvalve 50:3c630b5eba9f 1773 //
Lightvalve 50:3c630b5eba9f 1774 // }
Lightvalve 50:3c630b5eba9f 1775 // break;
Lightvalve 50:3c630b5eba9f 1776 // }
Lightvalve 50:3c630b5eba9f 1777 //
Lightvalve 50:3c630b5eba9f 1778 // }
Lightvalve 14:8e7590227d22 1779 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1780 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1781 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1782 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1783 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1784
Lightvalve 14:8e7590227d22 1785 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1786 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1787 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1788 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1789 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1790
Lightvalve 38:118df027d851 1791 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 1792 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 1793 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1794
Lightvalve 30:8d561f16383b 1795 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1796 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1797 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1798 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1799
Lightvalve 30:8d561f16383b 1800 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1801 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1802 }
Lightvalve 13:747daba9cf59 1803 } else {
Lightvalve 57:f4819de54e7a 1804 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1805 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1806 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1807 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1808 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1809 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1810
Lightvalve 170:42c938a40313 1811 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1812 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 1813 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1814
Lightvalve 30:8d561f16383b 1815 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1816 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1817 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1818 }
Lightvalve 14:8e7590227d22 1819 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1820 break;
Lightvalve 13:747daba9cf59 1821 }
Lightvalve 14:8e7590227d22 1822
Lightvalve 50:3c630b5eba9f 1823 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1824 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1825 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1826 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1827 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1828 // }
Lightvalve 50:3c630b5eba9f 1829 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1830 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1831 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1832 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1833 // }
Lightvalve 50:3c630b5eba9f 1834 // } else {
Lightvalve 50:3c630b5eba9f 1835 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1836 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1837 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1838 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1839 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1840 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1841 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1842 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1843 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1844 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1845 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1846 //
Lightvalve 50:3c630b5eba9f 1847 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1848 //
Lightvalve 50:3c630b5eba9f 1849 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1850 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1851 // }
Lightvalve 50:3c630b5eba9f 1852 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1853 // break;
Lightvalve 50:3c630b5eba9f 1854 // }
Lightvalve 19:23b7c1ad8683 1855
Lightvalve 50:3c630b5eba9f 1856 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1857 // 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 1858 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1859 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1860 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1861 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1862 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1863 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 1864 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 1865 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1866 // }
Lightvalve 50:3c630b5eba9f 1867 // break;
Lightvalve 50:3c630b5eba9f 1868 // }
Lightvalve 14:8e7590227d22 1869
Lightvalve 14:8e7590227d22 1870 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 59:f308b1656d9c 1871 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1872 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1873
Lightvalve 14:8e7590227d22 1874 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 60:64181f1d3e60 1875 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1876 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 59:f308b1656d9c 1877 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1878 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1879 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1880 data_num = 0;
Lightvalve 14:8e7590227d22 1881 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1882 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1883 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1884 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 59:f308b1656d9c 1885 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1886 } else {
Lightvalve 13:747daba9cf59 1887 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1888 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1889 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1890 }
Lightvalve 14:8e7590227d22 1891
Lightvalve 17:1865016ca2e7 1892 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1893 int i;
Lightvalve 13:747daba9cf59 1894 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1895 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1896 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1897 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1898 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1899 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1900 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1901 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1902 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1903 }
Lightvalve 13:747daba9cf59 1904 }
Lightvalve 170:42c938a40313 1905 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1906 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 1907 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 170:42c938a40313 1908 for(int i=0; i<25; i++) {
Lightvalve 170:42c938a40313 1909 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 170:42c938a40313 1910 }
Lightvalve 13:747daba9cf59 1911 ID_index = 0;
Lightvalve 57:f4819de54e7a 1912 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1913 }
Lightvalve 14:8e7590227d22 1914
Lightvalve 14:8e7590227d22 1915
Lightvalve 13:747daba9cf59 1916 break;
Lightvalve 13:747daba9cf59 1917 }
Lightvalve 14:8e7590227d22 1918
Lightvalve 14:8e7590227d22 1919 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 59:f308b1656d9c 1920 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1921 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 1922 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1923 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1924 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1925 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1926 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1927 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 1928 } else if(VALVE_DZ_timer < (int) (2.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) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1931 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1932 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 1933 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1934 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 1935 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1936 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 1937 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1938 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 1939 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1940 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 1941 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 1942 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 1943 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1944 data_num = 0;
Lightvalve 14:8e7590227d22 1945
Lightvalve 30:8d561f16383b 1946 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1947 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1948 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1949
Lightvalve 30:8d561f16383b 1950 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1951 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1952 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1953
Lightvalve 30:8d561f16383b 1954 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1955 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1956 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1957 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1958
Lightvalve 60:64181f1d3e60 1959 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 1960 DZ_case = 1;
Lightvalve 60:64181f1d3e60 1961 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 1962 DZ_case = -1;
Lightvalve 14:8e7590227d22 1963 } else {
Lightvalve 13:747daba9cf59 1964 DZ_case = 0;
Lightvalve 13:747daba9cf59 1965 }
Lightvalve 61:bc8c8270f0ab 1966
Lightvalve 60:64181f1d3e60 1967 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 1968
Lightvalve 13:747daba9cf59 1969 first_check = 1;
Lightvalve 13:747daba9cf59 1970 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1971 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1972 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 1973 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 1974 DZ_index = 1;
Lightvalve 14:8e7590227d22 1975
Lightvalve 13:747daba9cf59 1976 }
Lightvalve 19:23b7c1ad8683 1977 } else {
Lightvalve 14:8e7590227d22 1978 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 1979 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 1980 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 1981 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 1982 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 1983 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1984 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1985 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1986 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1987 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1988 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1989 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1990 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1991 }
Lightvalve 14:8e7590227d22 1992 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1993
Lightvalve 30:8d561f16383b 1994 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1995 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1996 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1997
Lightvalve 14:8e7590227d22 1998 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 1999 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2000 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2001 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2002 } else {
Lightvalve 13:747daba9cf59 2003 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 2004 }
Lightvalve 14:8e7590227d22 2005
Lightvalve 13:747daba9cf59 2006 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2007 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2008 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2009 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2010 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2011 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2012 DZ_index = 1;
Lightvalve 13:747daba9cf59 2013 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2014 }
Lightvalve 13:747daba9cf59 2015 }
Lightvalve 14:8e7590227d22 2016 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 2017 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2018 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 2019 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2020 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2021 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2022 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2023 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2024 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2025 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2026 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2027 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2028 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2029 }
Lightvalve 14:8e7590227d22 2030 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2031
Lightvalve 30:8d561f16383b 2032 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2033 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2034 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2035 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2036 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2037
Lightvalve 14:8e7590227d22 2038 if((FINAL_POS - START_POS)>100) {
Lightvalve 60:64181f1d3e60 2039 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2040 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2041 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2042 } else {
Lightvalve 60:64181f1d3e60 2043 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2044 }
Lightvalve 14:8e7590227d22 2045
Lightvalve 13:747daba9cf59 2046 VALVE_DZ_timer = 0;
Lightvalve 60:64181f1d3e60 2047 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2048 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2049 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2050 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2051 first_check = 0;
Lightvalve 33:91b17819ec30 2052 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2053 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2054
Lightvalve 170:42c938a40313 2055 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2056 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2057 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2058 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2059
Lightvalve 60:64181f1d3e60 2060 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2061 DZ_index = 1;
Lightvalve 13:747daba9cf59 2062 }
Lightvalve 13:747daba9cf59 2063 }
Lightvalve 14:8e7590227d22 2064 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2065 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2066 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 2067 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2068 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2069 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2070 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2071 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2072 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2073 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2074 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2075 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2076 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2077 }
Lightvalve 14:8e7590227d22 2078 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2079
Lightvalve 30:8d561f16383b 2080 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2081 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2082 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2083
Lightvalve 14:8e7590227d22 2084 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2085 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2086 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2087 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2088 } else {
Lightvalve 13:747daba9cf59 2089 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2090 }
Lightvalve 13:747daba9cf59 2091 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2092 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2093 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2094 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2095 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2096 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2097 DZ_index = 1;
Lightvalve 13:747daba9cf59 2098 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2099 }
Lightvalve 13:747daba9cf59 2100 }
Lightvalve 14:8e7590227d22 2101 } else {
Lightvalve 30:8d561f16383b 2102 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2103 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 2104 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2105 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2106 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2107 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2108 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2109 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2110 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2111 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2112 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2113 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2114 }
Lightvalve 14:8e7590227d22 2115 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2116
Lightvalve 30:8d561f16383b 2117 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2118 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2119 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2120 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2121 FINAL_POS = pos.sen;
Lightvalve 61:bc8c8270f0ab 2122
Lightvalve 60:64181f1d3e60 2123 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2124 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2125 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2126 DZ_DIRECTION = -1;
Lightvalve 60:64181f1d3e60 2127 } else {
Lightvalve 13:747daba9cf59 2128 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2129 }
Lightvalve 14:8e7590227d22 2130
Lightvalve 13:747daba9cf59 2131 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2132 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2133 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2134 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2135 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2136 first_check = 0;
Lightvalve 33:91b17819ec30 2137 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2138 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 61:bc8c8270f0ab 2139
Lightvalve 170:42c938a40313 2140 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2141 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2142 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2143 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2144
Lightvalve 57:f4819de54e7a 2145 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2146 DZ_index = 1;
Lightvalve 13:747daba9cf59 2147 }
Lightvalve 13:747daba9cf59 2148 }
Lightvalve 13:747daba9cf59 2149 }
Lightvalve 14:8e7590227d22 2150 }
Lightvalve 13:747daba9cf59 2151 break;
Lightvalve 13:747daba9cf59 2152 }
Lightvalve 14:8e7590227d22 2153
Lightvalve 14:8e7590227d22 2154 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 59:f308b1656d9c 2155 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2156 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2157 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2158 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2159 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2160 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2161 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2162 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2163 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2164 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2165 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2166 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2167 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2168 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 59:f308b1656d9c 2169 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2170 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2171 first_check = 1;
Lightvalve 13:747daba9cf59 2172 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2173 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2174 ID_index = 0;
Lightvalve 13:747daba9cf59 2175 max_check = 0;
Lightvalve 13:747daba9cf59 2176 min_check = 0;
Lightvalve 13:747daba9cf59 2177 }
Lightvalve 14:8e7590227d22 2178 } else {
Lightvalve 30:8d561f16383b 2179 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2180 //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 2181 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 59:f308b1656d9c 2182 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2183 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2184 data_num = 0;
Lightvalve 57:f4819de54e7a 2185 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2186
Lightvalve 14:8e7590227d22 2187 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2188 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2189 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f4819de54e7a 2190 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2191 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2192 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2193 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2194 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2195 one_period_end = 1;
Lightvalve 13:747daba9cf59 2196 }
Lightvalve 30:8d561f16383b 2197 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2198 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2199 one_period_end = 1;
Lightvalve 59:f308b1656d9c 2200 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2201 }
Lightvalve 14:8e7590227d22 2202
Lightvalve 14:8e7590227d22 2203 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2204 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2205 max_check = 1;
Lightvalve 14:8e7590227d22 2206 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2207 min_check = 1;
Lightvalve 13:747daba9cf59 2208 }
Lightvalve 13:747daba9cf59 2209 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2210
Lightvalve 13:747daba9cf59 2211 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2212 one_period_end = 0;
Lightvalve 13:747daba9cf59 2213 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2214 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2215 }
Lightvalve 14:8e7590227d22 2216
Lightvalve 14:8e7590227d22 2217 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2218
Lightvalve 13:747daba9cf59 2219 VALVE_POS_NUM = ID_index;
Lightvalve 170:42c938a40313 2220 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2221 for(int i=0; i<100; i++) {
Lightvalve 170:42c938a40313 2222 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 170:42c938a40313 2223 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 170:42c938a40313 2224 }
Lightvalve 13:747daba9cf59 2225 ID_index = 0;
Lightvalve 13:747daba9cf59 2226 first_check = 0;
Lightvalve 13:747daba9cf59 2227 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2228 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 2229 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2230 }
Lightvalve 13:747daba9cf59 2231 }
Lightvalve 13:747daba9cf59 2232 break;
Lightvalve 13:747daba9cf59 2233 }
Lightvalve 58:2eade98630e2 2234
Lightvalve 57:f4819de54e7a 2235 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 2236 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 2237 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 2238 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 2239 cnt_sysid++;
Lightvalve 57:f4819de54e7a 2240 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 2241 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 2242 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 2243 }
Lightvalve 57:f4819de54e7a 2244 break;
Lightvalve 57:f4819de54e7a 2245 }
Lightvalve 58:2eade98630e2 2246
Lightvalve 169:645207e160ca 2247 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 2248 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 2249 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2250 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2251 } else {
Lightvalve 169:645207e160ca 2252 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2253 }
Lightvalve 169:645207e160ca 2254 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2255 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2256 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2257 } else {
Lightvalve 169:645207e160ca 2258 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2259 }
Lightvalve 58:2eade98630e2 2260
Lightvalve 169:645207e160ca 2261 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2262 cnt_freq_test++;
Lightvalve 169:645207e160ca 2263 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 2264 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 2265 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 2266 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2267 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 2268 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 2269 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2270 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2271 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2272 }
Lightvalve 169:645207e160ca 2273 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2274 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 2275
Lightvalve 169:645207e160ca 2276 }
Lightvalve 169:645207e160ca 2277 break;
Lightvalve 169:645207e160ca 2278 }
Lightvalve 169:645207e160ca 2279 case MODE_SEND_BUFFER: {
Lightvalve 169:645207e160ca 2280 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 169:645207e160ca 2281 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 169:645207e160ca 2282 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 169:645207e160ca 2283 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 169:645207e160ca 2284 // }
Lightvalve 169:645207e160ca 2285 // cnt_send_buffer++;
Lightvalve 169:645207e160ca 2286 // }
Lightvalve 169:645207e160ca 2287
Lightvalve 169:645207e160ca 2288 break;
Lightvalve 169:645207e160ca 2289 }
Lightvalve 169:645207e160ca 2290 case MODE_SEND_OVER: {
Lightvalve 169:645207e160ca 2291 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 169:645207e160ca 2292 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2293 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2294 break;
Lightvalve 169:645207e160ca 2295 }
Lightvalve 170:42c938a40313 2296
Lightvalve 169:645207e160ca 2297 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 2298 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2299 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2300 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2301 } else {
Lightvalve 169:645207e160ca 2302 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 2303 }
Lightvalve 169:645207e160ca 2304 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2305 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2306 } else {
Lightvalve 169:645207e160ca 2307 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2308 }
Lightvalve 169:645207e160ca 2309 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2310 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2311 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2312 } else {
Lightvalve 169:645207e160ca 2313 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2314 }
Lightvalve 169:645207e160ca 2315
Lightvalve 169:645207e160ca 2316 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2317 cnt_step_test++;
Lightvalve 170:42c938a40313 2318 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2319 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 2320 cnt_step_test = 0;
Lightvalve 169:645207e160ca 2321 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2322 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 2323 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2324 }
Lightvalve 169:645207e160ca 2325 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 169:645207e160ca 2326 // {
Lightvalve 169:645207e160ca 2327 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2328 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2329 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2330 // }
Lightvalve 170:42c938a40313 2331
Lightvalve 169:645207e160ca 2332 break;
Lightvalve 169:645207e160ca 2333 }
Lightvalve 57:f4819de54e7a 2334
Lightvalve 57:f4819de54e7a 2335 default:
Lightvalve 57:f4819de54e7a 2336 break;
Lightvalve 57:f4819de54e7a 2337 }
Lightvalve 57:f4819de54e7a 2338
Lightvalve 57:f4819de54e7a 2339 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 2340
Lightvalve 57:f4819de54e7a 2341 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 2342 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 2343 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 2344 break;
Lightvalve 57:f4819de54e7a 2345 }
Lightvalve 57:f4819de54e7a 2346
Lightvalve 57:f4819de54e7a 2347 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 2348 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 2349 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 2350 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 2351 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 2352 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 2353 } else {
Lightvalve 67:c2812cf26c38 2354 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 2355 }
Lightvalve 57:f4819de54e7a 2356 break;
Lightvalve 57:f4819de54e7a 2357 }
Lightvalve 57:f4819de54e7a 2358
Lightvalve 57:f4819de54e7a 2359 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 2360
Lightvalve 57:f4819de54e7a 2361 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 2362 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 2363 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 2364 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2365
Lightvalve 67:c2812cf26c38 2366 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 2367 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 2368 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 2369 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 2370
Lightvalve 139:15621998925b 2371 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 139:15621998925b 2372 torq_ref = torq.ref;
Lightvalve 57:f4819de54e7a 2373
Lightvalve 57:f4819de54e7a 2374 // torque feedback
Lightvalve 67:c2812cf26c38 2375 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 2376 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 2377
Lightvalve 57:f4819de54e7a 2378 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 2379
Lightvalve 57:f4819de54e7a 2380 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 2381 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 2382 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 2383
Lightvalve 57:f4819de54e7a 2384 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 2385 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 2386 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 2387
Lightvalve 69:3995ffeaa786 2388 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2389 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 2390 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 2391 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2392 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 2393 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 2394 }
Lightvalve 57:f4819de54e7a 2395 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 2396 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2397
Lightvalve 57:f4819de54e7a 2398 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 2399 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2400 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 2401 // 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 2402 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2403 // L feedforward velocity
Lightvalve 69:3995ffeaa786 2404 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2405 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 2406 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2407 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2408 // L feedforward velocity
Lightvalve 57:f4819de54e7a 2409 }
Lightvalve 57:f4819de54e7a 2410 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 2411 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2412 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 2413 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 2414 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 2415
Lightvalve 57:f4819de54e7a 2416 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 2417
Lightvalve 67:c2812cf26c38 2418 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 2419 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 2420 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 2421 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 2422 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 2423 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 2424 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2425 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 2426 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2427 } else 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 }
Lightvalve 67:c2812cf26c38 2433 }
Lightvalve 57:f4819de54e7a 2434
Lightvalve 57:f4819de54e7a 2435 } else {
Lightvalve 57:f4819de54e7a 2436 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 2437 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 2438 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 2439
Lightvalve 57:f4819de54e7a 2440 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) * 0.01f
Lightvalve 57:f4819de54e7a 2441 + 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 2442
Lightvalve 72:3436ce769b1e 2443 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 2444
Lightvalve 72:3436ce769b1e 2445 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 2446
Lightvalve 169:645207e160ca 2447
Lightvalve 72:3436ce769b1e 2448 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 2449 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 2450 } else {
Lightvalve 72:3436ce769b1e 2451 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 2452 }
Lightvalve 57:f4819de54e7a 2453
Lightvalve 57:f4819de54e7a 2454 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 2455 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 2456 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 2457 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2458 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2459 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2460 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2461 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 2462 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2463 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2464 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2465 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2466 }
Lightvalve 57:f4819de54e7a 2467 }
Lightvalve 61:bc8c8270f0ab 2468
Lightvalve 57:f4819de54e7a 2469 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2470
Lightvalve 67:c2812cf26c38 2471 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 60:64181f1d3e60 2472 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2473
Lightvalve 57:f4819de54e7a 2474 }
Lightvalve 169:645207e160ca 2475
Lightvalve 72:3436ce769b1e 2476 torq_ref_past = torq_ref;
Lightvalve 58:2eade98630e2 2477
Lightvalve 133:22ab22818e01 2478
Lightvalve 57:f4819de54e7a 2479 break;
Lightvalve 57:f4819de54e7a 2480 }
Lightvalve 58:2eade98630e2 2481
Lightvalve 57:f4819de54e7a 2482 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 2483 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2484 break;
Lightvalve 57:f4819de54e7a 2485 }
Lightvalve 169:645207e160ca 2486
Lightvalve 138:a843f32ced33 2487 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 2488
Lightvalve 169:645207e160ca 2489
Lightvalve 139:15621998925b 2490 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 2491 float Vb = (1256.6f + Amm * (79.0f - pos.sen/(float)(ENC_PULSE_PER_POSITION))) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 169:645207e160ca 2492
Lightvalve 139:15621998925b 2493 // float Va = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 139:15621998925b 2494 // float Vb = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 170:42c938a40313 2495 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 2496
Lightvalve 169:645207e160ca 2497
Lightvalve 170:42c938a40313 2498 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 2499
Lightvalve 138:a843f32ced33 2500 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 2501 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2502 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 2503 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2504 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 2505 } else {
Lightvalve 138:a843f32ced33 2506 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 2507 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 2508 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2509 } else {
Lightvalve 138:a843f32ced33 2510 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 2511 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2512 }
Lightvalve 138:a843f32ced33 2513 }
Lightvalve 138:a843f32ced33 2514 float tau = 0.01f;
Lightvalve 142:43026242815a 2515 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 2516
Lightvalve 138:a843f32ced33 2517 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 2518 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 2519 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2520 } else {
Lightvalve 138:a843f32ced33 2521 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2522 }
Lightvalve 138:a843f32ced33 2523 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 2524 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 2525
Lightvalve 139:15621998925b 2526 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 2527
Lightvalve 138:a843f32ced33 2528 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 2529 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 2530 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2531
Lightvalve 138:a843f32ced33 2532 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 2533 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 2534
Lightvalve 142:43026242815a 2535 float k3 = 20000.0f; //2000
Lightvalve 142:43026242815a 2536 float k4 = 10.0f;
Lightvalve 142:43026242815a 2537 float rho3 = 3.2f;
Lightvalve 142:43026242815a 2538 float rho4 = 25000000.0f;
Lightvalve 139:15621998925b 2539 float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 2540 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 2541 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 2542
Lightvalve 139:15621998925b 2543 if (x_4_des > 0) {
Lightvalve 139:15621998925b 2544 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2545 } else {
Lightvalve 139:15621998925b 2546 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2547 }
Lightvalve 169:645207e160ca 2548
Lightvalve 169:645207e160ca 2549
Lightvalve 138:a843f32ced33 2550 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 2551 x_4_des_old = x_4_des;
Lightvalve 169:645207e160ca 2552
Lightvalve 139:15621998925b 2553 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 169:645207e160ca 2554
Lightvalve 142:43026242815a 2555 float rho_gamma = 50000.0f;//5000
Lightvalve 139:15621998925b 2556 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 2557 gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 169:645207e160ca 2558 break;
Lightvalve 138:a843f32ced33 2559 }
Lightvalve 171:bfc1fd2629d8 2560
Lightvalve 170:42c938a40313 2561 case MODE_RL: {
Lightvalve 170:42c938a40313 2562 //t.reset();
Lightvalve 170:42c938a40313 2563 //t.start();
Lightvalve 170:42c938a40313 2564
Lightvalve 170:42c938a40313 2565 // if(LED == 0) LED = 1;
Lightvalve 170:42c938a40313 2566 // else LED = 0;
Lightvalve 170:42c938a40313 2567
Lightvalve 170:42c938a40313 2568 if (Update_Done_Flag == 1) {
Lightvalve 170:42c938a40313 2569 //Gather Data on each loop
Lightvalve 170:42c938a40313 2570 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 170:42c938a40313 2571 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2572 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2573 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2574 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2575 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2576 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2577 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 170:42c938a40313 2578 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 170:42c938a40313 2579 Actor_Network(temp_array);
Lightvalve 173:68c7914679ec 2580 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 2581 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 173:68c7914679ec 2582 }
Lightvalve 173:68c7914679ec 2583 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 2584 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 173:68c7914679ec 2585 }
Lightvalve 173:68c7914679ec 2586 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 173:68c7914679ec 2587 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 170:42c938a40313 2588 mean_array[RL_timer] = mean;
Lightvalve 170:42c938a40313 2589 deviation_array[RL_timer] = deviation;
Lightvalve 170:42c938a40313 2590 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 170:42c938a40313 2591
Lightvalve 179:d5377766d7ea 2592 virt_pos = virt_pos + (action_array[RL_timer] - 5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2593 if (virt_pos > 70 ) {
Lightvalve 170:42c938a40313 2594 virt_pos = 70.0f;
Lightvalve 179:d5377766d7ea 2595 }else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2596 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2597 }
Lightvalve 171:bfc1fd2629d8 2598
Lightvalve 170:42c938a40313 2599 RL_timer++;
Lightvalve 170:42c938a40313 2600
Lightvalve 170:42c938a40313 2601
Lightvalve 170:42c938a40313 2602 if (RL_timer >= batch_size) {
Lightvalve 170:42c938a40313 2603 RL_timer = 0;
Lightvalve 170:42c938a40313 2604 batch++;
Lightvalve 170:42c938a40313 2605 for(int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 2606 state_array[i][0] = train_set_x[i];
Lightvalve 170:42c938a40313 2607 state_array[i][1] = train_set_error[i];
Lightvalve 170:42c938a40313 2608 //state_array[i][2] = train_set_count[i];
Lightvalve 170:42c938a40313 2609 }
Lightvalve 170:42c938a40313 2610 Update_Case = 1;
Lightvalve 170:42c938a40313 2611 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2612 logging1 = virt_pos;
Lightvalve 179:d5377766d7ea 2613
Lightvalve 170:42c938a40313 2614 if(batch >= num_batch) {
Lightvalve 170:42c938a40313 2615 batch = 0;
Lightvalve 170:42c938a40313 2616 RL_timer = 0;
Lightvalve 170:42c938a40313 2617 Update_Case = 2;
Lightvalve 170:42c938a40313 2618 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2619 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2620 }
Lightvalve 170:42c938a40313 2621 }
Lightvalve 170:42c938a40313 2622 }
Lightvalve 170:42c938a40313 2623
Lightvalve 170:42c938a40313 2624 else {
Lightvalve 170:42c938a40313 2625 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2626 float temp_array[3] = {0.0f};
Lightvalve 170:42c938a40313 2627 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2628 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2629 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2630 Actor_Network(temp_array);
Lightvalve 170:42c938a40313 2631 action = rand_normal(mean, deviation);
Lightvalve 170:42c938a40313 2632 //logging1 = action;
Lightvalve 179:d5377766d7ea 2633 //logging2 = mean;
Lightvalve 179:d5377766d7ea 2634 //logging4 = deviation;
Lightvalve 179:d5377766d7ea 2635 virt_pos = virt_pos + (action-5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2636 if (virt_pos > 70) {
Lightvalve 170:42c938a40313 2637 virt_pos = 70.0f;
Lightvalve 179:d5377766d7ea 2638 }else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2639 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2640 }
Lightvalve 170:42c938a40313 2641
Lightvalve 170:42c938a40313 2642 logging3 = virt_pos;
Lightvalve 170:42c938a40313 2643 }
Lightvalve 170:42c938a40313 2644
Lightvalve 170:42c938a40313 2645 //t.stop();
Lightvalve 170:42c938a40313 2646 //logging1 = t.read()*1000.0f; //msec
Lightvalve 170:42c938a40313 2647
Lightvalve 170:42c938a40313 2648 break;
Lightvalve 170:42c938a40313 2649 }
Lightvalve 14:8e7590227d22 2650
Lightvalve 12:6f2531038ea4 2651 default:
Lightvalve 12:6f2531038ea4 2652 break;
Lightvalve 12:6f2531038ea4 2653 }
Lightvalve 14:8e7590227d22 2654
Lightvalve 57:f4819de54e7a 2655
Lightvalve 57:f4819de54e7a 2656 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 2657
Lightvalve 57:f4819de54e7a 2658 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2659 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 2660 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2661 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 2662 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 2663 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 2664
Lightvalve 57:f4819de54e7a 2665 I_ERR = I_REF_fil - cur.sen;
Lightvalve 67:c2812cf26c38 2666 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 2667
Lightvalve 57:f4819de54e7a 2668
Lightvalve 57:f4819de54e7a 2669 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 2670 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 2671 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 2672 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 2673 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 2674 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 2675
Lightvalve 57:f4819de54e7a 2676 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 2677 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 2678 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 2679 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 2680 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 2681 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 2682 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 2683 }
Lightvalve 57:f4819de54e7a 2684
Lightvalve 57:f4819de54e7a 2685 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 2686
Lightvalve 67:c2812cf26c38 2687 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 57:f4819de54e7a 2688 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 57:f4819de54e7a 2689 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 57:f4819de54e7a 2690 I_REF_fil_old = I_REF_fil;
Lightvalve 67:c2812cf26c38 2691 // 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 2692 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 67:c2812cf26c38 2693 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 2694
Lightvalve 57:f4819de54e7a 2695 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 2696 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 2697 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 2698 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2699 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 2700 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2701 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 2702 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 2703 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2704 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 2705 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2706 }
Lightvalve 57:f4819de54e7a 2707 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2708 } else {
Lightvalve 57:f4819de54e7a 2709 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 2710 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2711 }
Lightvalve 57:f4819de54e7a 2712
Lightvalve 57:f4819de54e7a 2713 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2714 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 2715 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2716 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 57:f4819de54e7a 2717 if (FLAG_VALVE_DEADZONE) {
Lightvalve 57:f4819de54e7a 2718 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2719 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2720
Lightvalve 57:f4819de54e7a 2721 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2722
Lightvalve 57:f4819de54e7a 2723 } else {
Lightvalve 57:f4819de54e7a 2724 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 57:f4819de54e7a 2725 }
Lightvalve 57:f4819de54e7a 2726
Lightvalve 57:f4819de54e7a 2727 // Output Voltage Linearization
Lightvalve 57:f4819de54e7a 2728 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 67:c2812cf26c38 2729 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 2730
Lightvalve 57:f4819de54e7a 2731 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 2732 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 2733 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 2734 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 2735 } else { //////////////////////////sw valve
Lightvalve 89:a7b45368ea0f 2736 // Output Voltage Linearization
Lightvalve 135:79885a39c161 2737 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 135:79885a39c161 2738 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 89:a7b45368ea0f 2739
Lightvalve 89:a7b45368ea0f 2740 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 135:79885a39c161 2741 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 135:79885a39c161 2742 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 135:79885a39c161 2743 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 135:79885a39c161 2744
Lightvalve 135:79885a39c161 2745 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 2746 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 2747 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 2748 }
Lightvalve 169:645207e160ca 2749
Lightvalve 67:c2812cf26c38 2750 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 67:c2812cf26c38 2751 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 67:c2812cf26c38 2752 // else V_out = V_out;
Lightvalve 169:645207e160ca 2753
jobuuu 7:e9086c72bb22 2754 /*******************************************************
jobuuu 7:e9086c72bb22 2755 *** PWM
jobuuu 7:e9086c72bb22 2756 ********************************************************/
Lightvalve 169:645207e160ca 2757 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 2758 V_out = -V_out;
Lightvalve 67:c2812cf26c38 2759 }
Lightvalve 169:645207e160ca 2760
Lightvalve 49:e7bcfc244d40 2761 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2762 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 2763 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2764 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2765 }
Lightvalve 49:e7bcfc244d40 2766 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2767
Lightvalve 19:23b7c1ad8683 2768 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2769 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2770 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2771
Lightvalve 30:8d561f16383b 2772 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2773 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2774 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2775 } else {
jobuuu 2:a1c0a37df760 2776 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2777 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2778 }
Lightvalve 13:747daba9cf59 2779
jobuuu 1:e04e563be5ce 2780 //pwm
Lightvalve 30:8d561f16383b 2781 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2782 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 2783
Lightvalve 61:bc8c8270f0ab 2784
Lightvalve 57:f4819de54e7a 2785 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2786
Lightvalve 54:647072f5307a 2787 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 2788 if (flag_data_request[0] == HIGH) {
Lightvalve 57:f4819de54e7a 2789 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 2790 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2791 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 2792 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 2793 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 2794 }
Lightvalve 57:f4819de54e7a 2795 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 2796 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2797 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 2798 } else if (SENSING_MODE == 1) {
Lightvalve 67:c2812cf26c38 2799 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 2800 }
Lightvalve 52:8ea76864368a 2801 }
Lightvalve 52:8ea76864368a 2802 }
Lightvalve 56:6f50d9d3bfee 2803 if (flag_data_request[1] == HIGH) {
Lightvalve 179:d5377766d7ea 2804 CAN_TX_TORQUE((int16_t) (return_G[0]*100.0f)); //1300
Lightvalve 54:647072f5307a 2805 }
Lightvalve 58:2eade98630e2 2806
Lightvalve 58:2eade98630e2 2807
Lightvalve 171:bfc1fd2629d8 2808 if (flag_data_request[2] == HIGH) {
Lightvalve 169:645207e160ca 2809 double t_value = 0.0f;
Lightvalve 171:bfc1fd2629d8 2810 if(value>=(float) VALVE_CENTER) {
Lightvalve 131:d08121ac87ba 2811 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2812 } else {
Lightvalve 131:d08121ac87ba 2813 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2814 }
Lightvalve 169:645207e160ca 2815 double t_value_ref = 0.0f;
Lightvalve 169:645207e160ca 2816 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2817 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2818 } else {
Lightvalve 169:645207e160ca 2819 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2820 }
Lightvalve 169:645207e160ca 2821
Lightvalve 169:645207e160ca 2822
Lightvalve 169:645207e160ca 2823 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 55:b25725257569 2824 }
Lightvalve 58:2eade98630e2 2825
Lightvalve 57:f4819de54e7a 2826 //If it doesn't rest, below can can not work.
Lightvalve 57:f4819de54e7a 2827 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 57:f4819de54e7a 2828 ;
Lightvalve 57:f4819de54e7a 2829 }
Lightvalve 58:2eade98630e2 2830
Lightvalve 169:645207e160ca 2831 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 2832 //PWM
Lightvalve 180:02be1711ee0b 2833 //CAN_TX_PWM((int16_t) (V[0]*100.0f)); //1500
Lightvalve 192:637092202815 2834 CAN_TX_PWM((int16_t) (f_future[1])); //1500
Lightvalve 54:647072f5307a 2835 }
Lightvalve 179:d5377766d7ea 2836
Lightvalve 56:6f50d9d3bfee 2837 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 2838 //valve position
Lightvalve 179:d5377766d7ea 2839 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 179:d5377766d7ea 2840 //CAN_TX_VALVE_POSITION((int16_t) action_array[20], (int16_t) virt_pos, (int16_t) Update_Case*1000, (int16_t) (logging4*1000.0f)); //1600
Lightvalve 54:647072f5307a 2841 }
Lightvalve 20:806196fda269 2842
Lightvalve 54:647072f5307a 2843 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 2844 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 2845 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 2846 // }
Lightvalve 54:647072f5307a 2847 //if (flag_delay_test == 1){
Lightvalve 58:2eade98630e2 2848 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 2849 //}
Lightvalve 52:8ea76864368a 2850
Lightvalve 54:647072f5307a 2851 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 2852 }
Lightvalve 54:647072f5307a 2853 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 2854
Lightvalve 20:806196fda269 2855 }
Lightvalve 52:8ea76864368a 2856 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 2857
Lightvalve 58:2eade98630e2 2858 }