hello_world_serial.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <rtthread.h>
  2. #include <ros.h>
  3. #include <std_msgs/String.h>
  4. static ros::NodeHandle nh;
  5. static std_msgs::String str_msg;
  6. static ros::Publisher chatter("chatter", &str_msg);
  7. static char hello_msg[13] = "hello world!";
  8. static void rosserial_thread_entry(void *parameter)
  9. {
  10. nh.initNode();
  11. nh.advertise(chatter);
  12. while (1)
  13. {
  14. if ( nh.connected() )
  15. {
  16. str_msg.data = hello_msg;
  17. chatter.publish( &str_msg );
  18. }
  19. /*
  20. else
  21. {
  22. rt_kprintf("[rosserial] Not Connected");
  23. }
  24. */
  25. nh.spinOnce();
  26. rt_thread_delay(RT_TICK_PER_SECOND);
  27. }
  28. }
  29. static void rosserial_hello_world_serial_example(int argc,char *argv[])
  30. {
  31. rt_thread_t thread = rt_thread_create("rosserial", rosserial_thread_entry, RT_NULL, 1024, 25, 10);
  32. if(thread != RT_NULL)
  33. {
  34. rt_thread_startup(thread);
  35. rt_kprintf("[rosserial] New thread rosserial\n");
  36. }
  37. else
  38. {
  39. rt_kprintf("[rosserial] Failed to create thread rosserial\n");
  40. }
  41. }
  42. MSH_CMD_EXPORT(rosserial_hello_world_serial_example, roserial hello world example with UART);
  43. // If you are using Keil, you can ignore everything below
  44. // This is required
  45. // If you'd like to compile with scons which uses arm-none-eabi-gcc
  46. // extern "C" void __cxa_pure_virtual()
  47. // {
  48. // while (1);
  49. //}
  50. // Moreover, you need to add:
  51. // CXXFLAGS = CFLAGS + ' -fno-rtti'
  52. // in rtconfig.py for arm-none-eabi-gcc