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