tweaking tracker for improved performance

main
mwinter 1 year ago
parent 73b1420c3a
commit 6662dc687e

@ -22,7 +22,7 @@ def rectFromPoint(center, len, width, axis):
return rect
def rectsFromPoint(center, l1, l2, l3, w, axis):
def rectsFromPoint(center, l1, l2, l3, w, cOffset, axis):
centerFine = center
fineInner = rectFromPoint(centerFine, l1, w, axis)
if(axis == 'x'):
@ -36,25 +36,25 @@ def rectsFromPoint(center, l1, l2, l3, w, axis):
centerCoarse = center
if(axis == 'x'):
centerCoarse = (center[0], center[1] + w)
centerCoarse = (center[0], center[1] + cOffset)
elif(axis == 'y'):
centerCoarse = (center[0] + w, center[1])
centerCoarse = (center[0] + cOffset, center[1])
coarse = rectFromPoint(centerCoarse, l3, w, axis)
return [fineInnerNeg, fineInnerPos, fineInner, fineOuter, coarse, center]
def moveROI(event, x, y, flags, params):
global roiX, roiY, moving, l1, l2, l3, w, selectedAxis
global roiX, roiY, moving, l1, l2, l3, w, cOffset, selectedAxis
if event == cv2.EVENT_LBUTTONDOWN:
moving = True
elif event==cv2.EVENT_MOUSEMOVE:
if moving==True:
if(selectedAxis == 'x'):
roiX = rectsFromPoint((x, y), l1, l2, l3, w, selectedAxis)
roiX = rectsFromPoint((x, y), l1, l2, l3, w, cOffset, selectedAxis)
elif(selectedAxis == 'y'):
roiY = rectsFromPoint((x, y), l1, l2, l3, w, selectedAxis)
roiY = rectsFromPoint((x, y), l1, l2, l3, w, cOffset, selectedAxis)
elif event == cv2.EVENT_LBUTTONUP:
moving = False
@ -118,7 +118,7 @@ def drawRoi(frame, roi):
cv2.line(frame, (center[0], center[1] - 5), (center[0], center[1] + 5), (0, 255, 0), 1)
def picameraToCVTrack():
global roiX, roiY, moving, l1, l2, l3, w, selectedAxis, dilationKernel, calibrate, oscClient
global roiX, roiY, moving, l1, l2, l3, w, selectedAxis, dilationVal, dilationKernel, calibrate, oscClient
while True:
frame = picam2.capture_buffer("lores")
@ -148,14 +148,18 @@ def picameraToCVTrack():
if key == ord('+'):
dilationVal = dilationVal + 1
dilationKernel = genDKernel(dilationVal)
print(dilationVal)
elif key == ord('-'):
if dilationVal > 0:
dilationVal = dilationVal - 1
dilationKernel = genDKernel(dilationVal)
print(dilationVal)
elif key == ord('x'):
selectedAxis = 'x'
print(selectedAxis)
elif key == ord('y'):
selectedAxis = 'y'
print(selectedAxis)
elif key == ord('c'):
if calibrate:
calibrate = False
@ -220,12 +224,13 @@ moving = False
l1 = 100
l2 = 300
l3 = 40
w = 20
w = 10
cOffset = 23;
roiXCenter = (384, 20)
roiYCenter = (20, 384)
roiX = rectsFromPoint(roiXCenter, l1, l2, l3, w, 'x')
roiY = rectsFromPoint(roiYCenter, l1, l2, l3, w, 'y')
dilationVal = 75
roiX = rectsFromPoint(roiXCenter, l1, l2, l3, w, cOffset, 'x')
roiY = rectsFromPoint(roiYCenter, l1, l2, l3, w, cOffset, 'y')
dilationVal = 70
dilationKernel = genDKernel(dilationVal)
calibrate = True

