Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * \file ctrl_main_gr2.cc
- * \brief Initialization, loop and finilization of the controller written in C (but compiled as C++)
- */
- #include "ctrl_main_gr6.h"
- #include "namespace_ctrl.h"
- #include <stdio.h>
- #include "math.h"
- #include <stdlib.h>
- #include <string.h>
- #include "set_output.h"
- NAMESPACE_INIT(ctrlGr6);
- /*! \brief initialize controller operations (called once)
- *
- * \param[in] cvs controller main structure
- */
- void controller_init(CtrlStruct *cvs)
- {
- cvs->l_sum = 0.0; // Somme integrale roue gauche ==> Controleur PI roues
- cvs->r_sum = 0.0; // Somme integrale roue droite ==> Controleur PI roues
- cvs->t_sum = 0.0; // Somme integrale tourelle ==> Controleur PI tourelle
- cvs->pos[0] = 0.0; // Position X odometrie
- cvs->pos[1] = 0.0; // Position Y odometrie
- cvs->postri[0] = 0.0; // Position X triangulation
- cvs->postri[1] = 0.0; // Position Y triangulation
- cvs->theta = 0.0; // Angle entre robot et inertiel
- cvs->oldang = 0.0; // Valeur de l'angle de la tourelle à l'iteration precedente ==> Controleur PI tourelle
- cvs->i1 = 1; // Indice pilone initialisation
- cvs->j1 = 0; // Indice position initialisation
- cvs->i2 = 1; // Indice pilone
- cvs->j2 = 0; // Indice position
- cvs->x_beam[0] = 0.0; //
- cvs->x_beam[1] = 0.0; //
- cvs->x_beam[2] = 0.0; // Coordonnees relatives aux 3 pilones
- cvs->y_beam[0] = 0.0; //
- cvs->y_beam[1] = 0.0; //
- cvs->y_beam[2] = 0.0; //
- }
- /*! \brief controller loop (called every timestep)
- *
- * \param[in] cvs controller main structure
- */
- void controller_loop(CtrlStruct *cvs)
- {
- // INITIALISATION DES VARIABLES
- double time = cvs->inputs->t; // Temps de la simulation
- double Omtow; // Vitesse de rotation de la tourelle (rad/s) ==> Controleur PI tourelle
- double V; // Vitesse d'avance du robot
- double Ki = 25.95; // Constante de l'intégrateur du PI des roues ==> Controleur PI roues
- double Kp = 2.72; // Constante du proportionnel du PI des roues ==> Controleur PI roues
- double Om[2] = { 0 , 0 }; // Commande de vitesse de rotation des roues (rad/s)
- double tow = 20.0; // Commande de vitesse de rotation de la tourelle (rad/s) !!! PAS OK !!!
- double delta_t = 0.001; // Pas de temps de la boucle
- double r = 0.03; // Rayon des roues ==> Odometrie
- double l = 0.225; // Distance entre les deux roues ==> Odometrie
- double realang; // Angle detecte dans le repere inertiel
- double tta; // Angle mesure entre la tourelle et un pilone ==> Triangulation
- double d; // Distance mesuree entre la tourelle et un pilone ==> Triangulation
- double trans12[2] = { 1.9 , 0.0 }; // Distance fixe entre les pilones 1 et 2 ==> Triangulation
- double trans13[2] = { 1.0 , 3.0 }; // Distance fixe entre les pilones 1 et 3 ==> Triangulation
- // CALCUL POSITION INITIALE
- if (time<=-10.0 && time>=-15)
- {
- // VITESSE NULLE IMPOSEE AUX ROUES
- Om[0] = 0.0;
- Om[1] = 0.0;
- cvs->outputs->tower_command = tow;
- if (cvs->inputs->falling_index_fixed == cvs->inputs->rising_index_fixed && cvs->inputs->falling_index_fixed != cvs->i1)
- {
- // CALCUL DE LA DISTANCE ET DE L'ANGLE RELATIF AU PILONE SUIVANT
- tta = (cvs->inputs->last_rising_fixed[cvs->inputs->falling_index_fixed] + cvs->inputs->last_falling_fixed[cvs->inputs->falling_index_fixed]) / 2.0;
- d = 0.04 / (sin((cvs->inputs->last_falling_fixed[cvs->inputs->falling_index_fixed] - cvs->inputs->last_rising_fixed[cvs->inputs->falling_index_fixed]) / 2.0));
- // ON RAMENE L'ANGLE AU REPERE INERTIEL
- tta = tta + cvs->theta;
- if (tta > M_PI)
- {
- tta = tta - 2 * M_PI;
- }
- else if (tta <= -M_PI)
- {
- tta = tta + 2 * M_PI;
- }
- cvs->i1 = cvs->inputs->falling_index_fixed;
- cvs->x_beam[cvs->j1] = -d * sin(tta);
- cvs->y_beam[cvs->j1] = -d * cos(tta) - 0.083;
- cvs->j1 = cvs->j1 + 1;
- // MISE A JOUR DE LA POSITION SUR BASE DES 3 POSITIONS CALCULEES A L'AIDE DES 3 PILONES
- if (cvs->j1 >= 3)
- {
- cvs->j1 = 0.0;
- cvs->postri[0] = (cvs->x_beam[0] + cvs->x_beam[1] + trans12[0] + cvs->x_beam[2] + trans13[0]) / 3.0;
- cvs->postri[1] = (cvs->y_beam[0] + cvs->y_beam[1] + trans12[1] + cvs->y_beam[2] + trans13[1]) / 3.0;
- cvs->pos[0] = (cvs->x_beam[0] + cvs->x_beam[1] + trans12[0] + cvs->x_beam[2] + trans13[0]) / 3.0;
- cvs->pos[1] = (cvs->y_beam[0] + cvs->y_beam[1] + trans12[1] + cvs->y_beam[2] + trans13[1]) / 3.0;
- }
- }
- cvs->finalpos[0] = (cvs->pos[0] + cvs->postri[0]) / 2.0;
- cvs->finalpos[1] = (cvs->pos[1] + cvs->postri[1]) / 2.0;
- }
- else
- {
- Om[0] = 4.0;
- Om[1] = 4.0;
- // CONTROLEUR DE VITESSE PI
- cvs->r_sum = cvs->r_sum + (Om[0] - cvs->inputs->r_wheel_speed) * delta_t;
- cvs->l_sum = cvs->l_sum + (Om[1] - cvs->inputs->l_wheel_speed) * delta_t;
- cvs->outputs->wheel_commands[0] =( Kp * (Om[0] - cvs->inputs->r_wheel_speed) + Ki * cvs->r_sum) * 100 / (24 * 0.9);
- cvs->outputs->wheel_commands[1] =( Kp * (Om[1] - cvs->inputs->l_wheel_speed) + Ki * cvs->l_sum) * 100 / (24 * 0.9);
- // ODOMETRIE
- V = (cvs->inputs->r_wheel_speed + cvs->inputs->l_wheel_speed) * r / 2.0;
- cvs->theta = cvs->theta + ((cvs->inputs->r_wheel_speed * r - cvs->inputs->l_wheel_speed * r) / l) * delta_t;
- cvs->pos[0] = cvs->pos[0] + (V * sin(cvs->theta)) * delta_t;
- cvs->pos[1] = cvs->pos[1] + (V * cos(cvs->theta)) * delta_t;
- // TRIANGULATION
- cvs->outputs->tower_command = tow;
- if (cvs->inputs->falling_index_fixed == cvs->inputs->rising_index_fixed && cvs->inputs->falling_index_fixed != cvs->i2)
- {
- // CALCUL DE LA DISTANCE ET DE L'ANGLE RELATIF AU PILONE SUIVANT
- tta = (cvs->inputs->last_rising_fixed[cvs->inputs->falling_index_fixed] + cvs->inputs->last_falling_fixed[cvs->inputs->falling_index_fixed]) / 2.0;
- d = 0.04 / (sin((cvs->inputs->last_falling_fixed[cvs->inputs->falling_index_fixed] - cvs->inputs->last_rising_fixed[cvs->inputs->falling_index_fixed]) / 2.0));
- // ON RAMENE L'ANGLE AU REPERE INERTIEL
- tta = tta + cvs->theta;
- if (tta > M_PI)
- {
- tta = tta - 2 * M_PI;
- }
- else if(tta <= -M_PI)
- {
- tta = tta + 2 * M_PI;
- }
- cvs->i2 = cvs->inputs->falling_index_fixed;
- cvs->x_beam[cvs->j2] = -d * sin(tta) ;
- cvs->y_beam[cvs->j2] = -d * cos(tta) - 0.083 ;
- cvs->j2 = cvs->j2 + 1;
- // MISE A JOUR DE LA POSITION SUR BASE DES 3 POSITIONS CALCULEES A L'AIDE DES 3 PILONES
- if (cvs->j2 >= 3)
- {
- cvs->j2 = 0.0;
- cvs->postri[0] = (cvs->x_beam[0] + cvs->x_beam[1] + trans12[0] + cvs->x_beam[2] + trans13[0]) / 3.0;
- cvs->postri[1] = (cvs->y_beam[0] + cvs->y_beam[1] + trans12[1] + cvs->y_beam[2] + trans13[1]) / 3.0;
- }
- }
- cvs->finalpos[0] = (cvs->pos[0] + cvs->postri[0]) / 2.0;
- cvs->finalpos[1] = (cvs->pos[1] + cvs->postri[1]) / 2.0;
- }
- //printf("%f \n",cvs->inputs->target_detected);
- set_output(cvs->finalpos[0], "X");
- set_output(cvs->finalpos[1], "Y");
- // CONTROLEUR PI TOURELLE
- /*
- Omtow = (cvs->inputs->tower_pos - cvs->oldang) / delta_t;
- cvs->t_sum = cvs->t_sum + (tow - Omtow) * delta_t;
- cvs->outputs->tower_command = (1 * (tow - Omtow) + 1 * cvs->t_sum);
- cvs->oldang = cvs->inputs->tower_pos;
- */
- // ECRITURE FICHIER
- /*
- if (time >= -15.0 && time <= 10.0)
- {
- char files[30];
- char buffer[10];
- strcpy(files, "x");
- FILE* fichier1 = NULL;
- fichier1 = fopen(files, "a");
- // On peut lire et écrire dans le fichier
- //fwrite(5.0, sizeof(double), 1, fichier);
- fprintf(fichier1, "%f", cvs->pos[0]);
- fclose(fichier1);
- //char files[30];
- //char buffer[10];
- strcpy(files, "y");
- FILE* fichier2 = NULL;
- fichier2 = fopen(files, "a");
- // On peut lire et écrire dans le fichier
- //fwrite(5.0, sizeof(double), 1, fichier);
- fprintf(fichier2, "%f", cvs->pos[1]);
- fclose(fichier2);
- }*/
- }
- /*! \brief last controller operations (called once)
- *
- * \param[in] cvs controller main structure
- */
- void controller_finish(CtrlStruct *cvs)
- {
- }
- void rot(CtrlStruct *cvs, double Om[2], double side)
- {
- if (side == 0)
- {
- Om[0] = 2.0;
- Om[1] = 6.0;
- }
- if (side == 1)
- {
- Om[0] = 6.0;
- Om[1] = 2.0;
- }
- else
- {
- Om[0] = 0.0;
- Om[0] = 0.0;
- }
- }
- void fwd(CtrlStruct *cvs, double Om[2], double spd)
- {
- Om[0] = spd;
- Om[1] = spd;
- }
- NAMESPACE_CLOSE();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement