1
+ import copy
2
+ from typing_extensions import (
3
+ Literal ,
4
+ )
5
+ from ..version import PiperSDKVersion
6
+
7
+ class C_PiperParamManager ():
8
+ _instance = None
9
+
10
+ def __new__ (cls , * args , ** kwargs ):
11
+ if cls ._instance is None :
12
+ cls ._instance = super ().__new__ (cls )
13
+ return cls ._instance
14
+
15
+ def __init__ (self ) -> None :
16
+ '''
17
+ |joint_name| limit(rad) | limit(angle) |
18
+ |----------| ---------- | ---------- |
19
+ |joint1 | [-2.618, 2.618] | [-150.0, 150.0] |
20
+ |joint2 | [0, 3.14] | [0, 180.0] |
21
+ |joint3 | [-2.967, 0] | [-170, 0] |
22
+ |joint4 | [-1.745, 1.745] | [-100.0, 100.0] |
23
+ |joint5 | [-1.22, 1.22] | [-70.0, 70.0] |
24
+ |joint6 | [-2.0944, 2.0944]| [-120.0, 120.0] |
25
+ '''
26
+ if not hasattr (self , "PIPER_PARAM" ):
27
+ self .__PIPER_PARAM_ORIGIN = {
28
+ "joint_limit" :{
29
+ "j1" : [- 2.618 , 2.618 ],
30
+ "j2" : [0 , 3.14 ],
31
+ "j3" : [- 2.967 , 0 ],
32
+ "j4" : [- 1.745 , 1.745 ],
33
+ "j5" : [- 1.22 , 1.22 ],
34
+ "j6" : [- 2.0944 , 2.0944 ],
35
+ },
36
+ "gripper_range" : [0.0 , 0.07 ],
37
+ "piper_sdk_version" : PiperSDKVersion .PIPER_SDK_CURRENT_VERSION
38
+ }
39
+ self .PIPER_PARAM = copy .deepcopy (self .__PIPER_PARAM_ORIGIN )
40
+
41
+ def ResetDefaultParam (self ):
42
+ self .PIPER_PARAM .update (copy .deepcopy (self .__PIPER_PARAM_ORIGIN ))
43
+
44
+ def GetPiperParamOrigin (self ):
45
+ return copy .deepcopy (self .__PIPER_PARAM_ORIGIN )
46
+
47
+ def GetCurrentPiperParam (self ):
48
+ return copy .deepcopy (self .PIPER_PARAM )
49
+
50
+ def GetCurrentPiperSDKVersion (self ):
51
+ return self .PIPER_PARAM ["piper_sdk_version" ]
52
+
53
+ def GetJointLimitParam (self ,
54
+ joint_name : Literal ["j1" , "j2" , "j3" , "j4" , "j5" , "j6" ]):
55
+ if joint_name not in ["j1" , "j2" , "j3" , "j4" , "j5" , "j6" ]:
56
+ raise ValueError (f'"joint_name" Value { joint_name } is not in ["j1", "j2", "j3", "j4", "j5", "j6"]' )
57
+ return self .PIPER_PARAM ["joint_limit" ][joint_name ][0 ], self .PIPER_PARAM ["joint_limit" ][joint_name ][1 ]
58
+
59
+ def GetGripperRangeParam (self ):
60
+ return self .PIPER_PARAM ["gripper_range" ][0 ], self .PIPER_PARAM ["gripper_range" ][1 ]
61
+
62
+ def SetJointLimitParam (self ,
63
+ joint_name : Literal ["j1" , "j2" , "j3" , "j4" , "j5" , "j6" ],
64
+ min_val : float ,
65
+ max_val : float ):
66
+ if joint_name not in ["j1" , "j2" , "j3" , "j4" , "j5" , "j6" ]:
67
+ raise ValueError (f'"joint_name" Value { joint_name } is not in ["j1", "j2", "j3", "j4", "j5", "j6"]' )
68
+ if max_val - min_val < 0 :
69
+ raise ValueError (f'max_val should be greater than min_val.' )
70
+ self .PIPER_PARAM ["joint_limit" ][joint_name ] = [min_val , max_val ]
71
+
72
+ def SetGripperRangeParam (self ,
73
+ min_val : float ,
74
+ max_val : float ):
75
+ if max_val - min_val < 0 :
76
+ raise ValueError (f'max_val should be greater than min_val.' )
77
+ self .PIPER_PARAM ["gripper_range" ] = [min_val , max_val ]
78
+
79
+ # a = C_PiperParamManager()
80
+ # print( a.GetCurrentPiperParam())
81
+ # a.SetGripperRangeParam(-20000,30000)
82
+ # a.SetJointLimitParam("j1",-20000,30000)
83
+ # print( a.GetCurrentPiperParam())
84
+ # a.ResetDefaultParam()
85
+ # print( a.GetCurrentPiperParam())
86
+
87
+ # # ✅ 测试单例模式
88
+ # manager1 = C_PiperParamManager()
89
+ # manager2 = C_PiperParamManager()
90
+
91
+ # print(manager1 is manager2) # ✅ True,确保是同一个实例
92
+
93
+ # # 修改 manager1 的参数
94
+ # manager1.SetGripperRangeParam(-500, 500)
95
+ # print(manager2.GetCurrentPiperParam()) # ✅ manager2 也受影响,说明是同一个实例
96
+
97
+ # # 复位参数
98
+ # manager2.ResetDefaultParam()
99
+ # print(manager1.GetCurrentPiperParam()) # ✅ manager1 也被复位,单例模式生效!
0 commit comments