ParameterList.cc 8.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of class OpalRT::ParameterList
 *   @author Bryan Godbolt <godbolt@ualberta.ca>
 */

30 31 32 33
#include "ParameterList.h"
using namespace OpalRT;

ParameterList::ParameterList()
34
    :params(new QMap<int, QMap<QGCParamID, Parameter> >),
35
    paramList(new QList<QList<Parameter*> >())
36
{
37
//    params = new QMap<int, QMap<QGCParamID, Parameter> >;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    /* Populate the map with parameter names.  There is no elegant way of doing this so all
       parameter paths and names must be known at compile time and defined here.
       Note: This function is written in a way that calls a lot of copy constructors and is
           therefore not particularly efficient.  However since it is only called once memory
           and computation time are sacrificed for code clarity when adding and modifying
           parameters.
           When defining the path, the trailing slash is necessary
       */
    Parameter *p;
    /* Component: Navigation Filter */
    p = new Parameter("avionics_src/sm_ampro/NAV_FILT_INIT/",
                            "Value",
                            OpalRT::NAV_ID,
                            QGCParamID("NAV_FILT_INIT"));
    (*params)[OpalRT::NAV_ID].insert(p->getParamID(), *p);
    delete p;

    p = new Parameter("avionics_src/sm_ampro/Gain/",
                            "Gain",
                            OpalRT::NAV_ID,
                            QGCParamID("TEST_OUTP_GAIN"));
    (*params)[OpalRT::NAV_ID].insert(p->getParamID(), *p);
    delete p;

    /* Component: Log Facility */
    p = new Parameter("avionics_src/sm_ampro/LOG_FILE_ON/",
                            "Value",
                            OpalRT::LOG_ID,
                            QGCParamID("LOG_FILE_ON"));
    (*params)[OpalRT::LOG_ID].insert(p->getParamID(), *p);
    delete p;

    /* Get a list of the available parameters from opal-rt */
    QMap<QString, unsigned short> *opalParams = new QMap<QString, unsigned short>;
    getParameterList(opalParams);

    /* Iterate over the parameters we want to use in qgc and populate their ids */
    QMap<int, QMap<QGCParamID, Parameter> >::iterator componentIter;
    QMap<QGCParamID, Parameter>::iterator paramIter;
    QString s;
    for (componentIter = params->begin(); componentIter != params->end(); ++componentIter)
    {
81
        paramList->append(QList<Parameter*>());
82 83
        for (paramIter = (*componentIter).begin(); paramIter != (*componentIter).end(); ++paramIter)
        {
84
            paramList->last().append(paramIter.operator ->());
85 86 87 88
            s = (*paramIter).getSimulinkPath() + (*paramIter).getSimulinkName();
            if (opalParams->contains(s))
            {
                (*paramIter).setOpalID(opalParams->value(s));
89
//                qDebug() << __FILE__ << " Line:" << __LINE__ << ": Successfully added " << s;
90 91 92
            }
            else
            {
93
                qWarning() << __FILE__ << " Line:" << __LINE__ << ": " << s << " was not found in param list";
94 95 96 97 98 99 100 101 102 103
            }
        }
    }
    delete opalParams;

}

