pre commentaire

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Fork of Code_APP3_C by Éric Bisson

Files at this revision

API Documentation at this revision

Comitter:
ericbisson
Date:
Tue Feb 14 04:08:58 2017 +0000
Parent:
8:6f2b7f9b0d0d
Child:
10:565271e4d52a
Commit message:
app3

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
header.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
xbee.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Team-APP1/code/EthernetInterface/#3f4995e0e6c7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/samux/code/WebSocketClient/#4567996414a5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,5 @@
+static const short PAN_ID = 0x0777;
+static char* WEBSOCKET_URL= "ws://192.168.0.1:8000/";
+static const char* mbedIp       = "192.168.0.2";
+static const char* mbedMask     = "255.255.255.0";
+static const char* mbedGateway  = "192.168.0.1";
\ No newline at end of file
--- a/header.h	Tue Jan 17 02:09:22 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-/********************************/
-// Configuration
-/********************************/
-#define IS_USING_SPI false
-#define MOVING_AVG_SIZE 10
-/********************************/
-#define PI 3.1416
-
-int* PINSEL0 = (int*)0x4002C000;
-int* U3FCR = (int*)0x4009C008;
-int* U3LCR = (int*)0x4009C00C;
-int* U3DLM = (int*)0x4009C004;
-int* U3DLL = (int*)0x4009C000;
-int* PCONP = (int*)0x400FC0C4;
-int* PCLKSEL1 = (int*)0x400FC1AC;
-int* U3TER = (int*)0x4009C030;
-int* U3THR = (int*)0x4009C000;
-int* U3LSR = (int*)0x4009C014;
-
-struct MovingAverage_t
-{
-    int buffer[MOVING_AVG_SIZE];
-    char cursor;
-    bool bFilled;
-};
-MovingAverage_t MovingAverage = {
-        {},
-        0,
-        false
-    };
-
-void write_to_7segment(char d1, char d2, char d3, char d4);
-int moyenne_mobile(int newData);
-void calculer_angle(char bufferAngle[], int accZ);
-void change_dots(char dot);
-void chip_select(bool bSet);
-void SetClockAndMode(int Speed, char Mode);
-void ResetCursor();
-unsigned char reverse(unsigned char b);
-void write_uart(unsigned char value);
\ No newline at end of file
--- a/main.cpp	Tue Jan 17 02:09:22 2017 +0000
+++ b/main.cpp	Tue Feb 14 04:08:58 2017 +0000
@@ -1,202 +1,126 @@
-#include "mbed.h"
-#include "header.h"
-
-Serial pc(USBTX, USBRX);
-SPI spi(p11, p12, p13);
-DigitalOut cs(p14);
-I2C i2c(p28, p27);
-PwmOut led1(LED1);
+#include "xbee.h"
+#include "config.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
 
