integrated code with threads to be removed

Dependencies:   mbed-rtos mbed

Fork of BAE_vr3honeycomb1 by sakthi priya amirtharaj

Committer:
greenroshks
Date:
Wed Jan 28 17:35:45 2015 +0000
Revision:
3:89a09736acd3
Parent:
2:edd107ea4740
iist version

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 3:89a09736acd3 9 DigitalOut SSN_MAG (PTA7); // ssn for magnetometer PTB11
greenroshks 3:89a09736acd3 10 DigitalInOut DRDY (PTA6); // 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 3:89a09736acd3 34 printf("\n the moment is %f",M[0]);
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
greenroshks 3:89a09736acd3 215 wait_ms(100);
raizel_varun 0:ebdf4f859dca 216 spi_acs.write(0x82); //initiates measurement
greenroshks 3:89a09736acd3 217 wait_ms(100);
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;
greenroshks 3:89a09736acd3 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 3:89a09736acd3 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 {
greenroshks 3:89a09736acd3 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);
greenroshks 3:89a09736acd3 259 Bnewvalue[0] = 100.00;
raizel_varun 0:ebdf4f859dca 260 printf("\t%lf\n",Bnewvalue[axis]);
raizel_varun 0:ebdf4f859dca 261
raizel_varun 0:ebdf4f859dca 262 }
raizel_varun 0:ebdf4f859dca 263 SSN_MAG=1;
greenroshks 2:edd107ea4740 264 /* for test only to removed */
greenroshks 3:89a09736acd3 265 //Bnewvalue[0]=Bnewvalue[1]=Bnewvalue[2]=100.0;
raizel_varun 0:ebdf4f859dca 266 return Bnewvalue; //return here? doubt..
raizel_varun 0:ebdf4f859dca 267 break;
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 /*------------------------------------------------------------------------------------------------------------------------------------------------------
raizel_varun 0:ebdf4f859dca 274 -------------------------------------------CONTROL ALGORITHM------------------------------------------------------------------------------------------*/
raizel_varun 0:ebdf4f859dca 275
raizel_varun 0:ebdf4f859dca 276 float * FUNC_ACS_CNTRLALGO(float b[3],float omega[3])
raizel_varun 0:ebdf4f859dca 277 {
raizel_varun 0:ebdf4f859dca 278 float db[3]; /// inputs
raizel_varun 0:ebdf4f859dca 279 //initialization
raizel_varun 0:ebdf4f859dca 280 float bb[3] = {0, 0, 0};
raizel_varun 0:ebdf4f859dca 281 float d[3] = {0, 0, 0};
raizel_varun 0:ebdf4f859dca 282 float Jm[3][3] = {{0.2730, 0, 0}, {0, 0.3018, 0}, {0, 0, 0.3031}};
raizel_varun 0:ebdf4f859dca 283 float den = 0;
raizel_varun 0:ebdf4f859dca 284 float den2;
raizel_varun 0:ebdf4f859dca 285 int i, j; //temporary variables
raizel_varun 0:ebdf4f859dca 286 float Mu[2], z[2], dv[2], v[2], u[2], tauc[3] = {0, 0, 0}; //outputs
raizel_varun 0:ebdf4f859dca 287 float invJm[3][3];
raizel_varun 0:ebdf4f859dca 288 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 289 printf("Entered cntrl algo\n");
raizel_varun 0:ebdf4f859dca 290 //structure parameters
raizel_varun 0:ebdf4f859dca 291
raizel_varun 0:ebdf4f859dca 292 void inverse (float mat[3][3], float inv[3][3]);
raizel_varun 0:ebdf4f859dca 293 void getInput (float x[9]);
raizel_varun 0:ebdf4f859dca 294 //functions
raizel_varun 0:ebdf4f859dca 295
raizel_varun 0:ebdf4f859dca 296 ////////// Input from Matlab //////////////
raizel_varun 0:ebdf4f859dca 297 while(1)
raizel_varun 0:ebdf4f859dca 298 {
raizel_varun 0:ebdf4f859dca 299
raizel_varun 0:ebdf4f859dca 300 /*getInput(inputs);
raizel_varun 0:ebdf4f859dca 301 //while(1)
raizel_varun 0:ebdf4f859dca 302 b[0] = inputs[0];
raizel_varun 0:ebdf4f859dca 303 b[1] = inputs[1];
raizel_varun 0:ebdf4f859dca 304 b[2] = inputs[2];
raizel_varun 0:ebdf4f859dca 305 db[0] = inputs[3];
raizel_varun 0:ebdf4f859dca 306 db[1] = inputs[4];
raizel_varun 0:ebdf4f859dca 307 db[2] = inputs[5];
raizel_varun 0:ebdf4f859dca 308 omega[0] = inputs[6];
raizel_varun 0:ebdf4f859dca 309 omega[1] = inputs[7];
raizel_varun 0:ebdf4f859dca 310 omega[2] = inputs[8];*/
raizel_varun 0:ebdf4f859dca 311 /////////// Control Algorithm //////////////////////
raizel_varun 0:ebdf4f859dca 312 // calculate norm b, norm db
raizel_varun 0:ebdf4f859dca 313 den = sqrt((b[0]*b[0]) + (b[1]*b[1]) + (b[2]*b[2]));
raizel_varun 0:ebdf4f859dca 314 den2 = (b[0]*db[0]) + (b[1]*db[1]) + (b[2]*db[2]);
raizel_varun 0:ebdf4f859dca 315
raizel_varun 0:ebdf4f859dca 316 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 317 {
raizel_varun 0:ebdf4f859dca 318 db[i] = (db[i]*den*den-b[i]*den2) / (pow(den,3));
raizel_varun 0:ebdf4f859dca 319 //db[i]/=den*den*den;
raizel_varun 0:ebdf4f859dca 320 }
raizel_varun 0:ebdf4f859dca 321
raizel_varun 0:ebdf4f859dca 322 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 323 {
raizel_varun 0:ebdf4f859dca 324 printf("\nreached here\n");
raizel_varun 0:ebdf4f859dca 325 if(den!=0)
raizel_varun 0:ebdf4f859dca 326 //b[i]=b[i]/den; //there is a problem here. The code gets stuck here. Maf value is required
raizel_varun 0:ebdf4f859dca 327 ;
raizel_varun 0:ebdf4f859dca 328 }
raizel_varun 0:ebdf4f859dca 329
raizel_varun 0:ebdf4f859dca 330 // select kz, kmu, gamma
raizel_varun 0:ebdf4f859dca 331 if(b[0]>0.9 || b[0]<-0.9)
raizel_varun 0:ebdf4f859dca 332 {
raizel_varun 0:ebdf4f859dca 333 kz = kz2;
raizel_varun 0:ebdf4f859dca 334 kmu = kmu2;
raizel_varun 0:ebdf4f859dca 335 gamma = gamma2;
raizel_varun 0:ebdf4f859dca 336 }
raizel_varun 0:ebdf4f859dca 337 // calculate Mu, v, dv, z, u
raizel_varun 0:ebdf4f859dca 338 for(i=0;i<2;i++)
raizel_varun 0:ebdf4f859dca 339 {
raizel_varun 0:ebdf4f859dca 340 Mu[i] = b[i+1];
raizel_varun 0:ebdf4f859dca 341 v[i] = -kmu*Mu[i];
raizel_varun 0:ebdf4f859dca 342 dv[i] = -kmu*db[i+1];
raizel_varun 0:ebdf4f859dca 343 z[i] = db[i+1] - v[i];
raizel_varun 0:ebdf4f859dca 344 u[i] = -kz*z[i] + dv[i]-(Mu[i] / gamma);
raizel_varun 0:ebdf4f859dca 345 }
raizel_varun 0:ebdf4f859dca 346 inverse(Jm, invJm);
raizel_varun 0:ebdf4f859dca 347 // calculate cross(omega,J*omega)for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 348
raizel_varun 0:ebdf4f859dca 349 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 350 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 351
raizel_varun 0:ebdf4f859dca 352 // calculate invJm*cross(omega,J*omega) store in d
raizel_varun 0:ebdf4f859dca 353 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 354 {
raizel_varun 0:ebdf4f859dca 355 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 356 d[i] += bb[j]*invJm[i][j];
raizel_varun 0:ebdf4f859dca 357 }
raizel_varun 0:ebdf4f859dca 358 // calculate d = cross(invJm*cross(omega,J*omega),b) -cross(omega,db)
raizel_varun 0:ebdf4f859dca 359 // bb =[0;u-d(2:3)]
raizel_varun 0:ebdf4f859dca 360 // store in bb
raizel_varun 0:ebdf4f859dca 361 bb[1] = u[0] + (d[0]*b[2])-(d[2]*b[0])-(omega[0]*db[2]) + (omega[2]*db[0]);
raizel_varun 0:ebdf4f859dca 362 bb[2] = u[1]-(d[0]*b[1]) + (d[1]*b[0]) + (omega[0]*db[1])-(omega[1]*db[0]);
raizel_varun 0:ebdf4f859dca 363 bb[0] = 0;
raizel_varun 0:ebdf4f859dca 364 // calculate N
raizel_varun 0:ebdf4f859dca 365 // reusing invJm as N
raizel_varun 0:ebdf4f859dca 366
raizel_varun 0:ebdf4f859dca 367 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 368 {
raizel_varun 0:ebdf4f859dca 369 d[i] = invJm[1][i];
raizel_varun 0:ebdf4f859dca 370 invJm[ 1][i] = b[2]*invJm[0][i] - b[0]*invJm[2][i];
raizel_varun 0:ebdf4f859dca 371 invJm[2][i] = -b[1]*invJm[0][i] + b[0]*d[i];
raizel_varun 0:ebdf4f859dca 372 invJm[0][i] = b[i];
raizel_varun 0:ebdf4f859dca 373 }
raizel_varun 0:ebdf4f859dca 374 // calculate inv(N) store in Jm
raizel_varun 0:ebdf4f859dca 375 inverse(invJm, Jm);
raizel_varun 0:ebdf4f859dca 376 // calculate tauc
raizel_varun 0:ebdf4f859dca 377 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 378 {
raizel_varun 0:ebdf4f859dca 379 for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 380 tauc[i] += Jm[i][j]*bb[j];
raizel_varun 0:ebdf4f859dca 381 }
raizel_varun 0:ebdf4f859dca 382
raizel_varun 0:ebdf4f859dca 383 return(tauc);
raizel_varun 0:ebdf4f859dca 384 }
raizel_varun 0:ebdf4f859dca 385 }
raizel_varun 0:ebdf4f859dca 386 /////////// Output to Matlab //////////////////
raizel_varun 0:ebdf4f859dca 387 /* for(i=0;i<3;i++) {
raizel_varun 0:ebdf4f859dca 388 printf("%f\n",tauc[i]*10000000);
raizel_varun 0:ebdf4f859dca 389 wait_ms(10);
raizel_varun 0:ebdf4f859dca 390 }
raizel_varun 0:ebdf4f859dca 391 }
raizel_varun 0:ebdf4f859dca 392
raizel_varun 0:ebdf4f859dca 393 }*/
raizel_varun 0:ebdf4f859dca 394 void inverse(float mat[3][3], float inv[3][3])
raizel_varun 0:ebdf4f859dca 395 {
raizel_varun 0:ebdf4f859dca 396 int i, j;
raizel_varun 0:ebdf4f859dca 397 float det = 0;
raizel_varun 0:ebdf4f859dca 398 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 399 { for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 400 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 401 [(j+1)%3]*mat[(i+1)%3][(j+2)%3]);
raizel_varun 0:ebdf4f859dca 402 }
raizel_varun 0:ebdf4f859dca 403 det += (mat[0][0]*inv[0][0]) + (mat[0][1]*inv[1][0]) + (mat[0][2]*inv[2][0]);
raizel_varun 0:ebdf4f859dca 404 for(i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 405 { for(j=0;j<3;j++)
raizel_varun 0:ebdf4f859dca 406 inv[i][j] /= det;
raizel_varun 0:ebdf4f859dca 407 }
raizel_varun 0:ebdf4f859dca 408 }/*
raizel_varun 0:ebdf4f859dca 409 void getInput (float x[9]) {
raizel_varun 0:ebdf4f859dca 410 //Functions used to generate PWM signal
raizel_varun 0:ebdf4f859dca 411 //PWM output comes from pins p6
raizel_varun 0:ebdf4f859dca 412 Serial pc1(USBTX, USBRX);
raizel_varun 0:ebdf4f859dca 413 char c[10];
raizel_varun 0:ebdf4f859dca 414 char tempchar[8];
raizel_varun 0:ebdf4f859dca 415 int i, j;
raizel_varun 0:ebdf4f859dca 416 //float f[9];
raizel_varun 0:ebdf4f859dca 417 long n = 0;
raizel_varun 0:ebdf4f859dca 418 float flval = 0;
raizel_varun 0:ebdf4f859dca 419 for(j=0;j<9;j++) {
raizel_varun 0:ebdf4f859dca 420 for(i=0;i<9;i++) {
raizel_varun 0:ebdf4f859dca 421 c[i] = pc1.getc(); if(i<8) {
raizel_varun 0:ebdf4f859dca 422 tempchar[i] = c[i];
raizel_varun 0:ebdf4f859dca 423 }
raizel_varun 0:ebdf4f859dca 424 }
raizel_varun 0:ebdf4f859dca 425 sscanf (tempchar, "%8x", &n);
raizel_varun 0:ebdf4f859dca 426 memcpy(&flval, &n, sizeof(long));
raizel_varun 0:ebdf4f859dca 427 printf("%f\n", flval);
raizel_varun 0:ebdf4f859dca 428 x[j] = flval;
raizel_varun 0:ebdf4f859dca 429 }
raizel_varun 0:ebdf4f859dca 430 }*/
raizel_varun 0:ebdf4f859dca 431
raizel_varun 0:ebdf4f859dca 432 void trSub();
raizel_varun 0:ebdf4f859dca 433 void drSub();
raizel_varun 0:ebdf4f859dca 434 void init_gyro();
raizel_varun 0:ebdf4f859dca 435 float * FUNC_ACS_EXEC_GYR();
raizel_varun 0:ebdf4f859dca 436
raizel_varun 0:ebdf4f859dca 437 void drSub() //In this function we setting data-ready flag to 1
raizel_varun 0:ebdf4f859dca 438 {
raizel_varun 0:ebdf4f859dca 439 drFlag=1;
raizel_varun 0:ebdf4f859dca 440 }
raizel_varun 0:ebdf4f859dca 441 void trSub() //In this function we are setting ticker flag to 0
raizel_varun 0:ebdf4f859dca 442 {
raizel_varun 0:ebdf4f859dca 443 trFlag=0;
raizel_varun 0:ebdf4f859dca 444 }
raizel_varun 0:ebdf4f859dca 445 void FUNC_ACS_INIT_GYR()
raizel_varun 0:ebdf4f859dca 446 {
raizel_varun 0:ebdf4f859dca 447 uint8_t response;
raizel_varun 0:ebdf4f859dca 448 ssn_gyr=1; //Deselecting the chip
greenroshks 2:edd107ea4740 449 spi_acs.format(8,0); // Spi format is 8 bits, and clock mode 3
raizel_varun 0:ebdf4f859dca 450 spi_acs.frequency(1000000); //frequency to be set as 1MHz
raizel_varun 0:ebdf4f859dca 451 drFlag=0; //Intially defining data-ready flag to be 0
raizel_varun 0:ebdf4f859dca 452 dr.mode(PullDown);
raizel_varun 0:ebdf4f859dca 453 dr.rise(&drSub);
raizel_varun 0:ebdf4f859dca 454 __disable_irq();
raizel_varun 0:ebdf4f859dca 455
raizel_varun 0:ebdf4f859dca 456 /*Following the above mentioned algorithm for initializing the register and changing its configuration*/
raizel_varun 0:ebdf4f859dca 457 ssn_gyr=0; //Selecting chip(Mpu-3300)
raizel_varun 0:ebdf4f859dca 458 spi_acs.write(USER_CTRL|READFLAG); //sending USER_CTRL address with read bit
raizel_varun 0:ebdf4f859dca 459 response=spi_acs.write(DUMMYBIT); //sending dummy bit to get default values of the register
raizel_varun 0:ebdf4f859dca 460
raizel_varun 0:ebdf4f859dca 461 ssn_gyr=1; //Deselecting the chip
raizel_varun 0:ebdf4f859dca 462 wait(0.1); //waiting according the product specifications
raizel_varun 0:ebdf4f859dca 463
raizel_varun 0:ebdf4f859dca 464 ssn_gyr=0; //again selecting the chip
raizel_varun 0:ebdf4f859dca 465 spi_acs.write(USER_CTRL); //sending USER_CTRL address without read bit
raizel_varun 0:ebdf4f859dca 466 spi_acs.write(response|BIT_I2C_IF_DIS); //disabling the I2C mode in the register
raizel_varun 0:ebdf4f859dca 467 ssn_gyr=1; //deselecting the chip
raizel_varun 0:ebdf4f859dca 468 wait(0.1); // waiting for 100ms before going for another register
raizel_varun 0:ebdf4f859dca 469
raizel_varun 0:ebdf4f859dca 470 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 471 spi_acs.write(PWR_MGMT_1|READFLAG); //Power Management register-1
raizel_varun 0:ebdf4f859dca 472 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 473 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 474 wait(0.1);
raizel_varun 0:ebdf4f859dca 475
raizel_varun 0:ebdf4f859dca 476 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 477 spi_acs.write(PWR_MGMT_1);
raizel_varun 0:ebdf4f859dca 478 response=spi_acs.write(response|BIT_CLKSEL_X); //Selecting the X axis gyroscope as clock as mentioned above
raizel_varun 0:ebdf4f859dca 479 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 480 wait(0.1);
raizel_varun 0:ebdf4f859dca 481
raizel_varun 0:ebdf4f859dca 482 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 483 spi_acs.write(GYRO_CONFIG|READFLAG); //sending GYRO_CONFIG address with read bit
raizel_varun 0:ebdf4f859dca 484 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 485 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 486 wait(0.1);
raizel_varun 0:ebdf4f859dca 487
raizel_varun 0:ebdf4f859dca 488 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 489 spi_acs.write(GYRO_CONFIG); //sending GYRO_CONFIG address to write to register
raizel_varun 0:ebdf4f859dca 490 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 491 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 492 wait(0.1);
raizel_varun 0:ebdf4f859dca 493
raizel_varun 0:ebdf4f859dca 494 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 495 spi_acs.write(CONFIG|READFLAG); //sending CONFIG address with read bit
raizel_varun 0:ebdf4f859dca 496 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 497 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 498 wait(0.1);
raizel_varun 0:ebdf4f859dca 499
raizel_varun 0:ebdf4f859dca 500 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 501 spi_acs.write(CONFIG); //sending CONFIG address to write to register
raizel_varun 0:ebdf4f859dca 502 spi_acs.write(response|BITS_DLPF_CFG); //selecting a bandwidth of 42 hz and delay of 4.8ms
raizel_varun 0:ebdf4f859dca 503 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 504 wait(0.1);
raizel_varun 0:ebdf4f859dca 505
raizel_varun 0:ebdf4f859dca 506 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 507 spi_acs.write(SMPLRT_DIV|READFLAG); //sending SMPLRT_DIV address with read bit
raizel_varun 0:ebdf4f859dca 508 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 509 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 510 wait(0.1);
raizel_varun 0:ebdf4f859dca 511
raizel_varun 0:ebdf4f859dca 512 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 513 spi_acs.write(SMPLRT_DIV); //sending SMPLRT_DIV address to write to register
raizel_varun 0:ebdf4f859dca 514 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 515 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 516 wait(0.1);
raizel_varun 0:ebdf4f859dca 517
raizel_varun 0:ebdf4f859dca 518 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 519 spi_acs.write(INT_ENABLE|READFLAG); //sending address of INT_ENABLE with readflag
raizel_varun 0:ebdf4f859dca 520 response=spi_acs.write(DUMMYBIT); //sending dummy byte to get the default values of the
raizel_varun 0:ebdf4f859dca 521 // regiser
raizel_varun 0:ebdf4f859dca 522 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 523 wait(0.1);
raizel_varun 0:ebdf4f859dca 524
raizel_varun 0:ebdf4f859dca 525 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 526 spi_acs.write(INT_ENABLE); //sending INT_ENABLE address to write to register
raizel_varun 0:ebdf4f859dca 527 spi_acs.write(response|BIT_DATA_RDY_ENABLE); //Enbling data ready interrupt
raizel_varun 0:ebdf4f859dca 528 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 529 wait(0.1);
raizel_varun 0:ebdf4f859dca 530
raizel_varun 0:ebdf4f859dca 531 __enable_irq();
raizel_varun 0:ebdf4f859dca 532 }
raizel_varun 0:ebdf4f859dca 533
raizel_varun 0:ebdf4f859dca 534 float * FUNC_ACS_EXEC_GYR()
raizel_varun 0:ebdf4f859dca 535 {
raizel_varun 0:ebdf4f859dca 536 printf("\nEntered gyro\n");
raizel_varun 0:ebdf4f859dca 537 uint8_t response;
raizel_varun 0:ebdf4f859dca 538 uint8_t MSB,LSB;
raizel_varun 0:ebdf4f859dca 539 int16_t bit_data;
raizel_varun 0:ebdf4f859dca 540 float data[3],error[3]={0,0,0}; //declaring error array to add to the values when required
raizel_varun 0:ebdf4f859dca 541 float senstivity = 145.6; //senstivity is 145.6 for full scale mode of +/-225 deg/sec
raizel_varun 0:ebdf4f859dca 542 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 543 spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of INT_ENABLE with readflag
raizel_varun 0:ebdf4f859dca 544 response=spi_acs.write(DUMMYBIT); //
raizel_varun 0:ebdf4f859dca 545 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 546 wait(0.1);
raizel_varun 0:ebdf4f859dca 547
raizel_varun 0:ebdf4f859dca 548 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 549 spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
raizel_varun 0:ebdf4f859dca 550 response=spi_acs.write(response&(~(BIT_SLEEP))); //waking up the gyroscope from sleep
raizel_varun 0:ebdf4f859dca 551 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 552 wait(0.1);
raizel_varun 0:ebdf4f859dca 553
raizel_varun 0:ebdf4f859dca 554 trFlag=1;
raizel_varun 0:ebdf4f859dca 555 tr.attach(&trSub,1); //executes the function trSub afer 1sec
raizel_varun 0:ebdf4f859dca 556
raizel_varun 0:ebdf4f859dca 557 while(trFlag)
raizel_varun 0:ebdf4f859dca 558 {
raizel_varun 0:ebdf4f859dca 559 wait_ms(5); //This is required for this while loop to exit. I don't know why.
raizel_varun 0:ebdf4f859dca 560 if(drFlag==1)
raizel_varun 0:ebdf4f859dca 561 {
raizel_varun 0:ebdf4f859dca 562 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 563 spi_acs.write(GYRO_XOUT_H|READFLAG); //sending address of PWR_MGMT_1 with readflag
raizel_varun 0:ebdf4f859dca 564 for(int i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 565 {
raizel_varun 0:ebdf4f859dca 566 MSB = spi_acs.write(DUMMYBIT); //reading the MSB values of x,y and z respectively
raizel_varun 0:ebdf4f859dca 567 LSB = spi_acs.write(DUMMYBIT); //reading the LSB values of x,y and z respectively
raizel_varun 0:ebdf4f859dca 568 bit_data= ((int16_t)MSB<<8)|LSB; //concatenating to get 16 bit 2's complement of the required gyroscope values
raizel_varun 0:ebdf4f859dca 569 data[i]=(float)bit_data;
raizel_varun 0:ebdf4f859dca 570 data[i]=data[i]/senstivity; //dividing with senstivity to get the readings in deg/sec
raizel_varun 0:ebdf4f859dca 571 data[i]+=error[i]; //adding with error to remove offset errors
raizel_varun 0:ebdf4f859dca 572 }
raizel_varun 0:ebdf4f859dca 573 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 574 for (int i=0;i<3;i++)
raizel_varun 0:ebdf4f859dca 575 {
raizel_varun 0:ebdf4f859dca 576 printf("%f\t",data[i]); //printing the angular velocity values
raizel_varun 0:ebdf4f859dca 577 }
raizel_varun 0:ebdf4f859dca 578 printf("\n");
raizel_varun 0:ebdf4f859dca 579 break;
raizel_varun 0:ebdf4f859dca 580 }
raizel_varun 0:ebdf4f859dca 581 drFlag=0;
raizel_varun 0:ebdf4f859dca 582 }
raizel_varun 0:ebdf4f859dca 583 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 584 spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of PWR_MGMT_1 with readflag
raizel_varun 0:ebdf4f859dca 585 response=spi_acs.write(DUMMYBIT);
raizel_varun 0:ebdf4f859dca 586 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 587 wait(0.1);
raizel_varun 0:ebdf4f859dca 588
raizel_varun 0:ebdf4f859dca 589 ssn_gyr=0;
raizel_varun 0:ebdf4f859dca 590 spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
raizel_varun 0:ebdf4f859dca 591 response=spi_acs.write(response|BIT_SLEEP); //setting the gyroscope in sleep mode
raizel_varun 0:ebdf4f859dca 592 ssn_gyr=1;
raizel_varun 0:ebdf4f859dca 593 wait(0.1);
raizel_varun 0:ebdf4f859dca 594 printf("\nExited gyro\n");
raizel_varun 0:ebdf4f859dca 595 return data;
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
raizel_varun 0:ebdf4f859dca 602