-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cab0142
commit 0c1becc
Showing
11 changed files
with
230 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 对应 example 中的内容 | ||
set(CGRAPH_EXAMPLE_LIST | ||
E01-AutoPilot | ||
) | ||
|
||
foreach(example ${CGRAPH_EXAMPLE_LIST}) | ||
add_executable(${example} | ||
$<TARGET_OBJECTS:CGraph> | ||
${example}.cpp | ||
) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@File: E01-AutoPilot.cpp | ||
@Time: 2023/9/15 20:03 | ||
@Desc: 本example主要展示,Camera 每隔1000ms,采集一张图片 | ||
LaneDetector 和 CarDetector同时消费这张图片,计算对应的内容 | ||
并且最终在Show中展示对应的结果信息 | ||
***************************/ | ||
|
||
#include <iostream> | ||
#include <algorithm> | ||
|
||
#include "CGraph.h" | ||
|
||
using namespace CGraph; | ||
|
||
static const int DEFAULT_IMAGE_SIZE = 64; | ||
static const int DEFAULT_MESSAGE_BUF_SIZE = 16; | ||
static const char* EXAMPLE_IMAGE_TOPIC = "/example/image/topic"; | ||
static const char* EXAMPLE_PARAM_KEY = "example-param-key"; | ||
|
||
|
||
struct ImageMParam : public GMessageParam { | ||
int frame_id_ = 0; | ||
char image_buf_[DEFAULT_IMAGE_SIZE] = {0}; | ||
|
||
ImageMParam& operator=(const ImageMParam& param) { | ||
if (this == ¶m) { | ||
return *this; | ||
} | ||
|
||
this->frame_id_ = param.frame_id_; | ||
memset(image_buf_, 0, DEFAULT_IMAGE_SIZE); | ||
memcpy(image_buf_, param.image_buf_, DEFAULT_IMAGE_SIZE); | ||
return *this; | ||
} | ||
}; | ||
|
||
|
||
class CameraGDaemon : public GDaemon { | ||
public: | ||
CVoid daemonTask(GDaemonParamPtr param) override { | ||
ImageMParam image; | ||
image.frame_id_ = cur_index_; | ||
std::string info = "this is " + std::to_string(cur_index_) + " image"; | ||
memcpy(image.image_buf_, info.c_str(), info.length()); | ||
cur_index_++; | ||
|
||
CGRAPH_PUB_MPARAM(ImageMParam, EXAMPLE_IMAGE_TOPIC, image); | ||
} | ||
|
||
private: | ||
int cur_index_ = 0; | ||
}; | ||
|
||
|
||
struct DetectResultGParam : public GParam { | ||
int lane_num_ = 0; | ||
int car_num_ = 0; | ||
int frame_id_ = 0; | ||
|
||
CStatus setup() override { | ||
lane_num_ = 0; | ||
car_num_ = 0; | ||
frame_id_ = 0; | ||
return CStatus(); | ||
} | ||
}; | ||
|
||
|
||
class LaneDetectorGNode : public GNode { | ||
public: | ||
CStatus init() override { | ||
// 订阅 EXAMPLE_IMAGE_TOPIC 消息。每次 bind的返回值,是不一样的,用于区分 | ||
conn_id_ = CGRAPH_BIND_MESSAGE_TOPIC(ImageMParam, EXAMPLE_IMAGE_TOPIC, DEFAULT_MESSAGE_BUF_SIZE) | ||
return CStatus(); | ||
} | ||
|
||
CStatus run() override { | ||
ImageMParam image; | ||
auto status = CGRAPH_SUB_MPARAM(ImageMParam, conn_id_, image); | ||
if (status.isErr()) { | ||
return status; | ||
} | ||
|
||
auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY) | ||
// detector lane in image.image_buf_ ... | ||
CGRAPH_ECHO("detecting lane in frame [%d], info is [%s]", image.frame_id_, image.image_buf_); | ||
param->frame_id_ = image.frame_id_; | ||
param->lane_num_ = std::abs((int)std::random_device{}()) % 10; | ||
return CStatus(); | ||
} | ||
|
||
private: | ||
int conn_id_ = 0; | ||
}; | ||
|
||
|
||
class CarDetectorGNode : public GNode { | ||
public: | ||
CStatus init() override { | ||
conn_id_ = CGRAPH_BIND_MESSAGE_TOPIC(ImageMParam, EXAMPLE_IMAGE_TOPIC, DEFAULT_MESSAGE_BUF_SIZE) | ||
return CStatus(); | ||
} | ||
|
||
CStatus run() override { | ||
ImageMParam image; | ||
auto status = CGRAPH_SUB_MPARAM(ImageMParam, conn_id_, image); | ||
if (status.isErr()) { | ||
return status; | ||
} | ||
|
||
auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY); | ||
CGRAPH_ECHO("finding car in frame [%d], info is [%s]", image.frame_id_, image.image_buf_); | ||
// find car in image.image_buf_ ... | ||
param->car_num_ = std::abs((int)std::random_device{}()) % 5; | ||
return CStatus(); | ||
} | ||
|
||
private: | ||
int conn_id_ = 0; | ||
}; | ||
|
||
|
||
class ShowGNode : public GNode { | ||
CStatus run() override { | ||
auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY); | ||
CGRAPH_ECHO("find [%d] car and [%d] lane in frame [%d] \n", param->car_num_, param->lane_num_, param->frame_id_); | ||
return CStatus(); | ||
} | ||
}; | ||
|
||
|
||
void example_auto_pilot() { | ||
GElementPtr lane, car, show = nullptr; | ||
auto pipeline = GPipelineFactory::create(); | ||
|
||
pipeline->registerGElement<LaneDetectorGNode>(&lane, {}, "lane"); | ||
pipeline->registerGElement<CarDetectorGNode>(&car, {}, "car"); | ||
pipeline->registerGElement<ShowGNode>(&show, {lane, car}, "show"); | ||
|
||
pipeline->createGParam<DetectResultGParam>(EXAMPLE_PARAM_KEY); | ||
pipeline->addGDaemon<CameraGDaemon>(1000); // 模拟相机,每间隔1000ms,生成一张图片 | ||
|
||
pipeline->process(10); | ||
GPipelineFactory::clear(); | ||
CGRAPH_CLEAR_MESSAGES() | ||
} | ||
|
||
|
||
int main() { | ||
example_auto_pilot(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
# 对应tutorial中的内容 | ||
set(CGRAPH_TUTORIAL_LIST | ||
T00-HelloCGraph | ||
T01-Simple | ||
T02-Cluster | ||
T03-Region | ||
T04-Complex | ||
T05-Param | ||
T06-Condition | ||
T07-MultiPipeline | ||
T08-Template | ||
T09-Aspect | ||
T10-AspectParam | ||
T11-Singleton | ||
T12-Function | ||
T13-Daemon | ||
T14-Hold | ||
T15-ElementParam | ||
T16-MessageSendRecv | ||
T17-MessagePubSub | ||
T18-Event | ||
T19-Cancel | ||
T20-YieldResume | ||
T21-MultiCondition | ||
T22-Timeout | ||
T23-Some | ||
T24-Fence | ||
T25-Coordinator | ||
) | ||
|
||
|
||
foreach(tut ${CGRAPH_TUTORIAL_LIST}) | ||
add_executable(${tut} | ||
# 在自己的工程中引入CGraph功能,仅需引入 CGraph-env-include.cmake 后,加入这一句话即可 | ||
$<TARGET_OBJECTS:CGraph> | ||
${tut}.cpp | ||
) | ||
endforeach() |