that_one_nigga_you_k

teste giro com cruzada que deu certo

Mar 29th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <MPU6050_tockn.h>
  3.  
  4. int vel1;
  5. int vel2=255;
  6.  
  7. /*int  ing = 0;
  8. int  x = 0;
  9. int  xa = 0;*/
  10.  
  11. bool motorEsquerda = 1;
  12. bool motorDireita = 0;
  13. int Lin1 = 4;
  14. int Lin2 = 3;
  15. int Rin1 = 6;
  16. int Rin2 = 5;
  17. int Mmode = 7;
  18.  
  19.  
  20. MPU6050 giro(Wire);
  21. //float ki,kd,kp,z,ang,0;
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   Wire.begin();
  26.   giro.begin();
  27.   giro.calcGyroOffsets(true);
  28.     pinMode(Rin1 ,OUTPUT);
  29.   pinMode(Rin2 ,OUTPUT);
  30.   pinMode(Lin1 ,OUTPUT);
  31.   pinMode(Lin2 ,OUTPUT);
  32.     pinMode(Mmode, INPUT_PULLUP);
  33. }
  34. void loop() {
  35.   // put your main code here, to run repeatedly:
  36.   //girar(1);
  37.   curva(180, 0);
  38.   frente();
  39.   delay(1700);
  40.   curva(180, 1);
  41. }
  42.  
  43. void parar(){
  44.    cruzada(1, 127);
  45.    cruzada(0, 127);
  46. }
  47. void re(){
  48.   cruzada(1, 0);
  49.   cruzada(0, 0);
  50. }
  51.  
  52. void frente(){
  53.     /*digitalWrite(3, 1);
  54.     digitalWrite(4, 0);
  55.     digitalWrite(5, 1);
  56.     digitalWrite(6, 0); */
  57.  
  58.     cruzada(1, 255);
  59.     cruzada(0, 255);
  60. }
  61.  
  62. void curva(int angulo, int lado){
  63.   giro.update();
  64. if(lado == 0){
  65.   int  z = giro.getAngleZ();
  66.   int  ang = z + angulo*2;
  67.  
  68.   while(z < ang){
  69.    giro.update();
  70.   cruzada(1, 255);
  71.   cruzada(0, 0);
  72.   z = giro.getAngleZ();
  73.   }
  74.   }
  75.   if(lado == 1){
  76.   int  z = giro.getAngleZ();
  77.   int  ang = z - angulo*2;
  78.  
  79.   while(z > ang){
  80.    giro.update();
  81.   cruzada(1, 0);
  82.   cruzada(0, 255);
  83.   z = giro.getAngleZ();
  84.   }  
  85.   }
  86. }
  87.  
  88. void cruzada(bool motor, int velocidade){
  89.   if(motor == 1){
  90.     if(velocidade == 127){
  91.       analogWrite(Rin2, 0);
  92.     }
  93.     if(velocidade > 127){
  94.       digitalWrite(Rin1, 1);
  95.       analogWrite(Rin2, map(velocidade, 128, 255, 0, 255));
  96.     }
  97.     if(velocidade < 127){
  98.       digitalWrite(Rin1, 0);
  99.       analogWrite(Rin2, map(velocidade, 126, 0, 0, 255));
  100.     }
  101.   }
  102.   if(motor == 0){
  103.     if(velocidade == 127){
  104.       analogWrite(Lin2, 0);
  105.     }
  106.     if(velocidade > 127){
  107.       digitalWrite(Lin1, 1);
  108.       analogWrite(Lin2, map(velocidade, 128, 255, 0, 255));
  109.     }
  110.     if(velocidade < 127){
  111.       digitalWrite(Lin1, 0);
  112.       analogWrite(Lin2, map(velocidade, 126, 0, 0, 255));
  113.     }
  114.   }
  115. }
Add Comment
Please, Sign In to add comment