Changeset 12047 for trunk


Ignore:
Timestamp:
09/08/11 21:57:11 (13 years ago)
Author:
tbretz
Message:
Changed name of mapping file; added possibility to set control loop parameters; added sending result; preliminarily fixed the control loop paremetrs to something which is working.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/feedback.cc

    r12028 r12047  
    5353        kStateConnected,
    5454        kStateRunning,
     55        kStateControlling,
    5556    };
    5657
     
    7374    vector<vector<float>> fData;
    7475
    75     int fCursor;
     76    uint64_t fCursor;
    7677
    7778    Time fBiasLast;
     
    8687    double fKd;               // Derivative   constant
    8788
     89    bool fOutputEnabled;
     90
    8891    pair<Time, int> GetNewState(DimStampedInfo &info) const
    8992    {
     
    108111        fSP = valarray<double>(0., 416);
    109112
     113        vector<float> vec(2*BIAS::kNumChannels);
     114        fDimDeviation.Update(vec);
     115
    110116        fPV[0].resize(0);
    111117        fPV[1].resize(0);
    112118        fPV[2].resize(0);
     119
     120        if (fKp==0 && fKi==0 && fKd==0)
     121            Warn("Control loop parameters are all set to zero.");
    113122    }
    114123
     
    165174                                               reinterpret_cast<float*>(curr->getData())+1440);
    166175
    167 
    168             ostringstream out;
    169             out << "New event received: " << fCursor;
    170             Info(out);
    171176
    172177            fCursor++;
     
    248253                const Time tm0 = Time();
    249254
    250                 const double T21 = (tm0-fStartTime).total_microseconds()/1000000.;
     255                /*const*/ double T21 = (tm0-fStartTime).total_microseconds()/1000000.;
    251256                const double T10 = fT;
    252257                fT = T21;
     
    254259                fStartTime = tm0;
    255260
    256                 cout << "Step..." << endl;
     261                ostringstream out;
     262                out << "New " << fData.size() << " event received: " << fCursor << " / " << setprecision(3) << T21 << "s";
     263                Info(out);
    257264
    258265                if (fPV[0].size()==0)
     
    284291                                return;
    285292
    286                             cout << "Calculating (" << fCursor << ")... " << endl;
     293                            cout << "Calculating (" << fCursor << ":" << T21 << ")... " << endl;
    287294
    288295                            // fKi[j] = response[j]*gain;
     
    290297                            // Kd = 0;
    291298
    292                             //                                                                                  (PV[2]-PV[1] + PV[0]-PV[1])
     299                            //                    -110 / -110 (-23 DAC / -0.51V)
     300                            // Reference voltage: -238 / -203
     301                            //                    -360 / -343 ( 23 DAC /  0.51V)
     302
     303                            // 0.005 A/V
     304                            // 220 Amplitude / 1V
     305
     306                            // Gain = 1V / 200 = 0.005
     307
     308                            // => Kp = 0.01 * gain     = 0.00005
     309                            // => Ki = 0.8  * gain/20s = 0.00025
     310                            // => Kd = 0.1  * gain/20s = 0.00003
     311
     312                            fKp = 0;
     313                            fKd = 0;
     314                            fKi = 0.00003*20;
     315                            T21 = 1;
     316
    293317                            //valarray<double> correction = - Kp*(PV[2] - PV[1]) + Ki * dT * (SP-PV[2]) - Kd/dT * (PV[2] - 2*PV[1] + PV[0]);
    294318                            //valarray<double> correction =
    295319                            //    - Kp*(PV[2] - PV[1]) + Ki * T21 * (SP-PV[2]) - Kd*(PV[2]-PV[1])/T21 - Kd*(PV[0]-PV[1])/T01;
    296320                            const valarray<double> correction =
    297                                 - (fKp+fKd/T21)*(fPV[2] - fPV[1]) + fKi*T21*(fSP-fPV[2]) + fKd/T10*(fPV[1]-fPV[0]);
     321                                - (fKp+fKd/T21)*(fPV[2] - fPV[1])
     322                                +  fKi*T21*(fSP-fPV[2])
     323                                +  fKd/T10*(fPV[1]-fPV[0]);
    298324
    299325                            vector<float> vec(2*BIAS::kNumChannels);
    300326                            for (int i=0; i<BIAS::kNumChannels; i++)
    301                                 vec[i] = fSP[i]-fPV[2][i];
     327                                vec[i] = fPV[2][i]-fSP[i];
    302328
    303329                            for (int i=0; i<BIAS::kNumChannels; i++)
     
    305331
    306332                            fDimDeviation.Update(vec);
     333
     334                            if (fOutputEnabled)
     335                            {
     336                                Info("Sending correction to feedback.");
     337
     338                                dic_cmnd_service("BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
     339                                                 (void*)(vec.data()+416), 416*sizeof(float));
     340
     341                                /*
     342                                if (!Dim::SendCommand("BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
     343                                                      (const void*)(vec.data()+416), 416*sizeof(float)))
     344                                {
     345                                    Error("Sending correction to bias control failed... switching off.");
     346                                    fOutputEnabled=false;
     347                                }
     348                                else
     349                                   Info("Success!");
     350                                */
     351                            }
    307352                        }
    308353
     
    383428    {
    384429        if (!CheckEventSize(evt.GetSize(), "SetConstant", 8))
    385             return T::kSM_FatalError;
     430            return kSM_FatalError;
    386431
    387432        switch (constant)
    388433        {
    389         case 0: fKi = evt.GetDouble(); break,
     434        case 0: fKi = evt.GetDouble(); break;
    390435        case 1: fKp = evt.GetDouble(); break;
    391436        case 2: fKd = evt.GetDouble(); break;
    392437        default:
    393438            Fatal("SetConstant got an unexpected constant id -- this is a program bug!");
    394         }
     439            return kSM_FatalError;
     440        }
     441
     442        return GetCurrentState();
     443    }
     444
     445    int EnableOutput(const EventImp &evt)
     446    {
     447        if (!CheckEventSize(evt.GetSize(), "EnableOutput", 1))
     448            return kSM_FatalError;
     449
     450        fOutputEnabled = evt.GetBool();
    395451
    396452        return GetCurrentState();
     
    472528        fDimReference("FEEDBACK/REFERENCE", "F:416",        ""),
    473529        fDimDeviation("FEEDBACK/DEVIATION", "F:416;F:416",  ""),
    474         fBiasData(NULL)
     530        fBiasData(NULL), fKp(0), fKi(0), fKd(0), fOutputEnabled(false)
    475531    {
    476532        // ba::io_service::work is a kind of keep_alive for the loop.
     
    505561            ("");
    506562
     563        AddEvent("ENABLE_OUTPUT", "B:1")//, kStateIdle)
     564            (bind(&StateMachineFeedback::EnableOutput, this, placeholders::_1))
     565            ("");
     566
    507567        AddEvent("STORE_REFERENCE")//, kStateIdle)
    508568            (bind(&StateMachineFeedback::StoreReference, this))
     
    510570
    511571        AddEvent("SET_Ki", "D:1")//, kStateIdle)
    512             (bind(&StateMachineFeedback::SetConstant, this, 0))
     572            (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 0))
    513573            ("Set integral constant Ki");
    514574
    515575        AddEvent("SET_Kp", "D:1")//, kStateIdle)
    516             (bind(&StateMachineFeedback::SetConstant, this, 1))
     576            (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 1))
    517577            ("Set proportional constant Kp");
    518578
    519579        AddEvent("SET_Kd", "D:1")//, kStateIdle)
    520             (bind(&StateMachineFeedback::SetConstant, this, 2))
     580            (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 2))
    521581            ("Set derivative constant Kd");
    522582
     
    538598        //fFSC.SetVerbose(!conf.Get<bool>("quiet"));
    539599
    540         if (!fMap.Read("FACTmapV5.txt"))
    541         {
    542             Error("Reading mapping table from FACTmapV5.txt failed.");
     600        if (!fMap.Read("FACTmapV5a.txt"))
     601        {
     602            Error("Reading mapping table from FACTmapV5a.txt failed.");
    543603            return 1;
    544604        }
     
    652712    return 0;
    653713}
     714
     715const PixelMapEntry PixelMap::empty = { 0, 0, 0, 0, 0, 0 };
Note: See TracChangeset for help on using the changeset viewer.