Thank you for your response!! I little bit worked more on the code, and now is behaving little bit strange. I am using MPU6050 with library Kalman.h and PID control also with library
PID: Arduino Playground - PIDLibrary
Kalman: KalmanFilter/Kalman.h at master · TKJElectronics/KalmanFilter · GitHub
The MPU on flat surface has still values around 0,7. I dont know why if you could help me i would really appraise it!
The PID control is the completelly right value for the Angle value on the left column. Setpoint = 0. I would understand if the PID value would not be all the time 0 becasue of the inaccuracy.
The PID value starts at 0, but everytime when I tilt MPU in X direction(the first column) and back the PID value increases to one number and when i have it on table it allready is not 0 but another number,now 13!
code:
#include <PID_v1.h>
#include <Servo.h>
#include <Wire.h>
#include "Kalman.h"
#define COM_CONST1 0.99
#define COM_CONST2 0.1
#define LSBsensitivity 131
Kalman kalmanX;
Kalman kalmanY;
Servo xservo;
uint8_t IMUAddress = 0x68; // MPU6050 Address
/* IMU Data */
int16_t accX;
int16_t accY;
int16_t accZ;
int16_t tempRaw;
int16_t gyroX;
int16_t gyroY;
int16_t gyroZ;
int mapX;
double pitch; //accXangle
double roll; //accYangle
double gyroXangle = 9;
double gyroYangle = 180;
double kalAngleX;
double kalAngleY;
uint32_t timer;
//Define Variables we'll be connecting to
double Setpoint, Output,Output2;
//Define the aggressive and conservative Tuning Parameters
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;
PID myPID(&kalAngleX, &Output2, &Setpoint, consKp, consKi, consKd, REVERSE);
PID myPID2(&kalAngleX, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
// ---------- VOID SETUP START -------------- /
void setup() {
Serial.begin(115200);
// xservo.attach(9);
// xservo.write(90);
Wire.begin();
i2cWrite(0x6B,0x00); // Disable sleep mode
if(i2cRead(0x75,1)[0] != IMUAddress) { // Read "WHO_AM_I" register
Serial.print(F("MPU-6050 with address 0x"));
Serial.print(IMUAddress,HEX);
Serial.println(F(" is not connected"));
while(1);
}
readValues();
rollpitch();
kalmanX.setAngle(roll); // Set starting angle
kalmanY.setAngle(pitch);
gyroXangle = roll;
gyroYangle = pitch;
timer = micros();
Setpoint = 0;
myPID.SetMode(AUTOMATIC);
myPID2.SetMode(AUTOMATIC);
}
void loop() {
/* Update all the values */
readValues();
KalmanValue();
double gap = abs(Setpoint-kalAngleX); //distance away from setpoint
if(gap<10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
myPID2.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
myPID2.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
myPID2.Compute();
Serial.print(kalAngleX);
Serial.print("\t");
Serial.print(kalAngleY);
Serial.print("\t");
Serial.print(Output+Output2);
Serial.print("\t");
Serial.print("\r\n");
delay(20); // delay to allow servos to move (ms)
}
void moveServo()
{
mapX = map(kalAngleX, 80, 280, 0, 179); //calculate limitation of servo mechanical
Serial.print("\t");
xservo.write(mapX); // Send signal to servo
Serial.print(mapX);
Serial.print("\n");
}
void KalmanValue()
{
rollpitch();
double dt = (double)(micros()-timer)/1000000;
timer = micros();
double gyroXrate = ( (double) gyroX / LSBsensitivity ); // LSB Sensitivity 131 LSB/s Full scale range = +- 250 degree/s (datasheet)
double gyroYrate =( (double) gyroY / LSBsensitivity );
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt) +0.7; // Calculate the angle using a Kalman filter
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter
}
void rollpitch()
{
roll = atan(accY / sqrt(pow(accX,2) + pow(accZ,2))) * RAD_TO_DEG;
pitch = atan(-accX / sqrt(pow(accY,2) + pow(accZ,2))) * RAD_TO_DEG;
}
// I2C
void i2cWrite(uint8_t registerAddress, uint8_t data){
Wire.beginTransmission(IMUAddress);
Wire.write(registerAddress);
Wire.write(data);
Wire.endTransmission(); // Send stop
}
uint8_t* i2cRead(uint8_t registerAddress, uint8_t nbytes) {
uint8_t data[nbytes];
Wire.beginTransmission(IMUAddress);
Wire.write(registerAddress);
Wire.endTransmission(false); // Don't release the bus
Wire.requestFrom(IMUAddress, nbytes); // Send a repeated start and then release the bus after reading
for(uint8_t i = 0; i < nbytes; i++)
data[i] = Wire.read();
return data;
}
void readValues()
{
uint8_t* data = i2cRead(0x3B,14);
accX = ((data[0] << 8) | data[1]);
accY = ((data[2] << 8) | data[3]);
accZ = ((data[4] << 8) | data[5]);
tempRaw = ((data[6] << 8) | data[7]);
gyroX = ((data[8] << 8) | data[9]);
gyroY = ((data[10] << 8) | data[11]);
gyroZ = ((data[12] << 8) | data[13]);
return ;
}
THANK YOU SO MUCH FOR HELP