|
| 1 | +' MPU6050 - Accelerometer, gyroscope and temperature sensor |
| 2 | +' ========================================================= |
| 3 | +' |
| 4 | +' This examample demonstrates how to use |
| 5 | +' the MPU-6050 I2C sensor. |
| 6 | +' |
| 7 | +' Connect the sensor to the IOIO-OTG board: |
| 8 | +' |
| 9 | +' ------ ------ |
| 10 | +' IOIO | |MPU6050 |
| 11 | +' PIN 4|-------|SDA |
| 12 | +' PIN 5|-------|SCL |
| 13 | +' GND |-------|GND |
| 14 | +' 3.3V |-------|VIN |
| 15 | +' | |XCL |
| 16 | +' | |XDA |
| 17 | +' | |ADD |
| 18 | +' | |INT |
| 19 | +'------- ------ |
| 20 | + |
| 21 | +' If ADD is open or connected to GND, 0x68 as I2C address |
| 22 | +' will be used. Otherwise 0x69. |
| 23 | +' Don't connect XCL, XDA, ADD and INT |
| 24 | +' |
| 25 | +' "https://github.com/tockn/MPU6050_tockn" was very helpful |
| 26 | +' to get the sensor working |
| 27 | + |
| 28 | +import ioio |
| 29 | + |
| 30 | +const ADDRESS = 0x68 |
| 31 | + |
| 32 | +Print "Connect to MPU-6050" |
| 33 | +sensor = ioio.openTwiMaster(0, 0) |
| 34 | +ioio.waitForConnect(10) |
| 35 | +Print "Connection established" |
| 36 | + |
| 37 | +delay(500) |
| 38 | + |
| 39 | +WhoamI = sensor.readwrite(ADDRESS, 1, 0x75) |
| 40 | +print "WHO_AM_I: ", hex(WhoamI) ' Check for connection: sensor returns 0x68 |
| 41 | + |
| 42 | +' SMPLRT_DIV |
| 43 | +sensor.write(ADDRESS, 0x19, 0x00) |
| 44 | +' MPU config |
| 45 | +sensor.write(ADDRESS, 0x1A, 0x00) |
| 46 | +' Gyro config |
| 47 | +sensor.write(ADDRESS, 0x1B, 0x08) |
| 48 | +' Accel config |
| 49 | +sensor.write(ADDRESS, 0x1C, 0x00) |
| 50 | +' Turn on |
| 51 | +sensor.write(ADDRESS, 0x6B, 0x01) |
| 52 | + |
| 53 | +for ii = 1 to 1000 |
| 54 | + |
| 55 | + A = GetAcceleration() |
| 56 | + G = GetGyroscope() |
| 57 | + T = GetTemperature() |
| 58 | + |
| 59 | + locate 5,0 |
| 60 | + print "Acc: ["; |
| 61 | + print USING "##.00 "; A.AccX, A.AccY, A.AccZ; |
| 62 | + print "] Gryo: ["; |
| 63 | + print USING "####.00 "; G.GyrX, G.GyrY, G.GyrZ; |
| 64 | + print "] Temp : "; |
| 65 | + print USING "##.00 "; T |
| 66 | + |
| 67 | + delay(100) |
| 68 | + showpage |
| 69 | +next |
| 70 | + |
| 71 | + |
| 72 | +func GetAcceleration() |
| 73 | + local A |
| 74 | + dim A |
| 75 | + |
| 76 | + A.AccX = short((sensor.readwrite(ADDRESS, 1, 0x3B) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x3C)) / 16384 |
| 77 | + A.AccY = short((sensor.readwrite(ADDRESS, 1, 0x3D) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x3E)) / 16384 |
| 78 | + A.AccZ = short((sensor.readwrite(ADDRESS, 1, 0x3F) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x40)) / 16384 |
| 79 | + |
| 80 | + return A |
| 81 | +end |
| 82 | + |
| 83 | +func GetGyroscope() |
| 84 | + local d |
| 85 | + dim d |
| 86 | + |
| 87 | + d.GyrX = short((sensor.readwrite(ADDRESS, 1, 0x43) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x44)) / 65.5 |
| 88 | + d.GyrY = short((sensor.readwrite(ADDRESS, 1, 0x45) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x46)) / 65.5 |
| 89 | + d.GyrZ = short((sensor.readwrite(ADDRESS, 1, 0x47) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x48)) / 65.5 |
| 90 | + |
| 91 | + return d |
| 92 | +end |
| 93 | + |
| 94 | +func GetTemperature() |
| 95 | + return short((sensor.readwrite(ADDRESS, 1, 0x41) lshift 8) BOR sensor.readwrite(ADDRESS, 1, 0x42)) / 340 + 36.53 |
| 96 | +end |
| 97 | + |
| 98 | +func CalculateAccelerationAngle(AccX, AccY, AccZ) |
| 99 | + local d |
| 100 | + dim d |
| 101 | + |
| 102 | + d.AngleAccX = atan2(AccY, sqr(AccZ^2 + AccX^2)) * 360 / 2.0 / PI |
| 103 | + d.AngleAccY = atan2(AccX, sqr(AccZ^2 * AccY^2)) * 360 / -2.0 / PI |
| 104 | + |
| 105 | + return d |
| 106 | +end |
| 107 | + |
| 108 | +func short(dat) |
| 109 | + if dat > 32767 then |
| 110 | + return dat - 65536 |
| 111 | + else |
| 112 | + return dat |
| 113 | + endif |
| 114 | +end |
0 commit comments