VideoCapturecap(0);// Camera objectMatimg;// Image objectwhile(1){cap>>img;// allocate captured video to imageimshow("",img);// showing imageif(waitKey(1)=='q')break;}
Finding Contours of Ball
Scalarlower=Scalar(10,126,75);// lower boundary of orange ball color in HSV unitScalarupper=Scalar(25,255,255);// upper boundary of orange ball color in HSV unitvector<vector<Point>>contours;vector<Vec4i>hierarchy;MathsvImg;cvtColor(img,hsvImg,COLOR_BGR2HSV);// RGB to HSVinRange(hsvImg,lower,upper,hsvImg);//Convert HSVfindContours(hsvImg,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);// Finding Contoursimshow("",hsvImg);// Show only Orange Area
Finding Positions of ball
intX,Y;Momentsmoment;moment=moments(contours[0]);// Calculate center of momentX=static_cast<float>(moment.m10/moment.m00+1e-5);// allocate XY=static_cast<float>(moment.m01/moment.m00+1e-5);// allocate Y
Path of Ball Expection
Target : Expect path of ball positions. Position data can get by above process.
Method : The method of least square.
Language : I use c++ because of computation speed and compatibility with linear actuator.
voidLinearRegression(int*pX,int*pY,intarraySize){inti;floatmeanX=0,meanY=0;floatconstant=0,slope=0,div=0;for(i=0;i<arraySize;i++){meanX+=pX[i];meanY+=pY[i];}meanX/=arraySize;// mean of x positionsmeanY/=arraySize;// mean of y positionsfor(i=0;i<arraySize;i++){div+=(X[i]-meanX)*(X[i]-meanX);// varianceslope+=(X[i]-meanX)*(Y[i]-meanY);// variance}slope/=div;constant=meanY-meanX*slope;}
Robot Arm
Target : Making robot arm with linear actuator and arduino
Language : I use Arduino for Dynamixel, and I use C++ for linear actuator.
Dynamixel
#define NUM 00
#define BR 00
uint8_tmoter=NUM;uint16_tm=0;DynamixelWorkbenchdxl;voidsetup(){Serial.begin(57600);constchar*log;dxl.init("",BR,&log);// initalization of port rate dxl.ping(moter,&m,&log);// initalization of moter pingdxl.jointMode(moter,0,0,&log);// setting moter mode}voidinitalizePosition(){dxl.goalPosition(moter[0],(int32_t)2047);delay(50);dxl.goalPosition(moter[1],(int32_t)2371);delay(50);dxl.goalPosition(moter[2],(int32_t)2747);delay(50);dxl.goalPosition(moter[3],(int32_t)2047);delay(50);return;}voidswing(){dxl.goalPosition(moter[2],(int32_t)2047);dxl.goalPosition(moter[3],(int32_t)1647);delay(1000);initalizePosition();return;}
Initalized State
Swing State
Linear Actuator
#include"stddef.h"
#include"CLinear_actu.h"intmain(){CLinear_actulinear_ac=CLinear_actu();intr;while(1){VideoCapturecap(0);Matimg;vector<float>Xs,Ys;GetPosition(img,Xs,Ys);// from Convert Video to ImageLinearRegression(int*pX,int*pY,intarraySize)// from Path of Ball Expectionr=slope*-150+constant;r/=4;r=r-70;linear_ac.move(r);delay(r*r/10);linear_ac.move(-r);}}