1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include "Wire.h" // Arduino Wire library
#include "I2Cdev.h" //Installer ces 2 librairies
#include "MPU6050.h"
#include "math.h"
#include <AFMotor.h>
#define moteurDirectionPin 8
#define moteurVitessePin 11
#define moteurDirectionCW HIGH // Sens des aiguilles d'une montre
#define moteurDirectionCCW LOW
int16_t Acc_rawY,Gyr_rawY;
float Acceleration[1];
float Gyro_angle[1];
float Total_angle[1];
float Acceleration_angle[1];
float elapsedTime,time,timePrev;
int i;
float rad_to_deg = 180/3.141592654;
float PID,error;
float previous_error =0;
float pid_d =0;
float pid_i =0;
float pid_p =0;
double kp=3.55;
double kd=0.005;
double ki=2.05;
float desired_angle =0;
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
int inputPin =A0;
int inputPin1 =A1;
uint8_t Accel_range;
uint8_t Gyro_range;
float angle=0;
int16_t ax, ay, az; //mesures brutes
int16_t gx, gy, gz;
// Sens contraire (faudra peut etre inverser)
byte moteurVitesse = 250;
void setup() {
pinMode (moteurVitessePin, OUTPUT);
pinMode(moteurDirectionPin,OUTPUT);
Wire.begin(); //I2C bus
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB (LEONARDO)
}
// initialize device
Serial.println("Initialisation I2C...");
accelgyro.initialize();
// verify connection
Serial.println("Test de la conection du dispositif ...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection reussie" : "MPU6050 connection echec");
delay(1000);
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(inputPin,INPUT_PULLUP);
pinMode(inputPin1,INPUT_PULLUP);
time = millis();
}
void loop() {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
angle=0.0*(angle+float(gy)*0.01/131) + 1*atan2((double)ax,(double)az)*180/PI;
Serial.println(angle);
controlemotV2(angle);
delay(10);
}
void controlemotV2(int alpha) {
int Val1=digitalRead(inputPin);
Serial.println(Val1);
int Val2=digitalRead(inputPin1);
Serial.println(Val2);
if ( Val1==0){
moteurVitesse = 255;
analogWrite(moteurVitessePin,moteurVitesse);
digitalWrite(moteurDirectionPin, moteurDirectionCCW);
}
else if ( Val2==0){
moteurVitesse = 255;
analogWrite(moteurVitessePin,moteurVitesse);
digitalWrite(moteurDirectionPin, moteurDirectionCW);
}
else if (int (alpha < -10)){
moteurVitesse = 255;
analogWrite(moteurVitessePin,moteurVitesse);
digitalWrite(moteurDirectionPin, moteurDirectionCCW); //fait avancer le chariot vers la droite
}
else if (int (alpha > 10)){
moteurVitesse = 255;
analogWrite(moteurVitessePin,moteurVitesse);
digitalWrite(moteurDirectionPin, moteurDirectionCW); //fait avancer le chariot vers la gauche
}
else{
moteurVitesse = 0;
analogWrite(moteurVitessePin,moteurVitesse);
digitalWrite(moteurDirectionPin, moteurDirectionCCW);
}
}
void correcteur() {
timePrev = time;
time = millis();
elapsedTime = (time - timePrev) / 1000;
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,6,true);
Acc_rawY = Wire.read()<<8| Wire.read();
Gyro_angle[1] = Gyr_rawY/131;
Total_angle[1] = 0.98*(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];
error = Total_angle[1] - desired_angle;
pid_p = kp*error;
if ((-3<error) and (error<3)){
pid_i = pid_i +(ki*error);
}
pid_d = kd*((error - previous_error)/elapsedTime);
PID = pid_d + pid_i + pid_p;
// je ne suis pas du tout sur si cela peut etre mis dans le correcteur PID
for(i=0; i<255; i=i+5) {
if ((abs (int (angle(timePrev))) - abs(int( angle(time)))) < 2 ){
moteur.setSpeed(i)
moteur.run(BACKWARD);
}
else if ((abs (int alpha(t)) - abs(int alpha(t+1))) > 2 ){
moteur.setSpeed(i)
moterur.run(FORWARD);
}
}
previous-error=error
} |
Partager