You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "../build/dora-node-api.h"
  2. #include "../build/dora-ros2-bindings.h"
  3. #include <iostream>
  4. #include <vector>
  5. #include <random>
  6. int main()
  7. {
  8. std::cout << "HELLO FROM C++" << std::endl;
  9. auto dora_node = init_dora_node();
  10. auto merged_events = dora_events_into_combined(std::move(dora_node.events));
  11. auto qos = qos_default();
  12. qos.durability = Ros2Durability::Volatile;
  13. qos.liveliness = Ros2Liveliness::Automatic;
  14. qos.reliable = true;
  15. qos.max_blocking_time = 0.1;
  16. auto ros2_context = init_ros2_context();
  17. auto node = ros2_context->new_node("/ros2_demo", "turtle_teleop");
  18. auto vel_topic = node->create_topic_geometry_msgs_Twist("/turtle1", "cmd_vel", qos);
  19. auto vel_publisher = node->create_publisher(vel_topic, qos);
  20. auto pose_topic = node->create_topic_turtlesim_Pose("/turtle1", "pose", qos);
  21. auto pose_subscription = node->create_subscription(pose_topic, qos, merged_events);
  22. std::random_device dev;
  23. std::default_random_engine gen(dev());
  24. std::uniform_real_distribution<> dist(0., 1.);
  25. auto received_ticks = 0;
  26. for (int i = 0; i < 1000; i++)
  27. {
  28. auto event = merged_events.next();
  29. if (event.is_dora())
  30. {
  31. auto dora_event = downcast_dora(std::move(event));
  32. auto ty = event_type(dora_event);
  33. if (ty == DoraEventType::AllInputsClosed)
  34. {
  35. break;
  36. }
  37. else if (ty == DoraEventType::Input)
  38. {
  39. auto input = event_as_input(std::move(dora_event));
  40. received_ticks += 1;
  41. std::cout << "Received input " << std::string(input.id) << std::endl;
  42. geometry_msgs::Twist twist = {
  43. .linear = {.x = dist(gen) + 1, .y = 0, .z = 0},
  44. .angular = {.x = 0, .y = 0, .z = (dist(gen) - 0.5) * 5.0}};
  45. vel_publisher->publish(twist);
  46. }
  47. else
  48. {
  49. std::cerr << "Unknown event type " << static_cast<int>(ty) << std::endl;
  50. }
  51. if (received_ticks > 20)
  52. {
  53. break;
  54. }
  55. }
  56. else if (pose_subscription->matches(event))
  57. {
  58. auto pose = pose_subscription->downcast(std::move(event));
  59. std::cout << "Received pose x:" << pose.x << ", y:" << pose.y << std::endl;
  60. }
  61. else
  62. {
  63. std::cout << "received unexpected event" << std::endl;
  64. }
  65. }
  66. std::cout << "GOODBYE FROM C++ node (using Rust API)" << std::endl;
  67. return 0;
  68. }