Hi NR users,
Currently, we just set up the NR according to the instruction provided in the website. When we try to write the plugins for closed-loop neural network training, we have problems in delivering the stimulation in a real-time manner.
Experiment condition:
A oscilloscope is connected to the Stim out port of the interface board to capture the stimulation.
Successful experiments:
1. Deliver stimulation using the Mannual Stim panel. We can see the stimulation delivered. Both the waveform and time is right.
2. In the plugin attached in the following context, we put the codes in the Setup() to deliver the stimulation in several fixed time. We can see the stimulation delivered. Both the waveform and time is right.
namespace StimulationTest
{
public class Test : NRTask
{
double[] times = { 20, 25, 27 };
int[] channels = { 3, 7, 9 };
protected override void Setup()
{
for (int i = 1; i < num; i++)
{
double[] waveform = PredefinedWaveform;
double fs = NRStimSrv.SampleFrequencyHz;
Console.WriteLine("Stimulation frequency is {0}", fs);
List<StimulusOutEvent> stimuli = new List<StimulusOutEvent>();
for (int i = 0; i < times.Length; i++)
{
stimuli.Add(new StimulusOutEvent(channels[i], (ulong)(times[i] * fs), waveform));
}
NRStimSrv.StimOut.WriteToBuffer(stimuli);
}
Thread.Sleep(20000);
}
protected override void Cleanup()
{
Console.WriteLine("terminating protocal");
}
}
}
Experiment falied to deliver stimulation:
In real experiment, we need to deliver stimulation according to the responses. We wrote codes as attached below using the NRStimSrv.StimOut.GetTime() method in the Loop(). However it failed to delivered stimulation. In the following short code, it should deliver stimulation roughly every 5 s.
namespace ResponsiveElectrodeLocating
{
class ResponsiveElectrodeLocating : NRTask
{
static int ChannelNum = 64;//Total channel number\
static double Delay = 2;//A time delay to ensure the stimulation is in the future
protected override void Setup()
{
Console.WriteLine("Starting plugin");
}
protected override void Loop(object sender, EventArgs e)
{
//A delay
Console.WriteLine("Loop");
Thread.Sleep(3000);
if (StimChannelNum < ChannelNum)//Run stimulation from channel 0 to ChannelNum (31 or 63), totally 32 or 64 channels
{
//Writing the stimulation buffer for output stimulation in the near future
List<StimulusOutEvent> Stimuli = new List<StimulusOutEvent>();
double[] WaveForm = PredefinedWaveform;
double CurrentTime = NRStimSrv.StimOut.GetTime();
double StimFreq = NRStimSrv.SampleFrequencyHz;
ulong StimTime = (ulong) ((CurrentTime / 1000 + Delay) * StimFreq);
Stimuli.Add(new StimulusOutEvent(StimChannelNum, StimTime, WaveForm));
NRStimSrv.StimOut.WriteToBuffer(Stimuli);
Console.WriteLine("Stimulation channel {0} at time {1} using predifined waveform in the UI", StimChannelNum, Time);
}
else
{
Console.WriteLine("All channels are stimulated. Please click STOP to quit the plugins");
}
//Upadte stimulation channel
StimChannelNum = StimChannelNum + 1;
//Console.WriteLine("Updating the stimulation channel number to next one");
}
protected override void Cleanup()
{
Console.WriteLine("Quiting the plugins");
}
}
}
Would you please troubleshoot the problem? We will appreciate your kind help! Thank you in advance.