Spindle Cam
02 Dec 2015 03:26 - 02 Dec 2015 03:29 #66196
by AnthonyT
Spindle Cam was created by AnthonyT
I have made a camera that attaches to my TAIG CNC micro mill spindle so that I can accurately locate an XY home point. I used a cheap webcam and a 3D printed housing.
I also wrote a Python script using OpenCV that allows for finding the center of rotation.
You can see it here: www.thingiverse.com/thing:1167516
This is the user interface showing centering setup:
I also wrote a Python script using OpenCV that allows for finding the center of rotation.
You can see it here: www.thingiverse.com/thing:1167516
This is the user interface showing centering setup:
Last edit: 02 Dec 2015 03:29 by AnthonyT.
Please Log in or Create an account to join the conversation.
05 Dec 2015 05:12 #66435
by cmorley
Replied by cmorley on topic Spindle Cam
that looks great. What distribution of linux are you using? Wheezy?
Please Log in or Create an account to join the conversation.
05 Dec 2015 12:43 #66451
by AnthonyT
Thank you
I installed LinuxCNC 2.6 (Wheezy) from here a few weeks ago.
I also tested this on Debian Jessie.
To test that the camera even works on the computer, I am using "Camorama" (available from the distribution). I could not get "Cheese" to recognize the camera on one computer, but it did on another computer.
Some people may have to change the camera number in my python script from
cap = cv2.VideoCapture(0)
to
cap = cv2.VideoCapture(1)
Older computers may run slow while the webcam (and my script) are running, it should be stopped when you are finished homing XY.
Replied by AnthonyT on topic Spindle Cam
that looks great. What distribution of linux are you using? Wheezy?
Thank you
I installed LinuxCNC 2.6 (Wheezy) from here a few weeks ago.
I also tested this on Debian Jessie.
To test that the camera even works on the computer, I am using "Camorama" (available from the distribution). I could not get "Cheese" to recognize the camera on one computer, but it did on another computer.
Some people may have to change the camera number in my python script from
cap = cv2.VideoCapture(0)
to
cap = cv2.VideoCapture(1)
Older computers may run slow while the webcam (and my script) are running, it should be stopped when you are finished homing XY.
Please Log in or Create an account to join the conversation.
07 Dec 2015 08:47 #66546
by cgc
Replied by cgc on topic Spindle Cam
Hi,
I have played a little bit with your code.
You can start it directly from Linuxcnc with "open file"
In "Auto"mode, it will find a circle.
In "Manu"mode, you can set the cross with the right mouse button, then hold the button and change the diameter.
With cursor you can fine change the position and with PageUp and PageDown you can fine change the diameter.
With the "OK" Button, it will create a small cnc programm witch will move the maschine with the tool center to the selected camera position.
Please edit before the position offsets camera-center to tool-center and the pixelsize of the camera and optics.
regards Frank
I have played a little bit with your code.
You can start it directly from Linuxcnc with "open file"
In "Auto"mode, it will find a circle.
In "Manu"mode, you can set the cross with the right mouse button, then hold the button and change the diameter.
With cursor you can fine change the position and with PageUp and PageDown you can fine change the diameter.
With the "OK" Button, it will create a small cnc programm witch will move the maschine with the tool center to the selected camera position.
Please edit before the position offsets camera-center to tool-center and the pixelsize of the camera and optics.
Warning: Spoiler!
#!/usr/bin/python
from Tkinter import *
import cv2
import sys
import getopt
import Image, ImageTk
import numpy as np #Needed for HoughCircles circle detection
args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
args = dict(args)
if len(sources) == 0:
sources = [ 0 ]
root = Tk()
#EDIT: mm/pixel
pixelsize = 6.6 / 184
#EDIT: offset center camera to center tool
Xoffset = 100
Yoffset = 10
width = 640
height = 480
radius = 50
scale = pixelsize * 2
AutoMode = 1
ManuMode = 0
X=width/2
Y=height/2
oldY = 0
cap = cv2.VideoCapture(0)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height)
_, frame = cap.read()
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
w=Label(root)
w.pack()
def LeftKey(event):
global X
X-=1
if X<0:
X=0
def RightKey(event):
global X
X+=1
if X>(width-1):
X=width-1
def UpKey(event):
global Y
Y-=1
if Y<0:
Y=0
def DownKey(event):
global Y
Y+=1
if Y>(height-1):
Y=height-1
def PriorKey(event):
global radius
radius+=1
if radius>((height/2)-1):
radius=(height/2)-1
def NextKey(event):
global radius
radius-=1
if radius<5:
radius=5
def MouseKlick(event):
global X,Y
X=event.x-1
Y=event.y-1
def MouseHold(event):
global radius,oldY
radius+=(oldY-event.y)
if radius<5:
radius=5
if radius>((height/2)-1):
radius=(height/2)-1
oldY=event.y
def ModeAuto():
global AutoMode, ManuMode
AutoMode = 1
ManuMode = 0
def ModeManu():
global AutoMode, ManuMode
AutoMode = 0
ManuMode = 1
root.bind("<Left>", LeftKey)
root.bind("<Right>", RightKey)
root.bind("<Up>", UpKey)
root.bind("<Down>", DownKey)
root.bind("<Prior>", PriorKey)
root.bind("<Next>", NextKey)
root.bind("<Button-3>", MouseKlick)
root.bind("<B3-Motion>", MouseHold)
root.title("CenterCam 0.1")
AutoButton = Button(root, text="Auto", command=ModeAuto)
AutoButton.pack(side=LEFT)
ManuButton = Button(root, text="Manu", command=ModeManu)
ManuButton.pack(side=LEFT)
quitButton = Button(root, text="Quit", command=root.quit)
quitButton.pack(side=RIGHT)
def show_frame():
global frame, frameg, framet
global cap
global crosshair
global X,Y,radius,width,height,pixelsize
global AutoMode, ManuMode
offset=3
_, frame = cap.read()
#Flip the frame
frame = cv2.flip(frame, 0)
#Circle detection needs some major work because all dots are analyzed which slows down analysis as dotted trails are made.
frameg = cv2.GaussianBlur(frame,(7,7),2)
ret, framet = cv2.threshold(frameg,50,255,cv2.THRESH_BINARY)
frameg = cv2.GaussianBlur(framet,(3,3),2)
circles = cv2.HoughCircles(frameg, cv2.cv.CV_HOUGH_GRADIENT, 2, 150,param1=70,param2=100,minRadius=45,maxRadius=300)
if (circles is not None) and AutoMode == 1:
circles = np.uint16(np.around(circles))
X =circles[0][0][0] + offset
Y =circles[0][0][1] + offset
radius=circles[0][0][2] + offset
#save frame for final display modifications
displayframe=frame.copy()
#Draw target circle
cv2.circle(displayframe,(X,Y),radius, (255,0,0), 1)
# draw vertical line
cv2.line(displayframe,(X,0),(X,height),(255,0,0), 1)
# draw horizontal line
cv2.line(displayframe,(0,Y),(width,Y),(255,0,0), 1)
cv2.putText(displayframe,'X:'+str((X-(width/2))*pixelsize),(10,(height-50)),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),1)
cv2.putText(displayframe,'Y:'+str(((height/2)-Y)*pixelsize),(10,(height-30)),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),1)
cv2.putText(displayframe,'D:'+str(radius*scale),(10,(height-10)),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),1)
img = Image.fromarray(displayframe)
imgtk = ImageTk.PhotoImage(image=img)
w.imgtk = imgtk
w.configure(image=imgtk)
w.after(1, show_frame)
show_frame()
root.mainloop()
print "G21 G91 G40 G17"
print "G0 X"+str(((X-(width/2))*pixelsize)+Xoffset)+" Y"+str((((height/2)-Y)*pixelsize)+Yoffset)
print "M30"
print "M2"
regards Frank
Please Log in or Create an account to join the conversation.
08 Dec 2015 03:52 #66608
by AnthonyT
Replied by AnthonyT on topic Spindle Cam
Hi Frank,
Thanks for your work on this!
Maybe you are using a different OpenCV than me because the code crashed with this error:until I changed line 150 in your code to (add cvtColor):
I noticed that you removed the trails. The reason I used trails, is because my webcam center is not the center of the spindle, and the tiny cheap plastic lens moves the center whenever I focus it. So, spinning the camera (while attached to the spindle) was the only way I could ensure that I could see the center of rotation (where the milling bit will enter my workpiece).
Also, I never know exactly how far away the camera is from the workpiece, so I would not reliably be able to determine mm/pixel.
There is a way to put new tabs in LinuxCNC, you might be able to put your program in a tab that starts when you click that tab. Maybe there is a way to send your Gcode directly to the machine through LinuxCNC.
Thanks, Anthony
Thanks for your work on this!
Maybe you are using a different OpenCV than me because the code crashed with this error:
cv2.error: /build/opencv-FWWjHr/opencv-2.4.9.1+dfsg/modules/imgproc/src/hough.cpp:1025: error: (-5) The source image must be 8-bit, single-channel in function cvHoughCircles
circles = cv2.HoughCircles(cv2.cvtColor(frameg,cv2.COLOR_BGR2GRAY), cv2.cv.CV_HOUGH_GRADIENT, 2, 150,param1=70,param2=100,minRadius=45,maxRadius=300)
I noticed that you removed the trails. The reason I used trails, is because my webcam center is not the center of the spindle, and the tiny cheap plastic lens moves the center whenever I focus it. So, spinning the camera (while attached to the spindle) was the only way I could ensure that I could see the center of rotation (where the milling bit will enter my workpiece).
Also, I never know exactly how far away the camera is from the workpiece, so I would not reliably be able to determine mm/pixel.
There is a way to put new tabs in LinuxCNC, you might be able to put your program in a tab that starts when you click that tab. Maybe there is a way to send your Gcode directly to the machine through LinuxCNC.
Thanks, Anthony
Please Log in or Create an account to join the conversation.
09 Dec 2015 06:29 #66665
by cgc
Replied by cgc on topic Spindle Cam
Hello Anthony,
I use a black white camera, therefore I delete the cvtColor function.
Now, I understand your trail function...
But I have not mount the camera in the spindle, my camera is fix mount at the Z axis.
I use a telecentric microskop objectiv with long working distance, small deep of sharp and low distortion.
I have a constant offset between camera and tool center.
To measure this offset, bore a small hole with the spindle, then zero the axis, then move this hole under the center of the camera.
This distance is the offset.
To calibrate the magnification, set the pixelsize to 1, then use a exact hole with known diameter, put it under the camera, make a circle and count the pixel diameter of this circle. Now divide the real diameter through this number of pixels.
It would be an elegant way to use this in a tab.
Thank you for the good template !
regards Frank
I use a black white camera, therefore I delete the cvtColor function.
Now, I understand your trail function...
But I have not mount the camera in the spindle, my camera is fix mount at the Z axis.
I use a telecentric microskop objectiv with long working distance, small deep of sharp and low distortion.
I have a constant offset between camera and tool center.
To measure this offset, bore a small hole with the spindle, then zero the axis, then move this hole under the center of the camera.
This distance is the offset.
To calibrate the magnification, set the pixelsize to 1, then use a exact hole with known diameter, put it under the camera, make a circle and count the pixel diameter of this circle. Now divide the real diameter through this number of pixels.
It would be an elegant way to use this in a tab.
Thank you for the good template !
regards Frank
Please Log in or Create an account to join the conversation.
09 Dec 2015 10:37 #66673
by cmorley
Replied by cmorley on topic Spindle Cam
mmm this seems like we should add it to linuxcnc. I think opencv is available in wheezy without compiling yes?
Chris M
Chris M
Please Log in or Create an account to join the conversation.
09 Dec 2015 13:36 #66680
by AnthonyT
Replied by AnthonyT on topic Spindle Cam
Thanks, Frank.
Maybe this link will help you for embedding in a tab:
wiki.linuxcnc.org/cgi-bin/wiki.pl?Axis_Embed_Video
Maybe this link will help you for embedding in a tab:
wiki.linuxcnc.org/cgi-bin/wiki.pl?Axis_Embed_Video
Please Log in or Create an account to join the conversation.
09 Dec 2015 13:38 #66682
by AnthonyT
Replied by AnthonyT on topic Spindle Cam
Yes Chris, OpenCV does not need any special compilation, just apt-get.
It would be great to add it to LinuxCNC.
It would be great to add it to LinuxCNC.
Please Log in or Create an account to join the conversation.
10 Dec 2015 06:55 #66717
by SvenH
Replied by SvenH on topic Spindle Cam
I would love to have this available as standard!
Please Log in or Create an account to join the conversation.
Time to create page: 0.107 seconds