|
-
Apr 6th, 2010, 12:08 AM
#1
Please help with the application for my CNC
I cleaned up the code as much as I could.
Before I get everyone confused, please see this thread first: http://www.vbforums.com/showthread.php?t=610022
I am new to threading when it comes to VB.NET, one of the problems is about that.
After the program runs (it sends and receives data to & from CNC), when I click the X (form close), it just hangs, and does not close. Every time I have to click the "Stop Debugging (Shift+F5)" button.
Now let me explain how the CNC works.
I hope you can keep up, because it's a long read, and a little complicated.
The computer generates RAW (compressed) data that sends to the CNC (the microcontroller).
The motors are stepper motors, there are 400 steps for one full rotation. The microcontroller (PIC18F4550 at 48MHz) sends impulses to the motor driver, and the motor driver gives current to the windings of the motor, therefore for each impulse the motor moves one "step", and each step is 0.9°. So 360° / 0.9 = 400 steps.
The microcontroller has 4 buffers.
2 buffers for incomming data (where data is compressed)
2 buffers for output data / information with each step and direction for the motors to move.
So when data comes in, it goes into one of the "incomming" buffers, when it's done, it calls a routine that will uncompress the data and then it goes to the "output data"
At the beginning of the routine that does the uncompression, it gives a signal to the computer to start sending more data to fill the other buffer.
So in short, as long as there is one empty buffer, the computer can send more data to fill it up.
The microcontroller also sends a signal when one of the "output data" buffers is done processing.
Also about 10 times per second, the microcontroller send the X & Y & Z positions (internal counters) to the computer.
OK, now how does the microcontroller know what to do ?
The data is basically a 2 bit number for each axis. So the 2 bits (therefore a 0 to 4 number),
0 means - do nothing
1 means - move left (also enables the motor / gives power)
2 means - move right (enables the motor)
3 means - do nothing - BUT keep the motor enabled
So for example, to move the motor 3 steps to the left, small pause, then 3 steps to right it sends something like this:
000033331113333333322233330000
It enables the motor first, then sends 1 to move to left, keeps the motor enabled, then sends 2 to move to right, keeps the motor enabled for a while then powers down the motor.
That was for one axis only, so for all 3 axes it uses 6 bits of data (for each step)
Originally all coordinates are calculated as 3 Integers (32 bits) for X&Y&Z, then it substracts previous point with current point, therefore giving values of -1 or 0 or 1. Where -1 is 2 (move right), 0 remains 0 (do nothing), and 1 remains 1 (move left).
Then it gets converted to 3 bytes (where each byte is 0 or 1 or 2), finally it gets "compressed" from 3 bytes (24 bits) to 6 bits, and then this data goes to the microcontroller.
The code for the microcontroller I wrote it in mikroC PRO. I don't expect anyone to make improvements to it, but i'm posing it for reference if anyone is interested. See the CNC_C_Code.txt file attached.
Now before I say what other problems I have with the code, I will let you "digg in" all this information, and see where we go from here...
Now the problem for you is... How will you run the application without a microcontroller to "talk" to ? does anyone know how to create a virtual serial port ?
Last edited by CVMichael; Apr 11th, 2010 at 11:13 PM.
-
Apr 6th, 2010, 03:53 AM
#2
Lively Member
Re: Please help with the application for my CNC
http://www.virtual-serial-port.com/
there are several api's/controls to this effect google
-
Apr 6th, 2010, 05:35 AM
#3
Fanatic Member
Re: Please help with the application for my CNC
for the multithreading issue i think it is probably because the thread is still running that you get a hang, you could put a cancel thread routine in the close form event. Not looked at the code as not at my desk but having worked with cnc before i think i could fix this, what you have done so far seems like it would work fine will look back later when i'm home.
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Apr 6th, 2010, 10:57 AM
#4
Re: Please help with the application for my CNC
There are two links in my signatue that might interest you.
Most people, and I know of one exception(besides me) on this forum, don't do serial port programming correctly in VB. There may be others.
-
Apr 6th, 2010, 05:03 PM
#5
Re: Please help with the application for my CNC
 Originally Posted by Joshwah!
That one looks really good, but the free trial is 15 days, or $129 to buy it.
The virtual port is for you guys to test my CNC application on your computers...
-
Apr 6th, 2010, 05:11 PM
#6
Re: Please help with the application for my CNC
 Originally Posted by dbasnett
