Chat GPT to write scripts for UCCNC

Post anything you want to discuss with others about the software.

Chat GPT to write scripts for UCCNC

Postby Jasonrm1983 » Sat Apr 08, 2023 1:11 am

Hi Everyone,
First time posting here, made my 4x8 CNC 4 years ago and going strong with UCCNC & a C94 with UC300 EITH. So I decided to buy Ger's UCCNC 2022 Screen Set, still waiting for him to send it after only being 12hrs, it is what it is (I am excited to use it)... Maybe he should have Paypal do automation to send the file to your email after purchase, just a suggestion (as Digital Download)

Anyway, so while I am waiting I decided to see if Chat GPT could write me a script for the main features that I initially wanted the screen set for.

So this is what I have asked it...
Q:
Can you create a script for UCCNC that let me assign two buttons on the screen to do Auto Tool Zero for a mobile prob, and one for a fixed probe? When moving to the probes I what each option to go to a Safe Z height in Machine Coordinates. Also, can you have the Safe Z height not go past soft limits?

A: (From Chat GTP)

-- Script for UCCNC to assign buttons for Auto Tool Zero with mobile and fixed probes

-- Define the safe Z height in machine coordinates for moving to the probes
local safeZ = 10

-- Define the Auto Tool Zero function for the mobile probe
function autoToolZeroMobile()
-- Move to the safe Z height, but make sure not to exceed soft limits
local currX, currY, currZ = mc.mcCntlGetCurrentPos(inst)
local newZ = math.min(safeZ, mc.mcCntlGetLimitPos(inst, mc.Z_AXIS))
mc.mcCntlGcodeExecute(inst, "G0 X" .. currX .. " Y" .. currY .. " Z" .. newZ)
-- Prompt the user to attach the mobile probe
local response = mc.mcMessageBox("Attach Mobile Probe", "Please attach the mobile probe and click OK.", 1)
-- If the user clicked OK, run the Auto Tool Zero sequence for the mobile probe
if response == 1 then
mc.mcCntlSetLastError(inst, "Setting mobile probe as tool zero.")
mc.mcCntlProbeMobile(inst)
end
end

-- Define the Auto Tool Zero function for the fixed probe
function autoToolZeroFixed()
-- Move to the safe Z height, but make sure not to exceed soft limits
local currX, currY, currZ = mc.mcCntlGetCurrentPos(inst)
local newZ = math.min(safeZ, mc.mcCntlGetLimitPos(inst, mc.Z_AXIS))
mc.mcCntlGcodeExecute(inst, "G0 X" .. currX .. " Y" .. currY .. " Z" .. newZ)
-- Prompt the user to attach the fixed probe
local response = mc.mcMessageBox("Attach Fixed Probe", "Please attach the fixed probe and click OK.", 1)
-- If the user clicked OK, run the Auto Tool Zero sequence for the fixed probe
if response == 1 then
mc.mcCntlSetLastError(inst, "Setting fixed probe as tool zero.")
mc.mcCntlProbeFixed(inst)
end
end

-- Define the button functions
function buttonMobile()
autoToolZeroMobile()
end

function buttonFixed()
autoToolZeroFixed()
end

-- Assign the button functions to buttons on the screen
local buttonMobileID = mc.mcIntLoad(inst, "UserButton99")
local buttonFixedID = mc.mcIntLoad(inst, "UserButton100")

mc.mcCntlSetToolTip(inst, buttonMobileID, "Auto Tool Zero with Mobile Probe")
mc.mcCntlSetToolTip(inst, buttonFixedID, "Auto Tool Zero with Fixed Probe")

mc.mcCntlSetButton(inst, buttonMobileID, buttonMobile)
mc.mcCntlSetButton(inst, buttonFixedID, buttonFixed)

Note from Chat GTP:
In this updated version of the script, the mc.mcCntlGetCurrentPos() function is used to get the current position of the machine, and the mc.mcCntlGetLimitPos() function is used to get the soft limit position for the Z axis. The math.min() function is then used to ensure that the Safe Z height doesn't exceed the soft limit before moving the spindle to that position. This modification should help prevent any collisions that could occur if the Safe Z height were set higher than the soft limit.

What do some of the scriptwriters here think of its output? I have not tried it yet btw...
Jasonrm1983
 
Posts: 18
Joined: Sat Mar 13, 2021 1:29 am

Re: Chat GPT to write scripts for UCCNC

Postby ger21 » Sat Apr 08, 2023 1:16 am

Jasonrm1983 wrote:still waiting for him to send it after only being 12hrs, it is what it is

I sent it to you 6 hours ago.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Chat GPT to write scripts for UCCNC

Postby Jasonrm1983 » Sat Apr 08, 2023 1:38 am

Hi Ger,
Sorry wasn't trying to put you under the bus, sorry if it came out that way... I sent you an email, but I didn't find it in my spam. Thank you for replying so quickly btw.
Jasonrm1983
 
Posts: 18
Joined: Sat Mar 13, 2021 1:29 am

Re: Chat GPT to write scripts for UCCNC

Postby ger21 » Sat Apr 08, 2023 2:09 am

Code: Select all
What do some of the scriptwriters here think of its output?

I'm not a real programmer, but I don't even know what language that's in. It also appears incomplete.
Take a look at the M6 macro from my screeenset.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Chat GPT to write scripts for UCCNC

Postby Jasonrm1983 » Sat Apr 08, 2023 5:40 am

I'll have to see if I can post an image of it's output, as when I copy and paste it's doesn't format correctly here. So it looks more like gibberish.

I do find it interesting though that if it can work it may help out people who are looking to create more complicated macros for more complex setups.

