home *** CD-ROM | disk | FTP | other *** search
/ The Starbase One Astronomy & Space Collection / STARBASE_ONE.ISO / a96 / disk07 / altaz.exe / GUIDE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-23  |  4.4 KB  |  215 lines

  1. #include <string.h>
  2. #include <values.h>
  3. #include <math.h>
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6. #include <fstream.h>
  7. #include <strstrea.h>
  8. #include <conio.h>
  9. #include <dos.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13.  
  14. #include "common.h"
  15. #include "video.hpp"
  16. #include "atimes.hpp"
  17. #include "pport.hpp"
  18. #include "handpad.hpp"
  19. #include "coord.hpp"
  20. #include "cmosclk.hpp"
  21. #include "steppers.hpp"
  22. #include "pec.hpp"
  23. #include "guide.hpp"
  24.  
  25. Guide::Guide
  26. (
  27.     const int GuideArcsecSec,
  28.     const int FullstepsPerPECArray,
  29.     const int InvertOutput,
  30.     const int MaxDelay,
  31.     const int MinDelay,
  32.     const int HsDelayX,
  33.     const int InterruptHs,
  34.     const int HoldReps,
  35.     const int MsRepsTick,
  36.     const int MsDelayX,
  37.     const int MsPause,
  38.     const int Ms,
  39.     int* PWM,
  40.     const unsigned PPortAddr
  41. ):
  42.     PEC
  43.     (
  44.         FullstepsPerPECArray,
  45.         InvertOutput,
  46.         MaxDelay,
  47.         MinDelay,
  48.         HsDelayX,
  49.         InterruptHs,
  50.         HoldReps,
  51.         MsRepsTick,
  52.         MsDelayX,
  53.         MsPause,
  54.         Ms,
  55.         PWM,
  56.         PPortAddr
  57.     ),
  58.     GuideArcsecSec(GuideArcsecSec)
  59. {
  60.     InitGuide();
  61. }
  62.  
  63. void Guide::InitGuide(void)
  64. {
  65.     GuideFlag=Yes;
  66.     GuideRadTick=GuideArcsecSec/RadToArcsec/ClockTicksSec;
  67.     AccumGuide.Alt=AccumGuide.Az=0;
  68.     GuideIxA=GuideIxZ=0;
  69. }
  70.  
  71. void Guide::EndAndSaveGuide(void)
  72. {
  73.     int Ix;
  74.  
  75.     GuideFlag=Off;
  76.     AccumGuide.Alt=AccumGuide.Az=0;
  77.     ofstream Output(GuideFile);
  78.     for(Ix=0;Ix<GuideIxA;Ix++)
  79.         Output<<(int)GuideA[Ix].PECIx<<" "<<GuideA[Ix].TenthsArcsec<<endl;
  80.     Output<<endl;
  81.     for(Ix=0;Ix<GuideIxZ;Ix++)
  82.         Output<<(int)GuideZ[Ix].PECIx<<" "<<GuideZ[Ix].TenthsArcsec<<endl;
  83. }
  84.  
  85. void Guide::ProcessGuide(void)
  86. {
  87.     static int SoundA,SoundZ;
  88.     static int HoldPECIxA,HoldPECIxZ;
  89.  
  90.     // only write to file if PEC index changes
  91.     if(PECIxA!=HoldPECIxA)
  92.     {
  93.         if(!PECIxA)
  94.         {
  95.             sound(1000);
  96.             SoundA=!SoundA;
  97.         }
  98.         else
  99.             if(SoundA)
  100.             {
  101.                 SoundA=!SoundA;
  102.                 nosound();
  103.             }
  104.         GuideA[GuideIxA].PECIx=PECIxA;
  105.         GuideA[GuideIxA].TenthsArcsec=AccumGuide.Alt*RadToTenthsArcsec;
  106.         HoldPECIxA=PECIxA;
  107.         GuideIxA++;
  108.     }
  109.     if(PECIxZ!=HoldPECIxZ)
  110.     {
  111.         if(!PECIxZ)
  112.         {
  113.             sound(500);
  114.             SoundZ=!SoundZ;
  115.         }
  116.         else
  117.             if(SoundZ)
  118.             {
  119.                 SoundZ=!SoundZ;
  120.                 nosound();
  121.             }
  122.         GuideZ[GuideIxZ].PECIx=PECIxZ;
  123.         GuideZ[GuideIxZ].TenthsArcsec=AccumGuide.Az*RadToTenthsArcsec;
  124.         HoldPECIxZ=PECIxZ;
  125.         GuideIxZ++;
  126.     }
  127.     if(GuideIxA>=MaxGuideIx || GuideIxZ>=MaxGuideIx)
  128.         EndAndSaveGuide();
  129. }
  130.  
  131. void Guide::AddGuideToPEC(int ArrayIx, GuideArray Array[], int PEC[])
  132. {
  133.     int IxA,IxB;
  134.     int IxOffset;
  135.     int K;
  136.     int Drift;
  137.     double Slide;
  138.     double SlideDrift;
  139.     int ZeroIx[1+MaxGuideIx/PECSize];
  140.     int Ix;
  141.     int ZeroCount;
  142.  
  143.     // store all Array.PECIx==0 in ZeroIx[] array;
  144.     // ZeroIx[0] contains 1st occurance of PECIx=0, ZeroIx[1] next occurance...
  145.     // ZeroCount is count of all zero PECIx's
  146.     for(IxA=0,Ix=0;IxA<ArrayIx;IxA++)
  147.         if(!Array[IxA].PECIx)
  148.             ZeroIx[Ix++]=IxA;
  149.     ZeroCount=Ix;
  150.     if(ZeroCount>=2)
  151.     {
  152.         // copy repetitions of PEC into 1st PEC
  153.         for(Ix=ZeroCount;Ix>2;Ix--)
  154.         {
  155.             IxOffset=PECSize*(Ix-2);
  156.             for(IxA=ZeroIx[0];IxA<ZeroIx[1];IxA++)
  157.                 Array[IxA].TenthsArcsec+=Array[IxA+IxOffset].TenthsArcsec;
  158.         }
  159.         // start PEC with 1st TenthsArcsec value = 0
  160.         K=Array[ZeroIx[0]].TenthsArcsec;
  161.         for(IxA=ZeroIx[0];IxA<ZeroIx[1];IxA++)
  162.             Array[IxA].TenthsArcsec-=K;
  163.         // eliminate drift, ie, final TenthsArcsec should = 0
  164.         Drift=Array[ZeroIx[1]-1].TenthsArcsec;
  165.         for(IxA=ZeroIx[0],IxB=0;IxA<ZeroIx[1];IxA++,IxB++)
  166.         {
  167.             Slide=(double)IxB/PECSize;
  168.             SlideDrift=Slide*Drift;
  169.             Array[IxA].TenthsArcsec-=(int)SlideDrift;
  170.         }
  171.         // add to PEC
  172.         for(IxA=ZeroIx[0];IxA<ZeroIx[1];IxA++)
  173.             PEC[Array[IxA].PECIx]+=Array[IxA].TenthsArcsec;
  174.     }
  175. }
  176.  
  177. void Guide::Test(void)
  178. {
  179.     char ValueA[ValueSize];
  180.     char ValueB[ValueSize];
  181.     int A,B;
  182.     int AFlag=Yes;
  183.  
  184.     GuideIxA=0;
  185.     ifstream File(GuideFile);
  186.     if(File)
  187.     {
  188.         while(!File.eof() && GuideIxA<MaxGuideIx && AFlag)
  189.         {
  190.             File>>ValueA>>ValueB;
  191.             GuideA[GuideIxA].PECIx=atoi(ValueA);
  192.             GuideA[GuideIxA].TenthsArcsec=atoi(ValueB);
  193.             if(GuideIxA)
  194.             {
  195.                 // check for end of altitude portion of GuideFile
  196.                 A=GuideA[GuideIxA].PECIx;
  197.                 B=GuideA[GuideIxA-1].PECIx;
  198.                 if(A==(B-1) || A==(B+1) || (A==0)&&(B==(PECSize-1))
  199.                 || (B==0)&&(A==(PECSize-1)))
  200.                     ;
  201.                 else
  202.                     AFlag=No;
  203.             }
  204.             GuideIxA++;
  205.         }
  206.         AddGuideToPEC(GuideIxA,GuideA,PECA);
  207.         EndAndSaveGuide();
  208.         SavePEC();
  209.     }
  210.  
  211.     else
  212.         cout<<GuideFile<<" not found"<<endl;
  213. }
  214.  
  215.