Original Version of STM Board

Dependencies:   FastPWM mbed

Committer:
Lightvalve
Date:
Mon Dec 28 01:47:51 2020 +0000
Revision:
220:153c6f5e1c44
Parent:
219:3cb6bd063f56
Child:
221:881c895e3617
Child:
222:5e19903365a5
Child:
223:e26830fbaffd
201228-1_good_f_k+1 => f_k ... f_k+10 =>f_k+9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 220:153c6f5e1c44 1 //201228_1_good
GiJeongKim 0:51c43836c1d7 2 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 3 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 4 #include "INIT_HW.h"
jobuuu 2:a1c0a37df760 5 #include "function_CAN.h"
GiJeongKim 0:51c43836c1d7 6 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 7 #include "I2C_AS5510.h"
GiJeongKim 0:51c43836c1d7 8 #include "setting.h"
Lightvalve 11:82d8768d7351 9 #include "function_utilities.h"
Lightvalve 16:903b5a4433b4 10 #include "stm32f4xx_flash.h"
Lightvalve 169:645207e160ca 11 #include "FlashWriter.h"
Lightvalve 170:42c938a40313 12 #include <string>
Lightvalve 170:42c938a40313 13 #include <iostream>
Lightvalve 179:d5377766d7ea 14 #include <cmath>
Lightvalve 61:bc8c8270f0ab 15
Lightvalve 61:bc8c8270f0ab 16 using namespace std;
Lightvalve 61:bc8c8270f0ab 17 Timer t;
GiJeongKim 0:51c43836c1d7 18
Lightvalve 31:66738bfecec5 19 ///191008////
Lightvalve 31:66738bfecec5 20
jobuuu 7:e9086c72bb22 21 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 22 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 23 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 24 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 25 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 26 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 27 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 28 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 29
Lightvalve 24:ef6e1092e9e6 30
jobuuu 7:e9086c72bb22 31 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 32 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 33 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 34
jobuuu 7:e9086c72bb22 35 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 36 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
GiJeongKim 0:51c43836c1d7 37 const int i2c_slave_addr1 = 0x56;
GiJeongKim 0:51c43836c1d7 38 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 39
jobuuu 7:e9086c72bb22 40 // SPI ///////////////////////////////////////////
Lightvalve 170:42c938a40313 41 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 170:42c938a40313 42 DigitalOut eeprom_cs(PB_12);
Lightvalve 16:903b5a4433b4 43 //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
GiJeongKim 0:51c43836c1d7 44 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 45 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 46 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 47
jobuuu 7:e9086c72bb22 48 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 49 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 50
jobuuu 7:e9086c72bb22 51 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 52 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 53 CANMessage msg;
Lightvalve 11:82d8768d7351 54 void onMsgReceived()
Lightvalve 11:82d8768d7351 55 {
Lightvalve 11:82d8768d7351 56 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 57 }
jobuuu 2:a1c0a37df760 58
jobuuu 7:e9086c72bb22 59 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 60 State pos;
jobuuu 7:e9086c72bb22 61 State vel;
jobuuu 7:e9086c72bb22 62 State Vout;
jobuuu 7:e9086c72bb22 63 State torq;
Lightvalve 207:c70c5a9f17dd 64 State torq_dot;
jobuuu 7:e9086c72bb22 65 State pres_A;
jobuuu 7:e9086c72bb22 66 State pres_B;
jobuuu 7:e9086c72bb22 67 State cur;
Lightvalve 14:8e7590227d22 68 State valve_pos;
Lightvalve 14:8e7590227d22 69
Lightvalve 14:8e7590227d22 70 State INIT_Vout;
Lightvalve 14:8e7590227d22 71 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 72 State INIT_Pos;
Lightvalve 14:8e7590227d22 73 State INIT_torq;
jobuuu 5:a4319f79457b 74
Lightvalve 19:23b7c1ad8683 75 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 76 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 77 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 78 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 79
Lightvalve 19:23b7c1ad8683 80 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 84 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 85 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 86
Lightvalve 61:bc8c8270f0ab 87
Lightvalve 61:bc8c8270f0ab 88
Lightvalve 61:bc8c8270f0ab 89
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91 // =============================================================================
jobuuu 7:e9086c72bb22 92 // =============================================================================
jobuuu 7:e9086c72bb22 93
Lightvalve 12:6f2531038ea4 94 /*******************************************************************************
Lightvalve 12:6f2531038ea4 95 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 96 ******************************************************************************/
Lightvalve 13:747daba9cf59 97 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 98 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 99 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 100 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 101 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 102 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 103 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 104 };
Lightvalve 12:6f2531038ea4 105
Lightvalve 12:6f2531038ea4 106 /*******************************************************************************
Lightvalve 12:6f2531038ea4 107 * CONTROL MODE
Lightvalve 12:6f2531038ea4 108 ******************************************************************************/
Lightvalve 13:747daba9cf59 109 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 110 //control mode
Lightvalve 12:6f2531038ea4 111 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 112 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 113 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 114
Lightvalve 47:fdcb8bd86fd6 115 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 116 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 117 MODE_RL, //5
Lightvalve 13:747daba9cf59 118
Lightvalve 12:6f2531038ea4 119 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 120 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 121 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 122
Lightvalve 12:6f2531038ea4 123 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 124 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 125
Lightvalve 14:8e7590227d22 126 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 127 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 128 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 171:bfc1fd2629d8 129 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 130
Lightvalve 12:6f2531038ea4 131 //utility
Lightvalve 12:6f2531038ea4 132 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 133 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 134 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 135 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 136 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 137 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 138 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 139
Lightvalve 12:6f2531038ea4 140 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 141 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 142 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 143 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 144 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 145 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 146 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 147 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 148 };
Lightvalve 12:6f2531038ea4 149
Lightvalve 65:a2d7c63419c2 150 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 151 {
Lightvalve 169:645207e160ca 152 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 153 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 154
Lightvalve 169:645207e160ca 155 /** Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 156 */
Lightvalve 169:645207e160ca 157 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 158 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 169:645207e160ca 159 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 160 */
Lightvalve 169:645207e160ca 161 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 162 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 163 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 164 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 165 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 166 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 167 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 168 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 169 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 170 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 171 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 172 //Error_Handler();
Lightvalve 169:645207e160ca 173 }
Lightvalve 169:645207e160ca 174 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 175 */
Lightvalve 169:645207e160ca 176 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 177 //Error_Handler();
Lightvalve 169:645207e160ca 178 }
Lightvalve 169:645207e160ca 179 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 180 */
Lightvalve 169:645207e160ca 181 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 182 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 183 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 184 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 185 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 186 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 187
Lightvalve 169:645207e160ca 188 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 189 //Error_Handler();
Lightvalve 169:645207e160ca 190 }
Lightvalve 65:a2d7c63419c2 191 }
Lightvalve 65:a2d7c63419c2 192
Lightvalve 99:7bbcb3c0fb06 193 float u_past[num_array_u_past] = {0.0f};
Lightvalve 68:328e1be06f5d 194 float x_past[num_array_x_past] = {0.0f};
Lightvalve 85:a3b46118b5cd 195 float x_future[num_array_x_future] = {0.0f};
Lightvalve 68:328e1be06f5d 196 float f_past[num_array_f_past] = {0.0f};
Lightvalve 68:328e1be06f5d 197 float f_future[num_array_f_future] = {0.0f};
Lightvalve 66:a8e6799dbce3 198
Lightvalve 73:f80dc3970c99 199 float input_NN[num_input] = { 0.0f };
Lightvalve 65:a2d7c63419c2 200
Lightvalve 169:645207e160ca 201 const float h1[num_input][16] = {
Lightvalve 219:3cb6bd063f56 202 {-1.6017948389053345f,1.2721383571624756f,0.1310378611087799f,-0.9444252848625183f,-1.9589552879333496f,1.1329879760742188f,-0.17743819952011108f,-0.19157131016254425f,-0.39761483669281006f,0.02001183293759823f,0.5025057196617126f,-2.509382724761963f,-0.2675858736038208f,0.27560269832611084f,-0.2611466646194458f,-0.9627107977867126f},
Lightvalve 219:3cb6bd063f56 203 {-1.2710213661193848f,0.9135757684707642f,-0.3988758325576782f,-0.7979171872138977f,-1.4895585775375366f,0.5568610429763794f,-0.4116867780685425f,-0.261433482170105f,-0.22742684185504913f,0.2772603929042816f,0.118864506483078f,-1.5505203008651733f,0.23985113203525543f,-0.2809593081474304f,-0.21292872726917267f,-0.6193060874938965f},
Lightvalve 219:3cb6bd063f56 204 {-1.1586071252822876f,0.7516070604324341f,0.36955419182777405f,-0.9032031297683716f,-0.9722328782081604f,0.5363096594810486f,0.2916664779186249f,-0.016872992739081383f,-0.34971633553504944f,0.2836141288280487f,-0.054457396268844604f,-0.9038598537445068f,-0.0020234500989317894f,0.19621078670024872f,0.056768983602523804f,-1.1061573028564453f},
Lightvalve 219:3cb6bd063f56 205 {-0.7506084442138672f,0.1879994422197342f,0.40358296036720276f,0.12524257600307465f,-0.5177642107009888f,-0.18764258921146393f,-0.11764177680015564f,0.31180232763290405f,-0.20574431121349335f,0.42218565940856934f,0.16467393934726715f,0.3004089593887329f,-0.1665881872177124f,0.09486014395952225f,-0.3007376194000244f,-0.8816207051277161f},
Lightvalve 219:3cb6bd063f56 206 {0.7436127662658691f,-0.604726254940033f,-0.005224883556365967f,0.9320474863052368f,0.5615028142929077f,-0.8275365829467773f,0.13140985369682312f,0.018719080835580826f,-0.24859893321990967f,0.23700864613056183f,-0.41653650999069214f,1.252004861831665f,-0.0686110407114029f,0.23053120076656342f,-0.39249828457832336f,-0.6377205848693848f},
Lightvalve 219:3cb6bd063f56 207 {3.0718040466308594f,-3.262662887573242f,0.22110894322395325f,2.670456647872925f,2.9312491416931152f,-3.062858819961548f,0.16770240664482117f,-0.1752457618713379f,-0.304975301027298f,0.24974356591701508f,-2.912475347518921f,3.297006845474243f,-0.2682695984840393f,0.3757273256778717f,-0.4196912348270416f,1.0734145641326904f},
Lightvalve 219:3cb6bd063f56 208 {-1.2198433876037598f,1.5182578563690186f,-0.2540779709815979f,-1.8283756971359253f,-1.570626139640808f,1.7989088296890259f,0.06107431650161743f,-0.7636352777481079f,0.3089294731616974f,-0.09506343305110931f,2.775860548019409f,0.7932590246200562f,-0.17504744231700897f,0.2239564061164856f,-0.15860587358474731f,-0.7237789630889893f},
Lightvalve 219:3cb6bd063f56 209 {0.7539365291595459f,-0.9152541160583496f,-0.2703247666358948f,0.3871593773365021f,0.3573713004589081f,-0.712091326713562f,-0.18203827738761902f,-0.5724472999572754f,-0.17589353024959564f,-0.025821663439273834f,0.3012785017490387f,0.5540553331375122f,0.3776646554470062f,0.029355080798268318f,0.0436977744102478f,1.1350784301757812f},
Lightvalve 219:3cb6bd063f56 210 {0.6773941516876221f,-0.1407381296157837f,0.10406997799873352f,-0.31886449456214905f,0.2701631486415863f,-0.2895267903804779f,-0.07359349727630615f,-0.522844135761261f,-0.4761183261871338f,0.22720466554164886f,-0.11068277060985565f,0.17022143304347992f,0.23315370082855225f,0.15179778635501862f,-0.31029027700424194f,0.851940393447876f},
Lightvalve 219:3cb6bd063f56 211 {0.11661386489868164f,-0.3948979079723358f,0.18748918175697327f,-0.2878238558769226f,0.28554558753967285f,0.11674453318119049f,-0.3036908507347107f,-0.1508290320634842f,-0.14107836782932281f,-0.5925581455230713f,0.2786004841327667f,-1.0318316221237183f,0.37844526767730713f,-0.270053893327713f,0.35390153527259827f,0.38183316588401794f},
Lightvalve 219:3cb6bd063f56 212 {0.4711846709251404f,0.11557799577713013f,0.3775894343852997f,0.0314268134534359f,0.03386480361223221f,0.016547810286283493f,-0.20654654502868652f,-0.06258935481309891f,-0.4769713580608368f,-0.07612045109272003f,0.6390635967254639f,-0.8360038995742798f,-0.06711658090353012f,-0.030321964994072914f,0.2710213363170624f,-0.054686423391103745f},
Lightvalve 219:3cb6bd063f56 213 {-0.37094420194625854f,-0.2944038212299347f,-0.40945154428482056f,-0.18097494542598724f,0.14135384559631348f,0.021318335086107254f,0.15575703978538513f,0.29287639260292053f,-0.12214788794517517f,0.1086469367146492f,-0.14901117980480194f,-0.15198558568954468f,0.1470927745103836f,-0.03396562486886978f,0.10656675696372986f,0.06443876773118973f},
Lightvalve 219:3cb6bd063f56 214 {0.025174643844366074f,-0.12816596031188965f,0.2015570104122162f,0.1076304167509079f,0.45414742827415466f,0.19509439170360565f,-0.24415965378284454f,0.013022400438785553f,-0.10836649686098099f,-0.29661229252815247f,0.09127697348594666f,-0.21214918792247772f,0.09341105818748474f,0.01174785103648901f,-0.22613362967967987f,-0.3625008761882782f},
Lightvalve 219:3cb6bd063f56 215 {-0.04146391525864601f,-0.02673570066690445f,0.41918185353279114f,-0.1233106479048729f,-0.07927072793245316f,-0.1876136064529419f,-0.32497477531433105f,-0.4840133786201477f,-0.044291287660598755f,-0.3259299099445343f,0.04178468510508537f,-0.0003480979357846081f,-0.31783685088157654f,-0.05072624981403351f,0.04871204495429993f,-0.08137178421020508f},
Lightvalve 219:3cb6bd063f56 216 {0.1422140747308731f,0.18844546377658844f,0.20596781373023987f,-0.2328251153230667f,0.34344229102134705f,0.06260570883750916f,0.042680561542510986f,0.07508613914251328f,0.4000869691371918f,-0.010638000443577766f,0.13212932646274567f,-0.657844066619873f,0.10472581535577774f,-0.038563571870326996f,0.06784489750862122f,-0.2556094229221344f},
Lightvalve 219:3cb6bd063f56 217 {0.15386898815631866f,-0.49337783455848694f,-0.4106670916080475f,-0.4150790572166443f,-0.05264211446046829f,-0.05708514153957367f,-0.15809619426727295f,-0.20627009868621826f,0.27094385027885437f,-0.09812130033969879f,-0.3514001667499542f,-0.9586353898048401f,-0.275861918926239f,0.02052518166601658f,-0.08373728394508362f,-0.0018482634332031012f},
Lightvalve 65:a2d7c63419c2 218 };
Lightvalve 65:a2d7c63419c2 219
Lightvalve 169:645207e160ca 220 const float h2[16][16] = {
Lightvalve 219:3cb6bd063f56 221 {-0.9739745259284973f,3.579744815826416f,-0.06966331601142883f,0.8622938990592957f,-0.21907491981983185f,2.2829554080963135f,-0.13155855238437653f,-0.014519207179546356f,-0.43008196353912354f,-0.1415480375289917f,0.17231711745262146f,-4.9480390548706055f,-0.02454538829624653f,-1.7999197244644165f,-0.38519009947776794f,1.4918317794799805f},
Lightvalve 219:3cb6bd063f56 222 {0.03609159216284752f,-0.6491178274154663f,0.057057321071624756f,-0.5475255846977234f,-0.35503754019737244f,-3.0916311740875244f,-0.5200656652450562f,-0.42126399278640747f,-0.33757925033569336f,0.2895788848400116f,-0.4909915030002594f,-1.1257127523422241f,0.18442146480083466f,1.348382830619812f,-0.5171104669570923f,-2.655162811279297f},
Lightvalve 219:3cb6bd063f56 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 219:3cb6bd063f56 224 {0.15409719944000244f,0.4271918833255768f,-0.2894435524940491f,0.4524352252483368f,0.08946844935417175f,-0.24254196882247925f,-0.5646932125091553f,0.2643083930015564f,0.26211628317832947f,0.2953031361103058f,0.0909804254770279f,0.8597109913825989f,-0.5144365429878235f,-2.37484073638916f,-0.026095986366271973f,0.29753774404525757f},
Lightvalve 219:3cb6bd063f56 225 {-0.7763098478317261f,2.959994316101074f,-0.25313520431518555f,0.5777999758720398f,0.036378175020217896f,0.7788732051849365f,-0.5079860091209412f,-0.20636579394340515f,-0.024399548768997192f,-0.39465832710266113f,-0.4901541471481323f,-3.3827083110809326f,-0.013386494480073452f,-1.876047134399414f,-0.6391417384147644f,0.6140359044075012f},
Lightvalve 219:3cb6bd063f56 226 {0.020159810781478882f,0.545926034450531f,-0.3519742488861084f,-0.17923703789710999f,-0.3015052080154419f,-1.5342668294906616f,-0.7026005387306213f,-0.12369749695062637f,-0.23047015070915222f,0.3579089343547821f,-0.2382185161113739f,-5.193862438201904f,-0.3443910777568817f,1.945488691329956f,0.014947117306292057f,-1.2414220571517944f},
Lightvalve 219:3cb6bd063f56 227 {-0.1828227937221527f,-0.02554568648338318f,-0.3260969817638397f,0.08422836661338806f,-0.38453540205955505f,-0.25432005524635315f,0.285016268491745f,0.12387624382972717f,-0.0982072651386261f,0.13111665844917297f,-0.03692615032196045f,-0.32796353101730347f,-0.21546880900859833f,0.049302369356155396f,-0.27088475227355957f,-0.4124959409236908f},
Lightvalve 219:3cb6bd063f56 228 {0.3839884400367737f,0.6259503960609436f,0.33083590865135193f,0.04438639059662819f,-0.14358049631118774f,0.12416914105415344f,-0.09043094515800476f,-0.3003333508968353f,0.2600560486316681f,-0.37898191809654236f,0.4972154498100281f,-0.2750113010406494f,-0.31673234701156616f,-0.015966646373271942f,-0.08780939131975174f,0.328056275844574f},
Lightvalve 219:3cb6bd063f56 229 {-0.4527643918991089f,0.3202667832374573f,-0.11379697918891907f,0.0684560090303421f,0.041274964809417725f,-0.40635451674461365f,-0.2745489478111267f,0.031847670674324036f,0.06103590130805969f,-0.38046833872795105f,-0.45007404685020447f,-0.5276026725769043f,-0.37024784088134766f,-0.3135625123977661f,-0.3714982867240906f,-0.12828999757766724f},
Lightvalve 219:3cb6bd063f56 230 {0.2802984118461609f,0.5001301765441895f,-0.13502129912376404f,0.6260269284248352f,0.12987366318702698f,0.6041213870048523f,0.2711336314678192f,-0.3632148504257202f,-0.13619378209114075f,0.16938945651054382f,0.5018476247787476f,0.3686026930809021f,0.3811538517475128f,-0.5396527647972107f,-0.19131632149219513f,0.09156869351863861f},
Lightvalve 219:3cb6bd063f56 231 {-1.5928384065628052f,0.23942683637142181f,0.0457797646522522f,-0.6350169777870178f,-0.03321319818496704f,0.08973085880279541f,-0.18186365067958832f,-0.3142535090446472f,-0.050184011459350586f,0.12480869889259338f,-0.5123732089996338f,-4.090933799743652f,-0.4121001660823822f,1.199095368385315f,-0.18886037170886993f,-0.7048641443252563f},
Lightvalve 219:3cb6bd063f56 232 {0.03677457198500633f,0.4954114556312561f,0.3954955041408539f,0.2689988911151886f,0.0033026933670043945f,1.278443694114685f,-0.4117635190486908f,-0.24394789338111877f,0.3487861454486847f,-0.32520344853401184f,0.14627091586589813f,-0.9104770421981812f,-0.07545550167560577f,-1.3930959701538086f,0.008092201314866543f,-1.8283593654632568f},
Lightvalve 219:3cb6bd063f56 233 {-0.31720414757728577f,-0.21001535654067993f,-0.15798500180244446f,0.3665239214897156f,-0.37393757700920105f,0.37094148993492126f,0.11292675137519836f,-0.2947862446308136f,-0.3764709532260895f,0.2424570620059967f,-0.1080506220459938f,-0.2837170362472534f,0.41839322447776794f,-0.01957082748413086f,-0.12957632541656494f,0.10856001079082489f},
Lightvalve 219:3cb6bd063f56 234 {0.03818705305457115f,0.27717405557632446f,-0.2018718123435974f,0.06585641950368881f,0.07545611262321472f,0.05265878140926361f,-0.11297860741615295f,-0.21416273713111877f,-0.24509364366531372f,0.19522181153297424f,0.351835161447525f,-0.2859584093093872f,-0.16718891263008118f,0.23180222511291504f,-0.23956191539764404f,-0.07242263853549957f},
Lightvalve 219:3cb6bd063f56 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 219:3cb6bd063f56 236 {-1.0752074718475342f,-2.8200089931488037f,0.12081471085548401f,-0.12609876692295074f,0.29976895451545715f,0.4510769844055176f,-0.2826606035232544f,0.18327592313289642f,-0.3893685042858124f,-0.02222958207130432f,-0.19482164084911346f,-0.37641236186027527f,-0.19991017878055573f,-1.0054714679718018f,0.06622982025146484f,1.2732210159301758f},
Lightvalve 65:a2d7c63419c2 237 };
Lightvalve 65:a2d7c63419c2 238
Lightvalve 169:645207e160ca 239 const float h3[16][16] = {
Lightvalve 219:3cb6bd063f56 240 {-0.36079341173171997f,0.5496202707290649f,-0.6524567604064941f,-1.0138130187988281f,-0.7639057636260986f,0.5781964063644409f,0.3332441747188568f,0.3036012351512909f,-0.2405819296836853f,0.04891335964202881f,-0.11870327591896057f,0.4001283347606659f,-0.533208429813385f,0.9845144152641296f,0.32505926489830017f,-0.19595709443092346f},
Lightvalve 219:3cb6bd063f56 241 {0.047732532024383545f,0.017954785376787186f,-0.18629395961761475f,-0.9162130951881409f,-0.43290695548057556f,-4.281400680541992f,-0.3198729455471039f,0.06247803568840027f,-0.19349214434623718f,-0.9508686065673828f,0.2572092115879059f,-0.41319137811660767f,-0.35956844687461853f,-0.5297766327857971f,0.10103966295719147f,-0.7109990119934082f},
Lightvalve 219:3cb6bd063f56 242 {0.07903262972831726f,0.2790505588054657f,-0.07798504829406738f,0.04248586297035217f,-0.1963958442211151f,-0.19260792434215546f,-0.4038352966308594f,0.015906542539596558f,0.15353140234947205f,0.030178606510162354f,0.2488909661769867f,0.13805970549583435f,-0.0816211998462677f,-0.20733052492141724f,-0.3036302626132965f,0.054825395345687866f},
Lightvalve 219:3cb6bd063f56 243 {-0.30922991037368774f,-0.05632366985082626f,-1.3186177015304565f,-0.9530683755874634f,-0.49597063660621643f,0.2564902603626251f,0.20021501183509827f,0.07002416253089905f,-0.4060443043708801f,-0.4593014717102051f,0.13192829489707947f,0.41021624207496643f,-0.2266009896993637f,0.25329798460006714f,0.28113701939582825f,-0.15740571916103363f},
Lightvalve 219:3cb6bd063f56 244 {-0.39607733488082886f,-0.05481579899787903f,0.1976260244846344f,0.022423356771469116f,0.16892847418785095f,-0.27518749237060547f,0.16012099385261536f,0.3626593053340912f,-0.08640444278717041f,-0.11053556203842163f,-0.10529157519340515f,-0.31317979097366333f,-0.1530032455921173f,-0.1336749792098999f,0.22959044575691223f,0.19986507296562195f},
Lightvalve 219:3cb6bd063f56 245 {-0.37449589371681213f,-0.02029903419315815f,-2.5072414875030518f,-12.973404884338379f,0.2337467223405838f,1.0865919589996338f,-0.18732719123363495f,0.22384825348854065f,-0.850281298160553f,-0.4489060342311859f,-0.23735009133815765f,0.05998014658689499f,0.23774290084838867f,0.08863037824630737f,0.5211813449859619f,-1.1956897974014282f},
Lightvalve 219:3cb6bd063f56 246 {0.4110594093799591f,0.2715781033039093f,-0.12724249064922333f,0.37246426939964294f,-0.06906148046255112f,-0.05251416191458702f,-0.08670487999916077f,-0.25336313247680664f,-0.030661463737487793f,-0.06259563565254211f,-0.1344406008720398f,0.35313835740089417f,0.13369451463222504f,0.2011522799730301f,0.3126353323459625f,-0.3391006588935852f},
Lightvalve 219:3cb6bd063f56 247 {-0.40892091393470764f,0.04420051723718643f,-0.26215535402297974f,0.25968697667121887f,0.4493691027164459f,-0.16038022935390472f,-0.23312048614025116f,-0.390264093875885f,0.28059282898902893f,-0.1559126079082489f,-0.14134526252746582f,-0.0003446042537689209f,-0.17052586376667023f,-0.4737553000450134f,0.08030081540346146f,0.005298197269439697f},
Lightvalve 219:3cb6bd063f56 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 219:3cb6bd063f56 249 {0.2847062647342682f,0.018552124500274658f,0.11435768008232117f,0.36562982201576233f,-0.047046810388565063f,0.30447837710380554f,0.2430230677127838f,0.2909286320209503f,-0.2802048921585083f,0.18043199181556702f,0.41849127411842346f,-0.287167489528656f,0.24394884705543518f,-0.14084559679031372f,-0.10168051719665527f,0.010465055704116821f},
Lightvalve 219:3cb6bd063f56 250 {0.15459725260734558f,0.3123319149017334f,0.10377500206232071f,-0.044353384524583817f,-0.2320195883512497f,0.12762269377708435f,-0.08742031455039978f,0.05785742402076721f,-0.07219423353672028f,-0.2943510413169861f,0.1268840730190277f,-0.30447322130203247f,-0.044554103165864944f,-0.20284022390842438f,0.5049844980239868f,0.23406195640563965f},
Lightvalve 219:3cb6bd063f56 251 {-0.408692330121994f,0.7442746162414551f,-0.3166918158531189f,-0.5923694968223572f,-2.5536251068115234f,2.459355354309082f,0.03285527229309082f,0.38763079047203064f,-0.20705322921276093f,-0.25883403420448303f,0.12809070944786072f,0.03996849060058594f,1.5557342767715454f,2.810499429702759f,0.883807361125946f,-0.5292647480964661f},
Lightvalve 219:3cb6bd063f56 252 {-0.2991822361946106f,0.3794580399990082f,-0.07897943258285522f,-0.05932474136352539f,-0.019097916781902313f,0.10188531875610352f,-0.11253207921981812f,0.34576353430747986f,0.04814547300338745f,-0.35770976543426514f,-0.044228196144104004f,-0.36229726672172546f,0.015840977430343628f,-0.13475483655929565f,0.36124154925346375f,-0.16869547963142395f},
Lightvalve 219:3cb6bd063f56 253 {-0.2675279378890991f,-0.4076881408691406f,0.6109089851379395f,1.124742865562439f,0.791110098361969f,-1.8034776449203491f,-0.32875844836235046f,-0.31893211603164673f,-0.6889935731887817f,-1.1343306303024292f,-0.5256567001342773f,-0.8404866456985474f,1.0291988849639893f,-1.3514559268951416f,0.12213380634784698f,0.17103822529315948f},
Lightvalve 219:3cb6bd063f56 254 {-0.015470266342163086f,-0.23503242433071136f,0.13451209664344788f,0.36471807956695557f,-0.08753460645675659f,-0.20316985249519348f,0.05196094512939453f,-0.008358269929885864f,-0.4239840805530548f,-0.38441595435142517f,0.13179203867912292f,-0.11511552333831787f,0.07671336829662323f,0.1646096110343933f,0.19001242518424988f,-0.29890790581703186f},
Lightvalve 219:3cb6bd063f56 255 {0.1863725483417511f,-0.07918193191289902f,-0.8252384066581726f,-36.685184478759766f,1.862860083580017f,1.8086168766021729f,0.04860696196556091f,0.17769548296928406f,-0.32842522859573364f,-0.40967434644699097f,-0.3929237127304077f,-0.0008496989612467587f,2.3736817836761475f,1.629492998123169f,-0.45149895548820496f,-0.32196617126464844f},
Lightvalve 66:a8e6799dbce3 256 };
Lightvalve 65:a2d7c63419c2 257
Lightvalve 219:3cb6bd063f56 258 const float hout[16] = { 0.45773375034332275f,0.3443804383277893f,-0.43964359164237976f,-0.21056394279003143f,0.19662600755691528f,0.7040731906890869f,0.0030125975608825684f,0.15066689252853394f,0.10095822811126709f,-0.13003124296665192f,0.18801097571849823f,0.0785079076886177f,-0.15748845040798187f,0.1868625432252884f,0.18598346412181854f,-0.9022102355957031f };
Lightvalve 219:3cb6bd063f56 259
Lightvalve 219:3cb6bd063f56 260 const float b1[16] = { 0.7107149362564087f,1.0883506536483765f,-1.7145336866378784f,0.09724511206150055f,1.4542453289031982f,1.417521595954895f,-0.058932315558195114f,0.9398505687713623f,0.4389953017234802f,-0.9609658718109131f,0.8672975301742554f,1.5156910419464111f,-0.3718140721321106f,-0.886523962020874f,-1.087764859199524f,2.5278995037078857f };
Lightvalve 219:3cb6bd063f56 261
Lightvalve 219:3cb6bd063f56 262 const float b2[16] = { -0.4577406942844391f,-0.4344789981842041f,-1.4564176797866821f,-0.6435210704803467f,-0.6939148902893066f,-0.16397996246814728f,-0.013260572217404842f,-0.43840786814689636f,-1.20063054561615f,-1.912178635597229f,-0.1346415877342224f,0.10342646390199661f,-0.23722384870052338f,0.9154923558235168f,-0.1802087128162384f,1.0650097131729126f };
Lightvalve 219:3cb6bd063f56 263
Lightvalve 219:3cb6bd063f56 264 const float b3[16] = { -1.963319182395935f,-0.7180035710334778f,0.7040019035339355f,-0.4437018930912018f,-0.7515134811401367f,1.0377838611602783f,-0.45353031158447266f,-0.6562485098838806f,0.04508165270090103f,0.06766403466463089f,-0.29913392663002014f,-0.09212813526391983f,-1.5836485624313354f,0.42195138335227966f,-1.3884198665618896f,-0.511356770992279f };
Lightvalve 219:3cb6bd063f56 265
Lightvalve 219:3cb6bd063f56 266 const float bout[1] = { 0.09115694463253021f };
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 218:066030f7951f 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 218:066030f7951f 805
Lightvalve 218:066030f7951f 806 for (int i=0; i<size; i++) {
Lightvalve 179:d5377766d7ea 807 add += x[i];
Lightvalve 179:d5377766d7ea 808 }
Lightvalve 179:d5377766d7ea 809 result = (float) add/size;
Lightvalve 179:d5377766d7ea 810 return result;
Lightvalve 179:d5377766d7ea 811 }
Lightvalve 179:d5377766d7ea 812 float deviation_adv(float x[], int size)
Lightvalve 179:d5377766d7ea 813 {
Lightvalve 179:d5377766d7ea 814 float sigma = 0.0f;
Lightvalve 179:d5377766d7ea 815 float resultDeb = 0.0f;
Lightvalve 218:066030f7951f 816
Lightvalve 218:066030f7951f 817 for (int k=0; k<size; k++) {
Lightvalve 179:d5377766d7ea 818 sigma = pow((float)x[k]-mean_adv(x,size), (float)2.0f)/(size-1);
Lightvalve 179:d5377766d7ea 819 resultDeb += sqrt(sigma);
Lightvalve 179:d5377766d7ea 820 }
Lightvalve 179:d5377766d7ea 821 return resultDeb;
Lightvalve 218:066030f7951f 822 }
Lightvalve 218:066030f7951f 823
Lightvalve 173:68c7914679ec 824
Lightvalve 170:42c938a40313 825 void Overwirte_Critic_Networks()
Lightvalve 170:42c938a40313 826 {
Lightvalve 173:68c7914679ec 827 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 828 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 829 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 170:42c938a40313 830 }
Lightvalve 170:42c938a40313 831 bc1[index2] = bc1_temp[index2];
Lightvalve 170:42c938a40313 832 }
Lightvalve 173:68c7914679ec 833 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 834 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 835 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 173:68c7914679ec 836 }
Lightvalve 173:68c7914679ec 837 bc2[index2] = bc2_temp[index2];
Lightvalve 173:68c7914679ec 838 hc3[index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 839 }
Lightvalve 173:68c7914679ec 840 bc3 = bc3_temp;
Lightvalve 170:42c938a40313 841 }
Lightvalve 170:42c938a40313 842 void Overwirte_Actor_Networks()
Lightvalve 170:42c938a40313 843 {
Lightvalve 173:68c7914679ec 844 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 845 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 846 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 170:42c938a40313 847 }
Lightvalve 170:42c938a40313 848 ba1[index2] = ba1_temp[index2];
Lightvalve 170:42c938a40313 849 }
Lightvalve 173:68c7914679ec 850 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 851 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:42c938a40313 852 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 170:42c938a40313 853 }
Lightvalve 170:42c938a40313 854 ba2[index2] = ba2_temp[index2];
Lightvalve 170:42c938a40313 855 }
Lightvalve 173:68c7914679ec 856 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 857 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 858 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 173:68c7914679ec 859 }
Lightvalve 173:68c7914679ec 860 ba3[index2] = ba3_temp[index2];
Lightvalve 173:68c7914679ec 861 }
Lightvalve 170:42c938a40313 862 }
Lightvalve 170:42c938a40313 863
Lightvalve 170:42c938a40313 864
GiJeongKim 0:51c43836c1d7 865 int main()
GiJeongKim 0:51c43836c1d7 866 {
Lightvalve 66:a8e6799dbce3 867
Lightvalve 65:a2d7c63419c2 868 HAL_Init();
Lightvalve 65:a2d7c63419c2 869 SystemClock_Config();
Lightvalve 169:645207e160ca 870
jobuuu 6:df07d3491e3a 871 /*********************************
jobuuu 1:e04e563be5ce 872 *** Initialization
jobuuu 6:df07d3491e3a 873 *********************************/
Lightvalve 69:3995ffeaa786 874 LED = 0;
Lightvalve 61:bc8c8270f0ab 875 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 876
GiJeongKim 0:51c43836c1d7 877 // i2c init
Lightvalve 8:5d2eebdad025 878 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 879 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 880 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 881 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 882 make_delay();
jobuuu 2:a1c0a37df760 883
GiJeongKim 0:51c43836c1d7 884 // // spi init
Lightvalve 170:42c938a40313 885 eeprom.format(8,3);
Lightvalve 170:42c938a40313 886 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 887 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 888 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 889 make_delay();
Lightvalve 21:e5f1a43ea6f9 890
Lightvalve 16:903b5a4433b4 891 //rom
Lightvalve 19:23b7c1ad8683 892 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 893 make_delay();
Lightvalve 13:747daba9cf59 894
GiJeongKim 0:51c43836c1d7 895 // ADC init
jobuuu 5:a4319f79457b 896 Init_ADC();
Lightvalve 11:82d8768d7351 897 make_delay();
jobuuu 2:a1c0a37df760 898
GiJeongKim 0:51c43836c1d7 899 // Pwm init
GiJeongKim 0:51c43836c1d7 900 Init_PWM();
GiJeongKim 0:51c43836c1d7 901 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 902 make_delay();
Lightvalve 13:747daba9cf59 903
Lightvalve 11:82d8768d7351 904 // TMR3 init
Lightvalve 11:82d8768d7351 905 Init_TMR3();
Lightvalve 11:82d8768d7351 906 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 907 make_delay();
Lightvalve 21:e5f1a43ea6f9 908
Lightvalve 50:3c630b5eba9f 909 // TMR2 init
Lightvalve 56:6f50d9d3bfee 910 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 911 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 912 // make_delay();
Lightvalve 21:e5f1a43ea6f9 913
GiJeongKim 0:51c43836c1d7 914 // CAN
jobuuu 2:a1c0a37df760 915 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 916 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 917 make_delay();
Lightvalve 34:bb2ca2fc2a8e 918
Lightvalve 23:59218d4a256d 919 //Timer priority
Lightvalve 23:59218d4a256d 920 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 57:f4819de54e7a 921 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 922 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 923
Lightvalve 23:59218d4a256d 924 //can.reset();
Lightvalve 19:23b7c1ad8683 925 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 926
GiJeongKim 0:51c43836c1d7 927 // spi _ enc
GiJeongKim 0:51c43836c1d7 928 spi_enc_set_init();
Lightvalve 11:82d8768d7351 929 make_delay();
Lightvalve 13:747daba9cf59 930
Lightvalve 11:82d8768d7351 931 //DAC init
Lightvalve 58:2eade98630e2 932 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 933 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 934 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 935 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 936 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 58:2eade98630e2 937 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 58:2eade98630e2 938 }
Lightvalve 11:82d8768d7351 939 make_delay();
Lightvalve 13:747daba9cf59 940
Lightvalve 19:23b7c1ad8683 941 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 942 if(i%2==0)
Lightvalve 38:118df027d851 943 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 944 else
Lightvalve 38:118df027d851 945 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 946 }
Lightvalve 169:645207e160ca 947
Lightvalve 173:68c7914679ec 948 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 949 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 950 hc1_temp[index1][index2] = (float) (rand()%100) * 0.007f ;
Lightvalve 170:42c938a40313 951 }
Lightvalve 179:d5377766d7ea 952 bc1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 953 }
Lightvalve 173:68c7914679ec 954 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 955 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 956 hc2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 957 }
Lightvalve 179:d5377766d7ea 958 bc2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 179:d5377766d7ea 959 hc3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 960 }
Lightvalve 179:d5377766d7ea 961 bc3_temp = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 962
Lightvalve 173:68c7914679ec 963 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 964 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 179:d5377766d7ea 965 ha1_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 966 }
Lightvalve 179:d5377766d7ea 967 ba1_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 968 }
Lightvalve 173:68c7914679ec 969 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 970 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 179:d5377766d7ea 971 ha2_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 173:68c7914679ec 972 }
Lightvalve 179:d5377766d7ea 973 ba2_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 974 }
Lightvalve 170:42c938a40313 975 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 976 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 179:d5377766d7ea 977 ha3_temp[index1][index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 978 }
Lightvalve 179:d5377766d7ea 979 ba3_temp[index2] = (float) (rand()%100) * 0.007f;
Lightvalve 170:42c938a40313 980 }
Lightvalve 171:bfc1fd2629d8 981
Lightvalve 170:42c938a40313 982 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 983 Overwirte_Actor_Networks();
Lightvalve 61:bc8c8270f0ab 984
jobuuu 6:df07d3491e3a 985 /************************************
jobuuu 1:e04e563be5ce 986 *** Program is operating!
jobuuu 6:df07d3491e3a 987 *************************************/
GiJeongKim 0:51c43836c1d7 988 while(1) {
Lightvalve 169:645207e160ca 989
Lightvalve 171:bfc1fd2629d8 990 // if(timer_while==27491) {
Lightvalve 171:bfc1fd2629d8 991 // timer_while = 0;
Lightvalve 171:bfc1fd2629d8 992 // 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 993 // //pc.printf("%f\n", virt_pos);
Lightvalve 171:bfc1fd2629d8 994 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 171:bfc1fd2629d8 995 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 171:bfc1fd2629d8 996 // }
Lightvalve 169:645207e160ca 997
Lightvalve 171:bfc1fd2629d8 998
Lightvalve 171:bfc1fd2629d8 999 //i2c
Lightvalve 180:02be1711ee0b 1000 read_field(i2c_slave_addr1);
Lightvalve 180:02be1711ee0b 1001 if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 171:bfc1fd2629d8 1002
Lightvalve 177:8e9cf31d63f4 1003 //timer_while ++;
Lightvalve 169:645207e160ca 1004
Lightvalve 170:42c938a40313 1005 ///////////////////////////////////////////////////////Neural Network
Lightvalve 169:645207e160ca 1006
Lightvalve 73:f80dc3970c99 1007 if(NN_Control_Flag == 0) {
Lightvalve 73:f80dc3970c99 1008 LED = 0;
Lightvalve 73:f80dc3970c99 1009 }
Lightvalve 169:645207e160ca 1010
Lightvalve 73:f80dc3970c99 1011 else if(NN_Control_Flag == 1) {
Lightvalve 169:645207e160ca 1012
Lightvalve 162:9dd4f35e9de8 1013 int ind = 0;
Lightvalve 162:9dd4f35e9de8 1014 for(int i=0; i<numpast_u; i++) {
Lightvalve 162:9dd4f35e9de8 1015 input_NN[ind] = u_past[time_interval*i];
Lightvalve 162:9dd4f35e9de8 1016 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1017 }
Lightvalve 162:9dd4f35e9de8 1018
Lightvalve 162:9dd4f35e9de8 1019 for(int i=0; i<numpast_x; i++) {
Lightvalve 162:9dd4f35e9de8 1020 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 162:9dd4f35e9de8 1021 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1022 }
Lightvalve 162:9dd4f35e9de8 1023 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 162:9dd4f35e9de8 1024 ind = ind + 1;
Lightvalve 169:645207e160ca 1025
Lightvalve 162:9dd4f35e9de8 1026 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 162:9dd4f35e9de8 1027 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 122:dcb3ce3056a0 1028 // ind = ind + 1;
Lightvalve 122:dcb3ce3056a0 1029 // }
Lightvalve 162:9dd4f35e9de8 1030
Lightvalve 162:9dd4f35e9de8 1031 for(int i=0; i<numpast_f; i++) {
Lightvalve 208:408f9f15c486 1032 // input_NN[ind] = f_past[time_interval*i] / 10000.0f * 8.0f + 0.5f;
Lightvalve 208:408f9f15c486 1033 input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1034 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1035 }
Lightvalve 208:408f9f15c486 1036 // input_NN[ind] = torq.sen / 10000.0f * 8.0f + 0.5f;
Lightvalve 220:153c6f5e1c44 1037 input_NN[ind] = torq.sen / 10000.0f + 0.5f;
Lightvalve 220:153c6f5e1c44 1038 ind = ind + 1;
Lightvalve 220:153c6f5e1c44 1039 for(int i=0; i<numfuture_f-1; i++) {
Lightvalve 208:408f9f15c486 1040 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f * 8.0f + 0.5f;
Lightvalve 219:3cb6bd063f56 1041 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f + 0.5f;
Lightvalve 206:2e4d0c287578 1042 // input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f*8.0f+0.5f;
Lightvalve 219:3cb6bd063f56 1043 input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1044 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1045 }
Lightvalve 169:645207e160ca 1046
Lightvalve 112:8dcb1600cb90 1047 float output1[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1048 float output2[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1049 float output3[16] = { 0.0f };
Lightvalve 66:a8e6799dbce3 1050 float output = 0.0f;
Lightvalve 65:a2d7c63419c2 1051
Lightvalve 112:8dcb1600cb90 1052 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 68:328e1be06f5d 1053 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 66:a8e6799dbce3 1054 output1[index2] = output1[index2]
Lightvalve 73:f80dc3970c99 1055 + h1[index1][index2] * input_NN[index1];
Lightvalve 66:a8e6799dbce3 1056 }
Lightvalve 66:a8e6799dbce3 1057 output1[index2] = output1[index2] + b1[index2];
Lightvalve 66:a8e6799dbce3 1058 if (output1[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1059 output1[index2] = 0;
Lightvalve 66:a8e6799dbce3 1060 }
Lightvalve 66:a8e6799dbce3 1061 }
Lightvalve 65:a2d7c63419c2 1062
Lightvalve 112:8dcb1600cb90 1063 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1064 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1065 output2[index2] = output2[index2]
Lightvalve 66:a8e6799dbce3 1066 + h2[index1][index2] * output1[index1];
Lightvalve 66:a8e6799dbce3 1067 }
Lightvalve 66:a8e6799dbce3 1068 output2[index2] = output2[index2] + b2[index2];
Lightvalve 66:a8e6799dbce3 1069 if (output2[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1070 output2[index2] = 0;
Lightvalve 66:a8e6799dbce3 1071 }
Lightvalve 66:a8e6799dbce3 1072 }
Lightvalve 65:a2d7c63419c2 1073
Lightvalve 112:8dcb1600cb90 1074 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1075 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1076 output3[index2] = output3[index2]
Lightvalve 66:a8e6799dbce3 1077 + h3[index1][index2] * output2[index1];
Lightvalve 66:a8e6799dbce3 1078 }
Lightvalve 66:a8e6799dbce3 1079 output3[index2] = output3[index2] + b3[index2];
Lightvalve 66:a8e6799dbce3 1080 if (output3[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1081 output3[index2] = 0;
Lightvalve 66:a8e6799dbce3 1082 }
Lightvalve 65:a2d7c63419c2 1083 }
Lightvalve 66:a8e6799dbce3 1084
Lightvalve 66:a8e6799dbce3 1085 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 112:8dcb1600cb90 1086 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 73:f80dc3970c99 1087 output = output + hout[index1] * output3[index1];
Lightvalve 66:a8e6799dbce3 1088 }
Lightvalve 66:a8e6799dbce3 1089 output = output + bout[index2];
Lightvalve 169:645207e160ca 1090
Lightvalve 66:a8e6799dbce3 1091 }
Lightvalve 73:f80dc3970c99 1092 output = 1.0f/(1.0f+exp(-output));
Lightvalve 101:50159049a518 1093 output_normalized = output;
Lightvalve 68:328e1be06f5d 1094 output = output * 20000.0f - 10000.0f;
Lightvalve 169:645207e160ca 1095
Lightvalve 66:a8e6799dbce3 1096 if(output>=0) {
Lightvalve 66:a8e6799dbce3 1097 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 66:a8e6799dbce3 1098 } else {
Lightvalve 66:a8e6799dbce3 1099 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 65:a2d7c63419c2 1100 }
Lightvalve 87:471334725012 1101
Lightvalve 169:645207e160ca 1102
Lightvalve 69:3995ffeaa786 1103 if(LED==1) {
Lightvalve 69:3995ffeaa786 1104 LED=0;
Lightvalve 69:3995ffeaa786 1105 } else
Lightvalve 69:3995ffeaa786 1106 LED = 1;
Lightvalve 169:645207e160ca 1107
Lightvalve 65:a2d7c63419c2 1108 }
Lightvalve 171:bfc1fd2629d8 1109
Lightvalve 171:bfc1fd2629d8 1110
Lightvalve 170:42c938a40313 1111 /////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 1112 switch (Update_Case) {
Lightvalve 170:42c938a40313 1113 case 0: {
Lightvalve 170:42c938a40313 1114 break;
Lightvalve 170:42c938a40313 1115 }
Lightvalve 170:42c938a40313 1116 case 1: {
Lightvalve 170:42c938a40313 1117 //Network Update(just update and hold network)
Lightvalve 170:42c938a40313 1118 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 170:42c938a40313 1119 float loss_sum = 0.0f;
Lightvalve 175:2f7289dbd488 1120 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 170:42c938a40313 1121 //Calculate Estimated V
Lightvalve 175:2f7289dbd488 1122 //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]};
Lightvalve 175:2f7289dbd488 1123 float temp_array[2] = {state_array[n][0], state_array[n][1]};
Lightvalve 175:2f7289dbd488 1124 V[n] = Critic_Network_Temp(temp_array);
Lightvalve 173:68c7914679ec 1125 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 175:2f7289dbd488 1126 hx_c_sum_array[n][i] = hx_c_sum[i];
Lightvalve 173:68c7914679ec 1127 }
Lightvalve 173:68c7914679ec 1128 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 175:2f7289dbd488 1129 hxh_c_sum_array[n][i] = hxh_c_sum[i];
Lightvalve 173:68c7914679ec 1130 }
Lightvalve 175:2f7289dbd488 1131 hxhh_c_sum_array[n] = hxhh_c_sum;
Lightvalve 177:8e9cf31d63f4 1132
Lightvalve 175:2f7289dbd488 1133 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 1134 Actor_Network_Old(temp_array);
Lightvalve 175:2f7289dbd488 1135 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 1136 r[n] = exp(-0.25f * 5.0f * state_array[n][1] * state_array[n][1]);
Lightvalve 175:2f7289dbd488 1137 if(n == batch_size-1) return_G[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1138 else return_G[n] = gamma * return_G[n+1] + r[n];
Lightvalve 175:2f7289dbd488 1139 if(n == batch_size-1) td_target[n] = r[n];
Lightvalve 175:2f7289dbd488 1140 else td_target[n] = r[n] + gamma * V[n+1];
Lightvalve 175:2f7289dbd488 1141 delta[n] = td_target[n] - V[n];
Lightvalve 175:2f7289dbd488 1142 if(n == batch_size-1) advantage[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1143 else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n];
Lightvalve 179:d5377766d7ea 1144 // return_G[n] = advantage[n] + V[n];
Lightvalve 175:2f7289dbd488 1145 ratio[n] = pi[n]/pi_old[n];
Lightvalve 179:d5377766d7ea 1146 }
Lightvalve 179:d5377766d7ea 1147 float mean_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1148 float dev_advantage = 0.0f;
Lightvalve 179:d5377766d7ea 1149 mean_advantage = mean_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1150 dev_advantage = deviation_adv(advantage, batch_size);
Lightvalve 179:d5377766d7ea 1151 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 179:d5377766d7ea 1152 //advantage[n] = (advantage[n]-mean_advantage)/dev_advantage;
Lightvalve 175:2f7289dbd488 1153 surr1[n] = ratio[n] * advantage[n];
Lightvalve 175:2f7289dbd488 1154 if (ratio[n] > 1.0f + epsilon) {
Lightvalve 175:2f7289dbd488 1155 surr2[n] = (1.0f + epsilon)*advantage[n];
Lightvalve 175:2f7289dbd488 1156 } else if( ratio[n] < 1.0f - epsilon) {
Lightvalve 175:2f7289dbd488 1157 surr2[n] = (1.0f - epsilon)*advantage[n];
Lightvalve 170:42c938a40313 1158 } else {
Lightvalve 175:2f7289dbd488 1159 surr2[n] = ratio[n]*advantage[n];
Lightvalve 170:42c938a40313 1160 }
Lightvalve 175:2f7289dbd488 1161 loss[n] = -min(surr1[n], surr2[n]);
Lightvalve 175:2f7289dbd488 1162 loss_sum = loss_sum + loss[n];
Lightvalve 170:42c938a40313 1163 }
Lightvalve 170:42c938a40313 1164 reward_sum = 0.0f;
Lightvalve 170:42c938a40313 1165 for (int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 1166 reward_sum = reward_sum + r[i];
Lightvalve 170:42c938a40313 1167 }
Lightvalve 170:42c938a40313 1168 logging5 = reward_sum;
Lightvalve 171:bfc1fd2629d8 1169
Lightvalve 171:bfc1fd2629d8 1170
Lightvalve 170:42c938a40313 1171 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 170:42c938a40313 1172 loss_batch = loss_sum;
Lightvalve 170:42c938a40313 1173 //Update Networks
Lightvalve 170:42c938a40313 1174 update_Critic_Networks(state_array);
Lightvalve 170:42c938a40313 1175 update_Actor_Networks(state_array);
Lightvalve 170:42c938a40313 1176 }
Lightvalve 170:42c938a40313 1177 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1178 Update_Case = 0;
Lightvalve 170:42c938a40313 1179 //logging1 = V[0];
Lightvalve 171:bfc1fd2629d8 1180
Lightvalve 170:42c938a40313 1181 break;
Lightvalve 170:42c938a40313 1182 }
Lightvalve 170:42c938a40313 1183 case 2: {
Lightvalve 170:42c938a40313 1184 //Network apply to next Network
Lightvalve 170:42c938a40313 1185 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1186 Overwirte_Actor_Networks();
Lightvalve 170:42c938a40313 1187 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1188 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1189 Update_Case = 0;
Lightvalve 170:42c938a40313 1190 break;
Lightvalve 170:42c938a40313 1191 }
Lightvalve 169:645207e160ca 1192
Lightvalve 170:42c938a40313 1193 }
GiJeongKim 0:51c43836c1d7 1194 }
jobuuu 1:e04e563be5ce 1195 }
jobuuu 1:e04e563be5ce 1196
Lightvalve 33:91b17819ec30 1197 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1198 {
Lightvalve 14:8e7590227d22 1199
Lightvalve 13:747daba9cf59 1200 int i = 0;
Lightvalve 48:889798ff9329 1201 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1202 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1203 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 1204 if(i==0) {
Lightvalve 50:3c630b5eba9f 1205 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 1206 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1207 } else {
Lightvalve 57:f4819de54e7a 1208 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 1209 }
Lightvalve 14:8e7590227d22 1210 } else {
Lightvalve 50:3c630b5eba9f 1211 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 1212 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1213 } else {
Lightvalve 57:f4819de54e7a 1214 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 1215 }
Lightvalve 13:747daba9cf59 1216 }
Lightvalve 13:747daba9cf59 1217 break;
Lightvalve 13:747daba9cf59 1218 }
Lightvalve 13:747daba9cf59 1219 }
Lightvalve 14:8e7590227d22 1220 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1221 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1222 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1223 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1224 }
Lightvalve 36:a46e63505ed8 1225
Lightvalve 57:f4819de54e7a 1226 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1227 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 1228
Lightvalve 13:747daba9cf59 1229 }
jobuuu 6:df07d3491e3a 1230
jobuuu 6:df07d3491e3a 1231
Lightvalve 30:8d561f16383b 1232 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1233 {
Lightvalve 13:747daba9cf59 1234 int i = 0;
Lightvalve 13:747daba9cf59 1235
Lightvalve 38:118df027d851 1236 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1237 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1238 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1239 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1240 }
Lightvalve 38:118df027d851 1241
Lightvalve 89:a7b45368ea0f 1242 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1243 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1244 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1245 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 1246 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 1247 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1248
Lightvalve 13:747daba9cf59 1249 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 1250
Lightvalve 18:b8adf1582ea3 1251 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 1252 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 1253 if(i==0) {
Lightvalve 48:889798ff9329 1254 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 1255 } else {
Lightvalve 48:889798ff9329 1256 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 1257 }
Lightvalve 13:747daba9cf59 1258 break;
Lightvalve 13:747daba9cf59 1259 }
Lightvalve 13:747daba9cf59 1260 }
Lightvalve 59:f308b1656d9c 1261 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1262 }
Lightvalve 13:747daba9cf59 1263
Lightvalve 14:8e7590227d22 1264 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1265 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 1266 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1267 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1268 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1269 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1270 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1271 }; // duty
Lightvalve 67:c2812cf26c38 1272 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 1273 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 1274 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 1275 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 1276 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 1277 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1278 }; // mV
Lightvalve 13:747daba9cf59 1279
Lightvalve 30:8d561f16383b 1280 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1281 {
Lightvalve 30:8d561f16383b 1282 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1283 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1284 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1285 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1286 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1287 } else {
Lightvalve 13:747daba9cf59 1288 int idx = 0;
Lightvalve 13:747daba9cf59 1289 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1290 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1291 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1292 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1293 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1294 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1295 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1296 break;
Lightvalve 13:747daba9cf59 1297 }
Lightvalve 13:747daba9cf59 1298 }
Lightvalve 13:747daba9cf59 1299 }
Lightvalve 14:8e7590227d22 1300
Lightvalve 13:747daba9cf59 1301 return PWM_duty;
Lightvalve 13:747daba9cf59 1302 }
jobuuu 6:df07d3491e3a 1303
Lightvalve 57:f4819de54e7a 1304
Lightvalve 57:f4819de54e7a 1305
Lightvalve 57:f4819de54e7a 1306
Lightvalve 57:f4819de54e7a 1307
jobuuu 2:a1c0a37df760 1308 /*******************************************************************************
jobuuu 2:a1c0a37df760 1309 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1310 *******************************************************************************/
jobuuu 2:a1c0a37df760 1311
Lightvalve 51:b46bed7fec80 1312 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 1313 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 1314 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 1315 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1316 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1317 {
Lightvalve 19:23b7c1ad8683 1318 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1319
Lightvalve 21:e5f1a43ea6f9 1320 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1321 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1322 ********************************************************/
Lightvalve 13:747daba9cf59 1323
Lightvalve 57:f4819de54e7a 1324 //Encoder
Lightvalve 57:f4819de54e7a 1325 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 1326 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 1327 }
Lightvalve 61:bc8c8270f0ab 1328
Lightvalve 61:bc8c8270f0ab 1329 ADC1->CR2 |= 0x40000000;
Lightvalve 58:2eade98630e2 1330 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1331 // Torque Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1332 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 58:2eade98630e2 1333 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 1334 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 1335 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 1336
Lightvalve 67:c2812cf26c38 1337
Lightvalve 67:c2812cf26c38 1338 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 67:c2812cf26c38 1339 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 67:c2812cf26c38 1340 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 67:c2812cf26c38 1341 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 67:c2812cf26c38 1342 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 67:c2812cf26c38 1343
Lightvalve 17:1865016ca2e7 1344
Lightvalve 58:2eade98630e2 1345 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1346 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1347 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 58:2eade98630e2 1348 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 58:2eade98630e2 1349 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 58:2eade98630e2 1350 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 1351 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 1352 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 1353 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 1354
Lightvalve 58:2eade98630e2 1355 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 1356 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 1357 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 1358 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 1359 }
Lightvalve 58:2eade98630e2 1360 }
Lightvalve 61:bc8c8270f0ab 1361
Lightvalve 58:2eade98630e2 1362 // //Pressure sensor A
Lightvalve 58:2eade98630e2 1363 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 58:2eade98630e2 1364 // //while((ADC1->SR & 0b10));
Lightvalve 58:2eade98630e2 1365 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1366 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 58:2eade98630e2 1367 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 58:2eade98630e2 1368 // 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 1369 //
Lightvalve 58:2eade98630e2 1370 //
Lightvalve 58:2eade98630e2 1371 // //Pressure sensor B
Lightvalve 58:2eade98630e2 1372 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1373 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 58:2eade98630e2 1374 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 58:2eade98630e2 1375 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1376
Lightvalve 17:1865016ca2e7 1377
Lightvalve 21:e5f1a43ea6f9 1378 //Current
Lightvalve 21:e5f1a43ea6f9 1379 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1380 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1381 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 1382 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1383 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1384 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1385 //cur.sen = raw_cur;
Lightvalve 58:2eade98630e2 1386
Lightvalve 57:f4819de54e7a 1387 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1388 }
Lightvalve 11:82d8768d7351 1389 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1390 }
Lightvalve 19:23b7c1ad8683 1391
Lightvalve 19:23b7c1ad8683 1392
Lightvalve 18:b8adf1582ea3 1393 int j =0;
Lightvalve 54:647072f5307a 1394 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 1395 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 1396 int cnt_trans = 0;
Lightvalve 48:889798ff9329 1397 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 1398 int can_rest =0;
Lightvalve 48:889798ff9329 1399
Lightvalve 11:82d8768d7351 1400 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1401 {
Lightvalve 19:23b7c1ad8683 1402 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 1403
Lightvalve 57:f4819de54e7a 1404 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 1405 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1406 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 1407 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1408 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 1409 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 1410 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1411 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 1412 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1413 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 1414 }
Lightvalve 50:3c630b5eba9f 1415
Lightvalve 50:3c630b5eba9f 1416 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 1417 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1418 cnt_trans++;
Lightvalve 46:2694daea349b 1419 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1420 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 1421 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 1422 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 1423 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1424 cnt_trans++;
Lightvalve 46:2694daea349b 1425 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1426 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 1427 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 1428 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 1429 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 1430 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 1431 } else {
Lightvalve 58:2eade98630e2 1432 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 1433 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 1434 }
Lightvalve 45:35fa6884d0c6 1435
Lightvalve 50:3c630b5eba9f 1436
Lightvalve 57:f4819de54e7a 1437 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 1438 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 1439
Lightvalve 57:f4819de54e7a 1440 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 1441 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1442 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1443 } else {
Lightvalve 57:f4819de54e7a 1444 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1445 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1446 }
Lightvalve 56:6f50d9d3bfee 1447
Lightvalve 56:6f50d9d3bfee 1448
Lightvalve 56:6f50d9d3bfee 1449
Lightvalve 57:f4819de54e7a 1450 // UTILITY MODE ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1451
Lightvalve 57:f4819de54e7a 1452 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 1453 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1454 break;
Lightvalve 13:747daba9cf59 1455 }
Lightvalve 14:8e7590227d22 1456
Lightvalve 14:8e7590227d22 1457 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1458 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1459 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1460 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1461
Lightvalve 14:8e7590227d22 1462 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1463 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1464 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1465
Lightvalve 84:c355d3e52bf1 1466 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1467
Lightvalve 30:8d561f16383b 1468 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 1469 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 50:3c630b5eba9f 1470
Lightvalve 16:903b5a4433b4 1471 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1472 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1473 }
Lightvalve 13:747daba9cf59 1474 } else {
Lightvalve 58:2eade98630e2 1475 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1476 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1477 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1478 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1479
Lightvalve 170:42c938a40313 1480 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1481 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1482
Lightvalve 30:8d561f16383b 1483 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1484
Lightvalve 13:747daba9cf59 1485 }
Lightvalve 14:8e7590227d22 1486 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1487 break;
Lightvalve 19:23b7c1ad8683 1488 }
Lightvalve 14:8e7590227d22 1489
Lightvalve 50:3c630b5eba9f 1490 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 1491 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 1492 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1493 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 1494 // }
Lightvalve 50:3c630b5eba9f 1495 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1496 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1497 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1498 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1499 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1500 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1501 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1502 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1503 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1504 // }
Lightvalve 50:3c630b5eba9f 1505 //
Lightvalve 50:3c630b5eba9f 1506 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1507 // 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 1508 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 1509 //
Lightvalve 50:3c630b5eba9f 1510 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 1511 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1512 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1513 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1514 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1515 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1516 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 1517 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1518 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1519 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1520 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1521 //
Lightvalve 50:3c630b5eba9f 1522 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 1523 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 1524 // // | / | / |/
Lightvalve 50:3c630b5eba9f 1525 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 1526 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 1527 // // /| / | / |
Lightvalve 50:3c630b5eba9f 1528 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 1529 //
Lightvalve 50:3c630b5eba9f 1530 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 1531 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 1532 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1533 // } else {
Lightvalve 50:3c630b5eba9f 1534 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 1535 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1536 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1537 // }
Lightvalve 50:3c630b5eba9f 1538 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 1539 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1540 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1541 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 1542 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1543 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1544 // }
Lightvalve 50:3c630b5eba9f 1545 // } else {
Lightvalve 50:3c630b5eba9f 1546 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 1547 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 1548 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 1549 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1550 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1551 // }
Lightvalve 50:3c630b5eba9f 1552 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1553 //
Lightvalve 50:3c630b5eba9f 1554 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1555 //
Lightvalve 50:3c630b5eba9f 1556 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 1557 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 1558 //
Lightvalve 50:3c630b5eba9f 1559 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1560 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1561 // }
Lightvalve 50:3c630b5eba9f 1562 // }
Lightvalve 50:3c630b5eba9f 1563 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 1564 // break;
Lightvalve 50:3c630b5eba9f 1565 // }
Lightvalve 14:8e7590227d22 1566
Lightvalve 14:8e7590227d22 1567 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1568 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1569 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1570 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1571 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 59:f308b1656d9c 1572 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 1573 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1574 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1575 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 1576 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1577 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1578 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1579 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1580 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1581 }
Lightvalve 29:69f3f5445d6d 1582 cnt_findhome++;
Lightvalve 14:8e7590227d22 1583
Lightvalve 29:69f3f5445d6d 1584 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1585 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1586 } else {
Lightvalve 29:69f3f5445d6d 1587 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1588 }
Lightvalve 19:23b7c1ad8683 1589
Lightvalve 57:f4819de54e7a 1590 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1591 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 67:c2812cf26c38 1592 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 67:c2812cf26c38 1593 else pos.ref = pos.ref - 12.0f;
Lightvalve 61:bc8c8270f0ab 1594
Lightvalve 59:f308b1656d9c 1595 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 59:f308b1656d9c 1596 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1597 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 59:f308b1656d9c 1598 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1599 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 1600
Lightvalve 59:f308b1656d9c 1601 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 1602 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1603
Lightvalve 34:bb2ca2fc2a8e 1604
Lightvalve 29:69f3f5445d6d 1605 } else {
Lightvalve 29:69f3f5445d6d 1606 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 67:c2812cf26c38 1607 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1608 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1609 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1610 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1611 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1612 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1613 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1614 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1615 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1616 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 1617
Lightvalve 67:c2812cf26c38 1618
Lightvalve 67:c2812cf26c38 1619 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 1620 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1621 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1622 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1623 vel.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1624 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 67:c2812cf26c38 1625 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 169:645207e160ca 1626
Lightvalve 169:645207e160ca 1627
Lightvalve 29:69f3f5445d6d 1628 }
Lightvalve 29:69f3f5445d6d 1629 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1630 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 1631 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 1632 //pos.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1633 vel.ref = 0.0f;
Lightvalve 169:645207e160ca 1634
Lightvalve 29:69f3f5445d6d 1635 // input for position control
Lightvalve 169:645207e160ca 1636
Lightvalve 67:c2812cf26c38 1637 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 1638 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1639
Lightvalve 67:c2812cf26c38 1640 double torq_ref = 0.0f;
Lightvalve 67:c2812cf26c38 1641 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 1642 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 1643 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 67:c2812cf26c38 1644
Lightvalve 67:c2812cf26c38 1645 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 67:c2812cf26c38 1646
Lightvalve 67:c2812cf26c38 1647 double I_REF_POS = 0.0f;
Lightvalve 67:c2812cf26c38 1648 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 67:c2812cf26c38 1649 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 67:c2812cf26c38 1650
Lightvalve 67:c2812cf26c38 1651 double temp_vel_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1652 double temp_vel_torq = 0.0f;
Lightvalve 67:c2812cf26c38 1653 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 67:c2812cf26c38 1654
Lightvalve 69:3995ffeaa786 1655 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 67:c2812cf26c38 1656 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 1657 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 1658 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 67:c2812cf26c38 1659 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 67:c2812cf26c38 1660 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:c2812cf26c38 1661 }
Lightvalve 67:c2812cf26c38 1662 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 1663 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1664
Lightvalve 67:c2812cf26c38 1665 I_REF = I_REF_POS;
Lightvalve 67:c2812cf26c38 1666
Lightvalve 67:c2812cf26c38 1667
Lightvalve 67:c2812cf26c38 1668
Lightvalve 67:c2812cf26c38 1669 } else {
Lightvalve 67:c2812cf26c38 1670 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 67:c2812cf26c38 1671 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 1672
Lightvalve 67:c2812cf26c38 1673 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 67:c2812cf26c38 1674 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 67:c2812cf26c38 1675 } else {
Lightvalve 67:c2812cf26c38 1676 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 67:c2812cf26c38 1677 }
Lightvalve 67:c2812cf26c38 1678
Lightvalve 67:c2812cf26c38 1679 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 67:c2812cf26c38 1680
Lightvalve 67:c2812cf26c38 1681 V_out = (float) Vout.ref;
Lightvalve 67:c2812cf26c38 1682
Lightvalve 67:c2812cf26c38 1683 }
Lightvalve 67:c2812cf26c38 1684
Lightvalve 67:c2812cf26c38 1685
Lightvalve 169:645207e160ca 1686
Lightvalve 169:645207e160ca 1687
Lightvalve 59:f308b1656d9c 1688 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 59:f308b1656d9c 1689 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1690 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 1691 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1692 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1693
Lightvalve 29:69f3f5445d6d 1694 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1695 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1696 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1697 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1698 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1699 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1700 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1701 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1702 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 1703 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1704 }
Lightvalve 13:747daba9cf59 1705 }
Lightvalve 19:23b7c1ad8683 1706
Lightvalve 13:747daba9cf59 1707 break;
Lightvalve 13:747daba9cf59 1708 }
Lightvalve 14:8e7590227d22 1709
Lightvalve 50:3c630b5eba9f 1710 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1711 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1712 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1713 // else {
Lightvalve 50:3c630b5eba9f 1714 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1715 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1716 // }
Lightvalve 50:3c630b5eba9f 1717 // }
Lightvalve 50:3c630b5eba9f 1718 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1719 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1720 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1721 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1722 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1723 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1724 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1725 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1726 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1727 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1728 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1729 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1730 // }
Lightvalve 50:3c630b5eba9f 1731 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1732 // }
Lightvalve 50:3c630b5eba9f 1733 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1734 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1735 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1736 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1737 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1738 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1739 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1740 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1741 // 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 1742 // // 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 1743 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1744 // }
Lightvalve 50:3c630b5eba9f 1745 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1746 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1747 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1748 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1749 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1750 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1751 // 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 1752 // // 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 1753 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1754 // }
Lightvalve 50:3c630b5eba9f 1755 // }
Lightvalve 50:3c630b5eba9f 1756 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1757 //
Lightvalve 50:3c630b5eba9f 1758 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1759 //
Lightvalve 50:3c630b5eba9f 1760 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1761 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1762 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1763 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1764 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1765 // }
Lightvalve 50:3c630b5eba9f 1766 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1767 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1768 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1769 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1770 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1771 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1772 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1773 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1774 // }
Lightvalve 50:3c630b5eba9f 1775 //
Lightvalve 50:3c630b5eba9f 1776 // }
Lightvalve 50:3c630b5eba9f 1777 // break;
Lightvalve 50:3c630b5eba9f 1778 // }
Lightvalve 50:3c630b5eba9f 1779 //
Lightvalve 50:3c630b5eba9f 1780 // }
Lightvalve 14:8e7590227d22 1781 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1782 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1783 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1784 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1785 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1786
Lightvalve 14:8e7590227d22 1787 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1788 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1789 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1790 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1791 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1792
Lightvalve 38:118df027d851 1793 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 1794 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 1795 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1796
Lightvalve 30:8d561f16383b 1797 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1798 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1799 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1800 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1801
Lightvalve 30:8d561f16383b 1802 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1803 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1804 }
Lightvalve 13:747daba9cf59 1805 } else {
Lightvalve 57:f4819de54e7a 1806 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1807 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1808 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1809 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1810 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1811 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1812
Lightvalve 170:42c938a40313 1813 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1814 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 1815 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1816
Lightvalve 30:8d561f16383b 1817 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1818 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1819 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1820 }
Lightvalve 14:8e7590227d22 1821 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1822 break;
Lightvalve 13:747daba9cf59 1823 }
Lightvalve 14:8e7590227d22 1824
Lightvalve 50:3c630b5eba9f 1825 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1826 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1827 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1828 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1829 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1830 // }
Lightvalve 50:3c630b5eba9f 1831 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1832 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1833 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1834 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1835 // }
Lightvalve 50:3c630b5eba9f 1836 // } else {
Lightvalve 50:3c630b5eba9f 1837 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1838 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1839 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1840 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1841 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1842 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1843 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1844 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1845 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1846 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1847 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1848 //
Lightvalve 50:3c630b5eba9f 1849 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1850 //
Lightvalve 50:3c630b5eba9f 1851 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1852 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1853 // }
Lightvalve 50:3c630b5eba9f 1854 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1855 // break;
Lightvalve 50:3c630b5eba9f 1856 // }
Lightvalve 19:23b7c1ad8683 1857
Lightvalve 50:3c630b5eba9f 1858 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1859 // 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 1860 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1861 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1862 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1863 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1864 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1865 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 1866 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 1867 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1868 // }
Lightvalve 50:3c630b5eba9f 1869 // break;
Lightvalve 50:3c630b5eba9f 1870 // }
Lightvalve 14:8e7590227d22 1871
Lightvalve 14:8e7590227d22 1872 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 59:f308b1656d9c 1873 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1874 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1875
Lightvalve 14:8e7590227d22 1876 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 60:64181f1d3e60 1877 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1878 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 59:f308b1656d9c 1879 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1880 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1881 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1882 data_num = 0;
Lightvalve 14:8e7590227d22 1883 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1884 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1885 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1886 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 59:f308b1656d9c 1887 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1888 } else {
Lightvalve 13:747daba9cf59 1889 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1890 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1891 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1892 }
Lightvalve 14:8e7590227d22 1893
Lightvalve 17:1865016ca2e7 1894 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1895 int i;
Lightvalve 13:747daba9cf59 1896 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1897 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1898 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1899 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1900 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1901 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1902 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1903 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1904 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1905 }
Lightvalve 13:747daba9cf59 1906 }
Lightvalve 170:42c938a40313 1907 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1908 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 1909 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 170:42c938a40313 1910 for(int i=0; i<25; i++) {
Lightvalve 170:42c938a40313 1911 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 170:42c938a40313 1912 }
Lightvalve 13:747daba9cf59 1913 ID_index = 0;
Lightvalve 57:f4819de54e7a 1914 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1915 }
Lightvalve 14:8e7590227d22 1916
Lightvalve 14:8e7590227d22 1917
Lightvalve 13:747daba9cf59 1918 break;
Lightvalve 13:747daba9cf59 1919 }
Lightvalve 14:8e7590227d22 1920
Lightvalve 14:8e7590227d22 1921 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 59:f308b1656d9c 1922 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 1923 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 1924 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1925 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1926 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1927 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1928 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1929 pos_plus_end = pos.sen;
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 30:8d561f16383b 1932 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1933 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1934 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 1935 } else if(VALVE_DZ_timer < (int) (3.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 30:8d561f16383b 1937 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1938 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 1939 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1940 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 1941 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 1942 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 1943 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 1944 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 1945 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1946 data_num = 0;
Lightvalve 14:8e7590227d22 1947
Lightvalve 30:8d561f16383b 1948 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1949 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1950 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1951
Lightvalve 30:8d561f16383b 1952 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1953 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1954 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1955
Lightvalve 30:8d561f16383b 1956 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1957 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1958 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1959 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1960
Lightvalve 60:64181f1d3e60 1961 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 1962 DZ_case = 1;
Lightvalve 60:64181f1d3e60 1963 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 1964 DZ_case = -1;
Lightvalve 14:8e7590227d22 1965 } else {
Lightvalve 13:747daba9cf59 1966 DZ_case = 0;
Lightvalve 13:747daba9cf59 1967 }
Lightvalve 61:bc8c8270f0ab 1968
Lightvalve 60:64181f1d3e60 1969 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 1970
Lightvalve 13:747daba9cf59 1971 first_check = 1;
Lightvalve 13:747daba9cf59 1972 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1973 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1974 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 1975 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 1976 DZ_index = 1;
Lightvalve 14:8e7590227d22 1977
Lightvalve 13:747daba9cf59 1978 }
Lightvalve 19:23b7c1ad8683 1979 } else {
Lightvalve 14:8e7590227d22 1980 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 1981 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 1982 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 1983 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 1984 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 1985 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1986 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1987 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1988 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1989 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1990 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1991 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1992 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1993 }
Lightvalve 14:8e7590227d22 1994 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1995
Lightvalve 30:8d561f16383b 1996 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1997 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1998 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1999
Lightvalve 14:8e7590227d22 2000 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2001 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2002 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2003 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2004 } else {
Lightvalve 13:747daba9cf59 2005 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 2006 }
Lightvalve 14:8e7590227d22 2007
Lightvalve 13:747daba9cf59 2008 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2009 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2010 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2011 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2012 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2013 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2014 DZ_index = 1;
Lightvalve 13:747daba9cf59 2015 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2016 }
Lightvalve 13:747daba9cf59 2017 }
Lightvalve 14:8e7590227d22 2018 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 2019 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2020 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 2021 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2022 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2023 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2024 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2025 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2026 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2027 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2028 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2029 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2030 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2031 }
Lightvalve 14:8e7590227d22 2032 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2033
Lightvalve 30:8d561f16383b 2034 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2035 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2036 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2037 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2038 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2039
Lightvalve 14:8e7590227d22 2040 if((FINAL_POS - START_POS)>100) {
Lightvalve 60:64181f1d3e60 2041 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2042 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2043 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2044 } else {
Lightvalve 60:64181f1d3e60 2045 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2046 }
Lightvalve 14:8e7590227d22 2047
Lightvalve 13:747daba9cf59 2048 VALVE_DZ_timer = 0;
Lightvalve 60:64181f1d3e60 2049 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2050 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2051 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2052 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2053 first_check = 0;
Lightvalve 33:91b17819ec30 2054 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2055 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2056
Lightvalve 170:42c938a40313 2057 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2058 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2059 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2060 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2061
Lightvalve 60:64181f1d3e60 2062 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2063 DZ_index = 1;
Lightvalve 13:747daba9cf59 2064 }
Lightvalve 13:747daba9cf59 2065 }
Lightvalve 14:8e7590227d22 2066 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2067 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2068 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 2069 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2070 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2071 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2072 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2073 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2074 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2075 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2076 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2077 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2078 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2079 }
Lightvalve 14:8e7590227d22 2080 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2081
Lightvalve 30:8d561f16383b 2082 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2083 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2084 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2085
Lightvalve 14:8e7590227d22 2086 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2087 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2088 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2089 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2090 } else {
Lightvalve 13:747daba9cf59 2091 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2092 }
Lightvalve 13:747daba9cf59 2093 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2094 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2095 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2096 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2097 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2098 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2099 DZ_index = 1;
Lightvalve 13:747daba9cf59 2100 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2101 }
Lightvalve 13:747daba9cf59 2102 }
Lightvalve 14:8e7590227d22 2103 } else {
Lightvalve 30:8d561f16383b 2104 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2105 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 2106 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2107 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2108 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2109 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2110 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2111 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2112 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2113 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2114 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2115 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2116 }
Lightvalve 14:8e7590227d22 2117 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2118
Lightvalve 30:8d561f16383b 2119 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2120 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2121 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2122 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2123 FINAL_POS = pos.sen;
Lightvalve 61:bc8c8270f0ab 2124
Lightvalve 60:64181f1d3e60 2125 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2126 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2127 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2128 DZ_DIRECTION = -1;
Lightvalve 60:64181f1d3e60 2129 } else {
Lightvalve 13:747daba9cf59 2130 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2131 }
Lightvalve 14:8e7590227d22 2132
Lightvalve 13:747daba9cf59 2133 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2134 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2135 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2136 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2137 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2138 first_check = 0;
Lightvalve 33:91b17819ec30 2139 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2140 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 61:bc8c8270f0ab 2141
Lightvalve 170:42c938a40313 2142 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2143 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2144 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2145 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2146
Lightvalve 57:f4819de54e7a 2147 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2148 DZ_index = 1;
Lightvalve 13:747daba9cf59 2149 }
Lightvalve 13:747daba9cf59 2150 }
Lightvalve 13:747daba9cf59 2151 }
Lightvalve 14:8e7590227d22 2152 }
Lightvalve 13:747daba9cf59 2153 break;
Lightvalve 13:747daba9cf59 2154 }
Lightvalve 14:8e7590227d22 2155
Lightvalve 14:8e7590227d22 2156 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 59:f308b1656d9c 2157 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2158 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2159 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2160 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2161 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2162 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2163 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2164 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2165 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2166 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2167 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2168 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2169 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2170 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 59:f308b1656d9c 2171 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2172 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2173 first_check = 1;
Lightvalve 13:747daba9cf59 2174 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2175 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2176 ID_index = 0;
Lightvalve 13:747daba9cf59 2177 max_check = 0;
Lightvalve 13:747daba9cf59 2178 min_check = 0;
Lightvalve 13:747daba9cf59 2179 }
Lightvalve 14:8e7590227d22 2180 } else {
Lightvalve 30:8d561f16383b 2181 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2182 //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 2183 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 59:f308b1656d9c 2184 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2185 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2186 data_num = 0;
Lightvalve 57:f4819de54e7a 2187 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2188
Lightvalve 14:8e7590227d22 2189 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2190 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2191 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f4819de54e7a 2192 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2193 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2194 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2195 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2196 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2197 one_period_end = 1;
Lightvalve 13:747daba9cf59 2198 }
Lightvalve 30:8d561f16383b 2199 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2200 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2201 one_period_end = 1;
Lightvalve 59:f308b1656d9c 2202 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2203 }
Lightvalve 14:8e7590227d22 2204
Lightvalve 14:8e7590227d22 2205 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2206 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2207 max_check = 1;
Lightvalve 14:8e7590227d22 2208 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2209 min_check = 1;
Lightvalve 13:747daba9cf59 2210 }
Lightvalve 13:747daba9cf59 2211 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2212
Lightvalve 13:747daba9cf59 2213 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2214 one_period_end = 0;
Lightvalve 13:747daba9cf59 2215 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2216 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2217 }
Lightvalve 14:8e7590227d22 2218
Lightvalve 14:8e7590227d22 2219 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2220
Lightvalve 13:747daba9cf59 2221 VALVE_POS_NUM = ID_index;
Lightvalve 170:42c938a40313 2222 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2223 for(int i=0; i<100; i++) {
Lightvalve 170:42c938a40313 2224 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 170:42c938a40313 2225 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 170:42c938a40313 2226 }
Lightvalve 13:747daba9cf59 2227 ID_index = 0;
Lightvalve 13:747daba9cf59 2228 first_check = 0;
Lightvalve 13:747daba9cf59 2229 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2230 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 2231 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2232 }
Lightvalve 13:747daba9cf59 2233 }
Lightvalve 13:747daba9cf59 2234 break;
Lightvalve 13:747daba9cf59 2235 }
Lightvalve 58:2eade98630e2 2236
Lightvalve 57:f4819de54e7a 2237 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 2238 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 2239 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 2240 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 2241 cnt_sysid++;
Lightvalve 57:f4819de54e7a 2242 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 2243 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 2244 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 2245 }
Lightvalve 57:f4819de54e7a 2246 break;
Lightvalve 57:f4819de54e7a 2247 }
Lightvalve 58:2eade98630e2 2248
Lightvalve 169:645207e160ca 2249 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 2250 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 2251 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2252 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2253 } else {
Lightvalve 169:645207e160ca 2254 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2255 }
Lightvalve 169:645207e160ca 2256 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2257 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2258 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2259 } else {
Lightvalve 169:645207e160ca 2260 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2261 }
Lightvalve 58:2eade98630e2 2262
Lightvalve 169:645207e160ca 2263 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2264 cnt_freq_test++;
Lightvalve 169:645207e160ca 2265 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 2266 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 2267 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 2268 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2269 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 2270 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 2271 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2272 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2273 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2274 }
Lightvalve 169:645207e160ca 2275 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2276 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 2277
Lightvalve 169:645207e160ca 2278 }
Lightvalve 169:645207e160ca 2279 break;
Lightvalve 169:645207e160ca 2280 }
Lightvalve 169:645207e160ca 2281 case MODE_SEND_BUFFER: {
Lightvalve 169:645207e160ca 2282 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 169:645207e160ca 2283 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 169:645207e160ca 2284 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 169:645207e160ca 2285 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 169:645207e160ca 2286 // }
Lightvalve 169:645207e160ca 2287 // cnt_send_buffer++;
Lightvalve 169:645207e160ca 2288 // }
Lightvalve 169:645207e160ca 2289
Lightvalve 169:645207e160ca 2290 break;
Lightvalve 169:645207e160ca 2291 }
Lightvalve 169:645207e160ca 2292 case MODE_SEND_OVER: {
Lightvalve 169:645207e160ca 2293 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 169:645207e160ca 2294 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2295 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2296 break;
Lightvalve 169:645207e160ca 2297 }
Lightvalve 170:42c938a40313 2298
Lightvalve 169:645207e160ca 2299 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 2300 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2301 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2302 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2303 } else {
Lightvalve 169:645207e160ca 2304 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 2305 }
Lightvalve 169:645207e160ca 2306 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2307 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2308 } else {
Lightvalve 169:645207e160ca 2309 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2310 }
Lightvalve 169:645207e160ca 2311 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2312 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2313 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2314 } else {
Lightvalve 169:645207e160ca 2315 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2316 }
Lightvalve 169:645207e160ca 2317
Lightvalve 169:645207e160ca 2318 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2319 cnt_step_test++;
Lightvalve 170:42c938a40313 2320 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2321 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 2322 cnt_step_test = 0;
Lightvalve 169:645207e160ca 2323 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2324 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 2325 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2326 }
Lightvalve 169:645207e160ca 2327 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 169:645207e160ca 2328 // {
Lightvalve 169:645207e160ca 2329 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2330 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2331 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2332 // }
Lightvalve 170:42c938a40313 2333
Lightvalve 169:645207e160ca 2334 break;
Lightvalve 169:645207e160ca 2335 }
Lightvalve 57:f4819de54e7a 2336
Lightvalve 57:f4819de54e7a 2337 default:
Lightvalve 57:f4819de54e7a 2338 break;
Lightvalve 57:f4819de54e7a 2339 }
Lightvalve 57:f4819de54e7a 2340
Lightvalve 57:f4819de54e7a 2341 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 2342
Lightvalve 57:f4819de54e7a 2343 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 2344 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 2345 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 2346 break;
Lightvalve 57:f4819de54e7a 2347 }
Lightvalve 57:f4819de54e7a 2348
Lightvalve 57:f4819de54e7a 2349 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 2350 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 2351 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 2352 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 2353 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 2354 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 2355 } else {
Lightvalve 67:c2812cf26c38 2356 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 2357 }
Lightvalve 57:f4819de54e7a 2358 break;
Lightvalve 57:f4819de54e7a 2359 }
Lightvalve 57:f4819de54e7a 2360
Lightvalve 57:f4819de54e7a 2361 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 2362
Lightvalve 57:f4819de54e7a 2363 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 2364 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 2365 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 2366 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2367
Lightvalve 67:c2812cf26c38 2368 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 2369 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 2370 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 2371 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 2372
Lightvalve 139:15621998925b 2373 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 139:15621998925b 2374 torq_ref = torq.ref;
Lightvalve 57:f4819de54e7a 2375
Lightvalve 57:f4819de54e7a 2376 // torque feedback
Lightvalve 67:c2812cf26c38 2377 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 2378 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 2379
Lightvalve 57:f4819de54e7a 2380 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 2381
Lightvalve 57:f4819de54e7a 2382 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 2383 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 2384 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 2385
Lightvalve 57:f4819de54e7a 2386 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 2387 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 2388 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 2389
Lightvalve 69:3995ffeaa786 2390 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2391 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 2392 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 2393 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2394 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2395 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 2396 }
Lightvalve 57:f4819de54e7a 2397 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 2398 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2399
Lightvalve 57:f4819de54e7a 2400 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 2401 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2402 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 2403 // 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 2404 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2405 // L feedforward velocity
Lightvalve 69:3995ffeaa786 2406 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2407 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 2408 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2409 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2410 // L feedforward velocity
Lightvalve 57:f4819de54e7a 2411 }
Lightvalve 57:f4819de54e7a 2412 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 2413 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2414 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 2415 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 2416 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 2417
Lightvalve 57:f4819de54e7a 2418 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 2419
Lightvalve 67:c2812cf26c38 2420 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 2421 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 2422 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 2423 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 2424 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 2425 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 2426 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2427 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 2428 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2429 } else if (I_REF < -I_MAX) {
Lightvalve 67:c2812cf26c38 2430 double I_rem = I_REF - (-I_MAX);
Lightvalve 67:c2812cf26c38 2431 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2432 I_REF = -I_MAX;
Lightvalve 67:c2812cf26c38 2433 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2434 }
Lightvalve 67:c2812cf26c38 2435 }
Lightvalve 57:f4819de54e7a 2436
Lightvalve 57:f4819de54e7a 2437 } else {
Lightvalve 57:f4819de54e7a 2438 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 2439 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 2440 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 2441
Lightvalve 209:ebc69d6ee6f1 2442 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * (torq.ref_diff - torq_dot.sen)) * 0.01f + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 57:f4819de54e7a 2443
Lightvalve 72:3436ce769b1e 2444 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 2445
Lightvalve 72:3436ce769b1e 2446 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 2447
Lightvalve 169:645207e160ca 2448
Lightvalve 72:3436ce769b1e 2449 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 2450 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 2451 } else {
Lightvalve 72:3436ce769b1e 2452 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 2453 }
Lightvalve 57:f4819de54e7a 2454
Lightvalve 57:f4819de54e7a 2455 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 2456 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 2457 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 2458 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2459 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2460 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2461 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2462 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 2463 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2464 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2465 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2466 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2467 }
Lightvalve 57:f4819de54e7a 2468 }
Lightvalve 61:bc8c8270f0ab 2469
Lightvalve 57:f4819de54e7a 2470 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2471
Lightvalve 67:c2812cf26c38 2472 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 60:64181f1d3e60 2473 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2474
Lightvalve 57:f4819de54e7a 2475 }
Lightvalve 169:645207e160ca 2476
Lightvalve 72:3436ce769b1e 2477 torq_ref_past = torq_ref;
Lightvalve 58:2eade98630e2 2478
Lightvalve 133:22ab22818e01 2479
Lightvalve 57:f4819de54e7a 2480 break;
Lightvalve 57:f4819de54e7a 2481 }
Lightvalve 58:2eade98630e2 2482
Lightvalve 57:f4819de54e7a 2483 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 2484 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2485 break;
Lightvalve 57:f4819de54e7a 2486 }
Lightvalve 169:645207e160ca 2487
Lightvalve 138:a843f32ced33 2488 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 2489
Lightvalve 169:645207e160ca 2490
Lightvalve 139:15621998925b 2491 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 2492 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 218:066030f7951f 2493
Lightvalve 170:42c938a40313 2494 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 2495
Lightvalve 218:066030f7951f 2496 //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 218:066030f7951f 2497 float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076
Lightvalve 169:645207e160ca 2498
Lightvalve 138:a843f32ced33 2499 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 2500 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2501 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 2502 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2503 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 2504 } else {
Lightvalve 138:a843f32ced33 2505 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 2506 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 2507 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2508 } else {
Lightvalve 138:a843f32ced33 2509 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 2510 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2511 }
Lightvalve 138:a843f32ced33 2512 }
Lightvalve 138:a843f32ced33 2513 float tau = 0.01f;
Lightvalve 142:43026242815a 2514 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 2515
Lightvalve 138:a843f32ced33 2516 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 2517 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 2518 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2519 } else {
Lightvalve 138:a843f32ced33 2520 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2521 }
Lightvalve 138:a843f32ced33 2522 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 2523 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 2524
Lightvalve 139:15621998925b 2525 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 2526
Lightvalve 138:a843f32ced33 2527 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 2528 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 2529 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2530
Lightvalve 138:a843f32ced33 2531 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 2532 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 2533
Lightvalve 212:ec41f1449ef9 2534 float k3 = 2000.0f; //2000 //20000
Lightvalve 142:43026242815a 2535 float k4 = 10.0f;
Lightvalve 142:43026242815a 2536 float rho3 = 3.2f;
Lightvalve 212:ec41f1449ef9 2537 float rho4 = 10000000.0f; //25000000.0f;
Lightvalve 218:066030f7951f 2538 float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 2539 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 2540 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 2541
Lightvalve 139:15621998925b 2542 if (x_4_des > 0) {
Lightvalve 139:15621998925b 2543 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2544 } else {
Lightvalve 139:15621998925b 2545 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2546 }
Lightvalve 169:645207e160ca 2547
Lightvalve 138:a843f32ced33 2548 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 2549 x_4_des_old = x_4_des;
Lightvalve 212:ec41f1449ef9 2550 float V_input = 0.0f;
Lightvalve 139:15621998925b 2551 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 212:ec41f1449ef9 2552 // //V_out LPF
Lightvalve 212:ec41f1449ef9 2553 // float alpha_V_out = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*50.0f)); // f_cutoff : 50Hz
Lightvalve 212:ec41f1449ef9 2554 // V_out = V_out*(1.0f-alpha_V_out)+V_input*(alpha_V_out);
Lightvalve 212:ec41f1449ef9 2555
Lightvalve 218:066030f7951f 2556 // float rho_gamma = 5000.0f;//5000 for change //50000 for not change
Lightvalve 218:066030f7951f 2557 // 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 218:066030f7951f 2558 // gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 218:066030f7951f 2559 //
Lightvalve 218:066030f7951f 2560 // if(gamma_hat > 10000.0f) gamma_hat = 10000.0f;
Lightvalve 218:066030f7951f 2561 // else if(gamma_hat < 100.0f) gamma_hat = 100.0f;
Lightvalve 218:066030f7951f 2562
Lightvalve 218:066030f7951f 2563 float rho_a = 0.00001f;
Lightvalve 218:066030f7951f 2564 float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err);
Lightvalve 218:066030f7951f 2565 a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 218:066030f7951f 2566
Lightvalve 218:066030f7951f 2567 if(a_hat > -3000000.0f) a_hat = -3000000.0f;
Lightvalve 218:066030f7951f 2568 else if(a_hat < -30000000.0f) a_hat = -30000000.0f;
Lightvalve 218:066030f7951f 2569
Lightvalve 169:645207e160ca 2570 break;
Lightvalve 138:a843f32ced33 2571 }
Lightvalve 171:bfc1fd2629d8 2572
Lightvalve 170:42c938a40313 2573 case MODE_RL: {
Lightvalve 170:42c938a40313 2574 //t.reset();
Lightvalve 170:42c938a40313 2575 //t.start();
Lightvalve 170:42c938a40313 2576
Lightvalve 170:42c938a40313 2577 // if(LED == 0) LED = 1;
Lightvalve 170:42c938a40313 2578 // else LED = 0;
Lightvalve 170:42c938a40313 2579
Lightvalve 170:42c938a40313 2580 if (Update_Done_Flag == 1) {
Lightvalve 170:42c938a40313 2581 //Gather Data on each loop
Lightvalve 170:42c938a40313 2582 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 170:42c938a40313 2583 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2584 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2585 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2586 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2587 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2588 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2589 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 170:42c938a40313 2590 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 170:42c938a40313 2591 Actor_Network(temp_array);
Lightvalve 173:68c7914679ec 2592 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 2593 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 173:68c7914679ec 2594 }
Lightvalve 173:68c7914679ec 2595 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 2596 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 173:68c7914679ec 2597 }
Lightvalve 173:68c7914679ec 2598 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 173:68c7914679ec 2599 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 170:42c938a40313 2600 mean_array[RL_timer] = mean;
Lightvalve 170:42c938a40313 2601 deviation_array[RL_timer] = deviation;
Lightvalve 170:42c938a40313 2602 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 170:42c938a40313 2603
Lightvalve 179:d5377766d7ea 2604 virt_pos = virt_pos + (action_array[RL_timer] - 5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2605 if (virt_pos > 70 ) {
Lightvalve 170:42c938a40313 2606 virt_pos = 70.0f;
Lightvalve 218:066030f7951f 2607 } else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2608 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2609 }
Lightvalve 171:bfc1fd2629d8 2610
Lightvalve 170:42c938a40313 2611 RL_timer++;
Lightvalve 170:42c938a40313 2612
Lightvalve 170:42c938a40313 2613
Lightvalve 170:42c938a40313 2614 if (RL_timer >= batch_size) {
Lightvalve 170:42c938a40313 2615 RL_timer = 0;
Lightvalve 170:42c938a40313 2616 batch++;
Lightvalve 170:42c938a40313 2617 for(int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 2618 state_array[i][0] = train_set_x[i];
Lightvalve 170:42c938a40313 2619 state_array[i][1] = train_set_error[i];
Lightvalve 170:42c938a40313 2620 //state_array[i][2] = train_set_count[i];
Lightvalve 170:42c938a40313 2621 }
Lightvalve 170:42c938a40313 2622 Update_Case = 1;
Lightvalve 170:42c938a40313 2623 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2624 logging1 = virt_pos;
Lightvalve 179:d5377766d7ea 2625
Lightvalve 170:42c938a40313 2626 if(batch >= num_batch) {
Lightvalve 170:42c938a40313 2627 batch = 0;
Lightvalve 170:42c938a40313 2628 RL_timer = 0;
Lightvalve 170:42c938a40313 2629 Update_Case = 2;
Lightvalve 170:42c938a40313 2630 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2631 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2632 }
Lightvalve 170:42c938a40313 2633 }
Lightvalve 170:42c938a40313 2634 }
Lightvalve 170:42c938a40313 2635
Lightvalve 170:42c938a40313 2636 else {
Lightvalve 170:42c938a40313 2637 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2638 float temp_array[3] = {0.0f};
Lightvalve 170:42c938a40313 2639 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2640 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2641 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2642 Actor_Network(temp_array);
Lightvalve 170:42c938a40313 2643 action = rand_normal(mean, deviation);
Lightvalve 170:42c938a40313 2644 //logging1 = action;
Lightvalve 179:d5377766d7ea 2645 //logging2 = mean;
Lightvalve 179:d5377766d7ea 2646 //logging4 = deviation;
Lightvalve 179:d5377766d7ea 2647 virt_pos = virt_pos + (action-5.0f) * 1000.0f * 0.0002f;
Lightvalve 179:d5377766d7ea 2648 if (virt_pos > 70) {
Lightvalve 170:42c938a40313 2649 virt_pos = 70.0f;
Lightvalve 218:066030f7951f 2650 } else if(virt_pos < -70) {
Lightvalve 170:42c938a40313 2651 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2652 }
Lightvalve 170:42c938a40313 2653
Lightvalve 170:42c938a40313 2654 logging3 = virt_pos;
Lightvalve 170:42c938a40313 2655 }
Lightvalve 170:42c938a40313 2656
Lightvalve 170:42c938a40313 2657 //t.stop();
Lightvalve 170:42c938a40313 2658 //logging1 = t.read()*1000.0f; //msec
Lightvalve 170:42c938a40313 2659
Lightvalve 170:42c938a40313 2660 break;
Lightvalve 170:42c938a40313 2661 }
Lightvalve 14:8e7590227d22 2662
Lightvalve 12:6f2531038ea4 2663 default:
Lightvalve 12:6f2531038ea4 2664 break;
Lightvalve 12:6f2531038ea4 2665 }
Lightvalve 14:8e7590227d22 2666
Lightvalve 57:f4819de54e7a 2667
Lightvalve 57:f4819de54e7a 2668 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 2669
Lightvalve 57:f4819de54e7a 2670 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2671 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 2672 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2673 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 2674 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 2675 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 2676
Lightvalve 57:f4819de54e7a 2677 I_ERR = I_REF_fil - cur.sen;
Lightvalve 67:c2812cf26c38 2678 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 2679
Lightvalve 57:f4819de54e7a 2680
Lightvalve 57:f4819de54e7a 2681 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 2682 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 2683 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 2684 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 2685 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 2686 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 2687
Lightvalve 57:f4819de54e7a 2688 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 2689 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 2690 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 2691 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 2692 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 2693 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 2694 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 2695 }
Lightvalve 57:f4819de54e7a 2696
Lightvalve 57:f4819de54e7a 2697 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 2698
Lightvalve 67:c2812cf26c38 2699 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 57:f4819de54e7a 2700 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 57:f4819de54e7a 2701 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 57:f4819de54e7a 2702 I_REF_fil_old = I_REF_fil;
Lightvalve 67:c2812cf26c38 2703 // 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 2704 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 67:c2812cf26c38 2705 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 2706
Lightvalve 57:f4819de54e7a 2707 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 2708 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 2709 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 2710 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2711 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 2712 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2713 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 2714 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 2715 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2716 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 2717 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2718 }
Lightvalve 57:f4819de54e7a 2719 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2720 } else {
Lightvalve 57:f4819de54e7a 2721 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 2722 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2723 }
Lightvalve 57:f4819de54e7a 2724
Lightvalve 57:f4819de54e7a 2725 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2726 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 2727 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2728 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 57:f4819de54e7a 2729 if (FLAG_VALVE_DEADZONE) {
Lightvalve 57:f4819de54e7a 2730 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2731 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2732
Lightvalve 57:f4819de54e7a 2733 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2734
Lightvalve 57:f4819de54e7a 2735 } else {
Lightvalve 57:f4819de54e7a 2736 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 57:f4819de54e7a 2737 }
Lightvalve 57:f4819de54e7a 2738
Lightvalve 57:f4819de54e7a 2739 // Output Voltage Linearization
Lightvalve 57:f4819de54e7a 2740 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 67:c2812cf26c38 2741 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 2742
Lightvalve 57:f4819de54e7a 2743 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 2744 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 2745 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 2746 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 2747 } else { //////////////////////////sw valve
Lightvalve 89:a7b45368ea0f 2748 // Output Voltage Linearization
Lightvalve 135:79885a39c161 2749 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 135:79885a39c161 2750 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 89:a7b45368ea0f 2751
Lightvalve 89:a7b45368ea0f 2752 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 135:79885a39c161 2753 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 135:79885a39c161 2754 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 135:79885a39c161 2755 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 135:79885a39c161 2756
Lightvalve 135:79885a39c161 2757 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 2758 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 2759 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 2760 }
Lightvalve 169:645207e160ca 2761
Lightvalve 67:c2812cf26c38 2762 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 67:c2812cf26c38 2763 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 67:c2812cf26c38 2764 // else V_out = V_out;
Lightvalve 169:645207e160ca 2765
jobuuu 7:e9086c72bb22 2766 /*******************************************************
jobuuu 7:e9086c72bb22 2767 *** PWM
jobuuu 7:e9086c72bb22 2768 ********************************************************/
Lightvalve 169:645207e160ca 2769 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 2770 V_out = -V_out;
Lightvalve 67:c2812cf26c38 2771 }
Lightvalve 169:645207e160ca 2772
Lightvalve 49:e7bcfc244d40 2773 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2774 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 2775 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2776 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2777 }
Lightvalve 49:e7bcfc244d40 2778 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2779
Lightvalve 19:23b7c1ad8683 2780 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2781 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2782 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2783
Lightvalve 30:8d561f16383b 2784 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2785 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2786 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2787 } else {
jobuuu 2:a1c0a37df760 2788 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2789 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2790 }
Lightvalve 13:747daba9cf59 2791
jobuuu 1:e04e563be5ce 2792 //pwm
Lightvalve 30:8d561f16383b 2793 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2794 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 2795
Lightvalve 61:bc8c8270f0ab 2796
Lightvalve 57:f4819de54e7a 2797 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2798
Lightvalve 54:647072f5307a 2799 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 2800 if (flag_data_request[0] == HIGH) {
Lightvalve 57:f4819de54e7a 2801 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 2802 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2803 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 2804 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 2805 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 2806 }
Lightvalve 57:f4819de54e7a 2807 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 2808 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2809 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 2810 } else if (SENSING_MODE == 1) {
Lightvalve 67:c2812cf26c38 2811 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 2812 }
Lightvalve 52:8ea76864368a 2813 }
Lightvalve 52:8ea76864368a 2814 }
Lightvalve 56:6f50d9d3bfee 2815 if (flag_data_request[1] == HIGH) {
Lightvalve 179:d5377766d7ea 2816 CAN_TX_TORQUE((int16_t) (return_G[0]*100.0f)); //1300
Lightvalve 54:647072f5307a 2817 }
Lightvalve 58:2eade98630e2 2818
Lightvalve 58:2eade98630e2 2819
Lightvalve 171:bfc1fd2629d8 2820 if (flag_data_request[2] == HIGH) {
Lightvalve 169:645207e160ca 2821 double t_value = 0.0f;
Lightvalve 171:bfc1fd2629d8 2822 if(value>=(float) VALVE_CENTER) {
Lightvalve 131:d08121ac87ba 2823 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2824 } else {
Lightvalve 131:d08121ac87ba 2825 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2826 }
Lightvalve 169:645207e160ca 2827 double t_value_ref = 0.0f;
Lightvalve 169:645207e160ca 2828 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2829 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2830 } else {
Lightvalve 169:645207e160ca 2831 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2832 }
Lightvalve 169:645207e160ca 2833
Lightvalve 169:645207e160ca 2834
Lightvalve 169:645207e160ca 2835 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 55:b25725257569 2836 }
Lightvalve 58:2eade98630e2 2837
Lightvalve 57:f4819de54e7a 2838 //If it doesn't rest, below can can not work.
Lightvalve 57:f4819de54e7a 2839 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 57:f4819de54e7a 2840 ;
Lightvalve 57:f4819de54e7a 2841 }
Lightvalve 58:2eade98630e2 2842
Lightvalve 169:645207e160ca 2843 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 2844 //PWM
Lightvalve 209:ebc69d6ee6f1 2845 CAN_TX_PWM((int16_t) (torq.ref)); //1500
Lightvalve 209:ebc69d6ee6f1 2846 // CAN_TX_PWM((int16_t) (f_future[1])); //1500
Lightvalve 54:647072f5307a 2847 }
Lightvalve 179:d5377766d7ea 2848
Lightvalve 56:6f50d9d3bfee 2849 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 2850 //valve position
Lightvalve 212:ec41f1449ef9 2851 //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 218:066030f7951f 2852 CAN_TX_VALVE_POSITION((int16_t) (a_hat*0.0001f), (int16_t) 0, (int16_t) 0, (int16_t) 0); //1600
Lightvalve 54:647072f5307a 2853 }
Lightvalve 20:806196fda269 2854
Lightvalve 54:647072f5307a 2855 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 2856 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 2857 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 2858 // }
Lightvalve 54:647072f5307a 2859 //if (flag_delay_test == 1){
Lightvalve 58:2eade98630e2 2860 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 2861 //}
Lightvalve 52:8ea76864368a 2862
Lightvalve 54:647072f5307a 2863 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 2864 }
Lightvalve 54:647072f5307a 2865 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 2866
Lightvalve 20:806196fda269 2867 }
Lightvalve 52:8ea76864368a 2868 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 2869
Lightvalve 58:2eade98630e2 2870 }