source: BAORadio/libindi/libindi/drivers/telescope/telescope_simulator.cpp @ 504

Last change on this file since 504 was 504, checked in by frichard, 13 years ago

-Version 0.8 de libini
-Formule de Marc
-Nouvelles fonctionnalités (goto nom-de l'objet etc...)

File size: 5.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <math.h>
5#include <unistd.h>
6#include <sys/time.h>
7#include <time.h>
8
9#include "telescope_simulator.h"
10#include "indicom.h"
11
12// We declare an auto pointer to ScopeSim.
13std::auto_ptr<ScopeSim> telescope_sim(0);
14
15#define SLEWRATE        1                               /* slew rate, degrees/s */
16#define POLLMS          250                             /* poll period, ms */
17#define SIDRATE         0.004178                        /* sidereal rate, degrees/s */
18
19
20void ISPoll(void *p);
21
22void ISInit()
23{
24   static int isInit =0;
25
26   if (isInit == 1)
27       return;
28
29    isInit = 1;
30    if(telescope_sim.get() == 0) telescope_sim.reset(new ScopeSim());
31    //IEAddTimer(POLLMS, ISPoll, NULL);
32
33}
34
35void ISGetProperties(const char *dev)
36{
37        ISInit();
38        telescope_sim->ISGetProperties(dev);
39}
40
41void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int num)
42{
43        ISInit();
44        telescope_sim->ISNewSwitch(dev, name, states, names, num);
45}
46
47void ISNewText( const char *dev, const char *name, char *texts[], char *names[], int num)
48{
49        ISInit();
50        telescope_sim->ISNewText(dev, name, texts, names, num);
51}
52
53void ISNewNumber(const char *dev, const char *name, double values[], char *names[], int num)
54{
55        ISInit();
56        telescope_sim->ISNewNumber(dev, name, values, names, num);
57}
58
59void ISNewBLOB (const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[], char *formats[], char *names[], int n)
60{
61  INDI_UNUSED(dev);
62  INDI_UNUSED(name);
63  INDI_UNUSED(sizes);
64  INDI_UNUSED(blobsizes);
65  INDI_UNUSED(blobs);
66  INDI_UNUSED(formats);
67  INDI_UNUSED(names);
68  INDI_UNUSED(n);
69}
70void ISSnoopDevice (XMLEle *root)
71{
72    INDI_UNUSED(root);
73}
74
75
76ScopeSim::ScopeSim()
77{
78    //ctor
79    currentRA=15;
80    currentDEC=15;
81    Parked=false;
82}
83
84ScopeSim::~ScopeSim()
85{
86    //dtor
87}
88
89const char * ScopeSim::getDefaultName()
90{
91    return (char *)"Telescope Simulator";
92}
93
94bool ScopeSim::Connect(char *)
95{
96    return true;
97}
98
99bool ScopeSim::Disconnect()
100{
101    return true;
102}
103
104bool ScopeSim::ReadScopeStatus()
105{
106    static struct timeval ltv;
107    struct timeval tv;
108    double dt, da, dx;
109    int nlocked;
110
111
112    /* update elapsed time since last poll, don't presume exactly POLLMS */
113    gettimeofday (&tv, NULL);
114
115    if (ltv.tv_sec == 0 && ltv.tv_usec == 0)
116        ltv = tv;
117
118    dt = tv.tv_sec - ltv.tv_sec + (tv.tv_usec - ltv.tv_usec)/1e6;
119    ltv = tv;
120    da = SLEWRATE*dt;
121
122    /* Process per current state. We check the state of EQUATORIAL_EOD_COORDS_REQUEST and act acoordingly */
123    switch (TrackState)
124    {
125    case SCOPE_SLEWING:
126    case SCOPE_PARKING:
127        /* slewing - nail it when both within one pulse @ SLEWRATE */
128        nlocked = 0;
129
130        dx = targetRA - currentRA;
131
132        if (fabs(dx) <= da)
133        {
134            currentRA = targetRA;
135            nlocked++;
136        }
137        else if (dx > 0)
138            currentRA += da/15.;
139        else
140            currentRA -= da/15.;
141
142
143        dx = targetDEC - currentDEC;
144        if (fabs(dx) <= da)
145        {
146            currentDEC = targetDEC;
147            nlocked++;
148        }
149        else if (dx > 0)
150          currentDEC += da;
151        else
152          currentDEC -= da;
153
154        if (nlocked == 2)
155        {
156            //eqNP.s = IPS_OK;
157            //eqNPR.s = IPS_OK;
158            //IDSetNumber(&eqNP, NULL);
159            //IDSetNumber(&eqNPR, "Now tracking");
160            if (TrackState == SCOPE_SLEWING)
161            {
162                TrackState = SCOPE_TRACKING;
163                IDMessage(deviceName(), "Telescope slew is complete. Tracking...");
164            }
165            else
166            {
167                TrackState = SCOPE_PARKED;
168                IDMessage(deviceName(), "Telescope parked successfully.");
169            }
170        }
171        //else
172          //  IDSetNumber(&eqNP, NULL);
173
174        break;
175
176    case SCOPE_TRACKING:
177        /* tracking */
178        /* RA moves at sidereal, Dec stands still */
179         currentRA += (SIDRATE*dt/15.);
180         break;
181
182    default:
183        break;
184    }
185
186    NewRaDec(currentRA, currentDEC);
187    return true;
188}
189
190bool ScopeSim::Goto(double r,double d)
191{
192    //IDLog("ScopeSim Goto\n");
193    targetRA=r;
194    targetDEC=d;
195    char RAStr[64], DecStr[64];
196
197    fs_sexa(RAStr, targetRA, 2, 3600);
198    fs_sexa(DecStr, targetDEC, 2, 3600);
199
200    Parked=false;
201    TrackState = SCOPE_SLEWING;
202    IDMessage(deviceName(), "Slewing to RA: %s - DEC: %s", RAStr, DecStr);
203    return true;
204}
205
206bool ScopeSim::Park()
207{
208    targetRA=0;
209    targetDEC=90;
210    Parked=true;
211    TrackState = SCOPE_PARKING;
212    IDMessage(deviceName(), "Parking telescope in progress...");
213    return true;
214}
215
216bool ScopeSim::Connect()
217{
218    //  Parent class is wanting a connection
219    //IDLog("INDI::Telescope calling connect with %s\n",PortT[0].text);
220    bool rc=false;
221
222    if(isConnected()) return true;
223
224    //IDLog("Calling Connect\n");
225
226    rc=Connect(PortT[0].text);
227
228    if(rc)
229        SetTimer(POLLMS);
230    return rc;
231}
232
233
234
235/* update the "mount" over time */
236void mountSim (void *p)
237{
238
239}
240
241
Note: See TracBrowser for help on using the repository browser.