|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "id": "6174d785-e532-4df1-ba61-235f0fe9f000", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "SCARA Arm Position -> X: 134.69, Y: 163.40, Z: 35.00 | Joint Angles: [30°, 45°, -20°], Z Offset: -15\n" |
| 14 | + ] |
| 15 | + } |
| 16 | + ], |
| 17 | + "source": [ |
| 18 | + "import math\n", |
| 19 | + "\n", |
| 20 | + "class SCARARobotArm:\n", |
| 21 | + " def __init__(self, link_lengths, base_height=0.0):\n", |
| 22 | + " \"\"\"\n", |
| 23 | + " Initializes the SCARA robot arm with a dynamic number of revolute joints.\n", |
| 24 | + "\n", |
| 25 | + " :param link_lengths: List of link lengths [L1, L2, ...]\n", |
| 26 | + " :param base_height: Base height for the prismatic joint (Z-axis)\n", |
| 27 | + " \"\"\"\n", |
| 28 | + " self.link_lengths = link_lengths\n", |
| 29 | + " self.n_joints = len(link_lengths)\n", |
| 30 | + " self.joint_angles = [0.0] * self.n_joints # In degrees\n", |
| 31 | + " self.base_height = base_height\n", |
| 32 | + " self.z_offset = 0.0 # Vertical prismatic joint (Z-axis)\n", |
| 33 | + "\n", |
| 34 | + " def set_joint_angles(self, angles):\n", |
| 35 | + " \"\"\"\n", |
| 36 | + " Sets the revolute joint angles (in degrees).\n", |
| 37 | + "\n", |
| 38 | + " :param angles: List of joint angles, same length as link_lengths\n", |
| 39 | + " \"\"\"\n", |
| 40 | + " if len(angles) != self.n_joints:\n", |
| 41 | + " raise ValueError(\"Number of angles must match number of links.\")\n", |
| 42 | + " self.joint_angles = angles\n", |
| 43 | + "\n", |
| 44 | + " def set_z_offset(self, z):\n", |
| 45 | + " \"\"\"Set vertical offset for the prismatic joint (Z-axis).\"\"\"\n", |
| 46 | + " self.z_offset = z\n", |
| 47 | + "\n", |
| 48 | + " def forward_kinematics(self):\n", |
| 49 | + " \"\"\"\n", |
| 50 | + " Calculates the end-effector position (x, y, z) based on current joint angles.\n", |
| 51 | + " Returns:\n", |
| 52 | + " (x, y, z): End-effector Cartesian coordinates\n", |
| 53 | + " \"\"\"\n", |
| 54 | + " x, y = 0.0, 0.0\n", |
| 55 | + " angle_sum = 0.0\n", |
| 56 | + "\n", |
| 57 | + " for i in range(self.n_joints):\n", |
| 58 | + " angle_sum += math.radians(self.joint_angles[i])\n", |
| 59 | + " x += self.link_lengths[i] * math.cos(angle_sum)\n", |
| 60 | + " y += self.link_lengths[i] * math.sin(angle_sum)\n", |
| 61 | + "\n", |
| 62 | + " z = self.base_height + self.z_offset\n", |
| 63 | + " return (x, y, z)\n", |
| 64 | + "\n", |
| 65 | + " def __str__(self):\n", |
| 66 | + " x, y, z = self.forward_kinematics()\n", |
| 67 | + " angles_str = \", \".join([f\"{a}°\" for a in self.joint_angles])\n", |
| 68 | + " return (f\"SCARA Arm Position -> X: {x:.2f}, Y: {y:.2f}, Z: {z:.2f} | \"\n", |
| 69 | + " f\"Joint Angles: [{angles_str}], Z Offset: {self.z_offset}\")\n", |
| 70 | + "\n", |
| 71 | + "# Example usage:\n", |
| 72 | + "if __name__ == \"__main__\":\n", |
| 73 | + " # Create a 3-joint SCARA-like robot (not common, but for flexibility/testing)\n", |
| 74 | + " arm = SCARARobotArm(link_lengths=[100, 75, 50], base_height=50)\n", |
| 75 | + " arm.set_joint_angles([30, 45, -20])\n", |
| 76 | + " arm.set_z_offset(-15)\n", |
| 77 | + " print(arm)\n" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "id": "259ff2bb-7891-4f33-af24-6ec565cd0cc7", |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [], |
| 86 | + "source": [] |
| 87 | + } |
| 88 | + ], |
| 89 | + "metadata": { |
| 90 | + "kernelspec": { |
| 91 | + "display_name": "Python 3 (ipykernel)", |
| 92 | + "language": "python", |
| 93 | + "name": "python3" |
| 94 | + }, |
| 95 | + "language_info": { |
| 96 | + "codemirror_mode": { |
| 97 | + "name": "ipython", |
| 98 | + "version": 3 |
| 99 | + }, |
| 100 | + "file_extension": ".py", |
| 101 | + "mimetype": "text/x-python", |
| 102 | + "name": "python", |
| 103 | + "nbconvert_exporter": "python", |
| 104 | + "pygments_lexer": "ipython3", |
| 105 | + "version": "3.12.11" |
| 106 | + } |
| 107 | + }, |
| 108 | + "nbformat": 4, |
| 109 | + "nbformat_minor": 5 |
| 110 | +} |
0 commit comments