-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.cpp
More file actions
283 lines (241 loc) · 8.18 KB
/
first.cpp
File metadata and controls
283 lines (241 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// #include <Arduino.h>
// #include <micro_ros_platformio.h>
// #include <rcl/rcl.h>
// #include <rclc/rclc.h>
// #include <rclc/executor.h>
// #include <std_msgs/msg/float32_multi_array.h>
// #include <rcl/error_handling.h>
// #include <std_msgs/msg/float32.h>
// #include <std_msgs/msg/string.h>
// // --- Configurable ---
// const char * serial_dev = "/dev/ttyUSB0";
// const size_t RING_BUFFER_SIZE = 512;
// // ----------------------
// // micro-ROS objects
// rcl_publisher_t publisher;
// rcl_subscription_t subscriber;
// std_msgs__msg__Float32 sub_msg;
// rclc_support_t support;
// rcl_allocator_t allocator;
// rcl_node_t node;
// rclc_executor_t executor;
// rcl_publisher_t debug_pub;
// std_msgs__msg__String debug_msg;
// // Callback: called when a new std_msgs/Float32 arrives
// void subscription_callback(const void * msgin)
// {
// const auto *in = (const std_msgs__msg__Float32 *)msgin;
// Serial.print("Received value: ");
// Serial.println(in->data, 4);
// char buf[64];
// int len = snprintf(buf, sizeof(buf),
// "Received: %.4f", in->data);
// debug_msg.data.data = (char *)buf;
// debug_msg.data.size = len;
// debug_msg.data.capacity = sizeof(buf);
// rcl_publish(&debug_pub, &debug_msg, NULL);
// }
// void setup() {
// Serial.begin(115200);
// while (!Serial) { delay(10); }
// Serial.println("ESP32 micro-ROS Serial Echo");
// set_microros_serial_transports(Serial);
// delay(2000);
// allocator = rcl_get_default_allocator();
// rclc_support_init(&support, 0, NULL, &allocator);
// // Node
// rclc_node_init_default(&node, "esp32_listener", "", &support);
// // Subscriber on "/esp32_value" topic, type Float32
// rclc_subscription_init_default(
// &subscriber,
// &node,
// ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32),
// "esp32_value"
// );
// rclc_executor_init(&executor, &support.context, 1, &allocator);
// rclc_executor_add_subscription(
// &executor,
// &subscriber,
// &sub_msg,
// subscription_callback,
// ON_NEW_DATA
// );
// rclc_publisher_init_default(
// &debug_pub,
// &node,
// ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
// "esp32_debug"
// );
// }
// void loop() {
// rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
// delay(10);
// }
// // #include <Arduino.h>
// // #include <micro_ros_platformio.h>
// // #include <rcl/rcl.h>
// // #include <rclc/rclc.h>
// // #include <rclc/executor.h>
// // #include <std_msgs/msg/float32_multi_array.h>
// // #include <std_msgs/msg/string.h>
// // #include <ESP32Servo.h>
// // const int SERVO_PIN_1 = 18;
// // const int SERVO_PIN_2 = 19;
// // const int SERVO_PIN_3 = 21;
// // Servo servo1, servo2, servo3;
// // // micro-ROS objects
// // rcl_subscription_t subscriber;
// // std_msgs__msg__Float32MultiArray sub_msg;
// // rclc_support_t support;
// // rcl_allocator_t allocator;
// // rcl_node_t node;
// // rclc_executor_t executor;
// // rcl_publisher_t debug_pub;
// // std_msgs__msg__String debug_msg;
// // void joint_callback(const void * msgin) {
// // const auto * msg = (const std_msgs__msg__Float32MultiArray *)msgin;
// // char buf[64];
// // int len = snprintf(buf, sizeof(buf),
// // "Got: %.1f,%.1f,%.1f",
// // msg->data.data[0],
// // msg->data.data[1],
// // msg->data.data[2]);
// // debug_msg.data.data = (char *)buf;
// // debug_msg.data.size = len;
// // debug_msg.data.capacity = sizeof(buf);
// // rcl_publish(&debug_pub, &debug_msg, NULL);
// // // if (msg->data.size < 3) {
// // // return;
// // // }
// // float angles[3];
// // for (size_t i = 0; i < 3; i++) {
// // angles[i] = msg->data.data[i];
// // }
// // servo1.write((int)angles[0]);
// // servo2.write((int)angles[1]);
// // servo3.write((int)angles[2]);
// // }
// // void setup() {
// // Serial.begin(115200);
// // while (!Serial) { delay(10); }
// // Serial.println("ESP32 delta controller starting...");
// // servo1.attach(SERVO_PIN_1, 500, 2500);
// // servo2.attach(SERVO_PIN_2, 500, 2500);
// // servo3.attach(SERVO_PIN_3, 500, 2500);
// // set_microros_serial_transports(Serial);
// // delay(2000);
// // allocator = rcl_get_default_allocator();
// // rclc_support_init(&support, 0, NULL, &allocator);
// // rclc_node_init_default(&node, "esp32_delta", "", &support);
// // rclc_subscription_init_default(
// // &subscriber,
// // &node,
// // ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32MultiArray),
// // "joint_angles"
// // );
// // rclc_executor_init(&executor, &support.context, 1, &allocator);
// // rclc_executor_add_subscription(
// // &executor,
// // &subscriber,
// // &sub_msg,
// // joint_callback,
// // ON_NEW_DATA
// // );
// // rclc_publisher_init_default(
// // &debug_pub,
// // &node,
// // ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
// // "esp32_debug"
// // );
// // }
// // void loop() {
// // rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
// // delay(10);
// // }
// // #include <Arduino.h>
// // #include <micro_ros_platformio.h>
// // #include <rcl/rcl.h>
// // #include <rclc/rclc.h>
// // #include <rclc/executor.h>
// // #include <std_msgs/msg/string.h>
// // #include <ESP32Servo.h>
// // // pins
// // const int PIN1 = 18, PIN2 = 19, PIN3 = 21;
// // Servo s1, s2, s3;
// // // micro-ROS objects
// // rcl_subscription_t sub;
// // std_msgs__msg__String sub_str;
// // rcl_publisher_t dbg_pub;
// // std_msgs__msg__String dbg_str;
// // rclc_support_t support;
// // rcl_allocator_t alloc;
// // rcl_node_t node;
// // rclc_executor_t exe;
// // void cb(const void * msgin) {
// // Serial.println(">> CB FIRED!");
// // const auto * m = (const std_msgs__msg__String*)msgin;
// // static char buf[64];
// // memcpy(buf, m->data.data, m->data.size);
// // buf[m->data.size] = 0;
// // // parse three floats
// // float a[3] = {0,0,0};
// // char *tok = strtok(buf, ",");
// // for (int i=0; i<3 && tok; ++i) {
// // a[i] = atof(tok);
// // tok = strtok(NULL, ",");
// // }
// // // drive servos
// // s1.write((int)a[0]);
// // s2.write((int)a[1]);
// // s3.write((int)a[2]);
// // // publish a debug string
// // int L = snprintf(buf, sizeof(buf),
// // "A=%.1f,%.1f,%.1f", a[0],a[1],a[2]);
// // dbg_str.data.data = buf;
// // dbg_str.data.size = L;
// // dbg_str.data.capacity = sizeof(buf);
// // rcl_publish(&dbg_pub, &dbg_str, nullptr);
// // }
// // void setup() {
// // Serial.begin(115200);
// // while(!Serial) delay(10);
// // Serial.println("ESP32 delta controller starting...");
// // // attach servos
// // s1.attach(PIN1, 500, 2500);
// // s2.attach(PIN2, 500, 2500);
// // s3.attach(PIN3, 500, 2500);
// // delay(500);
// // // micro-ROS over USB-CDC
// // set_microros_serial_transports(Serial);
// // delay(2000);
// // Serial.println(">> micro-ROS transport ready");
// // // init support & node
// // std_msgs__msg__String__init(&sub_str);
// // std_msgs__msg__String__init(&dbg_str);
// // alloc = rcl_get_default_allocator();
// // rclc_support_init(&support, 0, NULL, &alloc);
// // rclc_node_init_default(&node, "esp32_delta", "", &support);
// // // subscription & publisher
// // rclc_subscription_init_default(
// // &sub, &node,
// // ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
// // "joint_angles_str"
// // );
// // rclc_publisher_init_default(
// // &dbg_pub, &node,
// // ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
// // "esp32_debug"
// // );
// // Serial.println(">> created node, sub & pub");
// // // executor
// // rclc_executor_init(&exe, &support.context, 1, &alloc);
// // rclc_executor_add_subscription(&exe, &sub, &sub_str, cb, ON_NEW_DATA);
// // Serial.println(">> micro-ROS setup complete");
// // }
// // void loop() {
// // rclc_executor_spin_some(&exe, RCL_MS_TO_NS(100));
// // delay(10);
// // }
// // // const char* WIFI_SSID = "Wifinity Home Network";
// // const char* WIFI_PASS = "hole3last1spend";
// // const char* AGENT_IP = "192.168.64.4";