Interference current stimulation program with kaji-lab ES device.

Dependencies:   mbed SerialInputReactionHandler AMPulseTrain SwArr16MOSFET StrCommandHandler KajiLabES

Files at this revision

API Documentation at this revision

Comitter:
aktk
Date:
Mon Jan 06 21:08:31 2020 +0000
Parent:
2:5cb68cc8ecaa
Child:
4:8d73aaba879f
Commit message:
derived from interference

Changed in this revision

AMPulseTrain.lib Show annotated file Show diff for this revision Revisions of this file
DSinGenerator.lib Show diff for this revision Revisions of this file
SwArr16MOSFET.lib Show annotated file Show diff for this revision Revisions of this file
subroutines.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AMPulseTrain.lib	Mon Jan 06 21:08:31 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/aktk/code/AMPulseTrain/#7d5afb2e3b79
--- a/DSinGenerator.lib	Wed Nov 27 23:41:55 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/aktk/code/DSinGenerator/#6400e338266f
--- a/SwArr16MOSFET.lib	Wed Nov 27 23:41:55 2019 +0000
+++ b/SwArr16MOSFET.lib	Mon Jan 06 21:08:31 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/aktk/code/SwArr16MOSFET/#8af1b775e1dc
+https://os.mbed.com/users/aktk/code/SwArr16MOSFET/#25de14870b6f
--- a/subroutines.cpp	Wed Nov 27 23:41:55 2019 +0000
+++ b/subroutines.cpp	Mon Jan 06 21:08:31 2020 +0000
@@ -4,22 +4,64 @@
 
 #include "SwArr16MOSFET.h"
 
-#include "DSinGenerator.h"
 #include "StrCommandHandler.h"
 #include "SerialInputReactionHandler.h"
 
+#include "AMPulseTrain.h"
+
 Serial pc(USBTX, USBRX, 921600); // tx, rx
 Ticker ticker;
 KajiLabES   stimulator;
-SwArr16MOSFET swBrd(4, p11, p12, p13, p14, p10);
-DSinGenerator singen ( 
-    /*Initial Amplitude*/ 0,
-    /*Initial Frequency*/ 4000,
-    /*Discret.-fineness*/ 16
+static int const nofch = 8;
+SwArr16MOSFET swBrd(nofch, p14, p13, p12, p11, p10);
+
+void callback_PulseEdge(bool arg_pulsestate, PulseTrain*, AMSignal*);
+uint16_t callback_AMSignalExpression (PulseTrain*, AMSignal* arg_SignalModel);
+AMPulseTrain ampt (
+    new PulseTrain(20000, 1.0, 100000),
+    new AMSignal(50, 200)
 );
+
 StrCommandHandler command_handler;
-SerialInputReactionHandler reactor; //todo: implement function replace sigbind.cpp/h
+SerialInputReactionHandler reactor;
+
+static const int g_sample_size_imp = 100;
+uint16_t g_retval[g_sample_size_imp];
+
+/// Wrapper to be attached to PulseTrain's Callback as pulse rising
+void callback_PulseEdge(bool arg_pulsestate, AMPulseTrain* arg_ampt)
+{
+    static int ch = 0;
+    static int itr_sample = 0;
 
+    g_retval[itr_sample] = stimulator.DAAD(
+                               arg_ampt->Signal->getAMSinalValue() * arg_pulsestate
+                           );
+    swBrd.setTwin(ch + 1, ((ch + nofch/2) % nofch) + 1);
+    //swBrd.setTwin(ch + 1, ((ch + nofch/2 - 1) % nofch) + 1);
+    ch = (ch + 3) % nofch;
+    if (ch+1 == 2) ch = 3 -1;
+    //if (ch+1 == 2) ch = 6 -1;
+    //if (ch+1 == 6) ch = 2 -1;
+    //if (ch+1 == 5) ch = 2 - 1;
+    //if (ch+1 == 6) ch = 1 - 1;
+
+
+    if(arg_pulsestate == 1) {
+        itr_sample = (itr_sample + 1) % g_sample_size_imp;
+    }
+}
+
+uint16_t callback_AMSignalExpression (AMPulseTrain* arg_ampt)
+{
+    static int itr = -1;
+    uint16_t ph = (itr < arg_ampt->getPWidth_pPulse_Signal())
+                  ? arg_ampt->Signal->getAmplitude_u16()
+                  : 0;
+
+    itr = (itr + 1) % arg_ampt->getPeriod_pPulse_Signal();
+    return ph;
+}
 
 void init (void)
 {
@@ -32,10 +74,12 @@
     }
     myled1 = 1;
 
-    NVIC_SetPriority(TIMER3_IRQn, 1);
+    NVIC_SetPriority(TIMER3_IRQn, 255);
 
     //void init_commands(void)
     {
+        ampt.attachCallback_asPulseEdge(callback_PulseEdge);
+        ampt.attachAMSignalExpression(callback_AMSignalExpression);
         command_handler.map("a",    &switchState);
         command_handler.map("E",    &terminateLoop);
         command_handler.map("S",    &startLoop);
@@ -51,45 +95,12 @@
         reactor.startReception(&pc, SerialInputReactionHandler::KB_SINGLE_INPUT);
     }
     printKBManual();
-    printDSinSamples();
     wait(.1);
     myled2 = 1;
 }
 
 
 
