6
6
* INCLUDE
7
7
**************************************************************************************/
8
8
9
+ #include < Braccio++.h>
10
+
9
11
#include < micro_ros_arduino.h>
10
12
11
13
#include < stdio.h>
16
18
17
19
#include < sensor_msgs/msg/joint_state.h>
18
20
21
+ #include < rosidl_runtime_c/string_functions.h>
22
+
23
+ #include < vector>
24
+ #include < algorithm>
25
+
19
26
/* *************************************************************************************
20
27
* GLOBAL VARIABLES
21
28
**************************************************************************************/
@@ -47,8 +54,11 @@ void setup()
47
54
48
55
pinMode (LED_PIN, OUTPUT);
49
56
digitalWrite (LED_PIN, HIGH);
50
-
51
- delay (2000 );
57
+
58
+ if (!Braccio.begin ()) {
59
+ error_loop ();
60
+ }
61
+ Braccio.disengage ();
52
62
53
63
allocator = rcl_get_default_allocator ();
54
64
@@ -65,6 +75,32 @@ void setup()
65
75
ROSIDL_GET_MSG_TYPE_SUPPORT (sensor_msgs, msg, JointState),
66
76
" braccio_plusplus_node_joint_state_publisher" ));
67
77
78
+ /* Initialize JointState message. */
79
+ rosidl_runtime_c__String__init (&joint_state_msg.header .frame_id );
80
+ rosidl_runtime_c__String__assign (&joint_state_msg.header .frame_id , " /braccio_plusplus_joint_state" );
81
+
82
+ {
83
+ static rosidl_runtime_c__String joint_state_msg_name[SmartServoClass::NUM_MOTORS];
84
+ joint_state_msg.name .data = joint_state_msg_name;
85
+ joint_state_msg.name .size = SmartServoClass::NUM_MOTORS;
86
+ joint_state_msg.name .capacity = SmartServoClass::NUM_MOTORS;
87
+ std::vector<std::string> const JOINT_NAMES = {" base" , " shoulder" , " elbow" , " wrist_roll" , " wrist_pitch" , " pinch" };
88
+ int i = 0 ;
89
+ std::for_each (std::cbegin (JOINT_NAMES),
90
+ std::cend (JOINT_NAMES),
91
+ [&joint_state_msg, &i](std::string const & joint_name)
92
+ {
93
+ rosidl_runtime_c__String__init (&joint_state_msg.name .data [i]);
94
+ rosidl_runtime_c__String__assign (&joint_state_msg.name .data [i], joint_name.c_str ());
95
+ i++;
96
+ });
97
+ }
98
+
99
+ static double joint_state_msg_position[SmartServoClass::NUM_MOTORS] = {0 };
100
+ joint_state_msg.position .data = joint_state_msg_position;
101
+ joint_state_msg.position .size = SmartServoClass::NUM_MOTORS;
102
+ joint_state_msg.position .capacity = SmartServoClass::NUM_MOTORS;
103
+
68
104
/* Create Timer. */
69
105
const unsigned int timer_timeout = 100 ;
70
106
RCCHECK (rclc_timer_init_default (
@@ -102,6 +138,21 @@ void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
102
138
RCLC_UNUSED (last_call_time);
103
139
if (timer != NULL )
104
140
{
141
+ /* Retrieve current servo positions. */
142
+ float current_servo_pos[SmartServoClass::NUM_MOTORS] = {0 };
143
+ Braccio.positions (current_servo_pos);
144
+ /* Revert the array to fit with the names within the joint state message. */
145
+ std::reverse (current_servo_pos, current_servo_pos + SmartServoClass::NUM_MOTORS);
146
+
147
+ /* Populate header. */
148
+ joint_state_msg.header .stamp .sec = millis () / 1000 ;
149
+ joint_state_msg.header .stamp .nanosec = micros () * 1000 ;
150
+
151
+ /* Populate data. */
152
+ for (int i = 0 ; i < SmartServoClass::NUM_MOTORS; i++)
153
+ joint_state_msg.position .data [i] = current_servo_pos[i];
154
+
155
+ /* Publish message. */
105
156
RCSOFTCHECK (rcl_publish (&joint_state_publisher, &joint_state_msg, NULL ));
106
157
}
107
158
}
0 commit comments