ZigBee初探

来源:互联网 发布:tomcat下js文件未生效 编辑:程序博客网 时间:2024/05/17 09:17

最近几天在帮同学做毕设,日程如下:

5.27-5.31 完成了协调器接收路由器的信号信息,对协调器进行定位。

6.01-6.02 写了定位算法,重心法;想移植到CC2530上面去,结果会有错误,经过测试,发现是芯片要用40s完成一次计算;

6.03        完成了终端广播数据,路由器收到数据回传给终端;本以为接收到的数据就是发送方对接收方的RSSI,结果我错了。

     参考:http://www.deyisupport.com/question_answer/wireless_connectivity/zigbee/f/104/t/20299.aspx         您说的这一现象我遇到了,但是终端在广播数据时,opinion设置为AF_SKIP_ROUTING,那么其他路由节点收到该广播的信号强度就可以得到了,这又怎么理解呢,既然终端不具有广播能力,那么设置AF_SKIP_ROUTING又怎么可以广播成功  

要得到点对点的RSSI,就必须点对点的传输,所以要设置skip routing!!!!!!!!

6.04        改编了自己的毕设上位机,大概完成了定位功能,将定位算法移植到上位机上计算。

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

说说我的体会吧,终于体会到了51单片机计算能力的缺陷。CC2530带有51内核,但是处理协议栈估计就耗费很多资源了,最后的计算最好还是交给主控芯片ARM或者其他芯片处理吧。但是我还没有写过芯片到芯片的数据传输实验。。。哈哈,有待提高哦!!!!!

 

 

附SampleApp源代码

其中.h 头文件有部分定义 #define SAMPLEAPP_P2P_CLUSTERID      4

参考文档:FS_ZigBee评估板基础实验指导书.pdf FS_ZigBee协议栈实验指导书.pdf  FS_ZigBee2007视频教程-应用开发指导实验手册.pdf    入门可以看看锋硕的视频教程

 

 

 

