import  numpy as  np
import  matplotlib. pyplot as  plt
import  seaborn as  sns
import  plotly. express as  px
import  plotly. graph_objects as  go
from  plotly. subplots import  make_subplots
from  numpy import  sin
from  numpy import  cos
plt. rcParams[ "font.sans-serif" ] = [ "SimHei" ]             
plt. rcParams[ "axes.unicode_minus" ] = False               
sns. set_style( 'whitegrid' ,  { 'font.sans-serif' :  [ 'simhei' ,  'Arial' ] } ) 
sns. set ( rc= { "axes.facecolor" : "#FFF9ED" , "figure.facecolor" : "#FFF9ED" ,  'font.sans-serif' :  [ 'simhei' ,  'Arial' ] } )    
def  d_to_r ( degree) : """将角度值数据转换为弧度制  公式:弧度= (角度×π)/180:param degree::return:""" radian =  degree *  np. pi /  180   return  radiandef  abtain_J_orig ( L1,  L2,  L3,  theta_1,  theta_2,  theta_3) : """根据提供的参数求初始时(即第一次循环)的雅可比矩阵abtain_theta_der(4, 3, 2, 10, 20, 30):return:J, theta_old""" theta_1 =  d_to_r( theta_1) theta_2 =  d_to_r( theta_2) theta_3 =  d_to_r( theta_3) J =  np. array( [ [ - L1* sin( theta_1) - L2* sin( theta_1+ theta_2) - L3* sin( theta_1+ theta_2+ theta_3) ,  - L2* sin( theta_1+ theta_2) - L3* sin( theta_1+ theta_2+ theta_3) ,  - L3* sin( theta_1+ theta_2+ theta_3) ] , [ L1* cos( theta_1) + L2* cos( theta_1+ theta_2) + L3* cos( theta_1+ theta_2+ theta_3) ,  L2* cos( theta_1+ theta_2) + L3* cos( theta_1+ theta_2+ theta_3) ,  L3* cos( theta_1+ theta_2+ theta_3) ] , [ 1 ,  1 ,  1 ] ] ) theta_old =  np. array( [ theta_1,  theta_2,  theta_3] ) . Treturn  J,  theta_olddef  abtain_J ( L1,  L2,  L3,  theta_array) : """根据提供的参数求第2次到第n次循环时的雅可比矩阵abtain_theta_der(4, 3, 2, 10, 20, 30):return:J, theta_old""" theta_1 =  theta_array[ 0 ] theta_2 =  theta_array[ 1 ] theta_3 =  theta_array[ 2 ] J =  np. array( [ [ - L1* sin( theta_1) - L2* sin( theta_1+ theta_2) - L3* sin( theta_1+ theta_2+ theta_3) ,  - L2* sin( theta_1+ theta_2) - L3* sin( theta_1+ theta_2+ theta_3) ,  - L3* sin( theta_1+ theta_2+ theta_3) ] , [ L1* cos( theta_1) + L2* cos( theta_1+ theta_2) + L3* cos( theta_1+ theta_2+ theta_3) ,  L2* cos( theta_1+ theta_2) + L3* cos( theta_1+ theta_2+ theta_3) ,  L3* cos( theta_1+ theta_2+ theta_3) ] , [ 1 ,  1 ,  1 ] ] ) theta_old =  np. array( [ theta_1,  theta_2,  theta_3] ) . Treturn  J,  theta_olddef  abtain_theta_der ( J) : """根据提供的参数求关节速率theta_derabtain_theta_der(4, 3, 2, 10, 20, 30):return:""" Car_vel =  np. array( [ 0.2 ,  - 0.3 ,  - 0.2 ] ) . TJ_inv =  np. linalg. inv( J) theta_der =  np. dot( J_inv,  Car_vel)    return  theta_derdef  abtain_theta_new ( theta_old,  theta_der) : """计算求出theta_new:param theta_old::param theta_der::return:""" delta_t =  0.1 theta_new =  theta_old +  theta_der* delta_treturn  theta_newdef  run ( ) : """执行:return:""" loop_num =  50 L1 =  4 L2 =  3 L3 =  2 theta1_orig =  10 theta2_orig =  20 theta3_orig =  30 J,  theta_old =  abtain_J_orig( L1,  L2,  L3,  theta1_orig,  theta2_orig,  theta3_orig) print ( "初始时的J(雅可比矩阵):\n" ,  J) theta_all =  theta_old. reshape( ( 1 , 3 ) ) theta_der =  abtain_theta_der( J) theta_der_all =  theta_der. reshape( ( 1 , 3 ) ) theta_new =  abtain_theta_new( theta_old,  theta_der) for  i in  range ( loop_num- 1 ) :   J,  theta_old =  abtain_J( L1,  L2,  L3,  theta_new) print ( f"执行了 { i+ 1 } 次循环后的J(雅可比矩阵):\n" ,  J) theta_der =  abtain_theta_der( J) theta_all =  np. concatenate( ( theta_all,  theta_new. reshape( ( 1 ,  3 ) ) ) ) theta_new =  abtain_theta_new( theta_old,  theta_der) theta_der_all =  np. concatenate( ( theta_der_all,  theta_der. reshape( ( 1 , 3 ) ) ) ) print ( theta_der_all) return  theta_der_all, theta_alldef  plot ( ) : """画图画图可用三个库实现:matplotlib、seaborn、plotly。用哪个就需要安装对应的库。:return:""" theta_der_all,  theta_all =  run( ) theta_der_1 =  theta_der_all[ : , 0 ] theta_der_2 =  theta_der_all[ : , 1 ] theta_der_3 =  theta_der_all[ : , 2 ] theta_1 =  theta_all[ : , 0 ] theta_2 =  theta_all[ : , 1 ] theta_3 =  theta_all[ : , 2 ] time_array =  np. arange( 0 , 5 , 0.1 ) figure,  axes =  plt. subplots( nrows= 1 ,  ncols= 2 , figsize= ( 8 , 4 ) ) axes[ 0 ] . plot( time_array,  theta_der_1,  label= 'theta_der_1' ,  color= 'blue' ) axes[ 0 ] . plot( time_array,  theta_der_2,  label= 'theta_der_2' ,  color= 'green' ) axes[ 0 ] . plot( time_array,  theta_der_3,  label= 'theta_der_3' ,  color= 'red' ) axes[ 0 ] . set_title( '3个主动关节速率与时间的关系' ) axes[ 0 ] . set_xlabel( "时间" ) axes[ 0 ] . set_ylabel( "关节速率" ) x_ticks =  np. arange( 0 , 5.5 , 0.5 ) axes[ 0 ] . set_xticks( x_ticks,  fontsize= 2 )   axes[ 0 ] . legend( ) axes[ 1 ] . plot( time_array,  theta_1,  label= 'theta_1' ,  color= 'blue' ) axes[ 1 ] . plot( time_array,  theta_2,  label= 'theta_2' ,  color= 'green' ) axes[ 1 ] . plot( time_array,  theta_3,  label= 'theta_3' ,  color= 'red' ) axes[ 1 ] . set_title( '3个主动关节角与时间的关系' ) axes[ 1 ] . set_xlabel( "时间" ) axes[ 1 ] . set_ylabel( "关节角" ) x_ticks =  np. arange( 0 , 5.5 , 0.5 ) axes[ 1 ] . set_xticks( x_ticks)   axes[ 0 ] . legend( ) axes[ 1 ] . legend( ) plt. savefig( 'result.png' ) plt. show( ) if  __name__ ==  '__main__' : plot( )