better-actuator-models 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. better_actuator_models-1.0.0/LICENSE +202 -0
  2. better_actuator_models-1.0.0/PKG-INFO +82 -0
  3. better_actuator_models-1.0.0/README.md +55 -0
  4. better_actuator_models-1.0.0/bam/__init__.py +8 -0
  5. better_actuator_models-1.0.0/bam/actuator.py +317 -0
  6. better_actuator_models-1.0.0/bam/actuators.py +34 -0
  7. better_actuator_models-1.0.0/bam/drive_backdrive.py +66 -0
  8. better_actuator_models-1.0.0/bam/dynamixel/__init__.py +8 -0
  9. better_actuator_models-1.0.0/bam/dynamixel/actuator.py +129 -0
  10. better_actuator_models-1.0.0/bam/dynamixel/all_record.py +58 -0
  11. better_actuator_models-1.0.0/bam/dynamixel/dynamixel.py +307 -0
  12. better_actuator_models-1.0.0/bam/dynamixel/record.py +149 -0
  13. better_actuator_models-1.0.0/bam/erob/__init__.py +8 -0
  14. better_actuator_models-1.0.0/bam/erob/actuator.py +83 -0
  15. better_actuator_models-1.0.0/bam/erob/all_record.py +48 -0
  16. better_actuator_models-1.0.0/bam/erob/etherban.py +176 -0
  17. better_actuator_models-1.0.0/bam/erob/record.py +86 -0
  18. better_actuator_models-1.0.0/bam/erob/static.py +74 -0
  19. better_actuator_models-1.0.0/bam/feetech/__init__.py +8 -0
  20. better_actuator_models-1.0.0/bam/feetech/actuator.py +89 -0
  21. better_actuator_models-1.0.0/bam/feetech/all_record.py +51 -0
  22. better_actuator_models-1.0.0/bam/feetech/record.py +173 -0
  23. better_actuator_models-1.0.0/bam/fit.py +221 -0
  24. better_actuator_models-1.0.0/bam/jitter.py +39 -0
  25. better_actuator_models-1.0.0/bam/logs.py +105 -0
  26. better_actuator_models-1.0.0/bam/mae.py +107 -0
  27. better_actuator_models-1.0.0/bam/message.py +56 -0
  28. better_actuator_models-1.0.0/bam/mjlab.py +527 -0
  29. better_actuator_models-1.0.0/bam/model.py +332 -0
  30. better_actuator_models-1.0.0/bam/mujoco.py +221 -0
  31. better_actuator_models-1.0.0/bam/parameter.py +27 -0
  32. better_actuator_models-1.0.0/bam/params/erob80_100/m1.json +1 -0
  33. better_actuator_models-1.0.0/bam/params/erob80_100/m2.json +1 -0
  34. better_actuator_models-1.0.0/bam/params/erob80_100/m3.json +1 -0
  35. better_actuator_models-1.0.0/bam/params/erob80_100/m4.json +1 -0
  36. better_actuator_models-1.0.0/bam/params/erob80_100/m5.json +1 -0
  37. better_actuator_models-1.0.0/bam/params/erob80_100/m6.json +1 -0
  38. better_actuator_models-1.0.0/bam/params/erob80_50/m1.json +1 -0
  39. better_actuator_models-1.0.0/bam/params/erob80_50/m2.json +1 -0
  40. better_actuator_models-1.0.0/bam/params/erob80_50/m3.json +1 -0
  41. better_actuator_models-1.0.0/bam/params/erob80_50/m4.json +1 -0
  42. better_actuator_models-1.0.0/bam/params/erob80_50/m5.json +1 -0
  43. better_actuator_models-1.0.0/bam/params/erob80_50/m6.json +1 -0
  44. better_actuator_models-1.0.0/bam/params/feetech_sts3215_7_4V/m1.json +10 -0
  45. better_actuator_models-1.0.0/bam/params/mx106/m1.json +9 -0
  46. better_actuator_models-1.0.0/bam/params/mx106/m2.json +12 -0
  47. better_actuator_models-1.0.0/bam/params/mx106/m3.json +10 -0
  48. better_actuator_models-1.0.0/bam/params/mx106/m4.json +14 -0
  49. better_actuator_models-1.0.0/bam/params/mx106/m5.json +16 -0
  50. better_actuator_models-1.0.0/bam/params/mx106/m6.json +18 -0
  51. better_actuator_models-1.0.0/bam/params/mx64/m1.json +9 -0
  52. better_actuator_models-1.0.0/bam/params/mx64/m2.json +12 -0
  53. better_actuator_models-1.0.0/bam/params/mx64/m3.json +10 -0
  54. better_actuator_models-1.0.0/bam/params/mx64/m4.json +14 -0
  55. better_actuator_models-1.0.0/bam/params/mx64/m5.json +16 -0
  56. better_actuator_models-1.0.0/bam/params/mx64/m6.json +18 -0
  57. better_actuator_models-1.0.0/bam/params/xl320/m1.json +1 -0
  58. better_actuator_models-1.0.0/bam/params/xl320/m2.json +1 -0
  59. better_actuator_models-1.0.0/bam/params/xl320/m3.json +1 -0
  60. better_actuator_models-1.0.0/bam/params/xl320/m4.json +1 -0
  61. better_actuator_models-1.0.0/bam/params/xl320/m5.json +1 -0
  62. better_actuator_models-1.0.0/bam/params/xl320/m6.json +1 -0
  63. better_actuator_models-1.0.0/bam/params/xl330/m1.json +10 -0
  64. better_actuator_models-1.0.0/bam/params/xl330/m2.json +13 -0
  65. better_actuator_models-1.0.0/bam/params/xl330/m3.json +11 -0
  66. better_actuator_models-1.0.0/bam/params/xl330/m4.json +15 -0
  67. better_actuator_models-1.0.0/bam/params/xl330/m5.json +17 -0
  68. better_actuator_models-1.0.0/bam/params/xl330/m6.json +19 -0
  69. better_actuator_models-1.0.0/bam/plot.py +115 -0
  70. better_actuator_models-1.0.0/bam/process.py +54 -0
  71. better_actuator_models-1.0.0/bam/simulate.py +134 -0
  72. better_actuator_models-1.0.0/bam/testbench.py +75 -0
  73. better_actuator_models-1.0.0/bam/to_mujoco.py +39 -0
  74. better_actuator_models-1.0.0/bam/trajectory.py +160 -0
  75. better_actuator_models-1.0.0/bam/unitree/__init__.py +8 -0
  76. better_actuator_models-1.0.0/bam/unitree/actuator.py +81 -0
  77. better_actuator_models-1.0.0/better_actuator_models.egg-info/PKG-INFO +82 -0
  78. better_actuator_models-1.0.0/better_actuator_models.egg-info/SOURCES.txt +81 -0
  79. better_actuator_models-1.0.0/better_actuator_models.egg-info/dependency_links.txt +1 -0
  80. better_actuator_models-1.0.0/better_actuator_models.egg-info/requires.txt +11 -0
  81. better_actuator_models-1.0.0/better_actuator_models.egg-info/top_level.txt +1 -0
  82. better_actuator_models-1.0.0/pyproject.toml +43 -0
  83. better_actuator_models-1.0.0/setup.cfg +4 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,82 @@
