update protocol (add Node object) | update node.ino accordingly | fix packet (construct/destruct) + *.ino

This commit is contained in:
Adrien Marquès 2018-12-11 14:45:23 +01:00
parent 3186928912
commit d1ecf570db
5 changed files with 50 additions and 16 deletions

View File

@ -2,6 +2,7 @@
#include "lcd.h" #include "lcd.h"
#include "packet.h" // Packet #include "packet.h" // Packet
#include "xbee_wrapper.h" // WBeeWrapper #include "xbee_wrapper.h" // WBeeWrapper
#include "protocol.h"
#define SEND_TIMEOUT 10000 #define SEND_TIMEOUT 10000
@ -10,10 +11,10 @@ LCDWrapper screen(0x20, 16, 2);
XBeeWrapper xbee = XBeeWrapper(); XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
uint8_t wave_id = 255; // last wave
uint8_t dist = 255; // actual distance
unsigned long time; unsigned long time;
Packet recv, send; Node myself = Node();
Packet recv = Packet();
Packet send = Packet();
void setup() { void setup() {
Serial.begin(38400); Serial.begin(38400);
@ -26,6 +27,10 @@ void setup() {
time = millis(); time = millis();
} }
void send_data();
void manage_discover();
void manage_message();
void loop() { void loop() {
/* [1] If SEND_TIMEOUT reached -> send data */ /* [1] If SEND_TIMEOUT reached -> send data */
@ -56,10 +61,10 @@ void send_data(){
// 1. prepare message // 1. prepare message
send.setOpcode(OPCODE_MESSAGE); send.setOpcode(OPCODE_MESSAGE);
send.setSender(SENDERID); send.setSender(SENDERID);
send.setDist(dist); send.setDist(myself.getDist());
send.setTTL(MESSAGE_TTL); send.setTTL(MESSAGE_TTL);
char data[64] = {0}; char data[64] = {0};
sprintf(data, "%d has: dist %d / wave %d", SENDERID, dist, wave_id); sprintf(data, "%d has: dist %d / wave %d", SENDERID, myself.getDist(), myself.getWave());
send.setData(data); send.setData(data);
// 2. broadcast // 2. broadcast
@ -83,20 +88,16 @@ void manage_discover(){
screen.clear(); screen.clear();
screen.printfn(0, "dsc[ %3d / %3d ]", recv.getDist(), recv.getWave()); screen.printfn(0, "dsc[ %3d / %3d ]", recv.getDist(), recv.getWave());
// ignore if same wave // update features
if( recv.getWave() == wave_id ){ if( !myself.update(recv.getWave(), recv.getDist()) ){
screen.printfn(1, "x "); screen.printfn(1, "x ");
return; return;
} }
// update features
wave_id = recv.getWave();
dist = recv.getDist()+1;
// propagate wave // propagate wave
send.setOpcode(OPCODE_DISCOVER); send.setOpcode(OPCODE_DISCOVER);
send.setWave(wave_id); send.setWave(myself.getWave());
send.setDist(dist); send.setDist(myself.getDist());
screen.printfn(1, "dsc[ %3d / %3d ]", send.getDist(), send.getWave()); screen.printfn(1, "dsc[ %3d / %3d ]", send.getDist(), send.getWave());
xbee.broadcast(send); xbee.broadcast(send);
@ -122,7 +123,7 @@ void manage_message(){
delay(1000); delay(1000);
// ignore // ignore
if( recv.getDist() > dist ){ if( recv.getDist() > myself.getDist() ){
screen.printfn(1, "x "); screen.printfn(1, "x ");
return; return;
} }
@ -130,9 +131,11 @@ void manage_message(){
// propagation // propagation
// send.setOpcode(OPCODE_MESSAGE); // send.setOpcode(OPCODE_MESSAGE);
recv.setTTL(recv.getTTL()-1); recv.setTTL(recv.getTTL()-1);
recv.setDist(dist); recv.setDist(myself.getDist());
// send.setData(recv.getData()); // send.setData(recv.getData());
screen.printfn(1, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize()); screen.printfn(1, "msg[%3d/%3d] %3d", recv.getDist(), recv.getTTL(), recv.getSize());
delay(500);
screen.printfn(1, "%s", (char*) recv.getData());
xbee.broadcast(recv); xbee.broadcast(recv);
} }

View File

@ -3,6 +3,9 @@
Packet::Packet(){ Packet::Packet(){
msg.data = malloc(1 * sizeof(char)); msg.data = malloc(1 * sizeof(char));
} }
Packet::~Packet(){
free(msg.data);
}
/* PUBLIC /* PUBLIC
----------------------------------------*/ ----------------------------------------*/
// builds a packet from raw data and returns the error code // builds a packet from raw data and returns the error code

View File

@ -28,6 +28,7 @@
public: public:
Packet(); Packet();
~Packet();
// builds a packet from raw data and returns the status code // builds a packet from raw data and returns the status code
uint8_t read(uint8_t* buf, const size_t size); uint8_t read(uint8_t* buf, const size_t size);

View File

@ -32,4 +32,30 @@
#define PROTO_SIZE MESSAGE_MAX_SIZE #define PROTO_SIZE MESSAGE_MAX_SIZE
class Node{
private:
uint8_t wave = 0;
uint8_t dist = 255;
public:
getWave(){ return wave; }
getDist(){ return dist; }
bool update(uint8_t new_dist, uint8_t new_wave){
// ignore if already setup for this wave
if( new_wave == wave )
return false;
// update with new wave
//
// Note:
// - We consider that we will receive lowest dist(ance)
// in the first receive of each wave because of the XBee stack
wave = new_wave;
dist = new_dist+1;
return true;
}
};
#endif #endif

View File

@ -9,7 +9,8 @@ XBeeWrapper xbee = XBeeWrapper();
// ACTUAL DATA // ACTUAL DATA
uint8_t wave_id = 250; uint8_t wave_id = 250;
Packet recv, send; Packet recv = Packet();
Packet send = Packet();
unsigned long time; unsigned long time;
void setup() { void setup() {