2021.12.22.16:06

Dependencies:   mbed pca9685_2021_12_22 Eigen

Committer:
Kotttaro
Date:
Sun Dec 26 07:43:22 2021 +0000
Revision:
4:8a50c7822dac
Parent:
3:f824e4d8eef7
Child:
5:f225e0c61cfc
2021.12.26; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kotttaro 4:8a50c7822dac 1 //特別研究Ⅰで用いたプログラムを改良したもの
Kotttaro 0:4a5272e014d8 2 //ねじ運動を入力し,0.01秒ごとに1脚について各関節角度を出力する
Kotttaro 2:57237f0a4a34 3 //brent法の部分は『numerical recipes in c』 参照
Kotttaro 4:8a50c7822dac 4 #include "mbed.h"
Kotttaro 4:8a50c7822dac 5 #include <PCA9685.h>
Kotttaro 0:4a5272e014d8 6 #include "Eigen/Geometry.h"
Kotttaro 0:4a5272e014d8 7 #include "Eigen/Dense.h"
Kotttaro 0:4a5272e014d8 8 #include <math.h>
Kotttaro 4:8a50c7822dac 9
Kotttaro 2:57237f0a4a34 10 #define ITMAX 100
Kotttaro 2:57237f0a4a34 11 #define CGOLD 0.3819660
Kotttaro 2:57237f0a4a34 12 #define SHIFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
Kotttaro 2:57237f0a4a34 13 #define ZEPS 1.0e-10
Kotttaro 4:8a50c7822dac 14 #define GOLD 1.618034
Kotttaro 4:8a50c7822dac 15 #define TINY 1.0e-20
Kotttaro 4:8a50c7822dac 16 #define GLIMIT 100.0
Kotttaro 4:8a50c7822dac 17 #define SERVOMIN 700
Kotttaro 4:8a50c7822dac 18 #define SERVOMAX 2300
Kotttaro 4:8a50c7822dac 19 #define SERVOGAIN 29.6296300
Kotttaro 4:8a50c7822dac 20 #define PI 3.14159265358979323846264338327950288
Kotttaro 2:57237f0a4a34 21
Kotttaro 4:8a50c7822dac 22 PCA9685 pwm;//クラス宣言
Kotttaro 0:4a5272e014d8 23 Serial pc2(USBTX,USBRX);
Kotttaro 0:4a5272e014d8 24 Timer tim;
Kotttaro 4:8a50c7822dac 25 Timer manage;
Kotttaro 4:8a50c7822dac 26 int times= 100;//実行回数:実行時間は (sampling)*(times)秒
Kotttaro 0:4a5272e014d8 27
Kotttaro 0:4a5272e014d8 28 using namespace Eigen;
Kotttaro 0:4a5272e014d8 29
Kotttaro 0:4a5272e014d8 30 //以下変数定義
Kotttaro 4:8a50c7822dac 31
Kotttaro 4:8a50c7822dac 32 //brent法に必要な変数
Kotttaro 4:8a50c7822dac 33 double ax=-0.5*PI/180.0,bx=-0.2*PI/180.0,cx=0.0;
Kotttaro 4:8a50c7822dac 34 double fa,fb,fc;
Kotttaro 4:8a50c7822dac 35 //サーボの書き込みに必要な変数
Kotttaro 4:8a50c7822dac 36 double servo0[16]={7800.0, 7250.0, 6000.0, 7150.0, 6100.0, 6300.0, 8400.0, 7200.0, 6800.0, 7000.0, 5700.0, 8350.0, 6100.0, 8500.0, 5600.0, 6570.0};//servoの初期値
Kotttaro 4:8a50c7822dac 37 int ch[4][4]={{0 ,1 ,2 ,3} ,
Kotttaro 4:8a50c7822dac 38 {4 ,5 ,6 ,7} ,
Kotttaro 4:8a50c7822dac 39 {8 ,9 ,10,11},
Kotttaro 4:8a50c7822dac 40 {12,13,14,15} };
Kotttaro 0:4a5272e014d8 41 double r=50*PI/180;//斜面の傾き[°]
Kotttaro 4:8a50c7822dac 42 double sampling=0.1;//δtの時間[s]
Kotttaro 0:4a5272e014d8 43 double L[4] = {50.0,50.0,50.0,50.0};//4本のリンク長 後から足したのでL[3]を理論中のL0に対応させる
Kotttaro 0:4a5272e014d8 44 double tip[4][3];//足先座標
Kotttaro 0:4a5272e014d8 45 double con[4][3] = { 50.0, 50.0,0,
Kotttaro 0:4a5272e014d8 46 -50.0, 50.0,0,
Kotttaro 0:4a5272e014d8 47 -50.0,-50.0,0,
Kotttaro 0:4a5272e014d8 48 50.0,-50.0,0};//脚のコーナー座標,zは必ず0
Kotttaro 4:8a50c7822dac 49 double th[4][4] = { {135 * PI / 180, 60 * PI / 180, -30 * PI / 180, -30 * PI / 180},
Kotttaro 4:8a50c7822dac 50 {45 * PI / 180, 60 * PI / 180, -30 * PI / 180, -30 * PI / 180},
Kotttaro 4:8a50c7822dac 51 {-45 * PI / 180, 60 * PI / 180, -30 * PI / 180, -30 * PI / 180},
Kotttaro 4:8a50c7822dac 52 {-135 * PI / 180, 60 * PI / 180, -30 * PI / 180, -30 * PI / 180} };//サーボに入力する角度
Kotttaro 0:4a5272e014d8 53 double th0[4][4]= { 0.0,0.0,0.0,0.0,
Kotttaro 0:4a5272e014d8 54 0.0,0.0, 0.0,0.0,
Kotttaro 0:4a5272e014d8 55 0.0,0.0, 0.0,0.0,
Kotttaro 0:4a5272e014d8 56 0.0,0.0, 0.0,0.0 }; //計算用の関節角度
Kotttaro 0:4a5272e014d8 57 double Jacbi[4][3][4];//ヤコビアン 脚数×関節×次元
Kotttaro 0:4a5272e014d8 58 double a,a0, h,fi;//評価関数内の変数 fi=φ
Kotttaro 0:4a5272e014d8 59 double X,tan_u, tan_d;//計算用
Kotttaro 0:4a5272e014d8 60
Kotttaro 0:4a5272e014d8 61 //ねじ軸
Kotttaro 0:4a5272e014d8 62 //Lin:方向, L0:原点座標, vin:ねじ時に沿った速度, win:角速度ベクトルの大きさ
Kotttaro 0:4a5272e014d8 63 double Lin[3], L0[3], vin,v[3],wg[3],win,nol;//ねじ軸条件
Kotttaro 0:4a5272e014d8 64 double dfdth[4];//評価関数のナブラ
Kotttaro 0:4a5272e014d8 65
Kotttaro 0:4a5272e014d8 66
Kotttaro 0:4a5272e014d8 67 //以下行列定義
Kotttaro 0:4a5272e014d8 68 MatrixXd Q(3, 3);//Q行列
Kotttaro 0:4a5272e014d8 69 MatrixXd R(3, 4);//R行列
Kotttaro 0:4a5272e014d8 70 Vector3d vP[4];//各脚の速度ベクトル
Kotttaro 0:4a5272e014d8 71
Kotttaro 0:4a5272e014d8 72
Kotttaro 0:4a5272e014d8 73 void QR(int leg);//QR分解用関数,引数は脚番号
Kotttaro 0:4a5272e014d8 74 void vp(int leg);//引数は脚番号,与条件から各脚先の速度を導出する
Kotttaro 0:4a5272e014d8 75 void fwd(int leg);//順運動学より脚先の座標を導出する
Kotttaro 0:4a5272e014d8 76 void Jac(int leg);//指定した脚のヤコビアンを計算
Kotttaro 0:4a5272e014d8 77 void deff(int leg);//評価関数計算, legは距離と傾きから指定する
Kotttaro 0:4a5272e014d8 78 void dfd( int leg);//評価関数の勾配をとる
Kotttaro 0:4a5272e014d8 79 double search(int leg);//最大のthetaを探索するための関数
Kotttaro 0:4a5272e014d8 80 void solve(double w3,int leg,int det);//theta3の角速度から全関節の関節角度を導き出す
Kotttaro 2:57237f0a4a34 81 double fe(int leg,double dth3);//brent法に合わせてeを関数化,search文を一部抜粋したもの
Kotttaro 2:57237f0a4a34 82 double num_nolm(int leg , double dth3);//ノルム最小の解を導く際に使用する関数
Kotttaro 3:f824e4d8eef7 83 double f(int leg,double dth3);//テーラー展開第1項の値を返す, brent法用
Kotttaro 4:8a50c7822dac 84 void mnbrak(int leg,int discrimination);//brentに必要な極小値の囲い込んだ3点を決定する関数
Kotttaro 3:f824e4d8eef7 85 double brent(int leg,double min,double mid,double max,double tol,int discrimination);//brent法により1次元探索するプログラム
Kotttaro 4:8a50c7822dac 86 //discrimination 0:谷側(fe), 1:山側(nolm), 2:谷側(f),3:テスト用の関数(f_test)
Kotttaro 2:57237f0a4a34 87 double SIGN(double x,double y);//xにyの符号をつけたものを返す
Kotttaro 4:8a50c7822dac 88 double FMAX(double x,double y);//大きいほうの値が返される
Kotttaro 4:8a50c7822dac 89 double f_test(double x);//テスト用の関数
Kotttaro 4:8a50c7822dac 90
Kotttaro 4:8a50c7822dac 91 //以下サーボ関係
Kotttaro 4:8a50c7822dac 92 void setup_servo();//サーボセットアップ用関数
Kotttaro 4:8a50c7822dac 93 void servo_write(int ch,double ang);//angに
Kotttaro 4:8a50c7822dac 94 void servo_write7(int ch, double ang);
Kotttaro 4:8a50c7822dac 95 void servo_calib();//全ての角度を0度にする
Kotttaro 2:57237f0a4a34 96
Kotttaro 0:4a5272e014d8 97
Kotttaro 4:8a50c7822dac 98 //以下9軸センサ関係
Kotttaro 0:4a5272e014d8 99 int main()
Kotttaro 0:4a5272e014d8 100 {
Kotttaro 0:4a5272e014d8 101 double t;
Kotttaro 0:4a5272e014d8 102 pc2.baud(921600);
Kotttaro 4:8a50c7822dac 103 setup_servo();
Kotttaro 4:8a50c7822dac 104 servo_calib();
Kotttaro 4:8a50c7822dac 105 wait(2);
Kotttaro 4:8a50c7822dac 106
Kotttaro 4:8a50c7822dac 107 for(int u=0; u<4; u++)
Kotttaro 4:8a50c7822dac 108 {
Kotttaro 4:8a50c7822dac 109 for(int i=0; i<4; i++)
Kotttaro 4:8a50c7822dac 110 {
Kotttaro 4:8a50c7822dac 111 servo_write(ch[u][i],th[u][i]);
Kotttaro 4:8a50c7822dac 112 }
Kotttaro 4:8a50c7822dac 113 }
Kotttaro 4:8a50c7822dac 114 wait(2);
Kotttaro 0:4a5272e014d8 115 int count = 0;
Kotttaro 0:4a5272e014d8 116 //入力したねじ運動を換算する
Kotttaro 0:4a5272e014d8 117 Lin[0] = 0.0; //ねじ軸x
Kotttaro 4:8a50c7822dac 118 Lin[1] = 0.0; //ねじ軸y
Kotttaro 0:4a5272e014d8 119 Lin[2] = 1.0;//ねじ軸z
Kotttaro 0:4a5272e014d8 120 L0[0] = 0.0;//ねじ軸原点座標
Kotttaro 0:4a5272e014d8 121 L0[1] = 0.0;
Kotttaro 0:4a5272e014d8 122 L0[2] = 0.0;
Kotttaro 4:8a50c7822dac 123 vin = 0.0;
Kotttaro 4:8a50c7822dac 124 win = 3.0;
Kotttaro 2:57237f0a4a34 125 printf("\r\n\r\n");
Kotttaro 0:4a5272e014d8 126 nol = (double)sqrt(Lin[0] * Lin[0] + Lin[1] * Lin[1] + Lin[2] * Lin[2]);
Kotttaro 0:4a5272e014d8 127 for (int i = 0; i < 3; i++)
Kotttaro 0:4a5272e014d8 128 {
Kotttaro 0:4a5272e014d8 129 wg[i] = Lin[i] * win / nol;
Kotttaro 0:4a5272e014d8 130 v[i] = Lin[i] * vin / nol;
Kotttaro 0:4a5272e014d8 131 }
Kotttaro 0:4a5272e014d8 132
Kotttaro 0:4a5272e014d8 133 for (int i=0; i < 4; i++) {
Kotttaro 0:4a5272e014d8 134 fwd(i);
Kotttaro 0:4a5272e014d8 135 vp(i);
Kotttaro 0:4a5272e014d8 136 }
Kotttaro 0:4a5272e014d8 137 //printf("%lf , %lf , %lf",vP[0](0,0), vP[0](1, 0), vP[0](2, 0));
Kotttaro 0:4a5272e014d8 138
Kotttaro 0:4a5272e014d8 139 //times*δtの時間だけサーボを動かす
Kotttaro 0:4a5272e014d8 140 tim.start();
Kotttaro 4:8a50c7822dac 141 manage.start();
Kotttaro 0:4a5272e014d8 142 for (int i = 0; i < times;i++){
Kotttaro 4:8a50c7822dac 143 manage.reset();
Kotttaro 0:4a5272e014d8 144 count = count + 1;
Kotttaro 0:4a5272e014d8 145 double dth;
Kotttaro 4:8a50c7822dac 146 //////////計算部/////////////////
Kotttaro 0:4a5272e014d8 147 fwd(0);
Kotttaro 0:4a5272e014d8 148 vp(0);
Kotttaro 0:4a5272e014d8 149 Jac(0);
Kotttaro 0:4a5272e014d8 150 QR(0);
Kotttaro 0:4a5272e014d8 151 deff(0);
Kotttaro 4:8a50c7822dac 152 dfd(0);
Kotttaro 4:8a50c7822dac 153 ax=-0.5*PI/180.0;bx=-0.49*PI/180.0;cx=0.0;
Kotttaro 4:8a50c7822dac 154 mnbrak(0,2);
Kotttaro 4:8a50c7822dac 155 dth=brent(0,ax,bx,cx,0.0001,2);
Kotttaro 4:8a50c7822dac 156 solve(dth, 0, 1);
Kotttaro 4:8a50c7822dac 157 ////////////////////////////////
Kotttaro 4:8a50c7822dac 158
Kotttaro 4:8a50c7822dac 159 for(int u=0; u<4; u++)
Kotttaro 4:8a50c7822dac 160 {
Kotttaro 4:8a50c7822dac 161 for(int i=0; i<4; i++)
Kotttaro 4:8a50c7822dac 162 {
Kotttaro 4:8a50c7822dac 163 servo_write(ch[u][i],th[u][i]);
Kotttaro 4:8a50c7822dac 164 }
Kotttaro 4:8a50c7822dac 165 }
Kotttaro 0:4a5272e014d8 166 t=tim.read();
Kotttaro 4:8a50c7822dac 167 pc2.printf("%2.4lf:(%3.3lf, %3.3lf, %3.3lf, %3.3lf)\n\r",t,th[0][0]*180/PI, th[0][1]*180/PI , th[0][2]*180/PI , th[0][3]*180/PI );
Kotttaro 4:8a50c7822dac 168
Kotttaro 4:8a50c7822dac 169 while(1)
Kotttaro 4:8a50c7822dac 170 {
Kotttaro 4:8a50c7822dac 171 if(manage.read()>sampling)break;
Kotttaro 4:8a50c7822dac 172 }
Kotttaro 4:8a50c7822dac 173
Kotttaro 0:4a5272e014d8 174 }
Kotttaro 1:5c2562adca7d 175 t=tim.read();
Kotttaro 4:8a50c7822dac 176 wait(3);
Kotttaro 4:8a50c7822dac 177 servo_calib();
Kotttaro 0:4a5272e014d8 178 return 0; // ソフトの終了
Kotttaro 0:4a5272e014d8 179 }
Kotttaro 0:4a5272e014d8 180
Kotttaro 0:4a5272e014d8 181 void QR(int leg) {
Kotttaro 0:4a5272e014d8 182 double s, t;//要素計算用
Kotttaro 0:4a5272e014d8 183 MatrixXd ma(3, 4), ma1(3, 4);
Kotttaro 0:4a5272e014d8 184
Kotttaro 0:4a5272e014d8 185 ma << Jacbi[leg][0][0], Jacbi[leg][0][1], Jacbi[leg][0][2], Jacbi[leg][0][3],
Kotttaro 0:4a5272e014d8 186 Jacbi[leg][1][0], Jacbi[leg][1][1], Jacbi[leg][1][2], Jacbi[leg][1][3],
Kotttaro 0:4a5272e014d8 187 Jacbi[leg][2][0], Jacbi[leg][2][1], Jacbi[leg][2][2], Jacbi[leg][2][3];
Kotttaro 4:8a50c7822dac 188
Kotttaro 0:4a5272e014d8 189 //ハウスホルダー変換1回目
Kotttaro 0:4a5272e014d8 190 MatrixXd A1(3, 3);
Kotttaro 0:4a5272e014d8 191 A1 << 1.0, 0.0, 0.0,
Kotttaro 0:4a5272e014d8 192 0.0, 1.0, 0.0,
Kotttaro 0:4a5272e014d8 193 0.0, 0.0, 1.0;
Kotttaro 4:8a50c7822dac 194
Kotttaro 0:4a5272e014d8 195 s = (double)sqrt(ma(0, 0) * ma(0, 0) + ma(1, 0) * ma(1, 0) + ma(2, 0) * ma(2, 0));//分母のやつ
Kotttaro 4:8a50c7822dac 196
Kotttaro 0:4a5272e014d8 197 MatrixXd H1(3, 3);//1回目の行列
Kotttaro 0:4a5272e014d8 198 MatrixXd X11(3, 1), X12(1, 3);
Kotttaro 0:4a5272e014d8 199 Vector3d a11, a12;//a11が変換前,a12が変換後
Kotttaro 0:4a5272e014d8 200 a11 << ma(0, 0), ma(1, 0), ma(2, 0);
Kotttaro 0:4a5272e014d8 201 a12 << s, 0.0, 0.0;
Kotttaro 0:4a5272e014d8 202 X11 = a11 - a12;
Kotttaro 0:4a5272e014d8 203 X12 = X11.transpose();
Kotttaro 0:4a5272e014d8 204 t = (double)sqrt(X11(0, 0) * X11(0, 0) + X11(1, 0) * X11(1, 0) + X11(2, 0) * X11(2, 0));
Kotttaro 0:4a5272e014d8 205 H1 = A1 - 2.0 * (X11 * X12) / (t * t);
Kotttaro 0:4a5272e014d8 206 ma1 = H1 * ma;
Kotttaro 0:4a5272e014d8 207 //2回目
Kotttaro 0:4a5272e014d8 208 MatrixXd H2(3, 3), A2(2, 2), h2(2, 2);
Kotttaro 0:4a5272e014d8 209 A2 << 1.0, 0.0,
Kotttaro 0:4a5272e014d8 210 0.0, 1.0;
Kotttaro 0:4a5272e014d8 211 Vector2d a21, a22;
Kotttaro 0:4a5272e014d8 212 MatrixXd X21(2, 1), X22(1, 2);
Kotttaro 0:4a5272e014d8 213 a21 << ma1(1, 1), ma1(2, 1);
Kotttaro 0:4a5272e014d8 214 s = (double)sqrt(ma1(1, 1) * ma1(1, 1) + ma1(2, 1) * ma1(2, 1));
Kotttaro 0:4a5272e014d8 215 a22 << s, 0;
Kotttaro 0:4a5272e014d8 216 X21 = a21 - a22;
Kotttaro 0:4a5272e014d8 217 X22 = X21.transpose();
Kotttaro 0:4a5272e014d8 218 t = (double)sqrt(X21(0, 0) * X21(0, 0) + X21(1, 0) * X21(1, 0));
Kotttaro 0:4a5272e014d8 219 h2 = A2 - 2 * (X21 * X22) / (t * t);
Kotttaro 0:4a5272e014d8 220 H2 << 1.0, 0.0, 0.0,
Kotttaro 0:4a5272e014d8 221 0.0, h2(0, 0), h2(0, 1),
Kotttaro 0:4a5272e014d8 222 0.0, h2(1, 0), h2(1, 1);
Kotttaro 0:4a5272e014d8 223 R = H2 * ma1;
Kotttaro 0:4a5272e014d8 224 MatrixXd H1T(3, 3), H2T(3, 3);
Kotttaro 0:4a5272e014d8 225 H1T = H1.transpose();
Kotttaro 0:4a5272e014d8 226 H2T = H2.transpose();
Kotttaro 4:8a50c7822dac 227 Q = H1T * H2T;
Kotttaro 0:4a5272e014d8 228 }
Kotttaro 0:4a5272e014d8 229
Kotttaro 0:4a5272e014d8 230 void vp(int leg) {//5年生の時に作成したもの
Kotttaro 0:4a5272e014d8 231 double crosx, crosy, crosz;
Kotttaro 0:4a5272e014d8 232 double wA[3] = { (double)(-wg[0] * PI / 180.0),(double)(-wg[1] * PI / 180.0),(double)(-wg[2] * PI / 180.0) };
Kotttaro 0:4a5272e014d8 233 double vA[3] = { (-v[0]),(-v[1]) ,(-v[2]) };
Kotttaro 0:4a5272e014d8 234 double AP[3] = { (tip[leg][0] - L0[0]),(tip[leg][1] - L0[1]),tip[leg][2] - L0[2] };
Kotttaro 0:4a5272e014d8 235 if (Lin[2] != 0.0)
Kotttaro 0:4a5272e014d8 236 {
Kotttaro 0:4a5272e014d8 237 double LP[3] = { -(Lin[0] / nol) / (Lin[2] / nol) * tip[leg][2],-(Lin[1] / nol) / (Lin[2] / nol) * tip[leg][2],0.0 };
Kotttaro 0:4a5272e014d8 238 for (int i = 0; i < 3; i++) { AP[i] = AP[i] - LP[i]; }
Kotttaro 0:4a5272e014d8 239 AP[2] = 0.0;
Kotttaro 0:4a5272e014d8 240 }
Kotttaro 0:4a5272e014d8 241 crosx = AP[1] * wA[2] + (-AP[2]) * wA[1];
Kotttaro 0:4a5272e014d8 242 crosy = AP[2] * wA[0] + (-AP[0]) * wA[2];
Kotttaro 0:4a5272e014d8 243 crosz = AP[0] * wA[1] + (-AP[1]) * wA[0];
Kotttaro 0:4a5272e014d8 244 vP[leg] << crosx + vA[0], crosy + vA[1], crosz + vA[2];
Kotttaro 0:4a5272e014d8 245 //printf(" %lf,%lf,%lf\n", -v[0], -v[1], -v[2]);
Kotttaro 0:4a5272e014d8 246 //pc2.printf("input motion %d %lf,%lf,%lf\n\r", leg, vP[leg](0, 0), vP[leg](1, 0), vP[leg](2, 0));
Kotttaro 0:4a5272e014d8 247 //printf("vp finish\n");
Kotttaro 0:4a5272e014d8 248 }
Kotttaro 0:4a5272e014d8 249 void fwd(int leg) {
Kotttaro 0:4a5272e014d8 250 //printf("fwd start\n");
Kotttaro 0:4a5272e014d8 251 double c0 = (double)cos(th[leg][0]), s0 = (double)sin(th[leg][0]), c1 = (double)cos(th[leg][1]), s1 = (double)sin(th[leg][1]),
Kotttaro 0:4a5272e014d8 252 c12 = (double)cos(th[leg][1] + th[leg][2]), s12 = (double)sin(th[leg][1] + th[leg][2]), c123 = (double)cos(th[leg][1] + th[leg][2] + th[leg][3]),
Kotttaro 0:4a5272e014d8 253 s123 = (double)sin(th[leg][1] + th[leg][2] + th[leg][3]);
Kotttaro 0:4a5272e014d8 254 tip[leg][0] = (L[3]+L[0] * c1 + L[1] * c12 + L[2]*c123) * c0 + con[leg][0]; //x
Kotttaro 0:4a5272e014d8 255 tip[leg][1] = (L[3]+L[0] * c1 + L[1] * c12 + L[2] * c123) * s0 + con[leg][1]; //y
Kotttaro 0:4a5272e014d8 256 tip[leg][2] = L[0] * s1 + L[1] * s12+L[2]*s123; //z
Kotttaro 0:4a5272e014d8 257 //printf("fwd finish\n");
Kotttaro 0:4a5272e014d8 258 }
Kotttaro 0:4a5272e014d8 259 void Jac(int leg) {
Kotttaro 0:4a5272e014d8 260 //printf("Jac start\n");
Kotttaro 0:4a5272e014d8 261 double c0 = (double)cos(th[leg][0]), s0 = (double)sin(th[leg][0]), c1 = (double)cos(th[leg][1]), s1 = (double)sin(th[leg][1]),
Kotttaro 0:4a5272e014d8 262 c12 = (double)cos(th[leg][1] + th[leg][2]), s12 = (double)sin(th[leg][1] + th[leg][2]), c123 = (double)cos(th[leg][1] + th[leg][2] + th[leg][3]),
Kotttaro 0:4a5272e014d8 263 s123 = (double)sin(th[leg][1] + th[leg][2] + th[leg][3]);
Kotttaro 0:4a5272e014d8 264 Jacbi[leg][0][0] = -s0 * (L[3]+L[0] * c1 + L[1] * c12 + L[2] * c123);
Kotttaro 0:4a5272e014d8 265 Jacbi[leg][0][1] = (-L[0] * s1 - L[1] * s12 - L[2] * s123) * c0;
Kotttaro 0:4a5272e014d8 266 Jacbi[leg][0][2] = (-L[1] * s12 - L[2] * s123) * c0;
Kotttaro 0:4a5272e014d8 267 Jacbi[leg][0][3] = (-L[2] * s123) * c0;
Kotttaro 0:4a5272e014d8 268
Kotttaro 0:4a5272e014d8 269 Jacbi[leg][1][0] = c0 * (L[3]+L[0] * c1 + L[1] * c12 + L[2] * c123);
Kotttaro 0:4a5272e014d8 270 Jacbi[leg][1][1] = (-L[0] * s1 - L[1] * s12 - L[2] * s123) * s0;
Kotttaro 0:4a5272e014d8 271 Jacbi[leg][1][2] = (-L[1] * s12 - L[2] * s123) * s0;
Kotttaro 0:4a5272e014d8 272 Jacbi[leg][1][3] = (-L[2] * s123) * s0;
Kotttaro 0:4a5272e014d8 273
Kotttaro 0:4a5272e014d8 274 Jacbi[leg][2][0] = 0.0;
Kotttaro 0:4a5272e014d8 275 Jacbi[leg][2][1] = L[0] * c1 + L[1] * c12 + L[2] * c123;
Kotttaro 0:4a5272e014d8 276 Jacbi[leg][2][2] = L[1] * c12 + L[2] * c123;
Kotttaro 0:4a5272e014d8 277 Jacbi[leg][2][3] = L[2] * c123;
Kotttaro 0:4a5272e014d8 278
Kotttaro 0:4a5272e014d8 279
Kotttaro 0:4a5272e014d8 280 //printf("Jac finish\n");
Kotttaro 0:4a5272e014d8 281 }//ok
Kotttaro 0:4a5272e014d8 282 void deff(int leg) {
Kotttaro 0:4a5272e014d8 283 //printf(" 評価関数定義\n");
Kotttaro 0:4a5272e014d8 284 fi = r + atan2(-tip[leg][2], (double)sqrt((tip[leg][0]) * (tip[leg][0]) + (tip[leg][1]) * (tip[leg][1])));//y,xの順
Kotttaro 0:4a5272e014d8 285 a0 = (double)sqrt((tip[leg][0]) * (tip[leg][0]) + (tip[leg][1]) * (tip[leg][1]) + (tip[leg][2]) * (tip[leg][2]));
Kotttaro 0:4a5272e014d8 286 a = a0 * (double)cos(fi);
Kotttaro 0:4a5272e014d8 287 h = a * (1 / (double)cos(fi) - tan(fi));
Kotttaro 0:4a5272e014d8 288 X = tip[leg][2]*(double)sqrt((tip[leg][0]* (tip[leg][0])) + (tip[leg][1]) * (tip[leg][1]));//tan-1の中身
Kotttaro 0:4a5272e014d8 289 //tan-1の分母分子
Kotttaro 0:4a5272e014d8 290 tan_u = tip[leg][2];
Kotttaro 0:4a5272e014d8 291 tan_d = (double)sqrt((tip[leg][0]) * (tip[leg][0]) + (tip[leg][1]) * (tip[leg][1]));
Kotttaro 0:4a5272e014d8 292 //printf("評価関数計算完了\n");
Kotttaro 0:4a5272e014d8 293 }
Kotttaro 0:4a5272e014d8 294 void dfd(int leg) {
Kotttaro 0:4a5272e014d8 295 //printf("評価関数微分\n");
Kotttaro 0:4a5272e014d8 296 double c0 = (double)cos(th[leg][0]), s0 = (double)sin(th[leg][0]), c1 = (double)cos(th[leg][1]), s1 = (double)sin(th[leg][1]), s2 = (double)sin(th[leg][2]), s3 = (double)sin(th[leg][2]);
Kotttaro 0:4a5272e014d8 297 double c12 = (double)cos(th[leg][1] + th[leg][2]), s12 = (double)sin(th[leg][1] + th[leg][2]), s23 = (double)sin(th[leg][2] + th[leg][3]), c23 = (double)cos(th[leg][2] + th[leg][3]);
Kotttaro 0:4a5272e014d8 298 double c123 = (double)cos(th[leg][1] + th[leg][2] + th[leg][3]), s123 = (double)sin(th[leg][1] + th[leg][2] + th[leg][3]);
Kotttaro 1:5c2562adca7d 299 double cfi=cos(fi),sfi=sin(fi);
Kotttaro 1:5c2562adca7d 300 double x=tip[leg][0],y=tip[leg][1],z=tip[leg][2];
Kotttaro 1:5c2562adca7d 301
Kotttaro 1:5c2562adca7d 302 double df_da=1/cfi-tan(fi);
Kotttaro 1:5c2562adca7d 303 double df_dfi=a*(-sfi-1)/(cfi*cfi);
Kotttaro 1:5c2562adca7d 304 double da_dx=x*cfi/sqrt(x*x+y*y);
Kotttaro 1:5c2562adca7d 305 double da_dy=y*cfi/sqrt(x*x+y*y);
Kotttaro 1:5c2562adca7d 306 double da_dfi=-sqrt(x*x+y*y)*sfi;
Kotttaro 1:5c2562adca7d 307 double dfi_dx=-x*z/((x*x+y*y+z*z)*sqrt(x*x+y*y));
Kotttaro 1:5c2562adca7d 308 double dfi_dy=-y*z/((x*x+y*y+z*z)*sqrt(x*x+y*y));
Kotttaro 1:5c2562adca7d 309 double dfi_dz=sqrt(x*x+y*y)*z/(x*x+y*y+z*z);
Kotttaro 1:5c2562adca7d 310
Kotttaro 1:5c2562adca7d 311 dfdth[0]=df_da*(da_dx*Jacbi[leg][0][0]+da_dy*Jacbi[leg][1][0]+da_dfi*(dfi_dx*Jacbi[leg][0][0]+dfi_dy*Jacbi[leg][1][0]+dfi_dz*Jacbi[leg][2][0]))
Kotttaro 1:5c2562adca7d 312 +df_dfi*(dfi_dx*Jacbi[leg][0][0]+dfi_dy*Jacbi[leg][1][0]+dfi_dz*Jacbi[leg][2][0]);
Kotttaro 1:5c2562adca7d 313
Kotttaro 1:5c2562adca7d 314 dfdth[1]=df_da*(da_dx*Jacbi[leg][0][1]+da_dy*Jacbi[leg][1][1]+da_dfi*(dfi_dx*Jacbi[leg][0][1]+dfi_dy*Jacbi[leg][1][1]+dfi_dz*Jacbi[leg][2][1]))
Kotttaro 1:5c2562adca7d 315 +df_dfi*(dfi_dx*Jacbi[leg][0][1]+dfi_dy*Jacbi[leg][1][1]+dfi_dz*Jacbi[leg][2][1]);
Kotttaro 1:5c2562adca7d 316
Kotttaro 1:5c2562adca7d 317 dfdth[2]=df_da*(da_dx*Jacbi[leg][0][2]+da_dy*Jacbi[leg][1][2]+da_dfi*(dfi_dx*Jacbi[leg][0][2]+dfi_dy*Jacbi[leg][1][2]+dfi_dz*Jacbi[leg][2][2]))
Kotttaro 1:5c2562adca7d 318 +df_dfi*(dfi_dx*Jacbi[leg][0][2]+dfi_dy*Jacbi[leg][1][2]+dfi_dz*Jacbi[leg][2][2]);
Kotttaro 1:5c2562adca7d 319
Kotttaro 1:5c2562adca7d 320 dfdth[3]=df_da*(da_dx*Jacbi[leg][0][3]+da_dy*Jacbi[leg][1][3]+da_dfi*(dfi_dx*Jacbi[leg][0][3]+dfi_dy*Jacbi[leg][1][3]+dfi_dz*Jacbi[leg][2][3]))
Kotttaro 1:5c2562adca7d 321 +df_dfi*(dfi_dx*Jacbi[leg][0][3]+dfi_dy*Jacbi[leg][1][3]+dfi_dz*Jacbi[leg][2][3]);
Kotttaro 1:5c2562adca7d 322
Kotttaro 3:f824e4d8eef7 323 //pc2.printf("df_da=%lf df_dfi=%lf da_dx=%lf da_dy=%lf da_dfi=%lf dfi_dx=%lf dfi_dy=%lf dfi_dz=%lf\r\n",df_da,df_dfi,da_dx,da_dy,da_dfi,dfi_dx,dfi_dy,dfi_dz);
Kotttaro 0:4a5272e014d8 324 }
Kotttaro 4:8a50c7822dac 325 //使わない
Kotttaro 2:57237f0a4a34 326 double fe(int leg,double dth3) {
Kotttaro 2:57237f0a4a34 327 //brent法のための関数, 事前にdfdを実行してから使う
Kotttaro 3:f824e4d8eef7 328 double dfd_nolm,th0_nolm,e=0.0;
Kotttaro 2:57237f0a4a34 329 //∇hを正規化する
Kotttaro 3:f824e4d8eef7 330 dfd(leg);
Kotttaro 2:57237f0a4a34 331 dfd_nolm = sqrt(dfdth[0]* dfdth[0]+ dfdth[1]* dfdth[1]+ dfdth[2]* dfdth[2]+ dfdth[3]* dfdth[3]);
Kotttaro 2:57237f0a4a34 332 for (int i = 0; i < 4; i++) {
Kotttaro 2:57237f0a4a34 333 dfdth[i]=dfdth[i]/dfd_nolm;
Kotttaro 2:57237f0a4a34 334 }
Kotttaro 2:57237f0a4a34 335
Kotttaro 2:57237f0a4a34 336 solve(dth3, leg, 2);//後退代入でほかの3つのパラメータを導出
Kotttaro 2:57237f0a4a34 337
Kotttaro 2:57237f0a4a34 338 //dthベクトルを正規化
Kotttaro 2:57237f0a4a34 339 th0_nolm = sqrt(th0[leg][0] * th0[leg][0]+ th0[leg][1]* th0[leg][1]+ th0[leg][2]* th0[leg][2]+ th0[leg][3]*th0[leg][3]);
Kotttaro 2:57237f0a4a34 340 for (int i = 0; i < 4; i++) {
Kotttaro 2:57237f0a4a34 341 th0[leg][i] = th0[leg][i] / th0_nolm;
Kotttaro 2:57237f0a4a34 342 }
Kotttaro 2:57237f0a4a34 343 for (int i = 0; i < 4; i++) {
Kotttaro 2:57237f0a4a34 344 e += (th0[leg][i] - dfdth[i]) * (th0[leg][i] - dfdth[i]);
Kotttaro 4:8a50c7822dac 345 }
Kotttaro 2:57237f0a4a34 346 return e;//eベクトルのノルムの2乗を返す
Kotttaro 0:4a5272e014d8 347
Kotttaro 2:57237f0a4a34 348 }
Kotttaro 3:f824e4d8eef7 349 double f(int leg,double dth3) {
Kotttaro 3:f824e4d8eef7 350 double f_return=0.0;
Kotttaro 3:f824e4d8eef7 351 solve(dth3, leg, 2);//後退代入でほかの3つのパラメータを導出
Kotttaro 3:f824e4d8eef7 352 f_return=dfdth[0]*th0[leg][0]+dfdth[1]*th0[leg][1]+dfdth[2]*th0[leg][2]+dfdth[3]*th0[leg][3];
Kotttaro 4:8a50c7822dac 353
Kotttaro 4:8a50c7822dac 354 return -f_return;//テイラー展開第二項を返すがbrent法は極小値を求めるため、符号を反転させる
Kotttaro 3:f824e4d8eef7 355
Kotttaro 4:8a50c7822dac 356 }
Kotttaro 4:8a50c7822dac 357 void mnbrak(int leg,int discrimination)
Kotttaro 2:57237f0a4a34 358 {
Kotttaro 4:8a50c7822dac 359 double ulim,u,r,q,fu,fu_2,dum,fa,fb,fc;
Kotttaro 4:8a50c7822dac 360 //fa=f(ax);
Kotttaro 4:8a50c7822dac 361 //fb=f(bx);
Kotttaro 4:8a50c7822dac 362 if(discrimination==0){fa=fe(leg,ax);fb=fe(leg,bx);}
Kotttaro 4:8a50c7822dac 363 if(discrimination==1){fa=num_nolm(leg,ax);fb=num_nolm(leg,bx);}
Kotttaro 4:8a50c7822dac 364 if(discrimination==2){fa=f(leg,ax);fb=f(leg,bx);}
Kotttaro 4:8a50c7822dac 365 if(discrimination==3){fa=f_test(ax);fb=f_test(bx);}
Kotttaro 4:8a50c7822dac 366
Kotttaro 4:8a50c7822dac 367 if(fb>fa)
Kotttaro 4:8a50c7822dac 368 {
Kotttaro 4:8a50c7822dac 369 SHIFT(dum,ax,bx,dum);
Kotttaro 4:8a50c7822dac 370 SHIFT(dum,fb,fa,dum);
Kotttaro 4:8a50c7822dac 371 }
Kotttaro 4:8a50c7822dac 372 cx=bx+GOLD*(bx-ax);
Kotttaro 4:8a50c7822dac 373 //fc=f(cx);
Kotttaro 4:8a50c7822dac 374 if(discrimination==0){fc=fe(leg,cx);}
Kotttaro 4:8a50c7822dac 375 if(discrimination==1){fc=num_nolm(leg,cx);}
Kotttaro 4:8a50c7822dac 376 if(discrimination==2){fc=f(leg,cx);}
Kotttaro 4:8a50c7822dac 377 if(discrimination==3){fc=f_test(cx);}
Kotttaro 4:8a50c7822dac 378 while (fb>fc)
Kotttaro 4:8a50c7822dac 379 {
Kotttaro 4:8a50c7822dac 380 r=(bx-ax)*(fb-fc);
Kotttaro 4:8a50c7822dac 381 q=(bx-cx)*(fb-fa);
Kotttaro 4:8a50c7822dac 382 u=bx-((bx-cx)*q-(bx-cx)*r)/
Kotttaro 4:8a50c7822dac 383 (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
Kotttaro 4:8a50c7822dac 384 ulim=bx+GLIMIT*(cx-bx);
Kotttaro 4:8a50c7822dac 385
Kotttaro 4:8a50c7822dac 386 if((bx-u)*(u-cx)>0.0)
Kotttaro 4:8a50c7822dac 387 {
Kotttaro 4:8a50c7822dac 388 //fu=f(u);
Kotttaro 4:8a50c7822dac 389 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 390 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 391 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 392 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 393 if(fu<fc)
Kotttaro 4:8a50c7822dac 394 {
Kotttaro 4:8a50c7822dac 395 ax=bx;
Kotttaro 4:8a50c7822dac 396 bx=u;
Kotttaro 4:8a50c7822dac 397 fa=fb;
Kotttaro 4:8a50c7822dac 398 fb=fu;
Kotttaro 4:8a50c7822dac 399 return;
Kotttaro 4:8a50c7822dac 400 }
Kotttaro 4:8a50c7822dac 401 else if(fu>fb)
Kotttaro 4:8a50c7822dac 402 {
Kotttaro 4:8a50c7822dac 403 cx=u;
Kotttaro 4:8a50c7822dac 404 fc=fu;
Kotttaro 4:8a50c7822dac 405 return;
Kotttaro 4:8a50c7822dac 406 }
Kotttaro 4:8a50c7822dac 407 u=cx*+GOLD*(cx-bx);
Kotttaro 4:8a50c7822dac 408 //fu=f(u);
Kotttaro 4:8a50c7822dac 409 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 410 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 411 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 412 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 413 }
Kotttaro 4:8a50c7822dac 414 else if((cx-u)*(u-ulim)>0.0)
Kotttaro 4:8a50c7822dac 415 {
Kotttaro 4:8a50c7822dac 416 //fu=f(u);
Kotttaro 4:8a50c7822dac 417 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 418 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 419 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 420 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 421 if(fu<fc)
Kotttaro 4:8a50c7822dac 422 {
Kotttaro 4:8a50c7822dac 423 SHIFT(bx,cx,u,cx+GOLD*(cx-bx));
Kotttaro 4:8a50c7822dac 424 if(discrimination==0){fu_2=fe(leg,u);}
Kotttaro 4:8a50c7822dac 425 if(discrimination==1){fu_2=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 426 if(discrimination==2){fu_2=f(leg,u);}
Kotttaro 4:8a50c7822dac 427 if(discrimination==3){fu_2=f_test(u);}
Kotttaro 4:8a50c7822dac 428 //SHIFT(fb,fc,fu,f(u));
Kotttaro 4:8a50c7822dac 429 SHIFT(fb,fc,fu,fu_2);
Kotttaro 4:8a50c7822dac 430 }
Kotttaro 4:8a50c7822dac 431
Kotttaro 4:8a50c7822dac 432 }
Kotttaro 4:8a50c7822dac 433 else if((u-ulim)*(ulim-cx)>=0.0)
Kotttaro 4:8a50c7822dac 434 {
Kotttaro 4:8a50c7822dac 435 u=ulim;
Kotttaro 4:8a50c7822dac 436 //fu=f(u);
Kotttaro 4:8a50c7822dac 437 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 438 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 439 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 440 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 441 }
Kotttaro 4:8a50c7822dac 442 else
Kotttaro 4:8a50c7822dac 443 {
Kotttaro 4:8a50c7822dac 444 u=cx+GOLD*(cx-bx);
Kotttaro 4:8a50c7822dac 445 //fu=f(u);
Kotttaro 4:8a50c7822dac 446 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 447 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 448 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 449 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 450 }
Kotttaro 4:8a50c7822dac 451 SHIFT(ax,bx,cx,u);
Kotttaro 4:8a50c7822dac 452 SHIFT(fa,fb,fc,fu);
Kotttaro 4:8a50c7822dac 453 }
Kotttaro 4:8a50c7822dac 454 }
Kotttaro 4:8a50c7822dac 455 double brent(int leg,double min,double mid,double max,double tol,int discrimination)//成功したやつ
Kotttaro 4:8a50c7822dac 456 {
Kotttaro 4:8a50c7822dac 457
Kotttaro 2:57237f0a4a34 458 int iter;
Kotttaro 2:57237f0a4a34 459 double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm,xmin;
Kotttaro 2:57237f0a4a34 460 double e=0.0;
Kotttaro 4:8a50c7822dac 461 a=(ax <cx ? ax : cx);
Kotttaro 4:8a50c7822dac 462 b=(ax >cx ? ax : cx);
Kotttaro 4:8a50c7822dac 463 x=w=v=bx;
Kotttaro 4:8a50c7822dac 464 if(discrimination==0){fw=fv=fx=fe(leg,x);}
Kotttaro 4:8a50c7822dac 465 if(discrimination==1){fw=fv=fx=num_nolm(leg,x);}
Kotttaro 4:8a50c7822dac 466 if(discrimination==2){fw=fv=fx=f(leg,x);}
Kotttaro 4:8a50c7822dac 467 if(discrimination==3){fw=fv=fx=f_test(x);}
Kotttaro 4:8a50c7822dac 468 //fw=fv=fx=f(x);//関数部分
Kotttaro 2:57237f0a4a34 469 for(iter=1;iter<=ITMAX;iter++)
Kotttaro 2:57237f0a4a34 470 {
Kotttaro 2:57237f0a4a34 471 xm=0.5*(a+b);
Kotttaro 2:57237f0a4a34 472 tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
Kotttaro 4:8a50c7822dac 473 //pc2.printf("x =%lf,w = %lf,u = %lf\n\r",x,w,u);
Kotttaro 2:57237f0a4a34 474 if(fabs(x-xm)<=(tol2-0.5*(b-a)))
Kotttaro 2:57237f0a4a34 475 {
Kotttaro 4:8a50c7822dac 476 //pc.printf("bernt out");
Kotttaro 2:57237f0a4a34 477 xmin=x;
Kotttaro 4:8a50c7822dac 478 //pc2.printf("xmin=%lf\r\n",x);
Kotttaro 4:8a50c7822dac 479 //pc2.printf("fx=%lf\r\n",fx);
Kotttaro 2:57237f0a4a34 480 return xmin;
Kotttaro 2:57237f0a4a34 481 }
Kotttaro 2:57237f0a4a34 482 if(fabs(e)>tol1)
Kotttaro 2:57237f0a4a34 483 {
Kotttaro 2:57237f0a4a34 484 r=(x-w)*(fx-fv);
Kotttaro 2:57237f0a4a34 485 q=(x-v)*(fx-fw);
Kotttaro 2:57237f0a4a34 486 p=(x-v)*q-(x-w)*r;
Kotttaro 2:57237f0a4a34 487 q=2.0*(q-r);
Kotttaro 2:57237f0a4a34 488 if(q>0.0)p=-p;
Kotttaro 2:57237f0a4a34 489 q=fabs(q);
Kotttaro 2:57237f0a4a34 490 etemp=e;
Kotttaro 2:57237f0a4a34 491 e=d;
Kotttaro 2:57237f0a4a34 492 if(fabs(p)>=fabs(0.5*q*etemp)||p<=q*(a-x)||p>=q*(b-x))
Kotttaro 2:57237f0a4a34 493 { d=CGOLD*(e= (x>=xm ? a-x : b-x));}
Kotttaro 2:57237f0a4a34 494 else
Kotttaro 2:57237f0a4a34 495 {
Kotttaro 2:57237f0a4a34 496 d=p/q;
Kotttaro 2:57237f0a4a34 497 u=x+d;
Kotttaro 2:57237f0a4a34 498 if(u-a < tol2 || b-u < tol2)
Kotttaro 2:57237f0a4a34 499 {d=SIGN(tol1,xm-x);}
Kotttaro 2:57237f0a4a34 500 }
Kotttaro 2:57237f0a4a34 501 }
Kotttaro 2:57237f0a4a34 502 else
Kotttaro 2:57237f0a4a34 503 {
Kotttaro 2:57237f0a4a34 504 d=CGOLD*(e= (x>=xm ? a-x : b-x));
Kotttaro 2:57237f0a4a34 505 }
Kotttaro 2:57237f0a4a34 506 u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
Kotttaro 4:8a50c7822dac 507 if(discrimination==0){fu=fe(leg,u);}
Kotttaro 4:8a50c7822dac 508 if(discrimination==1){fu=num_nolm(leg,u);}
Kotttaro 4:8a50c7822dac 509 if(discrimination==2){fu=f(leg,u);}
Kotttaro 4:8a50c7822dac 510 if(discrimination==3){fu=f_test(u);}
Kotttaro 4:8a50c7822dac 511 //fu=f(u);//関数部分
Kotttaro 2:57237f0a4a34 512 if(fu <= fx)
Kotttaro 2:57237f0a4a34 513 {
Kotttaro 2:57237f0a4a34 514 if(u >= x)a=x; else b=x;
Kotttaro 2:57237f0a4a34 515 SHIFT(v,w,x,u);
Kotttaro 2:57237f0a4a34 516 SHIFT(fv,fw,fx,fu);
Kotttaro 2:57237f0a4a34 517 }
Kotttaro 2:57237f0a4a34 518 else{
Kotttaro 2:57237f0a4a34 519 if(u < x){a=u;}
Kotttaro 2:57237f0a4a34 520 else {b=u;}
Kotttaro 2:57237f0a4a34 521 if(fu <= fw || w==x)
Kotttaro 2:57237f0a4a34 522 {
Kotttaro 2:57237f0a4a34 523 v=w;
Kotttaro 2:57237f0a4a34 524 w=u;
Kotttaro 2:57237f0a4a34 525 fv=fw;
Kotttaro 2:57237f0a4a34 526 fw=fu;
Kotttaro 2:57237f0a4a34 527 }
Kotttaro 2:57237f0a4a34 528 else if (fu <= fv || v==x || v==w)
Kotttaro 2:57237f0a4a34 529 {
Kotttaro 2:57237f0a4a34 530 v=u;
Kotttaro 2:57237f0a4a34 531 fv=fu;
Kotttaro 2:57237f0a4a34 532 }
Kotttaro 2:57237f0a4a34 533 }
Kotttaro 2:57237f0a4a34 534
Kotttaro 2:57237f0a4a34 535 }
Kotttaro 4:8a50c7822dac 536 //pc2.printf("xmin=%lf\r\n",x);
Kotttaro 4:8a50c7822dac 537 //pc2.printf("fx=%lf\r\n",fx);
Kotttaro 2:57237f0a4a34 538 return xmin;
Kotttaro 4:8a50c7822dac 539 }
Kotttaro 4:8a50c7822dac 540
Kotttaro 2:57237f0a4a34 541 double SIGN(double x,double y)
Kotttaro 2:57237f0a4a34 542 {
Kotttaro 2:57237f0a4a34 543 double x_return;
Kotttaro 2:57237f0a4a34 544 x_return=abs(x);
Kotttaro 2:57237f0a4a34 545 if(y<0.0)x_return=-x_return;
Kotttaro 2:57237f0a4a34 546
Kotttaro 2:57237f0a4a34 547 return x_return;
Kotttaro 2:57237f0a4a34 548 }
Kotttaro 4:8a50c7822dac 549 double FMAX(double x, double y)
Kotttaro 4:8a50c7822dac 550 {
Kotttaro 4:8a50c7822dac 551 if(x>y){return x;}
Kotttaro 4:8a50c7822dac 552 if(y>x){return y;}
Kotttaro 4:8a50c7822dac 553 return 0;
Kotttaro 4:8a50c7822dac 554 }
Kotttaro 2:57237f0a4a34 555 double num_nolm(int leg,double dth3)
Kotttaro 2:57237f0a4a34 556 {
Kotttaro 4:8a50c7822dac 557
Kotttaro 2:57237f0a4a34 558 double nolm_return=0.0;
Kotttaro 4:8a50c7822dac 559 solve(dth3,leg,2);
Kotttaro 4:8a50c7822dac 560 nolm_return=th0[leg][0]*th0[leg][0]+th0[leg][1]*th0[leg][1]+th0[leg][2]*th0[leg][2]+th0[leg][3]*th0[leg][3];
Kotttaro 4:8a50c7822dac 561
Kotttaro 2:57237f0a4a34 562 return nolm_return;
Kotttaro 4:8a50c7822dac 563 }
Kotttaro 4:8a50c7822dac 564 //brent法のテスト用の関数
Kotttaro 4:8a50c7822dac 565 //極小値を求めたい関数を定義
Kotttaro 4:8a50c7822dac 566 double f_test(double x){
Kotttaro 4:8a50c7822dac 567 double x_return;
Kotttaro 4:8a50c7822dac 568 x_return=x*x-2*x+1;
Kotttaro 4:8a50c7822dac 569 return x_return;
Kotttaro 4:8a50c7822dac 570 }
Kotttaro 0:4a5272e014d8 571 void solve(double w3, int leg,int det) {
Kotttaro 4:8a50c7822dac 572
Kotttaro 0:4a5272e014d8 573 double dth[4];
Kotttaro 0:4a5272e014d8 574 MatrixXd v_Q(3,1),QT(3,3);
Kotttaro 0:4a5272e014d8 575
Kotttaro 0:4a5272e014d8 576 QT = Q.transpose();
Kotttaro 0:4a5272e014d8 577 v_Q = QT * vP[leg]*sampling;
Kotttaro 4:8a50c7822dac 578
Kotttaro 2:57237f0a4a34 579 dth[3] = w3 ;
Kotttaro 0:4a5272e014d8 580 dth[2] = (double)((v_Q(2, 0) - R(2, 3) * dth[3]) / R(2, 2));
Kotttaro 0:4a5272e014d8 581 dth[1] = (double)((v_Q(1, 0) - R(1, 2) * dth[2] - R(1, 3) * dth[3]) / R(1, 1));
Kotttaro 0:4a5272e014d8 582 dth[0] = (double)((v_Q(0, 0) - R(0, 1) * dth[1] - R(0, 2)*dth[2] - R(0, 3) * dth[3])/R(0,0));
Kotttaro 0:4a5272e014d8 583 if (det == 1) {
Kotttaro 0:4a5272e014d8 584 for (int i=0; i < 4; i++) {
Kotttaro 4:8a50c7822dac 585 th[leg][i] = th[leg][i] + dth[i];
Kotttaro 0:4a5272e014d8 586 }
Kotttaro 0:4a5272e014d8 587 }
Kotttaro 4:8a50c7822dac 588 else if (det == 2) {
Kotttaro 4:8a50c7822dac 589 for (int u=0; u < 4; u++) {
Kotttaro 4:8a50c7822dac 590 th0[leg][u] = dth[u];
Kotttaro 4:8a50c7822dac 591 }
Kotttaro 4:8a50c7822dac 592 }
Kotttaro 4:8a50c7822dac 593 else {pc2.printf("value det in function solve is incorrect\r\n");}
Kotttaro 4:8a50c7822dac 594
Kotttaro 0:4a5272e014d8 595 }
Kotttaro 0:4a5272e014d8 596
Kotttaro 4:8a50c7822dac 597 //サーボ関係
Kotttaro 4:8a50c7822dac 598 void setup_servo() {
Kotttaro 4:8a50c7822dac 599 pwm.begin();
Kotttaro 4:8a50c7822dac 600 pwm.setPWMFreq(200);
Kotttaro 4:8a50c7822dac 601 }
Kotttaro 4:8a50c7822dac 602 void servo_write(int ch,double ang)//引数は[°]
Kotttaro 4:8a50c7822dac 603 {
Kotttaro 4:8a50c7822dac 604 if(ch==0) ang=ang-135*PI/180;
Kotttaro 4:8a50c7822dac 605 if(ch==4) ang=ang-45*PI/180;
Kotttaro 4:8a50c7822dac 606 if(ch==8) ang=ang+45*PI/180;
Kotttaro 4:8a50c7822dac 607 if(ch==12) ang=ang+135*PI/180;
Kotttaro 4:8a50c7822dac 608 if( (ch!=2) && (ch!=5)&&(ch!=7) && (ch!=10) && (ch!=13)&&(ch!=15) )ang=-ang;
Kotttaro 4:8a50c7822dac 609 ang=servo0[ch]+ang*8000/270*180/PI;
Kotttaro 4:8a50c7822dac 610 servo_write7(ch,ang);
Kotttaro 4:8a50c7822dac 611 }
Kotttaro 4:8a50c7822dac 612
Kotttaro 4:8a50c7822dac 613 void servo_write7(int ch, double ang){
Kotttaro 4:8a50c7822dac 614 ang = ((ang-3500)/8000)*1600+700;//サーボモータ内部エンコーダは8000段階
Kotttaro 4:8a50c7822dac 615 //pc2.printf("%d ang=%5.0lf \r\n ",ch,ang) ; //初期状態を設定するときこの値を参考に設定したためそのまま利用  
Kotttaro 4:8a50c7822dac 616 pwm.setPWM(ch, 0, ang);
Kotttaro 4:8a50c7822dac 617 }
Kotttaro 4:8a50c7822dac 618 void servo_calib()
Kotttaro 4:8a50c7822dac 619 {
Kotttaro 4:8a50c7822dac 620 for(int u=0; u<4; u++)
Kotttaro 4:8a50c7822dac 621 {
Kotttaro 4:8a50c7822dac 622 for(int i=0; i<4; i++)
Kotttaro 4:8a50c7822dac 623 {
Kotttaro 4:8a50c7822dac 624 servo_write7(ch[u][i],servo0[ch[u][i]]);
Kotttaro 4:8a50c7822dac 625 }
Kotttaro 4:8a50c7822dac 626 }
Kotttaro 4:8a50c7822dac 627 }