Guest User

Untitled

a guest
Feb 10th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <micro_ros_platformio.h>
  3.  
  4. #include <rcl/rcl.h>
  5. #include <rclc/rclc.h>
  6. #include <rclc/executor.h>
  7.  
  8. #include <std_msgs/msg/int32.h>
  9.  
  10. #include <Wire.h>
  11.  
  12.  
  13. rcl_publisher_t publisher;
  14. std_msgs__msg__Int32 msg;
  15.  
  16. rclc_executor_t executor;
  17. rclc_support_t support;
  18. rcl_allocator_t allocator;
  19. rcl_node_t node;
  20. rcl_timer_t timer;
  21.  
  22. #define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
  23. #define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
  24.  
  25. // ADAM CODE
  26.  
  27. extern "C"
  28. {
  29. bool keplie_transport_i2c_open(struct uxrCustomTransport * transport);
  30. bool keplie_transport_i2c_close(struct uxrCustomTransport * transport);
  31. size_t kelpie_transport_i2c_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err);
  32. size_t kelpie_transport_ic2_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* err);
  33.  
  34. typedef struct
  35. {
  36. uint8_t masterAddress;
  37. uint8_t myAddress;
  38. } kelpie_i2c_args;
  39.  
  40. }
  41.  
  42. // ADAM CODE
  43.  
  44. // Error handle loop
  45. void error_loop() {
  46. while(1) {
  47. delay(100);
  48. }
  49. }
  50.  
  51. void timer_callback(rcl_timer_t * timer, int64_t last_call_time) {
  52. RCLC_UNUSED(last_call_time);
  53. if (timer != NULL) {
  54. RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
  55. msg.data++;
  56. }
  57. }
  58.  
  59. void setup() {
  60. // Configure serial transport
  61. Serial.begin(115200);
  62. //set_microros_serial_transports(Serial);
  63.  
  64. // ADAM CODE
  65. kelpie_i2c_args args = {0x0, 0x1};
  66. uxrCustomTransport transport;
  67.  
  68. rmw_uros_set_custom_transport(
  69. true,
  70. &args,
  71. keplie_transport_i2c_open,
  72. keplie_transport_i2c_close,
  73. kelpie_transport_i2c_write,
  74. kelpie_transport_ic2_read
  75. );
  76.  
  77. uxr_init_custom_transport(&transport, (void *) &args);
  78. // ADAM CODE
  79.  
  80. delay(2000);
  81.  
  82. allocator = rcl_get_default_allocator();
  83.  
  84. //create init_options
  85. RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
  86.  
  87. // create node
  88. RCCHECK(rclc_node_init_default(&node, "micro_ros_platformio_node", "", &support));
  89.  
  90. // create publisher
  91. RCCHECK(rclc_publisher_init_default(
  92. &publisher,
  93. &node,
  94. ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  95. "micro_ros_platformio_node_publisher"));
  96.  
  97. // create timer,
  98. const unsigned int timer_timeout = 1000;
  99. RCCHECK(rclc_timer_init_default(
  100. &timer,
  101. &support,
  102. RCL_MS_TO_NS(timer_timeout),
  103. timer_callback));
  104.  
  105. // create executor
  106. RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
  107. RCCHECK(rclc_executor_add_timer(&executor, &timer));
  108.  
  109. msg.data = 0;
  110. }
  111.  
  112. void loop() {
  113. delay(100);
  114. RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
  115. }
  116.  
  117. // ADAM CODE
  118. bool keplie_transport_i2c_open(struct uxrCustomTransport * transport)
  119. {
  120. kelpie_i2c_args *args = (kelpie_i2c_args*)(transport->args);
  121. Wire.begin(args->myAddress);
  122. return true;
  123. }
  124.  
  125. bool keplie_transport_i2c_close(struct uxrCustomTransport * transport)
  126. {
  127. Wire.end();
  128. return true;
  129. }
  130.  
  131. size_t kelpie_transport_i2c_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err)
  132. {
  133. kelpie_i2c_args *args = (kelpie_i2c_args*)(transport->args);
  134. size_t bytesWritten;
  135. Wire.beginTransmission(args->masterAddress);
  136. bytesWritten = Wire.write(buf, len);
  137. Wire.endTransmission();
  138.  
  139. return bytesWritten;
  140. }
  141.  
  142. size_t kelpie_transport_ic2_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* err)
  143. {
  144. kelpie_i2c_args *args = (kelpie_i2c_args*)(transport->args);
  145. //Wire.requestFrom(args->masterAddress, len); pico isnt the master on the bus, it shouldn't be requesting
  146. int i = 0;
  147. while(Wire.available())
  148. {
  149. buf[i] = Wire.read();
  150. i++;
  151. }
  152.  
  153. return i;
  154. }
  155.  
  156. // ADAM CODE
Advertisement
Add Comment
Please, Sign In to add comment