MBED code for Xbee module running on solar car

Dependencies:   XBeeLib mbed CUER_CAN

Fork of XBee802_Send_Data by Digi International Inc.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #include "CAN_Parser_Telemetry.h"
00016 #if defined(ENABLE_LOGGING)
00017 #include "DigiLoggerMbedSerial.h"
00018 using namespace DigiLog;
00019 using namespace CAN_IDs;
00020 #endif
00021 
00022 #define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
00023 
00024 //#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
00025 #define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x4076A4CB)
00026 
00027 //#error "Replace next define with the remote module's 16-bit address (MY parameter)"
00028 #define REMOTE_NODE_ADDR16      ((uint16_t)0x00)
00029 
00030 #define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
00031 
00032 #define DEBUG                   0
00033 using namespace XBeeLib;
00034 
00035 Serial *log_serial;
00036 
00037 static void send_broadcast_data(XBee802& xbee, uint8_t *data, uint16_t data_len)
00038 {
00039     const TxStatus txStatus = xbee.send_data_broadcast(data, data_len);
00040 
00041     if (txStatus == TxStatusSuccess)
00042         printf("send_broadcast_data OK\r\n");
00043     else
00044         printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
00045 }
00046 
00047 static void send_data_to_remote_node(XBee802& xbee, const RemoteXBee802& RemoteDevice, uint8_t *data, uint16_t data_len)
00048 {
00049     const TxStatus txStatus = xbee.send_data(RemoteDevice, data, data_len);
00050 
00051     if (txStatus == TxStatusSuccess) {
00052         if (DEBUG) {
00053         printf("send_data_to_remote_node OK\r\n");
00054         }
00055         }
00056     else
00057         printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
00058 }
00059 
00060 static void readFromVolBufferandRedirect2Xbee(XBee802& xbee, const RemoteXBee802 remoteDevice64b)
00061 {
00062     if(DEBUG) printf("In main loop \r\n");
00063     uint8_t actualData[10];
00064     //Import the data from the buffer into a non-volatile, more usable format
00065     CAN_Data can_data[CAN_BUFFER_SIZE]; //container for all of the raw data
00066     int received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
00067     for (int i = 0; i<CAN_BUFFER_SIZE; i++) {
00068         safe_to_write[i] = false;
00069         can_data[i].importCANData(buffer[i]);
00070         received_CAN_IDs[i] = buffer[i].id;
00071         buffer[i] = 0;
00072         safe_to_write[i] = true;
00073 
00074         if (received_CAN_IDs[i]!=BLANK_ID) {
00075             if (DEBUG) printf("Picked up CAN packet! \r\n");
00076             if (DEBUG) printf("CAN ID is %x \r\n", received_CAN_IDs[i]);
00077             //Xbee packet format is such that byte 1 = CAN ID, byte 1-9 = CAN data...
00078             actualData[0]=(uint8_t)received_CAN_IDs[i];
00079             actualData[1]=(uint8_t)(received_CAN_IDs[i]>>8);
00080             //printf("actualData[1] is %x \r\n", actualData[1]);
00081             for (int x=0; x<8; x++) {
00082                 actualData[x+2]=can_data[i].get_u8(x);
00083             }
00084             send_data_to_remote_node(xbee, remoteDevice64b, actualData, 10);
00085             wait_ms(100);
00086             if (DEBUG) printf("\r\n");
00087         }
00088     }
00089 }
00090 
00091 //Serial *log_serial;
00092 int main()
00093 {
00094     CAN_Init();
00095     CANIDsListUpdater();
00096     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00097     log_serial->baud(9600);
00098     //log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n");
00099     //log_serial->printf(XB_LIB_BANNER);
00100 
00101 #if defined(ENABLE_LOGGING)
00102     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00103 #endif
00104 
00105     XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00106 
00107     RadioStatus radioStatus = xbee.init();
00108     MBED_ASSERT(radioStatus == Success);
00109 
00110     const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64);
00111     const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16);
00112     while (1) {
00113         //send_broadcast_data(xbee);
00114         readFromVolBufferandRedirect2Xbee(xbee, remoteDevice64b);
00115         wait_ms(200);
00116         //wait(1);
00117         //send_data_to_remote_node(xbee, remoteDevice16b);
00118     }
00119     delete(log_serial);
00120 }