shared function for closeloop experiment