-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamixel_control.cpp
227 lines (192 loc) · 6.94 KB
/
dynamixel_control.cpp
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
/*****************************************************
* Header Includes *
*****************************************************/
#include <ros/ros.h>
#include "dynamixel_includes.h"
#include "dynamixel_control/ServoPosition.h"
/*****************************************************
* Prototypes *
*****************************************************/
static void set_servo_position(int32_t requested_degree);
static void Ros_Init(int argc, char **argv);
static bool Dynamixel_Init(void);
/*****************************************************
* File Scope Variables *
*****************************************************/
static uint8_t left_right_id;
static uint8_t forward_back_id;
static DynamixelWorkbench my_dynamixel;
static ros:: Subscriber sub;
/*****************************************************
* Definitions *
*****************************************************/
#define SERVO_DEGREE_MIN -130.0f
#define SERVO_DEGREE_MAX -50.0f
#define SERVO_DEGREE_CENTER -90.0f
#define RAD2DEG(x) x * (180.0/3.141592653589793238463)
#define DEG2RAD(x) x * (3.141592653589793238463/180.0)
/*****************************************************
* Callback for accepting *
* the servo position goal. *
*****************************************************/
void goalPositionCallback(dynamixel_control::ServoPosition requestedServoPosition)
{
float currentDegreeValue;
float requestedDegreeValue;
float servoRadValue;
float radianValue;
const char* log;
if(requestedServoPosition.servoSetCenter == 1)
{
set_servo_position(SERVO_DEGREE_CENTER);
}
else
{
my_dynamixel.getRadian(left_right_id, &radianValue, &log);
currentDegreeValue = RAD2DEG(radianValue);
/* Convert degrees to radians */
if(requestedServoPosition.servoClockWiseRotation == 0)
{
requestedDegreeValue = currentDegreeValue + requestedServoPosition.servoDegreeRotation;
}
else
{
requestedDegreeValue = currentDegreeValue - requestedServoPosition.servoDegreeRotation;
}
set_servo_position(requestedDegreeValue);
ROS_INFO("Requested final degree: %f", requestedDegreeValue);
}
}
/*****************************************************
* Initializations for ROS *
*****************************************************/
static void Ros_Init(int argc, char **argv)
{
/* Initialize the ROS system. */
ros::init(argc, argv, "dynamixel_control_rebecca");
/* Establish this program as a ROS node. */
ros::NodeHandle nh;
/* Set up the servo subscriber */
sub = nh.subscribe<dynamixel_control::ServoPosition>("dynamixel_control_rebecca", 100, goalPositionCallback);
}
/*****************************************************
* Initializations for the *
Dynamixels *
*****************************************************/
static bool Dynamixel_Init(void)
{
bool result;
uint32_t baud_rate;
const char* log;
const char* device_name;
/* Local inits */
result = false;
baud_rate = 1000000;
device_name = "/dev/ttyUSB0";
/* Initialize the necessary USB port for dynamixel communication */
if(my_dynamixel.init(device_name, baud_rate, &log) == true)
{
/* Ping Dynamixel to see if it is on */
if((my_dynamixel.ping(left_right_id, &log) == true) && (my_dynamixel.ping(forward_back_id, &log)))
{
/* Send some output as a log message. */
ROS_INFO_STREAM("Starting Dynamixel Node!");
/* Turn on the torque */
my_dynamixel.torqueOn(left_right_id, &log);
my_dynamixel.torqueOn(forward_back_id, &log);
result = true;
}
}
return(result);
}
/*****************************************************
* Set the requested
* position of the servo
*****************************************************/
static void set_servo_position(int32_t requested_degree)
{
float currentDegreeValue;
float requestedDegreeValue;
float servoRadValue;
float radianValue;
const char* log;
/* Bounds check */
if(requested_degree < SERVO_DEGREE_MIN)
{
requested_degree = SERVO_DEGREE_MIN;
}
else if(requested_degree > SERVO_DEGREE_MAX)
{
requested_degree = SERVO_DEGREE_MAX;
}
else
{
/* Do nothing */
}
/* Get the current position of the servo */
my_dynamixel.getRadian(left_right_id, &radianValue, &log);
currentDegreeValue = RAD2DEG(radianValue);
/* Determine which way to turn */
if(requested_degree > currentDegreeValue)
{
while(currentDegreeValue <= requested_degree)
{
currentDegreeValue = currentDegreeValue + 1;
/* Convert back to radians */
servoRadValue = DEG2RAD(currentDegreeValue);
/* Send the position to the servo */
my_dynamixel.goalPosition(left_right_id, servoRadValue, &log);
/* Get the new position */
my_dynamixel.getRadian(left_right_id, &radianValue, &log);
currentDegreeValue = RAD2DEG(radianValue);
}
}
else
{
while(currentDegreeValue >= requested_degree)
{
currentDegreeValue = currentDegreeValue - 1;
/* Convert back to radians */
servoRadValue = DEG2RAD(currentDegreeValue);
/* Send the position to the servo */
my_dynamixel.goalPosition(left_right_id, servoRadValue, &log);
/* Get the new position */
my_dynamixel.getRadian(left_right_id, &radianValue, &log);
currentDegreeValue = RAD2DEG(radianValue);
}
}
}
/*****************************************************
* Main Entry Point *
*****************************************************/
int main(int argc, char **argv)
{
const char* log;
float radianValue;
int32_t servoCurrentPosition;
/* Local Inits */
left_right_id = 2U;
forward_back_id = 1U;
/* Initialize ROS nodes and variables */
Ros_Init(argc, argv);
/* Set up the rate */
ros:: Rate rate(10);
/* Initialize the necessary USB port for dynamixel communication */
if(Dynamixel_Init() == true)
{
/* Start up servo centered */
set_servo_position(SERVO_DEGREE_CENTER);
/* Loop forever */
while(ros::ok())
{
/* Trigger callback to fire and grab latest data and update the servo */
ros::spinOnce();
/* Wait for some time */
rate.sleep();
}
}
else
{
ROS_ERROR("Couldn't connect to servo! Check that the USB is plugged in and the servo is powered on!");
}
}