Telescope.cpp   Telescope.cpp 
skipping to change at line 22 skipping to change at line 22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include "Telescope.hpp" #include "Telescope.hpp"
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "Translator.hpp" #include "StelTranslator.hpp"
#include "StelCore.hpp" #include "StelCore.hpp"
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <math.h> #include <math.h>
#include <QTextStream> #include <QTextStream>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QRegExp> #include <QRegExp>
skipping to change at line 133 skipping to change at line 133
<< ((d<10)?" ":"") << dec_sign << d << 'd' << ((d<10)?" ":"") << dec_sign << d << 'd'
<< setfill('0') << setw(2) << dec_m << 'm' << setfill('0') << setw(2) << dec_m << 'm'
<< setfill('0') << setw(2) << dec_s << '.' << setfill('0') << setw(2) << dec_s << '.'
<< setfill('0') << setw(3) << dec_ms << setfill('0') << setw(3) << dec_ms
<< setfill(' '); << setfill(' ');
return o; return o;
} }
class TelescopeDummy : public Telescope class TelescopeDummy : public Telescope
{ {
//! example Telescope class. A physical telescope does not exist.
//! This can be used as a starting point for implementing a derived
//! Telescope class.
public: public:
TelescopeDummy(const QString &name,const QString &params) : Telescop e(name) TelescopeDummy(const QString &name,const QString &params) : Telescop e(name)
{ {
desired_pos[0] = XYZ[0] = 1.0; desired_pos[0] = XYZ[0] = 1.0;
desired_pos[1] = XYZ[1] = 0.0; desired_pos[1] = XYZ[1] = 0.0;
desired_pos[2] = XYZ[2] = 0.0; desired_pos[2] = XYZ[2] = 0.0;
} }
private: private:
bool isConnected(void) const bool isConnected(void) const
{ {
return true; return true;
} }
bool hasKnownPosition(void) const bool hasKnownPosition(void) const
{ {
return true; return true;
} }
Vec3d getObsJ2000Pos(const Navigator *nav=0) const Vec3d getJ2000EquatorialPos(const StelNavigator *nav=0) const
{ {
return XYZ; return XYZ;
} }
void prepareSelectFds(fd_set&,fd_set&,int&) void prepareSelectFds(fd_set&,fd_set&,int&)
{ {
XYZ = XYZ*31.0+desired_pos; XYZ = XYZ*31.0+desired_pos;
const double lq = XYZ.lengthSquared(); const double lq = XYZ.lengthSquared();
if (lq > 0.0) XYZ *= (1.0/sqrt(lq)); if (lq > 0.0) XYZ *= (1.0/sqrt(lq));
else XYZ = desired_pos; else XYZ = desired_pos;
} }
skipping to change at line 171 skipping to change at line 174
{ {
desired_pos = j2000Pos; desired_pos = j2000Pos;
desired_pos.normalize(); desired_pos.normalize();
} }
Vec3d XYZ; // j2000 position Vec3d XYZ; // j2000 position
Vec3d desired_pos; Vec3d desired_pos;
}; };
class TelescopeTcp : public Telescope class TelescopeTcp : public Telescope
{ {
//! This Telescope class can controll a telescope by communicating
//! to a server process ("telescope server") via
//! the "Stellarium telescope control protocol" over TCP/IP.
//! The "Stellarium telescope control protocol" is specified in a seperat
e
//! document along with the telescope server software.
public: public:
TelescopeTcp(const QString &name,const QString &params); TelescopeTcp(const QString &name,const QString &params);
~TelescopeTcp(void) ~TelescopeTcp(void)
{ {
hangup(); hangup();
} }
private: private:
bool isConnected(void) const bool isConnected(void) const
{ {
return (!IS_INVALID_SOCKET(fd) && !wait_for_connection_estab lishment); return (!IS_INVALID_SOCKET(fd) && !wait_for_connection_estab lishment);
} }
Vec3d getObsJ2000Pos(const Navigator *nav=0) const; Vec3d getJ2000EquatorialPos(const StelNavigator *nav=0) const;
void prepareSelectFds(fd_set &read_fds,fd_set &write_fds,int &fd_max ); void prepareSelectFds(fd_set &read_fds,fd_set &write_fds,int &fd_max );
void handleSelectFds(const fd_set &read_fds,const fd_set &write_fds) ; void handleSelectFds(const fd_set &read_fds,const fd_set &write_fds) ;
void telescopeGoto(const Vec3d &j2000Pos); void telescopeGoto(const Vec3d &j2000Pos);
bool isInitialized(void) const bool isInitialized(void) const
{ {
return (ntohs(address.sin_port)!=0); return (ntohs(address.sin_port)!=0);
} }
void performReading(void); void performReading(void);
void performWriting(void); void performWriting(void);
private: private:
skipping to change at line 398 skipping to change at line 406
position_pointer->pos[0] = 0.0; position_pointer->pos[0] = 0.0;
position_pointer->pos[1] = 0.0; position_pointer->pos[1] = 0.0;
position_pointer->pos[2] = 0.0; position_pointer->pos[2] = 0.0;
position_pointer->status = 0; position_pointer->status = 0;
} }
position_pointer = positions; position_pointer = positions;
} }
void TelescopeTcp::telescopeGoto(const Vec3d &j2000Pos) void TelescopeTcp::telescopeGoto(const Vec3d &j2000Pos)
{ {
//! queues a GOTO command with the specified position to the write buffer
.
//! for the data format of the command see the
//! "Stellarium telescope control protocol"
if (isConnected()) if (isConnected())
{ {
if (write_buff_end-write_buff+20 < (int)sizeof(write_buff)) if (write_buff_end-write_buff+20 < (int)sizeof(write_buff))
{ {
const double ra = atan2(j2000Pos[1],j2000Pos[0]); const double ra = atan2(j2000Pos[1],j2000Pos[0]);
const double dec = atan2(j2000Pos[2], sqrt(j2000Pos[ 0]*j2000Pos[0]+j2000Pos[1]*j2000Pos[1])); const double dec = atan2(j2000Pos[2], sqrt(j2000Pos[ 0]*j2000Pos[0]+j2000Pos[1]*j2000Pos[1]));
unsigned int ra_int = (unsigned int)floor(0.5 + ra* (((unsigned int)0x80000000)/M_PI)); unsigned int ra_int = (unsigned int)floor(0.5 + ra* (((unsigned int)0x80000000)/M_PI));
int dec_int = (int)floor(0.5 + dec*(((unsigned int)0 x80000000)/M_PI)); int dec_int = (int)floor(0.5 + dec*(((unsigned int)0 x80000000)/M_PI));
// qDebug() << "TelescopeTcp(" << name << ")::telescopeGoto: " // qDebug() << "TelescopeTcp(" << name << ")::telescopeGoto: "
// "queuing packet: " << PrintRaDec(ra_int,dec_int); // "queuing packet: " << PrintRaDec(ra_int,dec_int);
skipping to change at line 486 skipping to change at line 498
{ {
// partly written // partly written
memmove(write_buff,write_buff+rc,to_write-rc); memmove(write_buff,write_buff+rc,to_write-rc);
write_buff_end -= rc; write_buff_end -= rc;
} }
} }
} }
void TelescopeTcp::performReading(void) void TelescopeTcp::performReading(void)
{ {
//! try to read some data from the telescope server
const int to_read = read_buff + sizeof(read_buff) - read_buff_end; const int to_read = read_buff + sizeof(read_buff) - read_buff_end;
const int rc = recv(fd,read_buff_end,to_read,0); const int rc = recv(fd,read_buff_end,to_read,0);
if (rc < 0) if (rc < 0)
{ {
if (ERRNO != EINTR && ERRNO != EAGAIN) if (ERRNO != EINTR && ERRNO != EAGAIN)
{ {
qDebug() << "TelescopeTcp(" << name << ")::performRe ading: " << "recv failed: " << STRERROR(ERRNO); qDebug() << "TelescopeTcp(" << name << ")::performRe ading: " << "recv failed: " << STRERROR(ERRNO);
hangup(); hangup();
} }
} }
else if (rc == 0) else if (rc == 0)
{ {
qDebug() << "TelescopeTcp(" << name << ")::performReading: " << "server has closed the connection"; qDebug() << "TelescopeTcp(" << name << ")::performReading: " << "server has closed the connection";
hangup(); hangup();
} }
else else
{ {
read_buff_end += rc; read_buff_end += rc;
char *p = read_buff; char *p = read_buff;
// parse the data in the read buffer:
while (read_buff_end-p >= 2) while (read_buff_end-p >= 2)
{ {
const int size = (int)(((unsigned char)(p[0])) | ((( unsigned int)(unsigned char)(p[1])) << 8)); const int size = (int)(((unsigned char)(p[0])) | ((( unsigned int)(unsigned char)(p[1])) << 8));
if (size > (int)sizeof(read_buff) || size < 4) if (size > (int)sizeof(read_buff) || size < 4)
{ {
qDebug() << "TelescopeTcp(" << name << ")::p erformReading: " << "bad packet size: " << size; qDebug() << "TelescopeTcp(" << name << ")::p erformReading: " << "bad packet size: " << size;
hangup(); hangup();
return; return;
} }
if (size > read_buff_end-p) if (size > read_buff_end-p)
{ {
// wait for complete packet // wait for complete packet
break; break;
} }
const int type = (int)(((unsigned char)(p[2])) | ((( unsigned int)(unsigned char)(p[3])) << 8)); const int type = (int)(((unsigned char)(p[2])) | ((( unsigned int)(unsigned char)(p[3])) << 8));
// dispatch: // dispatch:
switch (type) switch (type)
{ {
case 0: case 0:
{ {
// We have received position information.
// For the data format of the message see the
// "Stellarium telescope control protocol"
if (size < 24) if (size < 24)
{ {
qDebug() << "TelescopeTcp(" << name << ")::performReading: " << "type 0: bad packet size: " << size; qDebug() << "TelescopeTcp(" << name << ")::performReading: " << "type 0: bad packet size: " << size;
hangup(); hangup();
return; return;
} }
const long long int server_micros = (long lo ng int) const long long int server_micros = (long lo ng int)
(((unsigned long long int)(unsigned char)(p[ 4])) | (((unsigned long long int)(unsigned char)(p[ 4])) |
(((unsigned long long int)(unsigned char)(p[ 5])) << 8) | (((unsigned long long int)(unsigned char)(p[ 5])) << 8) |
(((unsigned long long int)(unsigned char)(p[ 6])) << 16) | (((unsigned long long int)(unsigned char)(p[ 6])) << 16) |
skipping to change at line 556 skipping to change at line 573
(int)(((unsigned int)(unsigned char) (p[16])) | (int)(((unsigned int)(unsigned char) (p[16])) |
(((unsigned int)(unsigned char)(p[17 ])) << 8) | (((unsigned int)(unsigned char)(p[17 ])) << 8) |
(((unsigned int)(unsigned char)(p[18 ])) << 16) | (((unsigned int)(unsigned char)(p[18 ])) << 16) |
(((unsigned int)(unsigned char)(p[19 ])) << 24)); (((unsigned int)(unsigned char)(p[19 ])) << 24));
const int status = const int status =
(int)(((unsigned int)(unsigned char)(p[2 0])) | (int)(((unsigned int)(unsigned char)(p[2 0])) |
(((unsigned int)(unsigned char)(p[21 ])) << 8) | (((unsigned int)(unsigned char)(p[21 ])) << 8) |
(((unsigned int)(unsigned char)(p[22 ])) << 16) | (((unsigned int)(unsigned char)(p[22 ])) << 16) |
(((unsigned int)(unsigned char)(p[23 ])) << 24)); (((unsigned int)(unsigned char)(p[23 ])) << 24));
// remember the time and received position
so that later we
// will know where the telescope is pointi
ng to:
position_pointer++; position_pointer++;
if (position_pointer >= end_position) if (position_pointer >= end_position)
position_pointer = positions; position_pointer = positions;
position_pointer->server_micros = server_mic ros; position_pointer->server_micros = server_mic ros;
position_pointer->client_micros = GetNow(); position_pointer->client_micros = GetNow();
const double ra = ra_int * (M_PI/(unsigned int)0x80000000); const double ra = ra_int * (M_PI/(unsigned int)0x80000000);
const double dec = dec_int * (M_PI/(unsigned int)0x80000000); const double dec = dec_int * (M_PI/(unsigned int)0x80000000);
const double cdec = cos(dec); const double cdec = cos(dec);
position_pointer->pos[0] = cos(ra)*cdec; position_pointer->pos[0] = cos(ra)*cdec;
position_pointer->pos[1] = sin(ra)*cdec; position_pointer->pos[1] = sin(ra)*cdec;
skipping to change at line 594 skipping to change at line 613
} }
else else
{ {
// partly handled // partly handled
memmove(read_buff,p,read_buff_end-p); memmove(read_buff,p,read_buff_end-p);
read_buff_end -= (p-read_buff); read_buff_end -= (p-read_buff);
} }
} }
} }
Vec3d TelescopeTcp::getObsJ2000Pos(const Navigator*) const Vec3d TelescopeTcp::getJ2000EquatorialPos(const StelNavigator*) const
{ {
// estimate wher the telescope is by interpolation in the stored
// telescope positions:
if (position_pointer->client_micros == 0x7FFFFFFFFFFFFFFFLL) if (position_pointer->client_micros == 0x7FFFFFFFFFFFFFFFLL)
{ {
return Vec3d(0,0,0); return Vec3d(0,0,0);
} }
const long long int now = GetNow() - time_delay; const long long int now = GetNow() - time_delay;
const Position *p = position_pointer; const Position *p = position_pointer;
do do
{ {
const Position *pp = p; const Position *pp = p;
if (pp == positions) pp = end_position; if (pp == positions) pp = end_position;
skipping to change at line 629 skipping to change at line 650
break; break;
} }
p = pp; p = pp;
} }
while (p != position_pointer); while (p != position_pointer);
return p->pos; return p->pos;
} }
void TelescopeTcp::prepareSelectFds(fd_set &read_fds,fd_set &write_fds, int &fd_max) void TelescopeTcp::prepareSelectFds(fd_set &read_fds,fd_set &write_fds, int &fd_max)
{ {
// just plain TCP/IP programming
if (IS_INVALID_SOCKET(fd)) if (IS_INVALID_SOCKET(fd))
{ {
// try reconnecting // try reconnecting
const long long int now = GetNow(); const long long int now = GetNow();
if (now < end_of_timeout) return; if (now < end_of_timeout) return;
end_of_timeout = now + 5000000; end_of_timeout = now + 5000000;
fd = socket(AF_INET,SOCK_STREAM,0); fd = socket(AF_INET,SOCK_STREAM,0);
if (IS_INVALID_SOCKET(fd)) if (IS_INVALID_SOCKET(fd))
{ {
qDebug() << "TelescopeTcp(" << name << ")::prepareSe lectFds: "<< "socket() failed: " << STRERROR(ERRNO); qDebug() << "TelescopeTcp(" << name << ")::prepareSe lectFds: "<< "socket() failed: " << STRERROR(ERRNO);
skipping to change at line 692 skipping to change at line 714
else else
{ {
if (write_buff_end > write_buff) FD_SET(fd,&write_fd s); if (write_buff_end > write_buff) FD_SET(fd,&write_fd s);
FD_SET(fd,&read_fds); FD_SET(fd,&read_fds);
} }
} }
} }
void TelescopeTcp::handleSelectFds(const fd_set &read_fds, const fd_set &wr ite_fds) void TelescopeTcp::handleSelectFds(const fd_set &read_fds, const fd_set &wr ite_fds)
{ {
// just plain TCP/IP programming
if (!IS_INVALID_SOCKET(fd)) if (!IS_INVALID_SOCKET(fd))
{ {
if (wait_for_connection_establishment) if (wait_for_connection_establishment)
{ {
if (FD_ISSET(fd,const_cast<fd_set *>(&write_fds))) if (FD_ISSET(fd,const_cast<fd_set *>(&write_fds)))
{ {
wait_for_connection_establishment = false; wait_for_connection_establishment = false;
int err = 0; int err = 0;
SOCKLEN_T length = sizeof(err); SOCKLEN_T length = sizeof(err);
if (getsockopt(fd,SOL_SOCKET,SO_ERROR,(char* )(&err),&length) != 0) if (getsockopt(fd,SOL_SOCKET,SO_ERROR,(char* )(&err),&length) != 0)
 End of changes. 14 change blocks. 
4 lines changed or deleted 31 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/