-void loop()
-{
-    static int ph = 0;
-    static int ph_sign = 0;
-
-    myled4 = 1;
-
-    ph = singen.getValue_p16m16();
-    if(ph > 0) {
-        ph_sign = 1;
-    }
-    //  when ph is negaive, make it positive
-    else if (ph < 0) {
-        ph = -ph;
-        ph_sign = -1;
-    }
-    stimulator.DAAD(ph);
-
-    if(ph == 0) {
-        if (ph_sign > 0)
-            swBrd.setTwin(1, 4);
-
-        else if (ph_sign < 0)
-            swBrd.setTwin(4, 1);
-        else;
-        ph_sign = 0;
-    }
-
-    myled4 = 0;
-}
-
-
 
 void calleddefault(char const * const arg_command, void * arg_errorcode)
 {
@@ -116,7 +127,10 @@
 {
     if (pstate != WAIT_A_CERTAIN_KEY) return NULL;
 
-    ticker.attach_us(loop, singen.getPulseWidth());
+    ticker.attach_us(
+        callback(&ampt, &AMPulseTrain::incrementClock),
+        ampt.getClockperiod_us()
+    );
     pstate = MAIN_ROUTINE;
     pc.printf("start\n");
     return NULL;
@@ -150,12 +164,12 @@
 
 void * increaseCurrent(void)
 {
-    float lampl = singen.getAmplitude();
-    if(lampl < singen.ampl_max - 0.5)
-        lampl += 0.5;
+    float lampl = ampt.Signal->getAmplitude_uf();
+    if(lampl < 0.95)
+        lampl += 0.05;
     else
-        lampl = singen.ampl_max;
-    singen.setAmplitude(lampl);
+        lampl = 1.0;
+    ampt.Signal->setAmplitude(lampl);
     printStatus();
     return NULL;
 }
@@ -164,12 +178,12 @@
 
 void * decreaseCurrent(void)
 {
-    float lampl = singen.getAmplitude();
-    if(1 <= lampl )
-        lampl -= 0.5;
+    float lampl = ampt.Signal->getAmplitude_uf();
+    if(0.05 <= lampl )
+        lampl -= 0.05;
     else
         lampl = 0;
-    singen.setAmplitude(lampl);
+    ampt.Signal->setAmplitude(lampl);
     printStatus();
     return NULL;
 }
@@ -178,13 +192,18 @@
 
 void * increaseFrequency(void)
 {
-    uint16_t lfreq = singen.getFrequency();
-    if(lfreq < singen.freq_max - 10)
-        lfreq += 10;
+    uint32_t lfreq = ampt.Carrier->getFrequency();
+    if(lfreq < ampt.Carrier->FREQ_MAX - 100)
+        lfreq += 100;
     else
-        lfreq  = 5000;
-    singen.setFrequency(lfreq);
-    ticker.attach_us(loop, singen.getPulseWidth());
+        lfreq  = ampt.Carrier->FREQ_MAX;
+    ampt.setFrequency_Carrier(lfreq);
+    printf("%d\n", ampt.getClockperiod_us());
+    if(pstate == MAIN_ROUTINE)
+        ticker.attach_us(
+            callback(&ampt, &AMPulseTrain::incrementClock),
+            ampt.getClockperiod_us()
+        );
     printStatus();
     return NULL;
 }
@@ -193,13 +212,17 @@
 
 void * decreaseFrequency(void)
 {
-    uint16_t lfreq = singen.getFrequency();
-    if(20 <= lfreq )
-        lfreq -= 10;
+    uint32_t lfreq = ampt.Carrier->getFrequency();
+    if(200 <= lfreq )
+        lfreq -= 100;
     else
-        lfreq = 10;
-    singen.setFrequency(lfreq);
-    ticker.attach_us(loop, singen.getPulseWidth());
+        lfreq = 100;
+    ampt.setFrequency_Carrier(lfreq);
+    if(pstate == MAIN_ROUTINE)
+        ticker.attach_us(
+            callback(&ampt, &AMPulseTrain::incrementClock),
+            ampt.getClockperiod_us()
+        );
     printStatus();
     return NULL;
 }
@@ -257,14 +280,14 @@
 
 void * SetAmplitude(char const * const arg_digits)
 {
-    float lampl = singen.getAmplitude();
+    float lampl = 10.0 * ampt.Signal->getAmplitude_uf();
 
     //  read values
-    if (scanValue(arg_digits, &lampl));
+    if (scanValue(arg_digits, &lampl));//max 10mA
 
     //  set values
-    if( lampl <   0 || singen.ampl_max < lampl) lampl = singen.getAmplitude();
-    singen.setAmplitude(lampl);
+    if( lampl < 0.0 || 10.0 < lampl) lampl = 10.0 * ampt.Signal->getAmplitude_uf();
+    ampt.Signal->setAmplitude((float)(lampl / 10.0));
 
     pc.puts("===\n");
     pc.printf("set frequency:");
@@ -274,12 +297,12 @@
 
 void * SetFrequency(char const * const arg_digits)
 {
-    uint16_t lfreq = singen.getFrequency();
+    uint32_t lfreq = ampt.Carrier->getFrequency();
 
     if (scanValue(arg_digits, &lfreq));
 
-    if( lfreq < 500 || singen.freq_max < lfreq) lfreq = singen.getFrequency();
-    singen.setFrequency(lfreq);
+    if( lfreq < 500 || ampt.Carrier->FREQ_MAX < lfreq) lfreq = ampt.Carrier->getFrequency();
+    ampt.Carrier->setFrequency(lfreq);
 
     pc.puts("...\n");
     printStatus();
@@ -301,7 +324,7 @@
 {
     pc.puts(
         "\n"
-        "keybind are following:\n"
+        "keybind:\n"
         "    ---\n"
         "    v: set all paramater by arbitrary value\n"
         "    ---\n"
@@ -322,23 +345,19 @@
 
 void * printStatus(void)
 {
+    unsigned int l_max = 0, l_min = 0, l_mean = 0;
+    for (int i = 0; i < g_sample_size_imp; i++) {
+        l_mean += g_retval[i];
+        if (l_max < g_retval[i]) l_max = g_retval[i];
+        if (l_min > g_retval[i]) l_min = g_retval[i];
+    }
+    l_mean /= g_sample_size_imp;
     pc.printf(
-        "Amplitude:  %2.3f  "
-        "Frequency:  %05d  "
-        "Pulse Width:%05d\n"
-        , singen.getAmplitude(), singen.getFrequency(), singen.getPulseWidth());
+        "Signal Amplitude:   %3.3f "
+        "Carrier Frequency:  %06d  \n"
+        "Z(min, mean, max)=(%d, %d, %d)\n"
+        , 10.0 * ampt.Signal->getAmplitude_uf(), ampt.Carrier->getFrequency()
+        , l_min, l_mean, l_max);
     return NULL;
 }
 
-void * printDSinSamples(void)
-{
-    pc.printf("resolution of sin: %d\n", singen.resolution_ofsin);
-
-    float dsin[singen.resolution_ofsin];
-    singen.getValueofSamplePoints(dsin);
-    for ( int i = 0; i < singen.resolution_ofsin; i++ )
-        pc.printf("sin[%02d]: %2.2f\n", i, dsin[i]);
-
-    return NULL;
-}
-