pubsub.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * rosserial PubSub Example
  3. * Prints "hello world!" and toggles led
  4. */
  5. #include <rtthread.h>
  6. #include <board.h>
  7. #include <ros.h>
  8. #include <std_msgs/String.h>
  9. #include <std_msgs/Empty.h>
  10. #define LED0_PIN GET_PIN(A, 5)
  11. static ros::NodeHandle nh;
  12. static void messageCb( const std_msgs::Empty& toggle_msg){
  13. rt_pin_write(LED0_PIN, PIN_HIGH - rt_pin_read(LED0_PIN)); // blink the led
  14. }
  15. static ros::Subscriber<std_msgs::Empty> sub("toggle_led", messageCb );
  16. static std_msgs::String str_msg;
  17. static ros::Publisher chatter("chatter", &str_msg);
  18. static char hello[13] = "hello world!";
  19. static void rosserial_pub_sub_thread_entry(void *parameter)
  20. {
  21. rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
  22. nh.initNode();
  23. nh.advertise(chatter);
  24. nh.subscribe(sub);
  25. while (1)
  26. {
  27. str_msg.data = hello;
  28. chatter.publish( &str_msg );
  29. nh.spinOnce();
  30. rt_thread_mdelay(500);
  31. }
  32. }
  33. static void rosserial_pub_sub_example(int argc,char *argv[])
  34. {
  35. rt_thread_t thread = rt_thread_create("r_pubsub", rosserial_pub_sub_thread_entry, RT_NULL, 1024, 25, 10);
  36. if(thread != RT_NULL)
  37. {
  38. rt_thread_startup(thread);
  39. rt_kprintf("[rosserial] New thread r_pubsub\n");
  40. }
  41. else
  42. {
  43. rt_kprintf("[rosserial] Failed to create thread r_pubsub\n");
  44. }
  45. }
  46. MSH_CMD_EXPORT(rosserial_pub_sub_example, roserial publish and subscribe with UART);
  47. // If you are using Keil, you can ignore everything below
  48. // This is required
  49. // If you'd like to compile with scons which uses arm-none-eabi-gcc
  50. // extern "C" void __cxa_pure_virtual()
  51. // {
  52. // while (1);
  53. //}
  54. // Moreover, you need to add:
  55. // CXXFLAGS = CFLAGS + ' -fno-rtti'
  56. // in rtconfig.py for arm-none-eabi-gcc