{"id":19005513,"url":"https://github.com/star2dust/two-link","last_synced_at":"2025-07-30T03:07:39.527Z","repository":{"id":54526950,"uuid":"287673648","full_name":"star2dust/two-link","owner":"star2dust","description":"Simlation of two-link robot arm using RTB. ","archived":false,"fork":false,"pushed_at":"2021-12-03T12:17:11.000Z","size":7156,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T19:05:28.059Z","etag":null,"topics":["robotics","robotics-simulation","robotics-toolbox"],"latest_commit_sha":null,"homepage":"","language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/star2dust.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-15T03:57:24.000Z","updated_at":"2025-04-10T02:53:26.000Z","dependencies_parsed_at":"2022-08-13T18:40:44.321Z","dependency_job_id":null,"html_url":"https://github.com/star2dust/two-link","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/star2dust/two-link","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/star2dust%2Ftwo-link","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/star2dust%2Ftwo-link/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/star2dust%2Ftwo-link/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/star2dust%2Ftwo-link/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/star2dust","download_url":"https://codeload.github.com/star2dust/two-link/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/star2dust%2Ftwo-link/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267802917,"owners_count":24146512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["robotics","robotics-simulation","robotics-toolbox"],"created_at":"2024-11-08T18:27:48.798Z","updated_at":"2025-07-30T03:07:39.492Z","avatar_url":"https://github.com/star2dust.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 写在前面\n\n本文使用的工具为matlab以及Peter Corke的[RTB](https://petercorke.com/toolboxes/robotics-toolbox/)(Robotics Toolbox)。基于RTB 10.3.1版本，我写了[RTE](https://github.com/star2dust/Robotics-Toolbox)(Robotics Toolbox Extension)，增加了一些移动机器人、机械臂以及路径规划相关代码。同时，RTE也修复了原RTB的一些小bug。\n\n听说最近RTB出了10.4，不知道bug修复完没有，有用过的同学可以谈谈感想。个人建议这篇文章最好用我GitHub里的RTE工具箱，下载点[这里](https://github.com/star2dust/Robotics-Toolbox)。安装方法见README文件，写的很详细了。\n\n本文的任务是利用matlab和RTB建模2连杆机械臂的动力学，并与matlab自带的simulink/simscape仿真进行对比，验证RTB建模的正确性。本文并不涉及控制部分，只是教大家如何建模真实的多刚体系统。\n\n# 2连杆机械臂\n\n我们要研究的是竖直平面上的2连杆机械臂，也可以说是双摆。因为本文不做控制，所以我们要模拟机械臂在重力的作用下运动的过程。\n\n\u003cimg src=\"twolink.png\" alt=\"twolink\" width=\"250\" /\u003e\n\n[这里](https://star2dust.github.io/post/matlab-rtb-two-link/)给出2连杆机械臂的动力学建模过程，之后可以代入数值验证代码正确性。\n\n# RTB建模\n\n- 给出物理参数\n```matlab\nlx = 1; lr = 0.1;  % 连杆的长度和半径\ngy = 9.81;  % 重力加速度(这里在y轴方向，因为z轴是关节转动轴)\nfvis = 0; fcou = 0; % 粘性摩擦系数和库伦摩擦系数\n```\n- 建立连杆模型\n这里的`Cuboid`和`Cylinder`都是我自己基于RTB写的类。\n```matlab\nrod = Cuboid([lx,lr,lr]); % 建立成长方体\nIrod = rod.inertia; % 可以直接获得转动惯量\n```\n这里建立成圆柱也行，获得转动惯量时需要进行一个刚体变换。\n```matlab\nrod = Cylinder(lr,lx);\nRcyl = SO3.rpy([0 -pi/2 0]);\nIrod = Rcyl.R*rod.inertia*Rcyl.R';\n```\n- 机械臂建模\n```matlab\n% 设定dh参数，质量，关节质心距离，关节约束，转动惯量，摩擦系数\ndpm = {'a', lx, 'm', rod.mass, 'r', [-lx/2,0,0],...\n 'qlim', [-pi/2, pi/2],'I', Irod,...\n    'B', fvis, 'Tc', [fcou -fcou]};\n% 建立2连杆机械臂，设定回转关节和重力\nr = SerialLink([Revolute(dpm{:}),Revolute(dpm{:})],...\n'name','two-link','gravity',[0 gy 0]);\n```\n经过以上步骤，2连杆机械臂建模完成，设置关节角`qz`，可以画出机械臂。\n```matlab\nws = [-4 4 -4 4 -4 4]; % 设置工作空间\nplotopt = {'workspace', ws, 'nobase', 'notiles', 'noshading', 'noshadow', 'nowrist','top'}; % 设置绘图参数\nh = r.plot(qz,plotopt{:});\n```\n\n# 仿真与验证\n\n我们这里验证2连杆机械臂在重力作用下运动过程，即双摆实验。\n```matlab\n% 给定初始关节角位置和速度\nqz = zeros(1,2);\nqd = zeros(1,2);\n% 设定仿真时间10s\ny0 = [qz,qd]'; tspan = [0 10]; \n% 刚体仿真用ode15s比较快，用accel函数直接获得关节加速度\ntic\n[tlist,ylist] = ode15s(@(t,y) [y(r.n+1:end);r.accel(y(1:r.n)',y(r.n+1:end)',zeros(1,r.n))],tspan,y0);\ntoc\n% 差不多6s可出结果，然后画出机械臂即可\nws = [-4 4 -4 4 -4 4];\nplotopt = {'workspace', ws, 'nobase', 'notiles', 'noshading', 'noshadow', 'nowrist','top'};\nh = r.plot(ylist(:,1:r.n),plotopt{:});\n```\n实验结果如下图所示。\n\n\u003cimg src=\"twolinkrtb.gif\" alt=\"twolinkrtb\" width=\"250\" /\u003e\n\n下面进行simulink/simscape的双摆仿真，对比实验如下图所示，可以看到前面几乎是同步的。\n\n\u003cimg src=\"twolinksim.gif\" alt=\"twolinksim\" width=\"250\" /\u003e\n\n如何使用simscape搭建一个双摆系统，可以参考b站[这个视频](https://www.bilibili.com/video/BV1X741147a5)。\n\n# 源代码\n\n本文所需全部源代码已上传至[我的GitHub](https://github.com/star2dust)，点击[这里](https://github.com/star2dust/two-link)下载。运行`two_link_test.m`即可。使用前请确认RTB已经正确安装，下载和安装说明点击[这里](https://github.com/star2dust/Robotics-Toolbox)。\n\n使用simulink/simscape做的双摆仿真也在该仓库里，见`link_test.slx`，但是需要matlab里有simscape的工具箱，否则打不开。另外matlab版本最好在2018b以上。\n\n如果喜欢，欢迎点赞和fork。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstar2dust%2Ftwo-link","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstar2dust%2Ftwo-link","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstar2dust%2Ftwo-link/lists"}