-void calculer_angle(char bufferAngle[], int accZ)
-{
-    float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
-    int angleInt = (int)(angle*100);
-    
-    // reset buffer
-    bufferAngle[0] = '0';
-    bufferAngle[1] = '0';
-    bufferAngle[2] = '0';
-    bufferAngle[3] = '0';
-    
-    signed char pos = 3;
-    while (angleInt > 1 && pos >= 0)
-    {
-        bufferAngle[pos] = angleInt % 10;
-        angleInt = angleInt / 10;
-        pos--;
-    }
-}
-
-void uart_init()
-{
-    pc.printf("uart before\n");
-    
-    *PCONP   |=  0x02000000;            // Turn ON PCUART3
-    *PCLKSEL1 |= 0x00040000;            // Set PCLK_UART3 to 01 (CLK / 1)
-    *PINSEL0 &= ~0x00000003;            // Turn Off TxD3 P0.0
-    *PINSEL0 |=  0x00000002;            // Enable TxD3 P0.0
-    *U3FCR   |=  0x00000007;            // Reset Tx, Reset Rx, Enable FIFO
-    *U3LCR   |=  0x00000083;            // Enable DLAB, 8-bit char length
-    *U3DLM   |=  0x00000000;            // DLMSB ; Baud rate
-    *U3DLL   |=  0x00000060;            // DLLSB ; Baud rate
-    *U3LCR   &= ~0x00000080;            // Turn off DLAB
-    
-    pc.printf("uart init()\n");
-}
+//#define __DEBUG__
+//#ifdef __DEBUG__
+Serial pc(USBTX, USBRX, 9600);
+//#endif
+PwmOut led1(LED1);
+PwmOut led4(LED4);
 
 int main() {
-    int addrChip = 0x3A;
-    char buffer[3] = {0,0,0};
     
-    SetClockAndMode(250000, 0);
-    change_dots(0x02);
+    // Setup du port ethernet
+    EthernetInterface eth;
+    eth.init(mbedIp, mbedMask, mbedGateway);
+    eth.connect();
+    
+    DigitalOut RESET(p8);
+    Serial XBee(p13, p14, 9600);
     
-    //Activer l'accéléromètre pour lecture 8 bits
-    char activation[2] = {0x2A, 0x03};
-    char fullScale[2] = {0x0E, 0x00};
-    char resultat[4] = {'0','0','0','0'};
-    i2c.write(addrChip, activation, 2, true);
-    i2c.write(addrChip, fullScale, 2, true);
+    // Selon le lab, reset le Xbee
+    RESET = 0;
+    wait_ms(400);
+    RESET = 1;
+
+#ifdef __DEBUG__
+    pc.format(8, SerialBase::None, 1);
+#endif
+    XBee.format(8, SerialBase::None, 1);
+    
+    CArray DATA_TO_READ;
+    
+    bool IsInitialized = false;
+    bool bAddressSet = false;
+    char InitBytes = 0;
+    
+    Websocket ws(WEBSOCKET_URL);
+    ws.connect();
+    wait(1);
     
     while(1)
-    {   
-        //Aller lire les valeurs d'accélération
-        buffer[0] = 0x01;
-        i2c.write(addrChip, buffer, 1, true);
-        i2c.read(addrChip, buffer, 3);
-
-        calculer_angle(resultat, buffer[2]);
-        
-        chip_select(false);
-        write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
-        chip_select(true);
-        wait(0.1);
-    }
-}
+    {
+#ifdef __DEBUG__
+        if (pc.readable())
+        {
+            XBee.putc(pc.getc());
+        }
+#endif
+    
+        if (IsInitialized)
+        {
+            DATA_TO_READ._ptr = NULL;
+#ifdef __DEBUG__
+            if (XBee.readable())
+            {
+                pc.putc(XBee.getc());
+            }
+#endif
+            read(&XBee, &DATA_TO_READ);
 
-void SetClockAndMode(int Speed, char Mode)
-{
-    if (IS_USING_SPI)
-    {
-        spi.format(8,Mode);
-        spi.frequency(Speed);
-    }
-    else
-    {
-        uart_init();
-    }
-}
-
-void write_uart(unsigned char value)
-{   pc.printf("%d - ", reverse(value));
-    *U3THR = reverse(value); // Data to send ; LSB first
-    *U3TER |= 0x80;          // Enable TXEn
-    //*U3TER &= ~0x80;         // Disable TXEn
-}
-
-// inverse bit order
-unsigned char reverse(unsigned char b) 
-{
-   b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
-   b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
-   b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
-   return b;
-}
+            if (DATA_TO_READ._ptr != NULL)
+            {
+                led1 = !led1;
+                
+                if (!bAddressSet)
+                {
+                    bAddressSet = true;
 
-// function to change displayed dots
-void change_dots(char dot)
-{
-    if (IS_USING_SPI)
-    {
-        spi.write(0x77);
-        spi.write(dot);
-    }
-    else
-    {
-        write_uart(0x77);
-        write_uart(dot);
-    }
-}
-
-void ResetCursor()
-{
-    if (IS_USING_SPI)
-    {
-        spi.write(0x79);
-        spi.write(0);
-    }
-    else
-    {
-        write_uart(0x79);
-        write_uart(0);
-    }
-}
-
-// function used to write numbers to all four digits
-void write_to_7segment(char d1, char d2, char d3, char d4)
-{
-    ResetCursor();
-    if (IS_USING_SPI)
-    {
-        spi.write(d1);
-        spi.write(d2);
-        spi.write(d3);
-        spi.write(d4);
-    }
-    else
-    {
-        write_uart(d1);
-        write_uart(d2);
-        write_uart(d3);
-        write_uart(d4);
-        pc.printf("\n");
-    }
-}
-
-void chip_select(bool bSet)
-{
-    if (IS_USING_SPI)
-    {
-        if (!bSet)
+                    CArray DATA_TO_SEND;
+                    
+                    DATA_TO_SEND._64bit = DATA_TO_READ._64bit; // <-- Je récupère l'addresse au premier paquet. Inutile de faire plus pour l'app ...
+                    DATA_TO_SEND._16bit = DATA_TO_READ._16bit;
+                    
+                    DATA_TO_SEND._FrameType = 0x17; // Remote AT Request
+                    
+                    // options pour l'envoie au coordinateur
+                    DATA_TO_SEND.options = new char[1];
+                    DATA_TO_SEND.options[0] = 0x02;
+                    DATA_TO_SEND.opt_size = 1;
+                    
+                    DATA_TO_SEND._ptr = new char[3];
+                    DATA_TO_SEND._ptr[0] = 'L';
+                    DATA_TO_SEND._ptr[1] = 'T';
+                    DATA_TO_SEND._ptr[2] = 100; // x10ms donc 1000ms donc 1hz
+                    DATA_TO_SEND.size = 3;
+                    
+                    send(&XBee, &DATA_TO_SEND);
+#ifdef __DEBUG__
+                    send(&pc, &DATA_TO_SEND);
+#endif
+                    
+                    delete DATA_TO_SEND._ptr;
+                    delete DATA_TO_SEND.options;
+                }
+                
+#ifdef __DEBUG__
+                for (int i = 0; i < DATA_TO_READ.size; i++)
+                {
+                    pc.putc(DATA_TO_READ._ptr[i]); // debug ; send to pc
+                }
+#endif
+                ws.send(DATA_TO_READ._ptr);
+                wait_ms(5);
+                char* result;
+                ws.read(result);
+                
+                pc.printf(result);
+                
+                delete DATA_TO_READ._ptr;
+            }
+        }
+        else if (XBee.readable())
         {
-            wait_us(25);
-        }
-        cs = bSet;
-        if (bSet)
-        {
-            wait_us(25);
+#ifdef __DEBUG__
+            pc.putc(XBee.getc());
+#endif
+            InitBytes++;
+            if (InitBytes == 6)
+            {
+                IsInitialized = true;
+            }
         }
     }
-}
-
-// function used to calculate and return the new value of a moving average
-int moyenne_mobile(int newData)
-{
-    int sum = 0;
-    MovingAverage.buffer[MovingAverage.cursor] = newData;
-    MovingAverage.cursor++;
-    if (MovingAverage.cursor >= MOVING_AVG_SIZE)
-    {
-        MovingAverage.cursor = 0;
-        MovingAverage.bFilled = true;
-    }
-    
-    if (MovingAverage.bFilled)
-    {
-        for (int i = 0; i < MOVING_AVG_SIZE; i++)
-        {
-            sum += MovingAverage.buffer[i];
-        }
-        sum = sum / MOVING_AVG_SIZE;
-    }
-    else
-    {
-        for (int i = 0; i < MovingAverage.cursor; i++)
-        {
-            sum += MovingAverage.buffer[i];
-        }
-        sum = sum / MovingAverage.cursor;
-    }
-    
-    return sum;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- a/mbed.bld	Tue Jan 17 02:09:22 2017 +0000
+++ b/mbed.bld	Tue Feb 14 04:08:58 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/99b5ccf27215
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.cpp	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,200 @@
+#include "xbee.h"
+
+Timeout timeOut;
+PwmOut led_error(LED2);
+void LED_TurnOff()
+{
+    led_error = 0;
+}
+
+//--------------------------------------------------------------//
+// Fonction d'envoie de données pour ZigBee (Transmit request)
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void send(Serial* COM, CArray* message)
+{
+    short opt_size = 0;
+    COM->putc(0x7E); // Start delimiter
+    COM->putc(0x00); // length MSB is always 0
+    
+    if (message->_FrameType == 0x08) // Les commandes AT ne reçoivent pas d'addresses
+    {
+        COM->putc(6);
+    }
+    else
+    {
+        COM->putc(message->size + 12 + message->opt_size);
+    }
+
+    COM->putc(message->_FrameType);
+    COM->putc(0x01); // Frame ID
+    
+    if (message->_FrameType != 0x08) // Les commandes AT ne reçoivent pas d'addresses
+    {
+        COM->putc(message->_64bit.bit[0]);
+        COM->putc(message->_64bit.bit[1]);
+        COM->putc(message->_64bit.bit[2]);
+        COM->putc(message->_64bit.bit[3]);
+        COM->putc(message->_64bit.bit[4]);
+        COM->putc(message->_64bit.bit[5]);
+        COM->putc(message->_64bit.bit[6]);
+        COM->putc(message->_64bit.bit[7]);
+        COM->putc(message->_16bit.bit[0]);
+        COM->putc(message->_16bit.bit[1]);
+        
+        for (int i = 0; i < message->opt_size; i++)
+        {
+            opt_size += message->options[i];
+            COM->putc(message->options[i]);
+        }
+    }
+    
+    for (int j = 0; j < message->size; j++)
+    {
+        COM->putc(message->_ptr[j]);
+    }
+    COM->putc(calculate_checksum(message, 
+        message->_FrameType + 
+        0x01 +
+        opt_size +      
+        message->_16bit.bit[0] + 
+        message->_16bit.bit[1] + 
+        message->_64bit.bit[0] + 
+        message->_64bit.bit[1] + 
+        message->_64bit.bit[2] + 
+        message->_64bit.bit[3] + 
+        message->_64bit.bit[4] + 
+        message->_64bit.bit[5] + 
+        message->_64bit.bit[6] + 
+        message->_64bit.bit[7]
+    ));
+}
+//--------------------------------------------------------------//
+
+//--------------------------------------------------------------//
+// Fonction pour la réception de données (Packet Receive)
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void read(Serial* COM, CArray* returnValue)
+{
+    char BYTE_ID = 0;
+    char opts = 0;
+    bool bIsTrame = false;
+    while (true)
+    if (COM->readable())
+    {            
+        char value = COM->getc();
+        
+        if (bIsTrame)
+        {
+            switch (BYTE_ID)
+            {
+            case 1:
+                break;
+            case 2: // on ignore le 1, car c'est toujours 0x00
+                returnValue->size = value - 12;
+                break;
+            case 3:                  
+                if (value != 0x90)
+                    return;
+                returnValue->_FrameType = value;
+                break;
+            case 4:
+            case 5:
+            case 6:
+            case 7:
+            case 8:
+            case 9:
+            case 10:
+            case 11:
+                returnValue->_64bit.bit[BYTE_ID - 4] = value;
+                break;
+            case 12:
+            case 13: // <-- Addresse 16 bit de l'envoyeur ici
+                returnValue->_16bit.bit[BYTE_ID - 12] = value;
+                break;
+            case 14: // je gère pas les options pour là
+                opts = value;
+                break;
+            default:
+                if (BYTE_ID < 15 + returnValue->size)
+                {
+                    if (returnValue->_ptr == NULL)
+                        returnValue->_ptr = new char[returnValue->size];
+                        
+                    returnValue->_ptr[BYTE_ID - 15] = value;
+                    break;
+                }
+                // Finalement, le checksum. On vérifie que c'est bon
+                if (value != calculate_checksum(returnValue, 
+                        0x90 +
+                        opts + 
+                        returnValue->_16bit.bit[0] + 
+                        returnValue->_16bit.bit[1] + 
+                        returnValue->_64bit.bit[0] + 
+                        returnValue->_64bit.bit[1] + 
+                        returnValue->_64bit.bit[2] + 
+                        returnValue->_64bit.bit[3] + 
+                        returnValue->_64bit.bit[4] + 
+                        returnValue->_64bit.bit[5] + 
+                        returnValue->_64bit.bit[6] + 
+                        returnValue->_64bit.bit[7]
+                ))
+                {
+                    // Gestion d'erreur ici?
+                    led_error = 1;
+                    timeOut.attach(LED_TurnOff, 1);
+                }
+                return;
+            }
+        }
+        else if (value == 0x7E)
+            bIsTrame = true;
+        else
+            return;
+
+        //wait_ms(3); // petit sleep pour recevoir un nouveau byte
+        BYTE_ID++;
+    }
+}
+//--------------------------------------------------------------//
+
+//--------------------------------------------------------------//
+// Fonction de checksum pour le protocole ZigBee
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+char calculate_checksum(CArray* message, short header_size)
+{
+    short sum = header_size;
+    if (message != NULL)
+    {
+        for (int i = 0; i < message->size; i++)
+        {
+            sum += message->_ptr[i];
+        }
+    }
+    return 0xFF - (sum & 0x00FF);
+}
+//--------------------------------------------------------------//
+
+void setPAN(Serial* COM, short pan_id)
+{
+    CArray DATA_TO_SEND;
+    for (int j = 0; j < 8; j++) // Je force tout à 0 pour les addresses
+        DATA_TO_SEND._64bit.bit[j] = 0;
+    DATA_TO_SEND._16bit.bit[0] = 0;
+    DATA_TO_SEND._16bit.bit[1] = 0;
+    DATA_TO_SEND.opt_size = 0;
+    
+    DATA_TO_SEND._FrameType = 0x08; // AT Command
+    DATA_TO_SEND.size = 4;
+    DATA_TO_SEND._ptr = new char[4];
+    DATA_TO_SEND._ptr[0] = 'I';
+    DATA_TO_SEND._ptr[1] = 'D';
+    DATA_TO_SEND._ptr[2] = pan_id >> 8;
+    DATA_TO_SEND._ptr[3] = pan_id & 0x00FF;
+    
+    send(COM, &DATA_TO_SEND);
+    
+    delete DATA_TO_SEND._ptr;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.h	Tue Feb 14 04:08:58 2017 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+
+#ifndef XBEE_H
+#define XBEE_H
+
+struct Addresse_64Bit {
+    char bit[8];
+};
+
+struct Addresse_16Bit {
+    char bit[2];
+};
+
+struct CArray {
+    char* _ptr;
+    short size;
+    char _FrameType;
+    Addresse_64Bit _64bit;
+    Addresse_16Bit _16bit;
+    char* options;
+    char opt_size;
+};
+
+char calculate_checksum(CArray* message, short header_size);
+void send(Serial* COM, CArray* message);
+void read(Serial* COM, CArray* returnValue);
+void read(Serial* COM, CArray* returnValue);
+void setPAN(Serial* COM, short pan_id);
+
+#endif // XBEE_H
\ No newline at end of file