File: irobot_create_service.cpp

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (480 lines) | stat: -rw-r--r-- 14,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <RobotRaconteur.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include "robotraconteur_generated.h"
#include <boost/asio/serial_port.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <map>
#include <cereal/archives/portable_binary.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <drekar_launch_process_cpp/drekar_launch_process_cpp.h>

// Only use namespace aliases in cpp files. Do not use aliases in header files.
namespace RR = RobotRaconteur;
namespace c3 = experimental::create3;

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define CREATE_OI_OP_START 128
#define CREATE_OI_OP_FULL 132
#define CREATE_OI_OP_DRIVE 137
#define CREATE_OI_OP_LEDS 139
#define CREATE_OI_OP_SONG 140
#define CREATE_OI_OP_DRIVE_DIRECT 145
#define CREATE_OI_OP_STREAM 148
#define CREATE_OI_OP_STREAM_RESUME 150

#define CREATE_OI_FLAGS_BUMP_RIGHT 0x1
#define CREATE_OI_FLAGS_BUMP_LEFT 0x2
#define CREATE_OI_FLAGS_WHEEL_DROP_RIGHT 0x4
#define CREATE_OI_FLAGS_WHEEL_DROP_LEFT 0x8
#define CREATE_OI_FLAGS_WHEEL_DROP_CASTER 0x10

#define CREATE_OI_FLAGS_PLAY_BUTTON 0x1
#define CREATE_OI_FLAGS_ADVANCE_BUTTON 0x4

#define CREATE_OI_SET_LED_PLAY 0x2
#define CREATE_OI_SET_LED_ADVANCE 0x8

class Create_impl : public c3::Create_default_impl, public boost::enable_shared_from_this<Create_impl>
{
  public:
    Create_impl() {}

    void Init(std::string port)
    {
        boost::mutex::scoped_lock lock(this_lock);

        serial_port.reset(
            new boost::asio::serial_port(RR::RobotRaconteurNode::s()->GetThreadPool()->get_io_context(), port));

        if (!serial_port->is_open())
        {
            throw std::runtime_error("Could not open port");
        }

        serial_port->set_option(boost::asio::serial_port::baud_rate(57600));
        serial_port->set_option(boost::asio::serial_port::stop_bits());
        serial_port->set_option(boost::asio::serial_port::flow_control());
        serial_port->set_option(boost::asio::serial_port::character_size());

        std::vector<uint8_t> start_cmd = {
            CREATE_OI_OP_START, CREATE_OI_OP_FULL, CREATE_OI_OP_STREAM_RESUME, 0, CREATE_OI_OP_LEDS, 0, 80, 255};
        boost::asio::write(*serial_port, boost::asio::buffer(start_cmd));

        boost::this_thread::sleep(boost::posix_time::milliseconds(100));

        lock.unlock();
        StartStreaming();
    }

    void Shutdown()
    {
        StopStreaming();

        boost::mutex::scoped_lock lock(this_lock);

        std::vector<uint8_t> stop_cmd = {CREATE_OI_OP_START};
        boost::asio::write(*serial_port, boost::asio::buffer(stop_cmd));

        serial_port->close();
    }

    void StartStreaming()
    {
        boost::mutex::scoped_lock lock(this_lock);

        if (streaming)
            return;

        streaming = true;

        // Start the thread that will receive serial data
        auto shared_this = shared_from_this();
        boost::thread([shared_this]() { shared_this->recv_thread_func(); });

        boost::this_thread::sleep(boost::posix_time::milliseconds(100));

        std::vector<uint8_t> command = {CREATE_OI_OP_STREAM, 1, 6};
        boost::asio::write(*serial_port, boost::asio::buffer(command));
    }

    void StopStreaming()
    {
        boost::mutex::scoped_lock lock(this_lock);

        if (!streaming)
            return;

        streaming = false;

        std::vector<uint8_t> command = {CREATE_OI_OP_STREAM_RESUME, 0};
        boost::asio::write(*serial_port, boost::asio::buffer(command));
    }

