Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
5e177398
Commit
5e177398
authored
Nov 10, 2020
by
Valentin Platzgummer
Browse files
test_rosbridge/rosbridgecpp rm
parent
9edcaeea
Changes
13
Hide whitespace changes
Inline
Side-by-side
v2.0
@
1eff57ee
Compare
2a47f11e
...
1eff57ee
Subproject commit
2a47f11e73eef6d817af7934692b1223d7fb434f
Subproject commit
1eff57ee1e54fbed0b24617b1d4452d26d8b2221
Drivers
@
318562ba
Compare
2a4865ad
...
318562ba
Subproject commit
2a4865adc3808687d6c6f550f497a02eb920c38
2
Subproject commit
318562bac7cd55e6cfafd4694094343a70e4f32
2
test_rosbridge/.directory
deleted
100644 → 0
View file @
9edcaeea
[Dolphin]
Timestamp=2020,6,21,18,42,22
Version=4
[Settings]
HiddenFilesShown=true
test_rosbridge/rosbridge/.gitignore
deleted
100644 → 0
View file @
9edcaeea
# 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
test_rosbridge/rosbridge/CMakeLists.txt
deleted
100644 → 0
View file @
9edcaeea
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
)
test_rosbridge/rosbridge/main.cpp
deleted
100644 → 0
View file @
9edcaeea
/*
* 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
;
}
test_rosbridge/rosbridge/main.h
deleted
100644 → 0
View file @
9edcaeea
#ifndef MAIN_H
#define MAIN_H
/*
* Created on: Apr 16, 2018
* Author: Poom Pianpak
*/
#include
"Simple-WebSocket-Server/client_ws.hpp"
#include
"rapidjson/include/rapidjson/document.h"
#include
"rapidjson/include/rapidjson/writer.h"
#include
"rapidjson/include/rapidjson/stringbuffer.h"
#include
"rapidjson/include/rapidjson/ostreamwrapper.h"
#include
<chrono>
#include
<functional>
#include
<thread>
using
WsClient
=
SimpleWeb
::
SocketClient
<
SimpleWeb
::
WS
>
;
using
InMessage
=
std
::
function
<
void
(
std
::
shared_ptr
<
WsClient
::
Connection
>
,
std
::
shared_ptr
<
WsClient
::
InMessage
>
)
>
;
class
RosbridgeWsClient
{
std
::
string
server_port_path
;
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>
client_map
;
void
start
(
const
std
::
string
&
client_name
,
std
::
shared_ptr
<
WsClient
>
client
,
const
std
::
string
&
message
)
{
if
(
!
client
->
on_open
)
{
#ifdef DEBUG
client
->
on_open
=
[
client_name
,
message
](
std
::
shared_ptr
<
WsClient
::
Connection
>
connection
)
{
#else
client
->
on_open
=
[
message
](
std
::
shared_ptr
<
WsClient
::
Connection
>
connection
)
{
#endif
#ifdef DEBUG
std
::
cout
<<
client_name
<<
": Opened connection"
<<
std
::
endl
;
std
::
cout
<<
client_name
<<
": Sending message: "
<<
message
<<
std
::
endl
;
#endif
connection
->
send
(
message
);
};
}
#ifdef DEBUG
if
(
!
client
->
on_message
)
{
client
->
on_message
=
[
client_name
](
std
::
shared_ptr
<
WsClient
::
Connection
>
/*connection*/
,
std
::
shared_ptr
<
WsClient
::
InMessage
>
in_message
)
{
std
::
cout
<<
client_name
<<
": Message received: "
<<
in_message
->
string
()
<<
std
::
endl
;
};
}
if
(
!
client
->
on_close
)
{
client
->
on_close
=
[
client_name
](
std
::
shared_ptr
<
WsClient
::
Connection
>
/*connection*/
,
int
status
,
const
std
::
string
&
/*reason*/
)
{
std
::
cout
<<
client_name
<<
": Closed connection with status code "
<<
status
<<
std
::
endl
;
};
}
if
(
!
client
->
on_error
)
{
// See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
client
->
on_error
=
[
client_name
](
std
::
shared_ptr
<
WsClient
::
Connection
>
/*connection*/
,
const
SimpleWeb
::
error_code
&
ec
)
{
std
::
cout
<<
client_name
<<
": Error: "
<<
ec
<<
", error message: "
<<
ec
.
message
()
<<
std
::
endl
;
};
}
#endif
#ifdef DEBUG
std
::
thread
client_thread
([
client_name
,
client
]()
{
#else
std
::
thread
client_thread
([
client
]()
{
#endif
client
->
start
();
#ifdef DEBUG
std
::
cout
<<
client_name
<<
": Terminated"
<<
std
::
endl
;
#endif
client
->
on_open
=
NULL
;
client
->
on_message
=
NULL
;
client
->
on_close
=
NULL
;
client
->
on_error
=
NULL
;
});
client_thread
.
detach
();
// This is to make sure that the thread got fully launched before we do anything to it (e.g. remove)
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
}
public:
RosbridgeWsClient
(
const
std
::
string
&
server_port_path
)
{
this
->
server_port_path
=
server_port_path
;
}
~
RosbridgeWsClient
()
{
for
(
auto
&
client
:
client_map
)
{
client
.
second
->
stop
();
client
.
second
.
reset
();
}
}
void
addClient
(
const
std
::
string
&
client_name
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
==
client_map
.
end
())
{
client_map
[
client_name
]
=
std
::
make_shared
<
WsClient
>
(
server_port_path
);
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
" has already been created"
<<
std
::
endl
;
}
#endif
}
std
::
shared_ptr
<
WsClient
>
getClient
(
const
std
::
string
&
client_name
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
return
it
->
second
;
}
return
NULL
;
}
void
stopClient
(
const
std
::
string
&
client_name
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
it
->
second
->
stop
();
it
->
second
->
on_open
=
NULL
;
it
->
second
->
on_message
=
NULL
;
it
->
second
->
on_close
=
NULL
;
it
->
second
->
on_error
=
NULL
;
#ifdef DEBUG
std
::
cout
<<
client_name
<<
" has been stopped"
<<
std
::
endl
;
#endif
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
" has not been created"
<<
std
::
endl
;
}
#endif
}
void
removeClient
(
const
std
::
string
&
client_name
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
it
->
second
->
stop
();
it
->
second
.
reset
();
client_map
.
erase
(
it
);
#ifdef DEBUG
std
::
cout
<<
client_name
<<
" has been removed"
<<
std
::
endl
;
#endif
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
" has not been created"
<<
std
::
endl
;
}
#endif
}
void
advertise
(
const
std
::
string
&
client_name
,
const
std
::
string
&
topic
,
const
std
::
string
&
type
,
const
std
::
string
&
id
=
""
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
std
::
string
message
=
"
\"
op
\"
:
\"
advertise
\"
,
\"
topic
\"
:
\"
"
+
topic
+
"
\"
,
\"
type
\"
:
\"
"
+
type
+
"
\"
"
;
if
(
id
.
compare
(
""
)
!=
0
)
{
message
+=
",
\"
id
\"
:
\"
"
+
id
+
"
\"
"
;
}
message
=
"{"
+
message
+
"}"
;
start
(
client_name
,
it
->
second
,
message
);
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
"has not been created"
<<
std
::
endl
;
}
#endif
}
void
publish
(
const
std
::
string
&
topic
,
const
rapidjson
::
Document
&
msg
,
const
std
::
string
&
id
=
""
)
{
rapidjson
::
StringBuffer
strbuf
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
strbuf
);
msg
.
Accept
(
writer
);
std
::
string
message
=
"
\"
op
\"
:
\"
publish
\"
,
\"
topic
\"
:
\"
"
+
topic
+
"
\"
,
\"
msg
\"
:"
+
strbuf
.
GetString
();
if
(
id
.
compare
(
""
)
!=
0
)
{
message
+=
",
\"
id
\"
:
\"
"
+
id
+
"
\"
"
;
}
message
=
"{"
+
message
+
"}"
;
std
::
shared_ptr
<
WsClient
>
publish_client
=
std
::
make_shared
<
WsClient
>
(
server_port_path
);
publish_client
->
on_open
=
[
message
](
std
::
shared_ptr
<
WsClient
::
Connection
>
connection
)
{
#ifdef DEBUG
std
::
cout
<<
"publish_client: Opened connection"
<<
std
::
endl
;
std
::
cout
<<
"publish_client: Sending message: "
<<
message
<<
std
::
endl
;
#endif
connection
->
send
(
message
);
// TODO: This could be improved by creating a thread to keep publishing the message instead of closing it right away
connection
->
send_close
(
1000
);
};
start
(
"publish_client"
,
publish_client
,
message
);
}
void
subscribe
(
const
std
::
string
&
client_name
,
const
std
::
string
&
topic
,
const
InMessage
&
callback
,
const
std
::
string
&
id
=
""
,
const
std
::
string
&
type
=
""
,
int
throttle_rate
=
-
1
,
int
queue_length
=
-
1
,
int
fragment_size
=
-
1
,
const
std
::
string
&
compression
=
""
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
std
::
string
message
=
"
\"
op
\"
:
\"
subscribe
\"
,
\"
topic
\"
:
\"
"
+
topic
+
"
\"
"
;
if
(
id
.
compare
(
""
)
!=
0
)
{
message
+=
",
\"
id
\"
:
\"
"
+
id
+
"
\"
"
;
}
if
(
type
.
compare
(
""
)
!=
0
)
{
message
+=
",
\"
type
\"
:
\"
"
+
type
+
"
\"
"
;
}
if
(
throttle_rate
>
-
1
)
{
message
+=
",
\"
throttle_rate
\"
:"
+
std
::
to_string
(
throttle_rate
);
}
if
(
queue_length
>
-
1
)
{
message
+=
",
\"
queue_length
\"
:"
+
std
::
to_string
(
queue_length
);
}
if
(
fragment_size
>
-
1
)
{
message
+=
",
\"
fragment_size
\"
:"
+
std
::
to_string
(
fragment_size
);
}
if
(
compression
.
compare
(
"none"
)
==
0
||
compression
.
compare
(
"png"
)
==
0
)
{
message
+=
",
\"
compression
\"
:
\"
"
+
compression
+
"
\"
"
;
}
message
=
"{"
+
message
+
"}"
;
it
->
second
->
on_message
=
callback
;
start
(
client_name
,
it
->
second
,
message
);
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
"has not been created"
<<
std
::
endl
;
}
#endif
}
void
advertiseService
(
const
std
::
string
&
client_name
,
const
std
::
string
&
service
,
const
std
::
string
&
type
,
const
InMessage
&
callback
)
{
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
WsClient
>>::
iterator
it
=
client_map
.
find
(
client_name
);
if
(
it
!=
client_map
.
end
())
{
std
::
string
message
=
"{
\"
op
\"
:
\"
advertise_service
\"
,
\"
service
\"
:
\"
"
+
service
+
"
\"
,
\"
type
\"
:
\"
"
+
type
+
"
\"
}"
;
it
->
second
->
on_message
=
callback
;
start
(
client_name
,
it
->
second
,
message
);
}
#ifdef DEBUG
else
{
std
::
cerr
<<
client_name
<<
"has not been created"
<<
std
::
endl
;
}
#endif
}
void
serviceResponse
(
const
std
::
string
&
service
,
const
std
::
string
&
id
,
bool
result
,
const
rapidjson
::
Document
&
values
=
{})
{
std
::
string
message
=
"
\"
op
\"
:
\"
service_response
\"
,
\"
service
\"
:
\"
"
+
service
+
"
\"
,
\"
result
\"
:"
+
((
result
)
?
"true"
:
"false"
);
// Rosbridge somehow does not allow service_response to be sent without id and values
// , so we cannot omit them even though the documentation says they are optional.
message
+=
",
\"
id
\"
:
\"
"
+
id
+
"
\"
"
;
// Convert JSON document to string
rapidjson
::
StringBuffer
strbuf
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
strbuf
);
values
.
Accept
(
writer
);
message
+=
",
\"
values
\"
:"
+
std
::
string
(
strbuf
.
GetString
());
message
=
"{"
+
message
+
"}"
;
std
::
shared_ptr
<
WsClient
>
service_response_client
=
std
::
make_shared
<
WsClient
>
(
server_port_path
);
service_response_client
->
on_open
=
[
message
](
std
::
shared_ptr
<
WsClient
::
Connection
>
connection
)
{
#ifdef DEBUG
std
::
cout
<<
"service_response_client: Opened connection"
<<
std
::
endl
;
std
::
cout
<<
"service_response_client: Sending message: "
<<
message
<<
std
::
endl
;
#endif
connection
->
send
(
message
);
connection
->
send_close
(
1000
);
};
start
(
"service_response_client"
,
service_response_client
,
message
);
}
void
callService
(
const
std
::
string
&
service
,
const
InMessage
&
callback
,
const
rapidjson
::
Document
&
args
=
{},
const
std
::
string
&
id
=
""
,
int
fragment_size
=
-
1
,
const
std
::
string
&
compression
=
""
)
{
std
::
string
message
=
"
\"
op
\"
:
\"
call_service
\"
,
\"
service
\"
:
\"
"
+
service
+
"
\"
"
;
if
(
!
args
.
IsNull
())
{
rapidjson
::
StringBuffer
strbuf
;
rapidjson
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
strbuf
);
args
.
Accept
(
writer
);
message
+=
",
\"
args
\"
:"
+
std
::
string
(
strbuf
.
GetString
());
}
if
(
id
.
compare
(
""
)
!=
0
)
{
message
+=
",
\"
id
\"
:
\"
"
+
id
+
"
\"
"
;
}
if
(
fragment_size
>
-
1
)
{
message
+=
",
\"
fragment_size
\"
:"
+
std
::
to_string
(
fragment_size
);
}
if
(
compression
.
compare
(
"none"
)
==
0
||
compression
.
compare
(
"png"
)
==
0
)
{
message
+=
",
\"
compression
\"
:
\"
"
+
compression
+
"
\"
"
;
}
message
=
"{"
+
message
+
"}"
;
std
::
shared_ptr
<
WsClient
>
call_service_client
=
std
::
make_shared
<
WsClient
>
(
server_port_path
);
if
(
callback
)
{
call_service_client
->
on_message
=
callback
;
}
else
{
call_service_client
->
on_message
=
[](
std
::
shared_ptr
<
WsClient
::
Connection
>
connection
,
std
::
shared_ptr
<
WsClient
::
InMessage
>
in_message
)
{
#ifdef DEBUG
std
::
cout
<<
"call_service_client: Message received: "
<<
in_message
->
string
()
<<
std
::
endl
;
std
::
cout
<<
"call_service_client: Sending close connection"
<<
std
::
endl
;
#endif
connection
->
send_close
(
1000
);
};
}
start
(
"call_service_client"
,
call_service_client
,
message
);
}
};
#endif // MAIN_H
test_rosbridge/rosbridge/snake.cpp
deleted
100644 → 0
View file @
9edcaeea
#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
;
}
}
test_rosbridge/rosbridge/snake.h
deleted
100644 → 0
View file @
9edcaeea
#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
test_rosbridge/test_rosbridge/.gitignore
deleted
100644 → 0
View file @
9edcaeea
# 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
test_rosbridge/test_rosbridge/main.cpp
deleted
100644 → 0
View file @
9edcaeea
/*
* 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
;
}
test_rosbridge/test_rosbridge/test_rosbridge.pro
deleted
100644 → 0
View file @
9edcaeea
TEMPLATE
=
app
CONFIG
+=
console
c
++
11
CONFIG
-=
app_bundle
CONFIG
-=
qt
SOURCES
+=
\
main
.
cpp
HEADERS
+=
\
..
/
rosbridgecpp
/
main
.
h
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment