fast-feedback virtual target task code on STM Nucleo

Dependencies:   mbed

Committer:
gwappa
Date:
Mon Oct 01 15:16:04 2018 +0000
Revision:
30:5f975f572ffb
Parent:
29:1fb060aab1f8
Child:
31:b320ca61a8c0
add trial index for test trials

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gwappa 4:fcf597f82632 1 #include "states.h"
gwappa 4:fcf597f82632 2 #include "rig.h"
gwappa 4:fcf597f82632 3 #include "automaton.h"
gwappa 27:b31ea8d74f9e 4 #include "scheduler.h"
gwappa 4:fcf597f82632 5 #include "IO.h"
gwappa 4:fcf597f82632 6
gwappa 4:fcf597f82632 7 #define STATE_IS_LOGGED
gwappa 4:fcf597f82632 8
gwappa 4:fcf597f82632 9 #ifdef STATE_IS_LOGGED
gwappa 4:fcf597f82632 10 #define LOGSTATE(S) IO::info(#S);
gwappa 4:fcf597f82632 11 #else
gwappa 4:fcf597f82632 12 #define LOGSTATE(S)
gwappa 4:fcf597f82632 13 #endif
gwappa 4:fcf597f82632 14
gwappa 4:fcf597f82632 15 void finalize() {
gwappa 29:1fb060aab1f8 16 trial.markTrialEnd(task);
gwappa 27:b31ea8d74f9e 17 scheduler::reset();
gwappa 4:fcf597f82632 18 }
gwappa 4:fcf597f82632 19
gwappa 4:fcf597f82632 20 void Delay::setup() {
gwappa 4:fcf597f82632 21 LOGSTATE(Delay)
gwappa 4:fcf597f82632 22
gwappa 4:fcf597f82632 23 // initialize the trial-related params
gwappa 4:fcf597f82632 24 trial.reset(task);
gwappa 4:fcf597f82632 25
gwappa 11:897ecd5413e0 26 // set up the timeout for the next state
gwappa 9:e136394bdb39 27 switch (task.mode.value) {
gwappa 28:797536a42b9f 28 case Condition:
gwappa 29:1fb060aab1f8 29 scheduler::set(ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value), &automaton::jump<Delay,Paired>);
gwappa 28:797536a42b9f 30 break;
gwappa 16:33c17c62840e 31 case MotionAlt:
gwappa 27:b31ea8d74f9e 32 scheduler::set(ms_to_us(trial.delay_dur_ms + task.prep_dur_ms.value), &automaton::jump<Delay,Cued>);
gwappa 16:33c17c62840e 33 break;
gwappa 9:e136394bdb39 34 default:
gwappa 27:b31ea8d74f9e 35 scheduler::set(ms_to_us(trial.delay_dur_ms), &automaton::jump<Delay,Prepare>);
gwappa 7:6744ec9ccc25 36 }
gwappa 4:fcf597f82632 37
gwappa 11:897ecd5413e0 38 trial.markTrialStart();
gwappa 4:fcf597f82632 39 }
gwappa 4:fcf597f82632 40
gwappa 4:fcf597f82632 41 void Delay::teardown() {
gwappa 4:fcf597f82632 42 // do nothing
gwappa 4:fcf597f82632 43 }
gwappa 4:fcf597f82632 44
gwappa 29:1fb060aab1f8 45 void Paired::setup() {
gwappa 29:1fb060aab1f8 46 LOGSTATE(Paired)
gwappa 28:797536a42b9f 47
gwappa 28:797536a42b9f 48 visualOut.run();
gwappa 29:1fb060aab1f8 49 trial.flag.cued = true;
gwappa 29:1fb060aab1f8 50 visualOut.attachTurnOffCallback(&automaton::jump<Paired,Monitor>);
gwappa 28:797536a42b9f 51 }
gwappa 28:797536a42b9f 52
gwappa 29:1fb060aab1f8 53 void Paired::teardown() {
gwappa 28:797536a42b9f 54 visualOut.detachTurnOffCallback();
gwappa 28:797536a42b9f 55 trial.markEndOfWait();
gwappa 28:797536a42b9f 56 }
gwappa 28:797536a42b9f 57
gwappa 29:1fb060aab1f8 58 void Monitor::setup() {
gwappa 29:1fb060aab1f8 59 LOGSTATE(Monitor)
gwappa 28:797536a42b9f 60
gwappa 30:5f975f572ffb 61 if (trial.flag.rewarded == true) {
gwappa 30:5f975f572ffb 62 rewardOut.run();
gwappa 30:5f975f572ffb 63 }
gwappa 29:1fb060aab1f8 64 lickIn.attach(&automaton::jump<Monitor,WithResp>);
gwappa 29:1fb060aab1f8 65 scheduler::set(ms_to_us(task.resp_dur_ms.value), &automaton::jump<Monitor,NoResp>);
gwappa 28:797536a42b9f 66 }
gwappa 28:797536a42b9f 67
gwappa 29:1fb060aab1f8 68 void Monitor::teardown() {
gwappa 29:1fb060aab1f8 69 lickIn.detach();
gwappa 28:797536a42b9f 70 rewardOut.wait();
gwappa 28:797536a42b9f 71 }
gwappa 28:797536a42b9f 72
gwappa 7:6744ec9ccc25 73 void Prepare::setup() {
gwappa 7:6744ec9ccc25 74 // mostly the same with for Delay
gwappa 7:6744ec9ccc25 75 // except that the animal cannot lick freely
gwappa 7:6744ec9ccc25 76 LOGSTATE(Prepare)
gwappa 7:6744ec9ccc25 77
gwappa 7:6744ec9ccc25 78 // configure the interrupts
gwappa 26:b4421d1ee57a 79 lickIn.attach(&automaton::jump<Prepare,Abort>);
gwappa 7:6744ec9ccc25 80
gwappa 7:6744ec9ccc25 81 // set timeout for the next state
gwappa 27:b31ea8d74f9e 82 scheduler::set(ms_to_us(task.prep_dur_ms.value), &automaton::jump<Prepare,Cued>);
gwappa 7:6744ec9ccc25 83 }
gwappa 7:6744ec9ccc25 84
gwappa 7:6744ec9ccc25 85 void Prepare::teardown() {
gwappa 7:6744ec9ccc25 86 // de-register lick inhibition
gwappa 26:b4421d1ee57a 87 lickIn.detach();
gwappa 9:e136394bdb39 88 }
gwappa 9:e136394bdb39 89
gwappa 4:fcf597f82632 90 void Cued::setup() {
gwappa 4:fcf597f82632 91 LOGSTATE(Cued)
gwappa 4:fcf597f82632 92
gwappa 11:897ecd5413e0 93 trial.markEndOfWait();
gwappa 7:6744ec9ccc25 94
gwappa 21:e51733fc1c36 95 switch (task.mode.value) {
gwappa 21:e51733fc1c36 96 case Report:
gwappa 21:e51733fc1c36 97 case Associate:
gwappa 27:b31ea8d74f9e 98 case Motion:
gwappa 21:e51733fc1c36 99 // wait for the visual cue to flag "cued"
gwappa 27:b31ea8d74f9e 100 // use visual feedback trigger as the "gate" response
gwappa 17:0b241aa1f5b6 101 // licking without a visual "go-cue" will be considered a "catch" response
gwappa 27:b31ea8d74f9e 102 gateIn.attach(&Cued::gate);
gwappa 26:b4421d1ee57a 103 lickIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 17:0b241aa1f5b6 104 break;
gwappa 17:0b241aa1f5b6 105 case MotionAlt:
gwappa 27:b31ea8d74f9e 106 // jumps directly to WithResp with the visual cue
gwappa 27:b31ea8d74f9e 107 Cued::gate();
gwappa 26:b4421d1ee57a 108 gateIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 26:b4421d1ee57a 109 break;
gwappa 7:6744ec9ccc25 110 }
gwappa 7:6744ec9ccc25 111
gwappa 7:6744ec9ccc25 112 // start cue output
gwappa 21:e51733fc1c36 113 visualOut.run();
gwappa 21:e51733fc1c36 114 audioOut.run();
gwappa 4:fcf597f82632 115
gwappa 4:fcf597f82632 116 // sets the timeout for the next state
gwappa 29:1fb060aab1f8 117 scheduler::set(ms_to_us(task.aud_dur_ms.value), &automaton::jump<Cued,NoResp>);
gwappa 4:fcf597f82632 118 }
gwappa 4:fcf597f82632 119
gwappa 7:6744ec9ccc25 120 void Cued::gate() {
gwappa 26:b4421d1ee57a 121 gateIn.detach();
gwappa 26:b4421d1ee57a 122 lickIn.attach(&automaton::jump<Cued,WithResp>);
gwappa 27:b31ea8d74f9e 123 trial.flag.cued = true; // in case it has not been (i.e. other than MotionAlt)
gwappa 7:6744ec9ccc25 124 }
gwappa 7:6744ec9ccc25 125
gwappa 4:fcf597f82632 126 void Cued::teardown() {
gwappa 26:b4421d1ee57a 127 lickIn.detach();
gwappa 27:b31ea8d74f9e 128 gateIn.detach();
gwappa 20:4c06d3041337 129
gwappa 7:6744ec9ccc25 130 // end cue output
gwappa 20:4c06d3041337 131 switch (task.mode.value) {
gwappa 20:4c06d3041337 132 case Report:
gwappa 20:4c06d3041337 133 case Associate:
gwappa 12:06ea96546af1 134 visualOut.stop();
gwappa 7:6744ec9ccc25 135 }
gwappa 20:4c06d3041337 136
gwappa 20:4c06d3041337 137 switch (task.mode.value) {
gwappa 20:4c06d3041337 138 case Associate:
gwappa 20:4c06d3041337 139 case Motion:
gwappa 20:4c06d3041337 140 case MotionAlt:
gwappa 20:4c06d3041337 141 audioOut.stop();
gwappa 20:4c06d3041337 142 }
gwappa 7:6744ec9ccc25 143 }
gwappa 7:6744ec9ccc25 144
gwappa 7:6744ec9ccc25 145 void Abort::setup() {
gwappa 7:6744ec9ccc25 146 LOGSTATE(Abort)
gwappa 26:b4421d1ee57a 147 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 148 gateIn.detach(); // if any
gwappa 11:897ecd5413e0 149 trial.markEndOfWait();
gwappa 19:50663f8815b8 150 trial.flag.reset = true;
gwappa 27:b31ea8d74f9e 151 scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<Abort>);
gwappa 7:6744ec9ccc25 152 }
gwappa 7:6744ec9ccc25 153
gwappa 7:6744ec9ccc25 154 void Abort::teardown() {
gwappa 7:6744ec9ccc25 155 finalize();
gwappa 4:fcf597f82632 156 }
gwappa 4:fcf597f82632 157
gwappa 4:fcf597f82632 158 void WithResp::setup() {
gwappa 4:fcf597f82632 159 LOGSTATE(WithResp)
gwappa 4:fcf597f82632 160
gwappa 19:50663f8815b8 161 trial.flag.responded = true;
gwappa 26:b4421d1ee57a 162 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 163 gateIn.detach(); // if any
gwappa 4:fcf597f82632 164
gwappa 30:5f975f572ffb 165 if (task.mode.value == Condition) {
gwappa 30:5f975f572ffb 166 if (!trial.flag.rewarded) {
gwappa 30:5f975f572ffb 167 rewardOut.start();
gwappa 30:5f975f572ffb 168 }
gwappa 30:5f975f572ffb 169 } else if (trial.flag.cued) {
gwappa 11:897ecd5413e0 170 rewardOut.start();
gwappa 9:e136394bdb39 171 }
gwappa 4:fcf597f82632 172
gwappa 27:b31ea8d74f9e 173 scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<WithResp>);
gwappa 4:fcf597f82632 174 }
gwappa 4:fcf597f82632 175
gwappa 4:fcf597f82632 176 void WithResp::teardown() {
gwappa 4:fcf597f82632 177 finalize();
gwappa 4:fcf597f82632 178 }
gwappa 4:fcf597f82632 179
gwappa 4:fcf597f82632 180 void NoResp::setup() {
gwappa 4:fcf597f82632 181 LOGSTATE(NoResp)
gwappa 26:b4421d1ee57a 182 lickIn.detach(); // if any
gwappa 26:b4421d1ee57a 183 gateIn.detach(); // if any
gwappa 4:fcf597f82632 184
gwappa 27:b31ea8d74f9e 185 scheduler::set(ms_to_us(task.post_dur_ms.value), &automaton::done<NoResp>);
gwappa 4:fcf597f82632 186 }
gwappa 4:fcf597f82632 187 void NoResp::teardown() {
gwappa 4:fcf597f82632 188 finalize();
gwappa 8:973dcd190672 189 }
gwappa 8:973dcd190672 190
gwappa 8:973dcd190672 191 void TestReward::setup() {
gwappa 8:973dcd190672 192 LOGSTATE(TestReward)
gwappa 8:973dcd190672 193
gwappa 8:973dcd190672 194 // open/close the valve
gwappa 16:33c17c62840e 195 rewardOut.setOnset(0);
gwappa 16:33c17c62840e 196 rewardOut.setDuration(ms_to_us(task.reward_dur_ms.value));
gwappa 11:897ecd5413e0 197 rewardOut.start();
gwappa 27:b31ea8d74f9e 198
gwappa 27:b31ea8d74f9e 199 scheduler::set(ms_to_us(task.reward_dur_ms.value+100), &automaton::done<TestReward>);
gwappa 8:973dcd190672 200 }
gwappa 8:973dcd190672 201
gwappa 8:973dcd190672 202 void TestReward::teardown() {
gwappa 8:973dcd190672 203 // do nothing
gwappa 30:5f975f572ffb 204 }
gwappa 30:5f975f572ffb 205
gwappa 30:5f975f572ffb 206 void ClearTrialIndex::setup() {
gwappa 30:5f975f572ffb 207 trial.index = 1;
gwappa 30:5f975f572ffb 208 }
gwappa 30:5f975f572ffb 209
gwappa 30:5f975f572ffb 210 void ClearTrialIndex::teardown() {
gwappa 30:5f975f572ffb 211 // do nothing
gwappa 30:5f975f572ffb 212 }