MBED code for Xbee module running on solar car
Dependencies: XBeeLib mbed CUER_CAN
Fork of XBee802_Send_Data by
main.cpp@14:e625e467e257, 2017-10-07 (annotated)
- Committer:
- ItsJustZi
- Date:
- Sat Oct 07 02:49:23 2017 +0000
- Revision:
- 14:e625e467e257
- Parent:
- 13:173e69872505
Final working implementation of send data before modifying to meet MDH requirements
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hbujanda | 5:1b2d489605b1 | 1 | /** |
hbujanda | 5:1b2d489605b1 | 2 | * Copyright (c) 2015 Digi International Inc., |
hbujanda | 5:1b2d489605b1 | 3 | * All rights not expressly granted are reserved. |
hbujanda | 5:1b2d489605b1 | 4 | * |
hbujanda | 5:1b2d489605b1 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
hbujanda | 5:1b2d489605b1 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
hbujanda | 5:1b2d489605b1 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
hbujanda | 5:1b2d489605b1 | 8 | * |
hbujanda | 5:1b2d489605b1 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
hbujanda | 5:1b2d489605b1 | 10 | * ======================================================================= |
hbujanda | 5:1b2d489605b1 | 11 | */ |
hbujanda | 5:1b2d489605b1 | 12 | |
hbujanda | 5:1b2d489605b1 | 13 | #include "mbed.h" |
hbujanda | 5:1b2d489605b1 | 14 | #include "XBeeLib.h" |
ItsJustZi | 12:13762b16b300 | 15 | #include "CAN_Parser_Telemetry.h" |
hbujanda | 5:1b2d489605b1 | 16 | #if defined(ENABLE_LOGGING) |
hbujanda | 5:1b2d489605b1 | 17 | #include "DigiLoggerMbedSerial.h" |
hbujanda | 5:1b2d489605b1 | 18 | using namespace DigiLog; |
ItsJustZi | 12:13762b16b300 | 19 | using namespace CAN_IDs; |
hbujanda | 5:1b2d489605b1 | 20 | #endif |
hbujanda | 5:1b2d489605b1 | 21 | |
hbujanda | 5:1b2d489605b1 | 22 | #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200) |
spastor | 7:ab86cae2e208 | 23 | |
ItsJustZi | 11:338fcc83ef26 | 24 | //#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)" |
ItsJustZi | 11:338fcc83ef26 | 25 | #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x4076A4CB) |
spastor | 7:ab86cae2e208 | 26 | |
ItsJustZi | 11:338fcc83ef26 | 27 | //#error "Replace next define with the remote module's 16-bit address (MY parameter)" |
ItsJustZi | 11:338fcc83ef26 | 28 | #define REMOTE_NODE_ADDR16 ((uint16_t)0x00) |
hbujanda | 5:1b2d489605b1 | 29 | |
hbujanda | 5:1b2d489605b1 | 30 | #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB) |
hbujanda | 5:1b2d489605b1 | 31 | |
ItsJustZi | 13:173e69872505 | 32 | #define DEBUG 0 |
hbujanda | 5:1b2d489605b1 | 33 | using namespace XBeeLib; |
hbujanda | 5:1b2d489605b1 | 34 | |
ItsJustZi | 12:13762b16b300 | 35 | Serial *log_serial; |
hbujanda | 5:1b2d489605b1 | 36 | |
ItsJustZi | 12:13762b16b300 | 37 | static void send_broadcast_data(XBee802& xbee, uint8_t *data, uint16_t data_len) |
hbujanda | 5:1b2d489605b1 | 38 | { |
ItsJustZi | 12:13762b16b300 | 39 | const TxStatus txStatus = xbee.send_data_broadcast(data, data_len); |
hbujanda | 5:1b2d489605b1 | 40 | |
hbujanda | 5:1b2d489605b1 | 41 | if (txStatus == TxStatusSuccess) |
ItsJustZi | 11:338fcc83ef26 | 42 | printf("send_broadcast_data OK\r\n"); |
hbujanda | 5:1b2d489605b1 | 43 | else |
ItsJustZi | 11:338fcc83ef26 | 44 | printf("send_broadcast_data failed with %d\r\n", (int) txStatus); |
hbujanda | 5:1b2d489605b1 | 45 | } |
hbujanda | 5:1b2d489605b1 | 46 | |
ItsJustZi | 12:13762b16b300 | 47 | static void send_data_to_remote_node(XBee802& xbee, const RemoteXBee802& RemoteDevice, uint8_t *data, uint16_t data_len) |
hbujanda | 5:1b2d489605b1 | 48 | { |
ItsJustZi | 12:13762b16b300 | 49 | const TxStatus txStatus = xbee.send_data(RemoteDevice, data, data_len); |
hbujanda | 5:1b2d489605b1 | 50 | |
ItsJustZi | 13:173e69872505 | 51 | if (txStatus == TxStatusSuccess) { |
ItsJustZi | 13:173e69872505 | 52 | if (DEBUG) { |
ItsJustZi | 11:338fcc83ef26 | 53 | printf("send_data_to_remote_node OK\r\n"); |
ItsJustZi | 13:173e69872505 | 54 | } |
ItsJustZi | 13:173e69872505 | 55 | } |
hbujanda | 5:1b2d489605b1 | 56 | else |
ItsJustZi | 11:338fcc83ef26 | 57 | printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus); |
hbujanda | 5:1b2d489605b1 | 58 | } |
hbujanda | 5:1b2d489605b1 | 59 | |
ItsJustZi | 12:13762b16b300 | 60 | static void readFromVolBufferandRedirect2Xbee(XBee802& xbee, const RemoteXBee802 remoteDevice64b) |
ItsJustZi | 12:13762b16b300 | 61 | { |
ItsJustZi | 13:173e69872505 | 62 | if(DEBUG) printf("In main loop \r\n"); |
ItsJustZi | 13:173e69872505 | 63 | uint8_t actualData[10]; |
ItsJustZi | 12:13762b16b300 | 64 | //Import the data from the buffer into a non-volatile, more usable format |
ItsJustZi | 12:13762b16b300 | 65 | CAN_Data can_data[CAN_BUFFER_SIZE]; //container for all of the raw data |
ItsJustZi | 12:13762b16b300 | 66 | int received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far |
ItsJustZi | 12:13762b16b300 | 67 | for (int i = 0; i<CAN_BUFFER_SIZE; i++) { |
ItsJustZi | 12:13762b16b300 | 68 | safe_to_write[i] = false; |
ItsJustZi | 12:13762b16b300 | 69 | can_data[i].importCANData(buffer[i]); |
ItsJustZi | 12:13762b16b300 | 70 | received_CAN_IDs[i] = buffer[i].id; |
ItsJustZi | 12:13762b16b300 | 71 | buffer[i] = 0; |
ItsJustZi | 12:13762b16b300 | 72 | safe_to_write[i] = true; |
ItsJustZi | 12:13762b16b300 | 73 | |
ItsJustZi | 12:13762b16b300 | 74 | if (received_CAN_IDs[i]!=BLANK_ID) { |
ItsJustZi | 13:173e69872505 | 75 | if (DEBUG) printf("Picked up CAN packet! \r\n"); |
ItsJustZi | 13:173e69872505 | 76 | if (DEBUG) printf("CAN ID is %x \r\n", received_CAN_IDs[i]); |
ItsJustZi | 12:13762b16b300 | 77 | //Xbee packet format is such that byte 1 = CAN ID, byte 1-9 = CAN data... |
ItsJustZi | 12:13762b16b300 | 78 | actualData[0]=(uint8_t)received_CAN_IDs[i]; |
ItsJustZi | 13:173e69872505 | 79 | actualData[1]=(uint8_t)(received_CAN_IDs[i]>>8); |
ItsJustZi | 13:173e69872505 | 80 | //printf("actualData[1] is %x \r\n", actualData[1]); |
ItsJustZi | 12:13762b16b300 | 81 | for (int x=0; x<8; x++) { |
ItsJustZi | 13:173e69872505 | 82 | actualData[x+2]=can_data[i].get_u8(x); |
ItsJustZi | 12:13762b16b300 | 83 | } |
ItsJustZi | 13:173e69872505 | 84 | send_data_to_remote_node(xbee, remoteDevice64b, actualData, 10); |
ItsJustZi | 13:173e69872505 | 85 | wait_ms(100); |
ItsJustZi | 13:173e69872505 | 86 | if (DEBUG) printf("\r\n"); |
ItsJustZi | 12:13762b16b300 | 87 | } |
ItsJustZi | 12:13762b16b300 | 88 | } |
ItsJustZi | 12:13762b16b300 | 89 | } |
ItsJustZi | 12:13762b16b300 | 90 | |
ItsJustZi | 12:13762b16b300 | 91 | //Serial *log_serial; |
hbujanda | 5:1b2d489605b1 | 92 | int main() |
hbujanda | 5:1b2d489605b1 | 93 | { |
ItsJustZi | 12:13762b16b300 | 94 | CAN_Init(); |
ItsJustZi | 12:13762b16b300 | 95 | CANIDsListUpdater(); |
hbujanda | 5:1b2d489605b1 | 96 | log_serial = new Serial(DEBUG_TX, DEBUG_RX); |
hbujanda | 5:1b2d489605b1 | 97 | log_serial->baud(9600); |
ItsJustZi | 12:13762b16b300 | 98 | //log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n"); |
ItsJustZi | 12:13762b16b300 | 99 | //log_serial->printf(XB_LIB_BANNER); |
hbujanda | 5:1b2d489605b1 | 100 | |
hbujanda | 5:1b2d489605b1 | 101 | #if defined(ENABLE_LOGGING) |
hbujanda | 5:1b2d489605b1 | 102 | new DigiLoggerMbedSerial(log_serial, LogLevelInfo); |
hbujanda | 5:1b2d489605b1 | 103 | #endif |
hbujanda | 5:1b2d489605b1 | 104 | |
hbujanda | 5:1b2d489605b1 | 105 | XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); |
hbujanda | 5:1b2d489605b1 | 106 | |
hbujanda | 5:1b2d489605b1 | 107 | RadioStatus radioStatus = xbee.init(); |
hbujanda | 5:1b2d489605b1 | 108 | MBED_ASSERT(radioStatus == Success); |
hbujanda | 5:1b2d489605b1 | 109 | |
hbujanda | 5:1b2d489605b1 | 110 | const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64); |
hbujanda | 5:1b2d489605b1 | 111 | const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16); |
ItsJustZi | 12:13762b16b300 | 112 | while (1) { |
ItsJustZi | 12:13762b16b300 | 113 | //send_broadcast_data(xbee); |
ItsJustZi | 12:13762b16b300 | 114 | readFromVolBufferandRedirect2Xbee(xbee, remoteDevice64b); |
ItsJustZi | 14:e625e467e257 | 115 | wait_ms(200); |
ItsJustZi | 13:173e69872505 | 116 | //wait(1); |
ItsJustZi | 12:13762b16b300 | 117 | //send_data_to_remote_node(xbee, remoteDevice16b); |
ItsJustZi | 12:13762b16b300 | 118 | } |
hbujanda | 5:1b2d489605b1 | 119 | delete(log_serial); |
ItsJustZi | 12:13762b16b300 | 120 | } |