/**************************************************************************************************  Filename:       SampleApp.c  Revised:        $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $  Revision:       $Revision: 19453 $  Description:    Sample Application (no Profile).  Copyright 2007 Texas Instruments Incorporated. All rights reserved.  IMPORTANT: Your use of this Software is limited to those specific rights  granted under the terms of a software license agreement between the user  who downloaded the software, his/her employer (which must be your employer)  and Texas Instruments Incorporated (the "License").  You may not use this  Software unless you agree to abide by the terms of the License. The License  limits your use, and you acknowledge, that the Software may not be modified,  copied or distributed unless embedded on a Texas Instruments microcontroller  or used solely and exclusively in conjunction with a Texas Instruments radio  frequency transceiver, which is integrated into your product.  Other than for  the foregoing purpose, you may not use, reproduce, copy, prepare derivative  works of, modify, distribute, perform, display or sell this Software and/or  its documentation for any purpose.  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE  PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.  Should you have any questions regarding your right to use this Software,  contact Texas Instruments Incorporated at www.TI.com.**************************************************************************************************//*********************************************************************  This application isn't intended to do anything useful, it is  intended to be a simple example of an application's structure.  This application sends it's messages either as broadcast or  broadcast filtered group messages.  The other (more normal)  message addressing is unicast.  Most of the other sample  applications are written to support the unicast message model.  Key control:    SW1:  Sends a flash command to all devices in Group 1.    SW2:  Adds/Removes (toggles) this device in and out          of Group 1.  This will enable and disable the          reception of the flash command.*********************************************************************//********************************************************************* * INCLUDES */#include "OSAL.h"#include "ZGlobals.h"#include "AF.h"#include "aps_groups.h"#include "ZDApp.h"#include "SampleApp.h"#include "SampleAppHw.h"#include "OnBoard.h"#include "string.h"#include "hal_lcd.h"#include "math.h"/* HAL */#include "hal_lcd.h"#include "hal_led.h"#include "hal_key.h"#include "MT_UART.h"#include "MT_APP.h"#include "MT.h"/********************************************************************* * MACROS *//********************************************************************* * CONSTANTS *//********************************************************************* * TYPEDEFS *//********************************************************************* * GLOBAL VARIABLES *///remain to be finishedfloat DistTable[70] = {0.00, 0.005,0.008,0.010,0.015,0.020,0.021, 0.023,0.026,0.030,0.04, 0.05, 0.06, 0.074, 0.08,0.088,0.096, 0.10, 0.11, 0.118,0.126,0.133,0.140,0.148,0.160,0.175,0.190,0.228,0.257,0.286,0.314,0.343,0.371,0.400,0.44, 0.48, 0.52, 0.56, 0.60, 0.65,0.70, 0.75, 0.80, 0.90, 1.0,  1.1,  1.2,  1.266,1.333,1.40,1.5,  1.6,  1.666,1.733,1.800,1.9,  2.0,  2.2,  2.266,2.333,2.4,  2.466,2.533,2.6,  2.65, 2.70, 2.75, 2.8,  2.9,  3.0};//下雨环境测量的数据float DistTableRain[51] = {0.01, 0.013,0.014,0.015,0.017,0.02, 0.022,0.026,0.027,0.031,0.038,0.004,0.045,0.052,0.055,0.061,0.08, 0.09, 0.108,0.115,0.124,0.146,0.160,0.175,0.188,0.215,0.27, 0.295,0.32, 0.353,0.39, 0.435,0.486,0.56, 0.61, 0.636,0.68, 0.74, 0.79, 0.82,0.89, 0.92, 0.95, 1.09, 1.16, 1.28, 1.33, 1.365,1.59, 1.80, 2.14};int cnt = 0;float dx = 0;float dy = 0;// This list should be filled with Application specific Cluster IDs.const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] ={  SAMPLEAPP_PERIODIC_CLUSTERID,  SAMPLEAPP_FLASH_CLUSTERID};const SimpleDescriptionFormat_t SampleApp_SimpleDesc ={  SAMPLEAPP_ENDPOINT,              //  int Endpoint;  SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];  SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];  SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;  SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;  SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;  (cId_t *)SampleApp_ClusterList,  //  uint8 *pAppInClusterList;  SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;  (cId_t *)SampleApp_ClusterList   //  uint8 *pAppInClusterList;};// This is the Endpoint/Interface description.  It is defined here, but// filled-in in SampleApp_Init().  Another way to go would be to fill// in the structure here and make it a "const" (in code space).  The// way it's defined in this sample app it is define in RAM.endPointDesc_t SampleApp_epDesc;/********************************************************************* * EXTERNAL VARIABLES *//********************************************************************* * EXTERNAL FUNCTIONS *//********************************************************************* * LOCAL VARIABLES */uint8 SampleApp_TaskID;   // Task ID for internal task/event processing                          // This variable will be received when                          // SampleApp_Init() is called.devStates_t SampleApp_NwkState;uint8 SampleApp_TransID;  // This is the unique message ID (counter)afAddrType_t SampleApp_Periodic_DstAddr; //广播afAddrType_t SampleApp_Flash_DstAddr;    //组播afAddrType_t SampleApp_P2P_DstAddr;      //点播aps_Group_t SampleApp_Group;uint8 SampleAppPeriodicCounter = 0;uint8 SampleAppFlashCounter = 0;/********************************************************************* * LOCAL FUNCTIONS */void SampleApp_HandleKeys( uint8 shift, uint8 keys );void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );void SampleApp_SendPeriodicMessage( void );void SampleApp_SendFlashMessage( uint16 flashTime );void SampleApp_Send_P2P_Message(void);void SampleApp_SerialCMD(mtOSALSerialData_t *cmdMsg);/********************************************************************* * NETWORK LAYER CALLBACKS *//********************************************************************* * PUBLIC FUNCTIONS *//********************************************************************* * @fn      SampleApp_Init * * @brief   Initialization function for the Generic App Task. *          This is called during initialization and should contain *          any application specific initialization (ie. hardware *          initialization/setup, table initialization, power up *          notificaiton ... ). * * @param   task_id - the ID assigned by OSAL.  This ID should be *                    used to send messages and set timers. * * @return  none */void SampleApp_Init( uint8 task_id ){   SampleApp_TaskID = task_id;  SampleApp_NwkState = DEV_INIT;  SampleApp_TransID = 0;    MT_UartInit();                  //串口初始化  MT_UartRegisterTaskID(task_id); //注册串口任务  HalUARTWrite(0,"UartInit OK\n", sizeof("UartInit OK\n"));//提示信息    // Device hardware initialization can be added here or in main() (Zmain.c).  // If the hardware is application specific - add it here.  // If the hardware is other parts of the device add it in main(). #if defined ( BUILD_ALL_DEVICES )  // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START  // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered  // together - if they are - we will start up a coordinator. Otherwise,  // the device will start as a router.  if ( readCoordinatorJumper() )    zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;  else    zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;#endif // BUILD_ALL_DEVICES#if defined ( HOLD_AUTO_START )  // HOLD_AUTO_START is a compile option that will surpress ZDApp  //  from starting the device and wait for the application to  //  start the device.  ZDOInitDevice(0);#endif  // Setup for the periodic message's destination address  // Broadcast to everyone  SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播  SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;  SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;  // Setup for the flash command's destination address - Group 1  SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;//组播  SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;  SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;    SampleApp_P2P_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; //点播   SampleApp_P2P_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;   SampleApp_P2P_DstAddr.addr.shortAddr = 0x0000;            //发给协调器  // Fill out the endpoint description.  SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;  SampleApp_epDesc.task_id = &SampleApp_TaskID;  SampleApp_epDesc.simpleDesc            = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;  SampleApp_epDesc.latencyReq = noLatencyReqs;  // Register the endpoint description with the AF  afRegister( &SampleApp_epDesc );  // Register for all key events - This app will handle all key events  RegisterForKeys( SampleApp_TaskID );  // By default, all devices start out in Group 1  SampleApp_Group.ID = 0x0001;  osal_memcpy( SampleApp_Group.name, "Group 1", 7  );  aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );#if defined ( LCD_SUPPORTED )  HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 );#endif}/********************************************************************* * @fn      SampleApp_ProcessEvent * * @brief   Generic Application Task event processor.  This function *          is called to process all events for the task.  Events *          include timers, messages and any other user defined events. * * @param   task_id  - The OSAL assigned task ID. * @param   events - events to process.  This is a bit map and can *                   contain more than one event. * * @return  none */uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ){  afIncomingMSGPacket_t *MSGpkt;  (void)task_id;  // Intentionally unreferenced parameter  if ( events & SYS_EVENT_MSG )  {    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );    while ( MSGpkt )    {      switch ( MSGpkt->hdr.event )      {        // Received when a key is pressed        case KEY_CHANGE:          SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );          break;        // Received when a messages is received (OTA) for this endpoint        case AF_INCOMING_MSG_CMD:          /*here to receive broadcasting msg from end...*/          //if coorp receive the msg...//          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);//          if ( (SampleApp_NwkState == DEV_ZB_COORD) )          {            HalLedBlink( HAL_LED_3, 3, 50, 100 );          //  if ( (SampleApp_NwkState == DEV_ZB_COORD) )                //    HalUARTWrite(0, "E2C:", 4);             SampleApp_MessageMSGCB( MSGpkt );            }                    break;        // Received whenever the device changes state in the network        case ZDO_STATE_CHANGE:          SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);          if ( //(SampleApp_NwkState == DEV_ZB_COORD) ||                 (SampleApp_NwkState == DEV_ROUTER)              || (SampleApp_NwkState == DEV_END_DEVICE) )          {            // Start sending the periodic message in a regular interval.            osal_start_timerEx( SampleApp_TaskID,                              SAMPLEAPP_SEND_PERIODIC_MSG_EVT,                              SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );          }          else          {            // Device is no longer in the network          }          break;        default:          break;      }      // Release the memory      osal_msg_deallocate( (uint8 *)MSGpkt );      // Next - if one is available      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );    }    // return unprocessed events    return (events ^ SYS_EVENT_MSG);  }  // Send a message out - This event is generated by a timer  //  (setup in SampleApp_Init()).  if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )  {    // Send the periodic message    //SampleApp_SendPeriodicMessage();    /*******************************************************************/    //self comment firstly.here    //  SampleApp_Send_P2P_Message();    //self comment end        /*here to finish end device to send periodic meg...**********/    if ( (SampleApp_NwkState == DEV_END_DEVICE) )    {      SampleApp_SendPeriodicMessage();        HalLedBlink( HAL_LED_3, 3, 50, 100 );      // Setup to send message again in normal period (+ a little jitter)      osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,        (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );      // return unprocessed events      return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);    }              }  // Discard unknown events  return 0;}/********************************************************************* * Event Generation Functions *//********************************************************************* * @fn      SampleApp_HandleKeys * * @brief   Handles all key events for this device. * * @param   shift - true if in shift/alt. * @param   keys - bit field for key events. Valid entries: *                 HAL_KEY_SW_2 *                 HAL_KEY_SW_1 * * @return  none */void SampleApp_HandleKeys( uint8 shift, uint8 keys ){  (void)shift;  // Intentionally unreferenced parameter    if ( keys & HAL_KEY_SW_1 )  {    /* This key sends the Flash Command is sent to Group 1.     * This device will not receive the Flash Command from this     * device (even if it belongs to group 1).     */    SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );  }  if ( keys & HAL_KEY_SW_2 )  {    /* The Flashr Command is sent to Group 1.     * This key toggles this device in and out of group 1.     * If this device doesn't belong to group 1, this application     * will not receive the Flash command sent to group 1.     */    aps_Group_t *grp;    grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );    if ( grp )    {      // Remove from the group      aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );    }    else    {      // Add to the flash group      aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );    }  }}/********************************************************************* * LOCAL FUNCTIONS *//********************************************************************* * @fn      SampleApp_MessageMSGCB * * @brief   Data message processor callback.  This function processes *          any incoming data - probably from other devices.  So, based *          on cluster ID, perform the intended action. * * @param   none * * @return  none */void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ){  uint16 flashTime;  unsigned char strRssi[2];  uint16 RcvRssi;  uint8 ROUTER[5] = "01234";  unsigned char strP2Pdist[5];/**************************************************************8*/   // add calc function //  float d;  int dtemp;  uint8 ge;  uint8 fen;  uint8 li;  unsigned char strDistx[5];  unsigned char strDisty[5];  int8 xiabiao1,xiabiao2,xiabiao3;    /******************************************************************/  /*  unsigned char ShortAddr[2];  byte  RouterAddr[8];  uint8 ROUTER[] = {  0x24,0x9A, // router 3  0x18,0xf3, // router 2  0x21,0x7B  // router 4 resevced..  };    */  switch ( pkt->clusterId )  {    case SAMPLEAPP_P2P_CLUSTERID://    if( (SampleApp_NwkState == DEV_ZB_COORD) ) // it must be from coorp, because it's P2P msg    {          RcvRssi = 0 - pkt->rssi;      strRssi[0]=RcvRssi/10+'0';      strRssi[1]=RcvRssi%10+'0';     /*      HalUARTWrite(0,"pkt->srcAddr.addr.extAddr", 25);      HalUARTWrite(0, pkt->srcAddr.addr.extAddr, 16);       //提示接收到数据//      HalUARTWrite(0,"pkt->srcAddr.addr.shortAddr", 27);//      strcpy(ShortAddr, pkt->srcAddr.addr.shortAddr);//      HalUARTWrite(0, ShortAddr, 2);       //提示接收到数据//      HalUARTWrite(0,"pkt->srcAddr.addr.addrMode", 26);//      HalUARTWrite(0, pkt->srcAddr.addr.shortAddr, 16);       //提示接收到数据 */      if( (pkt->cmd.Data[0] == ROUTER[2]) )      {        cnt ++;        HalUARTWrite(0, "Rx from router2:", 16);       //提示接收到数据        xiabiao1 = RcvRssi;         if(xiabiao1 > 70) xiabiao1  = 70;        if(xiabiao1 < 0) xiabiao1 = 0;        dtemp = ( int ) (DistTable[xiabiao1] * 100);      }      else if( (pkt->cmd.Data[0] == ROUTER[3]) )      {        cnt ++;        HalUARTWrite(0, "Rx from router3:", 16);       //提示接收到数据        xiabiao2 = RcvRssi;         if(xiabiao2 > 70) xiabiao2  = 70;        if(xiabiao2 < 0) xiabiao2 = 0;        dtemp = ( int ) (DistTable[xiabiao2] * 100);      }      else if( (pkt->cmd.Data[0] == ROUTER[4]) )      {          cnt ++;        HalUARTWrite(0, "Rx from router4:", 16);       //提示接收到数据        xiabiao3 = RcvRssi;         if(xiabiao3 > 70) xiabiao3  = 70;        if(xiabiao3 < 0) xiabiao3 = 0;        dtemp = ( int ) (DistTable[xiabiao3] * 100);            }      else          HalUARTWrite(0, "Rx from routerX:", 16);       //提示接收到数据            ge = dtemp / 100;      fen = (dtemp / 10) % 10;      li = dtemp % 10;      strDistx[0] = ge  + '0';      strDistx[1] =       '.';      strDistx[2] = fen + '0';      strDistx[3] = li  + '0';      strDistx[4] = 'm';            HalUARTWrite(0, strDistx, 5); //串口输出接收到的数据              HalUARTWrite(0, strRssi, 2); //串口输出接收到的数据      HalUARTWrite(0, "\n" , strlen("\n"));   /**************************************************************8*/        // add calc function...      if( cnt >= 3)      {        cnt = 0;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////              CalcDist( DistTable[xiabiao1], DistTable[xiabiao2], DistTable[xiabiao3] );            // global variables...      // dx, dy,       dtemp = ( int )(dx * 100);      ge = dtemp / 100;      fen = (dtemp / 10) % 10;      li = dtemp % 10;      strDistx[0] = ge  + '0';      strDistx[1] =       '.';      strDistx[2] = fen + '0';      strDistx[3] = li  + '0';      strDistx[4] =       ',';            dtemp = (int )(dy * 100);      ge = dtemp / 100;      fen = (dtemp / 10) % 10;      li = dtemp % 10;      strDisty[0] = ge  + '0';      strDisty[1] =       '.';      strDisty[2] = fen + '0';      strDisty[3] = li  + '0';      strDisty[4] =       ')';      HalUARTWrite(0, "location is:(", 13); //串口输出接收到的数据      HalUARTWrite(0, strDistx, 5); //串口输出接收到的数据      HalUARTWrite(0, strDisty, 5); //串口输出接收到的数据      HalUARTWrite(0, "\n" , strlen("\n"));         }                  /**************************************************************8*/            }          break;        case SAMPLEAPP_PERIODIC_CLUSTERID://    SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);    /***********************************************/    /* rcving a periodic msg, if by coorperation...*/    // to write the rssi from-end-to-coord    if ( (SampleApp_NwkState == DEV_ZB_COORD) )    {      //      cnt = 0;      RcvRssi = 0 - pkt->rssi;            HalLedBlink( HAL_LED_3, 3, 50, 100 );      strRssi[0]=RcvRssi/10+'0';      strRssi[1]=RcvRssi%10+'0';      HalUARTWrite(0, "E2C:", 4);      HalUARTWrite(0, strRssi, 2); //串口输出接收到的数据      HalUARTWrite(0, "\n" , strlen("\n"));        }    /***********************************************/    /* rcving a periodic msg, if by router      ...*/    // recv the packet, then preserve the rssi(end -> router) in pkt->cmd.data,     // then trigger a P2P event, transfer rssi to coord.    if ( (SampleApp_NwkState == DEV_ROUTER) )    {      HalLedBlink( HAL_LED_2, 3, 50, 100 );      SampleApp_Send_P2P_Message();//      osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,//        (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );        }                break;    case SAMPLEAPP_FLASH_CLUSTERID:      flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );      HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );      break;  }}/********************************************************************* * @fn      SampleApp_SendPeriodicMessage * * @brief   Send the periodic message. * * @param   none * * @return  none */void SampleApp_SendPeriodicMessage( void ){  uint8 data[6]="01234";  if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,                       SAMPLEAPP_PERIODIC_CLUSTERID,                       5,                       data, //(uint8*)&SampleAppPeriodicCounter,                       &SampleApp_TransID,                       AF_DISCV_ROUTE,                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  {  }  else  {    // Error occurred in request to send.  }}/********************************************************************* * @fn      SampleApp_SendFlashMessage * * @brief   Send the flash message to group 1. * * @param   flashTime - in milliseconds * * @return  none */void SampleApp_SendFlashMessage( uint16 flashTime ){  uint8 buffer[3];  buffer[0] = (uint8)(SampleAppFlashCounter++);  buffer[1] = LO_UINT16( flashTime );  buffer[2] = HI_UINT16( flashTime );  if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,                       SAMPLEAPP_FLASH_CLUSTERID,                       3,                       buffer,                       &SampleApp_TransID,                       AF_DISCV_ROUTE,                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  {  }  else  {    // Error occurred in request to send.  }}/********************************************************************* * @fn      SampleApp_Send_P2P_Message * * @brief   point to point. * * @param   none * * @return  none */void SampleApp_Send_P2P_Message( void ){  /* explaination: to configue witch router the P2P msg is from*/  // change the data[2] = 2, build project, and download to router2  // change the data[2] = 3, build project, and download to router3  // change the data[2] = 4, build project, and download to router4    uint8 data[2] = "2"; //different router sign   // N0.2 0x20, N0,3 0x90, N0.4 0xF0  Onboard_wait( 0x20 ); //(osal_rand() & 0x00FF)  if ( AF_DataRequest( &SampleApp_P2P_DstAddr, &SampleApp_epDesc,                       SAMPLEAPP_P2P_CLUSTERID,                       1,                       data,                       &SampleApp_TransID,                       AF_DISCV_ROUTE,                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  {  }  else  {    // Error occurred in request to send.  }}void CalcDist(float fd1, float fd2, float fd3 ){     // discrete position     float fDiscX = 0;    float fDiscY = 0;    // calc distance...    float fm1x = 0, fm1y = 0;    float fm2x = 0, fm2y = 0;    float fm3x = 0, fm3y = 0;    // estimate xy    float fEstiX = 0;    float fEstiY = 0;    // error min    float div = 0;    float a,b,c;        if(fd1 > 2) fd1 = 2;    else if(fd1 < 0) fd1 = 0;    if(fd2 > 2) fd2 = 2;    else if(fd2 < 0) fd2 = 0;    if(fd3 > 2) fd3 = 2;    else if(fd3 < 0) fd3 = 0;        // calc m1(x,y), m2(x,y), m3(x,y)    if( (fd1 + fd3) <= 2 )    {            fm1x = fd1 / (fd1 + fd3);            fm1y = sqrt(3) * fd1 / (fd1 + fd3);    }   if( (fd1 + fd2) <= 2 )    {            fm2x = 2 * fd1 / (fd1 + fd2);            fm2y = 0;    }    if( (fd2 + fd3) <= 2 )    {      fm3x = 1 + fd3 / (fd3 + fd2);      fm3y = sqrt(3) * fd2 / (fd3 + fd2);    }        for(fDiscY = 0; fDiscY < sqrt(3); fDiscY += 0.01 )      for(fDiscX = fDiscY / sqrt(3); fDiscX < (2 - fDiscY/sqrt(3) ); fDiscX += 0.01)      {                a = dist(fDiscX,fDiscY,0,0);        b = dist(fDiscX, fDiscY, 2, 0);        c = dist(fDiscX,fDiscY,1,sqrt(3));                if( (fd1 + fd3) > 2 )        if( (fabs( a- fd1) < 0.02) && (fabs(c - fd3) < 0.02))        {                fm1x = fDiscX;                fm1y = fDiscY;        }// end if                if( (fd2 + fd3) > 2 )        if( fabs(a - fd1) < 0.02 && fabs(b - fd2)< 0.02)        {          fm2x = fDiscX;          fm2y = fDiscY;        }// end if                if( (fd2 + fd3) > 2)        if(fabs(b - fd2) < 0.02 && fabs(c - fd3) < 0.02)        {            fm3x = fDiscX;            fm3y = fDiscY;        }// end if      }// end for        div = 1 / (fd1 + fd2) + 1 / (fd1 + fd3) + 1 / (fd2 + fd3);    fEstiX = ( fm1x/(fd1 + fd3) + fm2x/(fd1 + fd2) + fm3x/(fd2 + fd3) )/div;    fEstiY = ( fm1y/(fd1 + fd3) + fm2y/(fd1 + fd2) + fm3y/(fd2 + fd3) )/div;    dx = fEstiX;    dy = fEstiY;/* */    }float dist(float a, float b, float c, float d){return sqrt( pow(a-c, 2) + pow(b-d, 2));}/******************************************************************************************************************************************/


 

0 0
原创粉丝点击