Have you ever looked at a TP program and gotten lost following all the
JMPs? Have you ever gotten lazy handling your
TIMEOUTs because you didn’t want to bother adding the extra stuff to your main routine?
If you’re nodding “yes” right now, you should try isolating your
WAIT-statements into their own small programs.
In my eight-plus years of programming robots and troubleshooting other people’s code, I’ve found that a mess of labels and lack of refactoring large programs into smaller routines is the single biggest cause of issues.
Here’s an example of how you might code a machine unload program:
! unload_machine.ls LBL ; WAIT (DI[1:machine ready]) TIMEOUT,LBL ; ; ! go ahead and unload the machine ; ! grip part ; LBL ; WAIT (RI[1:part gripped]) TIMEOUT,LBL ; ! ok part is gripped, retreat and end the routine ; END ; ; LBL ; ! machine not ready timeout ; UALM ; JMP LBL ; ; LBL ; ! grip fail ; UALM ; JMP LBL ;
Among other things, I see two glaring problems here:
- There are FOUR labels in this program. I would argue that unless you’re handling a large
SELECTconditional, you should probably have two or fewer.
- The error handling parts of the program are far removed from the actual errors themselves, and they have to remember where to jump back to once the error has been acknowledged and (hopefully) fixed.
Here’s how I would fix the issue:
! machine_unload.ls ; CALL WAIT_MACHINE_READY ; ! go ahead and unload the machine ; ! grip part ; CALL WAIT_PART_GRIPPED ; ! ok part is gripped, retreat and end the routine ;
! wait_machine_ready.ls ; LBL ; WAIT (DI[1:machine ready]) TIMEOUT,LBL ; END ; ; LBL ; ! machine ready timeout ; UALM ; JMP LBL ;
! wait_part_gripped.ls ; LBL ; WAIT (RI[1:part gripped]) TIMEOUT,LBL ; END ; ; LBL ; ! grip fail ; UALM ; JMP LBL ;
We’ve made a number of improvements:
- The main routine now has zero labels. In my opinion, the fewer the better.
- The main routine is highly readable and has nothing unrelated to the actual function to the routine (e.g. gripper error handling).
wait_*utility routines have only 7 lines of code and contain only 2 labels.
- Since the different error conditions are isolated to their own unique TP programs, we can re-use
LBLfor the top of the program and
LBLfor the error itself– no cognitive overhead there.
In the first version it would be very easy to accidently jump to the wrong part of the program when an error is acknowledged or lose track of why that
LBL even exists. Do you think you’d make the same mistake if you could see the entire program in your editor at once?
Give it a try. Start isolating your
WAIT-statements into tiny programs and see how you like it. I bet you’ll get hooked and start thinking about all the other logic you can extract from your main routines.