diff --git a/buzz_scripts/include/timesync.bzz b/buzz_scripts/include/timesync.bzz index 41f6561..8843795 100644 --- a/buzz_scripts/include/timesync.bzz +++ b/buzz_scripts/include/timesync.bzz @@ -1,5 +1,7 @@ TIME_TO_SYNC = 100 sync_timer = 0 +timesync_old_state = 0 +timesync_itr = 0 timesync_state = 0 # Function to sync. algo @@ -9,11 +11,18 @@ function check_time_sync(){ log(" Synchronizing Clock : ",sync_timer,"/",TIME_TO_SYNC) timesync_state = 1 } - else timesync_state = 0 + else{ + timesync_state = 0 + } + if(timesync_old_state == 0 and timesync_state == 1){ + timesync_itr = timesync_itr + 1 + timesync_old_state = 0 + } + timesync_old_state = timesync_state } # Function to set sync timer to zero and reinitiate sync. algo function reinit_time_sync(){ sync_timer = 0 -} \ No newline at end of file +} diff --git a/include/buzz_utility.h b/include/buzz_utility.h index 951207e..047b575 100644 --- a/include/buzz_utility.h +++ b/include/buzz_utility.h @@ -34,18 +34,6 @@ struct rb_struct }; typedef struct rb_struct RB_struct; -struct neiMsg -{ - uint8_t* msg; - uint16_t msgid; - double time_received; - double time_sent; - neiMsg(uint8_t* m, uint16_t mid, double tr, double ts) - : msg(m), msgid(mid), time_received(tr), time_sent(ts) {}; - neiMsg(); -}; -typedef struct pos_struct nei_msg_struct; - struct neiStatus { uint gps_strenght = 0; @@ -66,7 +54,7 @@ struct neitime int age; neitime(uint64_t nht, uint64_t nlt, uint64_t mht, uint64_t mlt, double nr, double mr) : nei_hardware_time(nht), nei_logical_time(nlt), node_hardware_time(mht), node_logical_time(mlt), - nei_rate(nr), relative_rate(mr),age(0) {}; + nei_rate(nr), relative_rate(mr) {}; neitime() { } @@ -113,4 +101,6 @@ std::vector get_inmsg_vector(); std::string getuavstate(); int get_timesync_state(); + +int get_timesync_itr(); } diff --git a/include/roscontroller.h b/include/roscontroller.h index 1ab67af..9713a9d 100644 --- a/include/roscontroller.h +++ b/include/roscontroller.h @@ -49,7 +49,7 @@ typedef enum { } rosbuzz_msgtype; // Time sync algo. constants -#define COM_DELAY 33000000 // in nano seconds i.e 33 ms +#define COM_DELAY 100000000 // in nano seconds i.e 100 ms #define TIME_SYNC_JUMP_THR 500000000 #define MOVING_AVERAGE_ALPHA 0.1 @@ -62,25 +62,6 @@ using namespace std; namespace rosbzz_node { - -template -class circularBuffer { -private: - vector data; - unsigned int lastEntryPos; - int size; - -public: - circularBuffer(uint8_t s): data(s), lastEntryPos(0), size(s){}; - ~circularBuffer(){}; - void push(T d){ - data[lastEntryPos] = d; - if(lastEntryPos > size-1) lastEntryPos = 0; - else lastEntryPos++; - } - vector get_data(){ return data;}; -}; - class roscontroller { public: @@ -130,7 +111,6 @@ private: }; typedef struct MsgData msg_data; - ros_pose target, home, cur_pos; uint64_t payload; @@ -141,11 +121,10 @@ private: int robot_id = 0; ros::Time logical_clock; ros::Time previous_step_time; + std::vector inmsgdata; + double out_msg_time; double logical_time_rate; bool time_sync_jumped; - double com_delay; - std::vector inmsgdata; - double out_msg_time; std::string robot_name = ""; int rc_cmd; @@ -154,7 +133,7 @@ private: int barrier; int update; int statepub_active; - int16_t msg_id = 0; + int message_number = 0; uint8_t no_of_robots = 0; bool rcclient; bool xbeeplugged = false; diff --git a/src/buzz_utility.cpp b/src/buzz_utility.cpp index fdc7277..ebedb52 100644 --- a/src/buzz_utility.cpp +++ b/src/buzz_utility.cpp @@ -587,7 +587,7 @@ string getuavstate() int get_timesync_state() /* -/ return current logical time +/ return time sync state from bzz script --------------------------------------*/ { int timesync_state = 0; @@ -600,4 +600,20 @@ int get_timesync_state() } return timesync_state; } +int get_timesync_itr() +/* +/ return time sync iteration from bzz script +--------------------------------------*/ +{ + int timesync_itr = 0; + if(VM){ + buzzvm_pushs(VM, buzzvm_string_register(VM, "timesync_itr", 1)); + buzzvm_gload(VM); + buzzobj_t obj = buzzvm_stack_at(VM, 1); + if(obj->o.type == BUZZTYPE_INT) timesync_itr = obj->i.value; + buzzvm_pop(VM); + } + return timesync_itr; +} + } diff --git a/src/roscontroller.cpp b/src/roscontroller.cpp index 6e10840..1e42ebe 100644 --- a/src/roscontroller.cpp +++ b/src/roscontroller.cpp @@ -63,11 +63,9 @@ logical_clock(ros::Time()), previous_step_time(ros::Time()) } // time sync algo. parameter intialization previous_step_time.fromNSec(ros::Time::now().toNSec()); - if(robot_id==10)logical_clock.fromSec(101.0); - else logical_clock.fromNSec(0); - logical_time_rate = 1; + logical_clock.fromSec(0); + logical_time_rate = 0; time_sync_jumped = false; - com_delay = 0; out_msg_time=0; // Create log dir and open log file std::string path = @@ -172,12 +170,14 @@ void roscontroller::RosControllerRun() } if (debug) ROS_WARN("CURRENT PACKET DROP : %f ", cur_packet_loss); + // log data // hardware time now log< 0)log<<","; @@ -203,10 +203,10 @@ void roscontroller::RosControllerRun() } inmsgdata.clear(); log<payload64[0]; - uint8_t r8header[8]; - uint8_t mtype; - uint16_t mid; - uint32_t temptime; - memcpy(r8header,&rheader, sizeof(uint64_t)); - memcpy(&mtype, r8header, sizeof(uint8_t)); - memcpy(&mid, r8header+1, sizeof(uint16_t)); - memcpy(&temptime, r8header+3, sizeof(uint32_t)); + uint64_t rhead = msg->payload64[0]; + uint16_t r16head[4]; + memcpy(r16head,&rhead, sizeof(uint64_t)); + uint16_t mtype = r16head[0]; + uint16_t mid = r16head[1]; + uint32_t temptime=0; + memcpy(&temptime, r16head+2, sizeof(uint32_t)); float stime = (float)temptime/(float)100000; ROS_INFO("received stime %f for %u",stime, mid); + ROS_INFO("Received header : type %u mid %u time %u",r16head[0],r16head[1],r16head[2]); // Check for Updater message, if updater message push it into updater FIFO - if ((int)mtype == (int)UPDATER_MESSAGE) + if ((uint64_t)msg->payload64[0] == (uint64_t)UPDATER_MESSAGE) { uint16_t obt_msg_size = sizeof(uint64_t) * (msg->payload64.size()); uint64_t message_obt[obt_msg_size]; @@ -1147,7 +1140,7 @@ void roscontroller::payload_obt(const mavros_msgs::Mavlink::ConstPtr& msg) // Copy packet into temporary buffer neglecting update constant memcpy((void*)pl, (void*)(message_obt + 1), obt_msg_size); uint16_t unMsgSize = *(uint16_t*)(pl); - if(debug) ROS_INFO("Update packet received, size : %u", unMsgSize); + fprintf(stdout, "Update packet received, read msg size : %u \n", unMsgSize); if (unMsgSize > 0) { buzz_update::code_message_inqueue_append((uint8_t*)(pl + sizeof(uint16_t)), unMsgSize); @@ -1165,15 +1158,6 @@ void roscontroller::payload_obt(const mavros_msgs::Mavlink::ConstPtr& msg) { message_obt[i - 1] = (uint64_t)msg->payload64[i]; } - // determine buzz msg index based on msg type - int index = 3; - if((int)mtype == (int)BUZZ_MESSAGE_TIME) index = 5; - // Extract robot id of the neighbour - uint16_t* out = buzz_utility::u64_cvt_u16((uint64_t) * (message_obt + index)); - get_logical_time(); - // store in msg data for data log - msg_data inm(mid,out[1],out[0]*sizeof(uint64_t),stime,logical_clock.toSec()); - inmsgdata.push_back(inm); // Extract neighbours position from payload double neighbours_pos_payload[3]; memcpy(neighbours_pos_payload, message_obt, 3 * sizeof(uint64_t)); @@ -1184,8 +1168,16 @@ void roscontroller::payload_obt(const mavros_msgs::Mavlink::ConstPtr& msg) nei_pos.longitude = neighbours_pos_payload[1]; nei_pos.altitude = neighbours_pos_payload[2]; double cvt_neighbours_pos_payload[3]; - // Compute RB in robot body ref. frame gps_rb(nei_pos, cvt_neighbours_pos_payload); + int index = 3; + if((int)mtype == (int)BUZZ_MESSAGE_TIME) index = 5; + // Extract robot id of the neighbour + uint16_t* out = buzz_utility::u64_cvt_u16((uint64_t) * (message_obt + index)); + get_logical_time(); + // store in msg data for data log + msg_data inm(mid,out[1],out[0]*sizeof(uint64_t),stime,logical_clock.toSec()); + inmsgdata.push_back(inm); + if (debug) ROS_WARN("RAB of %i: %f, %f", (int)out[1], cvt_neighbours_pos_payload[0], cvt_neighbours_pos_payload[1]); // Pass neighbour position to local maintaner @@ -1196,16 +1188,17 @@ void roscontroller::payload_obt(const mavros_msgs::Mavlink::ConstPtr& msg) // TODO: remove roscontroller local map array for neighbors neighbours_pos_put((int)out[1], n_pos); buzzuav_closures::neighbour_pos_callback((int)out[1], n_pos.x, n_pos.y, n_pos.z); - if((int)mtype == (int)BUZZ_MESSAGE_TIME){ + if((int)mtype == (int)BUZZ_MESSAGE_TIME){ // update time struct for sync algo double nr = 0; push_timesync_nei_msg((int)out[1], message_obt[3],message_obt[4],nr); + delete[] out; buzz_utility::in_msg_append((message_obt + index)); } else{ + delete[] out; buzz_utility::in_msg_append((message_obt + index)); } - delete[] out; } } @@ -1529,7 +1522,7 @@ void roscontroller::push_timesync_nei_msg(int nid, uint64_t nh, uint64_t nl, dou buzz_utility::neighbor_time nt(nh, nl, ros::Time::now().toNSec(), logical_clock.toNSec(), nr, relativeRate); neighbours_time_map.insert(make_pair(nid, nt)); - + ROS_INFO("SYNC MSG RECEIVED deltaLocal from %i",nid); } uint64_t roscontroller::get_logical_time(){