Skip to content

Commit 2be2c7e

Browse files
committed
feat(tdma): setup of tdmamanager
1 parent 1751807 commit 2be2c7e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

acoustic_modem/include/tdma_manager.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
struct TDMAConfig{
9+
// should be two with just two nodes
910
uint8_t num_slots;
1011
uint8_t my_slot;
1112
std::chrono::seconds slot=std::chrono::seconds(25);
@@ -15,7 +16,7 @@ struct TDMAConfig{
1516

1617
class TDMAManager{
1718
public:
18-
TDMAManager(TDMAConfig config);
19+
TDMAManager(TDMAConfig config) : cfg(config){}
1920

2021
// who is transmitting?
2122
uint8_t current_slot(std::chrono::steady_clock::time_point now);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "tdma_manager.hpp"
2+
3+
// return type can be changed
4+
uint8_t TDMAManager::current_slot(std::chrono::steady_clock::time_point now){
5+
auto cycle=cfg.num_slots*cfg.slot;
6+
auto elapsed=now-cfg.t0;
7+
// how far are we in the cycle
8+
auto time_cycle=elapsed%cycle;
9+
uint8_t slot_i= time_cycle/cfg.slot;
10+
return slot_i
11+
}
12+
13+
bool TDMAManager::tx_allowed(std::chrono::steady_clock::time_point now){
14+
if(current_slot(now)!=cfg.my_slot){
15+
return false;
16+
}
17+
auto cycle=cfg.num_slots*cfg.slot;
18+
auto elapsed=now-cfg.t0;
19+
// how far are we in the cycle
20+
auto time_cycle=elapsed%cycle;
21+
uint8_t slot_i= time_cycle/cfg.slot;
22+
auto offset_in_slot=time_cycle%cfg.slot;
23+
24+
if(offset_in_slot<cfg.guard){
25+
return false;
26+
}
27+
if(offset_in_slot>(cfg.slot-cfg.guard)){
28+
return false;
29+
}
30+
return true;
31+
}

0 commit comments

Comments
 (0)