1
+ Metadata-Version: 2.4
2
+ Name: better-actuator-models
3
+ Version: 1.0.0
4
+ Summary: BAM: Better Actuator Models is a Python library dedicated to the identification and simulation of advanced actuator models.
5
+ Author: Marc Duclusaud, Grégoire Passault
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Rhoban/bam
8
+ Project-URL: Repository, https://github.com/Rhoban/bam
9
+ Keywords: robot,robotics,actuator,simulation,friction
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.12
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: zmq
16
+ Requires-Dist: protobuf<4.0,>=3.20
17
+ Requires-Dist: numpy
18
+ Requires-Dist: dynamixel_sdk
19
+ Requires-Dist: optuna
20
+ Requires-Dist: cmaes
21
+ Requires-Dist: wandb
22
+ Requires-Dist: matplotlib
23
+ Requires-Dist: scipy
24
+ Requires-Dist: colorama
25
+ Requires-Dist: rustypot>=1.5.0
26
+ Dynamic: license-file
27
+
28
+ <p align="center">
29
+ <img src="docs/_static/BAM_logo.png" alt="BAM logo" width="60%">
30
+ </p>
31
+
32
+ # BAM: Better Actuator Models
33
+
34
+ Accurate models of servo actuators are essential for the simulation of robotic systems. It is particularly important while performing Reinforcement Learning (RL) on real robots, as the precision of the model impacts directly the transferability of the learned policy.
35
+
36
+ The friction model generally implemented in widely used simulators like MuJoCo or IsaacGym is the Coulomb-Viscous, which is too simplistic to accurately represent complex friction phenomena like Stribeck, load-dependence or quadratic effects (read [this article](https://arxiv.org/pdf/2410.08650v1) for more details).
37
+
38
+ **BAM** aims at making servo-actuator simulation more faithful by:
39
+
40
+ - proposing an identification pipeline to fit friction models from recorded trajectories,
41
+ - providing a set of extended friction models that capture complex friction phenomena,
42
+ - sharing a library of identified models for common servos:
43
+ - Dynamixel MX-64
44
+ - Dynamixel MX-106
45
+ - Dynamixel XL-320
46
+ - Dynamixel XL330-288-T
47
+ - eRob80:50
48
+ - eRob80:100
49
+ - Feetech STS3215
50
+ - providing a simple API to use these models in MuJoCo CPU and MuJoCo Warp.
51
+
52
+ ## 📖 Documentation
53
+
54
+ Please refer to the [documentation](https://TODO-REPLACE-WITH-DOCUMENTATION-URL) for more details on how to use BAM and the provided friction models.
55
+
56
+ ## 📚 Citation
57
+
58
+ If you use BAM or the friction models provided in this repository for your scientific work, please cite the following publication:
59
+
60
+ ```bibtex
61
+ @inproceedings{duclusaud2025extended,
62
+ title={Extended Friction Models for the Physics Simulation of Servo Actuators},
63
+ author={Duclusaud, Marc and Passault, Gr{\'e}goire and Padois, Vincent and Ly, Olivier},
64
+ booktitle={2025 IEEE International Conference on Robotics and Automation (ICRA)},
65
+ pages={12091--12097},
66
+ year={2025},
67
+ organization={IEEE}
68
+ }
69
+ ```
70
+
71
+ If you also wish to cite the software directly, you may use:
72
+
73
+ ```bibtex
74
+ @software{BAM,
75
+ title = {{BAM: Better Actuator Models}},
76
+ author = {Duclusaud, Marc and Passault, Grégoire},
77
+ license = {Apache-2.0},
78
+ url = {https://github.com/Rhoban/bam},
79
+ version = {0.0.1},
80
+ year = {2024}
81
+ }
82
+ ```
@@ -0,0 +1,55 @@
1
+ <p align="center">
2
+ <img src="docs/_static/BAM_logo.png" alt="BAM logo" width="60%">
3
+ </p>
4
+
5
+ # BAM: Better Actuator Models
6
+
7
+ Accurate models of servo actuators are essential for the simulation of robotic systems. It is particularly important while performing Reinforcement Learning (RL) on real robots, as the precision of the model impacts directly the transferability of the learned policy.
8
+
9
+ The friction model generally implemented in widely used simulators like MuJoCo or IsaacGym is the Coulomb-Viscous, which is too simplistic to accurately represent complex friction phenomena like Stribeck, load-dependence or quadratic effects (read [this article](https://arxiv.org/pdf/2410.08650v1) for more details).
10
+
11
+ **BAM** aims at making servo-actuator simulation more faithful by:
12
+
13
+ - proposing an identification pipeline to fit friction models from recorded trajectories,
14
+ - providing a set of extended friction models that capture complex friction phenomena,
15
+ - sharing a library of identified models for common servos:
16
+ - Dynamixel MX-64
17
+ - Dynamixel MX-106
18
+ - Dynamixel XL-320
19
+ - Dynamixel XL330-288-T
20
+ - eRob80:50
21
+ - eRob80:100
22
+ - Feetech STS3215
23
+ - providing a simple API to use these models in MuJoCo CPU and MuJoCo Warp.
24
+
25
+ ## 📖 Documentation
26
+
27
+ Please refer to the [documentation](https://TODO-REPLACE-WITH-DOCUMENTATION-URL) for more details on how to use BAM and the provided friction models.
28
+
29
+ ## 📚 Citation
30
+
31
+ If you use BAM or the friction models provided in this repository for your scientific work, please cite the following publication:
32
+
33
+ ```bibtex
34
+ @inproceedings{duclusaud2025extended,
35
+ title={Extended Friction Models for the Physics Simulation of Servo Actuators},
36
+ author={Duclusaud, Marc and Passault, Gr{\'e}goire and Padois, Vincent and Ly, Olivier},
37
+ booktitle={2025 IEEE International Conference on Robotics and Automation (ICRA)},
38
+ pages={12091--12097},
39
+ year={2025},
40
+ organization={IEEE}
41
+ }
42
+ ```
43
+
44
+ If you also wish to cite the software directly, you may use:
45
+
46
+ ```bibtex
47
+ @software{BAM,
48
+ title = {{BAM: Better Actuator Models}},
49
+ author = {Duclusaud, Marc and Passault, Grégoire},
50
+ license = {Apache-2.0},
51
+ url = {https://github.com/Rhoban/bam},
52
+ version = {0.0.1},
53
+ year = {2024}
54
+ }
55
+ ```
@@ -0,0 +1,8 @@
1
+ # Copyright 2025 Marc Duclusaud & Grégoire Passault
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at:
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
@@ -0,0 +1,317 @@
1
+ # Copyright 2025 Marc Duclusaud & Grégoire Passault
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at:
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ import numpy as np
10
+ from .testbench import Testbench
11
+ from bam.parameter import Parameter
12
+ from .message import yellow, print_parameter
13
+
14
+
15
+ class Actuator:
16
+ """Abstract base class for all BAM actuator models.
17
+
18
+ Subclasses implement the firmware control law and the motor torque equation
19
+ for a specific actuator family. An actuator is always attached to a
20
+ :class:`~bam.model.Model` via :meth:`~bam.model.Model.set_actuator`, which
21
+ calls :meth:`initialize` to create the motor parameters (``kt``, ``R``, …)
22
+ on the model.
23
+
24
+ :param testbench_class: Class used to instantiate the testbench when a log
25
+ is loaded (typically :class:`~bam.testbench.Pendulum`).
26
+ """
27
+
28
+ def __init__(self, testbench_class: Testbench):
29
+ self.testbench_class = testbench_class
30
+ self.testbench: Testbench | None = None
31
+
32
+ def set_model(self, model):
33
+ """Attach this actuator to a model and run :meth:`initialize`.
34
+
35
+ :param model: :class:`~bam.model.Model` instance to attach to.
36
+ """
37
+ self.model = model
38
+ self.initialize()
39
+
40
+ def reset(self):
41
+ pass
42
+
43
+ def load_log(self, log: dict):
44
+ """Called when a log is loaded.
45
+
46
+ Instantiates the testbench from the log metadata (mass, length, …)
47
+ and updates actuator settings (kp, vin) from the log.
48
+
49
+ :param log: Log dict as loaded from a processed JSON file.
50
+ """
51
+ self.testbench = self.testbench_class(log)
52
+
53
+ def initialize(self):
54
+ """Create motor parameters on the attached model.
55
+
56
+ Called automatically by :meth:`set_model`. Must create at minimum
57
+ ``self.model.kt`` and ``self.model.R`` as :class:`~bam.parameter.Parameter`
58
+ instances.
59
+ """
60
+ raise NotImplementedError
61
+
62
+ def control_unit(self) -> str:
63
+ """Return the physical unit of the control signal (e.g. ``"volts"``, ``"amps"``)."""
64
+ raise NotImplementedError
65
+
66
+ def compute_control(
67
+ self, q_target: float, q: float, dq: float, dt: float
68
+ ) -> float | None:
69
+ """Compute the control signal from the current state and target.
70
+
71
+ :param q_target: Target joint angle [rad].
72
+ :param q: Current joint angle [rad].
73
+ :param dq: Current joint velocity [rad/s].
74
+ :param dt: Timestep [s].
75
+ :returns: Control signal in the unit given by :meth:`control_unit`.
76
+ """
77
+ raise NotImplementedError
78
+
79
+ def compute_torque(
80
+ self, control: float | None, torque_enable: bool, q: float, dq: float
81
+ ) -> float:
82
+ """Compute the motor torque from the control signal and current state.
83
+
84
+ :param control: Control signal (volts, amps, or Nm depending on the actuator).
85
+ :param torque_enable: Whether the actuator is powered.
86
+ :param q: Current joint angle [rad].
87
+ :param dq: Current joint velocity [rad/s].
88
+ :returns: Motor torque [Nm].
89
+ """
90
+ raise NotImplementedError
91
+
92
+ def get_extra_inertia(self) -> float:
93
+ """Return the actuator's apparent inertia added to the load [kg·m²]."""
94
+ raise NotImplementedError
95
+
96
+ def to_mujoco(self):
97
+ raise NotImplementedError("This actuator doesn't support to_mujoco")
98
+
99
+
100
+ class DCMotorActuator(Actuator):
101
+ """Base class for DC motor actuators with a supply voltage and a P-gain.
102
+
103
+ Loads ``kp`` and ``vin`` from the log metadata when :meth:`load_log` is called.
104
+
105
+ :param testbench_class: Testbench class (see :class:`Actuator`).
106
+ :param vin: Default supply voltage [V].
107
+ :param kp: Default firmware proportional gain.
108
+ """
109
+
110
+ def __init__(
111
+ self,
112
+ testbench_class: Testbench,
113
+ vin: float,
114
+ kp: float,
115
+ ):
116
+ super().__init__(testbench_class)
117
+ self.vin = vin
118
+ self.kp = kp
119
+
120
+ def load_log(self, log: dict):
121
+ super().load_log(log)
122
+ self.kp = log["kp"]
123
+ if "vin" in log:
124
+ self.vin = log["vin"]
125
+
126
+ def initialize(self):
127
+ """Create ``kt`` and ``R`` parameters on the attached model."""
128
+ # Torque constant [Nm/A] or [V/(rad/s)]
129
+ self.model.kt = Parameter(1.0, 0.0, 10.0)
130
+
131
+ # Motor resistance [Ohm]
132
+ self.model.R = Parameter(1.0, 0.0, 10.0)
133
+
134
+
135
+ class VoltageControlledActuator(DCMotorActuator):
136
+ """Voltage-controlled servo with a firmware P-position controller.
137
+
138
+ Implements the control law
139
+ ``duty_cycle = clip(error_gain * kp * Δq, -max_pwm, max_pwm)``
140
+ and the DC motor torque equation with back-EMF.
141
+
142
+ :param testbench_class: Testbench class (see :class:`Actuator`).
143
+ :param vin: Supply voltage [V].
144
+ :param kp: Firmware proportional gain.
145
+ :param error_gain: Converts ``kp * Δq`` to a duty cycle in [−1, 1].
146
+ Depends on the servo's internal encoder resolution and gain scaling.
147
+ :param max_pwm: Maximum duty cycle magnitude (default 1.0).
148
+ """
149
+
150
+ def __init__(
151
+ self,
152
+ testbench_class: Testbench,
153
+ vin: float,
154
+ kp: float,
155
+ error_gain: float,
156
+ max_pwm: float = 1.0,
157
+ ):
158
+ super().__init__(testbench_class, vin, kp)
159
+ self.error_gain = error_gain
160
+ self.max_pwm = max_pwm
161
+
162
+ def control_unit(self) -> str:
163
+ return "volts"
164
+
165
+ def compute_control(
166
+ self, q_target: float, q: float, dq: float, dt: float
167
+ ) -> float | None:
168
+ """Compute the voltage command from position error.
169
+
170
+ :param q_target: Target joint angle [rad].
171
+ :param q: Current joint angle [rad].
172
+ :param dq: Current joint velocity [rad/s] (unused here).
173
+ :param dt: Timestep [s] (unused here).
174
+ :returns: Voltage [V] sent to the motor.
175
+ """
176
+ duty_cycle = (q_target - q) * self.kp * self.error_gain
177
+ duty_cycle = np.clip(duty_cycle, -self.max_pwm, self.max_pwm)
178
+
179
+ return self.vin * duty_cycle
180
+
181
+ def compute_torque(
182
+ self, control: float | None, torque_enable: bool, q: float, dq: float
183
+ ) -> float:
184
+ """Compute motor torque using the DC motor equation with back-EMF.
185
+
186
+ :math:`\\tau = k_t V / R - k_t^2 \\dot{q} / R`
187
+
188
+ :param control: Voltage [V].
189
+ :param torque_enable: If ``False``, returns zero torque.
190
+ :param q: Current joint angle [rad] (unused here).
191
+ :param dq: Current joint velocity [rad/s].
192
+ :returns: Motor torque [Nm].
193
+ """
194
+ volts = control
195
+ torque = self.model.kt.value * volts / self.model.R.value
196
+ torque -= (self.model.kt.value**2) * dq / self.model.R.value
197
+ return torque * torque_enable
198
+
199
+ def to_mujoco(self):
200
+ if self.vin == 0 or self.kp == 0:
201
+ print(yellow(f"WARNING: kp or vin are not set"))
202
+
203
+ kt = self.model.kt.value
204
+ R = self.model.R.value
205
+
206
+ kp = self.error_gain * self.kp * self.vin * self.max_pwm * kt / R
207
+ damping = self.model.friction_viscous.value + kt**2 / R
208
+
209
+ print_parameter("forcerange", self.vin * self.model.kt.value / R)
210
+ print_parameter("armature", self.model.armature.value)
211
+ print_parameter("kp", kp)
212
+ print_parameter("damping", damping)
213
+ print_parameter("frictionloss", self.model.friction_base.value)
214
+
215
+ print("")
216
+
217
+
218
+ class CurrentControlledActuator(DCMotorActuator):
219
+ """Current-controlled servo with a firmware P-position controller.
220
+
221
+ The controller outputs a target current proportional to position error,
222
+ clipped by both the voltage limits and a user-configurable current cap.
223
+
224
+ :param testbench_class: Testbench class (see :class:`Actuator`).
225
+ :param vin: Supply voltage [V], used to compute the current saturation.
226
+ :param kp: Firmware proportional gain.
227
+ :param error_gain: Converts ``kp * Δq`` to a target current [A].
228
+ """
229
+
230
+ def __init__(
231
+ self, testbench_class: Testbench, vin: float, kp: float, error_gain: float
232
+ ):
233
+ super().__init__(testbench_class, vin, kp)
234
+ self.error_gain = error_gain
235
+
236
+ def control_unit(self) -> str:
237
+ return "amps"
238
+
239
+ def initialize(self):
240
+ super().initialize()
241
+ self.model.current_limit = Parameter(1.5, 0, 3)
242
+ self.model.viscous_damping_with_torque = Parameter(0.0, 0.0, 0.1)
243
+
244
+ def compute_control(
245
+ self, q_target: float, q: float, dq: float, dt: float
246
+ ) -> float | None:
247
+ """Compute the current command from position error.
248
+
249
+ Clips the P-controller output by both the back-EMF voltage limit and
250
+ the ``current_limit`` parameter.
251
+
252
+ :param q_target: Target joint angle [rad].
253
+ :param q: Current joint angle [rad].
254
+ :param dq: Current joint velocity [rad/s].
255
+ :param dt: Timestep [s] (unused here).
256
+ :returns: Target current [A].
257
+ """
258
+ # Target current using simple P controller
259
+ current = (q_target - q) * self.kp * self.error_gain
260
+
261
+ # Maximum allowable current due to voltage limits
262
+ current_limit_low = (1 / self.model.R.value) * (
263
+ self.vin - self.model.kt.value * dq
264
+ )
265
+ current_limit_high = (1 / self.model.R.value) * (
266
+ -self.vin - self.model.kt.value * dq
267
+ )
268
+ current = np.clip(current, current_limit_high, current_limit_low)
269
+
270
+ # Maximum current allowed by the user to avoid heating
271
+ current = np.clip(
272
+ current, -self.model.current_limit.value, self.model.current_limit.value
273
+ )
274
+
275
+ return current
276
+
277
+ def compute_torque(
278
+ self, control: float | None, torque_enable: bool, q: float, dq: float
279
+ ) -> float:
280
+ """Compute motor torque from current command.
281
+
282
+ :math:`\\tau = k_t I - b_{\\text{active}} \\dot{q}`
283
+
284
+ :param control: Current command [A].
285
+ :param torque_enable: If ``False``, returns zero torque.
286
+ :param q: Current joint angle [rad] (unused here).
287
+ :param dq: Current joint velocity [rad/s].
288
+ :returns: Motor torque [Nm].
289
+ """
290
+ torque = (
291
+ self.model.kt.value * control
292
+ - self.model.viscous_damping_with_torque.value * dq
293
+ )
294
+ return torque * torque_enable
295
+
296
+ def to_mujoco(self):
297
+ if self.vin == 0 or self.kp == 0:
298
+ print(yellow(f"WARNING: kp or vin are not set"))
299
+
300
+ kt = self.model.kt.value
301
+
302
+ kp = self.error_gain * self.kp * kt
303
+ damping = (
304
+ self.model.friction_viscous.value
305
+ + self.model.viscous_damping_with_torque.value
306
+ )
307
+
308
+ forcerange = self.vin * self.model.kt.value / self.model.R.value
309
+ forcerange = min(forcerange, self.model.current_limit.value * self.model.kt.value)
310
+
311
+ print_parameter("forcerange", forcerange)
312
+ print_parameter("armature", self.model.armature.value)
313
+ print_parameter("kp", kp)
314
+ print_parameter("damping", damping)
315
+ print_parameter("frictionloss", self.model.friction_base.value)
316
+
317
+ print("")