ROS C++ client library for emscripten WASM
Under the hood, roswasm uses both native ROS binary serialization and json serialization
to provide an interface that looks like native roscpp when communcating through
rosbridge_websocket
(see details below).
roswasm supports the following primitives, mirroring the ones in roscpp. For complete example nodes, see roswasm_tutorials.
You can create several NodeHandles. They can not be configured with a namespace.
To achieve this, namespace the rosbridge_websocket
instance instead.
#include <roswasm/roswasm.h>
...
roswasm::NodeHandle* nh = new roswasm::NodeHandle();
Subscribers receive data through the new cbor-raw
compression protocol. In practice, this means rosbridge_websocket
transmits the binary
serialized ROS messages, making overhead minimal.
void string_callback(const std_msgs::String& msg) {}
...
roswasm::Subscriber* sub = nh->subscribe<std_msgs::String>("test", callback);
To throttle the rate of messages to at most 1 message every 100 ms (10Hz), change the call to
roswasm::Subscriber* sub = nh->subscribe<std_msgs::String>("test", callback, 100);
Publishers use the json
compression protocol, since it is expected that the
bandwidth of publishers will be low.
roswasm::Publisher* pub = nh->advertise<std_msgs::String>("test");
std_msgs::String msg;
msg.data = "TEST";
pub->publish(msg);
Similarly, ServiceClients also send and receive data using json serialization.
roswasm::ServiceClient* service = nh->serviceClient<rosapi::TopicType>("/rosapi/topic_type", service_callback);
rosapi::TopicType::Request req;
req.topic = "/connected_clients";
service->call<rosapi::TopicType>(req);
Timers adhere to the roscpp timer callback interface. However, the TimerEvent
will
not contain any meaningful data.
void callback(const ros::TimerEvent& ev) {}
...
roswasm::Timer* timer = nh->createTimer(5., callback);
Any nodes that are to use roswasm and compile using Emscripten
just need to depend on roswasm
in the catkin build configuration.
This will automatically pull in the build configuration by
importing a CFG_EXTRAS
Cmake file.
Note that some configurations still are necessary and that the
produced Webassembly and JavaScript files need to be imported
by an html file in order to run in the web browser.
See the tutorials package
for examples on the necessary configurations.
Before starting any of the roswasm nodes, you need to have one instance of rosbridge_websocket
running:
roslaunch rosbridge_server rosbridge_websocket.launch
After building your node, you can run it similar to the following command:
rosrun roswasm run.py _pkg:=roswasm_tutorials _node:=listener.html _display_port:=8080
This will start a webserver and allow you to view the page at localhost:8080
.
If you want to see the output from the node, open the browser debug console.