Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment-03 #43

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lecture_04/assignment_03/hanbing_zhao/assignment_03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"dtype": "compas.robots/Configuration", "value": {"joint_values": [0.0, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [1.5707963267948966, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [3.141592653589793, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [4.71238898038469, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [6.283185307179586, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [7.853981633974483, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [9.42477796076938, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [10.995574287564276, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [12.566370614359172, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [14.137166941154069, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}, {"dtype": "compas.robots/Configuration", "value": {"joint_values": [15.707963267948966, 0.0, 0.0], "joint_types": [0, 0, 0], "joint_names": []}}]
63 changes: 63 additions & 0 deletions lecture_04/assignment_03/hanbing_zhao/assignment_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Assignment 03: Using inverse kinematics
"""
import os
import compas
from compas_fab.backends import RosClient
from compas_fab.robots import Configuration

from compas.geometry import Frame
from compas.geometry import Point
from compas.geometry import Vector

import math
import json


# Step 1: Inside this function, complete the main part of the solution for the assignment:
# - Taking a robot and a list of frames as parameter, calculate a feasible configuration for each of the frames
# - Try to find an optimal start_configuration for each so that the motion from one config to the next is minimized
def calculate_ik_for_frames(robot, frames):
configs = []
start_configuration = Configuration.from_revolute_values([math.pi/2, 0., 0., 0.,0.,0.])

for i in range(len(frames)):
# config = Configuration.from_revolute_values([i * math.pi/2, 0., 0., 0.,0.,0.])
config = robot.inverse_kinematics(frames[i], start_configuration, group=None, return_full_configuration=False, options=None)
# config.joint_values
configs.append(config)
return configs


# Step 2: store all found configurations in a JSON file using compas.json_dump or compas.json_dumps
def store_configurations(configurations, filename):
compas.json_dump(configurations, filename) # not the 'filename'

# Use the following to test from the command line
# Or copy solution_viewer.ghx next to the folder where you created assignment_03.py to visualize the same in Grasshopper
if __name__ == '__main__':

frames = [
Frame(Point(-0.329, 0.059, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(-0.260, 0.129, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(-0.186, 0.194, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(-0.106, 0.252, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(-0.020, 0.299, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.074, 0.329, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.172, 0.330, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.263, 0.295, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.339, 0.233, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.400, 0.155, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000)),
Frame(Point(0.448, 0.070, 0.082), Vector(1.000, 0.000, 0.000), Vector(0.000, -1.000, 0.000))]

# Loads the robot from ROS
with RosClient('localhost') as client:
robot = client.load_robot()

# Step 1: calculate IK solutions for each frame
configurations = calculate_ik_for_frames(robot, frames)
print("Found {} configurations".format(len(configurations)))

# Step 2: store all configurations in a JSON file
filename = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'assignment_03.json')
store_configurations(configurations, filename)
print("Stored results in {}".format(filename))
Loading