single_dynamixel_controller.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Taehun Lim (Darby) */
18 
20 
21 using namespace single_dynamixel_controller;
22 
24 {
25  // init Service Client
26  dynamixel_info_client_ = node_handle_.serviceClient<dynamixel_workbench_msgs::GetDynamixelInfo>("dynamixel/info");
27  dynamixel_command_client_ = node_handle_.serviceClient<dynamixel_workbench_msgs::DynamixelCommand>("dynamixel/command");
28 }
29 
31 {
32 
33 }
34 
36 {
37 
38 }
39 
41 {
42  ros::shutdown();
43  return true;
44 }
45 
47 {
48  struct termios oldt, newt;
49  int ch;
50 
51  tcgetattr( STDIN_FILENO, &oldt );
52  newt = oldt;
53  newt.c_lflag &= ~(ICANON | ECHO);
54  newt.c_cc[VMIN] = 0;
55  newt.c_cc[VTIME] = 1;
56  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
57  ch = getchar();
58  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
59 
60  return ch;
61 }
62 
64 {
65  struct termios oldt, newt;
66  int ch;
67  int oldf;
68 
69  tcgetattr(STDIN_FILENO, &oldt);
70  newt = oldt;
71  newt.c_lflag &= ~(ICANON | ECHO);
72  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
73  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
74  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
75 
76  ch = getchar();
77 
78  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
79  fcntl(STDIN_FILENO, F_SETFL, oldf);
80 
81  if (ch != EOF)
82  {
83  ungetc(ch, stdin);
84  return 1;
85  }
86  return 0;
87 }
88 
90 {
91  printf("----------------------------------------------------------------------\n");
92  printf("Single Manager supports GUI (dynamixel_workbench_single_manager_gui) \n");
93  printf("----------------------------------------------------------------------\n");
94  printf("Command list :\n");
95  printf("[help|h|?]...............: help\n");
96  printf("[info]...................: information of a Dynamixel\n");
97  printf("[table]..................: check a control table of a Dynamixel\n");
98  printf("[torque_on]..............: torque on Dynamixel\n");
99  printf("[torque_off].............: torque off Dynamixel\n");
100  printf("[joint] [data]...........: set data to goal position address\n");
101  printf("[wheel] [data]...........: set data to goal position address\n");
102  printf("[id] [new id]............: change id\n");
103  printf("[baud] [new baud]........: change baud rate\n");
104  printf("[version] [new version]..: change protocol version\n");
105  printf("[reboot].................: reboot a Dynamixel(only protocol version 2.0)\n");
106  printf("[reset]..................: command for all data back to factory settings values\n");
107  printf("[address name] [data]....: change address value of a Dynamixel\n");
108  printf("[exit]...................: shutdown\n");
109  printf("----------------------------------------------------------------------\n");
110  printf("Press Enter Key To Command A Dynamixel\n");
111 }
112 
113 bool SingleDynamixelController::sendCommandMsg(std::string cmd, std::string addr, int64_t value)
114 {
115  dynamixel_workbench_msgs::DynamixelCommand set_dynamixel_command;
116 
117  set_dynamixel_command.request.command = cmd;
118  set_dynamixel_command.request.addr_name = addr;
119  set_dynamixel_command.request.value = value;
120 
121  if (dynamixel_command_client_.call(set_dynamixel_command))
122  {
123  if (!set_dynamixel_command.response.comm_result)
124  return false;
125  else
126  return true;
127  }
128 }
129 
131 {
132  char input[128];
133  char cmd[80];
134  char param[20][30];
135  int num_param = 0;
136  char *token;
137  bool valid_cmd = false;
138 
139  if (kbhit())
140  {
141  if (getchar() == ENTER_ASCII_VALUE)
142  {
143  viewManagerMenu();
144 
145  printf("[CMD]");
146  fgets(input, sizeof(input), stdin);
147 
148  char *p;
149  if ((p = strchr(input, '\n'))!= NULL) *p = '\0';
150  fflush(stdin);
151 
152  if (strlen(input) == 0) return false;
153 
154  token = strtok(input, " ");
155 
156  if (token == 0) return false;
157 
158  strcpy(cmd, token);
159  token = strtok(0, " ");
160  num_param = 0;
161 
162  while (token != 0)
163  {
164  strcpy(param[num_param++], token);
165  token = strtok(0, " ");
166  }
167 
168  if (strcmp(cmd, "help") == 0 || strcmp(cmd, "h") == 0 || strcmp(cmd, "?") == 0)
169  {
170  viewManagerMenu();
171  }
172  else if (strcmp(cmd, "info") == 0)
173  {
174  dynamixel_workbench_msgs::GetDynamixelInfo get_dynamixel_info;
175 
176  if (dynamixel_info_client_.call(get_dynamixel_info))
177  {
178  printf("[ID] %u, [Model Name] %s, [Protocol Version] %1.f.0, [BAUD RATE] %ld\n", get_dynamixel_info.response.dynamixel_info.model_id,
179  get_dynamixel_info.response.dynamixel_info.model_name.c_str(),
180  get_dynamixel_info.response.dynamixel_info.load_info.protocol_version,
181  get_dynamixel_info.response.dynamixel_info.load_info.baud_rate);
182  }
183  }
184  else if (strcmp(cmd, "exit") == 0)
185  {
186  if (sendCommandMsg("exit"))
188 
189  return true;
190  }
191  else if (strcmp(cmd, "table") == 0)
192  {
193  if (!sendCommandMsg("table"))
194  printf("It didn't load DYNAMIXEL Control Table\n");
195  }
196  else if (strcmp(cmd, "reboot") == 0)
197  {
198  if (!sendCommandMsg("reboot"))
199  printf("It didn't reboot to DYNAMIXEL\n");
200  }
201  else if (strcmp(cmd, "reset") == 0)
202  {
203  if (!sendCommandMsg("factory_reset"))
204  printf("It didn't factory reset to DYNAMIXEL\n");
205  }
206  else if (strcmp(cmd, "torque_on") == 0)
207  {
208  if (!sendCommandMsg("torque", "on", 1))
209  printf("It didn't works\n");
210  else
211  printf("Torque On");
212  }
213  else if (strcmp(cmd, "torque_off") == 0)
214  {
215  if (!sendCommandMsg("torque", "off", 0))
216  printf("It didn't works\n");
217  else
218  printf("Torque Off");
219  }
220  else if (strcmp(cmd, "id") == 0)
221  {
222  if (!sendCommandMsg("addr", "ID", atoi(param[0])))
223  printf("It didn't works\n");
224  }
225  else if (strcmp(cmd, "baud") == 0)
226  {
227  if (!sendCommandMsg("addr", "Baud_Rate", atoi(param[0])))
228  printf("It didn't works\n");
229  }
230  else if (strcmp(cmd, "version") == 0)
231  {
232  if (!sendCommandMsg("addr", "Protocol_Version", atof(param[0])))
233  printf("It didn't works\n");
234  }
235  else if (strcmp(cmd, "joint") == 0)
236  {
237  if (!sendCommandMsg("Joint", "", atoi(param[0])))
238  printf("It didn't works\n");
239  }
240  else if (strcmp(cmd, "wheel") == 0)
241  {
242  if (!sendCommandMsg("Wheel", "", atoi(param[0])))
243  printf("It didn't works\n");
244  }
245  else if (num_param == 1)
246  {
247  if (sendCommandMsg("addr", cmd, atoi(param[0])))
248  printf("It works!!\n");
249  else
250  printf("It didn't works!!\n");
251  }
252  else
253  {
254  printf("Invalid command. Please check menu[help, h, ?]\n");
255  }
256  }
257  }
258 }
259 
260 int main(int argc, char **argv)
261 {
262  // Init ROS node
263  ros::init(argc, argv, "single_dynamixel_controller");
265  ros::Rate loop_rate(250);
266 
267  while (ros::ok())
268  {
269  single_dynamixel_controller.controlLoop();
270  ros::spinOnce();
271  loop_rate.sleep();
272  }
273 
274  return 0;
275 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool param(const std::string &param_name, T &param_val, const T &default_val)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
#define ENTER_ASCII_VALUE
bool sendCommandMsg(std::string cmd, std::string addr="", int64_t value=0)
ROSCPP_DECL bool ok()
bool sleep()
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()
int main(int argc, char **argv)


dynamixel_workbench_single_manager
Author(s): Darby Lim
autogenerated on Mon Jun 10 2019 13:06:13