Menu

Guide to Debugging a Slow Sketch

We've seen issues recently involving TAMProxy Sketch code running with long delay (hardware takes time to respond, and the delay gets longer and longer over time). This suggests that your communication queue between the NUC and the Teensy is backed up. Here are a few common reasons, and ways to debug them:

SyncedSketch

If you have a strange timing profile within your loop() method, you may see issues with deriving from the SyncedSketch class, as it tries to synchronize with the Teensy's loop while running loop(). In this case, you may want to fall back to the normal Sketch class, or fix your loop() method (see below).

Hogging the NUC processor in your loop()

You might see issues if you are running processor-intensive code within your Sketch loop() method:

  • You might be running a really tight loop, like the loop below. This looks innocent but actually eats up a bunch of CPU cycles while doing nothing.
    while timer.millis() < 2000:
    pass

    Instead, you probably want to have your loop() method give up control very quickly. Just do the check every loop and then return if it fails:

    def loop():
      if timer.millis() < 2000:
        return
      else:
        # Send some commands
        # Maybe set another timer
        # Don't take lots of processing time!

    You also might not need to do your loop() code every time you are given control. You can use a timer to only accept the loop every 10ms (as a suggestion):

    def loop():
      if global_timer.millis() > 10:
          # Do loop stuff
          global_timer.reset()
      else:
          return
  • You might be doing heavy processing within loop(), such as heavy-duty image processing. Instead you probably want to move this into another ROS node, and let it be managed as an entirely separate process.

Lots of communication with the Teensy

As you add more devices, you will inevitably just have more and more communication with the Teensy. If you are following the suggestions above and still have issues, you may want to edit tamproxy/config.yaml and increase the default_sleep_duration. This is the amount of time TAMProxy naturally sleeps the loop() thread, to give the communication thread time to send packets to the Teensy.