-
Notifications
You must be signed in to change notification settings - Fork 18
/
PacketBundler.cpp
99 lines (80 loc) · 2.13 KB
/
PacketBundler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Velodyne HDL Packet Bundler
// Nick Rypkema (rypkema@mit.edu), MIT 2017
// shared library to bundle velodyne packets into enough for a single frame
#include <cmath>
#include <stdint.h>
#include <iostream>
#include "PacketBundler.h"
PacketBundler::PacketBundler()
{
_max_num_of_bundles = 10;
UnloadData();
}
PacketBundler::~PacketBundler()
{
}
void PacketBundler::SetMaxNumberOfBundles(unsigned int max_num_of_bundles)
{
if (max_num_of_bundles <= 0) {
return;
} else {
_max_num_of_bundles = max_num_of_bundles;
}
while (_bundles.size() >= _max_num_of_bundles) {
_bundles.pop_front();
}
}
void PacketBundler::BundlePacket(std::string* data, unsigned int* data_length)
{
const unsigned char* data_char = reinterpret_cast<const unsigned char*>(data->c_str());
BundleHDLPacket(const_cast<unsigned char*>(data_char), *data_length);
}
void PacketBundler::BundleHDLPacket(unsigned char *data, unsigned int data_length)
{
if (data_length != 1206) {
std::cout << "PacketBundler: Warning, data packet is not 1206 bytes" << std::endl;
return;
}
HDLDataPacket* dataPacket = reinterpret_cast<HDLDataPacket *>(data);
for (int i = 0; i < HDL_FIRING_PER_PKT; ++i) {
HDLFiringData firingData = dataPacket->firingData[i];
if (firingData.rotationalPosition < _last_azimuth) {
SplitBundle();
}
_last_azimuth = firingData.rotationalPosition;
}
_bundle->append(reinterpret_cast<const char*>(data), (size_t)data_length);
}
void PacketBundler::SplitBundle()
{
if (_bundles.size() == _max_num_of_bundles-1) {
_bundles.pop_front();
}
_bundles.push_back(*_bundle);
delete _bundle;
_bundle = new std::string();
}
void PacketBundler::UnloadData()
{
_last_azimuth = 0;
_bundle = new std::string();
_bundles.clear();
}
std::deque<std::string> PacketBundler::GetBundles()
{
return _bundles;
}
void PacketBundler::ClearBundles()
{
_bundles.clear();
}
bool PacketBundler::GetLatestBundle(std::string* bundle, unsigned int* bundle_length)
{
if (_bundles.size()) {
*bundle = _bundles.back();
*bundle_length = bundle->size();
_bundles.clear();
return(true);
}
return(false);
}