    void drive(double velocity, double radius) override
    {
        boost::mutex::scoped_lock lock(this_lock);

        int16_t v = boost::numeric_cast<int16_t>(velocity * 1000);
        int16_t r = boost::numeric_cast<int16_t>(radius * 1000);

        // Use cereal PortableBinary to serialize the data
        std::stringstream ss;
        cereal::PortableBinaryOutputArchive ar(ss, cereal::PortableBinaryOutputArchive::Options::BigEndian());
        // opcode
        ar((uint8_t)CREATE_OI_OP_DRIVE);
        ar(v);
        ar(r);

        std::string s = ss.str();
        boost::asio::write(*serial_port, boost::asio::buffer(s));
    }

    void drive_direct(double right_velocity, double left_velocity) override
    {
        boost::mutex::scoped_lock lock(this_lock);

        int16_t vr = boost::numeric_cast<int16_t>(right_velocity * 1000);
        int16_t vl = boost::numeric_cast<int16_t>(left_velocity * 1000);

        // Use cereal PortableBinary to serialize the data
        std::stringstream ss;
        cereal::PortableBinaryOutputArchive ar(ss, cereal::PortableBinaryOutputArchive::Options::BigEndian());
        // opcode
        ar((uint8_t)CREATE_OI_OP_DRIVE_DIRECT);
        ar(vr);
        ar(vl);

        std::string s = ss.str();
        boost::asio::write(*serial_port, boost::asio::buffer(s));
    }

    void stop() override { drive(0, 0); }

    void setf_leds(RR::rr_bool play, RR::rr_bool advance) override
    {
        boost::mutex::scoped_lock lock(this_lock);

        uint8_t bits = 0;
        if (play.value)
            bits |= CREATE_OI_SET_LED_PLAY;
        if (advance.value)
            bits |= CREATE_OI_SET_LED_ADVANCE;

        std::vector<uint8_t> command = {CREATE_OI_OP_LEDS, bits, 80, 255};
        boost::asio::write(*serial_port, boost::asio::buffer(command));
    }

    void claim_play_callback() override
    {
        boost::mutex::scoped_lock lock(this_lock);

        play_client = RR::ServerEndpoint::GetCurrentEndpoint()->GetLocalEndpoint();
    }

  protected:
    void recv_thread_func()
    {
        size_t checksum_error_count = 0;
        try
        {
            while (streaming)
            {
                uint8_t magic;
                boost::asio::read(*serial_port, boost::asio::buffer(&magic, 1));

                if (magic != 19)
                    continue;

                uint8_t nbytes;
                boost::asio::read(*serial_port, boost::asio::buffer(&nbytes, 1));

                std::vector<uint8_t> data(nbytes);
                boost::asio::read(*serial_port, boost::asio::buffer(&data[0], nbytes));

                uint8_t checksum;
                boost::asio::read(*serial_port, boost::asio::buffer(&checksum, 1));

                uint32_t checksum_calc = 19 + nbytes;
                for (auto& b : data)
                {
                    checksum_calc += b;
                }
                checksum_calc += checksum;
                checksum_calc &= 0xff;

                if (checksum_calc != 0)
                {
                    checksum_error_count++;
                    if (checksum_error_count > 20)
                    {
                        std::cout << "Checksum error" << std::endl;
                        checksum_error_count = 0;
                    }
                    continue;
                }

                auto state = ParseSensorPackets(data);
                {
                    boost::mutex::scoped_lock lock(this_lock);
                    if (this->rrvar_create_state)
                    {
                        this->rrvar_create_state->SetOutValue(state);
                    }

                    if (state->create_state_flags &
                        (c3::CreateStateFlags::bump_right | c3::CreateStateFlags::bump_left))
                    {
                        if (!bump_fired)
                        {
                            this->FireBump();
                            bump_fired = true;
                        }
                    }
                    else
                    {
                        bump_fired = false;
                    }

                    rrvar_distance_traveled = state->distance_traveled;
                    rrvar_angle_traveled = state->angle_traveled;
                    rrvar_bumpers = state->create_state_flags &
                                    (c3::CreateStateFlags::bump_right | c3::CreateStateFlags::bump_left);

                    if (state->create_state_flags & c3::CreateStateFlags::play_button)
                    {
                        if (!play_pressed)
                        {
                            lock.unlock();
                            this->Play();
                            lock.lock();
                            play_pressed = true;
                        }
                    }
                    else
                    {
                        play_pressed = false;
                    }
                }
            }
        }
        catch (std::exception& exp)
        {
            std::cout << "Error in recv_thread_func: " << exp.what() << std::endl;
        }
    }

    c3::CreateStatePtr ParseSensorPackets(const std::vector<uint8_t>& data)
    {
        c3::CreateStatePtr ret(new c3::CreateState());

        // Use cereal PortableBinary to deserialize the data
        std::stringstream ss;
        ss.write((const char*)data.data(), data.size());
        ss.seekg(0);
        cereal::PortableBinaryInputArchive ar(ss, cereal::PortableBinaryInputArchive::Options::BigEndian());

        uint8_t bump_flags_b;
        ar(bump_flags_b);
        uint8_t wall_b;
        ar(wall_b);
        uint8_t cliff_left_b;
        ar(cliff_left_b);
        uint8_t cliff_front_left_b;
        ar(cliff_front_left_b);
        uint8_t cliff_front_right_b;
        ar(cliff_front_right_b);
        uint8_t cliff_right_b;
        ar(cliff_right_b);
        uint8_t virtual_wall_b;
        ar(virtual_wall_b);
        // skip four bytes
        uint8_t dummy;
        for (int i = 0; i < 4; i++)
        {
            ar(dummy);
        }

        uint8_t buttons_b;
        ar(buttons_b);
        int16_t distance_h;
        ar(distance_h);
        int16_t angle_h;
        ar(angle_h);

        // skip six bytes
        for (int i = 0; i < 6; i++)
        {
            ar(dummy);
        }

        uint16_t charge_H;
        ar(charge_H);
        uint16_t capacity_H;
        ar(capacity_H);

        // skip 18 bytes
        for (int i = 0; i < 18; i++)
        {
            ar(dummy);
        }

        int16_t velocity_h;
        ar(velocity_h);
        int16_t radius_h;
        ar(radius_h);
        int16_t velocity_right_h;
        ar(velocity_right_h);
        int16_t velocity_left_h;
        ar(velocity_left_h);

        int32_t state_flags = 0;
        if (bump_flags_b & CREATE_OI_FLAGS_BUMP_RIGHT)
        {
            state_flags |= c3::CreateStateFlags::bump_right;
        }
        if (bump_flags_b & CREATE_OI_FLAGS_BUMP_LEFT)
        {
            state_flags |= c3::CreateStateFlags::bump_left;
        }
        if (bump_flags_b & CREATE_OI_FLAGS_WHEEL_DROP_RIGHT)
        {
            state_flags |= c3::CreateStateFlags::wheel_drop_right;
        }
        if (bump_flags_b & CREATE_OI_FLAGS_WHEEL_DROP_LEFT)
        {
            state_flags |= c3::CreateStateFlags::wheel_drop_left;
        }
        if (bump_flags_b & CREATE_OI_FLAGS_WHEEL_DROP_CASTER)
        {
            state_flags |= c3::CreateStateFlags::wheel_drop_caster;
        }

        if (wall_b != 0)
        {
            state_flags |= c3::CreateStateFlags::wall_sensor;
        }

        if (cliff_left_b != 0)
        {
            state_flags |= c3::CreateStateFlags::cliff_left;
        }
        if (cliff_front_left_b != 0)
        {
            state_flags |= c3::CreateStateFlags::cliff_front_left;
        }
        if (cliff_front_right_b != 0)
        {
            state_flags |= c3::CreateStateFlags::cliff_front_right;
        }
        if (cliff_right_b != 0)
        {
            state_flags |= c3::CreateStateFlags::cliff_right;
        }

        if (virtual_wall_b != 0)
        {
            state_flags |= c3::CreateStateFlags::virtual_wall;
        }

        if ((buttons_b & CREATE_OI_FLAGS_PLAY_BUTTON) != 0)
        {
            state_flags |= c3::CreateStateFlags::play_button;
        }

        if ((buttons_b & CREATE_OI_FLAGS_ADVANCE_BUTTON) != 0)
        {
            state_flags |= c3::CreateStateFlags::advance_button;
        }

        ret->create_state_flags = state_flags;

        RR::TimeSpec time = RR::RobotRaconteurNode::s()->NowTimeSpec();

        ret->time = boost::numeric_cast<double>(time.seconds) + (boost::numeric_cast<double>(time.nanoseconds) / 1e9);
        ret->velocity = boost::numeric_cast<double>(velocity_h) * 1e-3;
        ret->radius = boost::numeric_cast<double>(radius_h) * 1e-3;
        ret->right_wheel_velocity = boost::numeric_cast<double>(velocity_right_h) * 1e-3;
        ret->left_wheel_velocity = boost::numeric_cast<double>(velocity_left_h) * 1e-3;
        ret->distance_traveled = boost::numeric_cast<double>(distance_h) * 1e-3;
        ret->angle_traveled = boost::numeric_cast<double>(angle_h) * (M_PI / 180.0);
        ret->battery_charge = boost::numeric_cast<double>(charge_H);
        ret->battery_capacity = boost::numeric_cast<double>(capacity_H);

        return ret;
    }

