步骤1:硬件
这是我以前用来构建的这个。我使用来自phidgets.com的部件以及我躺在房子周围的东西100%构建它。
phidgets boards,motors,hardware
hub0000 - vint hub phidget
1108 - 磁传感器
2x stc1001 - 2.5a stepper phidget
2x 3324 - 42sth38 nema-17双极无齿轮步进器
3x 3002 - phidget电缆60厘米
3403 - usb2.0 4端口集线器
3031 - 母尾5.5x2.1mm
3029 - 2线100‘绞线
3604 - 10mm白色led(10个袋子)
3402 - usb网络摄像头
其他部件
24vdc 2.0a电源
车库废木和金属
拉链
底部切断的塑料容器
第2步:设计机器人
我们需要设计一些可以从输入料斗中取出一个珠子,将其放在网络摄像头下,然后将其移动到适当的箱子中的东西。
珠子皮卡
我决定用2片圆形胶合板做第1部分,每个胶合板在同一个地方钻一个洞。底部件固定,顶部件连接到步进电机,步进电机可以在装有珠子的料斗下方旋转。当孔在料斗下移动时,它会拾取一个珠子。然后我可以在网络摄像头下旋转它,然后进一步旋转,直到它与底部的孔匹配,此时它就会落下。
在这张图片中,我正在测试系统能行得通。一切都是固定的,除了顶部的圆形胶合板,它与下面的步进电机相连。网络摄像头尚未安装。我现在只是使用phidget控制面板转向电机。
珠子存储
下一部分是设计用于保持每种颜色的bin系统。我决定使用下面的第二个步进电机来支撑和旋转具有均匀间隔隔间的圆形容器。这可以用来旋转珠子将从其中掉出的孔下方的正确隔间。
我使用纸板和胶带来构建它。这里最重要的是一致性 - 每个隔间应该是相同的尺寸,整个东西应该均匀加重,以便旋转而不会跳过。
通过紧密贴合的盖子完成珠子的移除一次只有一个隔间,所以可以倒出珠子。
摄像头
网络摄像头安装在料斗和下板孔位置之间的顶板上。这允许系统在掉落之前观察珠子。 led用于照亮相机下方的珠子,并且环境光被阻挡,以提供一致的照明环境。这对于准确的颜色检测非常重要,因为环境光可以真正地消除感知颜色。
位置检测
系统能够检测到珠子的旋转非常重要分隔器。这用于在启动时设置初始位置,但也用于检测步进电机是否已经不同步。在我的系统中,一个珠子有时会在拾取时卡住,系统需要能够检测并处理这种情况 - 通过备份和尝试agian。
有很多方法可以处理这个(事情。我决定使用1108磁传感器,在顶板的边缘嵌入磁铁。这允许我验证每次旋转的位置。一个更好的解决方案可能是步进电机上的编码器,但我有一个1108左右,所以我使用它。
完成机器人
此时,一切都已经完成出来,并测试。现在是时候安装好所有东西并转向编写软件。
2个步进电机由stc1001步进控制器驱动。 hub000-usb vint集线器用于运行步进控制器,以及读取磁传感器和驱动led。网络摄像头和hub0000都连接到一个小型usb集线器。使用3031引线和一些电线以及24v电源为电机供电。
步骤3:编写代码
c#和visual studio 2015用于此项目。下载本页顶部的源代码并按照步骤进行操作 - 主要部分概述如下
初始化
首先,我们必须创建,打开和初始化phidget对象。这是在表单加载事件和phidget附加处理程序中完成的。
private void form1_load(object sender, eventargs e) {
/* initialize and open phidgets */
top.hubport = 0;
top.attach += top_attach;
top.detach += top_detach;
top.positionchange += top_positionchange;
top.open();
bottom.hubport = 1;
bottom.attach += bottom_attach;
bottom.detach += bottom_detach;
bottom.positionchange += bottom_positionchange;
bottom.open();
magsensor.hubport = 2;
magsensor.ishubportdevice = true;
magsensor.attach += magsensor_attach;
magsensor.detach += magsensor_detach;
magsensor.sensorchange += magsensor_sensorchange;
magsensor.open();
led.hubport = 5;
led.ishubportdevice = true;
led.channel = 0;
led.attach += led_attach;
led.detach += led_detach;
led.open();
}
private void led_attach(object sender, phidget22.events.attacheventargs e) {
ledattachedchk.checked = true;
led.state = true;
ledchk.checked = true;
}
private void magsensor_attach(object sender, phidget22.events.attacheventargs e) {
magsensorattachedchk.checked = true;
magsensor.sensortype = voltageratiosensortype.pn_1108;
magsensor.datainterval = 16;
}
private void bottom_attach(object sender, phidget22.events.attacheventargs e) {
bottomattachedchk.checked = true;
bottom.currentlimit = bottomcurrentlimit;
bottom.engaged = true;
bottom.velocitylimit = bottomvelocitylimit;
bottom.acceleration = bottomaccel;
bottom.datainterval = 100;
}
private void top_attach(object sender, phidget22.events.attacheventargs e) {
topattachedchk.checked = true;
top.currentlimit = topcurrentlimit;
top.engaged = true;
top.rescalefactor = -1;
top.velocitylimit = -topvelocitylimit;
top.acceleration = -topaccel;
top.datainterval = 100;
}
我们还在初始化期间读取任何已保存的颜色信息,因此可以继续之前的运行。
电机定位
电机处理代码包括用于移动电机的便利功能。我使用的电机每转3,200 1/16步,所以我为此创建了一个常量。
对于顶部电机,我们希望能够将3个位置发送到电机:网络摄像头,孔和定位磁铁。有一个功能可以到达每个位置:
private void nextmagnet(boolean wait = false) {
double posn = top.position % stepsperrev;
top.targetposition += (stepsperrev - posn);
if (wait)
while (top.ismoving)
thread.sleep(50);
}
private void nextcamera(boolean wait = false) {
double posn = top.position % stepsperrev;
if (posn 《 properties.settings.default.cameraoffset)
top.targetposition += (properties.settings.default.cameraoffset - posn);
else
top.targetposition += ((properties.settings.default.cameraoffset - posn) + stepsperrev);
if (wait)
while (top.ismoving)
thread.sleep(50);
}
private void nexthole(boolean wait = false) {
double posn = top.position % stepsperrev;
if (posn 《 properties.settings.default.holeoffset)
top.targetposition += (properties.settings.default.holeoffset - posn);
else
top.targetposition += ((properties.settings.default.holeoffset - posn) + stepsperrev);
if (wait)
while (top.ismoving)
thread.sleep(50);
}
在开始运行之前,使用磁传感器对齐顶板。可以随时调用alignmotor函数来对齐顶板。该功能首先快速将板转动至1转,直至其磁铁数据高于阈值。然后它稍微后退一点并慢慢向前移动,捕捉传感器数据。最后,它将位置设置为最大磁铁数据位置,并将位置偏移重置为0.因此,最大磁铁位置应始终为(top.position%stepsperrev)
thread alignmotorthread;
boolean sawmagnet;
double magsensormax = 0;
private void alignmotor() {
//find the magnet
top.datainterval = top.mindatainterval;
sawmagnet = false;
magsensor.sensorchange += magsensorstopmotor;
top.velocitylimit = -1000;
int trycount = 0;
tryagain:
top.targetposition += stepsperrev;
while (top.ismoving && !sawmagnet)
thread.sleep(25);
if (!sawmagnet) {
if (trycount 》 3) {
console.writeline(“align failed”);
top.engaged = false;
bottom.engaged = false;
runtest = false;
return;
}
trycount++;
console.writeline(“are we stuck? trying a backup.。.”);
top.targetposition -= 600;
while (top.ismoving)
thread.sleep(100);
goto tryagain;
}
top.velocitylimit = -100;
magdata = new list》();
magsensor.sensorchange += magsensorcollectpositiondata;
top.targetposition += 300;
while (top.ismoving)
thread.sleep(100);
magsensor.sensorchange -= magsensorcollectpositiondata;
top.velocitylimit = -topvelocitylimit;
keyvaluepair max = magdata[0];
foreach (keyvaluepair pair in magdata)
if (pair.value 》 max.value)
max = pair;
top.addpositionoffset(-max.key);
magsensormax = max.value;
top.targetposition = 0;
while (top.ismoving)
thread.sleep(100);
console.writeline(“align succeeded”);
}
list》 magdata;
private void magsensorcollectpositiondata(object sender, phidget22.events.voltageratioinputsensorchangeeventargs e) {
magdata.add(new keyvaluepair(top.position, e.sensorvalue));
}
private void magsensorstopmotor(object sender, phidget22.events.voltageratioinputsensorchangeeventargs e) {
if (top.ismoving && e.sensorvalue 》 5) {
top.targetposition = top.position - 300;
magsensor.sensorchange -= magsensorstopmotor;
sawmagnet = true;
}
}
最后,通过将底部马达发送到其中一个胎圈容器位置来控制底部马达。对于这个项目,我们有19个职位。算法选择最短路径,顺时针或逆时针转动。
private int bottomposition {
get {
int posn = (int)bottom.position % stepsperrev;
if (posn 《 0)
posn += stepsperrev;
return (int)math.round(((posn * beadcompartments) / (double)stepsperrev));
}
}
private void setbottomposition(int posn, bool wait = false) {
posn = posn % beadcompartments;
double targetposn = (posn * stepsperrev) / beadcompartments;
double currentposn = bottom.position % stepsperrev;
double posndiff = targetposn - currentposn;
// keep it as full steps
posndiff = ((int)(posndiff / 16)) * 16;
if (posndiff 《= 1600)
bottom.targetposition += posndiff;
else
bottom.targetposition -= (stepsperrev - posndiff);
if (wait)
while (bottom.ismoving)
thread.sleep(50);
}
摄像机
opencv用于从网络摄像头读取图像。在启动主排序线程之前启动相机线程。该线程不断读入图像,使用mean计算特定区域的平均颜色并更新全局颜色变量。该线程还使用houghcircles来检测珠子或顶板上的孔,以细化它正在寻找颜色检测的区域。阈值和houghcircles数字是通过反复试验确定的,并且在很大程度上取决于网络摄像头,光照和间距。
bool runvideo = true;
bool videorunning = false;
videocapture capture;
thread cvthread;
color detectedcolor;
boolean detecting = false;
int detectcnt = 0;
private void cvthreadfunction() {
videorunning = false;
capture = new videocapture(selectedcamera);
using (window window = new window(“capture”)) {
mat image = new mat();
mat image2 = new mat();
while (runvideo) {
capture.read(image);
if (image.empty())
break;
if (detecting)
detectcnt++;
else
detectcnt = 0;
if (detecting || circledetectchecked || showdetectionimgchecked) {
cv2.cvtcolor(image, image2, colorconversioncodes.bgr2gray);
mat thres = image2.threshold((double)properties.settings.default.videothresh, 255, thresholdtypes.binary);
thres = thres.gaussianblur(new opencvsharp.size(9, 9), 10);
if (showdetectionimgchecked)
image = thres;
if (detecting || circledetectchecked) {
circlesegment[] bead = thres.houghcircles(houghmethods.gradient, 2, /*thres.rows/4*/ 20, 200, 100, 20, 65);
if (bead.length 》= 1) {
image.circle(bead[0].center, 3, new scalar(0, 100, 0), -1);
image.circle(bead[0].center, (int)bead[0].radius, new scalar(0, 0, 255), 3);
if (bead[0].radius 》= 55) {
properties.settings.default.x = (decimal)bead[0].center.x + (decimal)(bead[0].radius / 2);
properties.settings.default.y = (decimal)bead[0].center.y - (decimal)(bead[0].radius / 2);
} else {
properties.settings.default.x = (decimal)bead[0].center.x + (decimal)(bead[0].radius);
properties.settings.default.y = (decimal)bead[0].center.y - (decimal)(bead[0].radius);
}
properties.settings.default.size = 15;
properties.settings.default.height = 15;
} else {
circlesegment[] circles = thres.houghcircles(houghmethods.gradient, 2, /*thres.rows/4*/ 5, 200, 100, 60, 180);
if (circles.length 》 1) {
list xs = circles.select(c =》 c.center.x).tolist();
xs.sort();
list ys = circles.select(c =》 c.center.y).tolist();
ys.sort();
int medianx = (int)xs[xs.count / 2];
int mediany = (int)ys[ys.count / 2];
if (medianx 》 image.width - 15)
medianx = image.width - 15;
if (mediany 》 image.height - 15)
mediany = image.height - 15;
image.circle(medianx, mediany, 100, new scalar(0, 0, 150), 3);
if (detecting) {
properties.settings.default.x = medianx - 7;
properties.settings.default.y = mediany - 7;
properties.settings.default.size = 15;
properties.settings.default.height = 15;
}
}
}
}
}
rect r = new rect((int)properties.settings.default.x,
(int)properties.settings.default.y,
(int)properties.settings.default.size,
(int)properties.settings.default.height);
mat beadsample = new mat(image, r);
scalar avgcolor = cv2.mean(beadsample);
detectedcolor = color.fromargb((int)avgcolor[2], (int)avgcolor[1], (int)avgcolor[0]);
image.rectangle(r, new scalar(0, 150, 0));
window.showimage(image);
cv2.waitkey(1);
videorunning = true;
}
videorunning = false;
}
}
private void camerastartbtn_click(object sender, eventargs e) {
if (camerastartbtn.text == “start”) {
cvthread = new thread(new threadstart(cvthreadfunction));
runvideo = true;
cvthread.start();
camerastartbtn.text = “stop”;
while (!videorunning)
thread.sleep(100);
updatecolortimer.start();
} else {
runvideo = false;
cvthread.join();
camerastartbtn.text = “start”;
}
}
颜色
现在,我们可以确定珠子的颜色,并根据该颜色决定将其放入哪个容器。
这个步骤依赖于颜色比较。我们希望能够区分颜色以限制误报,但也允许足够的阈值来限制假阴性。比较颜色实际上是非常复杂的,因为计算机将颜色存储为rgb的方式,以及人类感知颜色的方式并不是线性相关的。更糟糕的是,还必须考虑在下面观察颜色的光的颜色。
有复杂的计算色差的算法。我们使用cie2000,如果2种颜色与人类无法区分,则输出接近1的数字。我们使用colormine c#库来完成这些复杂的计算。已发现deltae值为5可在假阳性和假阴性之间提供良好的折衷。
由于容器通常有更多颜色,因此最后一个位置保留为捕获箱。我通常将这些设置为第二次通过机器。
list
colors = new list
();
list
colorpanels = new list
();
list colorstxts = new list();
list colorcnts = new list();
const int numcolorspots = 18;
const int unknowncolorindex = 18;
int findcolorposition(color c) {
console.writeline(“finding color.。.”);
var crgb = new rgb();
crgb.r = c.r;
crgb.g = c.g;
crgb.b = c.b;
int bestmatch = -1;
double matchdelta = 100;
for (int i = 0; i 《 colors.count; i++) {
var rgb = new rgb();
rgb.r = colors[i].r;
rgb.g = colors[i].g;
rgb.b = colors[i].b;
double delta = crgb.compare(rgb, new ciede2000comparison());
//double delta = deltae(c, colors[i]);
console.writeline(“deltae (” + i.tostring() + “): ” + delta.tostring());
if (delta 《 matchdelta) {
matchdelta = delta;
bestmatch = i;
}
}
if (matchdelta 《 5) {
console.writeline(“found! (posn: ” + bestmatch + “ delta: ” + matchdelta + “)”);
return bestmatch;
}
if (colors.count 《 numcolorspots) {
console.writeline(“new color!”);
colors.add(c);
this.begininvoke(new action(setbackcolor), new object[] { colors.count - 1 });
writeoutcolors();
return (colors.count - 1);
} else {
console.writeline(“unknown color!”);
return unknowncolorindex;
}
}
排序逻辑
排序功能将所有部分汇集在一起以实际排序珠子。该函数在专用线程中运行;移动顶板,检测珠子颜色,将其放入垃圾箱,确保顶板保持对齐,计数珠子等。当垃圾箱变满时,它也会停止运行 - 否则我们最终会溢出珠子。
thread colourtestthread;
boolean runtest = false;
void colourtest() {
if (!top.engaged)
top.engaged = true;
if (!bottom.engaged)
bottom.engaged = true;
while (runtest) {
nextmagnet(true);
thread.sleep(100);
try {
if (magsensor.sensorvalue 《 (magsensormax - 4))
alignmotor();
} catch { alignmotor(); }
nextcamera(true);
detecting = true;
while (detectcnt 《 5)
thread.sleep(25);
console.writeline(“detect count: ” + detectcnt);
detecting = false;
color c = detectedcolor;
this.begininvoke(new action
(setcolordet), new object[] { c });
int i = findcolorposition(c);
setbottomposition(i, true);
nexthole(true);
colorcnts[i]++;
this.begininvoke(new action(setcolortxt), new object[] { i });
thread.sleep(250);
if (colorcnts[unknowncolorindex] 》 500) {
top.engaged = false;
bottom.engaged = false;
runtest = false;
this.begininvoke(new action(setgogreen), null);
return;
}
}
}
private void colourtestbtn_click(object sender, eventargs e) {
if (colourtestthread == null || !colourtestthread.isalive) {
colourtestthread = new thread(new threadstart(colourtest));
runtest = true;
colourtestthread.start();
colourtestbtn.text = “stop”;
colourtestbtn.backcolor = color.red;
} else {
runtest = false;
colourtestbtn.text = “go”;
colourtestbtn.backcolor = color.green;
}
}
此时,我们有一个工作程序。一些代码被遗漏在文章之外,所以看一下实际运行它的源代码。
鸿蒙与中兴新支点操作系统是否有冲突
蓄电池生产工艺之铅粉机
iPhone6SPlus被改装成无边框iPhone Mix,no zuo no die
河道水位监测系统的特点是什么
AI和5G需求高算力芯片 中国芯片企业如何把握机会
颜色分类机器人的制作
基于STM32开发板和思岚A1M8激光雷达制作地图扫描仪
使用MATLAB进行异常检测(上)
苹果3nm订单将助台积电Q3营收增长11%
AMD在CES 2023开幕主题演讲中强调高性能和自适应计算的未来
一篇文章带你了解什么是氮氧化物检测仪
创维布局大屏AIoT,推出智能电视S系列新品
华为在法国设立传感器和软件研发中心,将持续对法国创新领域进行投资
三大运营商运营数据半年报解析
具有直通功能的4开关降压-升压控制器可消除开关噪声
商业银行智能语音识别技术应用与发展
国内顶级科技企业力挺华为鸿蒙 却迟迟不见马云阿里系的身影
S7-1200和调试助手进行数据通信
佳维视15.6寸工业平板电脑在包装自动检测设备的应用
光调制散射器测量技术工作原理