ParameterList::~ParameterList()
{
    delete params;
104
    delete paramList;
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/**
  Get the list of parameters in the simulink model.  This function does not require
  any prior knowlege of the parameters.  It works by first calling OpalGetParameterList to
  get the number of paramters, then allocates the required amount of memory and then gets
  the paramter list using a second call to OpalGetParameterList.
  */
void ParameterList::getParameterList(QMap<QString, unsigned short> *opalParams)
{
    /* inputs */
    unsigned short allocatedParams=0;
    unsigned short allocatedPathLen=0;
    unsigned short allocatedNameLen=0;
    unsigned short allocatedVarLen=0;

    /* outputs */
    unsigned short numParams;
    unsigned short *idParam=NULL;
    unsigned short maxPathLen;
    char **paths=NULL;
    unsigned short maxNameLen;
    char **names=NULL;
    unsigned short maxVarLen;
    char **var=NULL;

    int returnValue;

    returnValue = OpalGetParameterList(allocatedParams, &numParams, idParam,
                             allocatedPathLen, &maxPathLen, paths,
                             allocatedNameLen, &maxNameLen, names,
                             allocatedVarLen, &maxVarLen, var);
    if (returnValue!=E2BIG)
    {
139 140
//        OpalRT::setLastErrorMsg();
        OpalRT::OpalErrorMsg::displayLastErrorMsg();
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        return;
    }

    // allocate memory for parameter list

    idParam = new unsigned short[numParams];
    allocatedParams = numParams;

    paths = new char*[numParams];
    for (int i=0; i<numParams; i++)
        paths[i]=new char[maxPathLen];
    allocatedPathLen = maxPathLen;

    names = new char*[numParams];
    for (int i=0; i<numParams; i++)
        names[i] = new char[maxNameLen];
    allocatedNameLen = maxNameLen;

    var = new char*[numParams];
    for (int i=0; i<numParams; i++)
        var[i] = new char[maxVarLen];
    allocatedVarLen = maxVarLen;

    returnValue = OpalGetParameterList(allocatedParams, &numParams, idParam,
                             allocatedPathLen, &maxPathLen, paths,
                             allocatedNameLen, &maxNameLen, names,
                             allocatedVarLen, &maxVarLen, var);

    if (returnValue != EOK)
    {
171 172
//        OpalRT::setLastErrorMsg();
        OpalRT::OpalErrorMsg::displayLastErrorMsg();
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        return;
    }

    QString path, name;
    for (int i=0; i<numParams; ++i)
    {
        path.clear();
        path.append(paths[i]);
        name.clear();
        name.append(names[i]);
        if (path[path.size()-1] == '/')
            opalParams->insert(path+name, idParam[i]);
        else
            opalParams->insert(path+'/'+name, idParam[i]);
    }
188
//     Dump out the list of parameters
189 190 191 192 193 194
//    QMap<QString, unsigned short>::const_iterator paramPrint;
//    for (paramPrint = opalParams->begin(); paramPrint != opalParams->end(); ++paramPrint)
//        qDebug() << paramPrint.key();


}
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
int ParameterList::indexOf(const Parameter &p)
{
    // incase p is a copy of the actual parameter we want (i.e., addresses differ)
    Parameter *pPtr = &((*params)[p.getComponentID()][p.getParamID()]);

    QList<QList<Parameter*> >::const_iterator iter;
    int index = -1;
    for (iter = paramList->begin(); iter != paramList->end(); ++iter)
    {
        if ((index = (*iter).indexOf(pPtr)) != -1)
            return index;
    }
    return index;
}


ParameterList::const_iterator::const_iterator(QList<Parameter> paramList)
{
    this->paramList = QList<Parameter>(paramList);
    index = 0;
}

ParameterList::const_iterator::const_iterator(const const_iterator &other)
{
    paramList = QList<Parameter>(other.paramList);
    index = other.index;
}

ParameterList::const_iterator ParameterList::begin() const
{
    QList<QMap<QGCParamID, Parameter> > compList = params->values();
    QList<Parameter> paramList;
    QList<QMap<QGCParamID, Parameter> >::const_iterator compIter;
    for (compIter = compList.begin(); compIter != compList.end(); ++compIter)
        paramList.append((*compIter).values());
    return const_iterator(paramList);
}

ParameterList::const_iterator ParameterList::end() const
235 236
{
    const_iterator iter = begin();
237 238 239 240 241 242 243 244 245 246
    return iter+=iter.paramList.size();
}

int ParameterList::count()
{
    int count = 0;
    QList<QList<Parameter*> >::const_iterator iter;
    for (iter = paramList->begin(); iter != paramList->end(); ++iter)
        count += (*iter).count();
    return count;
247
}