File size: 2,581 Bytes
41791c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using PhysX5ForUnity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// PSM with Suction Irrigator 6 DoF for IK, actually controlled with 5 joint inputs
/// </summary>
/// 
public class PSMSuctionIrrigator6DofControllerMod : PSMControllerBase
{
    public override void DriveJoints(float[] jointPositions)
    {
        if (jointPositions == null || jointPositions.Length != 5)
        {
            throw new ArgumentException("jointPositions must be an array of 5 floats.");
        }

        float[] extendedJointPositions = GetExtendedJointPositionSetPoint(jointPositions);

        base.DriveJoints(extendedJointPositions);
        m_jointPositionSetpoint = jointPositions;
    }

    public float[] GetExtendedJointPositionSetPoint(float[] jointPositions)
    {
        float[] extendedJointPositions = new float[7];
        extendedJointPositions[0] = jointPositions[0];
        extendedJointPositions[1] = jointPositions[1];
        extendedJointPositions[2] = jointPositions[2];
        extendedJointPositions[3] = 0;
        extendedJointPositions[4] = jointPositions[3];
        extendedJointPositions[5] = jointPositions[4];
        extendedJointPositions[6] = jointPositions[4];
        return extendedJointPositions;
    }

    public override void DriveCartesianPose(PxTransformData t)
    {
        float[] qInit = new float[6];
        for (int i = 0; i < 6; i++)
        {
            qInit[i] = m_robot.JointPositions[i];
        }

        t.quaternion = t.quaternion * m_rotationTooltipToJoint;
        bool result = Physx.GetRobotInverseKinematics(m_robot.NativeObjectPtr, ref qInit[0], ref t, 1e-3f, 100, 0.01f);
        if (!result)
        {
            float[] qActual = m_robot.JointPositions;
            string printStr = "q_actual: ";
            for (int i = 0; i < 6; i++)
            {
                printStr += qActual[i];
                printStr += ", ";
            }
            print(printStr);
            printStr = "q_solved: ";
            for (int i = 0; i < 6; i++)
            {
                printStr += qInit[i];
                printStr += ", ";
            }
            print(printStr);
            throw new Exception("IK Failed!");
        }

        bool shouldDriveJoints = true;
        for (int i = 0; i < 6; i++)
        {
            if (qInit[i] - m_robot.JointPositions[i] > 0.2)
            {
                shouldDriveJoints = false;
            }
        }
        if (shouldDriveJoints)
        {
            DriveJoints(qInit);
        }
    }
}