Advertisement
Guest User

Pixycam Follower Code

a guest
Mar 28th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <PIDLoop.h>
  2. #include <Pixy2I2C.h>
  3.  
  4. // Variablen für die Distanzmessung
  5. float knownWidth = 12.0;      // Tatsächliche Breite des Objekts
  6. float focalLength = 60.0;     // Brennweite der PixyCam
  7. float allowedDistance = 10;   // Maximal zulässige Distanz zum Objekt
  8.  
  9. Pixy2I2C pixy;
  10. PIDLoop panLoop(180, 0, 180, true);  // Initialisierung des PID-Regelkreises für den Pan-Winkel
  11. PIDLoop tiltLoop(180, 0, 180, true); // Initialisierung des PID-Regelkreises für den Tilt-Winkel
  12.  
  13. typedef struct
  14. {
  15.   int motor_rightspeed;
  16.   int motor_leftspeed;
  17. } MS;
  18.  
  19. int OutputMspeedR = 6;
  20. int OutputMotorRa = 4;
  21. int OutputMotorRb = 5;
  22. int OutputMspeedL = 9;
  23. int OutputMotorLa = 8;
  24. int OutputMotorLb = 7;
  25.  
  26. void setup()
  27. {
  28.   // Motoren initialisieren
  29.   pinMode(OutputMspeedL, OUTPUT);
  30.   pinMode(OutputMspeedR, OUTPUT);
  31.   pinMode(OutputMotorLa, OUTPUT);
  32.   pinMode(OutputMotorRa, OUTPUT);
  33.   pinMode(OutputMotorLb, OUTPUT);
  34.   pinMode(OutputMotorRb, OUTPUT);
  35.   // Initialize motor direction to forward
  36.   setMotors("Vorne");
  37.   analogWrite(OutputMspeedR, 0); // Ensure motors are initially stopped
  38.   analogWrite(OutputMspeedL, 0); // Ensure motors are initially stopped
  39.   Serial.begin(9600);
  40.   Serial.print("Setted pins...\n");
  41.   pixy.init();                                   // Initialisierung der Pixy2
  42.   pixy.changeProg("color_connected_components"); // Änderung des Pixy2-Modus auf "color_connected_components"
  43. }
  44.  
  45. void loop()
  46. {
  47.   static int i = 0;
  48.   int32_t panOffset, tiltOffset;
  49.  
  50.   // Aktive Blöcke von Pixy2 erhalten
  51.   pixy.ccc.getBlocks();
  52.   // Wenn ein oder mehrere Blöcke erfast worden.
  53.   if (pixy.ccc.numBlocks)
  54.   {
  55.     i++;
  56.     panOffset = (int32_t)pixy.frameWidth / 2 - (int32_t)pixy.ccc.blocks[0].m_x;
  57.     tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight / 2;
  58.     tiltOffset = -tiltOffset;
  59.     int blockWidth = pixy.ccc.blocks[0].m_width;
  60.     int blocksHeight = pixy.ccc.blocks[0].m_height;
  61.  
  62.     float distance = (knownWidth * focalLength) / blockWidth;
  63.  
  64.     panLoop.update(panOffset);
  65.     tiltLoop.update(tiltOffset);
  66.  
  67.     pixy.setServos(panLoop.m_command, tiltLoop.m_command);
  68.  
  69.     if (distance < allowedDistance)
  70.     {
  71.       analogWrite(OutputMspeedR, 140);
  72.       analogWrite(OutputMspeedL, 140);
  73.       setMotors("Hinten");
  74.       return;
  75.     }
  76.     panLoop.update(panOffset);
  77.     tiltLoop.update(tiltOffset);
  78.     MS MotorSpeed = adjustMotorSpeed();
  79.  
  80.     // Set motor speeds
  81.     analogWrite(OutputMspeedL, MotorSpeed.motor_leftspeed);
  82.     analogWrite(OutputMspeedR, MotorSpeed.motor_rightspeed);
  83.     setMotors("Vorne");
  84.   }
  85.   else
  86.   {
  87.     analogWrite(OutputMspeedL, 0);
  88.     analogWrite(OutputMspeedR, 0);
  89.     pixy.setServos(panLoop.m_command, tiltLoop.m_command);
  90.   }
  91. }
  92.  
  93. MS adjustMotorSpeed()
  94. {
  95.   // Definition der Konstanten für die Pan/Tilt-Werte und Motorgeschwindigkeiten
  96.   const int minValue = 0;
  97.   const int maxValue = 1000;
  98.   const int minSpeed = 40;    // Minimale Motorgeschwindigkeit
  99.   const int maxSpeed = 180;   // Maximale Motorgeschwindigkeit
  100.  
  101.   // Aktuelle Befehle von den PID-Regelkreisen abrufen
  102.   int currentCommand = panLoop.m_command;
  103.   int currentTilt = tiltLoop.m_command;
  104.  
  105.   // Servos der Pixy2-Kamera mit den aktuellen Winkeln einstellen
  106.   int motor_rightspeed = map(currentCommand, minValue, maxValue, minSpeed, maxSpeed);
  107.   int motor_leftspeed = map(currentCommand, minValue, maxValue, maxSpeed, minSpeed);
  108.  
  109.   // Speichern und zurückgeben der berechneten Motorgeschwindigkeiten
  110.   MS speed;
  111.   setMotors("Vorne");
  112.  
  113.   Serial.print("Linke Geschwindigkeit: ");
  114.   Serial.println(motor_leftspeed);
  115.   Serial.print("Rechte Geschwindigkeit: ");
  116.   Serial.println(motor_rightspeed);
  117.  
  118.   speed.motor_rightspeed = motor_rightspeed;
  119.   speed.motor_leftspeed = motor_leftspeed;
  120.   return speed;
  121. }
  122.  
  123. void setMotors(const char *richtung)
  124. {
  125.   // Setze beide Motoren so dass sie nach Vorne fahren
  126.   if (strcmp(richtung, "Vorne") == 0)
  127.   {
  128.     digitalWrite(OutputMotorLa, HIGH);
  129.     digitalWrite(OutputMotorLb, LOW);
  130.     digitalWrite(OutputMotorRa, HIGH);
  131.     digitalWrite(OutputMotorRb, LOW);
  132.   }
  133.   // Setze beide Motoren so dass sie nach Hinten fahren
  134.   if (strcmp(richtung, "Hinten") == 0)
  135.   {
  136.     digitalWrite(OutputMotorLa, LOW);
  137.     digitalWrite(OutputMotorLb, HIGH);
  138.     digitalWrite(OutputMotorRa, LOW);
  139.     digitalWrite(OutputMotorRb, HIGH);
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement