Communicate with custom sensors in ISR

Hello,

I am trying to integrate custom sensors (DHT11 and SGP30) to the robot’s wrist expansion header. Following the tutorial tutorial_data_transfer, I wrote a function to get values from the sensors which was called by stepWaccController_1000Hz() in Wacc.cpp. When testing, I found that the libraries from Adafruit (DHT sensor library and Adafruit SGP30 Library) used delay() to set the sensors ready for data transaction, and since stepWaccController_1000Hz() was in ISR, delay() caused the cpu to stall. I tried to remove all the delays but it seemed that sensors depended on them to init properly.

Is there a way to avoid this issue?

Hi @NewWheat ,

Thank you for your post. Could you share your code or email us at support@hello-robot.com, if you wish to keep it private, for better context? Thank you.

Without knowing more, here are some suggestions:

  1. You can extend the TimeManager class to implement your own delay logic. Basically poll the current_time_us(): stretch_firmware/arduino/hello_wacc/TimeManager.cpp at master · hello-robot/stretch_firmware · GitHub and setup logic around this.

  2. You can easily implement a non-blocking delay by skipping the read from your sensors until a specific time interval has passed:

unsigned long previousMillis = 0;
const long interval = 1000; // interval at which to do something (milliseconds) - this is the same value as your delay()

void read_from_sensors() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // Perform the read from sensor action

    previousMillis = currentMillis;
  }

}