{"id":"2CfC2NRkdt","url":"https://pastebin.ca/2CfC2NRkdt","raw_url":"https://raw.anybin.ca/2CfC2NRkdt","visibility":"public","access":"public","created_at":1794821335251,"expires_at":1795426135251,"fetch_limit":null,"fetches_used":0,"reads_remaining":null,"size_bytes":1822,"syntax_hint":null,"title":null,"filename":null,"change_note":null,"cipher":null,"cipher_meta":null,"parent_id":null,"root_id":"2CfC2NRkdt","version":1,"owner_id":null,"recipient_id":null,"body":"import math\nfrom controller import Robot\n\nPICK_XY = [0.25, 0.1] \nPLACE_XY = [0.1, 0.25]\nLIFT_HEIGHT = 0.035\nDROP_HEIGHT = -0.035\n\nL1 = 0.2\nL2 = 0.2\n\ndef calculate_ik(x, y):\n    d_squared = x**2 + y**2\n    d = math.sqrt(d_squared)\n\n    if d > (L1 + L2) or d < abs(L1 - L2):\n        print(f\"Target ({x}, {y}) out of reach!\")\n        return 0, 0\n\n    cos_theta2 = (d_squared - L1**2 - L2**2) / (2 * L1 * L2)\n    theta2 = math.acos(cos_theta2)\n\n    alpha = math.atan2(y, x)\n    beta = math.atan2(L2 * math.sin(theta2), L1 + L2 * math.cos(theta2))\n    theta1 = alpha - beta\n\n    return theta1, theta2\n\ndef wait(robot, timestep, seconds):\n    steps = int((seconds * 1000) / timestep)\n    for _ in range(steps):\n        robot.step(timestep)\n\ndef run_robot():\n    robot = Robot()\n    timestep = int(robot.getBasicTimeStep())\n\n    m1 = robot.getDevice('motor1')\n    m2 = robot.getDevice('motor2')\n    elevator = robot.getDevice('elevator_motor')\n    gripper = robot.getDevice('vacuum_gripper')\n    \n    gripper.enablePresence(timestep)\n\n    p1_m1, p1_m2 = calculate_ik(PICK_XY[0], PICK_XY[1])\n    p2_m1, p2_m2 = calculate_ik(PLACE_XY[0], PLACE_XY[1])\n\n    elevator.setPosition(LIFT_HEIGHT)\n    m1.setPosition(p1_m1)\n    m2.setPosition(p1_m2)\n    wait(robot, timestep, 2.0)\n\n    elevator.setPosition(DROP_HEIGHT)\n    wait(robot, timestep, 2.0)\n\n    gripper.turnOn()\n    wait(robot, timestep, 1.0)\n\n    elevator.setPosition(LIFT_HEIGHT)\n    wait(robot, timestep, 2.0)\n\n    m1.setPosition(p2_m1)\n    m2.setPosition(p2_m2)\n    wait(robot, timestep, 2.0)\n\n    elevator.setPosition(DROP_HEIGHT)\n    wait(robot, timestep, 2.0)\n\n    gripper.turnOff()\n    wait(robot, timestep, 1.0)\n\n    elevator.setPosition(LIFT_HEIGHT)\n    m1.setPosition(0)\n    m2.setPosition(0)\n    wait(robot, timestep, 2.0)\n\nif __name__ == \"__main__\":\n    run_robot()"}