Commit 5e177398 authored by Valentin Platzgummer's avatar Valentin Platzgummer

test_rosbridge/rosbridgecpp rm

parent 9edcaeea
Subproject commit 2a47f11e73eef6d817af7934692b1223d7fb434f
Subproject commit 1eff57ee1e54fbed0b24617b1d4452d26d8b2221
Subproject commit 2a4865adc3808687d6c6f550f497a02eb920c382
Subproject commit 318562bac7cd55e6cfafd4694094343a70e4f322
[Dolphin]
Timestamp=2020,6,21,18,42,22
Version=4
[Settings]
HiddenFilesShown=true
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
cmake_minimum_required(VERSION 3.5)
project(rosbridge LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(rosbridge main.cpp snake.cpp)
set(HEADER_FILES main.h snake.h)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../rosbridgecpp)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../rosbridgecpp rosbridgecpp)
target_link_libraries(${PROJECT_NAME} rosbridgecpp)
/*
* Created on: Apr 16, 2018
* Author: Poom Pianpak
*/
#include "main.h"
#include "snake.h"
#include <future>
RosbridgeWsClient rbc("localhost:9090");
void advertiseServiceCallback(std::shared_ptr<WsClient::Connection> /*connection*/, std::shared_ptr<WsClient::InMessage> in_message)
{
// message->string() is destructive, so we have to buffer it first
std::string messagebuf = in_message->string();
std::cout << "advertiseServiceCallback(): Message Received: " << messagebuf << std::endl;
rapidjson::Document document;
if (document.Parse(messagebuf.c_str()).HasParseError())
{
std::cerr << "advertiseServiceCallback(): Error in parsing service request message: " << messagebuf << std::endl;
return;
}
rapidjson::Document values(rapidjson::kObjectType);
rapidjson::Document::AllocatorType& allocator = values.GetAllocator();
values.AddMember("success", document["args"]["data"].GetBool(), allocator);
values.AddMember("message", "from advertiseServiceCallback", allocator);
rbc.serviceResponse(document["service"].GetString(), document["id"].GetString(), true, values);
}
void callServiceCallback(std::shared_ptr<WsClient::Connection> connection, std::shared_ptr<WsClient::InMessage> in_message)
{
std::cout << "serviceResponseCallback(): Message Received: " << in_message->string() << std::endl;
connection->send_close(1000);
}
void publisherThread(RosbridgeWsClient& rbc, const std::future<void>& futureObj)
{
rbc.addClient("topic_publisher");
rosbridge_msgs::Header header(0, rosbridge_msgs::Time(1,2), "/map");
rosbridge_msgs::Polygon polygon(std::vector<rosbridge_msgs::Point32>({rosbridge_msgs::Point32(1,1,1),
rosbridge_msgs::Point32(2,2,2),
rosbridge_msgs::Point32(3,3,3)}));
rosbridge_msgs::Polygon polygon2(std::vector<rosbridge_msgs::Point32>({rosbridge_msgs::Point32(1,1,3),
rosbridge_msgs::Point32(2,2,4),
rosbridge_msgs::Point32(3,3,5)}));
rosbridge_msgs::PolygonStamped polygonStamped(header, polygon);
rosbridge_msgs::PolygonStamped polygonStamped2(header, polygon2);
std::vector<rosbridge_msgs::PolygonStamped> parray({polygonStamped, polygonStamped2});
std::vector<uint32_t> labels({1,2});
std::vector<_Float32> likelihood({1,2});
rosbridge_msgs::PolygonArray polygonArray(header, parray, labels, likelihood);
rapidjson::Document doc(rapidjson::kObjectType);
void(polygonArray.toJson(doc, doc.GetAllocator()));
// Write to stdout
rapidjson::OStreamWrapper out(std::cout);
// Write document...
rapidjson::Writer<rapidjson::OStreamWrapper> writer(out);
doc.Accept(writer);
std::cout << std::endl;
while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
rbc.publish("/polygon_array_topic", doc);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << "publisherThread stops()" << std::endl;
}
void subscriberCallback(std::shared_ptr<WsClient::Connection> /*connection*/, std::shared_ptr<WsClient::InMessage> in_message)
{
std::cout << "subscriberCallback(): Message Received: " << in_message->string() << std::endl;
}
int main() {
// rbc.addClient("service_advertiser");
// rbc.advertiseService("service_advertiser", "/zservice", "std_srvs/SetBool", advertiseServiceCallback);
rbc.addClient("topic_advertiser");
rbc.advertise("topic_advertiser", "/polygon_array_topic", "jsk_recognition_msgs/PolygonArray");
// rbc.addClient("topic_subscriber");
// rbc.subscribe("topic_subscriber", "/ztopic", subscriberCallback);
// Test calling a service
// rapidjson::Document document(rapidjson::kObjectType);
// document.AddMember("data", true, document.GetAllocator());
// rbc.callService("/zservice", callServiceCallback, document);
// Test creating and stopping a publisher
{
// Create a std::promise object
std::promise<void> exitSignal;
// Fetch std::future object associated with promise
std::future<void> futureObj = exitSignal.get_future();
// Starting Thread & move the future object in lambda function by reference
std::thread th(&publisherThread, std::ref(rbc), std::cref(futureObj));
// Wait for 10 sec
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << "Asking publisherThread to Stop" << std::endl;
// Set the value in promise
exitSignal.set_value();
// Wait for thread to join
th.join();
}
// Test removing clients
// rbc.removeClient("service_advertiser");
rbc.removeClient("topic_advertiser");
// rbc.removeClient("topic_subscriber");
std::cout << "Program terminated" << std::endl;
}
This diff is collapsed.
#include "snake.h"
namespace rosbridge_msgs {
//===================================================================
// Point32
//===================================================================
Point32::Point32(): x(0), y(0), z(0) {}
Point32::Point32(_Float32 x, _Float32 y, _Float32 z): x(x), y(y), z(z) {}
bool Point32::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
doc.AddMember("x", rapidjson::Value().SetFloat(this->x), allocator);
doc.AddMember("y", rapidjson::Value().SetFloat(this->y), allocator);
doc.AddMember("z", rapidjson::Value().SetFloat(this->z), allocator);
return true;
}
//===================================================================
// Time
//===================================================================
Time::Time(): secs(0), nsecs(0) {}
Time::Time(uint32_t secs, uint32_t nsecs): secs(secs), nsecs(nsecs) {}
bool Time::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
doc.AddMember("secs", rapidjson::Value().SetUint(this->secs), allocator);
doc.AddMember("nsecs", rapidjson::Value().SetUint(this->nsecs), allocator);
return true;
}
//===================================================================
// Header
//===================================================================
Header::Header(): seq(0), frame_id("") {}
Header::Header(uint32_t seq, const Time &stamp, std::string frame_id): seq(seq), stamp(stamp), frame_id(frame_id) {}
bool Header::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
doc.AddMember("seq", rapidjson::Value().SetUint(this->seq), allocator);
rapidjson::Document stamp(rapidjson::kObjectType);
if (!this->stamp.toJson(stamp, allocator))
return false;
doc.AddMember("stamp", stamp, allocator);
doc.AddMember("frame_id", rapidjson::Value().SetString(this->frame_id.data(), this->frame_id.length(), allocator), allocator);
return true;
}
//===================================================================
// Polygon
//===================================================================
Polygon::Polygon(){}
Polygon::Polygon(const std::vector<Point32> &points) : points(points) {}
bool Polygon::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
rapidjson::Value points(rapidjson::kArrayType);
for(std::vector<Point32>::iterator it = this->points.begin(); it != this->points.end(); ++it) {
rapidjson::Document point(rapidjson::kObjectType);
if (!it->toJson(point, allocator))
return false;
points.PushBack(point, allocator);
}
doc.AddMember("points", points, allocator);
return true;
}
//===================================================================
// PolygonStamped
//===================================================================
PolygonStamped::PolygonStamped() {}
PolygonStamped::PolygonStamped(const Header &header, const Polygon &polygon) : header(header), polygon(polygon){}
bool PolygonStamped::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
rapidjson::Document header(rapidjson::kObjectType);
if (!this->header.toJson(header, allocator))
return false;
rapidjson::Document polygon(rapidjson::kObjectType);
if (!this->polygon.toJson(polygon, allocator))
return false;
doc.AddMember("header", header, allocator);
doc.AddMember("polygon", polygon, allocator);
return true;
}
//===================================================================
// PolygonArray
//===================================================================
PolygonArray::PolygonArray() {}
PolygonArray::PolygonArray(const Header &header,
const std::vector<PolygonStamped> &polygons,
const std::vector<uint32_t> &labels,
const std::vector<_Float32> &likelihood)
: header(header), polygons(polygons), labels(labels), likelihood(likelihood) {}
bool PolygonArray::toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator)
{
rapidjson::Document header(rapidjson::kObjectType);
if (!this->header.toJson(header, allocator))
return false;
doc.AddMember("header", header, allocator);
rapidjson::Value polygons(rapidjson::kArrayType);
for(auto it = this->polygons.begin(); it != this->polygons.end(); ++it){
rapidjson::Document polygon(rapidjson::kObjectType);
if (!it->toJson(polygon, allocator))
return false;
polygons.PushBack(polygon, allocator);
}
doc.AddMember("polygons", polygons, allocator);
rapidjson::Value labels(rapidjson::kArrayType);
for(auto it = this->labels.begin(); it != this->labels.end(); ++it)
labels.PushBack(rapidjson::Value().SetUint(*it), allocator);
doc.AddMember("labels", labels, allocator);
rapidjson::Value likelihood(rapidjson::kArrayType);
for(auto it = this->likelihood.begin(); it != this->likelihood.end(); ++it)
likelihood.PushBack(rapidjson::Value().SetFloat(*it), allocator);
doc.AddMember("likelihood", likelihood, allocator);
return true;
}
}
#ifndef SNAKE_H
#define SNAKE_H
#include <vector>
#include "rapidjson/include/rapidjson/document.h"
#include "rapidjson/include/rapidjson/writer.h"
#include "rapidjson/include/rapidjson/stringbuffer.h"
#include "rapidjson/include/rapidjson/ostreamwrapper.h"
using namespace std;
// C++ implementation of ROS messages in json representation to communicate with rosbridge.
namespace rosbridge_msgs {
// C++ representation of ros::Time with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class Time{
public:
Time();
Time(uint32_t secs, uint32_t nsecs);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
uint32_t secs;
uint32_t nsecs;
};
// C++ representation of std_msgs/Header with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class Header{
public:
Header();
Header(uint32_t seq, const Time &stamp, std::string frame_id);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
uint32_t seq;
Time stamp;
std::string frame_id;
};
// C++ representation of geometry_msgs/Point32 with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class Point32{
public:
Point32();
Point32(_Float32 x, _Float32 y, _Float32 z);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
_Float32 x;
_Float32 y;
_Float32 z;
};
// C++ representation of geometry_msgs/Polygon with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class Polygon{
public:
Polygon();
Polygon(const std::vector<Point32> &points);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
std::vector<Point32> points;
};
// C++ representation of geometry_msgs/PolygonStamped with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class PolygonStamped{
public:
PolygonStamped();
PolygonStamped(const Header &header, const Polygon &polygon);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
Header header;
Polygon polygon;
};
// C++ representation of jsk_recognition_msgs/PolygonArray with fromJson and toJson functions for rosbridge.
// fromJson not yet implemented.
class PolygonArray{
public:
PolygonArray();
PolygonArray(const Header &header, const std::vector<PolygonStamped> &polygons, const std::vector<uint32_t> &labels, const std::vector<_Float32> &likelihood);
bool toJson(rapidjson::Document &doc, rapidjson::Document::AllocatorType &allocator);
Header header;
std::vector<PolygonStamped> polygons;
std::vector<uint32_t> labels;
std::vector<_Float32> likelihood;
};
}
#endif // SNAKE_H
rosbridgecpp @ cb87f696
Subproject commit cb87f6966a1debe4af70a38ae56f988d99645d3b
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
/*
* Created on: Apr 16, 2018
* Author: Poom Pianpak
*/
#include "rosbridge_ws_client.hpp"
#include <future>
RosbridgeWsClient rbc("localhost:9090");
void advertiseServiceCallback(std::shared_ptr<WsClient::Connection> /*connection*/, std::shared_ptr<WsClient::InMessage> in_message)
{
// message->string() is destructive, so we have to buffer it first
std::string messagebuf = in_message->string();
std::cout << "advertiseServiceCallback(): Message Received: " << messagebuf << std::endl;
rapidjson::Document document;
if (document.Parse(messagebuf.c_str()).HasParseError())
{
std::cerr << "advertiseServiceCallback(): Error in parsing service request message: " << messagebuf << std::endl;
return;
}
rapidjson::Document values(rapidjson::kObjectType);
rapidjson::Document::AllocatorType& allocator = values.GetAllocator();
values.AddMember("success", document["args"]["data"].GetBool(), allocator);
values.AddMember("message", "from advertiseServiceCallback", allocator);
rbc.serviceResponse(document["service"].GetString(), document["id"].GetString(), true, values);
}
void callServiceCallback(std::shared_ptr<WsClient::Connection> connection, std::shared_ptr<WsClient::InMessage> in_message)
{
std::cout << "serviceResponseCallback(): Message Received: " << in_message->string() << std::endl;
connection->send_close(1000);
}
void publisherThread(RosbridgeWsClient& rbc, const std::future<void>& futureObj)
{
rbc.addClient("topic_publisher");
rapidjson::Document d;
d.SetObject();
d.AddMember("data", "Test message from /ztopic", d.GetAllocator());
while (futureObj.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
{
rbc.publish("/ztopic", d);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << "publisherThread stops()" << std::endl;
}
void subscriberCallback(std::shared_ptr<WsClient::Connection> /*connection*/, std::shared_ptr<WsClient::InMessage> in_message)
{
std::cout << "subscriberCallback(): Message Received: " << in_message->string() << std::endl;
}
int main() {
rbc.addClient("service_advertiser");
rbc.advertiseService("service_advertiser", "/zservice", "std_srvs/SetBool", advertiseServiceCallback);
rbc.addClient("topic_advertiser");
rbc.advertise("topic_advertiser", "/ztopic", "std_msgs/String");
rbc.addClient("topic_subscriber");
rbc.subscribe("topic_subscriber", "/ztopic", subscriberCallback);
// Test calling a service
rapidjson::Document document(rapidjson::kObjectType);
document.AddMember("data", true, document.GetAllocator());
rbc.callService("/zservice", callServiceCallback, document);
// Test creating and stopping a publisher
{
// Create a std::promise object
std::promise<void> exitSignal;
// Fetch std::future object associated with promise
std::future<void> futureObj = exitSignal.get_future();
// Starting Thread & move the future object in lambda function by reference
std::thread th(&publisherThread, std::ref(rbc), std::cref(futureObj));
// Wait for 10 sec
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << "Asking publisherThread to Stop" << std::endl;
// Set the value in promise
exitSignal.set_value();
// Wait for thread to join
th.join();
}
// Test removing clients
rbc.removeClient("service_advertiser");
rbc.removeClient("topic_advertiser");
rbc.removeClient("topic_subscriber");
std::cout << "Program terminated" << std::endl;
}
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
HEADERS += \
../rosbridgecpp/main.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment