Macroloop while running a program

If you have a question about the software please ask it here.

Re: Macroloop while running a program

Postby dezsoe » Fri Sep 21, 2018 4:04 pm

Fine! :)
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macroloop while running a program

Postby Battwell » Fri Sep 21, 2018 4:44 pm

i think i found it.
added a thread sleep if start button had been pressed after outputs set ( same as if a button was toggled so it didnt re toggle)
maybe it was seeing multiple cycle start presses? and this was blocking things?
it wasnt stopping if button was pressed on screen- only on button.
i hate niggly things !
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 819
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Macroloop while running a program

Postby dezsoe » Fri Sep 21, 2018 5:05 pm

I don't like sleeping threads. :) I save the last states and do slow operations (anything on the screen, button calls etc.) only once when needed.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macroloop while running a program

Postby Battwell » Fri Sep 21, 2018 7:31 pm

i only sleep the thread 300ms if a button has been pressed (as a debounce)
100ms sleep used between loops just so it doesnt overload the pc. is that too much?
all running nicely now- including the analogs after working out the math.
just changed spindle analog to be 50% to 150% which is more practical and less fiddly on the pot.
the sleep after pressing cycle start seems to have solved the problem i was having.
https://youtu.be/qJB7wor6OsY
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 819
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Macroloop while running a program

Postby dezsoe » Fri Sep 21, 2018 9:05 pm

As I wrote, I never use any delay. The debouncing is done by modbus, because the loop interval is set to 30ms. I used the "modbus advanced slave" example from this modbus library. The only thing I changed is that the 4 digital inputs are set to INPUT_PULLUP instead of INPUT. I tried to measure the CPU load of the macroloop below, but I could not. Note that the macroloop system in UCCNC makes a 50ms sleep after each loop.

Code: Select all
// ================================================================================================
// Arduino Modbus I/O sample
// ================================================================================================

ushort Inputs = 0;
ushort Pot1 = 0;
ushort Pot2 = 0;

exec.GetModbusregister(0, out Inputs);
exec.GetModbusregister(4, out Pot1);
exec.GetModbusregister(5, out Pot2);

int FRO = Convert.ToInt32(AS3.Getfield(232).Replace("%", ""));
int JRO = Convert.ToInt32(AS3.Getfield(913));

int newFRO = (int)((double)Pot1 / 1023.0 * 300.0 / 5.0) * 5;                    // 5% step
int newJRO = (int)((double)Pot2 / 1023.0 * 100.0);

if (newFRO != FRO)
{
  AS3.Setfield(newFRO, 232);
  AS3.Validatefield(232);
}

if (newJRO != JRO)
{
  AS3jog.Setfield(newJRO, 913);
  AS3jog.Validatefield(913);
}

int newInputs = ((int)Inputs & 0x0F) ^ 0x0F;                                    // 4 inputs, change to active high

if (lastInputs == 16) lastInputs = newInputs;

if (newInputs != lastInputs)
{
  int change = newInputs ^ lastInputs;
  // NC reset switch -> force reset to switch state
  if ((change & 0x01) != 0)
  {
    if ((newInputs & 0x01) != 0)
      exec.Callbutton(513);
    else
      exec.Callbutton(512);
  }
  // Compare SPST switch state to screen button state -> press button if needed
  if ((change & 0x02) != 0)
  {
    if (((newInputs & 0x02) != 0) != AS3.Getbuttonstate(114))
      exec.Callbutton(114);
  }
  // NO pushbutton
  if ((change & 0x04) != 0)
  {
    if ((newInputs & 0x04) != 0)
      exec.Callbutton(130);                                                     // Button pressed
//  else
//    exec.Callbutton(0);                                                       // Button released
  }
  // NO pushbutton
  if ((change & 0x08) != 0)
  {
    if ((newInputs & 0x08) != 0)
      exec.Callbutton(522);
//  else
//    exec.Callbutton(0);
  }
  lastInputs = newInputs;
}

bool LED1 = exec.GetLED(19);
bool LED2 = exec.GetLED(54);
bool LED3 = false;
bool LED4 = false;

ushort digitalOut = (ushort)((LED1 ? 0x01 : 0) + (LED2 ? 0x02 : 0) + (LED3 ? 0x04 : 0) + (LED4 ? 0x08 : 0));

ushort analogOut1 = (ushort)(AS3.Getfielddouble(2451) / AS3.Getfielddouble(178) * 255); // Overridden RPM / max. RPM
ushort analogOut2 = (ushort)(0);

ushort[] data = new ushort[3];
data[0] = digitalOut;
data[1] = analogOut1;
data[2] = analogOut2;

exec.SetModbusregisters(10, data);

// ================================================================================================

#Events

// ================================================================================================

int lastInputs = 16;
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macroloop while running a program

Postby Battwell » Sat Sep 22, 2018 8:34 am

Ah I’d read originally that we had to add a sleep . I didn’t know it had been added in uccnc
My macro has to run continuously as it updates the LEDs on the panel as soon as any buttons are pressed on screen.
It runs very fast. I had to add thread sleep after a toggle button press or it would re toggle as it would be read a few times while finger was on the button. Now it toggles just as I want.
Il put it in the machine later today
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 819
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

Re: Macroloop while running a program

Postby cncdrive » Mon Sep 24, 2018 3:56 pm

The macroloop always makes a wait of 40msec after it's cycles, but you can bypass that with making your own loop inside the macroloop.
It is like:
Code: Select all

while(loop)//The built in loop in the UCCNC macroloop core:
{

while(loop)//Your own loop. While this loop is running it will not allow the code execution to the end of the built in loop to execute the 40msec sleep and so you will have to execute a sleep to not overload the computer.
{
  //Some code here and add a thread sleep also...
}

Threat.Sleep(40);
} //Built in loop in the UCCNC macroloop core:

cncdrive
Site Admin
 
Posts: 4695
Joined: Tue Aug 12, 2014 11:17 pm

Re: Macroloop while running a program

Postby dezsoe » Mon Sep 24, 2018 6:21 pm

Is it 40 ms not 50? :shock: 20 loops take exactly 1 s, I use it for a timer to auto-reset the machine and it seems very precise.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Macroloop while running a program

Postby cncdrive » Mon Sep 24, 2018 6:47 pm

Yea, maybe it is 50msec, I'm not sure now, I should measure or check it. :)
cncdrive
Site Admin
 
Posts: 4695
Joined: Tue Aug 12, 2014 11:17 pm

Re: Macroloop while running a program

Postby Battwell » Mon Sep 24, 2018 7:54 pm

i tested mine with no thread sleep, (unless a button was actually pressed) and its blisteringly fast! (see test code i cobbled up for this in uccnc toolbox)
ive slowed it down now to 1/8th second loops by checking the 1/8th second timer i have the arduino udating its output timer at.
it skips the whole macroloop while the timer count equals itself when checked.
i did this to make sure it cant do anything unwanted if communication is lost.
its plenty fast enough for my control panel.

im just working on making all buttons dual function- by timing the length of button press. <2.5 seconds 1 function, >2.5 seconds another function.
its making my head hurt doing that in an i= loop :-(
Uc300eth on router and mill.
UK uccnc powered machine sales. https://cncrouter.uk/atc-cnc-routers.htm
Automateanything/duzzit cnc/mercury cnc
Battwell
 
Posts: 819
Joined: Sun Sep 25, 2016 7:39 pm
Location: South Wales. Uk

PreviousNext

Return to Ask a question from support here

Who is online

Users browsing this forum: Google [Bot] and 14 guests