Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Toshio.K on October 05, 2021, 05:28:13 AM

Title: How to use USB camera with Mach4
Post by: Toshio.K on October 05, 2021, 05:28:13 AM

I want to align the origin using a USB camera.
Therefore, I embedded the script below in the button (Orijin).
How are you doing?
Please tell me how.
-------------------------------------------------- -------------
--Camera Origin. Add2021.10.3
-------------------------------------------------- -------------
local inst = mc.mcGetInstance ()

mc.mcCntlGcodeExecute (inst, "G91 G0 Z0.0 \ n G0 Y1.7 \ n G0 X7 2.0")
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 06, 2021, 07:42:29 AM
Did it work?
I am not sure what you are asking.
Do you want to know how to do it?
Are you asking if that is the correct code?
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 06, 2021, 08:20:57 AM
Hello.
My idea is that the axis of the camera and the axis of the main axis are off, so I command the distance to move.
I have to install the camera vertically on the table and it's working fine.
However, I wanted to know your thoughts because my thoughts may be wrong.

This is the YouTube that I referred to.
https://www.youtube.com/watch?v=ZgWkOa7EgDA
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 06, 2021, 08:58:45 AM
Use the head shift feature

You will need to write some code.
This is for the Y axis;
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, TKOSftYVal)
TKOSftYVal is just a variable.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 06, 2021, 05:03:35 PM
When I pasted the program on the button, an error occurred.
What should I do?
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 06, 2021, 05:14:45 PM
you might need to put "10" or '10'
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, "10")
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, '10')

or just change this mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, 10)
to this
YShift = 10
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, YShift)
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 06, 2021, 05:38:02 PM
I changed the program, but I get an error.
I was in trouble.
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 06, 2021, 05:49:04 PM
I do not think the error is from the button.
It looks like it is saying the problem is at line 239.
The script you posted does not have 239 lines.
I do not know what the problem is.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 06, 2021, 05:57:29 PM
Where is line 239?
Title: Re: How to use USB camera with Mach4
Post by: jbuehn on October 06, 2021, 07:36:08 PM
In line 6, do you have commas separating each of the parameters in the mc.mcCntlSetPoundVar() call? I can't really tell from the pic.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 07, 2021, 12:03:43 AM
local inst = mc.mcGetInstance ()

YShift = 10
mc.mcCntlSetPoundVar(inst,mc.SV_HEAD_SHIFT_Y,YShift)

I put a colon, but I get an error.
Title: Re: How to use USB camera with Mach4
Post by: thosj on October 07, 2021, 08:15:41 AM
Line 239 is likely in the COMPLIED Lua script that Mach 4 compiles from all your Lua parts on run.

And, in this line of code:

mc.mcCntlGcodeExecute (inst, "G91 G0 Z0.0 \ n G0 Y1.7 \ n G0 X7 2.0")

What is \n G0 X7 2.0? Is it supposed to be something like Y2.0?

Just poking around!!!

Tom

Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 07, 2021, 10:33:29 AM
Delete the button and see if you get the same error.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 08, 2021, 01:39:51 AM
I can't move the camera after removing the button.
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 08, 2021, 07:16:21 AM
Really?
You take out the button that moves things and it doesnt work any more?

Look, you have an error in the code.
Take out the button and see if the error goes away.
Then we can work on getting the button back in correctly.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 08, 2021, 08:45:52 AM
> Take out the button and see if the error goes away.
Please tell me how to do it.
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 08, 2021, 10:29:42 AM
Who put it in?
Who it putting in the code?

If you are doing it this might help some.
Use the one from 6_22_2020 part way down.
https://www.machsupport.com/forum/index.php?topic=43260.msg279695#msg279695
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 08, 2021, 09:59:52 PM
Hello,
Thank you for the useful information.

Now,
About this problem solving method
As a method, should I use the example in the space between ************ below?

****************
• Macros can use a name instead of a number. Name ()
Example: toolheightset ()

• Here is the format of a macro.
     function m # ()
      your code
  end
  if (mc.mcInEditor () == 1) then
m # ()
end
  The last 3 lines let you run or step through the macro while in the editor.
*******************
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 13, 2021, 10:26:22 AM
This is the only part that is the code.

  function m # ()
      your code
  end
  if (mc.mcInEditor () == 1) then
       m # ()
  end
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 14, 2021, 12:50:16 AM
Please tell me the meaning of "mcInEditor".
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 14, 2021, 05:49:45 AM
Please tell me the meaning of this function.

mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, YShift)
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 14, 2021, 08:47:16 AM
 "mcInEditor"
that section lets you run the code while in editor
it is for macros

 "mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, YShift)"
