Better Way to Stop My Charts?

I am currently preferring OptoScript whenever possible as opposed to the GUI in PAC Control.

That said, is there a more elegant or fool-proof way to stop my charts during my initialization phase?

SetVariableFalse(bChartStatus);
repeat
StopChart(Test);
bChartStatus = not IsChartRunning(Test);
until (bChartStatus);

SetVariableFalse(bChartStatus);
repeat
StopChart(PID_Oil);
bChartStatus = not IsChartRunning(PID_Oil);
until (bChartStatus);

SetVariableFalse(bChartStatus);
repeat
StopChart(Bounds_Check);
bChartStatus = not IsChartRunning(Bounds_Check);
until (bChartStatus);

SetVariableFalse(bChartStatus);
repeat
StopChart(Data_Collection);
bChartStatus = not IsChartRunning(Data_Collection);
until (bChartStatus);

SetVariableFalse(bChartStatus);
repeat
StopChart(Safety);
bChartStatus = not IsChartRunning(Safety);
until (bChartStatus);

You say initialization - if that means on powerup/restart - then none of the charts are running at startup, so this is unnecessary.

If you are trying to stop your charts to reset/restart your process, it may be better to have a variable with a stop request command for your charts so that your charts can check this and exit themselves. This way you can have your charts stop at a clean point and not midway through some task. If your charts have real long delays in them, this will probably not work well though.

You can also eliminate the bChartStatus variable:

while(IsChartRunning(Test))
  StopChart(Test);
  Delaymsec(50);
wend

If you want to keep going the way you are, I would command all the StopCharts first, then put in a delay of 100ms or so, and then check if they stopped, but efficiency is probably not that important in this code.

Also, be aware that if you are using locks (semaphores), that your code can end up deadlocked and never exit since charts will continue to run if a lock is set. If you don’t know what a lock is, then you are probably not using them.

Thank you!

True on your first point, not necessary upon power-up / restart, but I am paving the way for (eventually) being able to jump back and re-init everything without power-up / restart taking place, sort of a re-init button in the GUI.

Good point on eliminating the status variable. I also realize I can do a similar thing below where I start them up.

Correct, I’m not using locks, but now I know they exist :slight_smile:

Revisiting since I have a while before I can activate the machine that this chart belongs to:

for stopping I am now currently using:

while(IsChartRunning(Test))
StopChart(Test);
DelayMsec(50);
wend

And starting:

while(StartChart(Data_Collection))
wend

I have a ways to go as I slowly build in error checking and status of operations, so I know there’s plenty of improvements to be made. I’m anticipating one critique of the above is that the loops will run indefinitely if there’s an error or something’s jammed up, correct? If so, I’m currently okay with that until a better safeguard system is in place.

Continued critiques welcome!

Is there a problem you are trying to solve where your charts don’t start? I’ve never had this happen - I just call

StartChart(Data_Collection);

and move on. My projects don’t have more charts than the controller can handle though, maybe yours does?

Anyways, I would throw a delay in the while loop if you need to keep it this way to improve performance on your other running charts. This one is wasting CPU time calling StartChart over and over.

Thanks Philip, this is exactly why I’m digging into this stuff. I inherited a few designs that I am slowly working through while also nearing commission of my first machine w/ Opto code in it.

The time’s come for me to review what stuff I put into my new machine, that was based on the old ones, that might not be needed in a reasonable strategy.

I’m going to keep the StopChart stuff, but just switch to pure StartChart. The original example chart that someone provided to my co-worker to make our first few machines had all these checks to see if a chart was started. I forget if they were from Opto or not, and wasn’t around at the time.

Circling back to this post from 2018, and specifically the above remark about having a variable with a stop request command, below is an excerpt of my chart. The areas circled in blue ask the question “Is stop_command =1?”. If yes, then the process is stopped and restarted from the beginning (paused, with a “Go?” command). Stop_command is normally 0, so if the user clicks the Stop button in groov View which is configured to change Stop_command to 1, then it stops the process.


Is this the correct approach? I am still very new at PAC control and while the above solution works fine, I am not sure if it is the preferred approach.

Seems reasonable to me, probably lots of different approaches.

I see a lot of loops in that chart, make sure you put a small delay in those.

Roger that. I have redone several charts recently and made a specific point to add delays wherever there are loops.

I typically like to use discrete states in my control strategies. One thing I did recently was use the state variable to exit the process and stop the chart like below. If the state value isn’t used, for example -1, then it falls through the conditions to the Stop block where I can shutdown outputs and any other charts started by this process.

1 Like