PixyCam Line Following error with DC Motors

I am having an issue running the pixyCam line following program. I have written code which changes wheel speeds when vector is left/right of center which executes. When DC Motors are not powered on, the program executes and prints direction it needs to navigate: as shown below

motors off

When DC Motors are on, the program executes only if left/right adjustment is needed. When it needs to go straight I get a checksum and no response error: as pictured below.

error

Hardware:
Arduino Mega v3
Arduino Motor Shield v2.3
Pololu 131:1 Metal Gearmotor w encoder
pixy cam v2

Code:

#include <Pixy2.h>
#include <PIDLoop.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

// Initializing Motor Shield, Motor speeds, maximum allowed is 250
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *leftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(2);

// Initialization of PixyCam
#define X_CENTER         (pixy.frameWidth/2)
Pixy2 pixy;

// Creating PID Loop
PIDLoop headingLoop(5000, 0, 0, false);

void setup()
{
  Serial.begin(115200);
  Serial.print("Starting...\n");
  AFMS.begin();
  leftMotor -> run(RELEASE);
  leftMotor -> setSpeed(0);
  rightMotor -> run(RELEASE);

  pixy.init();
  pixy.setLamp(1, 1);
  pixy.changeProg("line");

  // look straight and down
  pixy.setServos(500, 1000);
}
void loop()
{
  int16_t res;
  int32_t error;
  int left, right;
  char buf[96];
  // Get latest data from Pixy, including main vector, new intersections and new barcodes.
  res = pixy.line.getMainFeatures();
  if (res <= 0) {
    left = 0;
    right = 0;
    Serial.print("stop ");
  } else if (pixy.line.vectors->m_x1 > X_CENTER ) {
      left = 50;
      right = 25;
      Serial.println("RIGHT");
  } else if (pixy.line.vectors->m_x1 < X_CENTER) {
      left = 25;
      right = 50;
      Serial.println("LEFT");
  } else {
      left = 50;
      right = 50;
      Serial.println("Straight");
  }
  leftMotor -> run(FORWARD);
  leftMotor -> setSpeed(left);
  rightMotor -> run(FORWARD);
  rightMotor -> setSpeed(right);
}

What is the power source? A schematic would be welcome.

Could it be that if both motors turn on, the power supply can't supply enough current and the voltage drops off? But one motor it is OK? What do you see if you monitor the motor supply voltage with your DMM?

I don’t believe this is the issue. If I remove the segments of code which set the motor speed to 0 if there is an error, the motors run and the line is followed.

The issue then become that’s the robot doesn’t stop if no line is detected.

The power source is a Jameco LiPo 12V 4500mAh rechargeable battery. I will work on adding a schematic to the original post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.