@ -4,7 +4,7 @@
var imageDist, micronsPerStep, automation, imgPositions, curPos, tarPos,
netAddress, serialPort, serialListener,
moveTo, jogControl, jogHorizontal, jogVertical,
imgSelect, imgCalibrate, automate, lastSelect, trackerPos;
imgSelect, imgCalibrate, automate, lastSelect, trackerPos, trackerOffsetBaseDist, trackerOffset;
// init global vars
imageDist = 300; // in microns
@ -14,7 +14,23 @@ imgPositions = 9.collect({nil});
curPos = Point.new(0, 0);
tarPos = Point.new(0, 0);
netAddress = NetAddr.new("127.0.0.1", 8080);
lastSelect = 0;
lastSelect = -1;
trackerOffsetBaseDist = 16000;
trackerOffset = [
[trackerOffsetBaseDist * 1, trackerOffsetBaseDist * 1],
[trackerOffsetBaseDist * 0, trackerOffsetBaseDist * 1],
[trackerOffsetBaseDist * -1, trackerOffsetBaseDist * 1],
[trackerOffsetBaseDist * 1, trackerOffsetBaseDist * 0],
[trackerOffsetBaseDist * 0, trackerOffsetBaseDist * 0],
[trackerOffsetBaseDist * -1, trackerOffsetBaseDist * 0],
[trackerOffsetBaseDist * 1, trackerOffsetBaseDist * -1],
[trackerOffsetBaseDist * 0, trackerOffsetBaseDist * -1],
[trackerOffsetBaseDist * -1, trackerOffsetBaseDist * -1]
];
~serialPort = SerialPort("/dev/ttyACM0", baudrate: 115200, crtscts: true);
// recieve motor feedback
@ -121,7 +137,7 @@ imgSelect = {
lastSelect = imgIndex;
}, {
lastSelect.postln;
if(msg[1] == ((lastSelect + 1) * -1), {"here".postln; lastSelect = 0})
if(msg[1] == ((lastSelect + 1) * -1), {"here".postln; lastSelect = -1})
//lastSelect = 0;
/*
imgIndex = msg[1].neg - 1;
@ -136,8 +152,17 @@ imgSelect = {
imgCalibrate = {
var calibrateHold, imgIndex, setPos;
calibrateHold = Routine({
var imageCalibrationIndex = 0;
20.do({0.1.wait});
imgPositions[imgIndex] = setPos.deepCopy;
//imgPositions[imgIndex] = setPos.deepCopy;
[-1, 0, 1].do({arg row;
[-1, 0, 1].do({arg col;
imgPositions[imageCalibrationIndex] = setPos.deepCopy + Point.new(6550 * col, 6550 * row);
imgPositions[imageCalibrationIndex].postln;
imageCalibrationIndex = imageCalibrationIndex + 1;
});
});
imgPositions.postln;
netAddress.sendMsg("/STATE/SET", "{message: \"image calibrated\"}");
});
@ -167,17 +192,17 @@ automate = OSCFunc({arg msg;
trackerPos = OSCFunc({arg msg;
//imgPositions.postln[lastSelect.postln];
if(lastSelect.postln > 0, {
if(lastSelect.postln > -1, {
var distX, distY, isFocusing;
distX = curPos.x - imgPositions[lastSelect].x;
distY = curPos.y - imgPositions[lastSelect].y;
isFocusing = false;
if((imgPositions[lastSelect] != nil) && (distX.abs < 500) && (msg[1].abs > 8000), {
tarPos.x = curPos.x + (10 * msg[1].sign);// * -1);
if((imgPositions[lastSelect] != nil) && (distX.abs < 300) && ((msg[1] - trackerOffset[lastSelect][0]).abs > 3000), {
tarPos.x = curPos.x + (10 * (msg[1] - trackerOffset[lastSelect][0]).sign);// * -1);
isFocusing = true;
});
if((imgPositions[lastSelect] != nil) && (distY.abs < 500) && (msg[2].abs > 8000), {
tarPos.y = curPos.y + (10 * msg[2].sign);// * -1);
if((imgPositions[lastSelect] != nil) && (distY.abs < 300) && ((msg[2] - trackerOffset[lastSelect][1]).abs > 3000), {
tarPos.y = curPos.y + (10 * (msg[2] - trackerOffset[lastSelect][1]).sign);// * -1);
isFocusing = true;
});
if(isFocusing, {moveTo.value(tarPos)});

Loading…
Cancel
Save