Vision Code question.

Having finished writing the vision code, I tested our vision code. The only problem is that it did not seem to work. :frowning:
if you can pinpoint our problem, it would be very helpful. This is my first year coding camera code in c++, so… i expect many things to go wrong :stuck_out_tongue:

the vision.h:

#ifndef VISION_H
#define VISION_H
#include "WPILib.h"

class Vision {
private:
	AxisCamera * camera;

public:
	Vision();
	int Height();
	int Center_Mass();
	double GetDistance(int h);
};

#endif

the vision.cpp:

#include "../Robotmap.h"
#include "Vision.h"
#include <math.h>
#define PI 3.14159265


Vision::Vision() {
camera ->GetInstance("10.02.93.15");
}
int Vision::Center_Mass()
{
	ParticleFilterCriteria2 criteria] = {
						{IMAQ_MT_BOUNDING_RECT_WIDTH, 30, 400, false, false},
						{IMAQ_MT_BOUNDING_RECT_HEIGHT, 40, 400, false, false}
				};
			ColorImage *image = camera->GetImage();
			Threshold threshold(10, 255, 200, 255, 150, 255);
			BinaryImage *thresholdImage = image->ThresholdRGB(threshold);
			BinaryImage *bigObjectsImage = thresholdImage->RemoveSmallObjects(false, 2);  // remove small objects (noise)
			BinaryImage *convexHullImage = bigObjectsImage->ConvexHull(false);  // fill in partial and full rectangles
			BinaryImage *filteredImage = convexHullImage->ParticleFilter(criteria, 2);  // find the rectangles
			vector<ParticleAnalysisReport> *reports = filteredImage->GetOrderedParticleAnalysisReports();  // get the results				

			ParticleAnalysisReport *r = &(reports->at(1));
			int center_mass_x =  r->center_mass_x; // int, in pixels

			
			delete reports;
			delete filteredImage;
			delete convexHullImage;
			delete bigObjectsImage;
			delete thresholdImage;
			delete image;
			
			return center_mass_x;
}

and the aiming part:


	while (vision->Center_Mass() <159 || vision->Center_Mass() > 161){
		while (vision->Center_Mass() < 159){
			turret->DriveMotor(0.1);
			if (vision->Center_Mass() > 159) break;
		}
		while (vision->Center_Mass() > 161){
			turret->DriveMotor(-0.1);
			if (vision->Center_Mass() < 161) break;
		}
	}

There’s obviously some parts missing (declarations for your targeting code like vision and center mass, so this is assuming those are right). Have you verified yet that the center of mass X you’re finding in the image processing is the same as the value being passed into your targeting code?

Also note that the first particle in the list is particle 0 not particle 1. Unless you have a reason to skip particle 0, that could be one of the bugs. In addition, you should not blindly access the particle array within checking the particles->size(). It is possible that the vision code found no target. In that case, your code will fault.

Thanks for the suggestions. I have heavily modified our vision code so that it should work now. Unfortunately, due to the lack of time plus the fact that it seems that we are capable of aiming the camera quite well with just the stream on the driverstation, we may not need to use the targeting code at all this year. However, it was great practice for next year :slight_smile: