control_effort_wrist.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # coding: utf-8
3 
4 import math
5 import rospy
6 from sensor_msgs.msg import JointState
7 from std_msgs.msg import Float64
8 
9 
10 class PIDController(object):
11  def __init__(self, p_gain, i_gain, d_gain):
12 
13  self._p_gain = p_gain
14  self._i_gain = i_gain
15  self._d_gain = d_gain
16 
17  self._error_1 = 0.0
18  self._error_2 = 0.0
19  self._output = 0.0
20 
21  def update(self, current, target):
22  error = target - current
23 
24  delta_output = self._p_gain * (error - self._error_1)
25  delta_output += self._i_gain * (error)
26  delta_output += self._d_gain * (error - 2*self._error_1 + self._error_2)
27 
28  self._output += delta_output
29 
30  self._error_2 = self._error_1
31  self._error_1 = error
32 
33  return self._output
34 
35 
36 def stop():
37  pub_wrist_current.publish(0.0)
38  print("TORQUE_OFF")
39 
40 joint_state = JointState()
42  global joint_state
43  joint_state = msg
44 
45 def main():
46  global joint_state
47 
48  # Set P and D gain to avoid the wrist oscillation.
49  pid_controller = PIDController(0.5, 0.0, 3.0)
50 
51  r = rospy.Rate(60)
52  start_time = rospy.Time.now().secs
53  target_angle = math.radians(90)
54  while not rospy.is_shutdown():
55  if len(joint_state.position) < 7: # Wrist is the 7th joint.
56  continue
57 
58  wrist_angle = joint_state.position[6]
59 
60  # Switch the target angle every 5 seconds.
61  present_time = rospy.Time.now().secs
62  if present_time - start_time > 5:
63  start_time = present_time
64  target_angle *= -1.0
65 
66  target_effort = pid_controller.update(wrist_angle, target_angle)
67  pub_wrist_current.publish(target_effort)
68  r.sleep()
69 
70 
71 if __name__ == '__main__':
72  rospy.init_node("control_effort_wrist")
73 
74  sub_joint_state = rospy.Subscriber(
75  "/sciurus17/controller2/joint_states",
76  JointState,
77  joint_state_callback,
78  queue_size=1)
79 
80  pub_wrist_current = rospy.Publisher(
81  "/sciurus17/controller2/left_wrist_controller/command",
82  Float64,
83  queue_size=1)
84 
85  rospy.on_shutdown(stop)
86 
87  try:
88  if not rospy.is_shutdown():
89  main()
90  except rospy.ROSInterruptException:
91  pass
92 
def update(self, current, target)
def __init__(self, p_gain, i_gain, d_gain)


sciurus17_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Sun Oct 2 2022 02:21:39