Hello Guest it is March 28, 2024, 07:50:53 AM

Author Topic: variable  (Read 2577 times)

0 Members and 1 Guest are viewing this topic.

variable
« on: July 18, 2018, 04:16:44 AM »
I create new variable
i have register "csmio-ip/analog input 0" and new "new variable"

and i want to do sth mathematic operation, have I write lua sript?

sb can help me?
« Last Edit: July 18, 2018, 04:31:54 AM by michael123 »
Re: variable
« Reply #1 on: July 18, 2018, 04:33:41 AM »
I want to get "csmio-ip/analog input 0" and do mathematics operation and save in  "new variable"
Re: variable
« Reply #2 on: July 18, 2018, 07:48:18 AM »
Hi,
it is a fairly simple matter to read a register value.

Quote
LUA Syntax:
hReg, rc = mc.mcRegGetHandle(
      number mInst,
      string path)
This gets the handle of the register.

Quote
LUA Syntax:
value, rc = mcRegGetValue(
      number hReg)
This gets a numeric value. It may be that the value is stored as a string in which case you would use:

Quote
LUA Syntax:
buf, rc = mcRegGetValueString(
      number hReg)
You could then use all the standard arithmetic and mathematical functions within Lua and then write the result back to the same register
if you wish or another register of your choice. To make a new register use the Regfile plugin to create it. Then use mcRegGetHanndle() to get
the handle in the normal way and use mcRegSetValue() or mcReSetValueString() to write a value into the register.

Tell me a little more about what you want to do, maybe I can help with the code.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: variable
« Reply #3 on: July 19, 2018, 06:25:24 AM »
can you write example code?

for example if i want to read csmio-ip/analog input 1

and multiply by 5
and divide by

and save in gRegs0/newvariable45


I can ask you for it?

and where i must write this code?
Re: variable
« Reply #4 on: July 19, 2018, 09:04:38 AM »
Hi,

Quote
and divide by
Divide by what? I substituted a number 1.141 just as an example.

First you need to create a new register.

Go to Configure/Plugins/Regfile click on the Global Regs  and then the green cross to create a new global register. Type in a name
and an initial value, if you want one and whether its persistent, ie saved at the end of the session.

Now open the ZeroBrane editor and create this macro:

Code: [Select]
function m150()
local inst=mc.mcGetInstance()
local hreg=mc.mcRegGetHandle(inst,'ESS/Encoder_0')
local regvalue=mc.mcRegGetValueString(hreg)
regvalue=tonumber(regvalue)
regvalue=(regvalue*5)/1.141
hreg=mc.mcRegGetHandle(inst,'gReg0/newreg45')
mc.mcRegSetValueString(hreg,regvalue)
end
if (mc.mcInEditor()==1)then
m150()
end

Note because I don't have a CSMIO I called an ESS register 'Encoder_0', you would substitute the path to your CSMIO register.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: variable
« Reply #5 on: July 19, 2018, 09:17:15 AM »
Hi,
sorry, hadn't tested it properly....I needed to convert regvalue back into a string to use mcRegSetValueString() and also
had a typo in the path to your new register.

Code: [Select]
function m150()
local inst=mc.mcGetInstance()
local hreg=mc.mcRegGetHandle(inst,'ESS/Encoder_0')
local regvalue=mc.mcRegGetValueString(hreg)
regvalue=tonumber(regvalue)
regvalue=(regvalue*5)/1.141
regvalue=tostring(regvalue)
hreg=mc.mcRegGetHandle(inst,'gRegs0/newreg45')
mc.mcRegSetValueString(hreg,regvalue)
end
if (mc.mcInEditor()==1)then
m150()
end

Each time you MDI m150() the procedure will occur.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: variable
« Reply #6 on: July 23, 2018, 03:37:04 AM »
thank you very much :)

I wrote this and save, but program dont read it (in view lua script)

Can you help me? :)
Re: variable
« Reply #7 on: July 23, 2018, 04:15:20 AM »
and from mcLua

i must save in mach4hobby/ScreenScript or somewhere else ?
i save in mach4hobby/ScreenScript and program (mach4) dont read change
« Last Edit: July 23, 2018, 04:30:38 AM by michael123 »
Re: variable
« Reply #8 on: July 23, 2018, 08:24:35 AM »
ok, i write to code in edit screen
but how should I run it?
I want it to work all the time
Re: variable
« Reply #9 on: July 24, 2018, 02:33:12 AM »
Hi,
I wrote this code as a macro, if you wrote m150() in a Gcode file it would run or each time you
want it to run just MDI m150().

Note that this code is in a separate file called m150() in the Macros folder of your current profile.
I did it that way because it was simple and you still haven't provided any idea what you wish to do with it.
Putting the code in the screenload script is a waste of time.....this is written as a macro.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'