Skip to content

Commit 3855ed7

Browse files
committed
Publish current joint angles in degree every 100 ms.
1 parent f910a0a commit 3855ed7

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

Diff for: examples/ROS2_Braccio_Driver/ROS2_Braccio_Driver.ino

+53-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* INCLUDE
77
**************************************************************************************/
88

9+
#include <Braccio++.h>
10+
911
#include <micro_ros_arduino.h>
1012

1113
#include <stdio.h>
@@ -16,6 +18,11 @@
1618

1719
#include <sensor_msgs/msg/joint_state.h>
1820

21+
#include <rosidl_runtime_c/string_functions.h>
22+
23+
#include <vector>
24+
#include <algorithm>
25+
1926
/**************************************************************************************
2027
* GLOBAL VARIABLES
2128
**************************************************************************************/
@@ -47,8 +54,11 @@ void setup()
4754

4855
pinMode(LED_PIN, OUTPUT);
4956
digitalWrite(LED_PIN, HIGH);
50-
51-
delay(2000);
57+
58+
if (!Braccio.begin()) {
59+
error_loop();
60+
}
61+
Braccio.disengage();
5262

5363
allocator = rcl_get_default_allocator();
5464

@@ -65,6 +75,32 @@ void setup()
6575
ROSIDL_GET_MSG_TYPE_SUPPORT(sensor_msgs, msg, JointState),
6676
"braccio_plusplus_node_joint_state_publisher"));
6777

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+
68104
/* Create Timer. */
69105
const unsigned int timer_timeout = 100;
70106
RCCHECK(rclc_timer_init_default(
@@ -102,6 +138,21 @@ void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
102138
RCLC_UNUSED(last_call_time);
103139
if (timer != NULL)
104140
{
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. */
105156
RCSOFTCHECK(rcl_publish(&joint_state_publisher, &joint_state_msg, NULL));
106157
}
107158
}

0 commit comments

Comments
 (0)