Hello Guest it is April 25, 2024, 12:29:58 AM

Author Topic: Is there a trick to updating registers? what am I doing wrong  (Read 943 times)

0 Members and 1 Guest are viewing this topic.

For some reason, I can't get any registers to update (in any of my coding...?)

Here is the problem du jour:

I trigger this function via a SW toggle switch.  The code is running so it is reading the register correctly the first time (I get the "Joystick Jogging Enabled (Toggle)" logging message) but it is not able to read back the register to toggle off.

Is there something strange about 1 and 0, true and false?  Perhaps I am doing my booleans wrong?

Code: [Select]
function JJogEnToggleBrSp()
local inst = mc.mcGetInstance()
local JogEnH=mc.mcRegGetHandle(inst,"iRegs0/JJog/Enable");
local JogEn=mc.mcRegGetValue(inst,JogEnH);
if (JogEn == 0) then
mc.mcRegSetValue(JogEnH, 1);
mc.mcCntlSetLastError(inst, "Joystick Jogging Enabled (Toggle)") -- BS Debug
end
if (JogEn == 1) then
mc.mcRegSetValue(JogEnH, 0);
mc.mcCntlSetLastError(inst, "Joystick Jogging Disabled (Toggle)") -- BS Debug
end

end
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #1 on: March 06, 2023, 02:03:13 PM »
HI,
the code looks good to me.  Just as a matter of interest try enclosing the 1 or 0 in quotes.....it cant hurt to try.

mc.mcRegSetValue(JogEnH, "1");

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #2 on: March 06, 2023, 03:11:03 PM »
ok. will check.

I've been reading up on the unconventional way that LUA handles Booleans.  Is there a more reliable way to test for the conditions held in the registers?  (in C++, 1 and true are interchangeable, it seems not the case in LUA)
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #3 on: March 06, 2023, 03:19:22 PM »
Hi,
not to my knowledge. Lua has an incredibly small code burden and is blazing fast, one of the fastest scripting languages and not that very far behind C,
but it does take some big shortcuts. It dose not have a Boolean, for instance, in fact it one and only one type of variable, a table. Even a single value is in fact a table
with just one entry. The entries in a table are type free.....ie no Boolean.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #4 on: March 06, 2023, 03:32:52 PM »
HI,
I've often had code cock-ups and one of the common themes is Lua storing a string when I expected a number or vice versa.

I don't know if you've had a chance to play with Lua String functions. At first glance you'd say that these so few and so basic functions to render them less than useful.
But then you start using them and you realise they can in fact do about anything and compare closely to POSIX compliant string functionality....and yet the string function
table (yes all the functions are members of a table) is only about 5kB!!!! Lua surprises that despite its simplicity is as flexible as it is.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #5 on: March 06, 2023, 03:54:45 PM »
Unfortunately, treating the entries as strings like you suggested didn't work. 

One interesting observation, however is that i can get this script error when I try to save a string, yet I can't seem to compare to the readback value of the register as a number.

I have started getting a bit more liberal with my use of tonumber() to prevent accidental strings from getting in the way.

Is it possible that reading a "number" register isn't a numerical value?  hurts my head.


Re: Is there a trick to updating registers? what am I doing wrong
« Reply #6 on: March 06, 2023, 03:59:54 PM »
Hi,
well it was worth a try, as I say I've had errors of that type before. It the definite downside of a language  with seeming variable typing rules where C is typed up the yazhoo!

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #7 on: March 06, 2023, 04:02:18 PM »
incidentally, I'm 90% finished with my analog joystick jogging code.  I have everything working except for the final command to jog.

