MAKOTO MOTOYAMA
/
Lab4-04_detect_angular_velocity_i2c
HeptaSat
main.cpp@0:c1b538c0d17b, 2022-08-09 (annotated)
- Committer:
- csmk18112
- Date:
- Tue Aug 09 02:56:22 2022 +0000
- Revision:
- 0:c1b538c0d17b
- Child:
- 1:13e640890938
mako
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
csmk18112 | 0:c1b538c0d17b | 1 | #include "mbed.h" |
csmk18112 | 0:c1b538c0d17b | 2 | #include "HEPTA_EPS.h" |
csmk18112 | 0:c1b538c0d17b | 3 | #define GYRO 0x69<<1 |
csmk18112 | 0:c1b538c0d17b | 4 | Serial pc(USBTX, USBRX); |
csmk18112 | 0:c1b538c0d17b | 5 | I2C i2c(p28,p27); |
csmk18112 | 0:c1b538c0d17b | 6 | DigitalOut pin(p26); |
csmk18112 | 0:c1b538c0d17b | 7 | |
csmk18112 | 0:c1b538c0d17b | 8 | float gyroscope[3]; |
csmk18112 | 0:c1b538c0d17b | 9 | char cmd[2]={0}; |
csmk18112 | 0:c1b538c0d17b | 10 | const double dt = 1; |
csmk18112 | 0:c1b538c0d17b | 11 | uint8_t data[6]={0}; |
csmk18112 | 0:c1b538c0d17b | 12 | char send[1], get[1]; |
csmk18112 | 0:c1b538c0d17b | 13 | char temp; |
csmk18112 | 0:c1b538c0d17b | 14 | |
csmk18112 | 0:c1b538c0d17b | 15 | int main() { |
csmk18112 | 0:c1b538c0d17b | 16 | pin=1; |
csmk18112 | 0:c1b538c0d17b | 17 | i2c.frequency(100000); |
csmk18112 | 0:c1b538c0d17b | 18 | printf("gyroscope setting\r\n"); |
csmk18112 | 0:c1b538c0d17b | 19 | cmd[0]=0x0F; |
csmk18112 | 0:c1b538c0d17b | 20 | cmd[1]=0x04; |
csmk18112 | 0:c1b538c0d17b | 21 | i2c.write(GYRO,cmd,2); |
csmk18112 | 0:c1b538c0d17b | 22 | cmd[0]=0x10; |
csmk18112 | 0:c1b538c0d17b | 23 | cmd[1]=0x07; |
csmk18112 | 0:c1b538c0d17b | 24 | i2c.write(GYRO,cmd,2); |
csmk18112 | 0:c1b538c0d17b | 25 | cmd[0]=0x11; |
csmk18112 | 0:c1b538c0d17b | 26 | cmd[1]=0x00; |
csmk18112 | 0:c1b538c0d17b | 27 | i2c.write(GYRO,cmd,2); |
csmk18112 | 0:c1b538c0d17b | 28 | printf("\r\nrun\r\n"); |
csmk18112 | 0:c1b538c0d17b | 29 | while(1) { |
csmk18112 | 0:c1b538c0d17b | 30 | for(int i=0;i<6;i++){ |
csmk18112 | 0:c1b538c0d17b | 31 | send[0]=(char)(2+i); |
csmk18112 | 0:c1b538c0d17b | 32 | i2c.write(GYRO,send,1); |
csmk18112 | 0:c1b538c0d17b | 33 | i2c.read(GYRO,get,1); |
csmk18112 | 0:c1b538c0d17b | 34 | temp=get[0]; |
csmk18112 | 0:c1b538c0d17b | 35 | data[i]=temp; |
csmk18112 | 0:c1b538c0d17b | 36 | } |
csmk18112 | 0:c1b538c0d17b | 37 | for(int i=0;i<3;i++){ |
csmk18112 | 0:c1b538c0d17b | 38 | gyroscope[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4; |
csmk18112 | 0:c1b538c0d17b | 39 | if(gyroscope[i]>32767)gyroscope[i]-=65536; |
csmk18112 | 0:c1b538c0d17b | 40 | gyroscope[i]=gyroscope[i]*125/2048; |
csmk18112 | 0:c1b538c0d17b | 41 | } |
csmk18112 | 0:c1b538c0d17b | 42 | pc.printf("gx = %2.4f, gy = %2.4f, gz = %2.4f\r\n",gyroscope[0],gyroscope[1],gyroscope[2]); |
csmk18112 | 0:c1b538c0d17b | 43 | wait(dt); |
csmk18112 | 0:c1b538c0d17b | 44 | } |
csmk18112 | 0:c1b538c0d17b | 45 | } |