File indexing completed on 2024-04-14 03:42:13

0001 /*
0002     SPDX-FileCopyrightText: 2017 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ekos.h"
0008 
0009 #include <gsl/gsl_fit.h>
0010 #include <gsl/gsl_vector.h>
0011 #include <gsl/gsl_matrix.h>
0012 #include <gsl/gsl_multifit.h>
0013 
0014 #include <QDebug>
0015 
0016 namespace Ekos
0017 {
0018 const QString getGuideStatusString(GuideState state, bool translated)
0019 {
0020     return translated ? i18n(guideStates[state]) : guideStates[state];
0021 }
0022 const QString getCaptureStatusString(CaptureState state, bool translated)
0023 {
0024     return translated ? i18n(captureStates[state]) : captureStates[state];
0025 }
0026 const QString getFocusStatusString(FocusState state, bool translated)
0027 {
0028     return translated ? i18n(focusStates[state]) : focusStates[state];
0029 }
0030 const QString getAlignStatusString(AlignState state, bool translated)
0031 {
0032     return translated ? i18n(alignStates[state]) : alignStates[state];
0033 }
0034 const QString getFilterStatusString(FilterState state, bool translated)
0035 {
0036     return translated ? i18n(filterStates[state]) : filterStates[state];
0037 }
0038 const QString getSchedulerStatusString(SchedulerState state, bool translated)
0039 {
0040     return translated ? i18n(schedulerStates[state]) : schedulerStates[state];
0041 }
0042 
0043 /* Taken from https://codereview.stackexchange.com/questions/71300/wrapper-function-to-do-polynomial-fits-with-gsl */
0044 std::vector<double> gsl_polynomial_fit(const double *const data_x, const double *const data_y, const int n,
0045                                        const int order, double &chisq)
0046 {
0047     int status = 0;
0048     std::vector<double> vc;
0049     gsl_vector *y, *c;
0050     gsl_matrix *X, *cov;
0051     y   = gsl_vector_alloc(n);
0052     c   = gsl_vector_alloc(order + 1);
0053     X   = gsl_matrix_alloc(n, order + 1);
0054     cov = gsl_matrix_alloc(order + 1, order + 1);
0055 
0056     for (int i = 0; i < n; i++)
0057     {
0058         for (int j = 0; j < order + 1; j++)
0059         {
0060             gsl_matrix_set(X, i, j, pow(data_x[i], j));
0061         }
0062         gsl_vector_set(y, i, data_y[i]);
0063     }
0064 
0065     // Must turn off error handler or it aborts on error
0066     gsl_set_error_handler_off();
0067 
0068     gsl_multifit_linear_workspace *work = gsl_multifit_linear_alloc(n, order + 1);
0069     status                              = gsl_multifit_linear(X, y, c, cov, &chisq, work);
0070 
0071     if (status != GSL_SUCCESS)
0072     {
0073         qDebug() << Q_FUNC_INFO << "GSL multifit error:" << gsl_strerror(status);
0074         return vc;
0075     }
0076 
0077     gsl_multifit_linear_free(work);
0078 
0079     for (int i = 0; i < order + 1; i++)
0080     {
0081         vc.push_back(gsl_vector_get(c, i));
0082     }
0083 
0084     gsl_vector_free(y);
0085     gsl_vector_free(c);
0086     gsl_matrix_free(X);
0087     gsl_matrix_free(cov);
0088 
0089     return vc;
0090 }
0091 }
0092 
0093 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::CommunicationStatus &source)
0094 {
0095     argument.beginStructure();
0096     argument << static_cast<int>(source);
0097     argument.endStructure();
0098     return argument;
0099 }
0100 
0101 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::CommunicationStatus &dest)
0102 {
0103     int a;
0104     argument.beginStructure();
0105     argument >> a;
0106     argument.endStructure();
0107     dest = static_cast<Ekos::CommunicationStatus>(a);
0108     return argument;
0109 }
0110 
0111 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::CaptureState &source)
0112 {
0113     argument.beginStructure();
0114     argument << static_cast<int>(source);
0115     argument.endStructure();
0116     return argument;
0117 }
0118 
0119 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::CaptureState &dest)
0120 {
0121     int a;
0122     argument.beginStructure();
0123     argument >> a;
0124     argument.endStructure();
0125     dest = static_cast<Ekos::CaptureState>(a);
0126     return argument;
0127 }
0128 
0129 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::FocusState &source)
0130 {
0131     argument.beginStructure();
0132     argument << static_cast<int>(source);
0133     argument.endStructure();
0134     return argument;
0135 }
0136 
0137 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::FocusState &dest)
0138 {
0139     int a;
0140     argument.beginStructure();
0141     argument >> a;
0142     argument.endStructure();
0143     dest = static_cast<Ekos::FocusState>(a);
0144     return argument;
0145 }
0146 
0147 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::GuideState &source)
0148 {
0149     argument.beginStructure();
0150     argument << static_cast<int>(source);
0151     argument.endStructure();
0152     return argument;
0153 }
0154 
0155 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::GuideState &dest)
0156 {
0157     int a;
0158     argument.beginStructure();
0159     argument >> a;
0160     argument.endStructure();
0161     dest = static_cast<Ekos::GuideState>(a);
0162     return argument;
0163 }
0164 
0165 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::AlignState &source)
0166 {
0167     argument.beginStructure();
0168     argument << static_cast<int>(source);
0169     argument.endStructure();
0170     return argument;
0171 }
0172 
0173 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::AlignState &dest)
0174 {
0175     int a;
0176     argument.beginStructure();
0177     argument >> a;
0178     argument.endStructure();
0179     dest = static_cast<Ekos::AlignState>(a);
0180     return argument;
0181 }
0182 
0183 QDBusArgument &operator<<(QDBusArgument &argument, const Ekos::SchedulerState &source)
0184 {
0185     argument.beginStructure();
0186     argument << static_cast<int>(source);
0187     argument.endStructure();
0188     return argument;
0189 }
0190 
0191 const QDBusArgument &operator>>(const QDBusArgument &argument, Ekos::SchedulerState &dest)
0192 {
0193     int a;
0194     argument.beginStructure();
0195     argument >> a;
0196     argument.endStructure();
0197     dest = static_cast<Ekos::SchedulerState>(a);
0198     return argument;
0199 }