Changeset 12807 for trunk/FACT++/src
- Timestamp:
- 02/01/12 09:16:41 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/drivectrl.cc
r12770 r12807 14 14 #include "tools.h" 15 15 16 #define HAS_SQL 17 18 #ifdef HAS_SQL 19 #include <mysql++/mysql++.h> 20 #endif 16 21 17 22 namespace ba = boost::asio; … … 140 145 { 141 146 } 142 virtual void UpdateSource(const array<double, 6> & )147 virtual void UpdateSource(const array<double, 6> &, const string& = "") 143 148 { 144 149 } … … 149 154 ba::streambuf fBuffer; 150 155 151 Time ReadTime(istream &in) 156 public: 157 static Time ReadTime(istream &in) 152 158 { 153 159 uint16_t y, m, d, hh, mm, ss, ms; … … 157 163 } 158 164 159 double ReadAngle(istream &in)165 static double ReadAngle(istream &in) 160 166 { 161 167 char sgn; … … 169 175 } 170 176 177 protected: 171 178 void HandleReceivedReport(const boost::system::error_code& err, size_t bytes_received) 172 179 { … … 643 650 "|dZd[deg]:Control deviation Zd" 644 651 "|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", 646 653 "|Ra_cmd[h]:Command right ascension" 647 654 "|Dec_cmd[deg]:Command declination" … … 649 656 "|Dec_src[deg]:Source declination" 650 657 "|Offfset[deg]:Wobble offset" 651 "|Angle[deg]:Wobble angle"), 658 "|Angle[deg]:Wobble angle" 659 "|Name[string]:Source name if available"), 652 660 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", 653 661 "|Ra[h]:Command right ascension" … … 675 683 void UpdateSource() 676 684 { 677 const array<double, 6> arr = {{ 0, 0, 0, 0, 0, 0 }};685 const vector<char> empty(6*sizeof(double)+31, 0); 678 686 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 684 696 fDimSource.setQuality(1); 685 fDimSource.Update( arr);697 fDimSource.Update(dat); 686 698 } 687 699 … … 719 731 }; 720 732 733 typedef map<string,pair<double,double>> sources; 734 sources fSources; 735 721 736 // Status 0: Error 722 737 // Status 1: Unlocked … … 801 816 } 802 817 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; 814 826 815 827 const double cosdir = cos(dir); … … 823 835 { 824 836 const array<double, 6> dim = {{ ra, dec, ra, dec, 0, 0 }}; 825 fDrive.UpdateSource(dim );837 fDrive.UpdateSource(dim, name); 826 838 827 839 string command = "RADEC "; 828 command += AngleToStr( dat[0]) + ' ' + AngleToStr(dat[1]);840 command += AngleToStr(srcra) + ' ' + AngleToStr(srcdec); 829 841 return SendCommand(command, false); 830 842 } … … 846 858 847 859 const array<double, 6> dim = {{ ra, dec, nra, ndec, off, dir }}; 848 fDrive.UpdateSource(dim );860 fDrive.UpdateSource(dim, name); 849 861 850 862 string command = "RADEC "; … … 853 865 } 854 866 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 855 892 int SetLedBrightness(const EventImp &evt) 856 893 { … … 873 910 fDrive.SetVerbose(evt.GetBool()); 874 911 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 } 875 926 return T::GetCurrentState(); 876 927 } … … 999 1050 "|Offset[deg]:Wobble offset" 1000 1051 "|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"); 1001 1059 1002 1060 T::AddEvent("MOON", kStateArmed) … … 1055 1113 "|[host][string]:new ethernet address in the form <host:port>"); 1056 1114 1115 1116 T::AddEvent("PRINT") 1117 (bind(&StateMachineDrive::Print, this)) 1118 ("Print source list."); 1119 1057 1120 fDrive.StartConnect(); 1058 1121 } … … 1063 1126 } 1064 1127 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 1065 1191 int EvalOptions(Configuration &conf) 1066 1192 { … … 1068 1194 1069 1195 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")); 1070 1234 1071 1235 return -1; … … 1091 1255 ("addr,a", var<string>("localhost:7404"), "Network address of Cosy") 1092 1256 ("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\"") 1093 1259 ; 1094 1260
Note:
See TracChangeset
for help on using the changeset viewer.