Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug : buffer don't have enough space #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 58 additions & 29 deletions src/PandarGeneralRaw/include/pandarGeneral/pandarGeneral_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

#include <list>
#include <string>
#include <mutex>

#include <boost/function.hpp>
#include <condition_variable>

#include "pandarGeneral/point_types.h"
#include "input.h"
Expand Down Expand Up @@ -149,6 +151,8 @@ HS_LIDAR_L64_7_BLOCK_PACKET_BODY_SIZE + HS_LIDAR_L64_PACKET_TAIL_WITHOUT_UDPSEQ_
#define HS_LIDAR_QT_COORDINATE_CORRECTION_D0 (20)
#define COORDINATE_CORRECTION_CHECK (false)

#define PKT_ARRAY_SIZE 36000

struct Pandar40PUnit_s {
uint8_t intensity;
double distance;
Expand Down Expand Up @@ -335,49 +339,74 @@ typedef struct {
std::vector<HS_Object3D_Object> data; //!< Object data array
} HS_Object3D_Object_List;

typedef std::array<PandarPacket, 36000> PktArray;
typedef std::array<PandarPacket, PKT_ARRAY_SIZE> PktArray;

typedef struct PacketsBuffer_s {
private:
std::mutex mutex;
PktArray m_buffers{};
PktArray::iterator m_iterPush;
PktArray::iterator m_iterCalc;
bool m_startFlag;
PktArray::iterator m_iterPop;
bool m_full;
std::condition_variable buffer_not_full;
std::condition_variable buffer_not_empty;
public:
inline PacketsBuffer_s() {
m_iterPush = m_buffers.begin();
m_iterCalc = m_buffers.begin();
m_startFlag = false;
m_iterPop = m_buffers.begin();
m_full = false;
}
inline int push_back(PandarPacket pkt) {
if (!m_startFlag) {
*m_iterPush = pkt;
m_startFlag = true;
return 1;
}
m_iterPush++;

if (m_iterPush == m_iterCalc) {
printf("buffer don't have space!,%d\n", m_iterPush - m_buffers.begin());
return 0;
inline int push_back(const PandarPacket& pkt) {
std::unique_lock<std::mutex> lock(mutex);

// if (m_full) {
// printf("Pandar: buffer doesn't have space!\n");
// return 0;
// }

while(m_full)
{
buffer_not_full.wait(lock);
}

if (m_buffers.end() == m_iterPush) {
*m_iterPush = pkt;
++m_iterPush;
if (m_iterPush == m_buffers.end()) {
m_iterPush = m_buffers.begin();
*m_iterPush = pkt;
}
*m_iterPush = pkt;

if (m_iterPush == m_iterPop) {
m_full = true;
}

buffer_not_empty.notify_all();

return 1;

}
inline bool hasEnoughPackets() {
return ((m_iterPush - m_iterCalc > 0 ) ||
((m_iterPush - m_iterCalc + 36000 > 0 ) && (m_buffers.end() - m_iterCalc < 1000) && (m_iterPush - m_buffers.begin() < 1000)));

}
inline PktArray::iterator getIterCalc() { return m_iterCalc;}
inline void moveIterCalc() {
m_iterCalc++;
if (m_buffers.end() == m_iterCalc) {
m_iterCalc = m_buffers.begin();

inline bool pop(PandarPacket& packet) {
std::unique_lock<std::mutex> lock(mutex);

// if (!m_full && m_iterPush == m_iterPop) {
// return false;
// }

while(!m_full && m_iterPush == m_iterPop)
{
buffer_not_empty.wait(lock);
}

packet = *m_iterPop;

m_iterPop++;
if (m_buffers.end() == m_iterPop) {
m_iterPop = m_buffers.begin();
}
m_full = false;
buffer_not_full.notify_all();

return true;
}
} PacketsBuffer;

Expand Down
6 changes: 3 additions & 3 deletions src/PandarGeneralRaw/src/pandarGeneral_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,12 @@ void PandarGeneral_Internal::ProcessLiarPacket() {

while (enable_lidar_process_thr_) {
boost::this_thread::interruption_point();
if (!m_PacketsBuffer.hasEnoughPackets()) {
PandarPacket packet;
if (!m_PacketsBuffer.pop(packet))
{
usleep(1000);
continue;
}
PandarPacket packet = *(m_PacketsBuffer.getIterCalc());
m_PacketsBuffer.moveIterCalc();
m_dPktTimestamp = packet.stamp;

if (packet.size == PACKET_SIZE || packet.size == PACKET_SIZE + SEQ_NUM_SIZE) {
Expand Down