Guest User

Untitled

a guest
Apr 19th, 2021
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <micro_ros_arduino.h>
  2.  
  3. #include <stdio.h>
  4. #include <rcl/rcl.h>
  5. #include <rcl/error_handling.h>
  6. #include <rclc/rclc.h>
  7. #include <rclc/executor.h>
  8.  
  9. #include <std_msgs/msg/int32.h>
  10.  
  11. rcl_publisher_t publisher;
  12. std_msgs__msg__Int32 msg;
  13. rclc_executor_t executor;
  14. rclc_support_t support;
  15. rcl_allocator_t allocator;
  16. rcl_node_t node;
  17. rcl_timer_t timer;
  18.  
  19. #define LED_PIN 13
  20.  
  21. #define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
  22. #define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
  23.  
  24. #define trigPin 9
  25. #define echoPin 10
  26. long duration;
  27. int distance;
  28.  
  29. void error_loop(){
  30.   while(1){
  31.     digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  32.     delay(100);
  33.   }
  34. }
  35.  
  36. void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
  37. {  
  38.   RCLC_UNUSED(last_call_time);
  39.   if (timer != NULL) {
  40.     RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
  41.     msg.data++;
  42.   }
  43. }
  44.  
  45. void setup() {
  46.   set_microros_transports();
  47.  
  48.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  49.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  50.   Serial.begin(9600); // Starts the serial communication
  51.   pinMode(LED_PIN, OUTPUT);
  52.   //digitalWrite(LED_PIN, HIGH);  
  53.  
  54.   delay(2000);
  55.  
  56.   allocator = rcl_get_default_allocator();
  57.  
  58.   //create init_options
  59.   RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
  60.  
  61.   // create node
  62.   rcl_node_options_t node_ops = rcl_node_get_default_options();
  63.   node_ops.domain_id = 30;
  64.   RCCHECK(rclc_node_init_with_options(&node, "micro_ros_arduino_node", "", &support, &node_ops));
  65.   //RCCHECK(rclc_node_init_default(&node, "micro_ros_arduino_node", "", &support));
  66.  
  67.   // create publisher
  68.   RCCHECK(rclc_publisher_init_default(
  69.     &publisher,
  70.     &node,
  71.     ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  72.     "micro_ros_arduino_node_publisher"));
  73.  
  74.   // create timer,
  75.   const unsigned int timer_timeout = 1000;
  76.   RCCHECK(rclc_timer_init_default(
  77.     &timer,
  78.     &support,
  79.     RCL_MS_TO_NS(timer_timeout),
  80.     timer_callback));
  81.  
  82.   // create executor
  83.   RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
  84.   RCCHECK(rclc_executor_add_timer(&executor, &timer));
  85.  
  86.   msg.data = 0;
  87. }
  88.  
  89. void loop() {
  90.   digitalWrite(trigPin, LOW);
  91.   delayMicroseconds(2);
  92.   // Sets the trigPin on HIGH state for 10 micro seconds
  93.   digitalWrite(trigPin, HIGH);
  94.   delayMicroseconds(10);
  95.   digitalWrite(trigPin, LOW);
  96.   // Reads the echoPin, returns the sound wave travel time in microseconds
  97.   duration = pulseIn(echoPin, HIGH);
  98.   // Calculating the distance
  99.   distance= duration*0.034/2;
  100.   // Prints the distance on the Serial Monitor
  101.   Serial.print("Distance: ");
  102.   Serial.println(distance);
  103.   delay(100);
  104.   RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
  105. }
Advertisement
Add Comment
Please, Sign In to add comment