ROSBridge.cpp 1.75 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3
#include "ros_bridge/include/ROSBridge.h"

ROSBridge::ROSBridge::ROSBridge() :
4
    _stopped(std::make_shared<std::atomic_bool>(true))
5
  ,  _casePacker(&_typeFactory, &_jsonFactory)
6
  , _rbc("localhost:9090", false /*run*/)
7 8 9
  , _topicPublisher(_casePacker, _rbc)
  , _topicSubscriber(_casePacker, _rbc)
  , _server(_casePacker, _rbc)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10 11 12
{
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
13
void ROSBridge::ROSBridge::publish(ROSBridge::ROSBridge::JsonDocUPtr doc)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
14
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
15
    _topicPublisher.publish(std::move(doc));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
16 17
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
18
void ROSBridge::ROSBridge::subscribe(const char *topic, const std::function<void(JsonDocUPtr)> &callBack)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
19
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
20
    _topicSubscriber.subscribe(topic, callBack);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21 22
}

23 24 25 26 27 28 29
void ROSBridge::ROSBridge::advertiseService(const std::string &service,
                                            const std::string &type,
                                            const std::function<JsonDocUPtr(JsonDocUPtr)> &callback)
{
    _server.advertiseService(service, type, callback);
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
30
const ROSBridge::CasePacker *ROSBridge::ROSBridge::casePacker() const
Valentin Platzgummer's avatar
Valentin Platzgummer committed
31
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
32
    return &_casePacker;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
35
void ROSBridge::ROSBridge::start()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
36
{
37 38
    _stopped->store(false);
    _rbc.run();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
39 40
    _topicPublisher.start();
    _topicSubscriber.start();
41
    _server.start();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
42 43
}

44
void ROSBridge::ROSBridge::reset()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
45
{
46
    auto start = std::chrono::high_resolution_clock::now();
47
    _stopped->store(true);
48 49
    _topicPublisher.reset();
    _topicSubscriber.reset();
50
    _server.reset();
51
    _rbc.reset();
52 53 54
    auto end = std::chrono::high_resolution_clock::now();
    auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
    std::cout << "ROSBridge reset duration: " << delta << " ms" << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
55 56
}

57
bool ROSBridge::ROSBridge::connected()
58 59 60 61
{
    return _rbc.connected();
}

62 63
bool ROSBridge::ROSBridge::isRunning()
{
64
    return !_stopped->load();
65 66
}