Log in

View Full Version : Vision Code question.


thecakeisalie
19-03-2012, 18:18
Having finished writing the vision code, I tested our vision code. The only problem is that it did not seem to work. :(
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 :P

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;
}
}

frdrake
20-03-2012, 14:43
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?

mikets
20-03-2012, 15:37
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.

thecakeisalie
24-03-2012, 21:50
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 :)