<sect2 id="ekos-guide">
    <title>Guide</title>
    <indexterm>
        <primary>Tools</primary>
        <secondary>Ekos</secondary>
        <tertiary>Guide</tertiary>
    </indexterm>
    <screenshot>
        <screeninfo>
            Ekos Guide Module
        </screeninfo>
        <mediaobject>
            <imageobject>
                <imagedata fileref="ekos_guide.png" format="PNG"/>
            </imageobject>
            <textobject>
                <phrase>Ekos Guide Module</phrase>
            </textobject>
        </mediaobject>
    </screenshot>
    <sect3 id="guide-Introduction">
        <title>Introduction</title>
        <para>
            The Ekos Guide Module performs autoguiding using either the powerful built-in guider, or at your option, external guiding via <ulink url="https://openphdguiding.org/">PHD2</ulink> or <ulink url="https://sourceforge.net/projects/linguider/">lin_guider</ulink>. Using the internal guiding, guider camera frames are captured and sent to Ekos for analysis. Depending on the deviations of the stars from their lock positions, guiding pulses corrections are sent to your mount's RA and DEC axes motors. Most of the &GUI; options in the Guide Module are well documented so just hover your mouse over an item and a tooltip will popup with helpful information.
        </para>
    </sect3>

    <sect3 id="guide-Setup">
        <title>Setup</title>
        <screenshot>
            <screeninfo>
                Ekos Profile Guider Selection
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="ekos_profile_guide.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Ekos Profile Guider Selection</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            To perform guiding, you need (one time) to select a Guider in the Profile Editor for the profile you will be using. In the profile editor, choose Internal for the Ekos internal guider, or PHD2.
        </para>
        <screenshot>
            <screeninfo>
              Ekos Guider Optical Train
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="ekos_guide_optical_train.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Ekos Guider Optical Train</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            To perform guiding, you also need to set up your guiding optical train. This 2nd optical train is almost always different from the one you are using with capture/align/focus. See the image above for an example guider optical train configuration. Note that the telescope chosen is the guiding scope, which may be the same as your main telescope if you are using an OAG (off-axis-guiding) or ONAG guiding scheme. The camera selected is, of course, your guiding camera. The Guide Via should be your mount, assuming you are sending guide pulses directly to your mount, or the name of the ST4 device (e.g. your camera) should you be using ST4 guide pulses.
        </para>
        <para>
          Please look at the main guider page shown at the start of this Guider section. There are many parameters that also can be adjusted, some of which are listed below. 
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    <guilabel>Exposure</guilabel>: On the main guiding page you can adjust the guiding exposure time. After the guide-camera completes the exposure, the guide algorithm computes and sends the guide pulses to the mount, then it waits a user-configurable delay, and then then begins its next exposure.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Binning</guilabel>: Pixel binning for the guide image. It usually makes sense to bin the pixels 2x2. The algorithms can still find sub-pixel star positions and send proper guide pulses to the mount.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Box</guilabel>: This only is applicable to guide algorithms other than MultiStar, and MultiStar is the recommended guiding scheme. Size of the box enclosing the guide star. Select a suitable size that is neither too large or too small for the selected star.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Directions</guilabel>: Typically you want to keep all the directions boxes checked. Unchecking them will disable guiding in those directions. For instance it is possible to disable DEC guiding in the North direction.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Dark</guilabel>: Check this to enable dark-frame corrections to your guiding image. See below.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Clear Calibration</guilabel>: Check this to delete your calibration data. See the calibration section below.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Subframe, AutoStar</guilabel>: These only apply to guide algorithms other than MultiStar, and MultiStar is the recommended guiding scheme. 
                </para>
            </listitem>
        </itemizedlist>
    </sect3>
    <sect3 id="guide-calibration">
        <title>Calibration</title>

        <screenshot>
            <screeninfo>
                Calibration Settings
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="guide_calibration_settings.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Calibration Settings</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
          Autoguiding is a two-step process: Calibration &amp; Guiding. Calibration is needed for the scheme to understand the camera's orientation, relative to the RA and DEC axes, and also the effects of guide pulses (e.g. how much a 100ms RA guide pulse will typically move the RA axis). Once it estimates these values, the guider can correct the mount's position effectively. You can see calibrated values for those parameters in the above image in the "Calibrated Values" section.
        </para>
        <para>
          Similar to other guiders, we recommend that you carefully calibrate once, and then only re-calibrate when necessary. It is necessary to re-calibrate when the camera is moved (e.g. rotate) relative to the mount. It should not be necessary to calibrate every time you slew the mount.  You should calibrate when pointing near the Meridian and along the Celestial Equator (probably just West of it). Guiding (and guide calibration) is problematic near the pole--it probably won't work. <ulink url="https://openphdguiding.org/PHD2_BestPractices_2019-12.pdf">This slide show</ulink> contains good advice on how to calibrate the Internal Guider and/or PHD2.
        </para>
        <para>
          The important options on the calibration options page (above) are:
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    <guilabel>Pulse Size</guilabel>: should be large enough to move your image a few pixels.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Re-using Calibration</guilabel>: There are two checkboxes related to keeping your calibration. We recommend checking "Store and re-use guide calibration when possible", and un-checking "Reset Guide Calibration After Each Mount Slew".
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Reverse DEC...</guilabel>: It is also important to check or un-check (it is mount dependent) "Reverse DEC on pier-side change when re-using calibration". To find out the right setting for your mount, you need to successfully calibrate on one pier side, make sure guiding is working well on that side, then switch to the other side. Guide for a minute or two. If DEC runs away, then you probably have the wrong setting for the "Reverse DEC..." checkbox.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Max Move, Iterations</guilabel>: We recommend you keep iterations large (e.g. 10) and Max Move large (e.g. 20+ pixels). This way you should get a good estimate of the guiding calibration parameters. Calibration should be something you do rarely, so it is best to take a little extra time and get right.
                </para>
            </listitem>
        </itemizedlist>
        <para>
          To (re)calibrate, clear your calibration on the main guiding page, and then simply click on the <guibutton>Guide</guibutton> button. Note that if calibration was already completed successfully before, and you didn't clear the calibration, and you are re-using calibrations, then the autoguiding process will begin immediately, otherwise, it will start the calibration process. 
       </para>
        <para>
            Ekos begins the calibration process by sending pulses to move the mount in RA and DEC. It pulses out the RA axis, then pulses it back in. After that it moves a little in DEC to clear and backlash that might exist, and then pulses out and back in for DEC. To view this graphically, click on the "Calibration Plot" subtab on the main guiding page.
        </para>
        <sect4 id="guide-calibration-failures">
        <title>Calibration Failures</title>
            <para>
                Calibration can fail for a variety of reasons. To improve the chances of success, try the tips below.
            </para>
        <itemizedlist>
            <listitem>
                <para>
                    Bad sky conditions. If your sky condition are not great, it may not be worth fighting guiding/calibration.
                </para>
            </listitem>
            <listitem>
                <para>
                    Guide camera focus. 
                </para>
            </listitem>
            <listitem>
                <para>
                    Leave algorithm to the default value (<guimenuitem>SEP MultiStar</guimenuitem>) in the Guide Option tab.
                </para>
            </listitem>
            <listitem>
                <para>
                    Try the "Guide-Default" SEP star-detection parameters (in the Guide Option tab) and adjust them if necessary. 
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Better Polar Alignment</guilabel>: This is critical to the success of any astrophotography session. Use the Ekos <link linkend="ekos-align-polaralignment">Polar Alignment procedure</link> in the <guilabel>Align</guilabel> module.
                </para>
            </listitem>
            <listitem>
                <para>
                    <guilabel>Set binning to 2x2</guilabel>: Binning improves SNR and is often very important to the success of the calibration and guiding procedures.
                </para>
            </listitem>
            <listitem>
                <para>
                    Take dark frames to reduce noise.
                </para>
            </listitem>
        </itemizedlist>
    </sect4>
    </sect3>
    <sect3 id="guide-guiding">
        <title>Guiding</title>

        <screenshot>
            <screeninfo>
                Guide Settings
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="guide_guide_settings.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Guide Settings</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            Once the calibration process is completed successfully, guiding begins automatically. The guiding performance is displayed in the <guilabel>Drift Graphics</guilabel> region where <guilabel>Green</guilabel> reflects deviations in RA and <guilabel>Blue</guilabel> deviations in DEC. The colors of the RA/DE lines can be changed in <link linkend="colors">&kstars; color scheme</link> in &kstars; settings dialog. The vertical axis denotes the deviation in arcsecs from the lock position and the horizontal axis denotes time. You can hover over the line to get the exact deviation at this particular point in time. You can also zoom and drag/pan the graph to inspect a specific region of the graph. Another convenient place to examine guiding performance is in the Analyze tab.
        </para>
        <para>
            Ekos can utilize multiple algorithms to determine the drift from the (original) lock position, but by far the most accurate is the (default) SEP MultiStar algorithm. It uses the detected position of many stars (in the above settings, up to 50) to determine its best estimate for the current drift. It is dependent on accurate star detection. Thus, it may be important to adjust star-detection parameters. Start with the default Guide-Default SEP profile, and optionally edit its parameters if you feel stars are not being detected accurately.
        </para>
        <para>
          Here are some of the parameters you may want to adjust. Again, good advice in choosing parameters is available on the internet, e.g. from <ulink url="https://openphdguiding.org/PHD2_BestPractices_2019-12.pdf">the above slideshow</ulink>.
        </para>
        <itemizedlist>
            <listitem>
                <para>
                    Aggressiveness. How quickly you want the guider to correct the error. Values of 0.5 or 0.6 are usually best (i.e. correcting about half the observed error). Unintuitively, it seems that correcting 100% of the error can cause poor performance as the guider will often oscillate with overcorrections.
                </para>
            </listitem>
            <listitem>
                <para>
                    Algorithm. we strongly recommend you use the most up-to-date algorith: SEP MultiStar. Pretty much the only reason not to would be if you can't get your SEP star-detection to perform adequately.
                </para>
            </listitem>
            <listitem>
                <para>
                    SEP Profile. Start with Guide-Default, though you may choose others if you have very large or small stars (in terms of number of pixels in diameter).
                </para>
            </listitem>
        </itemizedlist>
    </sect3>
    <sect3 id="guide-dithering">
        <title>Dithering</title>

        <screenshot>
            <screeninfo>
                Dithering Settings
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="ekos_guide_dithering_settings.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Dithering Settings</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            To enable automatic dithering between frames, make sure to check the <guilabel>Dither</guilabel> checkbox. By default, Ekos should dither (&ie; move) the guiding box by up to 3 pixels after every N frames captured in <link linkend="ekos-capture">Ekos Capture Module</link>. The motion duration and direction are randomized. Since the guiding performance can oscillate immediately after dithering, you can set the appropriate <guilabel>Settle</guilabel> duration to wait after dither is complete before resuming the capture process. In rare cases where the dithering process can get stuck in an endless loop, set the appropriate <guilabel>Timeout</guilabel> to abort the process. But even if dithering fails, you can select whether this failure should terminate the autoguiding process or not. Toggle <guilabel>Abort Autoguide on failure</guilabel> to select the desired behavior.
        </para>
        <para>
          Dithing does not result in a long wander from the original target position. Ekos keeps track of the original and current target positions, and moves the target back towards the original target should the position have drifted too far.
        </para>
        <para>
            One-pulse dithering is an interesting quicker option which sends a pulse to dither, but does not verify that the dither reached its desired location. It is possible that the dithering for any given dither isn't as much as desired, but the overall effect should be good.
        </para>
        <para>
            Non-guide dithering is also supported. This is useful when no guide camera is available or when performing short exposures. In this case, the mount can be commanded to dither in a random direction for up to the pulse specified in the <guilabel>Non-Guide Dither Pulse</guilabel> option.
        </para>
    </sect3>
    <sect3 id="guide-drift-graphics">
        <title>Drift Graphics</title>
        <screenshot>
            <screeninfo>
                Drift Graphics
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="guide_drift_graphics.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Drift Graphics</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            The drift graphics is a very useful tool to monitor the guiding performance. It is a 2D plot of guiding <emphasis>deviations</emphasis> and <emphasis>corrections</emphasis>. By default, only the guiding deviations in RA and DE are displayed. The horizontal axis is the time in seconds since the autoguiding process was started while the vertical axis plots the guiding drift/deviation in arcsecs for each axis. Guiding corrections (pulses) can also be plotted in the same graph and you can enable them by checking the <guilabel>Corr</guilabel> checkbox below each Axis. The corrections are plotted as shaded areas in the background with the same color as that of the axis.
        </para>
        <para>
            You can pan and zoom the plot, and when hovering the mouse over the graph, a tooltip is displayed containing information about this specific point in time. It contains the guiding drift and any corrections made, in addition to the local time, this event was recorded. A vertical slider to the right of the image can be used to adjust the height of the secondary Y-axis for pulses corrections.</para>
        <para>
            The <guilabel>Trace</guilabel> horizontal slider at the bottom can be used to scroll through the guide history. Alternatively, you can click the <guilabel>Max</guilabel> checkbox to lock the graph onto the latest point so that the drift graphics autoscrolls. The buttons to the right of the slider are used for autoscaling the graphs, exporting the guide data to a CSV file, clearing all the guide data, and for scaling the target in the <guilabel>Drift Plot</guilabel>. Furthermore, the guide graph includes a label to indicate when a dither occurred so the user knows guiding was not bad at those points.
        </para>
        <para>
            The colors of each axis can be customized in <link linkend="colors">&kstars; Settings color scheme</link>.
        </para>
    </sect3>

    <sect3 id="guide-drift-plot">
        <title>Drift Plot</title>
        <para>
            A bulls-eye scatter plot can be used to gauge the <emphasis>accuracy</emphasis> of the overall guiding performance. It is composed of three concentric rings of varying radii with the central green ring having a default radius of 2 arcsecs. The last RMS value is plotted as <inlinemediaobject><imageobject><imagedata fileref="add-circle.png" format="PNG"/></imageobject></inlinemediaobject> with its color reflecting which concentric ring it falls within. You can change the radius of the innermost green circle by adjusting the drift plot accuracy.
        </para>
    </sect3>

    <sect3 id="guide-gpg">
        <title>Guiding with GPG</title>
        <screenshot>
            <screeninfo>
                GPG Guiding
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="ekos_guide_gpg_settings.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Guiding with GPG</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            With GPG guiding, the internal guider uses predictive and adaptive guiding for the RA axis. This adaptively models the periodic error of the mount, and adds its predicted contribution to each guide pulse. 
        </para>
        <para>
          The main settings to consider are Major Period and Estimate Period. If you know the worm period for your mount, perhaps by examining <ulink url="g/phd2/wiki/Mount-Worm-Period-Info">this table</ulink>, then uncheck Estimate Period, and enter your known Major Period. If not, then check Estimate Period.  Intra-frame dark guiding can be used to "spread out the GPG prediction. For instance, if you guide at 5s, you can set the dark guiding interval to 1s and its prediction is pulsed every second, but the guiding drift correction would be sent every 5s. In this way, it outputs the predicted corrections much faster than the guide camera exposure rate, effectively performing periodic error correction and allowing longer guide camera exposures. All the other parameters are best left to defaults.
        </para>
    </sect3>
    <sect3 id="guide-dark-frames">
        <title>Dark Frames</title>
        <para>
            Dark frames can be helpful to reduce noise in your guide frames. If you choose to use this option, then it is recommended that you take dark frames before you begin your calibration or guiding procedure. To take a dark frame, check the <guilabel>Dark</guilabel> checkbox and then click <guibutton>Capture</guibutton>. For the first time this is performed, Ekos will ask you about your camera shutter. If your camera does not have a shutter, then Ekos will warn you anytime you take a dark frame to cover your camera/telescope before proceeding with the capture. On the other hand, if the camera already includes a shutter, then Ekos will directly proceed with taking the dark frame. All dark frames are automatically saved to Ekos Dark Frame Library. By default, the Dark Library keeps reusing dark frames for 30 days after which it will capture new dark frames. This value is configurable and can be adjusted in <link linkend="ekos">Ekos settings</link> in the &kstars; settings dialog.
        </para>
        <screenshot>
            <screeninfo>
                Ekos Dark frames library
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="dark_library.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Ekos Dark frames library</phrase>
                </textobject>
            </mediaobject>
        </screenshot>
        <para>
            It is recommended to take dark frames covering several binning and exposure values so that they may be reused transparently by Ekos whenever needed.
        </para>
    </sect3>
    
    <sect3 id="guide-phd2-support">
        <title>PHD2 Support</title>
        <para>
            You can opt to select external PHD2 application to perform guiding instead of the built-in guider.
        </para>
        <screenshot>
            <screeninfo>
                Ekos Guide PHD2 settings
            </screeninfo>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="ekos_guide_phd2.png" format="PNG"/>
                </imageobject>
                <textobject>
                    <phrase>Ekos Guide PHD2 settings</phrase>
                </textobject>
            </mediaobject>
        </screenshot>

        <para>
            If PHD2 is selected, the <guibutton>Connect</guibutton> and <guibutton>Disconnect</guibutton> buttons are enabled to allow you to establish a connection with the PHD2 server. You can control PHD2 exposure and DEC guide settings. When clicking <guibutton>Guide</guibutton>, PHD2 should perform all the required actions to start the guiding process. PHD2 <emphasis role="bold">must</emphasis> be started and configured <emphasis>before</emphasis> Ekos.
        </para>
        <para>
            After launching PHD2, select your INDI equipment and set their options. From Ekos, connect to PHD2 by clicking the <guibutton>Connect</guibutton> button. On startup, Ekos will attempt to automatically connect to PHD2. Once the connection is established, you may begin the guiding immediately by click on the <guibutton>Guide</guibutton> button. PHD2 performs calibration if necessary. If dithering is selected, PHD2 is commanded to dither given the offset pixels indicated, and once guiding is settled and stable, the capture process in Ekos resumes.
        </para>
    </sect3>
    <sect3 id="guide-logs">
        <title>Guiding Logs</title>
        <para>
            Ekos' internal guider saves a CSV guide log in PHD2 format data that can be useful for analysis of the mount's performance. In Linux this is stored under <filename>~/.local/share/kstars/guidelogs/</filename>. This log is only available when using Ekos' internal guider. It should be compatible with <ulink url="https://openphdguiding.org/phd2-log-viewer/">PHD2's guide log viewer</ulink>.
        </para>
    </sect3>
</sect2>
