blink.cpp 1.4 KB

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