Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
rx28.h
1 
2 /***************************************************************************
3  * rx28.h - Class for accessing Robotis RX28 servos
4  *
5  * Created: Tue Jun 16 11:09:32 2009 (based on visca.h)
6  * Copyright 2005-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __PLUGINS_PANTILT_ROBOTIS_RX28_H_
25 #define __PLUGINS_PANTILT_ROBOTIS_RX28_H_
26 
27 #include <core/exception.h>
28 
29 #ifdef USE_TIMETRACKER
30 # warning Visca time tracker enabled
31 # include <utils/timetracker.h>
32 # include <fstream>
33 #endif
34 
35 #include <list>
36 #include <cstddef>
37 #include <cstdio>
38 
39 #define RX28_CONTROL_TABLE_LENGTH 0x32
40 #define RX28_MAX_NUM_SERVOS 254
41 
43 {
44  public:
45  /** List of servo IDs. */
46  typedef std::list<unsigned char> DeviceList;
47 
48  RobotisRX28(const char *device_file, unsigned int default_timeout_ms = 30);
49  ~RobotisRX28();
50 
51  void open();
52  void close();
53 
54  bool ping(unsigned char id, unsigned int timeout_ms = 100);
55  DeviceList discover(unsigned int total_timeout_ms = 50);
56 
57 
58  void write_table_value(unsigned char id, unsigned char addr,
59  unsigned int value, bool double_byte = false);
60  void write_table_values(unsigned char id, unsigned char start_addr,
61  unsigned char *values, unsigned int num_values);
62  void read_table_values(unsigned char id);
63  void read_table_value(unsigned char id,
64  unsigned char addr, unsigned char read_length);
65  void start_read_table_values(unsigned char id);
67 
68  void goto_position(unsigned char id, unsigned int value);
69  void goto_positions(unsigned int num_positions, ...);
70 
71  unsigned int get_model(unsigned char id, bool refresh = false);
72  unsigned int get_position(unsigned char id, bool refresh = false);
73  unsigned char get_firmware_version(unsigned char id, bool refresh = false);
74  unsigned char get_baudrate(unsigned char id, bool refresh = false);
75  unsigned char get_delay_time(unsigned char id, bool refresh = false);
76  void get_angle_limits(unsigned char id,
77  unsigned int &cw_limit, unsigned int &ccw_limit,
78  bool refresh = false);
79  unsigned char get_temperature_limit(unsigned char id, bool refresh = false);
80  void get_voltage_limits(unsigned char id,
81  unsigned char &low, unsigned char &high,
82  bool refresh = false);
83  unsigned int get_max_torque(unsigned char id, bool refresh = false);
84  unsigned char get_status_return_level(unsigned char id, bool refresh = false);
85  unsigned char get_alarm_led(unsigned char id, bool refresh = false);
86  unsigned char get_alarm_shutdown(unsigned char id, bool refresh = false);
87  void get_calibration(unsigned char id,
88  unsigned int &down_calib, unsigned int &up_calib,
89  bool refresh = false);
90  bool is_torque_enabled(unsigned char id, bool refresh = false);
91  bool is_led_enabled(unsigned char id, bool refresh = false);
92  void get_compliance_values(unsigned char id,
93  unsigned char &cw_margin, unsigned char &cw_slope,
94  unsigned char &ccw_margin, unsigned char &ccw_slope,
95  bool refresh = false);
96  unsigned int get_goal_position(unsigned char id, bool refresh = false);
97  unsigned int get_goal_speed(unsigned char id, bool refresh = false);
98  float get_max_supported_speed(unsigned char id, bool refresh = false);
99  unsigned int get_torque_limit(unsigned char id, bool refresh = false);
100  unsigned int get_speed(unsigned char id, bool refresh = false);
101  unsigned int get_load(unsigned char id, bool refresh = false);
102  unsigned char get_voltage(unsigned char id, bool refresh = false);
103  unsigned char get_temperature(unsigned char id, bool refresh = false);
104  bool is_moving(unsigned char id, bool refresh = false);
105  bool is_locked(unsigned char id, bool refresh = false);
106  unsigned int get_punch(unsigned char id, bool refresh = false);
107 
108  void set_id(unsigned char id, unsigned char new_id);
109  void set_baudrate(unsigned char id, unsigned char baudrate);
110  void set_return_delay_time(unsigned char id, unsigned char return_delay_time);
111  void set_angle_limits(unsigned char id,
112  unsigned int cw_limit, unsigned int ccw_limit);
113  void set_temperature_limit(unsigned char id, unsigned char temp_limit);
114  void set_voltage_limits(unsigned char id, unsigned char low, unsigned char high);
115  void set_max_torque(unsigned char id, unsigned int max_torque);
116  void set_status_return_level(unsigned char id, unsigned char status_return_level);
117  void set_alarm_led(unsigned char id, unsigned char alarm_led);
118  void set_alarm_shutdown(unsigned char id, unsigned char alarm_shutdown);
119  void set_torque_enabled(unsigned char id, bool enabled);
120  void set_torques_enabled(bool enabled, unsigned char num_servos, ...);
121  void set_led_enabled(unsigned char id, bool enabled);
122  void set_compliance_values(unsigned char id,
123  unsigned char cw_margin, unsigned char cw_slope,
124  unsigned char ccw_margin, unsigned char ccw_slope);
125  void set_goal_speed(unsigned char id, unsigned int goal_speed);
126  void set_goal_speeds(unsigned int num_servos, ...);
127  void set_torque_limit(unsigned char id, unsigned int torque_limit);
128  void lock_config(unsigned char id);
129  void set_punch(unsigned char id, unsigned int punch);
130 
131  bool data_available();
132 
133  // Status return level
134  static const unsigned char SRL_RESPOND_NONE;
135  static const unsigned char SRL_RESPOND_READ;
136  static const unsigned char SRL_RESPOND_ALL;
137 
138  static const unsigned char BROADCAST_ID;
139  static const unsigned int MAX_POSITION;
140  static const unsigned int CENTER_POSITION;
141  static const float MAX_ANGLE_DEG;
142  static const float MAX_ANGLE_RAD;
143  static const float RAD_PER_POS_TICK;
144  static const float POS_TICKS_PER_RAD;
145  static const float SEC_PER_60DEG_12V;
146  static const float SEC_PER_60DEG_16V;
147  static const unsigned int MAX_SPEED;
148 
149  // Parameter entry offsets
150  static const unsigned char P_MODEL_NUMBER_L;
151  static const unsigned char P_MODEL_NUMBER_H;
152  static const unsigned char P_VERSION;
153  static const unsigned char P_ID;
154  static const unsigned char P_BAUD_RATE;
155  static const unsigned char P_RETURN_DELAY_TIME;
156  static const unsigned char P_CW_ANGLE_LIMIT_L;
157  static const unsigned char P_CW_ANGLE_LIMIT_H;
158  static const unsigned char P_CCW_ANGLE_LIMIT_L;
159  static const unsigned char P_CCW_ANGLE_LIMIT_H;
160  static const unsigned char P_SYSTEM_DATA2;
161  static const unsigned char P_LIMIT_TEMPERATURE;
162  static const unsigned char P_DOWN_LIMIT_VOLTAGE;
163  static const unsigned char P_UP_LIMIT_VOLTAGE;
164  static const unsigned char P_MAX_TORQUE_L;
165  static const unsigned char P_MAX_TORQUE_H;
166  static const unsigned char P_RETURN_LEVEL;
167  static const unsigned char P_ALARM_LED;
168  static const unsigned char P_ALARM_SHUTDOWN;
169  static const unsigned char P_OPERATING_MODE;
170  static const unsigned char P_DOWN_CALIBRATION_L;
171  static const unsigned char P_DOWN_CALIBRATION_H;
172  static const unsigned char P_UP_CALIBRATION_L;
173  static const unsigned char P_UP_CALIBRATION_H;
174 
175  static const unsigned char P_TORQUE_ENABLE;
176  static const unsigned char P_LED;
177  static const unsigned char P_CW_COMPLIANCE_MARGIN;
178  static const unsigned char P_CCW_COMPLIANCE_MARGIN;
179  static const unsigned char P_CW_COMPLIANCE_SLOPE;
180  static const unsigned char P_CCW_COMPLIANCE_SLOPE;
181  static const unsigned char P_GOAL_POSITION_L;
182  static const unsigned char P_GOAL_POSITION_H;
183  static const unsigned char P_GOAL_SPEED_L;
184  static const unsigned char P_GOAL_SPEED_H;
185  static const unsigned char P_TORQUE_LIMIT_L;
186  static const unsigned char P_TORQUE_LIMIT_H;
187  static const unsigned char P_PRESENT_POSITION_L;
188  static const unsigned char P_PRESENT_POSITION_H;
189  static const unsigned char P_PRESENT_SPEED_L;
190  static const unsigned char P_PRESENT_SPEED_H;
191  static const unsigned char P_PRESENT_LOAD_L;
192  static const unsigned char P_PRESENT_LOAD_H;
193  static const unsigned char P_PRESENT_VOLTAGE;
194  static const unsigned char P_PRESENT_TEMPERATURE;
195  static const unsigned char P_REGISTERED_INSTRUCTION;
196  static const unsigned char P_PAUSE_TIME;
197  static const unsigned char P_MOVING;
198  static const unsigned char P_LOCK;
199  static const unsigned char P_PUNCH_L;
200  static const unsigned char P_PUNCH_H;
201 
202  private:
203  // Instructions
204  static const unsigned char INST_PING;
205  static const unsigned char INST_READ;
206  static const unsigned char INST_WRITE;
207  static const unsigned char INST_REG_WRITE;
208  static const unsigned char INST_ACTION;
209  static const unsigned char INST_RESET;
210  static const unsigned char INST_DIGITAL_RESET;
211  static const unsigned char INST_SYSTEM_READ;
212  static const unsigned char INST_SYSTEM_WRITE;
213  static const unsigned char INST_SYNC_WRITE;
214  static const unsigned char INST_SYNC_REG_WRITE;
215 
216  // Packet offsets
217  static const unsigned char PACKET_OFFSET_ID;
218  static const unsigned char PACKET_OFFSET_LENGTH;
219  static const unsigned char PACKET_OFFSET_INST;
220  static const unsigned char PACKET_OFFSET_PARAM;
221  static const unsigned char PACKET_OFFSET_ERROR;
222 
223  private:
224  unsigned char calc_checksum(const unsigned char id, const unsigned char instruction,
225  const unsigned char *params, const unsigned char plength);
226  void send(const unsigned char id, const unsigned char instruction,
227  const unsigned char *params, const unsigned char plength);
228  void recv(const unsigned char exp_length, unsigned int timeout_ms = 0xFFFFFFFF);
229  void assert_valid_id(unsigned char id);
230  unsigned int merge_twobyte_value(unsigned int id,
231  unsigned char ind_l, unsigned char ind_h);
232  unsigned int get_value(unsigned int id, bool refresh,
233  unsigned int ind_l, unsigned int ind_h = 0xFFFFFFFF);
234  bool inline responds_read(unsigned int id)
235  {
236 
237  return ((__control_table[id][P_RETURN_LEVEL] == SRL_RESPOND_READ) ||
238  (__control_table[id][P_RETURN_LEVEL] == SRL_RESPOND_ALL));
239  }
240 
241  bool inline responds_all(unsigned int id)
242  {
243  return (__control_table[id][P_RETURN_LEVEL] == SRL_RESPOND_ALL);
244  }
245 
246  int __fd;
247  char *__device_file;
248  unsigned int __default_timeout_ms;
249 
250  unsigned char __obuffer[260];
251  unsigned char __ibuffer[260];
252 
253  int __obuffer_length;
254  int __ibuffer_length;
255 
256  char __control_table[RX28_MAX_NUM_SERVOS][RX28_CONTROL_TABLE_LENGTH];
257 
258 #ifdef TIMETRACKER_VISCA
259  fawkes::TimeTracker *tracker;
260  std::ofstream track_file;
261  unsigned int ttcls_pantilt_get_send;
262  unsigned int ttcls_pantilt_get_read;
263  unsigned int ttcls_pantilt_get_handle;
264  unsigned int ttcls_pantilt_get_interpret;
265 #endif
266 
267 };
268 
269 
270 
271 #endif