Exemplo n.º 1
0
QString QDjangoCompiler::databaseColumn(const QString &name)
{
    QDjangoMetaModel model = baseModel;
    QString modelPath;
    QString modelRef = referenceModel(QString(), &model);
    QStringList bits = name.split(QLatin1String("__"));

    while (bits.size() > 1) {
        const QByteArray fk = bits.first().toLatin1();
        QDjangoMetaModel foreignModel;
        if (!model.foreignFields().contains(fk)) {
            // this might be a reverse relation, so look for the model
            // and if it exists continue
            foreignModel = QDjango::metaModel(fk);
            if (!foreignModel.isValid())
                break;

            reverseModelRefs[bits.first()] = foreignModel.primaryKey();
        } else {
            foreignModel = QDjango::metaModel(model.foreignFields()[fk]);
        }

        // store reference
        if (!modelPath.isEmpty())
            modelPath += QLatin1String("__");
        modelPath += bits.first();
        modelRef = referenceModel(modelPath, &foreignModel);

        model = foreignModel;
        bits.takeFirst();
    }

    const QDjangoMetaField field = model.localField(bits.join(QLatin1String("__")).toLatin1());
    return modelRef + QLatin1Char('.') + driver->escapeIdentifier(field.column(), QSqlDriver::FieldName);
}
Exemplo n.º 2
0
void pid_update(neural_state_t *n_s, float rin, int encoder_counter, int command_distance_count){

	n_s->rf_out = referenceModel(rin, n_s->rf_out_1);
   	n_s->e = n_s->rf_out - (float)encoder_counter;
    n_s->rf_out_2 = n_s->rf_out_1;
	n_s->rf_out_1 = n_s->rf_out;
    n_s->desire = n_s->desire +(int)n_s->rf_out;

    // Incremental PID
    n_s->xc[0] = n_s->e - n_s->e_1;
    n_s->xc[1] = n_s->e;
    n_s->xc[2] = n_s->e - (2 * n_s->e_1) + n_s->e_2; 

    // Calculate the output of PID controller
    n_s->du = n_s->kp * n_s->xc[0] + n_s->ki * n_s->xc[1] + n_s->kd * n_s->xc[2];
    n_s->u = n_s->u_1 + n_s->du;
    if (n_s->u < 0)
    {
    	n_s->u = 0;
    }else if(n_s->u > 110) {
    	n_s->u = 110;
    }

    // update yout(k-1) u(k-1)
    n_s->yout_1 = n_s->x[1];
    n_s->u_2 = n_s->u_1;
    n_s->u_1 = n_s->u;

    // update e(k-1) e(k-2)
    n_s->e_2 = n_s->e_1;
    n_s->e_1 = n_s->e;

}
Exemplo n.º 3
0
QStringList QDjangoCompiler::fieldNames(bool recurse, QDjangoMetaModel *metaModel, const QString &modelPath)
{
    QStringList columns;
    if (!metaModel)
        metaModel = &baseModel;

    // store reference
    const QString tableName = referenceModel(modelPath, metaModel);
    foreach (const QDjangoMetaField &field, metaModel->localFields())
        columns << tableName + QLatin1Char('.') + driver->escapeIdentifier(field.column(), QSqlDriver::FieldName);
    if (!recurse)
        return columns;

    // recurse for foreign keys
    const QString pathPrefix = modelPath.isEmpty() ? QString() : (modelPath + QLatin1String("__"));
    foreach (const QByteArray &fkName, metaModel->foreignFields().keys()) {
        QDjangoMetaModel metaForeign = QDjango::metaModel(metaModel->foreignFields()[fkName]);
        columns += fieldNames(recurse, &metaForeign, pathPrefix + QString::fromLatin1(fkName));
    }
    return columns;
}
Exemplo n.º 4
0
QString QDjangoCompiler::databaseColumn(const QString &name)
{
    QDjangoMetaModel model = baseModel;
    QString modelName;
    QString modelPath;
    QString modelRef = referenceModel(QString(), &model, false);
    QStringList bits = name.split(QLatin1String("__"));

    while (bits.size() > 1) {
        const QByteArray fk = bits.first().toLatin1();
        QDjangoMetaModel foreignModel;
        bool foreignNullable = false;

        if (!modelPath.isEmpty())
            modelPath += QLatin1String("__");
        modelPath += bits.first();

        if (!model.foreignFields().contains(fk)) {
            // this might be a reverse relation, so look for the model
            // and if it exists continue
            foreignModel = QDjango::metaModel(fk);
            QDjangoReverseReference rev;
            const QMap<QByteArray, QByteArray> foreignFields = foreignModel.foreignFields();
            foreach (const QByteArray &foreignKey, foreignFields.keys()) {
                if (foreignFields[foreignKey] == baseModel.className()) {
                    rev.leftHandKey = foreignModel.localField(foreignKey + "_id").column();
                    break;
                }
            }

            if (rev.leftHandKey.isEmpty()) {
                qWarning() << "Invalid field lookup" << name;
                return QString();
            }
            rev.rightHandKey = foreignModel.primaryKey();
            reverseModelRefs[modelPath] = rev;
        } else {
Exemplo n.º 5
0
void neural_update(neural_state_t *n_s, float rin, int encoder_counter, int command_distance_count){
if (car_state != CAR_STATE_IDLE)
{
	    int i = 0, j = 0;

        // 1. RBFNN Output
        n_s->ynout = 0;
        for ( i = 0; i < neuralNumber; ++i)
        {
        	n_s->norm_c_2[i] = pow2((n_s->x[0] - n_s->c[0][i]), 2) + pow2((n_s->x[1] - n_s->c[1][i]), 2) + pow2((n_s->x[2] - n_s->c[2][i]), 2);
            	            
            float tmp = (((-0.5) * n_s->norm_c_2[i]));
            tmp = tmp/ pow2(n_s->b[i], 2);
            n_s->h[i] = exponential(tmp);
            n_s->ynout = n_s->ynout + (n_s->h[i])*(n_s->w[i]);
        }
        n_s->ynout_sum += n_s->ynout;

        
        // 2 Get the motor speed of last clock cycle, and calculate the error of rbf
        n_s->erbf = encoder_counter - n_s->ynout;
        n_s->erbf_record[4] = n_s->erbf_record[3];
		n_s->erbf_record[3] = n_s->erbf_record[2];
		n_s->erbf_record[2] = n_s->erbf_record[1];
		n_s->erbf_record[1] = n_s->erbf_record[0];
		n_s->erbf_record[0] = abs2(n_s->erbf);
		n_s->erbf_avg = (n_s->erbf_record[0] + n_s->erbf_record[1] + n_s->erbf_record[2] + n_s->erbf_record[3] + n_s->erbf_record[4])/5.0;

        // 3. Update w of RBFNN
        for ( i = 0; i < neuralNumber; ++i)
        {
        	float tmp = n_s->erbf * n_s->h[i];
            n_s->dw[i] = n_s->eta * tmp;
            n_s->w_1[i] = n_s->w[i];
            n_s->w[i] = n_s->w_1[i] + n_s->dw[i];
        }

        // 4. Update bj
        for ( i = 0; i < neuralNumber; ++i)
        {
        	float tmp = n_s->eta * n_s->erbf;
        	tmp = tmp * n_s->w[i];
        	tmp = tmp * n_s->h[i];
        	tmp = tmp * n_s->norm_c_2[i];
        	tmp = tmp / pow2(n_s->b[i], 3);
            n_s->db[i] = tmp;
            n_s->b_1[i] = n_s->b[i];
            n_s->b[i] = n_s->b_1[i] + n_s->db[i];
        }

        // 5. Update Cj
        for ( i = 0; i < neuralNumber; ++i)
        {
            for ( j = 0; j < centerNumber; ++j)
            {
            	float tmp = n_s->eta * n_s->erbf;
            	tmp = tmp * n_s->w[i];
            	tmp = tmp * n_s->h[i];
            	tmp = tmp * (n_s->x[j] - n_s->c[j][i]);
            	tmp = tmp /  pow2(n_s->b[i], 2);
            	n_s->dc[j][i] = tmp;
                n_s->c_1[j][i] = n_s->c[j][i];
                n_s->c[j][i] = n_s->c_1[j][i] + n_s->dc[j][i];	                
            }
        }

        // 6. Calculate Jacobian
        n_s->yu = 0;
        for ( i = 0; i < neuralNumber; ++i)
        {
        	float tmp = n_s->w[i] * n_s->h[i];
        	float tmp2 = (-1) * n_s->x[0];
        	tmp = tmp *  (tmp2 + n_s->c[0][i]);
        	tmp = tmp / pow2(n_s->b[i], 2);
            n_s->yu = n_s->yu + tmp;
        }
        n_s->dyu = n_s->yu;

        // 6.2 Calculate the error with reference model
    	n_s->rf_out = referenceModel(rin, n_s->rf_out_1);
       	n_s->e = n_s->rf_out - (float)encoder_counter;
       	n_s->total_err += abs2(n_s->e);
	    n_s->rf_out_2 = n_s->rf_out_1;
   		n_s->rf_out_1 = n_s->rf_out;
        n_s->desire = n_s->desire +(int)n_s->rf_out;
        
        // 8. Incremental PID
        n_s->xc[0] = n_s->e - n_s->e_1;
        n_s->xc[1] = n_s->e;
        n_s->xc[2] = n_s->e - (2 * n_s->e_1) + n_s->e_2; 

        //int tmp_erbf = (int)(abs2(erbf)*100);
        int tmp_erbf = (command_distance_count - n_s -> ynout_sum)/command_distance_count;
        tmp_erbf = tmp_erbf * 100; 
        if (tmp_erbf < 10)
        {
        	n_s -> erbf_correct_times ++;
        }
        if ((tmp_erbf < 10) && (encoder_counter > 1) && (n_s->erbf_correct_times >30 && n_s->erbf_avg < 1)){
        	n_s->erbf_correct_times ++;
	        float kp_add = n_s->eta * n_s->e;
	        kp_add = kp_add * n_s->dyu;
	        kp_add = kp_add * n_s->xc[0];
	        
	        float ki_add = n_s->eta * n_s->e;
	        ki_add = ki_add * n_s->dyu;
	        ki_add = ki_add * n_s->xc[1];
	        
	        float kd_add = n_s->eta * n_s->e;
	        kd_add = kd_add * n_s->dyu;
	        kd_add = kd_add * n_s->xc[2];
	        
	        // 10. update kp(k-1) ki(k-1) kd(k-1)
	        n_s->kp_1 = n_s->kp;
	        n_s->ki_1 = n_s->ki;
	        n_s->kd_1 = n_s->kd;

	        // 9. Update the parameter of PID controller    
	        if (n_s->stop_tune == 0 && car_state == CAR_STATE_MOVE_FORWARD){

	        	n_s->kp = n_s->kp_1 + kp_add;
		        n_s->ki = n_s->ki_1 + ki_add;
//		        n_s->kd = n_s->kd_1 + kd_add;
		        if (n_s->kp <= 0)
		        {
		        	n_s->kp = 0;
		        }
		        if (n_s->ki <= 0)
		        {
		        	n_s->ki = 0;
		        }
		        if (n_s->kd <= 0)
		        {
		        	//n_s->kd = 0;
		        }
	        }
	        
	    }
        // 11. Calculate the output of PID controller
        n_s->du = n_s->kp * n_s->xc[0] + n_s->ki * n_s->xc[1] + n_s->kd * n_s->xc[2];
        n_s->u = n_s->u_1 + n_s->du;
        if (n_s->u < 0)
        {
        	n_s->u = 0;
        }else if(n_s->u > 110) {
        	n_s->u = 110;
        }
        // 12. update yout(k-1) u(k-1)
        n_s->yout_1 = n_s->x[1];
        n_s->u_2 = n_s->u_1;
        n_s->u_1 = n_s->u;

        // 13. update e(k-1) e(k-2)
        n_s->e_2 = n_s->e_1;
        n_s->e_1 = n_s->e;

        // 14. update input of RBFNN
        n_s->x[0] = n_s->du;
        n_s->x[1] = (float)encoder_counter;
        n_s->x[2] = n_s->yout_1;

    }
}