control_effort_wrist.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # coding: utf-8
3 
4 # Copyright 2020 RT Corporation
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 import math
19 import rospy
20 from sensor_msgs.msg import JointState
21 from std_msgs.msg import Float64
22 
23 
24 class PIDController(object):
25  def __init__(self, p_gain, i_gain, d_gain):
26 
27  self._p_gain = p_gain
28  self._i_gain = i_gain
29  self._d_gain = d_gain
30 
31  self._error_1 = 0.0
32  self._error_2 = 0.0
33  self._output = 0.0
34 
35  def update(self, current, target):
36  error = target - current
37 
38  delta_output = self._p_gain * (error - self._error_1)
39  delta_output += self._i_gain * (error)
40  delta_output += self._d_gain * (error - 2*self._error_1 + self._error_2)
41 
42  self._output += delta_output
43 
44  self._error_2 = self._error_1
45  self._error_1 = error
46 
47  return self._output
48 
49 
50 def stop():
51  pub_wrist_current.publish(0.0)
52  print("TORQUE_OFF")
53 
54 joint_state = JointState()
56  global joint_state
57  joint_state = msg
58 
59 def main():
60  global joint_state
61 
62  # Sets P and D gain to avoid the wrist oscillation.
63  pid_controller = PIDController(0.5, 0.0, 3.0)
64 
65  r = rospy.Rate(60)
66  start_time = rospy.Time.now().secs
67  target_angle = math.radians(90)
68  while not rospy.is_shutdown():
69  if len(joint_state.position) < 7: # Wrist is the 7th joint.
70  continue
71 
72  wrist_angle = joint_state.position[6]
73 
74  # Switches the target angle every 5 seconds.
75  present_time = rospy.Time.now().secs
76  if present_time - start_time > 5:
77  start_time = present_time
78  target_angle *= -1.0
79 
80  target_effort = pid_controller.update(wrist_angle, target_angle)
81  pub_wrist_current.publish(target_effort)
82  r.sleep()
83 
84 
85 if __name__ == '__main__':
86  rospy.init_node("control_effort_wrist")
87 
88  sub_joint_state = rospy.Subscriber(
89  "/sciurus17/controller2/joint_states",
90  JointState,
91  joint_state_callback,
92  queue_size=1)
93 
94  pub_wrist_current = rospy.Publisher(
95  "/sciurus17/controller2/left_wrist_controller/command",
96  Float64,
97  queue_size=1)
98 
99  rospy.on_shutdown(stop)
100 
101  try:
102  if not rospy.is_shutdown():
103  main()
104  except rospy.ROSInterruptException:
105  pass
106 
control_effort_wrist.stop
def stop()
Definition: control_effort_wrist.py:50
control_effort_wrist.PIDController
Definition: control_effort_wrist.py:24
control_effort_wrist.PIDController._i_gain
_i_gain
Definition: control_effort_wrist.py:28
control_effort_wrist.PIDController._p_gain
_p_gain
Definition: control_effort_wrist.py:27
control_effort_wrist.PIDController._output
_output
Definition: control_effort_wrist.py:33
control_effort_wrist.PIDController._d_gain
_d_gain
Definition: control_effort_wrist.py:29
control_effort_wrist.PIDController._error_2
_error_2
Definition: control_effort_wrist.py:32
control_effort_wrist.PIDController._error_1
_error_1
Definition: control_effort_wrist.py:31
control_effort_wrist.PIDController.update
def update(self, current, target)
Definition: control_effort_wrist.py:35
control_effort_wrist.main
def main()
Definition: control_effort_wrist.py:59
control_effort_wrist.joint_state_callback
def joint_state_callback(msg)
Definition: control_effort_wrist.py:55
control_effort_wrist.PIDController.__init__
def __init__(self, p_gain, i_gain, d_gain)
Definition: control_effort_wrist.py:25


sciurus17_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Fri Aug 2 2024 08:37:20