Isolate Your Wait Statements

Have you ever looked at a TP program and gotten lost following all the LBLs and JMPs? Have you ever gotten lazy handling your WAIT-statement 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.

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).

The 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 LBL[1] for the top of the program and LBL[500] for 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[2] 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.