Advertisement
Guest User

Robotics

a guest
May 20th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package robotkinematics;
  7.  
  8. /**
  9.  *
  10.  * @author Wolf
  11.  */
  12. import java.util.Scanner;
  13.  
  14. public class forwardKin {
  15.     static int x;
  16.     public static double theta = 0;
  17.     public static double alpha = 0;
  18.     public static double a = 0;
  19.     public static double d = 0;
  20.    
  21.     public static String vT;
  22.     public static String vA;
  23.     public static String vR;
  24.     public static String vD;
  25.    
  26.     public static double product[][] = new double[4][4];
  27.     public static String varPro[][] = new String[4][4];
  28.    
  29.    
  30.    public static double[][] transform(double theta, double d, double a, double alpha){
  31.    
  32.      double Tran[][] = {
  33.             {Math.cos(theta), -Math.sin(theta) * Math.cos(alpha), Math.sin(theta)* Math.sin(alpha), a*Math.cos(theta)},
  34.             {Math.sin(theta), Math.cos(theta)*Math.cos(alpha), - Math.cos(theta)*Math.sin(alpha), Math.sin(theta)*a},
  35.             {0, Math.sin(alpha), Math.cos(alpha), d},
  36.             {0, 0, 0, 1}
  37.        
  38.         };
  39.      return Tran;
  40. }
  41.        
  42.    
  43.     public static double[][] dhMatrix(int y){
  44.     double dh[][] = new double[x][4];
  45.         Scanner sc=new Scanner(System.in);
  46.     System.out.println("Enter the values of the DH Matrix!");  
  47.         for (int i = 0; i<y; i++){
  48.         for(int j = 0; j<4; j++){
  49.             dh[i][j]=sc.next.Double();
  50.         }
  51.       }
  52.         return dh;
  53.     }
  54.    
  55.     public static double[][] fk(int y, double dh[][]){
  56.         double arr[][] = new double[4][4];
  57.         double temp[][] = new double[4][4];
  58.         for(int i = 0; i < y; i++){
  59.        
  60.         theta = dh[i][0];
  61.         d = dh[i][1];
  62.         a = dh[i][2];
  63.         alpha = dh[i][3];
  64.         arr = transform(theta, d, a, alpha);
  65.         temp = arr;
  66.         if(i != 0){
  67.         arr = multiply(temp, arr);
  68.         }
  69.        }
  70.        
  71.          
  72.         return arr;
  73.     }
  74.     public static double[][] multiply(double arr1[][], double arr2[][]){
  75.     for (int i = 0; i < 4; i++)
  76.         {
  77.             for (int j = 0; j < 4; j++)
  78.             {
  79.                 for (int k = 0; k < 4; k++)
  80.                 {
  81.                     product[i][j] = product[i][j] + arr1[i][k] * arr2[k][j];
  82.                 }
  83.             }
  84.         }
  85.        return product;
  86.     }
  87.     public static void main(String args[]){
  88.        Scanner sc1=new Scanner(System.in);
  89.        System.out.println("Enter the number of joints!");  
  90.        System.out.println("==========================================================================");
  91.        x = sc1.nextInt();
  92.        Scanner sc=new Scanner(System.in);
  93.        System.out.println("Press 1 for Variable Values and 2 for Numerical Values!");  
  94.        System.out.println("==========================================================================");
  95.        
  96.         if(sc.nextInt() == 2){
  97.             //dhMatrix(x);
  98.             double T[][] =  fk(x, dhMatrix(x));
  99.              System.out.println("The Transformation Matrix for variable Joint Angles is: ");
  100.               System.out.println("==========================================================================");
  101.             for(int i = 0; i < 4; i++){
  102.             for(int j = 0; j < 4; j++){
  103.             System.out.print(T[i][j] + ", ");
  104.         }
  105.             System.out.println("");
  106.         }
  107.              
  108.              
  109.         }
  110.         else{
  111.             //varDHMatrix(x);
  112.             String VT[][] = varFK(x, varDHMatrix(x));
  113.              for(int i = 0; i < 4; i++){
  114.             for(int j = 0; j < 4; j++){
  115.             System.out.print(VT[i][j] + ", ");
  116.         }
  117.             System.out.println("");
  118.         }
  119.              System.out.println("INVERSE KINEMATICS");
  120.              System.out.println("==========================================================================");
  121.              
  122.              System.out.println("Insert The Position Vector of The end-effector to calculate the joint angles!");
  123.              
  124.              Scanner sc4=new Scanner(System.in);
  125.              double x=0,y=0,z=0;
  126.              for(int i = 0; i<3; i++){
  127.                if(i == 0){
  128.                   System.out.println("The value of x: ");
  129.                   x = sc4.nextDouble();
  130.                }
  131.                else if(i == 1){
  132.                   System.out.println("The value of y: ");
  133.                   y = sc4.nextDouble();
  134.                }
  135.                else if(i == 2){
  136.                   System.out.println("The value of z: ");
  137.                   z = sc4.nextDouble();
  138.                }
  139.                
  140.              }
  141.              String pos[][]=setPosition(x,y,z);
  142.               for(int i = 0; i<4; i++){
  143.                    for(int j = 0; j<4; j++){
  144.                        System.out.print(pos[i][j] + ", ");
  145.                    }
  146.                    System.out.println("");
  147.               }
  148.               String arra[]= equalize(pos, VT);
  149.                 for(int j = 0; j<4; j++){
  150.                        System.out.print(arra[j] + ", ");
  151.                        System.out.println("");
  152.                 }
  153.          System.out.println("For a SCARA robot: ");  
  154.          SCARA();
  155.         }
  156.        
  157.     }
  158.     public static String[][] varFK(int y, String varDh[][]){
  159.    
  160.         String arr[][] = new String[4][4];
  161.         String temp[][] = new String[4][4];
  162.         for(int i = 0; i < y; i++){
  163.        
  164.         vT = varDh[i][0];
  165.         vD = varDh[i][1];
  166.         vR = varDh[i][2];
  167.         vA = varDh[i][3];
  168.         arr = var_transform(vT, vD, vR, vA);
  169.         temp = arr;
  170.         if(i>0){
  171.            
  172.         arr = varMultiply(temp, arr);
  173.         }
  174.        }
  175.          
  176.         return arr;
  177.        
  178.     }
  179.    
  180.     public static String[][] var_transform(String vT, String vD, String vR, String vA){
  181. //        String var_tra[][] = new String[4][4];
  182.         String var_tra[][] = {
  183.                        {"cos("+vT+")", "-cos("+vA+ ")*sin(" +vT +")","sin("+vT+")*sin("+vA+")","cos("+vT+")*" +vR},
  184.                        {"sin("+vT+")", "cos("+vT+")*cos("+vA+")", "-cos("+vT+")*sin("+vA+")" ,"sin("+vT+")*"+vR},
  185.                        {"0", "sin{"+vA+")", "cos("+vA+")", vD},
  186.                        {"0", "0", "0", "1"}
  187.                        };
  188.        
  189.         return var_tra;
  190.     }
  191.     public static String[][] varMultiply(String arr1[][], String arr2[][]){
  192.      
  193.        
  194.        for (int i = 0; i < 4; i++)
  195.         {
  196.             for (int j = 0; j < 4; j++)
  197.             {
  198.                 for (int k = 0; k < 4; k++)
  199.                 {  
  200.                    
  201.                     varPro[i][j] = varPro[i][j] + "+" + arr1[i][k] + "*" + arr2[k][j];
  202.                    
  203.                 }
  204.              
  205.                
  206.             }
  207.            
  208.         }
  209.        return varPro;
  210.    
  211.     }
  212.    
  213.     public static String[][] varDHMatrix(int y){
  214.         String [][] varDh = new String[y][4];
  215.         Scanner sc=new Scanner(System.in);
  216.         System.out.println("Enter the values of the DH Matrix with variable joint angles!");  
  217.         for (int i = 0; i< y; i++){
  218.         for(int j = 0; j<4; j++){
  219.             varDh[i][j]=sc.next();
  220.         }
  221.       }
  222.         return varDh;
  223.     }
  224.     public static String[][] setPosition(double x, double y, double z){
  225.     String position[][] = {{"1","0","0","x"},{"0","1","0","y"},{"0","0","1","z"},{"0","0","0","1"}};
  226.     return position;
  227.     }
  228.     public static String[] equalize(String arr[][],String tra[][]){
  229.         String xa[] = new String[4];
  230.         for(int j = 0; j<4; j++){
  231.             xa[j] = arr[j][3] + "=" + tra[j][3];
  232.         }
  233.         return xa;
  234.     }
  235.     public static void SCARA(){
  236.     double theta[] = new double [6];
  237.     double alpha[] = new double[4];
  238.     double d[] = new double[4];
  239.     double a[] = new double[4];
  240.     Scanner sc7 = new Scanner(System.in);
  241.     System.out.println("Enter values for W");
  242.     for(int i =0;i<6;i++)
  243.       {
  244.         System.out.print("w"+(i+1) + ":");
  245.         theta[i] = sc7.nextDouble();
  246.         System.out.println();
  247.       }
  248.      
  249.     System.out.println("Enter values for joint distance");
  250.     for(int i = 0;i<4;i++)
  251.       {
  252.         System.out.print("d"+(i+1)+ ":");
  253.         d[i] = sc7.nextDouble();
  254.         System.out.println();
  255.       }
  256.      
  257.     System.out.println("Enter values for link length");
  258.     for(int i = 0;i<4;i++)
  259.       {
  260.         System.out.print("a"+(i+1)+ ":");
  261.         a[i] = sc7.nextDouble();
  262.         System.out.println();
  263.       }
  264.      
  265. alpha[2] = d[0]-theta[2]-d[3];
  266. double t11 = Math.pow(theta[0],2)+Math.pow(theta[1],2)-Math.pow(a[0], 2)-Math.pow(a[1], 2);
  267. double t12 = 2*a[0]*a[1];
  268. alpha[1] = Math.toDegrees(Math.acos(t11/t12));
  269.  
  270. double t21 = a[1]*Math.sin(Math.toRadians(alpha[1]))*theta[0];
  271. double t22 = (a[0]+(a[1]*Math.sin(Math.toRadians(alpha[1]))))*theta[1];
  272.  
  273. double t31 = (a[0]+a[1]*Math.cos(Math.toRadians(alpha[1])))*theta[0];
  274. double t32 = (a[1]*Math.sin(Math.toRadians(alpha[1])))*theta[1];
  275.  
  276. alpha[0] = Math.toDegrees(Math.atan((t21+t22)/(t31-t32)));
  277. alpha[3] = Math.PI*Math.log(Math.abs(theta[5]));
  278.  
  279. System.out.println("The I.K/ of SCARA is " +alpha[0]+" " +alpha[1]+" "+alpha[2]+" "+alpha[3]);
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement