HERBERT Nicolas / Mbed 2 deprecated Labo_TRSE_Drone

Dependencies:   mbed PID ADXL345 Camera_LS_Y201 ITG3200 RangeFinder mbos xbee_lib Motor Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Module_Communication.cpp Source File

Module_Communication.cpp

00001  /* Copyright (c) 2012 - 2013 AUTHEUR
00002  *
00003  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
00004  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00005  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00006  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00007  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00008  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00009  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00010  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00011  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00012  */
00013  
00014  /*
00015  * Description : Cette classe contient les fonctionnalités du module communication.
00016                  Le module communication gère la communication entre les modules présents sur le drône et le PC.
00017  * Input
00018  * Output
00019  */
00020  
00021  #include "Module_Communication.h"
00022  #include "os.h"
00023  #define TAILLE_BUFFER 6
00024  
00025  // Pointeur sur la classe systeme d'exploitation instancié dans le main
00026  extern mbos os;
00027  extern unsigned int COMMANDE_TRAJECTOIRE;
00028  extern bool STOP;
00029  extern bool EN_MOUVEMENT;
00030  extern position COORDONNEE_DRONE;
00031  extern Serial pc;
00032  
00033  char buffer[TAILLE_BUFFER];
00034  char reception[TAILLE_BUFFER];
00035  //extern char *envoi;
00036  char envoi[15];
00037  int pos;
00038  
00039  
00040 xbee *com_xbee = new xbee(PINTX, PINRX, PINRES);
00041  
00042  /* CONSRTRUCTEUR(S) */
00043  C_ModuleCommunication::C_ModuleCommunication()
00044  {
00045     m_xbee = new xbee(p9, p10, p11);
00046     m_trameRxBuffer = C_FrameBuffer();
00047     m_trameTxBuffer = C_FrameBuffer();
00048     m_charRxBuffer = new char[17];
00049     m_charTxBuffer = new char[17];
00050  }
00051  
00052  /* DESTRUCTEUR */
00053  C_ModuleCommunication::~C_ModuleCommunication()
00054  {
00055     delete [] m_charRxBuffer;
00056     delete [] m_charTxBuffer;
00057     delete m_xbee;
00058  }
00059  void flushRx(void)
00060  {
00061     for(int i = 0 ; i < TAILLE_BUFFER ; i++){
00062         buffer[i] = '\0';
00063     }
00064  }
00065  void addToReception(void)
00066  {
00067     for(int i = pos ; i < TAILLE_BUFFER ; i++){
00068         reception[i] = buffer[i-pos];
00069     }
00070     pos += strlen(buffer);
00071  }
00072  
00073  
00074  /* Point d'entrée de la tache Module Video */
00075  
00076  void moduleCommunicationEnvoi(void)
00077  {
00078     while(1){
00079         os.WaitEvent(COMMUNICATION_EVENT);
00080         
00081         if(com_xbee->SendData(envoi)){
00082             pc.printf("send %s\n", envoi);
00083         }
00084         else{
00085             pc.printf("envoi non envoye\n");
00086         }
00087     }
00088  }
00089  
00090  void moduleCommunicationReception(void)
00091  {
00092      pos = 0;
00093      os.SetTimer(TIMER_COM_ID, TIMER_COM_PERIOD, TIMER_COM_PERIOD);
00094      while(1){
00095         os.WaitEvent(TIMER_EVENT);
00096         if(os.GetEvent() & TIMER_EVENT){
00097             com_xbee->RecieveData(buffer, 1);
00098             pc.printf("recu : %s\n", buffer);
00099             /*if(strlen(buffer) + pos < TAILLE_BUFFER){ //trame non complète
00100                 addToReception();
00101                 pc.printf("buffer : %s\n", buffer);
00102                 flushRx();
00103             }
00104             else{
00105                 addToReception();
00106                 pos = 0;
00107                 pc.printf("recu : %s\n", reception);
00108                 //traite reception
00109             }*/
00110             
00111             //flushRx();
00112         }
00113     }
00114  }
00115  
00116  char *getEnvoi(){
00117     return envoi;
00118  }
00119  
00120  void C_ModuleCommunication::moduleCommunicationTask(void)
00121  {
00122 
00123  
00124     while(1){
00125     // Code
00126     }
00127  }
00128  
00129  void C_ModuleCommunication::envoiDeTrame(void)
00130  {
00131     frame newFrame = m_trameTxBuffer.frameBuffer();
00132     m_charTxBuffer[0] = 0x02;
00133     m_charTxBuffer[1] = newFrame.systemIdentifier;
00134     m_charTxBuffer[2] = newFrame.moduleIdentifier;
00135     m_charTxBuffer[3] = newFrame.messageIdentifier;
00136     m_charTxBuffer[16] = 0x03;
00137     
00138     for(int i = 0; i < 12; i++) {
00139         m_charTxBuffer[i+4] = newFrame.data[i];
00140     }
00141     
00142     m_xbee->SendData(m_charTxBuffer);
00143  }
00144  
00145  void C_ModuleCommunication::receptionDeTrame(void)
00146  {
00147     Serial pc(USBTX, USBRX);
00148     m_xbee->RecieveData(m_charRxBuffer, 0);
00149     pc.printf("You said:%s \n",m_charRxBuffer);
00150     
00151     
00152     frame newFrame;
00153     newFrame.systemIdentifier = m_charRxBuffer[1];
00154     newFrame.moduleIdentifier = m_charRxBuffer[2];
00155     newFrame.messageIdentifier = m_charRxBuffer[3];
00156     
00157     for(int i = 0; i < 12; i++) {
00158         newFrame.data[i] = m_charRxBuffer[i+4];
00159     }
00160     
00161     m_trameRxBuffer.frameBuffer(newFrame);
00162  }
00163  
00164  void C_ModuleCommunication::traitementTrame(void)
00165  {
00166     if(m_trameRxBuffer.numberMessage() <= 0) return;
00167     
00168     frame newFrame = m_trameRxBuffer.frameBuffer();
00169     
00170     switch(newFrame.moduleIdentifier) {
00171         case 0x31 : consigneModuleTrajectoire(newFrame);
00172                     break;
00173         case 0x32 : consigneModuleVideo(newFrame);
00174                     break;
00175                     
00176         default :  creerTrameSpeciale(0x99);
00177                    break;
00178                     
00179     }
00180  }
00181 
00182 void C_ModuleCommunication::consigneModuleTrajectoire(frame newFrame)
00183 {
00184     switch(newFrame.messageIdentifier) {
00185         case 0x11 : COMMANDE_TRAJECTOIRE = newFrame.data[0];
00186                     EN_MOUVEMENT = true;
00187                     while(EN_MOUVEMENT != false);
00188                     creerTrameModuleTrajectoire(0x12);
00189                     break;
00190         case 0x13 : STOP = true;
00191                     EN_MOUVEMENT = true;
00192                     while(EN_MOUVEMENT != false);
00193                     creerTrameModuleTrajectoire(0x14);
00194                     break;
00195         case 0x15 : creerTrameModuleTrajectoire(0x16);
00196                     break;
00197                                     
00198         default : creerTrameSpeciale(0x99);
00199                   break; 
00200     }
00201 }
00202 
00203 void C_ModuleCommunication::creerTrameModuleTrajectoire(unsigned char idMessage)
00204 {
00205     frame newFrame;
00206     newFrame.moduleIdentifier = 0x31;
00207     
00208     switch(idMessage) {
00209         case 0x12 : newFrame.messageIdentifier = 0x12;
00210                     break;
00211         case 0x14 : newFrame.messageIdentifier = 0x14;
00212                     break;
00213         case 0x16 : newFrame.messageIdentifier = 0x16;
00214                     union conversionCharFloat conv1;
00215                     conv1.f = COORDONNEE_DRONE.x;
00216                     newFrame.data[0] = conv1.ch[0];
00217                     newFrame.data[1] = conv1.ch[1];
00218                     newFrame.data[2] = conv1.ch[2];
00219                     newFrame.data[3] = conv1.ch[3];
00220                     conv1.f = 0;
00221                     conv1.f = COORDONNEE_DRONE.y;
00222                     newFrame.data[4] = conv1.ch[0];
00223                     newFrame.data[5] = conv1.ch[1];
00224                     newFrame.data[6] = conv1.ch[2];
00225                     newFrame.data[7] = conv1.ch[3];
00226                     conv1.f = 0;
00227                     conv1.f = COORDONNEE_DRONE.z;
00228                     newFrame.data[8] = conv1.ch[0];
00229                     newFrame.data[9] = conv1.ch[1];
00230                     newFrame.data[10] = conv1.ch[2];
00231                     newFrame.data[11] = conv1.ch[3];
00232                     break;
00233     }
00234     
00235     m_trameTxBuffer.frameBuffer(newFrame);
00236 }
00237 
00238 void C_ModuleCommunication::creerTrameSpeciale(unsigned char idMessage) {
00239     frame newFrame;
00240     
00241     switch(idMessage) {
00242         case 0x00 : newFrame.moduleIdentifier = 0x00;
00243                     newFrame.messageIdentifier = 0x00;
00244                     break;
00245         case 0x99 : newFrame.moduleIdentifier = 0x99;
00246                     newFrame.messageIdentifier = 0x99;
00247                     break;
00248     }
00249     m_trameTxBuffer.frameBuffer(newFrame);
00250 }
00251 
00252 void C_ModuleCommunication::consigneModuleVideo(frame newFrame)
00253 {
00254 }