Btw Ger. Love working with your screen set, it's well thought out and great for my needs. It's like it was made for the way I think and work.

Question: with your screen set; How does the Auto Z Initialize know the offset from the fix plate hight to say the table, is this calculated when you Auto Zero with the mobile probe to let's say from on top of the table? I have it all set up and working this is just something I am curious about...
Jasonrm1983
 
Posts: 18
Joined: Sat Mar 13, 2021 1:29 am

Re: Chat GPT to write scripts for UCCNC

Postby dezsoe » Sat Apr 08, 2023 6:09 am

ChatGPT is a nice toy, but it's far away from writing macros for UCCNC. To insert code into your post use the code button.
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Chat GPT to write scripts for UCCNC

Postby ger21 » Sat Apr 08, 2023 10:46 am

How does the Auto Z Initialize know the offset from the fix plate height to say the table, is this calculated when you Auto Zero with the mobile probe to let's say from on top of the table?

When you click the "Calibrate Tool Setter" button, it saves the difference between the fixed plate location (or tool setter) and Z zero. This value is displayed to the right of the button.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Chat GPT to write scripts for UCCNC

Postby Jasonrm1983 » Sat Apr 08, 2023 8:24 pm

ger21 wrote:
How does the Auto Z Initialize know the offset from the fix plate height to say the table, is this calculated when you Auto Zero with the mobile probe to let's say from on top of the table?

When you click the "Calibrate Tool Setter" button, it saves the difference between the fixed plate location (or tool setter) and Z zero. This value is displayed to the right of the button.


That's what I thought. I noticed when you do the initialization that it puts the offset number in that box next to the button but it writes the number long; like let's say 123.00000001. Is there a way for it to show the number like this 4.00, (I am running my machine in mm and not inch). it looks like it's a rounding "error" but I could be wrong.
Jasonrm1983
 
Posts: 18
Joined: Sat Mar 13, 2021 1:29 am

Re: Chat GPT to write scripts for UCCNC

Postby Jasonrm1983 » Sat Apr 08, 2023 8:38 pm

dezsoe wrote:ChatGPT is a nice toy, but it's far away from writing macros for UCCNC. To insert code into your post use the code button.


I don't know about a Toy, it's pretty powerful and if used in the proper context can come up with some compelling results. Not saying it's correct all the time. but I have testing in some programming use cases with good success. Not trying to sound like a fanboy.

For instance, I asked it this question to see if it is a viable solution to the feature to be implemented.

Q: Can you write a macro for UCCNC that reads the g-code and finds the material height and writes it as an offset that can be seen and manipulated in a field box that can be assigned on the GUI?

A: To create a macro in UCCNC that reads the material height from the G-code, you will need to use the UCCNC scripting language. Here's a general outline of the steps involved:
1. Define a variable to store the material height.
2. Use the "FileOpen" function to open the G-code file and read its contents.
3. Loop through the G-code lines and look for commands that set the material height. In G-code, this is typically done using the "G92" command.
4. When you find a "G92" command that sets the material height, extract the height value and store it in your variable.
5. Use the "FileClose" function to close the G-code file.
6. Write the material height value as an offset to a field box in the UCCNC GUI using the "SetOEMDRO" function.

Keep in mind that this is just a general outline, and the exact implementation will depend on the specifics of your use case and the UCCNC version you are using. It may be helpful to consult the UCCNC documentation and forum or seek assistance from experienced UCCNC users or developers.

Q: Can you write and example macro?

A:
Code: Select all
' Define variable to store material height
Dim material_height As Double

' Open G-code file
FileOpen(1, "C:\Path\To\Gcode\File.nc", "r")

' Loop through G-code lines
Do While Not EOF(1)
    ' Read G-code line
    Dim gcode_line As String
    Input #1, gcode_line

    ' Check if line contains G92 command
    If InStr(1, gcode_line, "G92") > 0 Then
        ' Extract material height from G92 command
        material_height = Val(Mid(gcode_line, InStr(1, gcode_line, "Z") + 1))
    End If
Loop

' Close G-code file
FileClose(1)

' Write material height as offset to field box
SetOEMDRO(800, material_height)

' Show message box with material height value
MsgBox "Material height: " & material_height


This macro assumes that the G-code file is located at "C:\Path\To\Gcode\File.nc", and that the material height is specified using the G92 command with a "Z" parameter.
The macro reads each line of the G-code file, and if it contains a G92 command with a "Z" parameter, it extracts the material height value and stores it in the "material_height" variable.
It then writes the material height as an offset to the UCCNC GUI field box with ID 800 using the "SetOEMDRO" function. Finally, it displays a message box with the material height value.
Note that this is just an example, and you may need to modify it to fit your specific use case and G-code file format.
Jasonrm1983
 
Posts: 18
Joined: Sat Mar 13, 2021 1:29 am

Re: Chat GPT to write scripts for UCCNC

Postby dezsoe » Sat Apr 08, 2023 9:09 pm

I'm sure that ChatGPT will be very useful somewhere in the future, but it is not now. Reading your sample there are two possibilities:
a) If you are a beginner in programing then this code will not help you. Some parts are missing and the line extraction assumes that in a line where a G92 is the last word is the Z value. (And there's no comment after it.) Also, it will not find the material height, just the last value of G92 Z. It seems like ChatGPT knows some bits about the g-code structure, but does not know how it works as a system. Not to mention that by default the macro language is C# and not Visual Basic.
b) If you are an expert in programing then you don't need this code, because you exactly know how to write a much better one.
To be precise I'd say "you definitely need to modify it".
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Next

Return to General discussion about the UCCNC software

Who is online

Users browsing this forum: No registered users and 27 guests