| 1 | /* | 
|---|
| 2 | LX200 16" | 
|---|
| 3 | Copyright (C) 2003 Jasem Mutlaq (mutlaqja@ikarustech.com) | 
|---|
| 4 |  | 
|---|
| 5 | This library is free software; you can redistribute it and/or | 
|---|
| 6 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 7 | License as published by the Free Software Foundation; either | 
|---|
| 8 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 9 |  | 
|---|
| 10 | This library is distributed in the hope that it will be useful, | 
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 13 | Lesser General Public License for more details. | 
|---|
| 14 |  | 
|---|
| 15 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 16 | License along with this library; if not, write to the Free Software | 
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA | 
|---|
| 18 |  | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #include "lx200_16.h" | 
|---|
| 22 | #include "lx200driver.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include <stdio.h> | 
|---|
| 25 | #include <stdlib.h> | 
|---|
| 26 | #include <string.h> | 
|---|
| 27 | #include <unistd.h> | 
|---|
| 28 | #include <math.h> | 
|---|
| 29 |  | 
|---|
| 30 | #define LX16GROUP       "GPS/16 inch Features" | 
|---|
| 31 |  | 
|---|
| 32 | #define currentAZ       HorizontalCoordsRNP.np[0].value | 
|---|
| 33 | #define currentALT      HorizontalCoordsRNP.np[1].value | 
|---|
| 34 | #define targetAZ        HorizontalCoordsWNP.np[0].value | 
|---|
| 35 | #define targetALT       HorizontalCoordsWNP.np[1].value | 
|---|
| 36 |  | 
|---|
| 37 | extern LX200Generic *telescope; | 
|---|
| 38 | extern ITextVectorProperty Time; | 
|---|
| 39 | extern int MaxReticleFlashRate; | 
|---|
| 40 |  | 
|---|
| 41 | static ISwitch FanStatusS[]             = { {"On", "", ISS_OFF, 0, 0}, {"Off", "", ISS_OFF, 0, 0}}; | 
|---|
| 42 | static ISwitch HomeSearchS[]            = { {"Save home", "", ISS_OFF, 0, 0} , {"Set home", "", ISS_OFF, 0, 0}}; | 
|---|
| 43 | static ISwitch FieldDeRotatorS[]        = { {"On", "", ISS_OFF, 0, 0}, {"Off", "", ISS_OFF,0 ,0}}; | 
|---|
| 44 |  | 
|---|
| 45 | static ISwitchVectorProperty FanStatusSP        = { mydev, "Fan", "", LX16GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE, FanStatusS, NARRAY(FanStatusS), "", 0}; | 
|---|
| 46 |  | 
|---|
| 47 | static ISwitchVectorProperty HomeSearchSP       = { mydev, "Park", "", LX16GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE, HomeSearchS, NARRAY(HomeSearchS), "", 0}; | 
|---|
| 48 |  | 
|---|
| 49 | static ISwitchVectorProperty FieldDeRotatorSP   = { mydev, "Field De-rotator", "", LX16GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE, FieldDeRotatorS, NARRAY(FieldDeRotatorS), "", 0}; | 
|---|
| 50 |  | 
|---|
| 51 | //static ISwitches SlewAltAzSw          = { mydev, "AltAzSet", "On Alt/Az Set",  SlewAltAzS, NARRAY(SlewAltAzS), ILS_IDLE, 0, LX16Group}; | 
|---|
| 52 |  | 
|---|
| 53 | /* Horizontal Coordinates: Read Only */ | 
|---|
| 54 | static INumber HorizontalCoordsRN[] = { | 
|---|
| 55 | {"ALT",  "Alt  D:M:S", "%10.6m",  -90., 90., 0., 0., 0, 0, 0}, | 
|---|
| 56 | {"AZ", "Az D:M:S", "%10.6m", 0., 360., 0., 0., 0, 0, 0}}; | 
|---|
| 57 | static INumberVectorProperty HorizontalCoordsRNP = { | 
|---|
| 58 | mydev, "HORIZONTAL_COORD", "Horizontal Coords", LX16GROUP, IP_RO, 120, IPS_IDLE, | 
|---|
| 59 | HorizontalCoordsRN, NARRAY(HorizontalCoordsRN), "", 0}; | 
|---|
| 60 |  | 
|---|
| 61 | /* Horizontal Coordinates: Request Only */ | 
|---|
| 62 | static INumber HorizontalCoordsWN[] = { | 
|---|
| 63 | {"ALT",  "Alt  D:M:S", "%10.6m",  -90., 90., 0., 0., 0, 0, 0}, | 
|---|
| 64 | {"AZ", "Az D:M:S", "%10.6m", 0., 360., 0., 0., 0, 0, 0}}; | 
|---|
| 65 | static INumberVectorProperty HorizontalCoordsWNP = { | 
|---|
| 66 | mydev, "HORIZONTAL_COORD_REQUEST", "Horizontal Coords", LX16GROUP, IP_WO, 120, IPS_IDLE, | 
|---|
| 67 | HorizontalCoordsWN, NARRAY(HorizontalCoordsWN), "", 0}; | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | void changeLX200_16DeviceName(const char * newName) | 
|---|
| 71 | { | 
|---|
| 72 | strcpy(HorizontalCoordsWNP.device, newName); | 
|---|
| 73 | strcpy(HorizontalCoordsRNP.device, newName); | 
|---|
| 74 | strcpy(FanStatusSP.device, newName); | 
|---|
| 75 | strcpy(HomeSearchSP.device, newName); | 
|---|
| 76 | strcpy(FieldDeRotatorSP.device,newName); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | LX200_16::LX200_16() : LX200Autostar() | 
|---|
| 80 | { | 
|---|
| 81 |  | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void LX200_16::ISGetProperties (const char *dev) | 
|---|
| 85 | { | 
|---|
| 86 |  | 
|---|
| 87 | if (dev && strcmp (thisDevice, dev)) | 
|---|
| 88 | return; | 
|---|
| 89 |  | 
|---|
| 90 | // process parent first | 
|---|
| 91 | LX200Autostar::ISGetProperties(dev); | 
|---|
| 92 |  | 
|---|
| 93 | IDDefNumber (&HorizontalCoordsWNP, NULL); | 
|---|
| 94 | IDDefNumber (&HorizontalCoordsRNP, NULL); | 
|---|
| 95 |  | 
|---|
| 96 | IDDefSwitch (&FanStatusSP, NULL); | 
|---|
| 97 | IDDefSwitch (&HomeSearchSP, NULL); | 
|---|
| 98 | IDDefSwitch (&FieldDeRotatorSP, NULL); | 
|---|
| 99 |  | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void LX200_16::ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n) | 
|---|
| 103 | { | 
|---|
| 104 |  | 
|---|
| 105 | // ignore if not ours // | 
|---|
| 106 | if (strcmp (dev, thisDevice)) | 
|---|
| 107 | return; | 
|---|
| 108 |  | 
|---|
| 109 | LX200Autostar::ISNewText (dev, name, texts, names,  n); | 
|---|
| 110 |  | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void LX200_16::ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n) | 
|---|
| 114 | { | 
|---|
| 115 | double newAlt=0, newAz=0; | 
|---|
| 116 | char altStr[64], azStr[64]; | 
|---|
| 117 | int err; | 
|---|
| 118 |  | 
|---|
| 119 | // ignore if not ours // | 
|---|
| 120 | if (strcmp (dev, thisDevice)) | 
|---|
| 121 | return; | 
|---|
| 122 |  | 
|---|
| 123 | if ( !strcmp (name, HorizontalCoordsWNP.name) ) | 
|---|
| 124 | { | 
|---|
| 125 | int i=0, nset=0; | 
|---|
| 126 |  | 
|---|
| 127 | if (checkPower(&HorizontalCoordsWNP)) | 
|---|
| 128 | return; | 
|---|
| 129 |  | 
|---|
| 130 | for (nset = i = 0; i < n; i++) | 
|---|
| 131 | { | 
|---|
| 132 | INumber *horp = IUFindNumber (&HorizontalCoordsWNP, names[i]); | 
|---|
| 133 | if (horp == &HorizontalCoordsWN[0]) | 
|---|
| 134 | { | 
|---|
| 135 | newAlt = values[i]; | 
|---|
| 136 | nset += newAlt >= -90. && newAlt <= 90.0; | 
|---|
| 137 | } else if (horp == &HorizontalCoordsWN[1]) | 
|---|
| 138 | { | 
|---|
| 139 | newAz = values[i]; | 
|---|
| 140 | nset += newAz >= 0. && newAz <= 360.0; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | if (nset == 2) | 
|---|
| 145 | { | 
|---|
| 146 | if ( (err = setObjAz(fd, newAz)) < 0 || (err = setObjAlt(fd, newAlt)) < 0) | 
|---|
| 147 | { | 
|---|
| 148 | handleError(&HorizontalCoordsWNP, err, "Setting Alt/Az"); | 
|---|
| 149 | return; | 
|---|
| 150 | } | 
|---|
| 151 | //HorizontalCoordsWNP.s = IPS_OK; | 
|---|
| 152 | //HorizontalCoordsWNP.n[0].value = values[0]; | 
|---|
| 153 | //HorizontalCoordsWNP.n[1].value = values[1]; | 
|---|
| 154 | targetAZ  = newAz; | 
|---|
| 155 | targetALT = newAlt; | 
|---|
| 156 |  | 
|---|
| 157 | fs_sexa(azStr, targetAZ, 2, 3600); | 
|---|
| 158 | fs_sexa(altStr, targetALT, 2, 3600); | 
|---|
| 159 |  | 
|---|
| 160 | //IDSetNumber (&HorizontalCoordsWNP, "Attempting to slew to Alt %s - Az %s", altStr, azStr); | 
|---|
| 161 | handleAltAzSlew(); | 
|---|
| 162 | } | 
|---|
| 163 | else | 
|---|
| 164 | { | 
|---|
| 165 | HorizontalCoordsWNP.s = IPS_ALERT; | 
|---|
| 166 | IDSetNumber(&HorizontalCoordsWNP, "Altitude or Azimuth missing or invalid"); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | return; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | LX200Autostar::ISNewNumber (dev, name, values, names, n); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 |  | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 | void LX200_16::ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n) | 
|---|
| 179 | { | 
|---|
| 180 | int index; | 
|---|
| 181 | int err; | 
|---|
| 182 |  | 
|---|
| 183 | if (strcmp (dev, thisDevice)) | 
|---|
| 184 | return; | 
|---|
| 185 |  | 
|---|
| 186 | if (!strcmp(name, FanStatusSP.name)) | 
|---|
| 187 | { | 
|---|
| 188 | if (checkPower(&FanStatusSP)) | 
|---|
| 189 | return; | 
|---|
| 190 |  | 
|---|
| 191 | IUResetSwitch(&FanStatusSP); | 
|---|
| 192 | IUUpdateSwitch(&FanStatusSP, states, names, n); | 
|---|
| 193 | index = getOnSwitch(&FanStatusSP); | 
|---|
| 194 |  | 
|---|
| 195 | if (index == 0) | 
|---|
| 196 | { | 
|---|
| 197 | if ( (err = turnFanOn(fd)) < 0) | 
|---|
| 198 | { | 
|---|
| 199 | handleError(&FanStatusSP, err, "Changing fan status"); | 
|---|
| 200 | return; | 
|---|
| 201 | } | 
|---|
| 202 | } | 
|---|
| 203 | else | 
|---|
| 204 | { | 
|---|
| 205 | if ( (err = turnFanOff(fd)) < 0) | 
|---|
| 206 | { | 
|---|
| 207 | handleError(&FanStatusSP, err, "Changing fan status"); | 
|---|
| 208 | return; | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | FanStatusSP.s = IPS_OK; | 
|---|
| 213 | IDSetSwitch (&FanStatusSP, index == 0 ? "Fan is ON" : "Fan is OFF"); | 
|---|
| 214 | return; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | if (!strcmp(name, HomeSearchSP.name)) | 
|---|
| 218 | { | 
|---|
| 219 | if (checkPower(&HomeSearchSP)) | 
|---|
| 220 | return; | 
|---|
| 221 |  | 
|---|
| 222 | IUResetSwitch(&HomeSearchSP); | 
|---|
| 223 | IUUpdateSwitch(&HomeSearchSP, states, names, n); | 
|---|
| 224 | index = getOnSwitch(&HomeSearchSP); | 
|---|
| 225 |  | 
|---|
| 226 | if (index == 0) | 
|---|
| 227 | seekHomeAndSave(fd); | 
|---|
| 228 | else | 
|---|
| 229 | seekHomeAndSet(fd); | 
|---|
| 230 |  | 
|---|
| 231 | HomeSearchSP.s = IPS_BUSY; | 
|---|
| 232 | IDSetSwitch (&HomeSearchSP, index == 0 ? "Seek Home and Save" : "Seek Home and Set"); | 
|---|
| 233 | return; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | if (!strcmp(name, FieldDeRotatorSP.name)) | 
|---|
| 237 | { | 
|---|
| 238 | if (checkPower(&FieldDeRotatorSP)) | 
|---|
| 239 | return; | 
|---|
| 240 |  | 
|---|
| 241 | IUResetSwitch(&FieldDeRotatorSP); | 
|---|
| 242 | IUUpdateSwitch(&FieldDeRotatorSP, states, names, n); | 
|---|
| 243 | index = getOnSwitch(&FieldDeRotatorSP); | 
|---|
| 244 |  | 
|---|
| 245 | if (index == 0) | 
|---|
| 246 | seekHomeAndSave(fd); | 
|---|
| 247 | else | 
|---|
| 248 | seekHomeAndSet(fd); | 
|---|
| 249 |  | 
|---|
| 250 | FieldDeRotatorSP.s = IPS_OK; | 
|---|
| 251 | IDSetSwitch (&FieldDeRotatorSP, index == 0 ? "Field deRotator is ON" : "Field deRotator is OFF"); | 
|---|
| 252 | return; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | LX200Autostar::ISNewSwitch (dev, name, states, names, n); | 
|---|
| 256 |  | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | void LX200_16::handleAltAzSlew() | 
|---|
| 260 | { | 
|---|
| 261 | int i=0; | 
|---|
| 262 | char altStr[64], azStr[64]; | 
|---|
| 263 |  | 
|---|
| 264 | if (HorizontalCoordsWNP.s == IPS_BUSY) | 
|---|
| 265 | { | 
|---|
| 266 | abortSlew(fd); | 
|---|
| 267 |  | 
|---|
| 268 | // sleep for 100 mseconds | 
|---|
| 269 | usleep(100000); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | if ((i = slewToAltAz(fd))) | 
|---|
| 273 | { | 
|---|
| 274 | HorizontalCoordsWNP.s = IPS_ALERT; | 
|---|
| 275 | IDSetNumber(&HorizontalCoordsWNP, "Slew is not possible."); | 
|---|
| 276 | return; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | HorizontalCoordsWNP.s = IPS_BUSY; | 
|---|
| 280 | HorizontalCoordsRNP.s = IPS_BUSY; | 
|---|
| 281 | fs_sexa(azStr, targetAZ, 2, 3600); | 
|---|
| 282 | fs_sexa(altStr, targetALT, 2, 3600); | 
|---|
| 283 |  | 
|---|
| 284 | IDSetNumber(&HorizontalCoordsWNP, "Slewing to Alt %s - Az %s", altStr, azStr); | 
|---|
| 285 | IDSetNumber(&HorizontalCoordsRNP, NULL); | 
|---|
| 286 | return; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | void LX200_16::ISPoll () | 
|---|
| 290 | { | 
|---|
| 291 | int searchResult=0; | 
|---|
| 292 | double dx, dy; | 
|---|
| 293 | int err; | 
|---|
| 294 |  | 
|---|
| 295 | LX200Autostar::ISPoll(); | 
|---|
| 296 |  | 
|---|
| 297 | switch (HomeSearchSP.s) | 
|---|
| 298 | { | 
|---|
| 299 | case IPS_IDLE: | 
|---|
| 300 | break; | 
|---|
| 301 |  | 
|---|
| 302 | case IPS_BUSY: | 
|---|
| 303 |  | 
|---|
| 304 | if ( (err = getHomeSearchStatus(fd, &searchResult)) < 0) | 
|---|
| 305 | { | 
|---|
| 306 | handleError(&HomeSearchSP, err, "Home search"); | 
|---|
| 307 | return; | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | if (searchResult == 0) | 
|---|
| 311 | { | 
|---|
| 312 | HomeSearchSP.s = IPS_ALERT; | 
|---|
| 313 | IDSetSwitch(&HomeSearchSP, "Home search failed."); | 
|---|
| 314 | } | 
|---|
| 315 | else if (searchResult == 1) | 
|---|
| 316 | { | 
|---|
| 317 | HomeSearchSP.s = IPS_OK; | 
|---|
| 318 | IDSetSwitch(&HomeSearchSP, "Home search successful."); | 
|---|
| 319 | } | 
|---|
| 320 | else if (searchResult == 2) | 
|---|
| 321 | IDSetSwitch(&HomeSearchSP, "Home search in progress..."); | 
|---|
| 322 | else | 
|---|
| 323 | { | 
|---|
| 324 | HomeSearchSP.s = IPS_ALERT; | 
|---|
| 325 | IDSetSwitch(&HomeSearchSP, "Home search error."); | 
|---|
| 326 | } | 
|---|
| 327 | break; | 
|---|
| 328 |  | 
|---|
| 329 | case IPS_OK: | 
|---|
| 330 | break; | 
|---|
| 331 | case IPS_ALERT: | 
|---|
| 332 | break; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | switch (HorizontalCoordsWNP.s) | 
|---|
| 336 | { | 
|---|
| 337 | case IPS_IDLE: | 
|---|
| 338 | break; | 
|---|
| 339 |  | 
|---|
| 340 | case IPS_BUSY: | 
|---|
| 341 |  | 
|---|
| 342 | if ( (err = getLX200Az(fd, ¤tAZ)) < 0 || (err = getLX200Alt(fd, ¤tALT)) < 0) | 
|---|
| 343 | { | 
|---|
| 344 | handleError(&HorizontalCoordsWNP, err, "Get Alt/Az"); | 
|---|
| 345 | return; | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | dx = targetAZ - currentAZ; | 
|---|
| 349 | dy = targetALT - currentALT; | 
|---|
| 350 |  | 
|---|
| 351 | HorizontalCoordsRNP.np[0].value = currentALT; | 
|---|
| 352 | HorizontalCoordsRNP.np[1].value = currentAZ; | 
|---|
| 353 |  | 
|---|
| 354 | // accuracy threshold (3'), can be changed as desired. | 
|---|
| 355 | if ( fabs(dx) <= 0.05 && fabs(dy) <= 0.05) | 
|---|
| 356 | { | 
|---|
| 357 |  | 
|---|
| 358 | HorizontalCoordsWNP.s = IPS_OK; | 
|---|
| 359 | HorizontalCoordsRNP.s = IPS_OK; | 
|---|
| 360 | currentAZ = targetAZ; | 
|---|
| 361 | currentALT = targetALT; | 
|---|
| 362 | IDSetNumber (&HorizontalCoordsWNP, "Slew is complete."); | 
|---|
| 363 | IDSetNumber (&HorizontalCoordsRNP, NULL); | 
|---|
| 364 | } else | 
|---|
| 365 | IDSetNumber (&HorizontalCoordsRNP, NULL); | 
|---|
| 366 | break; | 
|---|
| 367 |  | 
|---|
| 368 | case IPS_OK: | 
|---|
| 369 | break; | 
|---|
| 370 |  | 
|---|
| 371 | case IPS_ALERT: | 
|---|
| 372 | break; | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | void LX200_16::getBasicData() | 
|---|
| 378 | { | 
|---|
| 379 |  | 
|---|
| 380 | getLX200Az(fd, ¤tAZ); | 
|---|
| 381 | getLX200Alt(fd, ¤tALT); | 
|---|
| 382 | IDSetNumber (&HorizontalCoordsRNP, NULL); | 
|---|
| 383 |  | 
|---|
| 384 | LX200Autostar::getBasicData(); | 
|---|
| 385 |  | 
|---|
| 386 | } | 
|---|