this one thing added in Mach4
it lets you use multiple heads
it shifts the home position
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 14, 2021, 06:16:54 PM
question 1
The meaning of mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, YShift) is shown in the picture. Is this way of thinking all right?

I'm using Mach4 Hobby.

Question 2
Is it okay if the script without using the button is as follows?
function YShift ()

YShift = 10
mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, Y Shift)

if (mc.mcInEditor () == 1) then
YShift ()
end
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 15, 2021, 09:13:44 AM
1)  Yes. It moves where the machine thinks the axis is positioned. It does not actually move it just changes where it thinks it is located.

2)  The code looks correct. I would not use a variable named the same as the macro. Then the button just calls the macro.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 15, 2021, 10:39:57 AM
I don't know what to do in the future.
Would you like to paste this script into a button?

"The last 3 lines let you run or step through the macro while in the editor."
What is an editor?
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 15, 2021, 11:24:19 AM
To make and edit a macro go to Operator then Edit/Debug Macro Scrips
That is the editor.
That is where you will make and save your YShift macro.
I suggest you not use a name for your macro use a number like m111

To make and add a button go to Operator then Edit Screen.
Add your button and program it to do what you want.
Your button will just run that macro.
The code on your button will be like this;
inst = mc.mcGetInstance()
mc.mcCntlGcodeExecute(inst, "m111")
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 15, 2021, 08:15:37 PM
I ran as follows, but an error occurred.
I don't know the cause.

function m111 ()
YShift = 10
mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, YShift)
if (mc.mcInEditor () == 1) then
m111 ()
end

Button script
local inst = mc.mcGetInstance ()
mc.mcCntlGcodeExecute (inst, "m111")

Is mcCntlGcodeExecute bad?

How to execute with macro button mach4
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 16, 2021, 10:00:50 PM
Hello.
There was a full-width "(" in the code to write to the button.
This was the cause of the error. Fixed to half-width "(".

Now you can press the button.
However, the Y axis doesn't move at all. Is this normal?
The photo is the proof.
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 18, 2021, 08:47:45 AM
It will not move.
It is just supposed to put a value in the head shift so the machine knows how much to compensate.
I did not see a value in the head shift.
Exactly what code did you put in and where?
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 18, 2021, 09:01:05 AM
Hello.
The left side of the photo is in the macro script.
I put the right side of the photo in the Orijin button.
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 18, 2021, 10:08:11 AM
Go to the Parameters.ini in your profile.
Look for [5081] and [5082]
Those are the head shift parameters for x and y
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 18, 2021, 10:50:55 AM
Never mind.
Mine does not have them either and it works.
Are you actually hooked up to a machine?
Some things will not function in demo mode.
Title: Re: How to use USB camera with Mach4
Post by: SwiftyJ on October 18, 2021, 01:45:13 PM
Multiple things are wrong with your macro
 1. You don't declare 'inst' so this will likely be nil and cause the API to fail - you should check the return code to test for this
 2. You are missing 'end' from the function
 3. I would declare YShift as a local variable

If you hit F7 on your keyboard in ZeroBraneEditor it will compile the code and tell you if there are any mistakes... but won't tell you all the mistakes  ;)

Try the following
Code: [Select]
function m111 ()
local inst = mc.mcGetInstance()
local YShift = 10
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, YShift)
end

if (mc.mcInEditor () == 1) then
m111 ()
end
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 18, 2021, 01:56:30 PM
Thanks Swifty
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 18, 2021, 05:14:52 PM
Mach4 worked.

Current Position
From 0 to -10

Head Shift
From 0 to +10

Is this result normal?
The motor didn't work, only the numbers seemed to change.

* What should I do with the X-axis code?
Title: Re: How to use USB camera with Mach4
Post by: Bill_O on October 18, 2021, 05:50:28 PM
It does not make the motor move.
It just changes where the machine thinks it is.
You do the same thing for the x axis just use the x shift.
If you want it to move you need to tell it to move.
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 18, 2021, 07:29:30 PM
Should the code be as follows?

function m111 ()
local inst = mc.mcGetInstance ()
local YShift = 10
mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_Y, YShift)
--X axis
local inst = mc.mcGetInstance ()
local XShift = 10
mc.mcCntlSetPoundVar (inst, mc.SV_HEAD_SHIFT_X, XShift)



end
.
if (mc.mcInEditor () == 1) then
m111 ()
end
Title: Re: How to use USB camera with Mach4
Post by: Toshio.K on October 19, 2021, 10:54:00 PM
Mr. Bill_O. Hello.

I understand that this program is a program that changes the "Current Position" to the value after movement even though the head is not moving.
Thank you.