There are two links in my signatue that might interest you.
I looked at the "Serial Port" one, but I see way to much code to do something so simple.
I don't know what is the second link you are talking about.
-
Apr 6th, 2010, 08:49 PM
#7
Re: Please help with the application for my CNC
Hi CVMichael,
One thing you may consider using are a mechanical limit on each of the three axis, they could be as simple as micro switches. Once tripped, power is isolated for example. You may already have 'software' limits, however the mechanical ones would provide an extra layer of protection for the CNC hardware as well as personal safety.
These types of mechanical limits are handy when things run away for what ever reason, feedback loops open, software bugs, operator error etc.
Great job BTW .
-
Apr 6th, 2010, 10:37 PM
#8
Re: Please help with the application for my CNC
Thank you Bruce Fox,
Right now there are no limits (not even software), but I plan to have optical switches, I already tested with one, and it worked beautifully.
The switches that I plan to use look like this one:

Honestly, at the moment I am not worried about that too much.
Before I do anything I "simulate"... I have separate power supply to the motors and the microcontroller, therefore, if I don't plug the motors, and leave the microcontroller ON, then it executes all the commands from computer as usual without actually moving the motors.
But other things I want to add:
* Panic button
* A wheel (or 2) to manually move on any axis
* A small display to show the coordinates on the actual CNC (not just the computer)
* Rotary encoders for each axis that are at least 1000 points per rotation (still trying to find good ones that I can plug into a microcontroller)
* oh, and the most important: a good & powerfull spindle... (that I can control the speed through the microcontroller)
-
Apr 7th, 2010, 05:55 PM
#9
Re: Please help with the application for my CNC
After some testing, I found that if I comment out the sub that displays the coordinates in the label SetTextPos_XYZ(), then my application does not hang when I close it.
The weird thing is that the sub SetPicPreviewImage() works exactly the same way except it displays the bitmap (with the lines of the CNC does) instead of the coordinates in the label.
VB.NET Code:
Private Sub Read_PosXYZ(ByVal Buffer() As Byte)
' Got X, Y, Z coordinates
Dim Pos_X As Integer = BitConverter.ToInt32(Buffer, 0)
Dim Pos_Y As Integer = BitConverter.ToInt32(Buffer, 4)
Dim Pos_Z As Integer = BitConverter.ToInt32(Buffer, 8)
Me.CNC_Pos.SetValues(Pos_X, Pos_Y, Pos_Z)
'SetTextPos_XYZ()
End Sub
Private Sub SetTextPos_XYZ()
' Display the coordinates on the interface
Try
If Me.lblPos.InvokeRequired() Then
Dim d As New DelegateNoParams(AddressOf SetTextPos_XYZ)
Me.Invoke(d, New Object() {})
Else
Me.lblPos.Text = "X: " & Me.CNC_Pos.X.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.X / 6400.0, "00.00000") & """)" & _
", Y: " & Me.CNC_Pos.Y.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.Y / 6400.0, "00.00000") & """)" & _
", Z: " & Me.CNC_Pos.Z.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.Z / 6400.0, "00.00000") & """)"
End If
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
Can someone please help me with this problem ?
Thanks
-
Apr 7th, 2010, 07:40 PM
#10
Fanatic Member
Re: Please help with the application for my CNC
hmm just looked quickly but is this right """)" at the end of the last 3 lines, this would appear as ") in the string. I would comment out the try catch block too for testing
Last edited by Megalith; Apr 7th, 2010 at 07:43 PM.
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Apr 7th, 2010, 08:27 PM
#11
Re: Please help with the application for my CNC
 Originally Posted by Megalith
I would comment out the try catch block too for testing
Before it was without the try catch, and I had the same problem. I just added it so that in case there is an error, it will show up in the Output window
-
Apr 9th, 2010, 08:13 AM
#12
Re: Please help with the application for my CNC
I was expecting a little more help...
-
Apr 9th, 2010, 08:40 AM
#13
Re: Please help with the application for my CNC
 Originally Posted by CVMichael
I looked at the "Serial Port" one, but I see way to much code to do something so simple.
I don't know what is the second link you are talking about.
It amazes me that people think SerialPorts are easy, they aren't, especially when done correctly. Also, I read carefully your original post, and you don't have a simple application.
The link you looked at has an emulator embedded in it if you have a modem port. If not then you need a loopback for testing. Here is what mine looks like

You can just see my USB to SerialAdapter.
There is another poster here that knows serial ports well, Dick Grier sp?.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|