Changeset 12807 for trunk/FACT++/src


Ignore:
Timestamp:
02/01/12 09:16:41 (13 years ago)
Author:
tbretz
Message:
Added options to read sources names from a database and to start tracking with source names.
File:
1 edited

Legend:

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

    r12770 r12807  
    1414#include "tools.h"
    1515
     16#define HAS_SQL
     17
     18#ifdef HAS_SQL
     19#include <mysql++/mysql++.h>
     20#endif
    1621
    1722namespace ba = boost::asio;
     
    140145    {
    141146    }
    142     virtual void UpdateSource(const array<double, 6> &)
     147    virtual void UpdateSource(const array<double, 6> &, const string& = "")
    143148    {
    144149    }
     
    149154    ba::streambuf fBuffer;
    150155
    151     Time ReadTime(istream &in)
     156public:
     157    static Time ReadTime(istream &in)
    152158    {
    153159        uint16_t y, m, d, hh, mm, ss, ms;
     
    157163    }
    158164
    159     double ReadAngle(istream &in)
     165    static double ReadAngle(istream &in)
    160166    {
    161167        char     sgn;
     
    169175    }
    170176
     177protected:
    171178    void HandleReceivedReport(const boost::system::error_code& err, size_t bytes_received)
    172179    {
     
    643650                     "|dZd[deg]:Control deviation Zd"
    644651                     "|dAz[deg]:Control deviation Az"),
    645         fDimSource("DRIVE_CONTROL/SOURCE_POSITION", "D:1;D:1;D:1;D:1;D:1;D:1",
     652        fDimSource("DRIVE_CONTROL/SOURCE_POSITION", "D:1;D:1;D:1;D:1;D:1;D:1;C:31",
    646653                     "|Ra_cmd[h]:Command right ascension"
    647654                     "|Dec_cmd[deg]:Command declination"
     
    649656                     "|Dec_src[deg]:Source declination"
    650657                     "|Offfset[deg]:Wobble offset"
    651                      "|Angle[deg]:Wobble angle"),
     658                     "|Angle[deg]:Wobble angle"
     659                     "|Name[string]:Source name if available"),
    652660        fDimTPoint("DRIVE_CONTROL/TPOINT", "D:1;D:1;D:1;D:1;D:1;D:1;D:1;D:1;S:1;D:1;D:1;D:1;D:1;D:1;D:1;D:1;C",
    653661                   "|Ra[h]:Command right ascension"
     
    675683    void UpdateSource()
    676684    {
    677         const array<double, 6> arr = {{ 0, 0, 0, 0, 0, 0 }};
     685        const vector<char> empty(6*sizeof(double)+31, 0);
    678686        fDimSource.setQuality(0);
    679         fDimSource.Update(arr);
    680     }
    681 
    682     void UpdateSource(const array<double, 6> &arr)
    683     {
     687        fDimSource.Update(empty);
     688    }
     689
     690    void UpdateSource(const array<double, 6> &arr, const string &name="")
     691    {
     692        vector<char> dat(6*sizeof(double)+31, 0);
     693        memcpy(dat.data(), arr.data(), 6*sizeof(double));
     694        strncpy(dat.data()+6*sizeof(double), name.c_str(), 30);
     695
    684696        fDimSource.setQuality(1);
    685         fDimSource.Update(arr);
     697        fDimSource.Update(dat);
    686698    }
    687699
     
    719731    };
    720732
     733    typedef map<string,pair<double,double>> sources;
     734    sources fSources;
     735
    721736    // Status 0: Error
    722737    // Status 1: Unlocked
     
    801816    }
    802817
    803     int Wobble(const EventImp &evt)
    804     {
    805         if (!CheckEventSize(evt.GetSize(), "Wobble", 32))
    806             return T::kSM_FatalError;
    807 
    808         const double *dat = evt.Ptr<double>();
    809 
    810         const double ra  = dat[0]*M_PI/12;
    811         const double dec = dat[1]*M_PI/180;
    812         const double off = dat[2]*M_PI/180;
    813         const double dir = dat[3]*M_PI/180;
     818    int StartWobble(const double &srcra,  const double &srcdec,
     819                    const double &woboff, const double &wobang,
     820                    const string name="")
     821    {
     822        const double ra  = srcra *M_PI/12;
     823        const double dec = srcdec*M_PI/180;
     824        const double off = woboff*M_PI/180;
     825        const double dir = wobang*M_PI/180;
    814826
    815827        const double cosdir = cos(dir);
     
    823835        {
    824836            const array<double, 6> dim = {{ ra, dec, ra, dec, 0, 0 }};
    825             fDrive.UpdateSource(dim);
     837            fDrive.UpdateSource(dim, name);
    826838
    827839            string command = "RADEC ";
    828             command += AngleToStr(dat[0]) + ' ' + AngleToStr(dat[1]);
     840            command += AngleToStr(srcra) + ' ' + AngleToStr(srcdec);
    829841            return SendCommand(command, false);
    830842        }
     
    846858
    847859        const array<double, 6> dim = {{ ra, dec, nra, ndec, off, dir }};
    848         fDrive.UpdateSource(dim);
     860        fDrive.UpdateSource(dim, name);
    849861
    850862        string command = "RADEC ";
     
    853865    }
    854866
     867    int Wobble(const EventImp &evt)
     868    {
     869        if (!CheckEventSize(evt.GetSize(), "Wobble", 32))
     870            return T::kSM_FatalError;
     871
     872        const double *dat = evt.Ptr<double>();
     873
     874        return StartWobble(dat[0], dat[1], dat[2], dat[3]);
     875    }
     876
     877    int Track(const EventImp &evt)
     878    {
     879        const double *dat = evt.Ptr<double>();
     880        const string name = evt.Ptr<char>(16);
     881
     882        const sources::const_iterator it = fSources.find(name);
     883        if (it==fSources.end())
     884            return T::Error("Source '"+name+"'not found in list.");
     885
     886        const double &ra   = it->second.first;
     887        const double &dec  = it->second.second;
     888
     889        return StartWobble(ra, dec, dat[0], dat[1], name);
     890    }
     891
    855892    int SetLedBrightness(const EventImp &evt)
    856893    {
     
    873910        fDrive.SetVerbose(evt.GetBool());
    874911
     912        return T::GetCurrentState();
     913    }
     914
     915    int Print()
     916    {
     917        for (sources::const_iterator it=fSources.begin();
     918             it!=fSources.end(); it++)
     919        {
     920            const string &name = it->first;
     921            const double &ra   = it->second.first;
     922            const double &dec  = it->second.second;
     923
     924            T::Out() << name << "," << ra << "," << dec << endl;
     925        }
    875926        return T::GetCurrentState();
    876927    }
     
    9991050             "|Offset[deg]:Wobble offset"
    10001051             "|Angle[deg]:Wobble angle");
     1052
     1053        T::AddEvent("TRACK", "D:2;C", kStateArmed)   // ->RADEC/GRB
     1054            (bind(&StateMachineDrive::Track, this, placeholders::_1))
     1055            ("Move the telescope to the given wobble position around the given source and start tracking"
     1056             "|Offset[deg]:Wobble offset"
     1057             "|Angle[deg]:Wobble angle"
     1058             "|Name[string]:Source name");
    10011059
    10021060        T::AddEvent("MOON", kStateArmed)
     
    10551113             "|[host][string]:new ethernet address in the form <host:port>");
    10561114
     1115
     1116        T::AddEvent("PRINT")
     1117            (bind(&StateMachineDrive::Print, this))
     1118            ("Print source list.");
     1119
    10571120        fDrive.StartConnect();
    10581121    }
     
    10631126    }
    10641127
     1128    void ReadDatabase(const string &database)
     1129    {
     1130        //static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))?");
     1131        static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
     1132        // 2: user
     1133        // 4: pass
     1134        // 5: server
     1135        // 7: port
     1136        // 9: db
     1137
     1138        boost::smatch what;
     1139        if (!boost::regex_match(database, what, expr, boost::match_extra))
     1140            throw runtime_error("Couldn't parse '"+database+"'.");
     1141
     1142        if (what.size()!=10)
     1143            throw runtime_error("Error parsing '"+database+"'.");
     1144
     1145        const string user   = what[2];
     1146        const string passwd = what[4];
     1147        const string server = what[5];
     1148        const string db     = what[9];
     1149        const int port      = atoi(string(what[7]).c_str());
     1150
     1151        ostringstream out;
     1152        out << "Connecting to '";
     1153        if (!user.empty())
     1154            out << user << "@";
     1155        out << server;
     1156        if (port)
     1157            out << ":" << port;
     1158        if (!db.empty())
     1159            out << "/" << db;
     1160
     1161        T::Message(out);
     1162
     1163        mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
     1164        /* throws exceptions
     1165         if (!conn.connected())
     1166         {
     1167             cout << "MySQL connection error: " << conn.error() << endl;
     1168             throw;
     1169         }*/
     1170
     1171        const mysqlpp::StoreQueryResult res =
     1172            conn.query("SELECT fSourceName, fRightAscension, fDeclination FROM scheduling.source").store();
     1173        /* throws exceptions
     1174        if (!res)
     1175        {
     1176            cout << "MySQL query failed: " << query.error() << endl;
     1177            throw;
     1178        }*/
     1179
     1180        for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
     1181        {
     1182            const string name = (*v)[0].c_str();
     1183            const double ra   = (*v)[1];
     1184            const double dec  = (*v)[2];
     1185
     1186            // FIXME: Check double names
     1187            fSources[name] = make_pair(ra, dec);
     1188        }
     1189    }
     1190
    10651191    int EvalOptions(Configuration &conf)
    10661192    {
     
    10681194
    10691195        fDrive.SetVerbose(!conf.Get<bool>("quiet"));
     1196
     1197        const vector<string> &vec = conf.Vec<string>("source");
     1198
     1199        for (vector<string>::const_iterator it=vec.begin(); it!=vec.end(); it++)
     1200        {
     1201            istringstream stream(*it);
     1202
     1203            string name;
     1204            double ra=0;
     1205            double dec=0;
     1206
     1207            int i=0;
     1208
     1209            string buffer;
     1210            while (getline(stream, buffer, ','))
     1211            {
     1212                istringstream is(buffer);
     1213
     1214                switch (i++)
     1215                {
     1216                case 0: name = buffer; break;
     1217                case 1: ra   = ConnectionDrive::ReadAngle(is); break;
     1218                case 2: dec  = ConnectionDrive::ReadAngle(is); break;
     1219                }
     1220
     1221                if (is.fail())
     1222                    break;
     1223            }
     1224
     1225            if (i==3)
     1226            {
     1227                // FIXME: Check double names
     1228                fSources[name] = make_pair(ra, dec);
     1229            }
     1230        }
     1231
     1232        if (conf.Has("source-database"))
     1233            ReadDatabase(conf.Get<string>("source-database"));
    10701234
    10711235        return -1;
     
    10911255        ("addr,a",  var<string>("localhost:7404"),  "Network address of Cosy")
    10921256        ("quiet,q", po_bool(true),  "Disable printing contents of all received messages (except dynamic data) in clear text.")
     1257        ("source-database", var<string>(), "Database link as in\n\tuser:password@server[:port]/database.")
     1258        ("source", vars<string>(), "Additional source entry in the form \"name,hh:mm:ss,dd:mm:ss\"")
    10931259        ;
    10941260
Note: See TracChangeset for help on using the changeset viewer.