Guest User

Untitled

a guest
Jul 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4.  
  5. # thはラジアンです
  6.  
  7. # 回転行列の作成
  8. # base_angle 何度回転させるか
  9. def make_rot_mat(base_th):
  10. rot_matrix = [[np.cos(base_th), np.sin(base_th)], \
  11. [-1*np.sin(base_th), np.cos(base_th)]]
  12. rot_matrix = np.array(rot_matrix)
  13. return rot_matrix
  14.  
  15. # 回転速度行列の作成
  16. # 2つの行列を作成
  17. def make_vel_rot_mat(base_th, base_ome):
  18. #第1項
  19. vel_rot_mat_1 = [[-1*np.sin(base_th), np.cos(base_th)], \
  20. [-1*np.cos(base_th), -1*np.sin(base_th)]]
  21. vel_rot_mat_1 = base_ome * np.array(vel_rot_mat_1)
  22. #第2項
  23. vel_rot_mat_2 = [[np.cos(base_th), np.sin(base_th)], \
  24. [-1*np.sin(base_th), np.cos(base_th)]]
  25. vel_rot_mat_2 = np.array(vel_rot_mat_2)
  26.  
  27. return vel_rot_mat_1, vel_rot_mat_2
  28.  
  29. # 逆回転行列の作成
  30. # baseはもともと回転させた角度
  31. def make_inv_rot_mat(base_th):
  32. inv_rot_matrix = [[np.cos(base_th), -1 * np.sin(base_th)], \
  33. [np.sin(base_th), np.cos(base_th)]]
  34. inv_rot_matrix = np.array(inv_rot_matrix)
  35. return inv_rot_matrix
  36.  
  37. # 逆回転速度行列の作成
  38. # もとに戻す
  39. def make_inv_vel_rot_mat(base_th, base_ome):
  40. #第1項
  41. inv_vel_rot_mat_1 = [[-1*np.sin(base_th), -1*np.cos(base_th)], \
  42. [np.cos(base_th), -1*np.sin(base_th)]]
  43. inv_vel_rot_mat_1 = base_ome*np.array(inv_vel_rot_mat_1)
  44. #第2項
  45. inv_vel_rot_mat_2 = [[np.cos(base_th), -1*np.sin(base_th)], \
  46. [np.sin(base_th), np.cos(base_th)]]
  47. inv_vel_rot_mat_2 = np.array(inv_vel_rot_mat_2)
  48.  
  49. return inv_vel_rot_mat_1, inv_vel_rot_mat_2
  50.  
  51. def rotation_parallele(x, y, th, v_x, v_y, ome, base_x, base_y, base_th, base_v_x, base_v_y, base_ome):
  52. # ①下準備
  53. X = np.array([x, y, th, v_x, v_y, ome])
  54. base_X = np.array([base_x, base_y, base_th, base_v_x, base_v_y, base_ome])
  55.  
  56. # 回転行列の作成
  57. rot_mat = make_rot_mat(base_th)
  58. vel_rot_mat_1, vel_rot_mat_2 = make_vel_rot_mat(base_th, base_ome)
  59.  
  60. # ②平行移動
  61. para_X = X - base_X
  62.  
  63. # ③回転移動
  64. rot_xy = np.dot(rot_mat, para_X[:2].reshape(2,1))
  65. rot_v_xy = np.dot(vel_rot_mat_1, para_X[:2].reshape(2,1)) + np.dot(vel_rot_mat_2, para_X[3:5].reshape(2,1))
  66.  
  67. # 入れ直し
  68. para_rot_x, para_rot_y = rot_xy[0, 0], rot_xy[1, 0]
  69. para_rot_th = para_X[2]
  70. para_rot_v_x, para_rot_v_y = rot_v_xy[0, 0], rot_v_xy[1, 0]
  71. para_rot_ome = para_X[5]
  72.  
  73. print('座標変換前:\n x = {0} \n y = {1} \n th = {2} \n v_x = {3} \n v_y = {4} \n ome = {5}'\
  74. .format(x, y, th, v_x, v_y, ome))
  75. print('基準状態:\n x = {0} \n y = {1} \n th = {2} \n v_x = {3} \n v_y = {4} \n ome = {5}'\
  76. .format(base_x, base_y, base_th, base_v_x, base_v_y, base_ome))
  77. print('座標変換後:\n x = {0} \n y = {1} \n th = {2} \n v_x = {3} \n v_y = {4} \n ome = {5}'\
  78. .format(para_rot_x, para_rot_y, para_rot_th, para_rot_v_x, para_rot_v_y, para_rot_ome))
  79.  
  80. return para_rot_x, para_rot_y, para_rot_th, para_rot_v_x, para_rot_v_y, para_rot_ome
  81.  
  82. if __name__ == '__main__':
  83. x, y, th = 1.0, 2.0, 0.5 # thはラジアンです
  84. v_x, v_y, ome = 0.5, 0.75, 0.5 # omeはrad/sです
  85.  
  86. base_x, base_y, base_th = 1.3, 2.5, 0.5
  87. base_v_x, base_v_y, base_ome = 0.5, 0.75, 0.5
  88.  
  89. rotation_parallele(x, y, th, v_x, v_y, ome, base_x, base_y, base_th, base_v_x, base_v_y, base_ome)
Add Comment
Please, Sign In to add comment