Here is the last clean version before I started shredding it up to debug.  The logic and everything is all there... :(

Code: [Select]
--Analog Joystick jogging code chunk
--[[

X axis potentiometer connected to pin 43
Y axis potentiometer connected to pin 44

The following registers are necessary
"iRegs0/JJog/Enable"  -- connected to a toggle switch to enable joystick jogging
"iRegs0/JJog/EnLast"  -- Flag to prevent millions of announcements
"iRegs0/JJog/safeZEn"  -- connected to a toggle switch to first move to safe Z before jogging
"iRegs0/JJog/atSafeZ" -- Register to hold at safe z flag
"iRegs0/JJog/stickCtr"  -- center joystick position value (decimal fraction of 1)
"iRegs0/JJog/stickHyst"  --center position hysteresis value (decimal fraction of 1)
"iRegs0/JJog/velLim" -- positive limit (fraction of maximum velocity)
"iRegs0/JJog/jogDirX"  -- Readback for direction (pos, neg)
"iRegs0/JJog/jogVelX"  -- Readback for jog velocity
"iRegs0/JJog/jogDirY"  -- Readback for direction (pos, neg)
"iRegs0/JJog/jogVelY"  -- Readback for jog velocity
]]
--[[
If you follow the format from the ini file:
[iReg15]
Name=JJog/Enable
Desc=Enable Joystick Jogging
InitialValue=0
Persistent=0
[iReg16]
Name=JJog/EnLast
Desc=jog enable flag
InitialValue=0
Persistent=0
Value=0
[iReg17]
Name=JJog/safeZEn
Desc=Enable Safe Z before Jogging
InitialValue=1
Persistent=1
Value=1
[iReg18]
Name=JJog/atSafeZ
Desc=at safe z flag
InitialValue=0
Persistent=0
Value=0
[iReg19]
Name=JJog/stickCtr
Desc=center joystick position value
InitialValue=.52
Persistent=1
Value=.52
[iReg20]
Name=JJog/stickHyst
Desc=center position hysteresis
InitialValue=.1
Persistent=1
Value=.1
[iReg21]
Name=JJog/velLim
Desc=Velocity Limit fraction
InitialValue=.8
Persistent=1
Value=.8
[iReg22]
Name=JJog/jogDirX
Desc=Readback for X direction (pos, neg)
InitialValue=0
Persistent=0
Value=0
[iReg23]
Name=JJog/jogVelX
Desc=Readback for X jog velocity
InitialValue=0
Persistent=0
Value=0
[iReg24]
Name=JJog/jogDirY
Desc=Readback for Y direction (pos, neg)
InitialValue=0
Persistent=0
Value=0
[iReg25]
Name=JJog/jogVelY
Desc=Readback for Y jog velocity
InitialValue=0
Persistent=0
Value=0

]]

--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set Jog value
function JoystickJog(analogX, analogY)
    local inst = mc.mcGetInstance()
local safeZenH=mc.mcRegGetHandle(inst,"iRegs0/JJog/safeZEn")
local safeZen=mc.mcRegGetValue(inst,safeZenH)
local atSafeZH=mc.mcRegGetHandle(inst,"iRegs0/JJog/atSafeZ")
local atSafeZ=mc.mcRegGetValue(safeZenH,atSafeZ)
local stickCtrH=mc.mcRegGetHandle(inst,"iRegs0/JJog/stickCtr")
local stickCtr=mc.mcRegGetValue(inst,stickCtrH) --Read center position value (fraction)
local stickHystH=mc.mcRegGetHandle(inst,"iRegs0/JJog/stickHyst")
local stickHyst=mc.mcRegGetValue(inst,stickHystH) --Read center position hysteresis value (fraction)
local velLimH=mc.mcRegGetHandle(inst,"iRegs0/JJog/velLim")
local velLim=mc.mcRegGetValue(inst,velLimH) -- Read positive limit
local xAxisDirection=mc.MC_JOG_STOP -- MC_JOG_POS,1 for positive, MC_JOG_STOP,0 to stop, MC_JOG_NEG,2 for negative
local yAxisDirection=mc.MC_JOG_STOP
local jogVelX=0.0
local jogVelY=0.0

mc.mcCntlSetLastError(inst, "analogX")
mc.mcCntlSetLastError(inst, analogX)

stickHyst=tonumber(stickHyst) -- Make any accidental strings into numbers
stickCtr=tonumber(stickCtr)
velLim=tonumber(velLim)
analogX=tonumber(analogX)
analogY=tonumber(analogY)

-- Check to see if it is necessary to move to safe Z before jogging
if safeZen == 1 and atSafeZ == 0 then
GoToSafeZBrSp()  -- function to move to a predetermined safe Z position
end
-- *********** X Axis ***********
if analogX > (stickCtr + stickHyst) and analogX < velLim then
-- If greater than .5 + stickhyst, go positive
-- velocity value is 2*analog for positive,
xAxisDirection=mc.MC_JOG_POS
local ijogVelX=(2*(analogX-stickCtr))
if ijogVelX < velLim then
jogVelX=ijogVelX
else
jogVelX=velLim
end
end

if analogX < (stickCtr + stickHyst) and analogX > (stickCtr - stickHyst) then
-- if within .5 +/- stickhyst,
--write 0 to jog velocity register
--rc = mc.mcJogVelocityStop(mInst, axis); --Stop the axis movement
xAxisDirection=mc.MC_JOG_STOP
jogVelX=0
end

if analogX < (stickCtr - stickHyst) then
-- If less than .5 - stickhyst, go negative
-- 1-(2*analog) for negative
xAxisDirection=mc.MC_JOG_NEG
local ijogVelX=(2*(stickCtr-analogX))
if ijogVelX > velLim then
jogVelX=ijogVelX
else
jogVelX=velLim
end
end

-- *********** Y Axis ***********
if analogY > (stickCtr + stickHyst) and analogY < velLim then
-- If greater than .5 + stickhyst, go positive
-- velocity value is 2*analog for positive,
yAxisDirection=mc.MC_JOG_POS
local ijogVelY=(2*(analogY-stickCtr))
if ijogVelY < velLim then
jogVelY=ijogVelY
else
jogVelY=velLim
end
end

if analogY < (stickCtr + stickHyst) and analogY > (stickCtr - stickHyst) then
-- if within .5 +/- stickhyst,
--write 0 to jog velocity register
--rc = mc.mcJogVelocityStop(mInst, axis); --Stop the axis movement
yAxisDirection=mc.MC_JOG_STOP
jogVelY=0
end

if analogY < (stickCtr - stickHyst) then
-- If less than .5 - stickhyst, go negative
-- 1-(2*analog) for negative
yAxisDirection=mc.MC_JOG_NEG
local ijogVelY=(2*(stickCtr-analogY))
if ijogVelY > velLim then
jogVelY=ijogVelY
else
jogVelY=velLim
end
end

--Output Values to registers for diag
local jogDirXH=mc.mcRegGetHandle(inst,"iRegs0/JJog/jogDirX");
mc.mcRegSetValue(jogDirXH,xAxisDirection); -- Write jog direction to register
local jogVelXH=mc.mcRegGetHandle(inst,"iRegs0/JJog/jogVelX");
mc.mcRegSetValue(jogVelXH,jogVelX); -- Write jog velocity to register
local jogDirYH=mc.mcRegGetHandle(inst,"iRegs0/JJog/jogDirY");
mc.mcRegSetValue(jogDirYH,yAxisDirection); -- Write jog direction to register
local jogVelYH=mc.mcRegGetHandle(inst,"iRegs0/JJog/jogVelY");
mc.mcRegSetValue(jogVelYH,jogVelY); -- Write jog velocity to register

--Jog Axes accordingly

rc = mc.mcJogSetRate(inst, 1, jogVelX);
rc = mc.mcJogVelocityStart(inst, 1, xAxisDirection);
rc = mc.mcJogSetRate(inst, 2, jogVelY);
rc = mc.mcJogVelocityStart(inst, 2, yAxisDirection);
end


function GoToSafeZBrSp()
-- function to execute safe Z move or call built-in function to do the same.
end

function JJogEnToggleBrSp()
local JogEnH=mc.mcRegGetHandle(inst,"iRegs0/JJog/Enable");
local JogEn=mc.mcRegGetValue(inst,JogEnH);
if (JogEn == 0) then
mc.mcRegSetValue(JogEnH, 1);
mc.mcCntlSetLastError(inst, "Joystick Jogging Enabled (Toggle)") -- BS Debug
end
if (JogEn == 1) then
mc.mcRegSetValue(JogEnH, 0);
mc.mcCntlSetLastError(inst, "Joystick Jogging Disabled (Toggle)") -- BS Debug
end

end

function JJogSafeZEnToggleBrSp()
local safeZEnH=mc.mcRegGetHandle(inst,"iRegs0/JJog/safeZEn");
local safeZEn=mc.mcRegGetValue(inst,safeZEnH);
if (safeZEn == 0) then
mc.mcRegSetValue(safeZEnH, 1);
mc.mcCntlSetLastError(inst, "SafeZ Enabled (Toggle)")
end
if (safeZEn == 1) then
mc.mcRegSetValue(safeZEnH, 0);
mc.mcCntlSetLastError(inst, "SafeZ Disabled (Toggle)") -- BS Debug
end

end



Re: Is there a trick to updating registers? what am I doing wrong
« Reply #8 on: March 06, 2023, 04:06:03 PM »
It appears that anything that goes into or out of a register becomes an issue.  Lots of NILs from read back registers and no updating in the RegDiag screen.

Did I perhaps not set some hidden parameter of the registers correctly?  It appears that that Mach has a number of register types but it isn't clear how to indicate which you are wanting to use/make.
Re: Is there a trick to updating registers? what am I doing wrong
« Reply #9 on: March 07, 2023, 02:29:47 PM »
Code: [Select]
        --Jog Axes accordingly

rc = mc.mcJogSetRate(inst, 1, jogVelX);
rc = mc.mcJogVelocityStart(inst, 1, xAxisDirection);
rc = mc.mcJogSetRate(inst, 2, jogVelY);
rc = mc.mcJogVelocityStart(inst, 2, yAxisDirection);

What values do you have for jogVelX and jogVelY as mc.mcJogSetRate is a % of maximum motor velocity?
Also, you have the values for the X and Y axis incorrect. X axis is 0 and Y axis is 1. Alternatively you can use mc.X_AXIS and mc.Y_AXIS