Stop using global variables!

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2024
  • PLC-programmers, stop using global variables!
    Global variables, global variables and global variables, whatever example sample project or real life PLC-project I look at, I see global variables being used to store data and exchange information between software modules. This has to stop! What's wrong with global variables, especially since so many of us PLC-programmers are using them and the PLC vendors themself use them in their examples? Let's find out!
    Visit our sponsor → shipinsix.team/
    Please support this channel by either:
    - A donation by using the "Thanks" button under the video
    - A donation through PayPal → www.paypal.com/donate/?hosted...
    - Becoming a Patron → / sagatowski
    Subscribe so you don't miss new videos → th-cam.com/users/jakobsagatows...
    0:00 Introduction
    1:41 Non locality
    2:36 Implicit coupling
    3:40 Sponsor: Ship In Six
    4:14 Difficult to trace
    5:11 Unit testing
    6:20 Exceptions
    7:11 Outro
    #plc #programming #softwareengineering
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 48

  • @StifLabarque
    @StifLabarque 5 หลายเดือนก่อน +3

    I come from a world (line-of-business applications written in higher programming languages (e.g. C#)) where this knowledge comes natural to most of the people I worked with. I've recently dipped my toes into PLC programming and I was amazed how some of the software best-practices seem to only now being slowly picked up in this PLC world. Thanks for spreading this knowledge!

  • @alexchiosso221
    @alexchiosso221 ปีที่แล้ว +4

    Nice video Jakob, quite interesting topic and the concept you've mentioned is clear for the most.
    I started programming PLC's from the late '80s.
    At those days the situation was totally different.
    The PLC's programming tools rely on IL/AWL , Ladder and few on FBD (Siemens).
    The CPU's elaboration power was a lot less and the use of FBs and FCs slowed down the process capabilities, needless to say the low memory available.
    Moreover some industries like automotive industries made the use of the Ladder the only one accepted (Allen Bradley for the most in the US and Siemens in Europe).
    In the last decade the improvement of the CPUs performances and the IT technoligies introduced for the Industry 4.0 paradigm changed everything.
    The Industrial OOP concept in now a must to overcome the complexity but the pace is not so fast and the old habits still last. ;-)

  • @zigajavornik1026
    @zigajavornik1026 ปีที่แล้ว +3

    I was introduced to the world of PLC programming and the programs that I was given were such a mess, with global variables, no structures, no case statements, etc... I always dread the days where I need to connect to a customer for support and I have to debug hundreds of lines of code in Main 😂. Beckhoff has some decent introduction to the world of OOP (which does steer you away from globals, in my opinion), but you put the nail in the coffin as far as programming practice goes, for me at least. I have a project coming where I will definitely start using TDD as well, so thanks for that!
    For me, the only global variable I use is system's time, which I do not want to pass explicitly to every block that might need it and is only assigned by 1 source, the time provider instance and a few others that really are only written from one source and are only a read for the rest of the program (EtherCAT error state, first cycle, control system's (HMI) connection state).

  • @Hanssing
    @Hanssing ปีที่แล้ว +3

    In general all variables in some PLCs are in reality still global eg. Siemens TIA, instance-FBs are accessible from everywhere 😞
    I CoDeSys (Twincat) I still prefer the IO-map to be global, as it represents "physical the machine-structure" which I consider intrinsically "global". And then map using 'REF=', not at all needed but it just still sits right with me 🙂
    I must also admit to using it in CodeSys 2.3 for HMI-tags, where the access symbolic setting was easy, and inherited hiracial, so less chance of error in the SYM-file as interface structures, in a certain area was automatically exported.
    I fully agree with the generel points in the video, though.

  • @TohidAlizadeh
    @TohidAlizadeh ปีที่แล้ว +1

    Nice explanation. This information will be useful for many individuals 👍👍

  • @GISED_Link
    @GISED_Link ปีที่แล้ว +2

    As a lot of other people in the comment, I use GVL's for IO's (IN, OUT and IO_LINK). I also have a specific GVL for RETAIN and PERSISTENT. What I am more and more doing, is using the outputs of a PROGRAM. So you have a global variable but in read-only access. Usefull to pass the machine state or a particular recipe as input of a process

  • @ohwow2074
    @ohwow2074 ปีที่แล้ว +1

    Good points. Also you didn't mention the static and thread local variables. Including the ones declared at block scope such as function scope. Those types of variables are bad for unit testing too. A usual alternative to those variables is to just declare them at the call site and pass them by reference to the callee.

  • @miirazgmail
    @miirazgmail ปีที่แล้ว +2

    simple code - USE
    embedded code, USE if simple or have to get fast access (globals can be stored in registers of microcontroller)
    normal coding - DONT USE
    TwinCAT - I refactor my every code to not using them. Instead of globals I would rather use Function Blocks and initialize them in Main.

  • @AlbertoGarcia-ge1vw
    @AlbertoGarcia-ge1vw ปีที่แล้ว +2

    Very educational video. I did quit automation because the lack of these practices, I hope it gets better in the future and you are helping a lot bringing this information to the automation field.
    I feel you should start explaining as well what is a scope before talk about global variables. In automation there are a lot of people that don't even know that, I read in LinkedIn a guy who was confusing global variables with structs.

    • @JakobSagatowski
      @JakobSagatowski  ปีที่แล้ว +1

      Hey Alberto! Thanks for your feedback. You're right, so to clarify; The scope of a variable defines where it’s accessible in a program based off of where it was created. Global variables scope is the complete program, while local variables scope can only be accessed in where they are defined (function, function block or method).

  • @kunalpatil5250
    @kunalpatil5250 ปีที่แล้ว

    Very informative Jakob!
    Tracking the source of the change in the behavior of the program due to the use of global variables is pain in the a**. However I believe it’s up to the individuals’ preference how they want to use the variables. Some project are needed to be delivered quicker so just go with them.
    We avoid them mostly but handshaking between the modules is pretty easy and neat using them. Just pass them as input reference variables to the modules and the chain is easy to track.

  • @Fiddeflygare
    @Fiddeflygare ปีที่แล้ว

    As of the last 13 years of programming in building automation and some industry applications i do agree with you as far as using globals while programming function blocks, functions etc.
    My main use of globals are inputs/outputs from the io-cards on the plc and controlvalues related to the specific regulator of a temperature probe etc. This is because it’s pretty much needed in order to have those important values saved as persistent data, used in the scada application and hmi application.
    I do however thus far never used them for individual functionblocks, functions or anywhere else unrelated, if i need to take the input of two values in order to make a change or similar before it sets a global variable for an output in my program i use a local variable in that program.
    The structure of program that we use means that i have several subprograms with actions for every subpart function for every system in a big building like a hospital and as you say, if i had used global variables for all that it would be a nightmare to troubleshoot and in my case it also could be lethal if i made a change in a wrong spot in that global variable list 😬
    I think i understood your point as that i am programming as you are trying to explain in this video 😅

  • @SimOn-bz4xy
    @SimOn-bz4xy ปีที่แล้ว

    A question, i do not like using variabels, but i program in many diffrent PLC systems. And for example GX Works from Mitsubishi.
    There you cant call a program and get the value. For example in twincat you can say POU1.Variabel1 in another program and get reading access to that Variabel. What would you do instead in these situations ?

  • @RoRu87
    @RoRu87 ปีที่แล้ว +2

    Well said! When I first started with PLC programming, I was surprised by how often global variables are used. In any other language it is discouraged. Just look at any Stackoverflow question/answer where someone tries to solve it with global variables :D.
    I hope you're also making a follow up video to show people how they can prevent using globals 99% of the time?

    • @JakobSagatowski
      @JakobSagatowski  ปีที่แล้ว +3

      And unfortunately this is how most beginners are introduced to declaring variables, as even the official documentation is using globals like there is no tomorrow.
      I'll do my best to make more videos about this important topic!

  • @ramialcarmig1
    @ramialcarmig1 9 หลายเดือนก่อน

    Hello Jakob.
    Then What is the best way to share Information between two functions bloks?

  • @NeonGreenT
    @NeonGreenT ปีที่แล้ว +2

    Hey Jakob, awesome Video. What's your take on passing objects by Reference via VAR_INPUT or as implicit Reference via the good old VAR_IN_OUT ? Are there situations where one would opt for either one in the first place or does it even really matter ? I would argue using VAR_IN_OUT should be the standard way, but one could also argue that the introduction of references makes VAR_IN_OUT a legacy/deprecated feature.

    • @JakobSagatowski
      @JakobSagatowski  ปีที่แล้ว +4

      Hey there! I generally prefer VAR_IN_OUT as it's mandatory to supply any parameters, while it for VAR_INPUT is optional (even for references, that's why you have this annoying ISVALIDREF() check that you don't need to use if you use VAR_IN_OUT).

  • @sightf2
    @sightf2 ปีที่แล้ว

    Thanks for another great video. But as someone new to industrial automation, HOW would we go about passing data between routines without global variables. For example, ive written a metrics routine that gathers data like cycle time and fault counts from the other routines and don't know how to do that without writing that state to a global variable that can then be read by the metrics routine.

    • @JADJuanan
      @JADJuanan ปีที่แล้ว

      You dont need to use global vars to pass data if both routines have the same local scope. Lets say, for instance, you have 2 different routine instances that need to talk to each other, for that, write them inside the "main" routine and declare local variables in the "main" routine to interface between the two

  •  หลายเดือนก่อน

    Correct me If I am wrong, but the instance of the Fb need to be in global, so you can call the get and set From other parts of the code? Also the persistent and and retain for configuration parameters from the SCADA, I understand they need to be global? And what about status and commands from SCADA? I would love to see how you implement a real life example with all these details... Something simple like a DOL motor, I would love to see a video, showing this paradigm In a real industrial example, if you could make a live would be even better...

  • @TimVloeberghs
    @TimVloeberghs ปีที่แล้ว

    We build a framework to configure, control and monitor devices in the field. For example, you can start, stop, reverse, reset a motor, and monitor for error, running status, stopped status, and other status flags throughout the entire program. This is done using global variables (of structs) and eliminates the need for passing status from block to block. Because of the way it is build, it is still quite easy to monitor (and control), and we haven't found an "easier", or more "elegant" way to achieve this. Also towards the customer, this make the code very easy readable and takes away much of clutter regarding timers and conversions, since this is taken care of by function blocks, compiled in a library. So in our opinion, global vars sill have their uses.
    However, for the passing of states and commands between program blocks, we no NOT use global vars and totally agree that there are much better ways to do that.

  • @miroslavkorenek156
    @miroslavkorenek156 ปีที่แล้ว

    Anyone here working with Siemens TIA Portal? I am looking for some source of information about proper metodology how to manage your code/project. So far it has been hard to find on the internet. I have not come across similar person ... like Jakob ... for siemens. Do you have some tips?

  • @Hanssing
    @Hanssing ปีที่แล้ว +1

    And dont get me started about SCADA's like Indusoft, Zenon and the like - they import everything into a flat global scope.

  • @whathuh6965
    @whathuh6965 26 วันที่ผ่านมา

    AHA! I was right. I'm currently coding a trading algorithm and realized that I was having multiple issues with Global Variables. My project is currently over 14,000 lines of code wherein a large section of code was relying on an approximation of 20+ global variables. I was so stressed out to where I begin looking to see which GV's were causing the issues only arrive at the eureka moment that made me say to myself: "FUK GLOBAL VARIABLES" "THEY'RE BAD"! "I'M NEVER USING THEM AGAIN".

  • @ebastiaa
    @ebastiaa 5 หลายเดือนก่อน +1

    Great video, well explained
    What is also not done in pc coding is the Hungarian notation method
    It makes code less readable. Why is the plc world so persistent in this?

    • @JakobSagatowski
      @JakobSagatowski  5 หลายเดือนก่อน

      I think the primary reason is historically bad/proprietary IDE's with bad highlighting.

  • @AndyVallEEngineer
    @AndyVallEEngineer ปีที่แล้ว

    AB 5000 coding. We use a set of Data called Mechanism, which includes the bool, dints, real, timers, etc, all used for that mechanism. We also use sequence step methodology, which to me is close to state logic. Basically, the mechanism, which can be either an actual part of the equipment we are integrating, or an idea/process, is the only thing that can modify the values in the data mechanism. Anything, that when planning, looks like it will be tough to separate, because that part of the equipment works depends so much on what another part is doing, is kept together in a mechanism, which might be included under a higher level mechanism, so it is easier to debug parts without integration until later. My opinion is that global is fine, as long as the control engineering group uses a good practice.

  • @garethsmith7628
    @garethsmith7628 4 หลายเดือนก่อน

    Does it really matter until we get some memory safe languages, like Rust?

  • @wuketuke6601
    @wuketuke6601 6 หลายเดือนก่อน

    yeah but what about global variables that are read only (except of course in very specific places)? What im talking about is something like user preferences, especially in a user interface. thats both impossible to unit test, and unique to the application anyway. For example, i dont see the problem in storing the prefered color scheme in a (read only) variable, so that buttons and other ui elemts can easily get the color theyre supposed to be

  • @jacobchan4697
    @jacobchan4697 6 หลายเดือนก่อน

    It would be great if you can add some examples when mentioning topics in video

  • @MtotheTtotheB
    @MtotheTtotheB 5 หลายเดือนก่อน

    It is not only on low ressources where Global Variables make sense. We have big machines running with huge IPCs. However, if we don't structure the program with GVL online change etc. is a pain.

  • @str915
    @str915 5 หลายเดือนก่อน

    I agree, and it's a shame this still has to be mentioned.
    Sadly, every few months I see a new powerful loud ignorant that continues to do it wrong.
    Is there a list of studies, Pdf's, articles I can drop on them ?

  • @aaqilkhan
    @aaqilkhan ปีที่แล้ว +3

    I declare all the hardware linked variables in a GVL listing (AT%I* & AT%Q*) - there's no particular reason for doing this, just been doing it this way since I was first introduced to this during a training session by Beckhoff Canada. Anyone else does it this way? I'd be curious to know how other programmers do it.

    • @JakobSagatowski
      @JakobSagatowski  ปีที่แล้ว +5

      I would say that based on what I've seen in the industry, this is probably the most common way to do it (and one of the reasons why I've created this video in the first place). I'll be making a follow-up video where I will show how you can declare I/O in an alternative way where you don't use global variables.

    • @dennisc3904
      @dennisc3904 ปีที่แล้ว +1

      ​@@JakobSagatowski I look forward to this video! Variables for IO mapping are one of the few things that I use GVLs for still. Maybe because I too did training at Beckhoff Canada haha

    • @Fiddeflygare
      @Fiddeflygare ปีที่แล้ว +1

      I use a function block and structs to declare my IO mapping and link them in the systems manager to the correaponding hardware IO.
      I then link my nececcary global variables for the IO on the funtionblock instead.
      Makes it a LOT easier and also gives the benefit that you can change witch value that comes in on what io without relink anything in the system manager and it just takes an online change to the plc program 🙂👍🏻

    • @alexanderwagner4967
      @alexanderwagner4967 7 หลายเดือนก่อน

      @@JakobSagatowski its been a while since your global variable video. I really appreaciate it since i also struggle with the concept of global variables and in particular how beckhoff themselve use it in every example. We have developed an atrifical layer where we model each terminal and then have pointers on the terminals to access teh data and do the linking via pragmas. Anyhow the list of modelled terminals is still global what i dont like. Starting a new project i am now thinking again about a better concept. I am very curious how you are tackling that thing and looking forward for the promised video ;)

  • @SiBex_ovh
    @SiBex_ovh ปีที่แล้ว

    On MikroTik routers scripts are the same, each person use Global and Global.... I have no idea why all ppl use only them insted of `local` one. Ahh, people...

  • @TheEpicRandomGuy
    @TheEpicRandomGuy ปีที่แล้ว

    No 😢

  • @SakshatShinde
    @SakshatShinde ปีที่แล้ว +1

    Rust makes it very hard to have a global variable

    • @JakobSagatowski
      @JakobSagatowski  ปีที่แล้ว +1

      Haven't had the opportunity to try rust out, will definitely have to try it out!

  • @mail87523
    @mail87523 ปีที่แล้ว

    I like global variables.
    I often use them, but care should be taken about the name of the variable, which must have the prefix GV to avoid confusion in the entire program.
    Global variables must exist to be displayed by the jtag debugger as jlink or stlink .
    That way, you don't need a protocol that eats up cpu resources, while jlink doesn't eat up cpu resources, that's why communication is non-intrusive and much faster than any protocol.

  • @dd.SA32
    @dd.SA32 4 หลายเดือนก่อน +1

    Tell one popular open source project which doesn't have global variables.
    Saying don't use global variable is like saying there should no knife in this world as it a can cut someones hand.
    Just like a knife a global variables like anything else can hurt if you use it in the wrong way.
    But it doesn't justify abolition of global variables.

  • @greg4367
    @greg4367 ปีที่แล้ว +6

    "Stop using global variables!".. In your opinion. Fifty years of programming and I just do NOT agree. In my opinion. But this I'm sure of... A mindless adherence to blanket rules is SILLY.