Machsupport Forum

Support => Downloads => Macros => Topic started by: cjmerlin on November 05, 2006, 03:24:05 PM

Title: Using the serial port for a toolchanger macro
Post by: cjmerlin on November 05, 2006, 03:24:05 PM
Hi to all,

 Wow, the first to post a macro here.

I've been planning to update the way Mach3 sends signals to the toolchanger and decided on the serial port as the best option.

I'm using an 8051 microcontroller and have just finished the routines
and using the programmed micro, Try to get Mach3 to talk to it.

Using the info gathered from the forum, another thanks to Dave Murray's post for providing some of the code I have create this macro which shows how to use sendfifo () and getfifoentry () to communicate with a micro.


It features a simple handshaking to and from the micro and will display errors with the communication in Mach's message window.

All the code has been produced with helpful explaination

Mach3 now talks to the microcontroller and so the next job is to integrate the micro into some electronics and connect to my toolchanger. It'll be a while then!


All the usual diclaimers, This macro is intended to help you understand how to write your own macro for your machine.
Or you can alter this one if it suits your purpose.



cheers
John




'****************************************************************************
'Intellectual property of John Pearson (aka cjmerlin) created on 5th Nov 2006
'****************************************************************************

'This macro was designed for an 8051 microcontroller connected to a Denford Orac Toolchanger but can be
'used with any suitably programmed microcontroller.
'It uses a simple form of handshaking with the micro in the form of a carriage return value sent to, and received
'from the micro.

'it also has Error coding to get out of routine loops, which displays in the Mach message box

'First thing to do to enable Mach to talk and receive input from a micro is to click on 'config/ports & pins'
' and select event driven serial control.
'Next go to 'config/serial monitor' and configure the port,baud rate and click parity = none checkbox
'Click 'reconnect/save changes' button and then restart Mach.

'You should now be able to use this macro if your micro has been programmed to send and recieve the values below.
'Note to make them easier to see, all microcontroller values are in hexadecimal and start with '&H'


tool = GetSelectedTool()

ToolCommand = &H1E     'Tells microcontroller the job is tool change
JobDone = &HB6         'Micro sends this value when the job is done
CReturn = &H0D         'Value of carriage return in Hexadecimal


Sendval = 3           'Number of tries to send command before producing an error
RecVal = 10           'Number of time to wait for receive handshake value from micro before producing an error.


Select Case tool

Case 1
ToolN = &H1           'Tool value sent to microcontroller
Case 2
ToolN = &H2
Case 3
ToolN = &H3

Case 4

Case 5

End Select

message "Changing To Tool " & tool
 

'============================== Send Command and recieve return chr ===============================
Do
q =q +1
If GetInf = Chr(CReturn) Then Exit Do                  'if micro returned chr "1" ,job done exit loop to next routine

 
 For count = 1 To 5
 x = getfifoentry()                               'Flush buffer, Rule of thumb is to flush once per chr recieved during job.
 Next                                             'If Mach misses a carriage return it wont pickup what was sent
                                                  'and will add it to the next string of chr's sent causing errors

Call sendfifo (Chr(ToolCommand) & Chr(ToolN) & Chr(CReturn))   'send 3 chr's 'Tool Command,Tool Number,carriage return or something

 For t = 1 To 5                                  'Read buffer 5 times before sending command again
   GetInf = getfifoentry()                        'get buffer return chr
   If GetInf <> "None" Then Exit For              'if chr returned then exit
   SetTimer( 1 )
   While GetTimer(1) < 0.05                        'wait approx 0.5 seconds, this value works for me
   Wend
 Next t

Loop Until q= SendVal

If q = SendVal Then
Message "Sending Tool Command Error"
Exit Sub
End If

'====================================================================================================


'****************************** Waiting for Tool selected signal from micro **************************

 Mrec = "none"
 Do
    q= q+1
  If q = RecVal Then                    'Gets us out of the loop after 10 tries and produces an error.
    Message "Receive Error"
    Exit Sub
  End If           
 
    SetTimer( 1 )
  While GetTimer(1) < 0.05              'Wait
  Wend
    Mrec = getfifoentry()               'get return chr
    'message Mrec
Loop Until Mrec <> "None"

If Mrec = Chr(JobDone)  Then            'If returned chr correct then job done
message "Tool " & tool & " Selected"
SetCurrentTool(tool)

Else
Message "Tool Change Error"
End If
       

Title: Re: Using the serial port for a toolchanger macro
Post by: davidimurray on November 17, 2006, 04:17:54 AM
Hi John

Good to see i'm some use  ;)

How are you finding the toolchanger? What have you done for the drive motor and motor controller? Oh and did you just pop 5V instead of 12V through the optos?

I've only briefly looked through the code - but am I right in saying you have a fixed 'job done' code from the mircocontroller. I used a different code for each tool - e.g. T3D - pick tool 3  and T3S - tool 3 selected so that any error or miscommunication would be caught before Mach started running again.

The toolchanger I was working on has ground to a halt - the guy I was doing it for is busy working on his lathe and has yet to put his toolchanger back together after stripping it to clean.

Cheers

Dave
Title: Re: Using the serial port for a toolchanger macro
Post by: cjmerlin on November 17, 2006, 03:05:11 PM
Hi Dave, I haven't made the PCB yet for it, I've been so busy with work. The code for the micro is done and part of the PCB design. I've left the electrics in the toolchanger alone as the PCB replaces the existing one in the box and has alot of other ins/outs on it.

I've used opto-isolators between the micro port pins used, so the 3 opto-sensor wires from the toolchanger sort protection and the voltage drop. The motor control is supplied by an L298 bridge with a current sensing op-amp back to the micro so when the motor reverses onto the stop, the current will increase to a cutoff point and tells the micro to cut power.

The reason to do it this way comes from first getting a faster motor with gearbox to replace the old lot. I had a good Maxon motor/gearbox from an ebay sale a while ago and tried it direct on the worm drive.

It was turning the tool carousel at least twice as fast. Great! I'll make a new mounting plate and drive dog to the worm. I then measured the stalling current to find it was well over 3 amps so some kind of current limiter was needed.

I see what you mean with the 'job done' single code, I didn't see that before and it makes sense now you've mentioned it. Many thanks again, a slight mod to the micro code and the macro then.

Regards
John