diff --git a/README.md b/README.md index 1ae178e..262cafa 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,9 @@ # mpu6050 +This is a copy of the following library: +[MPU6050_tockn](https://github.com/tockn/MPU6050_tockn) +This copy does contains small changes. -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.com/jusax23/mpu6050.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.com/jusax23/mpu6050/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +# Licence +MIT \ No newline at end of file diff --git a/examples/GetAllData/GetAllData.ino b/examples/GetAllData/GetAllData.ino new file mode 100644 index 0000000..d97c3f0 --- /dev/null +++ b/examples/GetAllData/GetAllData.ino @@ -0,0 +1,46 @@ + +#include +#include + +MPU6050 mpu6050(Wire); + +long timer = 0; + +void setup() { + Serial.begin(9600); + Wire.begin(); + mpu6050.begin(); + mpu6050.calcGyroOffsets(true); +} + +void loop() { + mpu6050.update(); + + if(millis() - timer > 1000){ + + Serial.println("======================================================="); + Serial.print("temp : ");Serial.println(mpu6050.getTemp()); + Serial.print("accX : ");Serial.print(mpu6050.getAccX()); + Serial.print("\taccY : ");Serial.print(mpu6050.getAccY()); + Serial.print("\taccZ : ");Serial.println(mpu6050.getAccZ()); + + Serial.print("gyroX : ");Serial.print(mpu6050.getGyroX()); + Serial.print("\tgyroY : ");Serial.print(mpu6050.getGyroY()); + Serial.print("\tgyroZ : ");Serial.println(mpu6050.getGyroZ()); + + Serial.print("accAngleX : ");Serial.print(mpu6050.getAccAngleX()); + Serial.print("\taccAngleY : ");Serial.println(mpu6050.getAccAngleY()); + + Serial.print("gyroAngleX : ");Serial.print(mpu6050.getGyroAngleX()); + Serial.print("\tgyroAngleY : ");Serial.print(mpu6050.getGyroAngleY()); + Serial.print("\tgyroAngleZ : ");Serial.println(mpu6050.getGyroAngleZ()); + + Serial.print("angleX : ");Serial.print(mpu6050.getAngleX()); + Serial.print("\tangleY : ");Serial.print(mpu6050.getAngleY()); + Serial.print("\tangleZ : ");Serial.println(mpu6050.getAngleZ()); + Serial.println("=======================================================\n"); + timer = millis(); + + } + +} diff --git a/examples/GetAngle/GetAngle.ino b/examples/GetAngle/GetAngle.ino new file mode 100644 index 0000000..18c3b0f --- /dev/null +++ b/examples/GetAngle/GetAngle.ino @@ -0,0 +1,22 @@ + +#include +#include + +MPU6050 mpu6050(Wire); + +void setup() { + Serial.begin(9600); + Wire.begin(); + mpu6050.begin(); + mpu6050.calcGyroOffsets(true); +} + +void loop() { + mpu6050.update(); + Serial.print("angleX : "); + Serial.print(mpu6050.getAngleX()); + Serial.print("\tangleY : "); + Serial.print(mpu6050.getAngleY()); + Serial.print("\tangleZ : "); + Serial.println(mpu6050.getAngleZ()); +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..a3fc9a9 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=mpu6050 +version=1.0.0 +author=tockn +maintainer=jusax23 +sentence=Arduino library for easy communicating with the MPU6050. +paragraph=It acces get accel, gyro, and angle data. +category=Sensors +url=https://gitlab.com/jusax23/mpu6050 +architectures=* diff --git a/src/MPU6050_jusax23.cpp b/src/MPU6050_jusax23.cpp new file mode 100644 index 0000000..3f6c7ad --- /dev/null +++ b/src/MPU6050_jusax23.cpp @@ -0,0 +1,146 @@ +#include "MPU6050_jusax23.h" +#include "Arduino.h" + +MPU6050::MPU6050(TwoWire &w){ + wire = &w; + accCoef = 0.02f; + gyroCoef = 0.98f; +} + +MPU6050::MPU6050(TwoWire &w, float aC, float gC){ + wire = &w; + accCoef = aC; + gyroCoef = gC; +} + +void MPU6050::begin(){ + writeMPU6050(MPU6050_SMPLRT_DIV, 0x00); + writeMPU6050(MPU6050_CONFIG, 0x00); + writeMPU6050(MPU6050_GYRO_CONFIG, 0x08); + writeMPU6050(MPU6050_ACCEL_CONFIG, 0x00); + writeMPU6050(MPU6050_PWR_MGMT_1, 0x01); + this->update(); + angleGyroX = 0; + angleGyroY = 0; + angleX = this->getAccAngleX(); + angleY = this->getAccAngleY(); + preInterval = millis(); +} + +void MPU6050::writeMPU6050(byte reg, byte data){ + wire->beginTransmission(MPU6050_ADDR); + wire->write(reg); + wire->write(data); + wire->endTransmission(); +} + +byte MPU6050::readMPU6050(byte reg) { + wire->beginTransmission(MPU6050_ADDR); + wire->write(reg); + wire->endTransmission(true); + wire->requestFrom(MPU6050_ADDR, 1); + byte data = wire->read(); + return data; +} + +void MPU6050::setGyroOffsets(float x, float y, float z){ + gyroXoffset = x; + gyroYoffset = y; + gyroZoffset = z; +} + +void MPU6050::calcGyroOffsets(bool console, uint16_t delayBefore, uint16_t delayAfter){ + float x = 0, y = 0, z = 0; + int16_t rx, ry, rz; + + delay(delayBefore); + if(console){ + Serial.println(); + Serial.println("========================================"); + Serial.println("Calculating gyro offsets"); + Serial.print("DO NOT MOVE MPU6050"); + } + for(int i = 0; i < 3000; i++){ + if(console && i % 1000 == 0){ + Serial.print("."); + } + wire->beginTransmission(MPU6050_ADDR); + wire->write(0x43); + wire->endTransmission(false); + wire->requestFrom((int)MPU6050_ADDR, 6); + + rx = wire->read() << 8 | wire->read(); + ry = wire->read() << 8 | wire->read(); + rz = wire->read() << 8 | wire->read(); + + x += ((float)rx) / 65.5; + y += ((float)ry) / 65.5; + z += ((float)rz) / 65.5; + } + gyroXoffset = x / 3000; + gyroYoffset = y / 3000; + gyroZoffset = z / 3000; + + if(console){ + Serial.println(); + Serial.println("Done!"); + Serial.print("X : ");Serial.println(gyroXoffset); + Serial.print("Y : ");Serial.println(gyroYoffset); + Serial.print("Z : ");Serial.println(gyroZoffset); + Serial.println("Program will start after 3 seconds"); + Serial.print("========================================"); + delay(delayAfter); + } +} + +void MPU6050::update(){ + wire->beginTransmission(MPU6050_ADDR); + wire->write(0x3B); + wire->endTransmission(false); + wire->requestFrom((int)MPU6050_ADDR, 14); + + rawAccX = wire->read() << 8 | wire->read(); + rawAccY = wire->read() << 8 | wire->read(); + rawAccZ = wire->read() << 8 | wire->read(); + rawTemp = wire->read() << 8 | wire->read(); + rawGyroX = wire->read() << 8 | wire->read(); + rawGyroY = wire->read() << 8 | wire->read(); + rawGyroZ = wire->read() << 8 | wire->read(); + + temp = (rawTemp + 12412.0) / 340.0; + + accX = ((float)rawAccX) / 16384.0; + accY = ((float)rawAccY) / 16384.0; + accZ = ((float)rawAccZ) / 16384.0; + + /*accX -= 0.04f; + accZ -= 0.3f;*/ + + /*angleAccX = atan2(accY, accZ + abs(accX)) * 360 / 2.0 / PI; + angleAccY = atan2(accX, accZ + abs(accY)) * 360 / -2.0 / PI;*/ + angleAccX = atan2(accY, sqrt(accZ * accZ + accX * accX)) * 360 / 2.0 / PI; + angleAccY = atan2(accX, sqrt(accZ * accZ + accY * accY)) * 360 / -2.0 / PI; + + gyroX = ((float)rawGyroX) / 65.5; + gyroY = ((float)rawGyroY) / 65.5; + gyroZ = ((float)rawGyroZ) / 65.5; + + gyroX -= gyroXoffset; + gyroY -= gyroYoffset; + gyroZ -= gyroZoffset; + + nowInterval = millis(); + interval = (nowInterval - preInterval) * 0.001; + preInterval = nowInterval; + + angleGyroX += gyroX * interval; + angleGyroY += gyroY * interval; + angleGyroZ += gyroZ * interval; + + angleX = (gyroCoef * (angleX + gyroX * interval)) + (accCoef * angleAccX); + angleY = (gyroCoef * (angleY + gyroY * interval)) + (accCoef * angleAccY); + angleZ = angleGyroZ; + + angleY -= angleX * sin(gyroZ * interval * (PI / 180)); //If the IMU has yawed transfer the roll angle to the pitch angel. + angleX += angleY * sin(gyroZ * interval * (PI / 180)); +} diff --git a/src/MPU6050_jusax23.h b/src/MPU6050_jusax23.h new file mode 100644 index 0000000..b6acdb3 --- /dev/null +++ b/src/MPU6050_jusax23.h @@ -0,0 +1,92 @@ +#ifndef MPU6050_jusax23_H +#define MPU6050_jusax23_H + +#include "Arduino.h" +#include "Wire.h" + +#define MPU6050_ADDR 0x68 +#define MPU6050_SMPLRT_DIV 0x19 +#define MPU6050_CONFIG 0x1a +#define MPU6050_GYRO_CONFIG 0x1b +#define MPU6050_ACCEL_CONFIG 0x1c +#define MPU6050_WHO_AM_I 0x75 +#define MPU6050_PWR_MGMT_1 0x6b +#define MPU6050_TEMP_H 0x41 +#define MPU6050_TEMP_L 0x42 + +class MPU6050{ + public: + + MPU6050(TwoWire &w); + MPU6050(TwoWire &w, float aC, float gC); + + void begin(); + + void setGyroOffsets(float x, float y, float z); + + void writeMPU6050(byte reg, byte data); + byte readMPU6050(byte reg); + + int16_t getRawAccX(){ return rawAccX; }; + int16_t getRawAccY(){ return rawAccY; }; + int16_t getRawAccZ(){ return rawAccZ; }; + + int16_t getRawTemp(){ return rawTemp; }; + + int16_t getRawGyroX(){ return rawGyroX; }; + int16_t getRawGyroY(){ return rawGyroY; }; + int16_t getRawGyroZ(){ return rawGyroZ; }; + + float getTemp(){ return temp; }; + + float getAccX(){ return accX; }; + float getAccY(){ return accY; }; + float getAccZ(){ return accZ; }; + + float getGyroX(){ return gyroX; }; + float getGyroY(){ return gyroY; }; + float getGyroZ(){ return gyroZ; }; + + void calcGyroOffsets(bool console = false, uint16_t delayBefore = 1000, uint16_t delayAfter = 3000); + + float getGyroXoffset(){ return gyroXoffset; }; + float getGyroYoffset(){ return gyroYoffset; }; + float getGyroZoffset(){ return gyroZoffset; }; + + void update(); + + float getAccAngleX(){ return angleAccX; }; + float getAccAngleY(){ return angleAccY; }; + + float getGyroAngleX(){ return angleGyroX; }; + float getGyroAngleY(){ return angleGyroY; }; + float getGyroAngleZ(){ return angleGyroZ; }; + + float getAngleX(){ return angleX; }; + float getAngleY(){ return angleY; }; + float getAngleZ(){ return angleZ; }; + + private: + + TwoWire *wire; + + int16_t rawAccX, rawAccY, rawAccZ, rawTemp, + rawGyroX, rawGyroY, rawGyroZ; + + float gyroXoffset, gyroYoffset, gyroZoffset; + + float temp, accX, accY, accZ, gyroX, gyroY, gyroZ; + + float angleGyroX, angleGyroY, angleGyroZ, + angleAccX, angleAccY, angleAccZ; + + float angleX, angleY, angleZ; + + float interval; + long preInterval; + long nowInterval; + + float accCoef, gyroCoef; +}; + +#endif