joint_control.h
Go to the documentation of this file.
1 #ifndef DXL_JOINT_CONTROL_H
2 #define DXL_JOINT_CONTROL_H
3 
4 #include <cstdio>
5 #include <string>
9 #include <dynamic_reconfigure/server.h>
10 #include <crane_x7_msgs/ServoParameterConfig.h>
11 
12 // Protocol version
13 #define PROTOCOL_VERSION (2.0) // See which protocol version is used in the Dynamixel
14 
15 // DYNAMIXEL REGISTER TABLE (Dynamixel X)
16 typedef enum {
20 typedef struct {
21  std::string name; /* データ名称 */
22  uint32_t address; /* アドレス */
23  uint32_t length; /* データ長 */
24  uint32_t init_value; /* 初期値 */
25  EN_DXL_MEMTYPE type; /* メモリ種別 */
26  bool selfcheck; /* セルフチェック対象 */
28 
29 #define REG_LENGTH_BYTE (1)
30 #define REG_LENGTH_WORD (2)
31 #define REG_LENGTH_DWORD (4)
32 
33 typedef enum {
58 } EN_TABLE_ID;
59 
61  /* NAME ADDR LEN INIT TYPE */
62  { "RETURN_DELAY_TIME", 9, REG_LENGTH_BYTE, 250, enDXL_ROM, false },
63  { "DRIVE_MODE", 10, REG_LENGTH_BYTE, 0, enDXL_ROM, false },
64  { "OPERATION_MODE", 11, REG_LENGTH_BYTE, 3, enDXL_ROM, false },
65  { "HOMING_OFFSET", 20, REG_LENGTH_DWORD, 0, enDXL_ROM, false },
66  { "MOVING_THRESHOLD", 24, REG_LENGTH_DWORD, 10, enDXL_ROM, false },
67  { "TEMPRATURE_LIMIT", 31, REG_LENGTH_BYTE, 80, enDXL_ROM, false },
68  { "MAX_VOL_LIMIT", 32, REG_LENGTH_WORD, 160, enDXL_ROM, false },
69  { "MIN_VOL_LIMIT", 34, REG_LENGTH_WORD, 95, enDXL_ROM, false },
70  { "CURRENT_LIMIT", 38, REG_LENGTH_WORD, 1193, enDXL_ROM, false },
71  { "SHUTDOWN", 63, REG_LENGTH_BYTE, 52, enDXL_ROM, false },
72  { "TORQUE_ENABLE", 64, REG_LENGTH_BYTE, 0, enDXL_RAM, false },
73  { "VELOCITY_I_GAIN", 76, REG_LENGTH_WORD, 1920, enDXL_RAM, true },
74  { "VELOCITY_P_GAIN", 78, REG_LENGTH_WORD, 100, enDXL_RAM, true },
75  { "POSITION_D_GAIN", 80, REG_LENGTH_WORD, 0, enDXL_RAM, true },
76  { "POSITION_I_GAIN", 82, REG_LENGTH_WORD, 0, enDXL_RAM, true },
77  { "POSITION_P_GAIN", 84, REG_LENGTH_WORD, 800, enDXL_RAM, false },
78  { "BUS_WATCHDOG", 98, REG_LENGTH_BYTE, 0, enDXL_RAM, false },
79  { "GOAL_CURRENT", 102, REG_LENGTH_WORD, 0, enDXL_RAM, false },
80  { "GOAL_VELOCITY", 104, REG_LENGTH_DWORD, 0, enDXL_RAM, false },
81  { "GOAL_POSITION", 116, REG_LENGTH_DWORD, 0, enDXL_RAM, false },
82  { "PRESENT_CURRENT", 126, REG_LENGTH_WORD, 0, enDXL_RAM, false },
83  { "PRESENT_VELOCITY", 128, REG_LENGTH_DWORD, 0, enDXL_RAM, false },
84  { "PRESENT_POSITION", 132, REG_LENGTH_DWORD, 0, enDXL_RAM, false },
85  { "PRESENT_TEMPRATURE", 146, REG_LENGTH_BYTE, 0, enDXL_RAM, false },
86 };
87 
88 typedef struct ST_JOINT_PARAM
89 {
90  uint8_t dxl_id;
92  uint8_t drive_mode;
93  uint8_t operation_mode;
94  uint16_t moving_threshold;
95  int32_t homing_offset;
97  uint8_t max_vol_limit;
98  uint8_t min_vol_limit;
99  uint16_t current_limit;
100  uint8_t torque_enable;
101  uint16_t velocity_i_gain;
102  uint16_t velocity_p_gain;
103  uint16_t position_d_gain;
104  uint16_t position_i_gain;
105  uint16_t position_p_gain;
107 
108 // Parameter
109 #define BAUDRATE (3000000) // 通信速度
110 #define ADDR_RETURN_DELAY (RegTable[enTableId_ReturnDelay].address)
111 #define LEN_PRETURN_DELAY (RegTable[enTableId_ReturnDelay].length)
112 #define ADDR_DRIVE_MODE (RegTable[enTableId_DriveMode].address)
113 #define LEN_DRIVE_MODE (RegTable[enTableId_DriveMode].length)
114 #define ADDR_OPE_MODE (RegTable[enTableId_OpeMode].address)
115 #define LEN_OPE_MODE (RegTable[enTableId_OpeMode].length)
116 #define ADDR_HOMING_OFFSET (RegTable[enTableId_HomingOffset].address)
117 #define LEN_HOMING_OFFSET (RegTable[enTableId_HomingOffset].length)
118 #define ADDR_MOVING_THRESHOLD (RegTable[enTableId_MovingThreshold].address)
119 #define LEN_MOVING_THRESHOLD (RegTable[enTableId_MovingThreshold].length)
120 #define ADDR_TEMPRATURE_LIMIT (RegTable[enTableId_TempLimit].address)
121 #define LEN_TEMPRATURE_LIMIT (RegTable[enTableId_TempLimit].length)
122 #define ADDR_MAX_VOL_LIMIT (RegTable[enTableId_MaxVolLimit].address)
123 #define LEN_MAX_VOL_LIMIT (RegTable[enTableId_MaxVolLimit].length)
124 #define ADDR_MIN_VOL_LIMIT (RegTable[enTableId_MinVolLimit].address)
125 #define LEN_MIN_VOL_LIMIT (RegTable[enTableId_MinVolLimit].length)
126 #define ADDR_CURRENT_LIMIT (RegTable[enTableId_CurrentLimit].address)
127 #define LEN_CURRENT_LIMIT (RegTable[enTableId_CurrentLimit].length)
128 #define ADDR_TORQUE_ENABLE (RegTable[enTableId_TorqueEnable].address) // Control table address is different in Dynamixel model
129 #define ADDR_VELOCITY_IGAIN (RegTable[enTableId_VelocityIGain].address)
130 #define LEN_VELOCITY_IGAIN (RegTable[enTableId_VelocityIGain].length)
131 #define ADDR_VELOCITY_PGAIN (RegTable[enTableId_VelocityPGain].address)
132 #define LEN_VELOCITY_PGAIN (RegTable[enTableId_VelocityPGain].length)
133 #define ADDR_POSITION_DGAIN (RegTable[enTableId_PositionDGain].address)
134 #define LEN_POSITION_DGAIN (RegTable[enTableId_PositionDGain].length)
135 #define ADDR_POSITION_IGAIN (RegTable[enTableId_PositionIGain].address)
136 #define LEN_POSITION_IGAIN (RegTable[enTableId_PositionIGain].length)
137 #define ADDR_POSITION_PGAIN (RegTable[enTableId_PositionPGain].address)
138 #define LEN_POSITION_PGAIN (RegTable[enTableId_PositionPGain].length)
139 #define ADDR_BUS_WATCHDOG (RegTable[enTableId_BusWatchdog].address)
140 #define LEN_BUS_WATCHDOG (RegTable[enTableId_BusWatchdog].length)
141 #define ADDR_GOAL_CURRENT (RegTable[enTableId_GoalCurrent].address) // ゴールカレントアドレス
142 #define LEN_GOAL_CURRENT (RegTable[enTableId_GoalCurrent].length) // ゴールカレント
143 #define ADDR_GOAL_POSITION (RegTable[enTableId_GoalPosition].address) // ゴールポジションアドレス
144 #define LEN_GOAL_POSITION (RegTable[enTableId_GoalPosition].length) // ゴールポジション
145 #define ADDR_PRESENT_CURRENT (RegTable[enTableId_PresentCurrent].address)
146 #define LEN_PRESENT_CURRENT (RegTable[enTableId_PresentCurrent].length)
147 #define ADDR_PRESENT_VEL (RegTable[enTableId_PresentVelocity].address)
148 #define LEN_PRESENT_VEL (RegTable[enTableId_PresentVelocity].length)
149 #define ADDR_PRESENT_POSITION (RegTable[enTableId_PresentPosition].address)
150 #define LEN_PRESENT_POSITION (RegTable[enTableId_PresentPosition].length)
151 #define ADDR_PRESENT_TEMP (RegTable[enTableId_PresentTemp].address)
152 #define LEN_PRESENT_TEMP (RegTable[enTableId_PresentTemp].length)
153 
154 #define ADDR_PRESENT_MOVEMENT (ADDR_PRESENT_CURRENT)
155 #define LEN_PRESENT_MOVEMENT (LEN_PRESENT_CURRENT+LEN_PRESENT_VEL+LEN_PRESENT_POSITION)
156 
157 #define TORQUE_ENABLE (1) // Value for enabling the torque
158 #define TORQUE_DISABLE (0) // Value for disabling the torque
159 
160 #define POSITION_STEP (4096.0)
161 #define DXL_MIN_LIMIT (0.0)
162 #define DXL_MAX_LIMIT (4095.0)
163 #define DXL_CURRENT_UNIT (2.69) // mA
164 #define DXL_EFFORT_CONST (1.79)
165 #define DXL_TEMP_READ_DURATION (5)
166 #define DXL_PGAIN_MAX (16383)
167 #define DXL_DEFAULT_PGAIN (800)
168 #define DXL_FREE_PGAIN (0)
169 #define DXL_FREE_IGAIN (0)
170 #define DXL_FREE_DGAIN (0)
171 
172 #define DXL_OFFSET_DEFAULT (2048)
173 
174 #define DXL_TORQUE_ON_TIME (5*1000) // msec
175 #define DXL_TORQUR_ON_STEP (20) // Hz
176 #define DXL_TORQUE_ON_STEP_MAX (DXL_TORQUE_ON_TIME / (1000 / DXL_TORQUR_ON_STEP))
177 
178 #define DXL_CURRENT2EFFORT(c,coef) (DXL_CURRENT_UNIT * (c) * (coef) * 0.001)// (mA*CONST*0.001)=Nm
179 #define EFFORT2DXL_CURRENT(e,coef) ((e) / (coef) / 0.001 / DXL_CURRENT_UNIT)// (Nm/CONST/0.001)=mA
180 #define DEFAULT_MAX_EFFORT (5.0)
181 #define EFFORT_LIMITING_CNT (10)
182 #define DXL_VELOCITY2RAD_S(v) ((v) * 0.229 * 0.1047)
183 
184 #define DXL_WATCHDOG_RESET_VALUE (0)
185 #define MSEC2DXL_WATCHDOG(msec) ((uint8_t)(msec) / 20)
186 
187 #define OPERATING_MODE_CURRENT (0)
188 #define OPERATING_MODE_VELOCITY (1)
189 #define OPERATING_MODE_POSITION (3)
190 #define OPERATING_MODE_EXT_POS (4)
191 #define OPERATING_MODE_CURR_POS (5)
192 #define OPERATING_MODE_PWM (16)
193 
194 
195 // Joint control class
197 {
198 public:
199  JOINT_CONTROL(void);
200  JOINT_CONTROL( std::string init_name, uint8_t init_dxlid, uint16_t init_center, uint16_t init_home, double init_eff_const, uint8_t init_mode );
201  JOINT_CONTROL( const JOINT_CONTROL &src );
202  ~JOINT_CONTROL(void){ /* Nothing todo... */ }
203  void init_parameter( std::string init_name, uint8_t init_dxlid, uint16_t init_center, uint16_t init_home, double init_eff_const, uint8_t init_mode );
204  void set_joint_name( std::string set_name ) { name = set_name; }
205  void set_dxl_id( uint8_t set_id ){ id = set_id; }
206  void set_position( double set_rad ){ pos = set_rad; }
207  void set_velocity( double set_vel ){ vel = set_vel; }
208  void set_effort( double set_eff ){ eff = set_eff; }
209  void set_command( double set_cmd ){ cmd = set_cmd; }
210  void set_torque( bool set_trq ){ torque = set_trq; }
211  void set_center( uint16_t set_center ){ center = set_center; }
212  void set_home( uint16_t set_home ){ home = set_home; }
213  void set_current( double set_curr ){ curr = set_curr; }
214  void set_temprature( double set_temp ){ temp = set_temp; }
215  void set_connect( bool set_connect ){ connect = set_connect; }
216  void set_dxl_pos( uint32_t set_dxl_pos ){ dxl_pos = set_dxl_pos; }
217  void set_dxl_curr( uint16_t set_dxl_curr ){ dxl_curr = set_dxl_curr; }
218  void set_dxl_temp( uint8_t set_dxl_temp ){ dxl_temp = set_dxl_temp; }
219  //void set_dxl_goal( uint32_t set_dxl_goal ){ dxl_goal = set_dxl_goal; }/* TBD */
221  void set_eff_limiting( bool set_limiting ){ eff_limiting = set_limiting; }
222  void inc_eff_over( void ){ ++eff_over_cnt; }
223  void clear_eff_over( void ){ eff_over_cnt = 0; }
224  void set_joint_param( ST_JOINT_PARAM set_param){ param = set_param; }
225 
226  std::string get_joint_name( void ) { return name; }
227  uint8_t get_dxl_id( void ){ return id; }
228  double get_position( void ){ return pos; }
229  double* get_position_addr( void ){ return &pos; }
230  double get_velocity( void ){ return vel; }
231  double* get_velocity_addr( void ){ return &vel; }
232  double get_effort( void ){ return eff; }
233  double get_max_effort( void ){ return (limits.has_effort_limits?limits.max_effort:DEFAULT_MAX_EFFORT); }
234  double* get_effort_addr( void ){ return &eff; }
235  double get_command( void ){ return cmd; }
236  double* get_command_addr( void ){ return &cmd; }
237  bool get_torque( void ){ return torque; }
238  uint16_t get_center( void ){ return center; }
239  uint16_t get_home( void ){ return home; }
240  double get_current( void ){ return curr; }
241  double get_temprature( void ){ return temp; }
242  bool is_connect( void ){ return connect; }
243  uint32_t get_dxl_pos( void ){ return dxl_pos; }
244  uint16_t get_dxl_curr( void ){ return dxl_curr; }
245  uint8_t get_dxl_temp( void ){ return dxl_temp; }
246  uint8_t get_dxl_goal( void ){ return (uint32_t)(dxl_goal[0]&0x000000FF)
247  | (uint32_t)((dxl_goal[1]<<8)&0x0000FF00)
248  | (uint32_t)((dxl_goal[2]<<16)&0x00FF0000)
249  | (uint32_t)((dxl_goal[3]<<24)&0xFF0000FF); }
250  uint8_t* get_dxl_goal_addr( void ){ return dxl_goal; }
251  bool is_effort_limiting( void ){ return eff_limiting; }
252  uint8_t get_eff_over_cnt( void ){ return eff_over_cnt; }
253  double get_eff_const( void ){ return eff_const; }
254  uint8_t get_ope_mode( void ){ return ope_mode; }
256  void updt_d_command( double val ){ d_cmd = (val - prev_cmd); prev_cmd = val; }
257  double get_d_command( void ){ return d_cmd; }
258 
259 private:
260  std::string name; // ROS joint name
261  uint8_t id; // Dynamixel ServoID
262  double pos; // Present position
263  double vel; // Present verocity
264  double eff; // Present effort
265  double cmd; // ROS HwInterface value
266  double d_cmd; // cmd value delta
267  double prev_cmd; // Previous cmd value
268  double curr; // Present current[mA]
269  double temp; // Present temprature
270  bool torque; // Servo torque
271  uint16_t center; // Servo center position offset( dynamixel position value, default:2048 )
272  uint16_t home; // Servo home position( dynamixel position value )
273  bool connect; // Servo connect status
274  double eff_const; // Servo effort constant
275  bool eff_limiting;// Effort limiting status
276  uint8_t eff_over_cnt;// Effort limiting status
277  uint8_t ope_mode; // Operating mode
278 
279  double goal_pos; // Goal position[rad]
280  double goal_vel; // Goal velocity
281  double goal_eff; // Goal effort
282 
283  uint32_t dxl_pos; // Dynamixel present position
284  uint16_t dxl_curr; // Dynamixel current
285  uint8_t dxl_temp; // Dynamixel temprature
286  uint8_t dxl_goal[4];// Dynamixel goal position
287 
288  joint_limits_interface::JointLimits limits;// Joint limit parameters
289 
291 };
292 #endif /*DXL_JOINT_CONTROL_H */
uint8_t * get_dxl_goal_addr(void)
bool get_torque(void)
void clear_eff_over(void)
bool is_connect(void)
bool param(const std::string &param_name, T &param_val, const T &default_val)
uint16_t current_limit
Definition: joint_control.h:99
void set_velocity(double set_vel)
uint8_t get_dxl_temp(void)
void set_command(double set_cmd)
uint16_t center
void set_dxl_pos(uint32_t set_dxl_pos)
void set_joint_name(std::string set_name)
#define REG_LENGTH_DWORD
Definition: joint_control.h:31
int32_t homing_offset
Definition: joint_control.h:95
string cmd
uint8_t temprature_limit
Definition: joint_control.h:96
static const ST_DYNAMIXEL_REG_TABLE RegTable[]
Definition: joint_control.h:60
void set_joint_param(ST_JOINT_PARAM set_param)
uint8_t torque_enable
void set_temprature(double set_temp)
uint16_t dxl_curr
void set_connect(bool set_connect)
uint16_t moving_threshold
Definition: joint_control.h:94
ST_JOINT_PARAM param
uint16_t position_p_gain
uint8_t get_dxl_goal(void)
uint8_t ope_mode
uint32_t dxl_pos
double * get_command_addr(void)
std::string get_joint_name(void)
double get_effort(void)
double * get_effort_addr(void)
EN_DXL_MEMTYPE
Definition: joint_control.h:16
void set_effort(double set_eff)
void inc_eff_over(void)
EN_TABLE_ID
Definition: joint_control.h:33
void set_eff_limiting(bool set_limiting)
double get_current(void)
uint16_t get_home(void)
uint8_t get_ope_mode(void)
#define REG_LENGTH_WORD
Definition: joint_control.h:30
uint16_t position_i_gain
uint8_t max_vol_limit
Definition: joint_control.h:97
void set_dxl_curr(uint16_t set_dxl_curr)
void set_torque(bool set_trq)
double get_max_effort(void)
double get_command(void)
uint16_t get_dxl_curr(void)
uint16_t velocity_p_gain
EN_DXL_MEMTYPE type
Definition: joint_control.h:25
uint8_t min_vol_limit
Definition: joint_control.h:98
void set_dxl_temp(uint8_t set_dxl_temp)
#define DEFAULT_MAX_EFFORT
uint8_t return_delay_time
Definition: joint_control.h:91
uint16_t velocity_i_gain
double get_position(void)
void set_home(uint16_t set_home)
double get_velocity(void)
struct ST_JOINT_PARAM ST_JOINT_PARAM
uint16_t position_d_gain
void set_center(uint16_t set_center)
double * get_position_addr(void)
void updt_d_command(double val)
uint8_t get_dxl_id(void)
#define REG_LENGTH_BYTE
Definition: joint_control.h:29
uint8_t eff_over_cnt
void set_position(double set_rad)
uint8_t drive_mode
Definition: joint_control.h:92
double get_eff_const(void)
uint16_t get_center(void)
uint32_t get_dxl_pos(void)
~JOINT_CONTROL(void)
uint8_t dxl_temp
uint8_t operation_mode
Definition: joint_control.h:93
void set_dxl_id(uint8_t set_id)
bool is_effort_limiting(void)
double get_d_command(void)
std::string name
void set_current(double set_curr)
joint_limits_interface::JointLimits limits
void set_limits(joint_limits_interface::JointLimits &set_limits)
double * get_velocity_addr(void)
double get_temprature(void)
ST_JOINT_PARAM get_joint_param(void)
uint8_t get_eff_over_cnt(void)


crane_x7_control
Author(s): Hiroyuki Nomura , Geoffrey Biggs
autogenerated on Mon Mar 1 2021 03:18:36