<< return to Pixycam.com

Comparing the x-positions of the head and tail of a vector in line following

I am trying to make a line following robot using a Pixy2. The robot only has two DC motors on it so when we need to make a turn we just shut one of the motors off. Im not sure what im doing but for some reason my arduino isnt comparing x0 and x1 the way i thought it would. Can you help me see whats wrong?

#include <Pixy2.h>
#include <Pixy2Line.h>
#include <PIDLoop.h>

Pixy2 pixy;
const uint8_t xcenter = 39;
const int en1 = 3;
const int leftmotorpos = 2;
const int leftmotorneg = 4;
const int en2 = 5;
const int rightmotorneg = 7;
const int rightmotorpos = 6;
const int high = 80;
const int low = 0;

PIDLoop headingLoop(5000, 0, 0, false);

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(“Starting…”);
pixy.init();
pixy.setLamp(1, 1);
pixy.changeProg(“line_tracking”);

pinMode (en1, OUTPUT);
pinMode (en2, OUTPUT);

digitalWrite (leftmotorpos, HIGH);
digitalWrite (leftmotorneg, LOW);
digitalWrite(rightmotorpos, HIGH);
digitalWrite(rightmotorneg, LOW);

}

void loop() {
// put your main code here, to run repeatedly:
//uint8_t xtail = m_y0;
//uint8_t ytail = m_y0;
//uint8_t xhead=m_x1;
//uint8_t yhead= m_y1;
int32_t error;
int8_t x1 = pixy.line.vectors->m_x1;
int8_t x0 = pixy.line.vectors->m_x0;
int8_t y1 = pixy.line.vectors->m_y1;
int8_t y0 = pixy.line.vectors->m_y0;

int res = pixy.line.getMainFeatures(1);

// Serial.println (res);
if (res <= 0)
{
analogWrite(en1, 0);
analogWrite(en2, 0);
//Serial.println(res);
return;
}

if (res & LINE_VECTOR)
{
error = (int32_t)pixy.line.vectors->m_x1 - (int32_t) xcenter;
// pixy.line.vectors->print();
headingLoop.update(error);
Serial.println(x0,x1);
//delay(100);
// if (x1 = x0) {
// analogWrite(en1, 200);
// analogWrite(en2, 200);
// }
// else if (x1 > x0) {
//
// analogWrite(en1, 200);
// analogWrite(en2, 0);
// }
// else if (x1 < x0){
// analogWrite(en1, 0);
// analogWrite(en2, 200);
//
// }
//}
}}

Hello,
I can see your code, but bear in mind that we don’t know anything about your platform or much about what you are trying to accomplish. (Are you just trying to follow a line?) We don’t know how your platform is controlled from the Arduino, how many axes it has, or what axis does what. Perhaps some code comments would give us some hints.

Edward

One basic error I can see is that you’re using the assignment operator ( = ) instead of the comparison operator ( == )

More info: https://www.tutorialspoint.com/arduino/arduino_comparison_operators.htm

Specifically, this line:
// if (x1 = x0) {

Should read:
// if (x1 == x0) {

Cheers,
Jesse