sw_test_workin

Dependencies:   mbed-rtos mbed

Fork of BAE_icecream_1_0 by sakthi priya amirtharaj

Committer:
greenroshks
Date:
Fri Dec 26 06:12:09 2014 +0000
Revision:
2:edd107ea4740
Parent:
0:ebdf4f859dca
Child:
3:20647ff68b3c
integrated code with threas 2 b removed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
raizel_varun 0:ebdf4f859dca 1 #include "ACS.h"
raizel_varun 0:ebdf4f859dca 2 #include "MPU3300.h"
raizel_varun 0:ebdf4f859dca 3 #include "pin_config.h"
raizel_varun 0:ebdf4f859dca 4
raizel_varun 0:ebdf4f859dca 5 //PwmOut PWM1(PTD4); //Functions used to generate PWM signal
raizel_varun 0:ebdf4f859dca 6 //PWM output comes from pins p6
raizel_varun 0:ebdf4f859dca 7 Serial pc1(USBTX, USBRX);
raizel_varun 0:ebdf4f859dca 8 SPI spi_acs (PIN16, PIN17, PIN15); // mosi, miso, sclk PTE18,19,17
greenroshks 2:edd107ea4740 9 DigitalOut SSN_MAG (PTC16); // ssn for magnetometer PTB11
greenroshks 2:edd107ea4740 10 DigitalInOut DRDY (PTE3); // drdy for magnetometer PTA17
greenroshks 2:edd107ea4740 11 DigitalOut ssn_gyr (PTE2); //Slave Select pin of gyroscope PTB16
greenroshks 2:edd107ea4740 12 InterruptIn dr(PTC6); //Interrupt pin for gyro PTC5
greenroshks 2:edd107ea4740 13 PwmOut PWM1(D2); //Functions used to generate PWM signal
greenroshks 2:edd107ea4740 14 PwmOut PWM2(D3);
greenroshks 2:edd107ea4740 15 PwmOut PWM3(D4); //PWM output comes from pins p6
raizel_varun 0:ebdf4f859dca 16 Ticker tr; //Ticker function to give values for limited amount of time for gyro
raizel_varun 0:ebdf4f859dca 17 Timeout tr_mag;
raizel_varun 0:ebdf4f859dca 18 uint8_t trflag_mag;
raizel_varun 0:ebdf4f859dca 19 uint8_t trFlag; //ticker Flag for gyro
raizel_varun 0:ebdf4f859dca 20 uint8_t drFlag; //data-ready interrupt flag for gyro
raizel_varun 0:ebdf4f859dca 21
raizel_varun 0:ebdf4f859dca 22 //--------------------------------TORQUE ROD--------------------------------------------------------------------------------------------------------------//
raizel_varun 0:ebdf4f859dca 23
raizel_varun 0:ebdf4f859dca 24 void FUNC_ACS_GENPWM(float M[3])
raizel_varun 0:ebdf4f859dca 25 {
raizel_varun 0:ebdf4f859dca 26
raizel_varun 0:ebdf4f859dca 27
raizel_varun 0:ebdf4f859dca 28 printf("\nEnterd PWMGEN function\n");
raizel_varun 0:ebdf4f859dca 29 float DCx = 0; //Duty cycle of Moment in x, y, z directions
raizel_varun 0:ebdf4f859dca 30 float ix = 0; //Current sent in x, y, z TR's
raizel_varun 0:ebdf4f859dca 31 float timep = 0.02 ;
raizel_varun 0:ebdf4f859dca 32 float Mx=M[0]; //Time period is set to 0.02s
raizel_varun 0:ebdf4f859dca 33 //Moment in x, y, z directions
greenroshks 2:edd107ea4740 34
raizel_varun 0:ebdf4f859dca 35
raizel_varun 0:ebdf4f859dca 36
raizel_varun 0:ebdf4f859dca 37 ix = Mx * 0.3 ; //Moment and Current always have the linear relationship
raizel_varun 0:ebdf4f859dca 38
raizel_varun 0:ebdf4f859dca 39 if( ix>0&& ix < 0.006 ) //Current and Duty cycle have the linear relationship between 1% and 100%
raizel_varun 0:ebdf4f859dca 40 {
raizel_varun 0:ebdf4f859dca 41 DCx = 6*1000000*pow(ix,4) - 377291*pow(ix,3) + 4689.6*pow(ix,2) + 149.19*ix - 0.0008;
raizel_varun 0:ebdf4f859dca 42 PWM1.period(timep);
raizel_varun 0:ebdf4f859dca 43 PWM1 = DCx/100 ;
raizel_varun 0:ebdf4f859dca 44 }
raizel_varun 0:ebdf4f859dca 45 else if( ix >= 0.006&& ix < 0.0116)
raizel_varun 0:ebdf4f859dca 46 {
raizel_varun 0:ebdf4f859dca 47 DCx = 1*100000000*pow(ix,4) - 5*1000000*pow(ix,3) + 62603*pow(ix,2) - 199.29*ix + 0.7648;
raizel_varun 0:ebdf4f859dca 48 PWM1.period(timep);
raizel_varun 0:ebdf4f859dca 49 PWM1 = DCx/100 ;
raizel_varun 0:ebdf4f859dca 50 }
raizel_varun 0:ebdf4f859dca 51 else if (ix >= 0.0116&& ix < 0.0624)
raizel_varun 0:ebdf4f859dca 52 {
raizel_varun 0:ebdf4f859dca 53
raizel_varun 0:ebdf4f859dca 54 DCx = 212444*pow(ix,4) - 33244*pow(ix,3) + 1778.4*pow(ix,2) + 120.91*ix + 0.3878;
raizel_varun 0:ebdf4f859dca 55 PWM1.period(timep);
raizel_varun 0:ebdf4f859dca 56 PWM1 = DCx/100 ;
raizel_varun 0:ebdf4f859dca 57 }
raizel_varun 0:ebdf4f859dca 58 else if(ix >= 0.0624&& ix < 0.555)
raizel_varun 0:ebdf4f859dca 59 {
raizel_varun 0:ebdf4f859dca 60 printf("\nACS entered if\n");
raizel_varun 0:ebdf4f859dca 61 DCx = 331.15*pow(ix,4) - 368.09*pow(ix,3) + 140.43*pow(ix,2) + 158.59*ix + 0.0338;
raizel_varun 0:ebdf4f859dca 62 PWM1.period(timep);
raizel_varun 0:ebdf4f859dca 63 PWM1 = DCx/100 ;
raizel_varun 0:ebdf4f859dca 64 }
raizel_varun 0:ebdf4f859dca 65 else if(ix==0)
raizel_varun 0:ebdf4f859dca 66 {
greenroshks 2:edd107ea4740 67 DCx = 50;
raizel_varun 0:ebdf4f859dca 68 PWM1.period(timep);
raizel_varun 0:ebdf4f859dca 69 PWM1 = DCx/100 ;
raizel_varun 0:ebdf4f859dca 70 }
raizel_varun 0:ebdf4f859dca 71 else
raizel_varun 0:ebdf4f859dca 72 {
raizel_varun 0:ebdf4f859dca 73 // printf("!!!!!!!!!!Error!!!!!!!!!");
raizel_varun 0:ebdf4f859dca 74 }
greenroshks 2:edd107ea4740 75
greenroshks 2:edd107ea4740 76 printf("\n moment :%f\n",DCx);
raizel_varun 0:ebdf4f859dca 77 float DCy = 0; //Duty cycle of Moment in x, y, z directions
raizel_varun 0:ebdf4f859dca 78 float iy = 0; //Current sent in x, y, z TR's
raizel_varun 0:ebdf4f859dca 79
raizel_varun 0:ebdf4f859dca 80 float My=M[1]; //Time period is set to 0.2s
raizel_varun 0:ebdf4f859dca 81 //Moment in x, y, z directions
raizel_varun 0:ebdf4f859dca 82
raizel_varun 0:ebdf4f859dca 83
raizel_varun 0:ebdf4f859dca 84 iy = My * 0.3 ; //Moment and Current always have the linear relationship
raizel_varun 0:ebdf4f859dca 85
raizel_varun 0:ebdf4f859dca 86 if( iy>0&& iy < 0.006 ) //Current and Duty cycle have the linear relationship between 1% and 100%
raizel_varun 0:ebdf4f859dca 87 {
raizel_varun 0:ebdf4f859dca 88 DCy = 6*1000000*pow(iy,4) - 377291*pow(iy,3) + 4689.6*pow(iy,2) + 149.19*iy - 0.0008;
raizel_varun 0:ebdf4f859dca 89 PWM2.period(timep);
raizel_varun 0:ebdf4f859dca 90 PWM2 = DCy/100 ;
raizel_varun 0:ebdf4f859dca 91 }
raizel_varun 0:ebdf4f859dca 92 else if( iy >= 0.006&& iy < 0.0116)
raizel_varun 0:ebdf4f859dca 93 {
raizel_varun 0:ebdf4f859dca 94 DCy = 1*100000000*pow(iy,4) - 5*1000000*pow(iy,3) + 62603*pow(iy,2) - 199.29*iy + 0.7648;
raizel_varun 0:ebdf4f859dca 95 PWM2.period(timep);
raizel_varun 0:ebdf4f859dca 96 PWM2 = DCy/100 ;
raizel_varun 0:ebdf4f859dca 97 }
raizel_varun 0:ebdf4f859dca 98 else if (iy >= 0.0116&& iy < 0.0624)
raizel_varun 0:ebdf4f859dca 99 {
raizel_varun 0:ebdf4f859dca 100
raizel_varun 0:ebdf4f859dca 101 DCy = 212444*pow(iy,4) - 33244*pow(iy,3) + 1778.4*pow(iy,2) + 120.91*iy + 0.3878;
raizel_varun 0:ebdf4f859dca 102 PWM2.period(timep);
raizel_varun 0:ebdf4f859dca 103 PWM2 = DCy/100 ;
raizel_varun 0:ebdf4f859dca 104 }
raizel_varun 0:ebdf4f859dca 105 else if(iy >= 0.0624&& iy < 0.555)
raizel_varun 0:ebdf4f859dca 106 {
raizel_varun 0:ebdf4f859dca 107 printf("\nACS entered if\n");
raizel_varun 0:ebdf4f859dca 108 DCy = 331.15*pow(iy,4) - 368.09*pow(iy,3) + 140.43*pow(iy,2) + 158.59*iy + 0.0338;
raizel_varun 0:ebdf4f859dca 109 PWM2.period(timep);
raizel_varun 0:ebdf4f859dca 110 PWM2 = DCy/100 ;
raizel_varun 0:ebdf4f859dca 111 }
raizel_varun 0:ebdf4f859dca 112 else if(iy==0)
raizel_varun 0:ebdf4f859dca 113 {
raizel_varun 0:ebdf4f859dca 114 DCy = 0;
raizel_varun 0:ebdf4f859dca 115 PWM2.period(timep);
raizel_varun 0:ebdf4f859dca 116 PWM2 = DCy/100 ;
raizel_varun 0:ebdf4f859dca 117 }
raizel_varun 0:ebdf4f859dca 118 else
raizel_varun 0:ebdf4f859dca 119 {
raizel_varun 0:ebdf4f859dca 120 // printf("!!!!!!!!!!Error!!!!!!!!!");
raizel_varun 0:ebdf4f859dca 121 }
raizel_varun 0:ebdf4f859dca 122 float DCz = 0; //Duty cycle of Moment in x, y, z directions
raizel_varun 0:ebdf4f859dca 123 float iz = 0; //Current sent in x, y, z TR's
raizel_varun 0:ebdf4f859dca 124
raizel_varun 0:ebdf4f859dca 125 float Mz=M[2]; //Time period is set to 0.2s
raizel_varun 0:ebdf4f859dca 126 //Moment in x, y, z directions
raizel_varun 0:ebdf4f859dca 127
raizel_varun 0:ebdf4f859dca 128
raizel_varun 0:ebdf4f859dca 129 iz = Mz * 0.3 ; //Moment and Current always have the linear relationship
raizel_varun 0:ebdf4f859dca 130
raizel_varun 0:ebdf4f859dca 131 if( iz>0&& iz < 0.006 ) //Current and Duty cycle have the linear relationship between 1% and 100%
raizel_varun 0:ebdf4f859dca 132 {
raizel_varun 0:ebdf4f859dca 133 DCz = 6*1000000*pow(iz,4) - 377291*pow(iz,3) + 4689.6*pow(iz,2) + 149.19*iz - 0.0008;
raizel_varun 0:ebdf4f859dca 134 PWM3.period(timep);
raizel_varun 0:ebdf4f859dca 135 PWM3 = DCz/100 ;
raizel_varun 0:ebdf4f859dca 136 }
raizel_varun 0:ebdf4f859dca 137 else if( iz >= 0.006&& iz < 0.0116)
raizel_varun 0:ebdf4f859dca 138 {
raizel_varun 0:ebdf4f859dca 139 DCz = 1*100000000*pow(iz,4) - 5*1000000*pow(iz,3) + 62603*pow(iz,2) - 199.29*iz + 0.7648;
raizel_varun 0:ebdf4f859dca 140 PWM3.period(timep);
raizel_varun 0:ebdf4f859dca 141 PWM3 = DCz/100 ;
raizel_varun 0:ebdf4f859dca 142 }
raizel_varun 0:ebdf4f859dca 143 else if (iz >= 0.0116&& iz < 0.0624)
raizel_varun 0:ebdf4f859dca 144 {
raizel_varun 0:ebdf4f859dca 145
raizel_varun 0:ebdf4f859dca 146 DCz = 212444*pow(iz,4) - 33244*pow(iz,3) + 1778.4*pow(iz,2) + 120.91*iz + 0.3878;
raizel_varun 0:ebdf4f859dca 147 PWM3.period(timep);
raizel_varun 0:ebdf4f859dca 148 PWM3 = DCz/100 ;
raizel_varun 0:ebdf4f859dca 149 }
raizel_varun 0:ebdf4f859dca 150 else if(iz >= 0.0624&& iz < 0.555)
raizel_varun 0:ebdf4f859dca 151 {
raizel_varun 0:ebdf4f859dca 152 printf("\nACS entered if\n");
raizel_varun 0:ebdf4f859dca 153 DCz = 331.15*pow(iz,4) - 368.09*pow(iz,3) + 140.43*pow(iz,2) + 158.59*iz + 0.0338;
raizel_varun 0:ebdf4f859dca 154 PWM3.period(timep);
raizel_varun 0:ebdf4f859dca 155 PWM3 = DCz/100 ;
raizel_varun 0:ebdf4f859dca 156 }
raizel_varun 0:ebdf4f859dca 157 else if(iz==0)
raizel_varun 0:ebdf4f859dca 158 {
raizel_varun 0:ebdf4f859dca 159 DCz = 0;
raizel_varun 0:ebdf4f859dca 160 PWM3.period(timep);
raizel_varun 0:ebdf4f859dca 161 PWM3 = DCz/100 ;
raizel_varun 0:ebdf4f859dca 162 }
raizel_varun 0:ebdf4f859dca 163 else
raizel_varun 0:ebdf4f859dca 164 {
raizel_varun 0:ebdf4f859dca 165 // printf("!!!!!!!!!!Error!!!!!!!!!");
raizel_varun 0:ebdf4f859dca 166 }
raizel_varun 0:ebdf4f859dca 167
raizel_varun 0:ebdf4f859dca 168 printf("\nExited PWMGEN function\n");
raizel_varun 0:ebdf4f859dca 169 }
raizel_varun 0:ebdf4f859dca 170 /*-------------------------------------------------------------------------------------------------------------------------------------------------------
raizel_varun 0:ebdf4f859dca 171 -------------------------------------------MAGNETOMETER-------------------------------------------------------------------------------------------------*/
raizel_varun 0:ebdf4f859dca 172
raizel_varun 0:ebdf4f859dca 173 void trsub_mag()
raizel_varun 0:ebdf4f859dca 174 {
raizel_varun 0:ebdf4f859dca 175 trflag_mag=0;
raizel_varun 0:ebdf4f859dca 176 }
raizel_varun 0:ebdf4f859dca 177
raizel_varun 0:ebdf4f859dca 178 void FUNC_ACS_MAG_INIT()
raizel_varun 0:ebdf4f859dca 179 {
greenroshks 2:edd107ea4740 180 //DRDY.output();
greenroshks 2:edd107ea4740 181 DRDY = 0;
greenroshks 2:edd107ea4740 182 int a ;
greenroshks 2:edd107ea4740 183 a=DRDY;
greenroshks 2:edd107ea4740 184 printf("\n DRDY is %d\n",a);
raizel_varun 0:ebdf4f859dca 185 SSN_MAG=1; //pin is disabled
raizel_varun 0:ebdf4f859dca 186 spi_acs.format(8,0); // 8bits,Mode 0
raizel_varun 0:ebdf4f859dca 187 spi_acs.frequency(100000); //clock frequency
raizel_varun 0:ebdf4f859dca 188
raizel_varun 0:ebdf4f859dca 189 SSN_MAG=0; // Selecting pin
raizel_varun 0:ebdf4f859dca 190 wait_ms(10); //accounts for delay.can be minimised.
raizel_varun 0:ebdf4f859dca 191
raizel_varun 0:ebdf4f859dca 192 spi_acs.write(0x83); //
raizel_varun 0:ebdf4f859dca 193
raizel_varun 0:ebdf4f859dca 194 wait_ms(10);
raizel_varun 0:ebdf4f859dca 195
raizel_varun 0:ebdf4f859dca 196 unsigned char i;
raizel_varun 0:ebdf4f859dca 197 for(i=0;i<3;i++)//initialising values.
raizel_varun 0:ebdf4f859dca 198 {
raizel_varun 0:ebdf4f859dca 199 spi_acs.write(0x00); //MSB of X,y,Z
raizel_varun 0:ebdf4f859dca 200 spi_acs.write(0xc8); //LSB of X,Y,z;pointer increases automatically.
raizel_varun 0:ebdf4f859dca 201 }
raizel_varun 0:ebdf4f859dca 202 SSN_MAG=1;
raizel_varun 0:ebdf4f859dca 203
raizel_varun 0:ebdf4f859dca 204 }
raizel_varun 0:ebdf4f859dca 205
raizel_varun 0:ebdf4f859dca 206 float* FUNC_ACS_MAG_EXEC()
raizel_varun 0:ebdf4f859dca 207 {
greenroshks 2:edd107ea4740 208 //printf("\nEntered magnetometer function\n");
greenroshks 2:edd107ea4740 209 //DRDY.output();
greenroshks 2:edd107ea4740 210 DRDY.write(0);
greenroshks 2:edd107ea4740 211 int a;
greenroshks 2:edd107ea4740 212 a = DRDY;
greenroshks 2:edd107ea4740 213 printf("\n DRDY is %d\n",a);
raizel_varun 0:ebdf4f859dca 214 SSN_MAG=0; //enabling slave to measure the values
raizel_varun 0:ebdf4f859dca 215 wait_ms(10);
raizel_varun 0:ebdf4f859dca 216 spi_acs.write(0x82); //initiates measurement
raizel_varun 0:ebdf4f859dca 217 wait_ms(10);
raizel_varun 0:ebdf4f859dca 218 spi_acs.write(0x01); //selecting x,y and z axes, measurement starts now
raizel_varun 0:ebdf4f859dca 219 SSN_MAG=1;
raizel_varun 0:ebdf4f859dca 220 wait_ms(10);
raizel_varun 0:ebdf4f859dca 221
raizel_varun 0:ebdf4f859dca 222 trflag_mag=1;
raizel_varun 0:ebdf4f859dca 223 tr_mag.attach(&trsub_mag,1); //runs in background,makes trflag_mag=0 after 1s
greenroshks 2:edd107ea4740 224 DRDY.input();
raizel_varun 0:ebdf4f859dca 225 while(trflag_mag) /*initially flag is 1,so loop is executed,if DRDY is high,then data is retrieved and programme ends,else
raizel_varun 0:ebdf4f859dca 226 loop runs for at the max 1s and if still DRDY is zero,the flag becomes 0 and loop is not executed and
raizel_varun 0:ebdf4f859dca 227 programme is terminated*/
raizel_varun 0:ebdf4f859dca 228 {
raizel_varun 0:ebdf4f859dca 229 wait_ms(5);
raizel_varun 0:ebdf4f859dca 230 if(DRDY==1)
raizel_varun 0:ebdf4f859dca 231 {
greenroshks 2:edd107ea4740 232 printf("\nwth\n");
raizel_varun 0:ebdf4f859dca 233 SSN_MAG=0;
raizel_varun 0:ebdf4f859dca 234 spi_acs.write(0xc9); //command byte for retrieving data
raizel_varun 0:ebdf4f859dca 235
raizel_varun 0:ebdf4f859dca 236 unsigned char axis;
raizel_varun 0:ebdf4f859dca 237 float Bnewvalue[3]={0.0,0.0,0.0};
raizel_varun 0:ebdf4f859dca 238 int32_t Bvalue[3]={0,0,0};
raizel_varun 0:ebdf4f859dca 239 int32_t a= pow(2.0,24.0);
raizel_varun 0:ebdf4f859dca 240 int32_t b= pow(2.0,23.0);
raizel_varun 0:ebdf4f859dca 241
raizel_varun 0:ebdf4f859dca 242 for(axis=0;axis<3;axis++)
raizel_varun 0:ebdf4f859dca 243 {
raizel_varun 0:ebdf4f859dca 244 Bvalue[axis]=spi_acs.write(0x00)<<16; //MSB 1 is send first
raizel_varun 0:ebdf4f859dca 245 wait_ms(10);
raizel_varun 0:ebdf4f859dca 246 Bvalue[axis]|=spi_acs.write(0x00)<<8; //MSB 2 is send next
raizel_varun 0:ebdf4f859dca 247 wait_ms(10);
raizel_varun 0:ebdf4f859dca 248 Bvalue[axis]|=spi_acs.write(0x00); //LSB is send.....total length is 24 bits(3*8bits)...which are appended to get actual bit configuration
raizel_varun 0:ebdf4f859dca 249
raizel_varun 0:ebdf4f859dca 250
raizel_varun 0:ebdf4f859dca 251 if((Bvalue[axis]&b)==b)
raizel_varun 0:ebdf4f859dca 252 {
raizel_varun 0:ebdf4f859dca 253 Bvalue[axis]=Bvalue[axis]-a; //converting 2s complement to signed decimal
raizel_varun 0:ebdf4f859dca 254
raizel_varun 0:ebdf4f859dca 255 }
raizel_varun 0:ebdf4f859dca 256 Bnewvalue[axis]=(float)Bvalue[axis]*22.0*pow(10.0,-3.0); //1 LSB=(22nT)...final value of field obtained in micro tesla
raizel_varun 0:ebdf4f859dca 257
raizel_varun 0:ebdf4f859dca 258 wait_ms(10);
raizel_varun 0:ebdf4f859dca 259 printf("\t%lf\n",Bnewvalue[axis]);
raizel_varun 0:ebdf4f859dca 260
raizel_varun 0:ebdf4f859dca 261 }
raizel_varun 0:ebdf4f859dca 262 SSN_MAG=1;
greenroshks 2:edd107ea4740 263 /* for test only to removed */
greenroshks 2:edd107ea4740 264 Bnewvalue[0]=Bnewvalue[1]=Bnewvalue[2]=100;
raizel_varun 0:ebdf4f859dca 265 return Bnewvalue; //return here? doubt..
raizel_varun 0:ebdf4f859dca 266 break;
raizel_varun 0:ebdf4f859dca 267 }
raizel_varun 0:ebdf4f859dca 268
raizel_varun 0:ebdf4f859dca 269 }
raizel_varun 0:ebdf4f859dca 270
raizel_varun 0:ebdf4f859dca 271 }
raizel_varun 0:ebdf4f859dca 272 /*------------------------------------------------------------------------------------------------------------------------------------------------------
raizel_varun 0:ebdf4f859dca 273 -------------------------------------------CONTROL ALGORITHM------------------------------------------------------------------------------------------*/
raizel_varun 0:ebdf4f859dca 274
raizel_varun 0:ebdf4f859dca 275 float * FUNC_ACS_CNTRLALGO(float b[3],float omega[3])
raizel_varun 0:ebdf4f859dca 276 {
raizel_varun 0:ebdf4f859dca 277 float db[3]; /// inputs
raizel_varun 0:ebdf4f859dca 278 //initialization
raizel_varun 0:ebdf4f859dca 279 float bb[3] = {0, 0, 0};
raizel_varun 0:ebdf4f859dca 280 float d[3] = {0, 0, 0};
raizel_varun 0:ebdf4f859dca 281 float Jm[3][3] = {{0.2730, 0, 0}, {0, 0.3018, 0}, {0, 0, 0.3031}};
raizel_varun 0:ebdf4f859dca 282 float den = 0;
raizel_varun 0:ebdf4f859dca 283 float den2;
raizel_varun 0:ebdf4f859dca 284 int i, j; //temporary variables
raizel_varun 0:ebdf4f859dca 285 float Mu[2], z[2], dv[2], v[2], u[2], tauc[3] = {0, 0, 0}; //outputs
raizel_varun 0:ebdf4f859dca 286 float invJm[3][3];
raizel_varun 0:ebdf4f859dca 287 float kmu2 = 0.07, gamma2 = 1.9e4, kz2 = 0.4e-2, kmu = 0.003, gamma = 5.6e4, kz = 0.1e-4;
raizel_varun 0:ebdf4f859dca 288 printf("Entered cntrl algo\n");
raizel_varun 0:ebdf4f859dca 289 //structure parameters
raizel_varun 0:ebdf4f859dca 290
raizel_varun 0:ebdf4f859dca 291 void inverse (float mat[3][3], float inv[3][3]);
raizel_varun 0:ebdf4f859dca 292 void getInput (float x[9]);
raizel_varun 0:ebdf4f859dca 293 //functions
raizel_varun 0:ebdf4f859dca 294
raizel_varun 0:ebdf4f859dca 295 ////////// Input from Matlab //////////////
raizel_varun 0:ebdf4f859dca 296 while(1)
raizel_varun 0:ebdf4f859dca 297 {
raizel_varun 0:ebdf4f859dca 298
raizel_varun 0:ebdf4f859dca 299 /*getInput(inputs);
raizel_varun 0:ebdf4f859dca 300 //while(1)
raizel_varun 0:ebdf4f859dca 301 b[0] = inputs[0];
raizel_varun 0:ebdf4f859dca 302 b[1] = inputs[1];
raizel_varun 0:ebdf4f859dca 303 b[2] = inputs[2];
raizel_varun 0:ebdf4f859dca 304 db[0] = inputs[3];
raizel_varun 0:ebdf4f859dca 305 db[1] = inputs[4];
raizel_varun 0:ebdf4f859dca 306 db[2] = inputs[5];
raizel_varun 0:ebdf4f859dca 307 omega[0] = inputs[6];
raizel_varun 0:ebdf4f859dca 308 omega[1] = inputs[7];
raizel_varun 0:ebdf4f859dca 309 omega[2] = inputs[8];*/
raizel_varun 0:ebdf4f859dca 310 /////////// Control Algorithm //////////////////////
raizel_varun 0:ebdf4f859dca 311 // calculate norm b, norm db
raizel_varun 0:ebdf4f859dca 312 den = sqrt((b[0]*b[0]) + (b[1]*b[1]) + (b[2]*b[2]));
raizel_varun 0:ebdf4f859dca 313 den2 = (b[0]*db[0]) + (b[1]*db[1]) + (b[2]*db[2]);
raizel_varun 0:ebdf4f859dca 314
raizel_varun 0:ebdf4f859dca 315 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 316 {
raizel_varun 0:ebdf4f859dca 317 db[i] = (db[i]*den*den-b[i]*den2) / (pow(den,3));
raizel_varun 0:ebdf4f859dca 318 //db[i]/=den*den*den;
raizel_varun 0:ebdf4f859dca 319 }
raizel_varun 0:ebdf4f859dca 320
raizel_varun 0:ebdf4f859dca 321 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 322 {
raizel_varun 0:ebdf4f859dca 323 printf("\nreached here\n");
raizel_varun 0:ebdf4f859dca 324 if(den!=0)
raizel_varun 0:ebdf4f859dca 325 //b[i]=b[i]/den; //there is a problem here. The code gets stuck here. Maf value is required
raizel_varun 0:ebdf4f859dca 326 ;
raizel_varun 0:ebdf4f859dca 327 }
raizel_varun 0:ebdf4f859dca 328
raizel_varun 0:ebdf4f859dca 329 // select kz, kmu, gamma
raizel_varun 0:ebdf4f859dca 330 if(b[0]>0.9 || b[0]<-0.9)
raizel_varun 0:ebdf4f859dca 331 {
raizel_varun 0:ebdf4f859dca 332 kz = kz2;
raizel_varun 0:ebdf4f859dca 333 kmu = kmu2;
raizel_varun 0:ebdf4f859dca 334 gamma = gamma2;
raizel_varun 0:ebdf4f859dca 335 }
raizel_varun 0:ebdf4f859dca 336 // calculate Mu, v, dv, z, u
raizel_varun 0:ebdf4f859dca 337 for(i=0;i<2;i++)
raizel_varun 0:ebdf4f859dca 338 {
raizel_varun 0:ebdf4f859dca 339 Mu[i] = b[i+1];
raizel_varun 0:ebdf4f859dca 340 v[i] = -kmu*Mu[i];
raizel_varun 0:ebdf4f859dca 341 dv[i] = -kmu*db[i+1];
raizel_varun 0:ebdf4f859dca 342 z[i] = db[i+1] - v[i];
raizel_varun 0:ebdf4f859dca 343 u[i] = -kz*z[i] + dv[i]-(Mu[i] / gamma);
raizel_varun 0:ebdf4f859dca 344 }
raizel_varun 0:ebdf4f859dca 345 inverse(Jm, invJm);
raizel_varun 0:ebdf4f859dca 346 // calculate cross(omega,J*omega)for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 347
raizel_varun 0:ebdf4f859dca 348 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 349 bb[i] += omega[j]*(omega[(i+1)%3]*Jm[(i+2)%3][j] - omega[(i+2)%3]*Jm[(i+1)%3][j]);
raizel_varun 0:ebdf4f859dca 350
raizel_varun 0:ebdf4f859dca 351 // calculate invJm*cross(omega,J*omega) store in d
raizel_varun 0:ebdf4f859dca 352 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 353 {
raizel_varun 0:ebdf4f859dca 354 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 355 d[i] += bb[j]*invJm[i][j];
raizel_varun 0:ebdf4f859dca 356 }
raizel_varun 0:ebdf4f859dca 357 // calculate d = cross(invJm*cross(omega,J*omega),b) -cross(omega,db)
raizel_varun 0:ebdf4f859dca 358 // bb =[0;u-d(2:3)]
raizel_varun 0:ebdf4f859dca 359 // store in bb
raizel_varun 0:ebdf4f859dca 360 bb[1] = u[0] + (d[0]*b[2])-(d[2]*b[0])-(omega[0]*db[2]) + (omega[2]*db[0]);
raizel_varun 0:ebdf4f859dca 361 bb[2] = u[1]-(d[0]*b[1]) + (d[1]*b[0]) + (omega[0]*db[1])-(omega[1]*db[0]);
raizel_varun 0:ebdf4f859dca 362 bb[0] = 0;
raizel_varun 0:ebdf4f859dca 363 // calculate N
raizel_varun 0:ebdf4f859dca 364 // reusing invJm as N
raizel_varun 0:ebdf4f859dca 365
raizel_varun 0:ebdf4f859dca 366 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 367 {
raizel_varun 0:ebdf4f859dca 368 d[i] = invJm[1][i];
raizel_varun 0:ebdf4f859dca 369 invJm[ 1][i] = b[2]*invJm[0][i] - b[0]*invJm[2][i];
raizel_varun 0:ebdf4f859dca 370 invJm[2][i] = -b[1]*invJm[0][i] + b[0]*d[i];
raizel_varun 0:ebdf4f859dca 371 invJm[0][i] = b[i];
raizel_varun 0:ebdf4f859dca 372 }
raizel_varun 0:ebdf4f859dca 373 // calculate inv(N) store in Jm
raizel_varun 0:ebdf4f859dca 374 inverse(invJm, Jm);
raizel_varun 0:ebdf4f859dca 375 // calculate tauc
raizel_varun 0:ebdf4f859dca 376 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 377 {
raizel_varun 0:ebdf4f859dca 378 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 379 tauc[i] += Jm[i][j]*bb[j];
raizel_varun 0:ebdf4f859dca 380 }
raizel_varun 0:ebdf4f859dca 381
raizel_varun 0:ebdf4f859dca 382 return(tauc);
raizel_varun 0:ebdf4f859dca 383 }
raizel_varun 0:ebdf4f859dca 384 }
raizel_varun 0:ebdf4f859dca 385 /////////// Output to Matlab //////////////////
raizel_varun 0:ebdf4f859dca 386 /* for(i=0;i<3;i++) {
raizel_varun 0:ebdf4f859dca 387 printf("%f\n",tauc[i]*10000000);
raizel_varun 0:ebdf4f859dca 388 wait_ms(10);
raizel_varun 0:ebdf4f859dca 389 }
raizel_varun 0:ebdf4f859dca 390 }
raizel_varun 0:ebdf4f859dca 391
raizel_varun 0:ebdf4f859dca 392 }*/
raizel_varun 0:ebdf4f859dca 393 void inverse(float mat[3][3], float inv[3][3])
raizel_varun 0:ebdf4f859dca 394 {
raizel_varun 0:ebdf4f859dca 395 int i, j;
raizel_varun 0:ebdf4f859dca 396 float det = 0;
raizel_varun 0:ebdf4f859dca 397 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 398 { for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 399 inv[j][i] = (mat[(i+1)%3][(j+1)%3]*mat[(i+2)%3][(j+2)%3]) - (mat[(i+2)%3]
raizel_varun 0:ebdf4f859dca 400 [(j+1)%3]*mat[(i+1)%3][(j+2)%3]);
raizel_varun 0:ebdf4f859dca 401 }
raizel_varun 0:ebdf4f859dca 402 det += (mat[0][0]*inv[0][0]) + (mat[0][1]*inv[1][0]) + (mat[0][2]*inv[2][0]);
raizel_varun 0:ebdf4f859dca 403 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 404 { for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 405 inv[i][j] /= det;
raizel_varun 0:ebdf4f859dca 406 }
raizel_varun 0:ebdf4f859dca 407 }/*
raizel_varun 0:ebdf4f859dca 408 void getInput (float x[9]) {
raizel_varun 0:ebdf4f859dca 409 //Functions used to generate PWM signal
raizel_varun 0:ebdf4f859dca 410 //PWM output comes from pins p6
raizel_varun 0:ebdf4f859dca 411 Serial pc1(USBTX, USBRX);
raizel_varun 0:ebdf4f859dca 412 char c[10];
raizel_varun 0:ebdf4f859dca 413 char tempchar[8];
raizel_varun 0:ebdf4f859dca 414 int i, j;
raizel_varun 0:ebdf4f859dca 415 //float f[9];
raizel_varun 0:ebdf4f859dca 416 long n = 0;
raizel_varun 0:ebdf4f859dca 417 float flval = 0;
raizel_varun 0:ebdf4f859dca 418 for(j=0;j<9;j++) {
raizel_varun 0:ebdf4f859dca 419 for(i=0;i<9;i++) {
raizel_varun 0:ebdf4f859dca 420 c[i] = pc1.getc(); if(i<8) {
raizel_varun 0:ebdf4f859dca 421 tempchar[i] = c[i];
raizel_varun 0:ebdf4f859dca 422 }
raizel_varun 0:ebdf4f859dca 423 }
raizel_varun 0:ebdf4f859dca 424 sscanf (tempchar, "%8x", &n);
raizel_varun 0:ebdf4f859dca 425 memcpy(&flval, &n, sizeof(long));
raizel_varun 0:ebdf4f859dca 426 printf("%f\n", flval);
raizel_varun 0:ebdf4f859dca 427 x[j] = flval;
raizel_varun 0:ebdf4f859dca 428 }
raizel_varun 0:ebdf4f859dca 429 }*/
raizel_varun 0:ebdf4f859dca 430
raizel_varun 0:ebdf4f859dca 431 void trSub();
raizel_varun 0:ebdf4f859dca 432 void drSub();
raizel_varun 0:ebdf4f859dca 433 void init_gyro();
raizel_varun 0:ebdf4f859dca 434 float * FUNC_ACS_EXEC_GYR();
raizel_varun 0:ebdf4f859dca 435
raizel_varun 0:ebdf4f859dca 436 void drSub() //In this function we setting data-ready flag to 1
raizel_varun 0:ebdf4f859dca 437 {
raizel_varun 0:ebdf4f859dca 438 drFlag=1;
raizel_varun 0:ebdf4f859dca 439 }
raizel_varun 0:ebdf4f859dca 440 void trSub() //In this function we are setting ticker flag to 0
raizel_varun 0:ebdf4f859dca 441 {
raizel_varun 0:ebdf4f859dca 442 trFlag=0;
raizel_varun 0:ebdf4f859dca 443 }
raizel_varun 0:ebdf4f859dca 444 void FUNC_ACS_INIT_GYR()
raizel_varun 0:ebdf4f859dca 445 {
raizel_varun 0:ebdf4f859dca 446 uint8_t response;
raizel_varun 0:ebdf4f859dca 447 ssn_gyr=1; //Deselecting the chip
greenroshks 2:edd107ea4740 448 spi_acs.format(8,0); // Spi format is 8 bits, and clock mode 3
raizel_varun 0:ebdf4f859dca 449 spi_acs.frequency(1000000); //frequency to be set as 1MHz
raizel_varun 0:ebdf4f859dca 450 drFlag=0; //Intially defining data-ready flag to be 0
raizel_varun 0:ebdf4f859dca 451 dr.mode(PullDown);
raizel_varun 0:ebdf4f859dca 452 dr.rise(&drSub);
raizel_varun 0:ebdf4f859dca 453 __disable_irq();
raizel_varun 0:ebdf4f859dca 454
raizel_varun 0:ebdf4f859dca 455 /*Following the above mentioned algorithm for initializing the register and changing its configuration*/
raizel_varun 0:ebdf4f859dca 456 ssn_gyr=0; //Selecting chip(Mpu-3300)
raizel_varun 0:ebdf4f859dca 457 spi_acs.write(USER_CTRL|READFLAG); //sending USER_CTRL address with read bit
raizel_varun 0:ebdf4f859dca 458 response=spi_acs.write(DUMMYBIT); //sending dummy bit to get default values of the register
raizel_varun 0:ebdf4f859dca 459
raizel_varun 0:ebdf4f859dca 460 ssn_gyr=1; //Deselecting the chip
raizel_varun 0:ebdf4f859dca 461 wait(0.1); //waiting according the product specifications
raizel_varun 0:ebdf4f859dca 462
raizel_varun 0:ebdf4f859dca 463 ssn_gyr=0; //again selecting the chip
raizel_varun 0:ebdf4f859dca 464 spi_acs.write(USER_CTRL); //sending USER_CTRL address without read bit
raizel_varun 0:ebdf4f859dca 465 spi_acs.write(response|BIT_I2C_IF_DIS); //disabling the I2C mode in the register
raizel_varun 0:ebdf4f859dca 466 ssn_gyr=1; //deselecting the chip
raizel_varun 0:ebdf4f859dca 467 wait(0.1); // waiting for 100ms before going for another register
raizel_varun 0:ebdf4f859dca 468
raizel_varun 0:ebdf4f859dca 469 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 470 spi_acs.write(PWR_MGMT_1|READFLAG); //Power Management register-1
raizel_varun 0:ebdf4f859dca 471 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 472 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 473 wait(0.1);
raizel_varun 0:ebdf4f859dca 474
raizel_varun 0:ebdf4f859dca 475 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 476 spi_acs.write(PWR_MGMT_1);
raizel_varun 0:ebdf4f859dca 477 response=spi_acs.write(response|BIT_CLKSEL_X); //Selecting the X axis gyroscope as clock as mentioned above
raizel_varun 0:ebdf4f859dca 478 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 479 wait(0.1);
raizel_varun 0:ebdf4f859dca 480
raizel_varun 0:ebdf4f859dca 481 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 482 spi_acs.write(GYRO_CONFIG|READFLAG); //sending GYRO_CONFIG address with read bit
raizel_varun 0:ebdf4f859dca 483 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 484 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 485 wait(0.1);
raizel_varun 0:ebdf4f859dca 486
raizel_varun 0:ebdf4f859dca 487 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 488 spi_acs.write(GYRO_CONFIG); //sending GYRO_CONFIG address to write to register
raizel_varun 0:ebdf4f859dca 489 spi_acs.write(response&(~(BITS_FS_SEL_3|BITS_FS_SEL_4))); //selecting a full scale mode of +/=225 deg/sec
raizel_varun 0:ebdf4f859dca 490 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 491 wait(0.1);
raizel_varun 0:ebdf4f859dca 492
raizel_varun 0:ebdf4f859dca 493 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 494 spi_acs.write(CONFIG|READFLAG); //sending CONFIG address with read bit
raizel_varun 0:ebdf4f859dca 495 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 496 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 497 wait(0.1);
raizel_varun 0:ebdf4f859dca 498
raizel_varun 0:ebdf4f859dca 499 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 500 spi_acs.write(CONFIG); //sending CONFIG address to write to register
raizel_varun 0:ebdf4f859dca 501 spi_acs.write(response|BITS_DLPF_CFG); //selecting a bandwidth of 42 hz and delay of 4.8ms
raizel_varun 0:ebdf4f859dca 502 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 503 wait(0.1);
raizel_varun 0:ebdf4f859dca 504
raizel_varun 0:ebdf4f859dca 505 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 506 spi_acs.write(SMPLRT_DIV|READFLAG); //sending SMPLRT_DIV address with read bit
raizel_varun 0:ebdf4f859dca 507 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 508 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 509 wait(0.1);
raizel_varun 0:ebdf4f859dca 510
raizel_varun 0:ebdf4f859dca 511 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 512 spi_acs.write(SMPLRT_DIV); //sending SMPLRT_DIV address to write to register
raizel_varun 0:ebdf4f859dca 513 spi_acs.write(response&BITS_SMPLRT_DIV); //setting the sampling rate division to be 0 to make sample rate = gyroscopic output rate
raizel_varun 0:ebdf4f859dca 514 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 515 wait(0.1);
raizel_varun 0:ebdf4f859dca 516
raizel_varun 0:ebdf4f859dca 517 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 518 spi_acs.write(INT_ENABLE|READFLAG); //sending address of INT_ENABLE with readflag
raizel_varun 0:ebdf4f859dca 519 response=spi_acs.write(DUMMYBIT); //sending dummy byte to get the default values of the
raizel_varun 0:ebdf4f859dca 520 // regiser
raizel_varun 0:ebdf4f859dca 521 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 522 wait(0.1);
raizel_varun 0:ebdf4f859dca 523
raizel_varun 0:ebdf4f859dca 524 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 525 spi_acs.write(INT_ENABLE); //sending INT_ENABLE address to write to register
raizel_varun 0:ebdf4f859dca 526 spi_acs.write(response|BIT_DATA_RDY_ENABLE); //Enbling data ready interrupt
raizel_varun 0:ebdf4f859dca 527 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 528 wait(0.1);
raizel_varun 0:ebdf4f859dca 529
raizel_varun 0:ebdf4f859dca 530 __enable_irq();
raizel_varun 0:ebdf4f859dca 531 }
raizel_varun 0:ebdf4f859dca 532
raizel_varun 0:ebdf4f859dca 533 float * FUNC_ACS_EXEC_GYR()
raizel_varun 0:ebdf4f859dca 534 {
raizel_varun 0:ebdf4f859dca 535 printf("\nEntered gyro\n");
raizel_varun 0:ebdf4f859dca 536 uint8_t response;
raizel_varun 0:ebdf4f859dca 537 uint8_t MSB,LSB;
raizel_varun 0:ebdf4f859dca 538 int16_t bit_data;
raizel_varun 0:ebdf4f859dca 539 float data[3],error[3]={0,0,0}; //declaring error array to add to the values when required
raizel_varun 0:ebdf4f859dca 540 float senstivity = 145.6; //senstivity is 145.6 for full scale mode of +/-225 deg/sec
raizel_varun 0:ebdf4f859dca 541 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 542 spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of INT_ENABLE with readflag
raizel_varun 0:ebdf4f859dca 543 response=spi_acs.write(DUMMYBIT); //
raizel_varun 0:ebdf4f859dca 544 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 545 wait(0.1);
raizel_varun 0:ebdf4f859dca 546
raizel_varun 0:ebdf4f859dca 547 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 548 spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
raizel_varun 0:ebdf4f859dca 549 response=spi_acs.write(response&(~(BIT_SLEEP))); //waking up the gyroscope from sleep
raizel_varun 0:ebdf4f859dca 550 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 551 wait(0.1);
raizel_varun 0:ebdf4f859dca 552
raizel_varun 0:ebdf4f859dca 553 trFlag=1;
raizel_varun 0:ebdf4f859dca 554 tr.attach(&trSub,1); //executes the function trSub afer 1sec
raizel_varun 0:ebdf4f859dca 555
raizel_varun 0:ebdf4f859dca 556 while(trFlag)
raizel_varun 0:ebdf4f859dca 557 {
raizel_varun 0:ebdf4f859dca 558 wait_ms(5); //This is required for this while loop to exit. I don't know why.
raizel_varun 0:ebdf4f859dca 559 if(drFlag==1)
raizel_varun 0:ebdf4f859dca 560 {
raizel_varun 0:ebdf4f859dca 561 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 562 spi_acs.write(GYRO_XOUT_H|READFLAG); //sending address of PWR_MGMT_1 with readflag
raizel_varun 0:ebdf4f859dca 563 for(int i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 564 {
raizel_varun 0:ebdf4f859dca 565 MSB = spi_acs.write(DUMMYBIT); //reading the MSB values of x,y and z respectively
raizel_varun 0:ebdf4f859dca 566 LSB = spi_acs.write(DUMMYBIT); //reading the LSB values of x,y and z respectively
raizel_varun 0:ebdf4f859dca 567 bit_data= ((int16_t)MSB<<8)|LSB; //concatenating to get 16 bit 2's complement of the required gyroscope values
raizel_varun 0:ebdf4f859dca 568 data[i]=(float)bit_data;
raizel_varun 0:ebdf4f859dca 569 data[i]=data[i]/senstivity; //dividing with senstivity to get the readings in deg/sec
raizel_varun 0:ebdf4f859dca 570 data[i]+=error[i]; //adding with error to remove offset errors
raizel_varun 0:ebdf4f859dca 571 }
raizel_varun 0:ebdf4f859dca 572 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 573 for (int i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 574 {
raizel_varun 0:ebdf4f859dca 575 printf("%f\t",data[i]); //printing the angular velocity values
raizel_varun 0:ebdf4f859dca 576 }
raizel_varun 0:ebdf4f859dca 577 printf("\n");
raizel_varun 0:ebdf4f859dca 578 break;
raizel_varun 0:ebdf4f859dca 579 }
raizel_varun 0:ebdf4f859dca 580 drFlag=0;
raizel_varun 0:ebdf4f859dca 581 }
raizel_varun 0:ebdf4f859dca 582 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 583 spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of PWR_MGMT_1 with readflag
raizel_varun 0:ebdf4f859dca 584 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 585 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 586 wait(0.1);
raizel_varun 0:ebdf4f859dca 587
raizel_varun 0:ebdf4f859dca 588 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 589 spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
raizel_varun 0:ebdf4f859dca 590 response=spi_acs.write(response|BIT_SLEEP); //setting the gyroscope in sleep mode
raizel_varun 0:ebdf4f859dca 591 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 592 wait(0.1);
raizel_varun 0:ebdf4f859dca 593 printf("\nExited gyro\n");
raizel_varun 0:ebdf4f859dca 594 return data;
raizel_varun 0:ebdf4f859dca 595 }
raizel_varun 0:ebdf4f859dca 596
raizel_varun 0:ebdf4f859dca 597
raizel_varun 0:ebdf4f859dca 598
raizel_varun 0:ebdf4f859dca 599
raizel_varun 0:ebdf4f859dca 600
raizel_varun 0:ebdf4f859dca 601