Team Fox / Mbed 2 deprecated BAE_QM_MAR9

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of workinQM_5thJan_azad by Team Fox

Committer:
lakshya
Date:
Sat May 14 11:19:13 2016 +0000
Revision:
18:3662058a7c10
Parent:
17:fc782f7548c6
Child:
19:79e69017c855
bcn working no hanging; ; rtos timer used for calling bcn_fen ; ( spi connection valid  if it waits for too long then maybe BCN_hw is commented make sure it sets the spi ); ; both short and long beacon working; ; also ACS (modifed version as earlier)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sakthipriya 0:7b4c00e3912f 1 /*------------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:7b4c00e3912f 2 -------------------------------------------CONTROL ALGORITHM------------------------------------------------------------------------------------------*/
sakthipriya 0:7b4c00e3912f 3 #include <mbed.h>
sakthipriya 0:7b4c00e3912f 4 #include <math.h>
sakthipriya 0:7b4c00e3912f 5
sakthipriya 0:7b4c00e3912f 6 #include "pni.h" //pni header file
sakthipriya 0:7b4c00e3912f 7 #include "pin_config.h"
sakthipriya 0:7b4c00e3912f 8 #include "ACS.h"
sakthipriya 6:036d08b62785 9 #include "EPS.h"
sakthipriya 0:7b4c00e3912f 10
sakthipriya 0:7b4c00e3912f 11 //********************************flags******************************************//
sakthipriya 0:7b4c00e3912f 12 extern uint32_t BAE_STATUS;
sakthipriya 0:7b4c00e3912f 13 extern uint32_t BAE_ENABLE;
Bragadeesh153 13:fb7facaf308b 14 extern uint8_t ACS_INIT_STATUS;
Bragadeesh153 13:fb7facaf308b 15 extern uint8_t ACS_DATA_ACQ_STATUS;
Bragadeesh153 13:fb7facaf308b 16 extern uint8_t ACS_ATS_STATUS;
Bragadeesh153 13:fb7facaf308b 17 extern uint8_t ACS_MAIN_STATUS;
Bragadeesh153 13:fb7facaf308b 18 extern uint8_t ACS_STATUS;
sakthipriya 0:7b4c00e3912f 19
Bragadeesh153 13:fb7facaf308b 20 extern uint8_t ACS_ATS_ENABLE;
Bragadeesh153 13:fb7facaf308b 21 extern uint8_t ACS_DATA_ACQ_ENABLE;
Bragadeesh153 13:fb7facaf308b 22 extern uint8_t ACS_STATE;
sakthipriya 0:7b4c00e3912f 23
sakthipriya 0:7b4c00e3912f 24 DigitalOut phase_TR_x(PIN27); // PHASE pin for x-torquerod
sakthipriya 0:7b4c00e3912f 25 DigitalOut phase_TR_y(PIN28); // PHASE pin for y-torquerod
sakthipriya 0:7b4c00e3912f 26 DigitalOut phase_TR_z(PIN86); // PHASE pin for z-torquerod
sakthipriya 0:7b4c00e3912f 27
sakthipriya 0:7b4c00e3912f 28 extern PwmOut PWM1; //x //Functions used to generate PWM signal
sakthipriya 0:7b4c00e3912f 29 extern PwmOut PWM2; //y
sakthipriya 0:7b4c00e3912f 30 extern PwmOut PWM3; //z //PWM output comes from pins p6
sakthipriya 0:7b4c00e3912f 31
sakthipriya 0:7b4c00e3912f 32 int g_err_flag_TR_x=0; // setting x-flag to zero
sakthipriya 0:7b4c00e3912f 33 int g_err_flag_TR_y=0; // setting y-flag to zero
sakthipriya 0:7b4c00e3912f 34 int g_err_flag_TR_z=0; // setting z-flag to zero
sakthipriya 0:7b4c00e3912f 35
sakthipriya 0:7b4c00e3912f 36 extern float data[6];
sakthipriya 6:036d08b62785 37 extern BAE_HK_actual actual_data;
sakthipriya 0:7b4c00e3912f 38
sakthipriya 0:7b4c00e3912f 39
lakshya 10:f93407b97750 40 //DigitalOut gpo1(PTC0); // enable of att sens2 switch
lakshya 10:f93407b97750 41 //DigitalOut gpo2(PTC16); // enable of att sens switch
sakthipriya 0:7b4c00e3912f 42
sakthipriya 0:7b4c00e3912f 43
sakthipriya 0:7b4c00e3912f 44 Serial pc_acs(USBTX,USBRX); //for usb communication
lakshya 10:f93407b97750 45 //CONTROL_ALGO
lakshya 10:f93407b97750 46
lakshya 10:f93407b97750 47 float moment[3]; // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 48 float b_old[3]={1.15e-5,-0.245e-5,1.98e-5}; // Unit: Tesla
lakshya 10:f93407b97750 49 int flag_firsttime=1, controlmode, alarmmode=0;
lakshya 10:f93407b97750 50
lakshya 10:f93407b97750 51
lakshya 10:f93407b97750 52
lakshya 10:f93407b97750 53 void controller (float moment[3], float b1[3], float omega1[3], float b_old[3], int &alarmmode, int &flag_firsttime, int &controlmode);
lakshya 10:f93407b97750 54 void controlmodes(float moment[3], float b[3], float db[3], float omega[3], int controlmode1, float MmntMax);
lakshya 10:f93407b97750 55 float max_array(float arr[3]);
sakthipriya 0:7b4c00e3912f 56 void inverse(float mat[3][3],float inv[3][3]);
lakshya 10:f93407b97750 57
lakshya 10:f93407b97750 58 //CONTROLALGO PARAMETERS
lakshya 10:f93407b97750 59
lakshya 10:f93407b97750 60
sakthipriya 0:7b4c00e3912f 61 void FCTN_ACS_CNTRLALGO(float b[3],float omega[3])
sakthipriya 0:7b4c00e3912f 62 {
lakshya 10:f93407b97750 63 float b1[3];
lakshya 10:f93407b97750 64 float omega1[3];
lakshya 10:f93407b97750 65 b1[0] = b[0]/1000000.0;
lakshya 10:f93407b97750 66 b1[1] = b[1]/1000000.0;
lakshya 10:f93407b97750 67 b1[2] = b[2]/1000000.0;
lakshya 10:f93407b97750 68
lakshya 10:f93407b97750 69 omega1[0] = omega[0]*3.14159/180;
lakshya 10:f93407b97750 70 omega1[1] = omega[1]*3.14159/180;
lakshya 10:f93407b97750 71 omega1[2] = omega[2]*3.14159/180;
lakshya 10:f93407b97750 72 controller (moment, b1, omega1, b_old, alarmmode, flag_firsttime, controlmode);
sakthipriya 0:7b4c00e3912f 73
lakshya 10:f93407b97750 74 }
lakshya 10:f93407b97750 75 void controller (float moment[3], float b1[3], float omega1[3], float b_old[3], int &alarmmode, int &flag_firsttime, int &controlmode)
lakshya 10:f93407b97750 76 {
lakshya 10:f93407b97750 77 float db1[3]; // Unit: Tesla/Second
lakshya 10:f93407b97750 78 float sampling_time=10; // Unit: Seconds. Digital Control law excuted once in 10 seconds
lakshya 10:f93407b97750 79 float MmntMax=1.1; // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 80 float OmegaMax=1*3.1415/180.0; // Unit: Radians/Second
lakshya 10:f93407b97750 81 float normalising_fact;
lakshya 10:f93407b97750 82 float b1_copy[3], omega1_copy[3], db1_copy[3];
lakshya 10:f93407b97750 83 int i;
lakshya 10:f93407b97750 84 if(flag_firsttime==1)
lakshya 10:f93407b97750 85 {
lakshya 10:f93407b97750 86 for(i=0;i<3;i++)
lakshya 10:f93407b97750 87 {
lakshya 10:f93407b97750 88 db1[i]=0; // Unit: Tesla/Second
lakshya 10:f93407b97750 89 }
lakshya 10:f93407b97750 90 flag_firsttime=0;
lakshya 10:f93407b97750 91 }
sakthipriya 0:7b4c00e3912f 92 else
sakthipriya 0:7b4c00e3912f 93 {
sakthipriya 0:7b4c00e3912f 94 for(i=0;i<3;i++)
sakthipriya 0:7b4c00e3912f 95 {
lakshya 10:f93407b97750 96 db1[i]= (b1[i]-b_old[i])/sampling_time; // Unit: Tesla/Second
sakthipriya 0:7b4c00e3912f 97 }
lakshya 10:f93407b97750 98 }
lakshya 10:f93407b97750 99
lakshya 10:f93407b97750 100 if(max_array(omega1)<(0.8*OmegaMax) && alarmmode==1)
sakthipriya 0:7b4c00e3912f 101 {
lakshya 10:f93407b97750 102 alarmmode=0;
sakthipriya 0:7b4c00e3912f 103 }
lakshya 10:f93407b97750 104 else if(max_array(omega1)>OmegaMax && alarmmode==0)
sakthipriya 0:7b4c00e3912f 105 {
lakshya 10:f93407b97750 106 alarmmode=1;
sakthipriya 0:7b4c00e3912f 107 }
lakshya 10:f93407b97750 108
lakshya 10:f93407b97750 109 for (i=0;i<3;i++)
sakthipriya 0:7b4c00e3912f 110 {
lakshya 10:f93407b97750 111 b1_copy[i]=b1[i];
lakshya 10:f93407b97750 112 db1_copy[i]=db1[i];
lakshya 10:f93407b97750 113 omega1_copy[i]=omega1[i];
sakthipriya 0:7b4c00e3912f 114 }
lakshya 10:f93407b97750 115
lakshya 10:f93407b97750 116 if(alarmmode==0)
lakshya 10:f93407b97750 117 {
lakshya 10:f93407b97750 118 controlmode=0;
lakshya 10:f93407b97750 119 controlmodes(moment,b1,db1,omega1,controlmode,MmntMax);
lakshya 10:f93407b97750 120 for (i=0;i<3;i++)
lakshya 10:f93407b97750 121 {
lakshya 10:f93407b97750 122 b1[i]=b1_copy[i];
lakshya 10:f93407b97750 123 db1[i]=db1_copy[i];
lakshya 10:f93407b97750 124 omega1[i]=omega1_copy[i];
lakshya 10:f93407b97750 125 }
lakshya 10:f93407b97750 126 if(max_array(moment)>MmntMax)
lakshya 10:f93407b97750 127 {
lakshya 10:f93407b97750 128 controlmode=1;
lakshya 10:f93407b97750 129 controlmodes(moment,b1,db1,omega1,controlmode,MmntMax);
lakshya 10:f93407b97750 130 for (i=0;i<3;i++)
lakshya 10:f93407b97750 131 {
lakshya 10:f93407b97750 132 b1[i]=b1_copy[i];
lakshya 10:f93407b97750 133 db1[i]=db1_copy[i];
lakshya 10:f93407b97750 134 omega1[i]=omega1_copy[i];
lakshya 10:f93407b97750 135 }
lakshya 10:f93407b97750 136 if(max_array(moment)>MmntMax)
lakshya 10:f93407b97750 137 {
lakshya 10:f93407b97750 138 normalising_fact=max_array(moment)/MmntMax;
lakshya 10:f93407b97750 139 for(i=0;i<3;i++)
lakshya 10:f93407b97750 140 {
lakshya 10:f93407b97750 141 moment[i]/=normalising_fact; // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 142 }
lakshya 10:f93407b97750 143 }
lakshya 10:f93407b97750 144 }
lakshya 10:f93407b97750 145 }
lakshya 10:f93407b97750 146 else
lakshya 10:f93407b97750 147 {
lakshya 10:f93407b97750 148 controlmode=1;
lakshya 10:f93407b97750 149 controlmodes(moment,b1,db1,omega1,controlmode,MmntMax);
lakshya 10:f93407b97750 150 for (i=0;i<3;i++)
lakshya 10:f93407b97750 151 {
lakshya 10:f93407b97750 152 b1[i]=b1_copy[i];
lakshya 10:f93407b97750 153 db1[i]=db1_copy[i];
lakshya 10:f93407b97750 154 omega1[i]=omega1_copy[i];
lakshya 10:f93407b97750 155 }
lakshya 10:f93407b97750 156 if(max_array(moment)>MmntMax)
lakshya 10:f93407b97750 157 {
lakshya 10:f93407b97750 158 normalising_fact=max_array(moment)/MmntMax;
lakshya 10:f93407b97750 159 for(i=0;i<3;i++)
lakshya 10:f93407b97750 160 {
lakshya 10:f93407b97750 161 moment[i]/=normalising_fact; // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 162 }
lakshya 10:f93407b97750 163 }
lakshya 10:f93407b97750 164
lakshya 10:f93407b97750 165 }
lakshya 10:f93407b97750 166 for (i=0;i<3;i++)
sakthipriya 0:7b4c00e3912f 167 {
lakshya 10:f93407b97750 168 b_old[i]=b1[i];
sakthipriya 0:7b4c00e3912f 169 }
sakthipriya 0:7b4c00e3912f 170 }
lakshya 10:f93407b97750 171
sakthipriya 0:7b4c00e3912f 172 void inverse(float mat[3][3],float inv[3][3])
sakthipriya 0:7b4c00e3912f 173 {
sakthipriya 0:7b4c00e3912f 174 int i,j;
sakthipriya 0:7b4c00e3912f 175 float det=0;
sakthipriya 0:7b4c00e3912f 176 for(i=0;i<3;i++)
sakthipriya 0:7b4c00e3912f 177 {
sakthipriya 0:7b4c00e3912f 178 for(j=0;j<3;j++)
lakshya 10:f93407b97750 179 {
sakthipriya 0:7b4c00e3912f 180 inv[j][i]=(mat[(i+1)%3][(j+1)%3]*mat[(i+2)%3][(j+2)%3])-(mat[(i+2)%3][(j+1)%3]*mat[(i+1)%3][(j+2)%3]);
lakshya 10:f93407b97750 181 }
sakthipriya 0:7b4c00e3912f 182 }
sakthipriya 0:7b4c00e3912f 183 det+=(mat[0][0]*inv[0][0])+(mat[0][1]*inv[1][0])+(mat[0][2]*inv[2][0]);
sakthipriya 0:7b4c00e3912f 184 for(i=0;i<3;i++)
lakshya 10:f93407b97750 185 {
sakthipriya 0:7b4c00e3912f 186 for(j=0;j<3;j++)
lakshya 10:f93407b97750 187 {
sakthipriya 0:7b4c00e3912f 188 inv[i][j]/=det;
lakshya 10:f93407b97750 189 }
sakthipriya 0:7b4c00e3912f 190 }
sakthipriya 0:7b4c00e3912f 191 }
sakthipriya 0:7b4c00e3912f 192
lakshya 10:f93407b97750 193 float max_array(float arr[3])
lakshya 10:f93407b97750 194 {
lakshya 10:f93407b97750 195 int i;
lakshya 10:f93407b97750 196 float temp_max=fabs(arr[0]);
lakshya 10:f93407b97750 197 for(i=1;i<3;i++)
lakshya 10:f93407b97750 198 {
lakshya 10:f93407b97750 199 if(fabs(arr[i])>temp_max)
lakshya 10:f93407b97750 200 {
lakshya 10:f93407b97750 201 temp_max=fabs(arr[i]);
lakshya 10:f93407b97750 202 }
lakshya 10:f93407b97750 203 }
lakshya 10:f93407b97750 204 return temp_max;
lakshya 10:f93407b97750 205 }
lakshya 10:f93407b97750 206
lakshya 10:f93407b97750 207
lakshya 10:f93407b97750 208 void controlmodes(float moment[3], float b[3], float db[3], float omega[3], int controlmode1, float MmntMax)
lakshya 10:f93407b97750 209 {
lakshya 10:f93407b97750 210 float bb[3]={0,0,0};
lakshya 10:f93407b97750 211 float d[3]={0,0,0};
lakshya 10:f93407b97750 212 float Jm[3][3]={{0.2271,0.0014,-0.0026},{0.0014,0.2167,-0.004},{-0.0026,-0.004,0.2406}}; // Unit: Kilogram*Meter^2. Jm may change depending on the final satellite structure
lakshya 10:f93407b97750 213 float den=0,den2;
lakshya 10:f93407b97750 214 float bcopy[3];
lakshya 10:f93407b97750 215 int i, j;//temporary variables
lakshya 10:f93407b97750 216 float Mu[2],z[2],dv[2],v[2],u[2],tauc[3]={0,0,0},Mmnt[3];//outputs
lakshya 10:f93407b97750 217 float invJm[3][3];
lakshya 10:f93407b97750 218 float kmu2=0.07,gamma2=1.9e4,kz2=0.4e-2,kmu=0.003,gamma=5.6e4,kz=0.1e-4,kdetumble=2000000;
lakshya 10:f93407b97750 219 int infflag; // Flag variable to check if the moment value is infinity or NaN
lakshya 10:f93407b97750 220
lakshya 10:f93407b97750 221 if(controlmode1==0)
lakshya 10:f93407b97750 222 {
lakshya 10:f93407b97750 223 den=sqrt((b[0]*b[0])+(b[1]*b[1])+(b[2]*b[2]));
lakshya 10:f93407b97750 224 den2=(b[0]*db[0])+(b[1]*db[1])+(b[2]*db[2]);
lakshya 10:f93407b97750 225 for(i=0;i<3;i++)
lakshya 10:f93407b97750 226 {
lakshya 10:f93407b97750 227 db[i]=((db[i]*den*den)-(b[i]*(den2)))/(pow(den,3)); // Normalized db. Hence the unit is Second^(-1)
lakshya 10:f93407b97750 228 }
lakshya 10:f93407b97750 229 for(i=0;i<3;i++)
lakshya 10:f93407b97750 230 {
lakshya 10:f93407b97750 231 b[i]/=den; // Mormalized b. Hence no unit.
lakshya 10:f93407b97750 232 }
lakshya 10:f93407b97750 233 if(b[2]>0.9 || b[2]<-0.9)
lakshya 10:f93407b97750 234 {
lakshya 10:f93407b97750 235 kz=kz2;
lakshya 10:f93407b97750 236 kmu=kmu2;
lakshya 10:f93407b97750 237 gamma=gamma2;
lakshya 10:f93407b97750 238 }
lakshya 10:f93407b97750 239 for(i=0;i<2;i++)
lakshya 10:f93407b97750 240 {
lakshya 10:f93407b97750 241 Mu[i]=b[i];
lakshya 10:f93407b97750 242 v[i]=-kmu*Mu[i];
lakshya 10:f93407b97750 243 dv[i]=-kmu*db[i];
lakshya 10:f93407b97750 244 z[i]=db[i]-v[i];
lakshya 10:f93407b97750 245 u[i]=-kz*z[i]+dv[i]-(Mu[i]/gamma);
lakshya 10:f93407b97750 246 }
lakshya 10:f93407b97750 247 inverse(Jm,invJm);
lakshya 10:f93407b97750 248 for(i=0;i<3;i++)
lakshya 10:f93407b97750 249 {
lakshya 10:f93407b97750 250 for(j=0;j<3;j++)
lakshya 10:f93407b97750 251 {
lakshya 10:f93407b97750 252 bb[i]+=omega[j]*(omega[(i+1)%3]*Jm[(i+2)%3][j]-omega[(i+2)%3]*Jm[(i+1)%3][j]);
lakshya 10:f93407b97750 253 }
lakshya 10:f93407b97750 254 }
lakshya 10:f93407b97750 255 for(i=0;i<3;i++)
lakshya 10:f93407b97750 256 {
lakshya 10:f93407b97750 257 for(j=0;j<3;j++)
lakshya 10:f93407b97750 258 {
lakshya 10:f93407b97750 259 d[i]+=bb[j]*invJm[i][j];
lakshya 10:f93407b97750 260 }
lakshya 10:f93407b97750 261 }
lakshya 10:f93407b97750 262 bb[1]=u[0]-(d[1]*b[2])+(d[2]*b[1])-(omega[1]*db[2])+(omega[2]*db[1]);
lakshya 10:f93407b97750 263 bb[2]=u[1]-(d[2]*b[0])+(d[0]*b[2])-(omega[2]*db[0])+(omega[0]*db[2]);
lakshya 10:f93407b97750 264 bb[0]=0;
lakshya 10:f93407b97750 265 for(i=0;i<3;i++)
lakshya 10:f93407b97750 266 {
lakshya 10:f93407b97750 267 d[i]=invJm[2][i];
lakshya 10:f93407b97750 268 invJm[1][i]=-b[2]*invJm[1][i]+b[1]*d[i];
lakshya 10:f93407b97750 269 invJm[2][i]=b[2]*invJm[0][i]-b[0]*d[i];
lakshya 10:f93407b97750 270 invJm[0][i]=b[i];
lakshya 10:f93407b97750 271 }
lakshya 10:f93407b97750 272 inverse(invJm,Jm);
lakshya 10:f93407b97750 273 for(i=0;i<3;i++)
lakshya 10:f93407b97750 274 {
lakshya 10:f93407b97750 275 for(j=0;j<3;j++)
lakshya 10:f93407b97750 276 {
lakshya 10:f93407b97750 277 tauc[i]+=Jm[i][j]*bb[j]; // Unit: Newton*Meter^2
lakshya 10:f93407b97750 278 }
lakshya 10:f93407b97750 279 }
lakshya 10:f93407b97750 280 for(i=0;i<3;i++)
lakshya 10:f93407b97750 281 {
lakshya 10:f93407b97750 282 bcopy[i]=b[i]*den;
lakshya 10:f93407b97750 283 }
lakshya 10:f93407b97750 284 for(i=0;i<3;i++)
lakshya 10:f93407b97750 285 {
lakshya 10:f93407b97750 286 Mmnt[i]=bcopy[(i+1)%3]*tauc[(i+2)%3]-bcopy[(i+2)%3]*tauc[(i+1)%3];
lakshya 10:f93407b97750 287 Mmnt[i]/=(den*den); // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 288 }
lakshya 10:f93407b97750 289 infflag=0;
lakshya 10:f93407b97750 290 for (i=0; i<3 && infflag==0; i++)
lakshya 10:f93407b97750 291 {
lakshya 10:f93407b97750 292 if (isinf(Mmnt[i])==1 || isnan(Mmnt[i])==1)
lakshya 10:f93407b97750 293 infflag=1;
lakshya 10:f93407b97750 294 }
lakshya 10:f93407b97750 295 if (infflag==1)
lakshya 10:f93407b97750 296 {
lakshya 10:f93407b97750 297 for (i=0; i<3; i++)
lakshya 10:f93407b97750 298 Mmnt[i]=2*MmntMax;
lakshya 10:f93407b97750 299 }
lakshya 10:f93407b97750 300
lakshya 10:f93407b97750 301 }
lakshya 10:f93407b97750 302 else if(controlmode1==1)
lakshya 10:f93407b97750 303 {
lakshya 10:f93407b97750 304 for(i=0;i<3;i++)
lakshya 10:f93407b97750 305 {
lakshya 10:f93407b97750 306 Mmnt[i]=-kdetumble*(b[(i+1)%3]*omega[(i+2)%3]-b[(i+2)%3]*omega[(i+1)%3]); // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 307 }
lakshya 10:f93407b97750 308 }
lakshya 10:f93407b97750 309 for(i=0;i<3;i++)
lakshya 10:f93407b97750 310 {
lakshya 10:f93407b97750 311 moment[i]=Mmnt[i]; // Unit: Ampere*Meter^2
lakshya 10:f93407b97750 312 }
lakshya 10:f93407b97750 313 }
sakthipriya 0:7b4c00e3912f 314
sakthipriya 0:7b4c00e3912f 315 I2C i2c (PTC9,PTC8); //PTC9-sda,PTC8-scl for the attitude sensors and battery gauge
sakthipriya 0:7b4c00e3912f 316
sakthipriya 0:7b4c00e3912f 317 void FCTN_ACS_INIT(void); //initialization of registers happens
sakthipriya 0:7b4c00e3912f 318 void FCTN_ATS_DATA_ACQ(); //data is obtained
sakthipriya 0:7b4c00e3912f 319 void T_OUT(); //timeout function to stop infinite loop
sakthipriya 0:7b4c00e3912f 320 Timeout to; //Timeout variable to
sakthipriya 0:7b4c00e3912f 321 int toFlag;
sakthipriya 0:7b4c00e3912f 322
sakthipriya 0:7b4c00e3912f 323 int count =0; // Time for which the BAE uC is running (in seconds)
sakthipriya 0:7b4c00e3912f 324 void T_OUT()
sakthipriya 0:7b4c00e3912f 325 {
sakthipriya 0:7b4c00e3912f 326 toFlag=0; //as T_OUT function gets called the while loop gets terminated
sakthipriya 0:7b4c00e3912f 327 }
sakthipriya 0:7b4c00e3912f 328
sakthipriya 0:7b4c00e3912f 329
sakthipriya 0:7b4c00e3912f 330 //DEFINING VARIABLES
sakthipriya 0:7b4c00e3912f 331 char cmd[2];
sakthipriya 0:7b4c00e3912f 332 char raw_gyro[6];
sakthipriya 0:7b4c00e3912f 333 char raw_mag[6];
sakthipriya 0:7b4c00e3912f 334 char store,status;
sakthipriya 0:7b4c00e3912f 335 int16_t bit_data;
sakthipriya 0:7b4c00e3912f 336 float gyro_data[3], mag_data[3],combined_values[6];
sakthipriya 0:7b4c00e3912f 337 float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps
sakthipriya 0:7b4c00e3912f 338 float senstivity_mag =32.768; //senstivity is obtained from 2^15/1000microtesla
sakthipriya 0:7b4c00e3912f 339 float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0};
sakthipriya 0:7b4c00e3912f 340
sakthipriya 0:7b4c00e3912f 341 void FCTN_ACS_INIT()
sakthipriya 0:7b4c00e3912f 342 {
lakshya 10:f93407b97750 343 ACS_INIT_STATUS = 's'; //set ACS_INIT_STATUS flag
sakthipriya 3:07e15677a75c 344 //FLAG();
sakthipriya 0:7b4c00e3912f 345 pc_acs.printf("Attitude sensor init called \n \r");
sakthipriya 0:7b4c00e3912f 346 //FLAG();
lakshya 16:f9e484dc505e 347 /* as of now no reset
lakshya 16:f9e484dc505e 348 cmd[0]=RESETREQ;
sakthipriya 0:7b4c00e3912f 349 cmd[1]=BIT_RESREQ;
sakthipriya 0:7b4c00e3912f 350 i2c.write(SLAVE_ADDR,cmd,2); //When 0x01 is written in reset request register Emulates a hard power down/power up
sakthipriya 0:7b4c00e3912f 351 wait_ms(2000); //waiting for loading configuration file stored in EEPROM
lakshya 16:f9e484dc505e 352 // */
lakshya 16:f9e484dc505e 353 //wait_ms(3000);
sakthipriya 0:7b4c00e3912f 354 cmd[0]=SENTRALSTATUS;
sakthipriya 0:7b4c00e3912f 355 i2c.write(SLAVE_ADDR,cmd,1);
sakthipriya 0:7b4c00e3912f 356 i2c.read(SLAVE_ADDR_READ,&store,1);
lakshya 16:f9e484dc505e 357 wait_ms(20); // initially 100
sakthipriya 0:7b4c00e3912f 358 //to check whether EEPROM is uploaded
sakthipriya 0:7b4c00e3912f 359 switch((int)store) {
lakshya 16:f9e484dc505e 360 case(3): { //actually this state correct
sakthipriya 0:7b4c00e3912f 361 break;
sakthipriya 0:7b4c00e3912f 362 }
sakthipriya 0:7b4c00e3912f 363 case(11): {
sakthipriya 0:7b4c00e3912f 364 break;
sakthipriya 0:7b4c00e3912f 365 }
sakthipriya 0:7b4c00e3912f 366 default: {
sakthipriya 0:7b4c00e3912f 367 cmd[0]=RESETREQ;
sakthipriya 0:7b4c00e3912f 368 cmd[1]=BIT_RESREQ;
sakthipriya 0:7b4c00e3912f 369 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 16:f9e484dc505e 370 wait_ms(2000);//see if it can be changed
sakthipriya 0:7b4c00e3912f 371 }
sakthipriya 0:7b4c00e3912f 372 }
lakshya 16:f9e484dc505e 373 pc_acs.printf("\n\n\rwait is 1 \n\r");
sakthipriya 0:7b4c00e3912f 374 pc_acs.printf("Sentral Status is %x\n \r",(int)store);
lakshya 17:fc782f7548c6 375
lakshya 17:fc782f7548c6 376 /*ways to make the sensors work even if one sensor cups first making data rate 0x00
lakshya 17:fc782f7548c6 377 1>make data rate 0x00 change the enable events register value to 0x0A or 0x22 as suitable
lakshya 17:fc782f7548c6 378 2>change the condition for getting the values from the sensors i.e in data_acq function
lakshya 17:fc782f7548c6 379 3>check the other register values
lakshya 17:fc782f7548c6 380
lakshya 17:fc782f7548c6 381 other method is pass through working in how it works / decoding SENtral algorithms
lakshya 17:fc782f7548c6 382 */
lakshya 17:fc782f7548c6 383
sakthipriya 0:7b4c00e3912f 384 cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors
sakthipriya 0:7b4c00e3912f 385 cmd[1]=BIT_RUN_ENB;
sakthipriya 0:7b4c00e3912f 386 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 17:fc782f7548c6 387 wait_ms(100);
sakthipriya 0:7b4c00e3912f 388 cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer
sakthipriya 0:7b4c00e3912f 389 cmd[1]=BIT_MAGODR;
sakthipriya 0:7b4c00e3912f 390 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 17:fc782f7548c6 391 wait_ms(100);
lakshya 17:fc782f7548c6 392 //___________________________________________________________________________________________________
lakshya 17:fc782f7548c6 393
lakshya 17:fc782f7548c6 394 /*just leave it then see what happens?? the gyro data without inputing anything*/
lakshya 17:fc782f7548c6 395
lakshya 17:fc782f7548c6 396 //___________________________________________________________________________________________________
sakthipriya 0:7b4c00e3912f 397 cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope
sakthipriya 0:7b4c00e3912f 398 cmd[1]=BIT_GYROODR;
sakthipriya 0:7b4c00e3912f 399 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 16:f9e484dc505e 400 wait_ms(1);
sakthipriya 0:7b4c00e3912f 401 cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values
lakshya 17:fc782f7548c6 402 cmd[1]=0x00;//actually 0x00
sakthipriya 0:7b4c00e3912f 403 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 16:f9e484dc505e 404 wait_ms(1);
sakthipriya 0:7b4c00e3912f 405 cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values
lakshya 17:fc782f7548c6 406 cmd[1]=BIT_EVT_ENB;//__________________________________________________________________________________
lakshya 17:fc782f7548c6 407 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 17:fc782f7548c6 408 wait_ms(1);
lakshya 17:fc782f7548c6 409 //_______________________________________________________________________________//
lakshya 17:fc782f7548c6 410
lakshya 18:3662058a7c10 411 /*start as of now this approach i.e pass through state*/
lakshya 18:3662058a7c10 412 /*
lakshya 18:3662058a7c10 413 cmd[0]=0x35;//event status
lakshya 18:3662058a7c10 414 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 18:3662058a7c10 415 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 18:3662058a7c10 416 wait_ms(1);
lakshya 18:3662058a7c10 417 pc_acs.printf("\n\rEvent Status at start is %x\n \r",(int)status);
lakshya 18:3662058a7c10 418
lakshya 17:fc782f7548c6 419 cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values
lakshya 17:fc782f7548c6 420 cmd[1]=0x01;//Places sentral in standby state
sakthipriya 0:7b4c00e3912f 421 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 16:f9e484dc505e 422 wait_ms(1);
lakshya 17:fc782f7548c6 423
lakshya 17:fc782f7548c6 424
lakshya 17:fc782f7548c6 425 cmd[0]=0xA0; //Pass through control register
lakshya 17:fc782f7548c6 426 cmd[1]=0x01;//places SENtral in pass through state
lakshya 17:fc782f7548c6 427 i2c.write(SLAVE_ADDR,cmd,2);
lakshya 17:fc782f7548c6 428 wait_ms(1);
lakshya 17:fc782f7548c6 429
lakshya 17:fc782f7548c6 430
lakshya 17:fc782f7548c6 431 cmd[0]=0x9E;//Pass through status reg
lakshya 17:fc782f7548c6 432 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 433 i2c.read(SLAVE_ADDR_READ,&store,1);
lakshya 17:fc782f7548c6 434 wait_ms(1);
lakshya 17:fc782f7548c6 435 printf("\n\r the value of the pass through status register is = %x",(int)store);
lakshya 17:fc782f7548c6 436
lakshya 18:3662058a7c10 437 cmd[0]=0x35;//event status
lakshya 18:3662058a7c10 438 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 18:3662058a7c10 439 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 18:3662058a7c10 440 wait_ms(1);
lakshya 18:3662058a7c10 441 pc_acs.printf("\n\rEvent Status at end initialization is %x\n \r",(int)status);
lakshya 17:fc782f7548c6 442 */
lakshya 18:3662058a7c10 443
lakshya 17:fc782f7548c6 444 //_______________________________________________________________________________//
lakshya 17:fc782f7548c6 445
lakshya 10:f93407b97750 446 ACS_INIT_STATUS = 'c'; //set ACS_INIT_STATUS flag
sakthipriya 0:7b4c00e3912f 447 }
sakthipriya 0:7b4c00e3912f 448
sakthipriya 0:7b4c00e3912f 449 void FCTN_ATS_DATA_ACQ()
sakthipriya 0:7b4c00e3912f 450 {
Bragadeesh153 13:fb7facaf308b 451 ACS_DATA_ACQ_STATUS = 1; //set ACS_DATA_ACQ_STATUS flag for att sens 2
Bragadeesh153 13:fb7facaf308b 452 if( ACS_ATS_ENABLE == 1)
sakthipriya 0:7b4c00e3912f 453 {
sakthipriya 0:7b4c00e3912f 454 FLAG();
sakthipriya 0:7b4c00e3912f 455 pc_acs.printf("attitude sensor execution called \n \r");
sakthipriya 0:7b4c00e3912f 456 toFlag=1; //toFlag is set to 1 so that it enters while loop
sakthipriya 0:7b4c00e3912f 457 to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated
sakthipriya 0:7b4c00e3912f 458 while(toFlag) {
sakthipriya 0:7b4c00e3912f 459 cmd[0]=EVT_STATUS;
sakthipriya 0:7b4c00e3912f 460 i2c.write(SLAVE_ADDR,cmd,1);
sakthipriya 0:7b4c00e3912f 461 i2c.read(SLAVE_ADDR_READ,&status,1);
sakthipriya 0:7b4c00e3912f 462 wait_ms(100);
sakthipriya 0:7b4c00e3912f 463 pc_acs.printf("Event Status is %x\n \r",(int)status);
sakthipriya 0:7b4c00e3912f 464 //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take
lakshya 17:fc782f7548c6 465 if(((int)status&40)==40){ //when both work
lakshya 17:fc782f7548c6 466 /// if((int)status==8){ //for just mag
lakshya 17:fc782f7548c6 467 /// if((int)status == 36){ //for just gyro as status 24(in binary) = 36 in decimal
lakshya 18:3662058a7c10 468 /// if((int)status==___yet to be decided___){ //for pass through state see how it tworks
lakshya 17:fc782f7548c6 469
lakshya 17:fc782f7548c6 470 /*don't ask for info if the gyro cupped as interrupt makes it go haywire that is enables the pins for gyro*/
lakshya 17:fc782f7548c6 471 cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x
lakshya 17:fc782f7548c6 472 i2c.write(SLAVE_ADDR,cmd,1);
sakthipriya 0:7b4c00e3912f 473 i2c.read(SLAVE_ADDR_READ,raw_gyro,6);
lakshya 17:fc782f7548c6 474 printf("\n\n\n\r here reached");
lakshya 17:fc782f7548c6 475 /*editing the data*/
lakshya 17:fc782f7548c6 476
sakthipriya 0:7b4c00e3912f 477 cmd[0]=MAG_XOUT_H; //LSB of x
sakthipriya 0:7b4c00e3912f 478 i2c.write(SLAVE_ADDR,cmd,1);
sakthipriya 0:7b4c00e3912f 479 i2c.read(SLAVE_ADDR_READ,raw_mag,6);
sakthipriya 0:7b4c00e3912f 480 // pc_acs.printf("\nGyro Values:\n");
sakthipriya 0:7b4c00e3912f 481 for(int i=0; i<3; i++) {
sakthipriya 0:7b4c00e3912f 482 //concatenating gyro LSB and MSB to get 16 bit signed data values
sakthipriya 0:7b4c00e3912f 483 bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i];
sakthipriya 0:7b4c00e3912f 484 gyro_data[i]=(float)bit_data;
sakthipriya 0:7b4c00e3912f 485 gyro_data[i]=gyro_data[i]/senstivity_gyro;
sakthipriya 0:7b4c00e3912f 486 gyro_data[i]+=gyro_error[i];
lakshya 15:e09aaaccf134 487 // pc_acs.printf("%f\t",gyro_data[i]);
sakthipriya 0:7b4c00e3912f 488 }
lakshya 17:fc782f7548c6 489
lakshya 17:fc782f7548c6 490 /*read the status values to determine the actual condition/registers values for the gyro/mag off case */
lakshya 17:fc782f7548c6 491 //starts here
lakshya 17:fc782f7548c6 492
lakshya 17:fc782f7548c6 493 cmd[0]=0x35;//event status
lakshya 17:fc782f7548c6 494 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 495 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 496 wait_ms(1);
lakshya 18:3662058a7c10 497 pc_acs.printf("\n\rEvent Status at the end is %x",(int)status);
lakshya 17:fc782f7548c6 498
lakshya 17:fc782f7548c6 499
lakshya 17:fc782f7548c6 500 cmd[0]=0x33;//enable events
lakshya 17:fc782f7548c6 501 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 502 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 503 wait_ms(1);
lakshya 18:3662058a7c10 504 pc_acs.printf("\n\rEnable events is %x",(int)status);
lakshya 17:fc782f7548c6 505
lakshya 17:fc782f7548c6 506
lakshya 17:fc782f7548c6 507 cmd[0]=0x57;//gyro rate
lakshya 17:fc782f7548c6 508 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 509 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 510 wait_ms(1);
lakshya 18:3662058a7c10 511 pc_acs.printf("\n\rgyro rate is %x",(int)status);
lakshya 17:fc782f7548c6 512
lakshya 17:fc782f7548c6 513
lakshya 17:fc782f7548c6 514 cmd[0]=0x55;//mag rate 0x00 indicate value lost
lakshya 17:fc782f7548c6 515 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 516 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 517 wait_ms(1);
lakshya 18:3662058a7c10 518 pc_acs.printf("\n\rmag rate is %x",(int)status);
lakshya 17:fc782f7548c6 519
lakshya 17:fc782f7548c6 520
lakshya 17:fc782f7548c6 521 cmd[0]=0x36;//sensorstatus
lakshya 17:fc782f7548c6 522 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 523 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 524 wait_ms(1);
lakshya 18:3662058a7c10 525 pc_acs.printf("\n\rsensor Status is %x",(int)status);
lakshya 17:fc782f7548c6 526
lakshya 17:fc782f7548c6 527
lakshya 17:fc782f7548c6 528 cmd[0]=0x50;//error register
lakshya 17:fc782f7548c6 529 i2c.write(SLAVE_ADDR,cmd,1);
lakshya 17:fc782f7548c6 530 i2c.read(SLAVE_ADDR_READ,&status,1);
lakshya 17:fc782f7548c6 531 wait_ms(1);
lakshya 17:fc782f7548c6 532 pc_acs.printf("\n\rerror register value is %x\n \r",(int)status);
lakshya 17:fc782f7548c6 533
lakshya 17:fc782f7548c6 534 //end here
lakshya 17:fc782f7548c6 535
sakthipriya 0:7b4c00e3912f 536 // pc_acs.printf("\nMag Values:\n");
sakthipriya 0:7b4c00e3912f 537 for(int i=0; i<3; i++) {
sakthipriya 0:7b4c00e3912f 538 //concatenating mag LSB and MSB to get 16 bit signed data values
sakthipriya 0:7b4c00e3912f 539 bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i];
sakthipriya 0:7b4c00e3912f 540 mag_data[i]=(float)bit_data;
sakthipriya 0:7b4c00e3912f 541 mag_data[i]=mag_data[i]/senstivity_mag;
sakthipriya 0:7b4c00e3912f 542 mag_data[i]+=mag_error[i];
lakshya 15:e09aaaccf134 543 //pc_acs.printf("%f\t",mag_data[i]);
sakthipriya 0:7b4c00e3912f 544 }
sakthipriya 0:7b4c00e3912f 545 for(int i=0; i<3; i++) {
sakthipriya 6:036d08b62785 546 // data[i]=gyro_data[i];
sakthipriya 6:036d08b62785 547 actual_data.AngularSpeed_actual[i] = gyro_data[i];
sakthipriya 6:036d08b62785 548 actual_data.Bvalue_actual[i] = mag_data[i];
sakthipriya 6:036d08b62785 549 //data[i+3]=mag_data[i];
sakthipriya 0:7b4c00e3912f 550 }
sakthipriya 0:7b4c00e3912f 551 // return(combined_values); //returning poiter combined values
sakthipriya 0:7b4c00e3912f 552 }
sakthipriya 0:7b4c00e3912f 553 //checking for the error
sakthipriya 0:7b4c00e3912f 554 else if (((int)status&2)==2) {
sakthipriya 0:7b4c00e3912f 555 FCTN_ACS_INIT(); //when there is any error then Again inilization is done to remove error
sakthipriya 0:7b4c00e3912f 556 }
sakthipriya 0:7b4c00e3912f 557 }
sakthipriya 0:7b4c00e3912f 558 }
sakthipriya 0:7b4c00e3912f 559 else //ACS_DATA_ACQ_STATUS = ACS_DATA_ACQ_FAILURE
sakthipriya 0:7b4c00e3912f 560 {
lakshya 10:f93407b97750 561 ACS_DATA_ACQ_STATUS = 'f';
sakthipriya 0:7b4c00e3912f 562 }
Bragadeesh153 13:fb7facaf308b 563 ACS_DATA_ACQ_STATUS = 0; //clear ACS_DATA_ACQ_STATUS flag for att sens 2
sakthipriya 0:7b4c00e3912f 564 }
sakthipriya 0:7b4c00e3912f 565
sakthipriya 0:7b4c00e3912f 566 void FCTN_ACS_GENPWM_MAIN(float Moment[3])
sakthipriya 0:7b4c00e3912f 567 {
sakthipriya 0:7b4c00e3912f 568 printf("\n\rEntered executable PWMGEN function\n"); // entering the PWMGEN executable function
sakthipriya 0:7b4c00e3912f 569
sakthipriya 0:7b4c00e3912f 570 float l_duty_cycle_x=0; //Duty cycle of Moment in x direction
sakthipriya 0:7b4c00e3912f 571 float l_current_x=0; //Current sent in x TR's
sakthipriya 0:7b4c00e3912f 572 float l_duty_cycle_y=0; //Duty cycle of Moment in y direction
sakthipriya 0:7b4c00e3912f 573 float l_current_y=0; //Current sent in y TR's
sakthipriya 0:7b4c00e3912f 574 float l_duty_cycle_z=0; //Duty cycle of Moment in z direction
sakthipriya 0:7b4c00e3912f 575 float l_current_z=0; //Current sent in z TR's
sakthipriya 0:7b4c00e3912f 576
sakthipriya 0:7b4c00e3912f 577
sakthipriya 0:7b4c00e3912f 578 for(int i = 0 ; i<3;i++)
sakthipriya 0:7b4c00e3912f 579 {
sakthipriya 0:7b4c00e3912f 580 // printf(" %f \t ",Moment[i]); // taking the moment values from control algorithm as inputs
sakthipriya 0:7b4c00e3912f 581 }
sakthipriya 0:7b4c00e3912f 582
sakthipriya 0:7b4c00e3912f 583 //----------------------------- x-direction TR --------------------------------------------//
sakthipriya 0:7b4c00e3912f 584
sakthipriya 0:7b4c00e3912f 585
sakthipriya 0:7b4c00e3912f 586 float l_moment_x = Moment[0]; //Moment in x direction
sakthipriya 0:7b4c00e3912f 587
sakthipriya 0:7b4c00e3912f 588 phase_TR_x = 1; // setting the default current direction
sakthipriya 0:7b4c00e3912f 589 if (l_moment_x <0)
sakthipriya 0:7b4c00e3912f 590 {
sakthipriya 0:7b4c00e3912f 591 phase_TR_x = 0; // if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
sakthipriya 0:7b4c00e3912f 592 l_moment_x = abs(l_moment_x);
sakthipriya 0:7b4c00e3912f 593 }
sakthipriya 0:7b4c00e3912f 594
sakthipriya 0:7b4c00e3912f 595 l_current_x = l_moment_x * TR_CONSTANT ; //Moment and Current always have the linear relationship
lakshya 10:f93407b97750 596 printf("current in trx is %f \r \n",l_current_x);
lakshya 10:f93407b97750 597 if( l_current_x>0 && l_current_x < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
lakshya 10:f93407b97750 598 {
lakshya 10:f93407b97750 599 l_duty_cycle_x = 3*10000000*pow(l_current_x,3)- 90216*pow(l_current_x,2) + 697.78*l_current_x - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 600 printf("DC for trx is %f \r \n",l_duty_cycle_x);
lakshya 10:f93407b97750 601 PWM1.period(TIME_PERIOD);
lakshya 10:f93407b97750 602 PWM1 = l_duty_cycle_x/100 ;
lakshya 10:f93407b97750 603 }
lakshya 10:f93407b97750 604 else if (l_current_x >= 0.0016 && l_current_x < 0.0171)
lakshya 10:f93407b97750 605 {
lakshya 10:f93407b97750 606 l_duty_cycle_x = - 76880*pow(l_current_x,3) + 1280.8*pow(l_current_x,2) + 583.78*l_current_x + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 607 printf("DC for trx is %f \r \n",l_duty_cycle_x);
lakshya 10:f93407b97750 608 PWM1.period(TIME_PERIOD);
lakshya 10:f93407b97750 609 PWM1 = l_duty_cycle_x/100 ;
lakshya 10:f93407b97750 610 }
lakshya 10:f93407b97750 611 else if(l_current_x >= 0.0171 && l_current_x < 0.1678)
lakshya 10:f93407b97750 612 {
lakshya 10:f93407b97750 613 l_duty_cycle_x = 275.92*pow(l_current_x,2) + 546.13*l_current_x + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 614 printf("DC for trx is %f \r \n",l_duty_cycle_x);
lakshya 10:f93407b97750 615 PWM1.period(TIME_PERIOD);
lakshya 10:f93407b97750 616 PWM1 = l_duty_cycle_x/100 ;
lakshya 10:f93407b97750 617 }
lakshya 10:f93407b97750 618 else if(l_current_x==0)
lakshya 10:f93407b97750 619 {
lakshya 10:f93407b97750 620 printf("\n \r l_current_x====0");
lakshya 10:f93407b97750 621 l_duty_cycle_x = 0; // default value of duty cycle
lakshya 10:f93407b97750 622 printf("DC for trx is %f \r \n",l_duty_cycle_x);
lakshya 10:f93407b97750 623 PWM1.period(TIME_PERIOD);
lakshya 10:f93407b97750 624 PWM1 = l_duty_cycle_x/100 ;
lakshya 10:f93407b97750 625 }
lakshya 10:f93407b97750 626 else //not necessary
lakshya 10:f93407b97750 627 {
lakshya 10:f93407b97750 628 g_err_flag_TR_x = 1;
lakshya 10:f93407b97750 629 }
lakshya 10:f93407b97750 630
lakshya 10:f93407b97750 631 //------------------------------------- y-direction TR--------------------------------------//
lakshya 10:f93407b97750 632
lakshya 10:f93407b97750 633
lakshya 10:f93407b97750 634 float l_moment_y = Moment[1]; //Moment in y direction
lakshya 10:f93407b97750 635
lakshya 10:f93407b97750 636 phase_TR_y = 1; // setting the default current direction
lakshya 10:f93407b97750 637 if (l_moment_y <0)
lakshya 10:f93407b97750 638 {
lakshya 10:f93407b97750 639 phase_TR_y = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
lakshya 10:f93407b97750 640 l_moment_y = abs(l_moment_y);
lakshya 10:f93407b97750 641 }
lakshya 10:f93407b97750 642
lakshya 10:f93407b97750 643
lakshya 10:f93407b97750 644 l_current_y = l_moment_y * TR_CONSTANT ; //Moment and Current always have the linear relationship
lakshya 10:f93407b97750 645 printf("current in try is %f \r \n",l_current_y);
lakshya 10:f93407b97750 646 if( l_current_y>0 && l_current_y < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
lakshya 10:f93407b97750 647 {
lakshya 10:f93407b97750 648 l_duty_cycle_y = 3*10000000*pow(l_current_y,3)- 90216*pow(l_current_y,2) + 697.78*l_current_y - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 649 printf("DC for try is %f \r \n",l_duty_cycle_y);
lakshya 10:f93407b97750 650 PWM2.period(TIME_PERIOD);
lakshya 10:f93407b97750 651 PWM2 = l_duty_cycle_y/100 ;
lakshya 10:f93407b97750 652 }
lakshya 10:f93407b97750 653 else if (l_current_y >= 0.0016 && l_current_y < 0.0171)
lakshya 10:f93407b97750 654 {
lakshya 10:f93407b97750 655 l_duty_cycle_y = - 76880*pow(l_current_y,3) + 1280.8*pow(l_current_y,2) + 583.78*l_current_y + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 656 printf("DC for try is %f \r \n",l_duty_cycle_y);
lakshya 10:f93407b97750 657 PWM2.period(TIME_PERIOD);
lakshya 10:f93407b97750 658 PWM2 = l_duty_cycle_y/100 ;
lakshya 10:f93407b97750 659 }
lakshya 10:f93407b97750 660 else if(l_current_y >= 0.0171 && l_current_y < 0.1678)
lakshya 10:f93407b97750 661 {
lakshya 10:f93407b97750 662 l_duty_cycle_y = 275.92*pow(l_current_y,2) + 546.13*l_current_y + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 663 printf("DC for try is %f \r \n",l_duty_cycle_y);
lakshya 10:f93407b97750 664 PWM2.period(TIME_PERIOD);
lakshya 10:f93407b97750 665 PWM2 = l_duty_cycle_y/100 ;
lakshya 10:f93407b97750 666 }
lakshya 10:f93407b97750 667 else if(l_current_y==0)
lakshya 10:f93407b97750 668 {
lakshya 10:f93407b97750 669 printf("\n \r l_current_y====0");
lakshya 10:f93407b97750 670 l_duty_cycle_y = 0; // default value of duty cycle
lakshya 10:f93407b97750 671 printf("DC for try is %f \r \n",l_duty_cycle_y);
lakshya 10:f93407b97750 672 PWM2.period(TIME_PERIOD);
lakshya 10:f93407b97750 673 PWM2 = l_duty_cycle_y/100 ;
lakshya 10:f93407b97750 674 }
lakshya 10:f93407b97750 675 else // not necessary
lakshya 10:f93407b97750 676 {
lakshya 10:f93407b97750 677 g_err_flag_TR_y = 1;
lakshya 10:f93407b97750 678 }
lakshya 10:f93407b97750 679
lakshya 10:f93407b97750 680 //----------------------------------------------- z-direction TR -------------------------//
lakshya 10:f93407b97750 681
lakshya 10:f93407b97750 682
lakshya 10:f93407b97750 683 float l_moment_z = Moment[2]; //Moment in z direction
lakshya 10:f93407b97750 684
lakshya 10:f93407b97750 685 phase_TR_z = 1; // setting the default current direction
lakshya 10:f93407b97750 686 if (l_moment_z <0)
lakshya 10:f93407b97750 687 {
lakshya 10:f93407b97750 688 phase_TR_z = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
lakshya 10:f93407b97750 689 l_moment_z = abs(l_moment_z);
lakshya 10:f93407b97750 690 }
lakshya 10:f93407b97750 691
lakshya 10:f93407b97750 692
lakshya 10:f93407b97750 693 l_current_z = l_moment_z * TR_CONSTANT ; //Moment and Current always have the linear relationship
lakshya 10:f93407b97750 694 printf("current in trz is %f \r \n",l_current_z);
lakshya 10:f93407b97750 695 if( l_current_z>0 && l_current_z < 0.0016 ) //Current and Duty cycle have the linear relationship between 1% and 100%
lakshya 10:f93407b97750 696 {
lakshya 10:f93407b97750 697 l_duty_cycle_z = 3*10000000*pow(l_current_z,3)- 90216*pow(l_current_z,2) + 697.78*l_current_z - 0.0048; // calculating upto 0.1% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 698 printf("DC for trz is %f \r \n",l_duty_cycle_z);
lakshya 10:f93407b97750 699 PWM3.period(TIME_PERIOD);
lakshya 10:f93407b97750 700 PWM3 = l_duty_cycle_z/100 ;
lakshya 10:f93407b97750 701 }
lakshya 10:f93407b97750 702 else if (l_current_z >= 0.0016 && l_current_z < 0.0171)
lakshya 10:f93407b97750 703 {
lakshya 10:f93407b97750 704 l_duty_cycle_z = - 76880*pow(l_current_z,3) + 1280.8*pow(l_current_z,2) + 583.78*l_current_z + 0.0281; // calculating upto 10% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 705 printf("DC for trz is %f \r \n",l_duty_cycle_z);
lakshya 10:f93407b97750 706 PWM3.period(TIME_PERIOD);
lakshya 10:f93407b97750 707 PWM3 = l_duty_cycle_z/100 ;
lakshya 10:f93407b97750 708 }
lakshya 10:f93407b97750 709 else if(l_current_z >= 0.0171 && l_current_z < 0.1678)
lakshya 10:f93407b97750 710 {
lakshya 10:f93407b97750 711 l_duty_cycle_z = 275.92*pow(l_current_z,2) + 546.13*l_current_z + 0.5316; // calculating upto 100% dutycycle by polynomial interpolation
lakshya 10:f93407b97750 712 printf("DC for trz is %f \r \n",l_duty_cycle_z);
lakshya 10:f93407b97750 713 PWM3.period(TIME_PERIOD);
lakshya 10:f93407b97750 714 PWM3 = l_duty_cycle_z/100 ;
lakshya 10:f93407b97750 715 }
lakshya 10:f93407b97750 716 else if(l_current_z==0)
lakshya 10:f93407b97750 717 {
lakshya 10:f93407b97750 718 printf("\n \r l_current_z====0");
lakshya 10:f93407b97750 719 l_duty_cycle_z = 0; // default value of duty cycle
lakshya 10:f93407b97750 720 printf("DC for trz is %f \r \n",l_duty_cycle_z);
lakshya 10:f93407b97750 721 PWM3.period(TIME_PERIOD);
lakshya 10:f93407b97750 722 PWM3 = l_duty_cycle_z/100 ;
lakshya 10:f93407b97750 723 }
lakshya 10:f93407b97750 724 else // not necessary
lakshya 10:f93407b97750 725 {
lakshya 10:f93407b97750 726 g_err_flag_TR_z = 1;
lakshya 10:f93407b97750 727 }
lakshya 10:f93407b97750 728
lakshya 10:f93407b97750 729 //-----------------------------------------exiting the function-----------------------------------//
lakshya 10:f93407b97750 730
lakshya 10:f93407b97750 731 printf("\n\rExited executable PWMGEN function\n\r"); // stating the successful exit of TR function
lakshya 10:f93407b97750 732
lakshya 10:f93407b97750 733 }
lakshya 10:f93407b97750 734
lakshya 10:f93407b97750 735
lakshya 10:f93407b97750 736 /*void FCTN_ACS_GENPWM_MAIN(float Moment[3])
lakshya 10:f93407b97750 737 {
lakshya 10:f93407b97750 738 printf("\n\rEntered executable PWMGEN function\n"); // entering the PWMGEN executable function
lakshya 10:f93407b97750 739
lakshya 10:f93407b97750 740 float l_duty_cycle_x=0; //Duty cycle of Moment in x direction
lakshya 10:f93407b97750 741 float l_current_x=0; //Current sent in x TR's
lakshya 10:f93407b97750 742 float l_duty_cycle_y=0; //Duty cycle of Moment in y direction
lakshya 10:f93407b97750 743 float l_current_y=0; //Current sent in y TR's
lakshya 10:f93407b97750 744 float l_duty_cycle_z=0; //Duty cycle of Moment in z direction
lakshya 10:f93407b97750 745 float l_current_z=0; //Current sent in z TR's
lakshya 10:f93407b97750 746
lakshya 10:f93407b97750 747
lakshya 10:f93407b97750 748 for(int i = 0 ; i<3;i++)
lakshya 10:f93407b97750 749 {
lakshya 10:f93407b97750 750 printf("pwm %f \t ",Moment[i]); // taking the moment values from control algorithm as inputs
lakshya 10:f93407b97750 751 }
lakshya 10:f93407b97750 752
lakshya 10:f93407b97750 753 //----------------------------- x-direction TR --------------------------------------------//
lakshya 10:f93407b97750 754
lakshya 10:f93407b97750 755
lakshya 10:f93407b97750 756 float l_moment_x = Moment[0]; //Moment in x direction
lakshya 10:f93407b97750 757
lakshya 10:f93407b97750 758 phase_TR_x = 1; // setting the default current direction
lakshya 10:f93407b97750 759 if (l_moment_x <0)
lakshya 10:f93407b97750 760 {
lakshya 10:f93407b97750 761 phase_TR_x = 0; // if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
lakshya 10:f93407b97750 762 l_moment_x = abs(l_moment_x);
lakshya 10:f93407b97750 763 }
lakshya 10:f93407b97750 764
lakshya 10:f93407b97750 765 l_current_x = l_moment_x * TR_CONSTANT ; //Moment and Current always have the linear relationship
sakthipriya 0:7b4c00e3912f 766 pc_acs.printf("current in trx is %f \r \n",l_current_x);
sakthipriya 0:7b4c00e3912f 767 if( l_current_x>0 && l_current_x < 0.006 ) //Current and Duty cycle have the linear relationship between 1% and 100%
sakthipriya 0:7b4c00e3912f 768 {
sakthipriya 0:7b4c00e3912f 769 l_duty_cycle_x = 6*1000000*pow(l_current_x,4) - 377291*pow(l_current_x,3) + 4689.6*pow(l_current_x,2) + 149.19*l_current_x - 0.0008; // calculating upto 0.1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 770 pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
sakthipriya 0:7b4c00e3912f 771 PWM1.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 772 PWM1 = l_duty_cycle_x/100 ;
sakthipriya 0:7b4c00e3912f 773 }
sakthipriya 0:7b4c00e3912f 774 else if( l_current_x >= 0.006 && l_current_x < 0.0116)
sakthipriya 0:7b4c00e3912f 775 {
sakthipriya 0:7b4c00e3912f 776 l_duty_cycle_x = 1*100000000*pow(l_current_x,4) - 5*1000000*pow(l_current_x,3) + 62603*pow(l_current_x,2) - 199.29*l_current_x + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 777 pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
sakthipriya 0:7b4c00e3912f 778 PWM1.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 779 PWM1 = l_duty_cycle_x/100 ;
sakthipriya 0:7b4c00e3912f 780 }
sakthipriya 0:7b4c00e3912f 781 else if (l_current_x >= 0.0116 && l_current_x < 0.0624)
sakthipriya 0:7b4c00e3912f 782 {
sakthipriya 0:7b4c00e3912f 783 l_duty_cycle_x = 212444*pow(l_current_x,4) - 33244*pow(l_current_x,3) + 1778.4*pow(l_current_x,2) + 120.91*l_current_x + 0.3878; // calculating upto 10% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 784 pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
sakthipriya 0:7b4c00e3912f 785 PWM1.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 786 PWM1 = l_duty_cycle_x/100 ;
sakthipriya 0:7b4c00e3912f 787 }
sakthipriya 0:7b4c00e3912f 788 else if(l_current_x >= 0.0624 && l_current_x < 0.555)
sakthipriya 0:7b4c00e3912f 789 {
sakthipriya 0:7b4c00e3912f 790 l_duty_cycle_x = 331.15*pow(l_current_x,4) - 368.09*pow(l_current_x,3) + 140.43*pow(l_current_x,2) + 158.59*l_current_x + 0.0338; // calculating upto 100% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 791 pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
sakthipriya 0:7b4c00e3912f 792 PWM1.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 793 PWM1 = l_duty_cycle_x/100 ;
sakthipriya 0:7b4c00e3912f 794 }
sakthipriya 0:7b4c00e3912f 795 else if(l_current_x==0)
sakthipriya 0:7b4c00e3912f 796 {
sakthipriya 0:7b4c00e3912f 797 printf("\n \r l_current_x====0");
sakthipriya 0:7b4c00e3912f 798 l_duty_cycle_x = 0; // default value of duty cycle
sakthipriya 0:7b4c00e3912f 799 pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x);
sakthipriya 0:7b4c00e3912f 800 PWM1.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 801 PWM1 = l_duty_cycle_x/100 ;
sakthipriya 0:7b4c00e3912f 802 }
sakthipriya 0:7b4c00e3912f 803 else //not necessary
sakthipriya 0:7b4c00e3912f 804 {
sakthipriya 0:7b4c00e3912f 805 g_err_flag_TR_x = 1;
sakthipriya 0:7b4c00e3912f 806 }
sakthipriya 0:7b4c00e3912f 807
sakthipriya 0:7b4c00e3912f 808 //------------------------------------- y-direction TR--------------------------------------//
sakthipriya 0:7b4c00e3912f 809
sakthipriya 0:7b4c00e3912f 810
sakthipriya 0:7b4c00e3912f 811 float l_moment_y = Moment[1]; //Moment in y direction
sakthipriya 0:7b4c00e3912f 812
sakthipriya 0:7b4c00e3912f 813 phase_TR_y = 1; // setting the default current direction
sakthipriya 0:7b4c00e3912f 814 if (l_moment_y <0)
sakthipriya 0:7b4c00e3912f 815 {
sakthipriya 0:7b4c00e3912f 816 phase_TR_y = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
sakthipriya 0:7b4c00e3912f 817 l_moment_y = abs(l_moment_y);
sakthipriya 0:7b4c00e3912f 818 }
sakthipriya 0:7b4c00e3912f 819
sakthipriya 0:7b4c00e3912f 820
sakthipriya 0:7b4c00e3912f 821 l_current_y = l_moment_y * TR_CONSTANT ; //Moment and Current always have the linear relationship
sakthipriya 0:7b4c00e3912f 822 pc_acs.printf("current in try is %f \r \n",l_current_y);
sakthipriya 0:7b4c00e3912f 823 if( l_current_y>0 && l_current_y < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100%
sakthipriya 0:7b4c00e3912f 824 {
sakthipriya 0:7b4c00e3912f 825 l_duty_cycle_y = 6*1000000*pow(l_current_y,4) - 377291*pow(l_current_y,3) + 4689.6*pow(l_current_y,2) + 149.19*l_current_y - 0.0008; // calculating upto 0.1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 826 pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
sakthipriya 0:7b4c00e3912f 827 PWM2.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 828 PWM2 = l_duty_cycle_y/100 ;
sakthipriya 0:7b4c00e3912f 829 }
sakthipriya 0:7b4c00e3912f 830 else if( l_current_y >= 0.006 && l_current_y < 0.0116)
sakthipriya 0:7b4c00e3912f 831 {
sakthipriya 0:7b4c00e3912f 832 l_duty_cycle_y = 1*100000000*pow(l_current_y,4) - 5*1000000*pow(l_current_y,3) + 62603*pow(l_current_y,2) - 199.29*l_current_y + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 833 pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
sakthipriya 0:7b4c00e3912f 834 PWM2.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 835 PWM2 = l_duty_cycle_y/100 ;
sakthipriya 0:7b4c00e3912f 836 }
sakthipriya 0:7b4c00e3912f 837 else if (l_current_y >= 0.0116&& l_current_y < 0.0624)
sakthipriya 0:7b4c00e3912f 838 {
sakthipriya 0:7b4c00e3912f 839 l_duty_cycle_y = 212444*pow(l_current_y,4) - 33244*pow(l_current_y,3) + 1778.4*pow(l_current_y,2) + 120.91*l_current_y + 0.3878;// calculating upto 10% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 840 pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
sakthipriya 0:7b4c00e3912f 841 PWM2.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 842 PWM2 = l_duty_cycle_y/100 ;
sakthipriya 0:7b4c00e3912f 843 }
sakthipriya 0:7b4c00e3912f 844 else if(l_current_y >= 0.0624 && l_current_y < 0.555)
sakthipriya 0:7b4c00e3912f 845 {
sakthipriya 0:7b4c00e3912f 846 l_duty_cycle_y = 331.15*pow(l_current_y,4) - 368.09*pow(l_current_y,3) + 140.43*pow(l_current_y,2) + 158.59*l_current_y + 0.0338;// calculating upto 100% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 847 pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
sakthipriya 0:7b4c00e3912f 848 PWM2.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 849 PWM2 = l_duty_cycle_y/100 ;
sakthipriya 0:7b4c00e3912f 850 }
sakthipriya 0:7b4c00e3912f 851 else if(l_current_y==0)
sakthipriya 0:7b4c00e3912f 852 {
sakthipriya 0:7b4c00e3912f 853 printf("\n \r l_current_y====0");
sakthipriya 0:7b4c00e3912f 854 l_duty_cycle_y = 0; // default value of duty cycle
sakthipriya 0:7b4c00e3912f 855 pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y);
sakthipriya 0:7b4c00e3912f 856 PWM2.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 857 PWM2 = l_duty_cycle_y/100 ;
sakthipriya 0:7b4c00e3912f 858 }
sakthipriya 0:7b4c00e3912f 859 else // not necessary
sakthipriya 0:7b4c00e3912f 860 {
sakthipriya 0:7b4c00e3912f 861 g_err_flag_TR_y = 1;
sakthipriya 0:7b4c00e3912f 862 }
sakthipriya 0:7b4c00e3912f 863
sakthipriya 0:7b4c00e3912f 864 //----------------------------------------------- z-direction TR -------------------------//
sakthipriya 0:7b4c00e3912f 865
sakthipriya 0:7b4c00e3912f 866
sakthipriya 0:7b4c00e3912f 867 float l_moment_z = Moment[2]; //Moment in z direction
sakthipriya 0:7b4c00e3912f 868
sakthipriya 0:7b4c00e3912f 869 phase_TR_z = 1; // setting the default current direction
sakthipriya 0:7b4c00e3912f 870 if (l_moment_z <0)
sakthipriya 0:7b4c00e3912f 871 {
sakthipriya 0:7b4c00e3912f 872 phase_TR_z = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high
sakthipriya 0:7b4c00e3912f 873 l_moment_z = abs(l_moment_z);
sakthipriya 0:7b4c00e3912f 874 }
sakthipriya 0:7b4c00e3912f 875
sakthipriya 0:7b4c00e3912f 876
sakthipriya 0:7b4c00e3912f 877 l_current_z = l_moment_z * TR_CONSTANT ; //Moment and Current always have the linear relationship
sakthipriya 0:7b4c00e3912f 878 pc_acs.printf("current in trz is %f \r \n",l_current_z);
sakthipriya 0:7b4c00e3912f 879 if( l_current_z>0 && l_current_z < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100%
sakthipriya 0:7b4c00e3912f 880 {
sakthipriya 0:7b4c00e3912f 881 l_duty_cycle_z = 6*1000000*pow(l_current_z,4) - 377291*pow(l_current_z,3) + 4689.6*pow(l_current_z,2) + 149.19*l_current_z - 0.0008;// calculating upto 0.1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 882 pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
sakthipriya 0:7b4c00e3912f 883 PWM3.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 884 PWM3 = l_duty_cycle_z/100 ;
sakthipriya 0:7b4c00e3912f 885 }
sakthipriya 0:7b4c00e3912f 886 else if( l_current_z >= 0.006 && l_current_z < 0.0116)
sakthipriya 0:7b4c00e3912f 887 {
sakthipriya 0:7b4c00e3912f 888 l_duty_cycle_z = 1*100000000*pow(l_current_z,4) - 5*1000000*pow(l_current_z,3) + 62603*pow(l_current_z,2) - 199.29*l_current_z + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 889 pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
sakthipriya 0:7b4c00e3912f 890 PWM3.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 891 PWM3 = l_duty_cycle_z/100 ;
sakthipriya 0:7b4c00e3912f 892 }
sakthipriya 0:7b4c00e3912f 893 else if (l_current_z >= 0.0116 && l_current_z < 0.0624)
sakthipriya 0:7b4c00e3912f 894 {
sakthipriya 0:7b4c00e3912f 895 l_duty_cycle_z = 212444*pow(l_current_z,4) - 33244*pow(l_current_z,3) + 1778.4*pow(l_current_z,2) + 120.91*l_current_z + 0.3878;// calculating upto 10% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 896 pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
sakthipriya 0:7b4c00e3912f 897 PWM3.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 898 PWM3 = l_duty_cycle_z/100 ;
sakthipriya 0:7b4c00e3912f 899 }
sakthipriya 0:7b4c00e3912f 900 else if(l_current_z >= 0.0624 && l_current_z < 0.555)
sakthipriya 0:7b4c00e3912f 901 {
sakthipriya 0:7b4c00e3912f 902 l_duty_cycle_z = 331.15*pow(l_current_z,4) - 368.09*pow(l_current_z,3) + 140.43*pow(l_current_z,2) + 158.59*l_current_z + 0.0338;// calculating upto 100% dutycycle by polynomial interpolation
sakthipriya 0:7b4c00e3912f 903 pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
sakthipriya 0:7b4c00e3912f 904 PWM3.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 905 PWM3 = l_duty_cycle_z/100 ;
sakthipriya 0:7b4c00e3912f 906 }
sakthipriya 0:7b4c00e3912f 907 else if(l_current_z==0)
sakthipriya 0:7b4c00e3912f 908 {
sakthipriya 0:7b4c00e3912f 909 printf("\n \r l_current_z====0");
sakthipriya 0:7b4c00e3912f 910 l_duty_cycle_z = 0; // default value of duty cycle
sakthipriya 0:7b4c00e3912f 911 pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z);
sakthipriya 0:7b4c00e3912f 912 PWM3.period(TIME_PERIOD);
sakthipriya 0:7b4c00e3912f 913 PWM3 = l_duty_cycle_z/100 ;
sakthipriya 0:7b4c00e3912f 914 }
sakthipriya 0:7b4c00e3912f 915 else // not necessary
sakthipriya 0:7b4c00e3912f 916 {
sakthipriya 0:7b4c00e3912f 917 g_err_flag_TR_z = 1;
sakthipriya 0:7b4c00e3912f 918 }
sakthipriya 0:7b4c00e3912f 919
sakthipriya 0:7b4c00e3912f 920 //-----------------------------------------exiting the function-----------------------------------//
sakthipriya 0:7b4c00e3912f 921
sakthipriya 0:7b4c00e3912f 922 printf("\n\rExited executable PWMGEN function\n\r"); // stating the successful exit of TR function
sakthipriya 0:7b4c00e3912f 923
lakshya 10:f93407b97750 924 }*/
sakthipriya 0:7b4c00e3912f 925
sakthipriya 0:7b4c00e3912f 926
sakthipriya 0:7b4c00e3912f 927