aigamo get data
Dependencies: AigamozuControlPackets XBee agz_common mbed
Fork of agz_base_ver2 by
agz_base.cpp@1:533008368d76, 2014-06-18 (annotated)
- Committer:
- kityann
- Date:
- Wed Jun 18 13:50:11 2014 +0000
- Revision:
- 1:533008368d76
- Parent:
- 0:0aada4db04db
aigamo get data;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kityann | 0:0aada4db04db | 1 | #include "mbed.h" |
kityann | 0:0aada4db04db | 2 | #include "XBee.h" |
kityann | 0:0aada4db04db | 3 | |
kityann | 0:0aada4db04db | 4 | #include "agz_common.h" |
kityann | 0:0aada4db04db | 5 | #include "AigamozuControlPackets.h" |
kityann | 0:0aada4db04db | 6 | |
kityann | 0:0aada4db04db | 7 | |
kityann | 0:0aada4db04db | 8 | #define MY_BASE_NUMBER 0 |
kityann | 0:0aada4db04db | 9 | |
kityann | 0:0aada4db04db | 10 | XBee xbee(p13,p14); |
kityann | 0:0aada4db04db | 11 | Serial pc(USBTX, USBRX); // tx, rx |
kityann | 0:0aada4db04db | 12 | |
kityann | 0:0aada4db04db | 13 | Ticker timer; |
kityann | 0:0aada4db04db | 14 | |
kityann | 0:0aada4db04db | 15 | //robot data |
kityann | 0:0aada4db04db | 16 | AGZ_ROBOT robot[AGZ_NUM_ROBOTS]; |
kityann | 0:0aada4db04db | 17 | |
kityann | 0:0aada4db04db | 18 | |
kityann | 0:0aada4db04db | 19 | //function |
kityann | 0:0aada4db04db | 20 | //transform packetdata to robot data |
kityann | 0:0aada4db04db | 21 | void agz_transform_papcket(uint8_t* packet_data,int id){ |
kityann | 0:0aada4db04db | 22 | //set latitude, longitude, state |
kityann | 0:0aada4db04db | 23 | robot[id].set_state(packet_data[9]); |
kityann | 0:0aada4db04db | 24 | robot[id].set_LatitudeH(&packet_data[13]); |
kityann | 0:0aada4db04db | 25 | robot[id].set_LatitudeL(&packet_data[17]); |
kityann | 0:0aada4db04db | 26 | robot[id].set_LongitudeH(&packet_data[21]); |
kityann | 0:0aada4db04db | 27 | robot[id].set_LongitudeL(&packet_data[25]); |
kityann | 0:0aada4db04db | 28 | |
kityann | 0:0aada4db04db | 29 | } |
kityann | 0:0aada4db04db | 30 | |
kityann | 0:0aada4db04db | 31 | /*---------registere robot--------------*/ |
kityann | 0:0aada4db04db | 32 | /* |
kityann | 0:0aada4db04db | 33 | void agz_register_robot(uint8_*t packet_data){ |
kityann | 0:0aada4db04db | 34 | uint8_t robot_id = packet_data[5]; |
kityann | 0:0aada4db04db | 35 | robot[robot_id].set_state(0); |
kityann | 0:0aada4db04db | 36 | send_register_ack(); |
kityann | 0:0aada4db04db | 37 | } |
kityann | 0:0aada4db04db | 38 | */ |
kityann | 0:0aada4db04db | 39 | |
kityann | 0:0aada4db04db | 40 | /*---------timer set----*/ |
kityann | 0:0aada4db04db | 41 | void agz_send_request(){ |
kityann | 0:0aada4db04db | 42 | static uint8_t next_send_address = 0,i; |
kityann | 0:0aada4db04db | 43 | AigamozuControlPackets agz_packet; |
kityann | 0:0aada4db04db | 44 | |
kityann | 0:0aada4db04db | 45 | /*------search next moving robot--------*/ |
kityann | 0:0aada4db04db | 46 | for(i = next_send_address;i < AGZ_NUM_ROBOTS;i++){ |
kityann | 0:0aada4db04db | 47 | if(robot[i].get_state() != 0xFF){ |
kityann | 0:0aada4db04db | 48 | next_send_address = i; |
kityann | 0:0aada4db04db | 49 | break; |
kityann | 0:0aada4db04db | 50 | } |
kityann | 0:0aada4db04db | 51 | } |
kityann | 0:0aada4db04db | 52 | if(i == AGZ_NUM_ROBOTS){ |
kityann | 0:0aada4db04db | 53 | for(i = 0;i < AGZ_NUM_ROBOTS;i++){ |
kityann | 0:0aada4db04db | 54 | if(robot[i].get_state() != 0xFF){ |
kityann | 0:0aada4db04db | 55 | next_send_address = i; |
kityann | 0:0aada4db04db | 56 | break; |
kityann | 0:0aada4db04db | 57 | } |
kityann | 0:0aada4db04db | 58 | } |
kityann | 0:0aada4db04db | 59 | } |
kityann | 0:0aada4db04db | 60 | |
kityann | 0:0aada4db04db | 61 | agz_packet.createRequestCommand(MY_BASE_NUMBER,next_send_address); |
kityann | 0:0aada4db04db | 62 | |
kityann | 0:0aada4db04db | 63 | ZBTxRequest agz_request_packet = ZBTxRequest(Agz_XBee_Remote_Address[next_send_address], agz_packet.packetData, REQUEST_COMMNAD_LENGTH ); |
kityann | 0:0aada4db04db | 64 | |
kityann | 0:0aada4db04db | 65 | xbee.send(agz_request_packet); |
kityann | 0:0aada4db04db | 66 | |
kityann | 0:0aada4db04db | 67 | next_send_address++; |
kityann | 0:0aada4db04db | 68 | } |
kityann | 0:0aada4db04db | 69 | |
kityann | 0:0aada4db04db | 70 | |
kityann | 0:0aada4db04db | 71 | /*---------init---------*/ |
kityann | 0:0aada4db04db | 72 | void agz_base_init(){ |
kityann | 0:0aada4db04db | 73 | xbee.begin(57600); |
kityann | 0:0aada4db04db | 74 | pc.baud(57600); |
kityann | 0:0aada4db04db | 75 | timer.attach(&agz_send_request,2.0); |
kityann | 0:0aada4db04db | 76 | |
kityann | 0:0aada4db04db | 77 | |
kityann | 0:0aada4db04db | 78 | } |
kityann | 0:0aada4db04db | 79 | |
kityann | 0:0aada4db04db | 80 | int main(void){ |
kityann | 0:0aada4db04db | 81 | int sender,reciever; |
kityann | 0:0aada4db04db | 82 | |
kityann | 0:0aada4db04db | 83 | ZBRxResponse xbeeresponse; |
kityann | 0:0aada4db04db | 84 | |
kityann | 0:0aada4db04db | 85 | agz_base_init(); |
kityann | 0:0aada4db04db | 86 | |
kityann | 0:0aada4db04db | 87 | wait(1); |
kityann | 0:0aada4db04db | 88 | |
kityann | 0:0aada4db04db | 89 | printf("start\n"); |
kityann | 0:0aada4db04db | 90 | |
kityann | 0:0aada4db04db | 91 | |
kityann | 0:0aada4db04db | 92 | robot[0].set_state(0); |
kityann | 0:0aada4db04db | 93 | robot[1].set_state(0); |
kityann | 0:0aada4db04db | 94 | robot[2].set_state(0); |
kityann | 0:0aada4db04db | 95 | robot[3].set_state(0); |
kityann | 0:0aada4db04db | 96 | |
kityann | 0:0aada4db04db | 97 | |
kityann | 0:0aada4db04db | 98 | |
kityann | 0:0aada4db04db | 99 | while(1){ |
kityann | 0:0aada4db04db | 100 | xbee.readPacket(); |
kityann | 0:0aada4db04db | 101 | |
kityann | 0:0aada4db04db | 102 | //get packet data |
kityann | 0:0aada4db04db | 103 | if(xbee.getResponse().isAvailable() ){ |
kityann | 0:0aada4db04db | 104 | xbee.getResponse(xbeeresponse); |
kityann | 0:0aada4db04db | 105 | |
kityann | 0:0aada4db04db | 106 | //get data packet |
kityann | 0:0aada4db04db | 107 | if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE){ |
kityann | 0:0aada4db04db | 108 | uint8_t *packet_data = xbeeresponse.getData(); |
kityann | 0:0aada4db04db | 109 | uint8_t packet_kinds = packet_data[3]; |
kityann | 0:0aada4db04db | 110 | |
kityann | 0:0aada4db04db | 111 | //get sensor data |
kityann | 0:0aada4db04db | 112 | if(packet_kinds == 'R'){ |
kityann | 0:0aada4db04db | 113 | sender = packet_data[5] - 'A'; |
kityann | 0:0aada4db04db | 114 | reciever = packet_data[7] - 'a'; |
kityann | 0:0aada4db04db | 115 | agz_transform_papcket(packet_data,sender); |
kityann | 0:0aada4db04db | 116 | printf("%d,%d,%d,%ld.%ld,%ld.%ld\n",robot[sender].get_state(),sender,reciever, |
kityann | 0:0aada4db04db | 117 | robot[sender].get_LatitudeH(),robot[sender].get_LatitudeL(), |
kityann | 0:0aada4db04db | 118 | robot[sender].get_LongitudeH(),robot[sender].get_LongitudeL()); |
kityann | 0:0aada4db04db | 119 | } |
kityann | 0:0aada4db04db | 120 | //get register packet |
kityann | 0:0aada4db04db | 121 | if(packet_kinds == 'E'){ |
kityann | 0:0aada4db04db | 122 | //agz_register_robot(packet_data); |
kityann | 0:0aada4db04db | 123 | } |
kityann | 0:0aada4db04db | 124 | }else{ |
kityann | 0:0aada4db04db | 125 | //get not rx response |
kityann | 0:0aada4db04db | 126 | } |
kityann | 0:0aada4db04db | 127 | }else{ |
kityann | 0:0aada4db04db | 128 | |
kityann | 0:0aada4db04db | 129 | } |
kityann | 0:0aada4db04db | 130 | } |
kityann | 0:0aada4db04db | 131 | } |