Projet Drone de surveillance du labo TRSE (INGESUP)

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

Module_Communication/Module_Communication.cpp

Committer:
IngesupMbed01
Date:
2013-04-17
Revision:
31:ec7d635636bf
Parent:
30:119e060b45b3
Child:
34:4466839f5bb7

File content as of revision 31:ec7d635636bf:

 /* Copyright (c) 2012 - 2013 AUTHEUR
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 /*
 * Description : Cette classe contient les fonctionnalités du module communication.
                 Le module communication gère la communication entre les modules présents sur le drône et le PC.
 * Input
 * Output
 */
 
 #include "Module_Communication.h"
 
 // Pointeur sur la classe systeme d'exploitation instancié dans le main
 extern mbos *os;
 extern unsigned int COMMANDE_TRAJECTOIRE;
 extern bool STOP;
 extern bool EN_MOUVEMENT;
 extern position COORDONNEE_DRONE;
 
 
 /* CONSRTRUCTEUR(S) */
 C_ModuleCommunication::C_ModuleCommunication()
 {
    m_xbee = new xbee(p9, p10, p11);
    m_trameRxBuffer = C_FrameBuffer();
    m_trameTxBuffer = C_FrameBuffer();
    m_charRxBuffer = new char[17];
    m_charTxBuffer = new char[17];
 }
 
 /* DESTRUCTEUR */
 C_ModuleCommunication::~C_ModuleCommunication()
 {
    delete [] m_charRxBuffer;
    delete [] m_charTxBuffer;
    delete m_xbee;
 }
 
 /* Point d'entrée de la tache Module Video */
 void C_ModuleCommunication::moduleCommunicationTask(void)
 {
    while(1){
    // Code
    }
 }
 
 void C_ModuleCommunication::envoiDeTrame(void)
 {
    frame newFrame = m_trameTxBuffer.frameBuffer();
    m_charTxBuffer[0] = 0x02;
    m_charTxBuffer[1] = newFrame.systemIdentifier;
    m_charTxBuffer[2] = newFrame.moduleIdentifier;
    m_charTxBuffer[3] = newFrame.messageIdentifier;
    m_charTxBuffer[16] = 0x03;
    
    for(int i = 0; i < 12; i++) {
        m_charTxBuffer[i+4] = newFrame.data[i];
    }
    
    m_xbee->SendData(m_charTxBuffer);
 }
 
 void C_ModuleCommunication::receptionDeTrame(void)
 {
    Serial pc(USBTX, USBRX);
    m_xbee->RecieveData(m_charRxBuffer, 0);
    pc.printf("You said:%s \n",m_charRxBuffer);
    
    
    frame newFrame;
    newFrame.systemIdentifier = m_charRxBuffer[1];
    newFrame.moduleIdentifier = m_charRxBuffer[2];
    newFrame.messageIdentifier = m_charRxBuffer[3];
    
    for(int i = 0; i < 12; i++) {
        newFrame.data[i] = m_charRxBuffer[i+4];
    }
    
    m_trameRxBuffer.frameBuffer(newFrame);
 }
 
 void C_ModuleCommunication::traitementTrame(void)
 {
    if(m_trameRxBuffer.numberMessage() <= 0) return;
    
    frame newFrame = m_trameRxBuffer.frameBuffer();
    
    switch(newFrame.moduleIdentifier) {
        case 0x31 : consigneModuleTrajectoire(newFrame);
                    break;
        case 0x32 : consigneModuleVideo(newFrame);
                    break;
                    
        default :  creerTrameSpeciale(0x99);
                   break;
                    
    }
 }

void C_ModuleCommunication::consigneModuleTrajectoire(frame newFrame)
{
    switch(newFrame.messageIdentifier) {
        case 0x11 : COMMANDE_TRAJECTOIRE = newFrame.data[0];
                    EN_MOUVEMENT = true;
                    while(EN_MOUVEMENT != false);
                    creerTrameModuleTrajectoire(0x12);
                    break;
        case 0x13 : STOP = true;
                    EN_MOUVEMENT = true;
                    while(EN_MOUVEMENT != false);
                    creerTrameModuleTrajectoire(0x14);
                    break;
        case 0x15 : creerTrameModuleTrajectoire(0x16);
                    break;
                                    
        default : creerTrameSpeciale(0x99);
                  break; 
    }
}

void C_ModuleCommunication::creerTrameModuleTrajectoire(unsigned char idMessage)
{
    frame newFrame;
    newFrame.moduleIdentifier = 0x31;
    
    switch(idMessage) {
        case 0x12 : newFrame.messageIdentifier = 0x12;
                    break;
        case 0x14 : newFrame.messageIdentifier = 0x14;
                    break;
        case 0x16 : newFrame.messageIdentifier = 0x16;
                    union conversionCharFloat conv1;
                    conv1.f = COORDONNEE_DRONE.x;
                    newFrame.data[0] = conv1.ch[0];
                    newFrame.data[1] = conv1.ch[1];
                    newFrame.data[2] = conv1.ch[2];
                    newFrame.data[3] = conv1.ch[3];
                    conv1.f = 0;
                    conv1.f = COORDONNEE_DRONE.y;
                    newFrame.data[4] = conv1.ch[0];
                    newFrame.data[5] = conv1.ch[1];
                    newFrame.data[6] = conv1.ch[2];
                    newFrame.data[7] = conv1.ch[3];
                    conv1.f = 0;
                    conv1.f = COORDONNEE_DRONE.z;
                    newFrame.data[8] = conv1.ch[0];
                    newFrame.data[9] = conv1.ch[1];
                    newFrame.data[10] = conv1.ch[2];
                    newFrame.data[11] = conv1.ch[3];
                    break;
    }
    
    m_trameTxBuffer.frameBuffer(newFrame);
}

void C_ModuleCommunication::creerTrameSpeciale(unsigned char idMessage) {
    frame newFrame;
    
    switch(idMessage) {
        case 0x00 : newFrame.moduleIdentifier = 0x00;
                    newFrame.messageIdentifier = 0x00;
                    break;
        case 0x99 : newFrame.moduleIdentifier = 0x99;
                    newFrame.messageIdentifier = 0x99;
                    break;
    }
    m_trameTxBuffer.frameBuffer(newFrame);
}

void C_ModuleCommunication::consigneModuleVideo(frame newFrame)
{
}