    void FireBump() { rrvar_bump(); }

    void Play()
    {
        if (play_client == 0)
        {
            return;
        }

        try
        {
            if (!rrvar_play_callback)
            {
                return;
            }
            auto cb_func = rrvar_play_callback->GetClientFunction(play_client);
            auto notes = cb_func(rrvar_distance_traveled, rrvar_angle_traveled);

            std::vector<uint8_t> command = {CREATE_OI_OP_SONG, 0, (uint8_t)(notes->size() / 2)};
            boost::range::copy(*notes, std::back_inserter(command));
            command.push_back(141);
            command.push_back(0);

            boost::mutex::scoped_lock lock(this_lock);
            boost::asio::write(*serial_port, boost::asio::buffer(command));
        }
        catch (std::exception& exp)
        {
            std::cout << "Error in Play: " << exp.what() << std::endl;
        }
    }

    boost::shared_ptr<boost::asio::serial_port> serial_port;
    bool bump_fired = false;
    bool play_pressed = false;
    bool streaming = false;

    uint32_t play_client = 0;
};

int main(int argc, char* argv[])
{
    std::string port = "/dev/ttyUSB0";
    if (argc > 1)
    {
        port = argv[1];
    }

    boost::shared_ptr<Create_impl> create = boost::make_shared<Create_impl>();
    create->Init(port);

    RR::ServerNodeSetup node_setup(ROBOTRACONTEUR_SERVICE_TYPES, "experimental.create3", 22354);

    auto ctx = RR::RobotRaconteurNode::s()->RegisterService("create", "experimental.create3", create);

    // Print out some info for the user
    std::cout << "iRobot Create CPP Service Started" << std::endl << std::endl;

    std::cout << "Candidate connection urls:" << std::endl;
    ctx->PrintCandidateConnectionURLs();
    std::cout << std::endl;
    std::cout << "Press Ctrl-C to quit" << std::endl;

    // Use drekar_launch_process_cpp package to wait for exit
    drekar_launch_process_cpp::CWaitForExit wait_exit;
    wait_exit.WaitForExit();

    create->Shutdown();

    return 0;
}