Projet Drone de surveillance du labo TRSE (INGESUP)

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

Committer:
Gaetan
Date:
Wed Mar 19 09:18:53 2014 +0000
Revision:
34:4466839f5bb7
Parent:
31:ec7d635636bf
19/03/2014 commit before update;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IngesupMbed01 18:0bfd3ad421e5 1 /* Copyright (c) 2012 - 2013 AUTHEUR
IngesupMbed01 18:0bfd3ad421e5 2 *
IngesupMbed01 18:0bfd3ad421e5 3 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
IngesupMbed01 18:0bfd3ad421e5 4 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
IngesupMbed01 18:0bfd3ad421e5 5 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
IngesupMbed01 18:0bfd3ad421e5 6 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
IngesupMbed01 18:0bfd3ad421e5 7 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
IngesupMbed01 18:0bfd3ad421e5 8 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
IngesupMbed01 18:0bfd3ad421e5 9 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
IngesupMbed01 18:0bfd3ad421e5 10 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
IngesupMbed01 18:0bfd3ad421e5 11 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IngesupMbed01 18:0bfd3ad421e5 12 */
IngesupMbed01 18:0bfd3ad421e5 13
IngesupMbed01 18:0bfd3ad421e5 14 /*
IngesupMbed01 18:0bfd3ad421e5 15 * Description : Cette classe contient les fonctionnalités du module communication.
IngesupMbed01 18:0bfd3ad421e5 16 Le module communication gère la communication entre les modules présents sur le drône et le PC.
IngesupMbed01 18:0bfd3ad421e5 17 * Input
IngesupMbed01 18:0bfd3ad421e5 18 * Output
IngesupMbed01 18:0bfd3ad421e5 19 */
IngesupMbed01 18:0bfd3ad421e5 20
IngesupMbed01 18:0bfd3ad421e5 21 #include "Module_Communication.h"
Gaetan 34:4466839f5bb7 22 #include "os.h"
Gaetan 34:4466839f5bb7 23 #define TAILLE_BUFFER 6
IngesupMbed01 18:0bfd3ad421e5 24
IngesupMbed01 18:0bfd3ad421e5 25 // Pointeur sur la classe systeme d'exploitation instancié dans le main
Gaetan 34:4466839f5bb7 26 extern mbos os;
IngesupMbed01 31:ec7d635636bf 27 extern unsigned int COMMANDE_TRAJECTOIRE;
IngesupMbed01 31:ec7d635636bf 28 extern bool STOP;
IngesupMbed01 31:ec7d635636bf 29 extern bool EN_MOUVEMENT;
IngesupMbed01 31:ec7d635636bf 30 extern position COORDONNEE_DRONE;
Gaetan 34:4466839f5bb7 31 extern Serial pc;
IngesupMbed01 18:0bfd3ad421e5 32
Gaetan 34:4466839f5bb7 33 char buffer[TAILLE_BUFFER];
Gaetan 34:4466839f5bb7 34 char reception[TAILLE_BUFFER];
Gaetan 34:4466839f5bb7 35 //extern char *envoi;
Gaetan 34:4466839f5bb7 36 char envoi[15];
Gaetan 34:4466839f5bb7 37 int pos;
Gaetan 34:4466839f5bb7 38
Gaetan 34:4466839f5bb7 39
Gaetan 34:4466839f5bb7 40 xbee *com_xbee = new xbee(PINTX, PINRX, PINRES);
IngesupMbed01 18:0bfd3ad421e5 41
IngesupMbed01 18:0bfd3ad421e5 42 /* CONSRTRUCTEUR(S) */
IngesupMbed01 18:0bfd3ad421e5 43 C_ModuleCommunication::C_ModuleCommunication()
IngesupMbed01 18:0bfd3ad421e5 44 {
IngesupMbed01 18:0bfd3ad421e5 45 m_xbee = new xbee(p9, p10, p11);
IngesupMbed01 24:3462a304f9a0 46 m_trameRxBuffer = C_FrameBuffer();
IngesupMbed01 24:3462a304f9a0 47 m_trameTxBuffer = C_FrameBuffer();
IngesupMbed01 30:119e060b45b3 48 m_charRxBuffer = new char[17];
IngesupMbed01 30:119e060b45b3 49 m_charTxBuffer = new char[17];
IngesupMbed01 18:0bfd3ad421e5 50 }
IngesupMbed01 18:0bfd3ad421e5 51
IngesupMbed01 18:0bfd3ad421e5 52 /* DESTRUCTEUR */
IngesupMbed01 18:0bfd3ad421e5 53 C_ModuleCommunication::~C_ModuleCommunication()
IngesupMbed01 18:0bfd3ad421e5 54 {
IngesupMbed01 24:3462a304f9a0 55 delete [] m_charRxBuffer;
IngesupMbed01 24:3462a304f9a0 56 delete [] m_charTxBuffer;
IngesupMbed01 24:3462a304f9a0 57 delete m_xbee;
IngesupMbed01 18:0bfd3ad421e5 58 }
Gaetan 34:4466839f5bb7 59 void flushRx(void)
Gaetan 34:4466839f5bb7 60 {
Gaetan 34:4466839f5bb7 61 for(int i = 0 ; i < TAILLE_BUFFER ; i++){
Gaetan 34:4466839f5bb7 62 buffer[i] = '\0';
Gaetan 34:4466839f5bb7 63 }
Gaetan 34:4466839f5bb7 64 }
Gaetan 34:4466839f5bb7 65 void addToReception(void)
Gaetan 34:4466839f5bb7 66 {
Gaetan 34:4466839f5bb7 67 for(int i = pos ; i < TAILLE_BUFFER ; i++){
Gaetan 34:4466839f5bb7 68 reception[i] = buffer[i-pos];
Gaetan 34:4466839f5bb7 69 }
Gaetan 34:4466839f5bb7 70 pos += strlen(buffer);
Gaetan 34:4466839f5bb7 71 }
Gaetan 34:4466839f5bb7 72
IngesupMbed01 18:0bfd3ad421e5 73
IngesupMbed01 18:0bfd3ad421e5 74 /* Point d'entrée de la tache Module Video */
Gaetan 34:4466839f5bb7 75
Gaetan 34:4466839f5bb7 76 void moduleCommunicationEnvoi(void)
Gaetan 34:4466839f5bb7 77 {
Gaetan 34:4466839f5bb7 78 while(1){
Gaetan 34:4466839f5bb7 79 os.WaitEvent(COMMUNICATION_EVENT);
Gaetan 34:4466839f5bb7 80
Gaetan 34:4466839f5bb7 81 if(com_xbee->SendData(envoi)){
Gaetan 34:4466839f5bb7 82 pc.printf("send %s\n", envoi);
Gaetan 34:4466839f5bb7 83 }
Gaetan 34:4466839f5bb7 84 else{
Gaetan 34:4466839f5bb7 85 pc.printf("envoi non envoye\n");
Gaetan 34:4466839f5bb7 86 }
Gaetan 34:4466839f5bb7 87 }
Gaetan 34:4466839f5bb7 88 }
Gaetan 34:4466839f5bb7 89
Gaetan 34:4466839f5bb7 90 void moduleCommunicationReception(void)
Gaetan 34:4466839f5bb7 91 {
Gaetan 34:4466839f5bb7 92 pos = 0;
Gaetan 34:4466839f5bb7 93 os.SetTimer(TIMER_COM_ID, TIMER_COM_PERIOD, TIMER_COM_PERIOD);
Gaetan 34:4466839f5bb7 94 while(1){
Gaetan 34:4466839f5bb7 95 os.WaitEvent(TIMER_EVENT);
Gaetan 34:4466839f5bb7 96 if(os.GetEvent() & TIMER_EVENT){
Gaetan 34:4466839f5bb7 97 com_xbee->RecieveData(buffer, 1);
Gaetan 34:4466839f5bb7 98 pc.printf("recu : %s\n", buffer);
Gaetan 34:4466839f5bb7 99 /*if(strlen(buffer) + pos < TAILLE_BUFFER){ //trame non complète
Gaetan 34:4466839f5bb7 100 addToReception();
Gaetan 34:4466839f5bb7 101 pc.printf("buffer : %s\n", buffer);
Gaetan 34:4466839f5bb7 102 flushRx();
Gaetan 34:4466839f5bb7 103 }
Gaetan 34:4466839f5bb7 104 else{
Gaetan 34:4466839f5bb7 105 addToReception();
Gaetan 34:4466839f5bb7 106 pos = 0;
Gaetan 34:4466839f5bb7 107 pc.printf("recu : %s\n", reception);
Gaetan 34:4466839f5bb7 108 //traite reception
Gaetan 34:4466839f5bb7 109 }*/
Gaetan 34:4466839f5bb7 110
Gaetan 34:4466839f5bb7 111 //flushRx();
Gaetan 34:4466839f5bb7 112 }
Gaetan 34:4466839f5bb7 113 }
Gaetan 34:4466839f5bb7 114 }
Gaetan 34:4466839f5bb7 115
Gaetan 34:4466839f5bb7 116 char *getEnvoi(){
Gaetan 34:4466839f5bb7 117 return envoi;
Gaetan 34:4466839f5bb7 118 }
Gaetan 34:4466839f5bb7 119
IngesupMbed01 24:3462a304f9a0 120 void C_ModuleCommunication::moduleCommunicationTask(void)
IngesupMbed01 18:0bfd3ad421e5 121 {
Gaetan 34:4466839f5bb7 122
Gaetan 34:4466839f5bb7 123
IngesupMbed01 18:0bfd3ad421e5 124 while(1){
IngesupMbed01 18:0bfd3ad421e5 125 // Code
IngesupMbed01 18:0bfd3ad421e5 126 }
IngesupMbed01 18:0bfd3ad421e5 127 }
IngesupMbed01 18:0bfd3ad421e5 128
IngesupMbed01 24:3462a304f9a0 129 void C_ModuleCommunication::envoiDeTrame(void)
IngesupMbed01 24:3462a304f9a0 130 {
IngesupMbed01 30:119e060b45b3 131 frame newFrame = m_trameTxBuffer.frameBuffer();
IngesupMbed01 30:119e060b45b3 132 m_charTxBuffer[0] = 0x02;
IngesupMbed01 30:119e060b45b3 133 m_charTxBuffer[1] = newFrame.systemIdentifier;
IngesupMbed01 30:119e060b45b3 134 m_charTxBuffer[2] = newFrame.moduleIdentifier;
IngesupMbed01 30:119e060b45b3 135 m_charTxBuffer[3] = newFrame.messageIdentifier;
IngesupMbed01 30:119e060b45b3 136 m_charTxBuffer[16] = 0x03;
IngesupMbed01 24:3462a304f9a0 137
IngesupMbed01 30:119e060b45b3 138 for(int i = 0; i < 12; i++) {
IngesupMbed01 30:119e060b45b3 139 m_charTxBuffer[i+4] = newFrame.data[i];
IngesupMbed01 24:3462a304f9a0 140 }
IngesupMbed01 24:3462a304f9a0 141
IngesupMbed01 24:3462a304f9a0 142 m_xbee->SendData(m_charTxBuffer);
IngesupMbed01 24:3462a304f9a0 143 }
IngesupMbed01 24:3462a304f9a0 144
IngesupMbed01 24:3462a304f9a0 145 void C_ModuleCommunication::receptionDeTrame(void)
IngesupMbed01 24:3462a304f9a0 146 {
IngesupMbed01 29:a0800d3da787 147 Serial pc(USBTX, USBRX);
IngesupMbed01 24:3462a304f9a0 148 m_xbee->RecieveData(m_charRxBuffer, 0);
IngesupMbed01 29:a0800d3da787 149 pc.printf("You said:%s \n",m_charRxBuffer);
IngesupMbed01 30:119e060b45b3 150
IngesupMbed01 30:119e060b45b3 151
IngesupMbed01 24:3462a304f9a0 152 frame newFrame;
IngesupMbed01 30:119e060b45b3 153 newFrame.systemIdentifier = m_charRxBuffer[1];
IngesupMbed01 30:119e060b45b3 154 newFrame.moduleIdentifier = m_charRxBuffer[2];
IngesupMbed01 30:119e060b45b3 155 newFrame.messageIdentifier = m_charRxBuffer[3];
IngesupMbed01 24:3462a304f9a0 156
IngesupMbed01 30:119e060b45b3 157 for(int i = 0; i < 12; i++) {
IngesupMbed01 30:119e060b45b3 158 newFrame.data[i] = m_charRxBuffer[i+4];
IngesupMbed01 24:3462a304f9a0 159 }
IngesupMbed01 24:3462a304f9a0 160
IngesupMbed01 30:119e060b45b3 161 m_trameRxBuffer.frameBuffer(newFrame);
IngesupMbed01 28:8b5ccd2f837e 162 }
IngesupMbed01 28:8b5ccd2f837e 163
IngesupMbed01 28:8b5ccd2f837e 164 void C_ModuleCommunication::traitementTrame(void)
IngesupMbed01 28:8b5ccd2f837e 165 {
IngesupMbed01 28:8b5ccd2f837e 166 if(m_trameRxBuffer.numberMessage() <= 0) return;
IngesupMbed01 28:8b5ccd2f837e 167
IngesupMbed01 28:8b5ccd2f837e 168 frame newFrame = m_trameRxBuffer.frameBuffer();
IngesupMbed01 28:8b5ccd2f837e 169
IngesupMbed01 30:119e060b45b3 170 switch(newFrame.moduleIdentifier) {
IngesupMbed01 31:ec7d635636bf 171 case 0x31 : consigneModuleTrajectoire(newFrame);
IngesupMbed01 28:8b5ccd2f837e 172 break;
IngesupMbed01 31:ec7d635636bf 173 case 0x32 : consigneModuleVideo(newFrame);
IngesupMbed01 28:8b5ccd2f837e 174 break;
IngesupMbed01 28:8b5ccd2f837e 175
IngesupMbed01 31:ec7d635636bf 176 default : creerTrameSpeciale(0x99);
IngesupMbed01 31:ec7d635636bf 177 break;
IngesupMbed01 31:ec7d635636bf 178
IngesupMbed01 28:8b5ccd2f837e 179 }
IngesupMbed01 31:ec7d635636bf 180 }
IngesupMbed01 31:ec7d635636bf 181
IngesupMbed01 31:ec7d635636bf 182 void C_ModuleCommunication::consigneModuleTrajectoire(frame newFrame)
IngesupMbed01 31:ec7d635636bf 183 {
IngesupMbed01 31:ec7d635636bf 184 switch(newFrame.messageIdentifier) {
IngesupMbed01 31:ec7d635636bf 185 case 0x11 : COMMANDE_TRAJECTOIRE = newFrame.data[0];
IngesupMbed01 31:ec7d635636bf 186 EN_MOUVEMENT = true;
IngesupMbed01 31:ec7d635636bf 187 while(EN_MOUVEMENT != false);
IngesupMbed01 31:ec7d635636bf 188 creerTrameModuleTrajectoire(0x12);
IngesupMbed01 31:ec7d635636bf 189 break;
IngesupMbed01 31:ec7d635636bf 190 case 0x13 : STOP = true;
IngesupMbed01 31:ec7d635636bf 191 EN_MOUVEMENT = true;
IngesupMbed01 31:ec7d635636bf 192 while(EN_MOUVEMENT != false);
IngesupMbed01 31:ec7d635636bf 193 creerTrameModuleTrajectoire(0x14);
IngesupMbed01 31:ec7d635636bf 194 break;
IngesupMbed01 31:ec7d635636bf 195 case 0x15 : creerTrameModuleTrajectoire(0x16);
IngesupMbed01 31:ec7d635636bf 196 break;
IngesupMbed01 31:ec7d635636bf 197
IngesupMbed01 31:ec7d635636bf 198 default : creerTrameSpeciale(0x99);
IngesupMbed01 31:ec7d635636bf 199 break;
IngesupMbed01 31:ec7d635636bf 200 }
IngesupMbed01 31:ec7d635636bf 201 }
IngesupMbed01 31:ec7d635636bf 202
IngesupMbed01 31:ec7d635636bf 203 void C_ModuleCommunication::creerTrameModuleTrajectoire(unsigned char idMessage)
IngesupMbed01 31:ec7d635636bf 204 {
IngesupMbed01 31:ec7d635636bf 205 frame newFrame;
IngesupMbed01 31:ec7d635636bf 206 newFrame.moduleIdentifier = 0x31;
IngesupMbed01 31:ec7d635636bf 207
IngesupMbed01 31:ec7d635636bf 208 switch(idMessage) {
IngesupMbed01 31:ec7d635636bf 209 case 0x12 : newFrame.messageIdentifier = 0x12;
IngesupMbed01 31:ec7d635636bf 210 break;
IngesupMbed01 31:ec7d635636bf 211 case 0x14 : newFrame.messageIdentifier = 0x14;
IngesupMbed01 31:ec7d635636bf 212 break;
IngesupMbed01 31:ec7d635636bf 213 case 0x16 : newFrame.messageIdentifier = 0x16;
IngesupMbed01 31:ec7d635636bf 214 union conversionCharFloat conv1;
IngesupMbed01 31:ec7d635636bf 215 conv1.f = COORDONNEE_DRONE.x;
IngesupMbed01 31:ec7d635636bf 216 newFrame.data[0] = conv1.ch[0];
IngesupMbed01 31:ec7d635636bf 217 newFrame.data[1] = conv1.ch[1];
IngesupMbed01 31:ec7d635636bf 218 newFrame.data[2] = conv1.ch[2];
IngesupMbed01 31:ec7d635636bf 219 newFrame.data[3] = conv1.ch[3];
IngesupMbed01 31:ec7d635636bf 220 conv1.f = 0;
IngesupMbed01 31:ec7d635636bf 221 conv1.f = COORDONNEE_DRONE.y;
IngesupMbed01 31:ec7d635636bf 222 newFrame.data[4] = conv1.ch[0];
IngesupMbed01 31:ec7d635636bf 223 newFrame.data[5] = conv1.ch[1];
IngesupMbed01 31:ec7d635636bf 224 newFrame.data[6] = conv1.ch[2];
IngesupMbed01 31:ec7d635636bf 225 newFrame.data[7] = conv1.ch[3];
IngesupMbed01 31:ec7d635636bf 226 conv1.f = 0;
IngesupMbed01 31:ec7d635636bf 227 conv1.f = COORDONNEE_DRONE.z;
IngesupMbed01 31:ec7d635636bf 228 newFrame.data[8] = conv1.ch[0];
IngesupMbed01 31:ec7d635636bf 229 newFrame.data[9] = conv1.ch[1];
IngesupMbed01 31:ec7d635636bf 230 newFrame.data[10] = conv1.ch[2];
IngesupMbed01 31:ec7d635636bf 231 newFrame.data[11] = conv1.ch[3];
IngesupMbed01 31:ec7d635636bf 232 break;
IngesupMbed01 31:ec7d635636bf 233 }
IngesupMbed01 31:ec7d635636bf 234
IngesupMbed01 31:ec7d635636bf 235 m_trameTxBuffer.frameBuffer(newFrame);
IngesupMbed01 31:ec7d635636bf 236 }
IngesupMbed01 31:ec7d635636bf 237
IngesupMbed01 31:ec7d635636bf 238 void C_ModuleCommunication::creerTrameSpeciale(unsigned char idMessage) {
IngesupMbed01 31:ec7d635636bf 239 frame newFrame;
IngesupMbed01 31:ec7d635636bf 240
IngesupMbed01 31:ec7d635636bf 241 switch(idMessage) {
IngesupMbed01 31:ec7d635636bf 242 case 0x00 : newFrame.moduleIdentifier = 0x00;
IngesupMbed01 31:ec7d635636bf 243 newFrame.messageIdentifier = 0x00;
IngesupMbed01 31:ec7d635636bf 244 break;
IngesupMbed01 31:ec7d635636bf 245 case 0x99 : newFrame.moduleIdentifier = 0x99;
IngesupMbed01 31:ec7d635636bf 246 newFrame.messageIdentifier = 0x99;
IngesupMbed01 31:ec7d635636bf 247 break;
IngesupMbed01 31:ec7d635636bf 248 }
IngesupMbed01 31:ec7d635636bf 249 m_trameTxBuffer.frameBuffer(newFrame);
IngesupMbed01 31:ec7d635636bf 250 }
IngesupMbed01 31:ec7d635636bf 251
IngesupMbed01 31:ec7d635636bf 252 void C_ModuleCommunication::consigneModuleVideo(frame newFrame)
IngesupMbed01 31:ec7d635636bf 253 {
IngesupMbed01 31:ec7d635636bf 254 }