Hello guys
I am working on a project that calculates the angle to be turned by a robot. And then turn the robot to that specific angle from an initial stage.
I understand that using a gyrometer i can keep an eye on the degrees(or radians ) of angle the robot has turned, and make sure that the robot stops at the specific angle. But im not sure it works.
I have found this code on the internet that gives a reading in degrees when the gyrometer is turned. But it gives only the rate of change of angle and not the total angle itself. On doing some research on this topic i found out that the rate of change should be integrated in order to find the total angular displacement. But i didnt quit understand the statement. Can any one please tell me how to resolve this problem.#include <Wire.h>
long gyroZ;
float rotZ;
void setup()
{
Serial.begin(9600);
Wire.begin();
setupMPU();
}
void loop()
{
recordGyroRegisters();
printData();
delay(100);
}
void setupMPU()
{
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordGyroRegisters()
{
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
while(Wire.available() < 6);
gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processGyroData();
}
void processGyroData()
{
rotZ = gyroZ / 131.0;
}
void printData() {
Serial.print(" Z=");
Serial.print(rotZ);
delay(1000);
}