altb_pmic / Mbed 2 deprecated Test_Realbot

Dependencies:   mbed

Committer:
pmic
Date:
Thu Dec 05 09:02:14 2019 +0000
Revision:
3:e6d345973797
Parent:
2:27f16e37a176
Child:
5:4c03a1a0ad98
Include and test Motion class from Marcel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 0:937360eb9f8c 1 #include "mbed.h"
pmic 0:937360eb9f8c 2 #include "PT1_Controller.h"
pmic 0:937360eb9f8c 3 #include "Target_Planer.h"
pmic 3:e6d345973797 4 #include "Motion.h"
pmic 0:937360eb9f8c 5
pmic 0:937360eb9f8c 6 using namespace std;
pmic 0:937360eb9f8c 7
pmic 0:937360eb9f8c 8 #define pi 3.141592653589793
pmic 0:937360eb9f8c 9
pmic 0:937360eb9f8c 10 Serial pc(SERIAL_TX, SERIAL_RX);
pmic 0:937360eb9f8c 11
pmic 0:937360eb9f8c 12 double kpv = 0.6412; // proportional controller for distance error
pmic 0:937360eb9f8c 13 double kpw = 0.6801; // proportional controller for angle error
pmic 0:937360eb9f8c 14 double Ts = 1/30.0; // samplingrate 30 Hz
pmic 0:937360eb9f8c 15 double Tf = 1/(2.0*pi*4.0); // fg = 4 Hz
pmic 0:937360eb9f8c 16 PT1_Controller distance_cntr(kpv, Ts, Tf);
pmic 0:937360eb9f8c 17 PT1_Controller angle_cntr(kpw, Ts, Tf);
pmic 0:937360eb9f8c 18
pmic 0:937360eb9f8c 19 // (vRx (m/s), wR (rad/s)) -> (vRxRB (unit unknown), wRRB (unit unknown))
pmic 0:937360eb9f8c 20 // Cu2uRB = 2.8070 0
pmic 0:937360eb9f8c 21 // 0 0.9263
pmic 0:937360eb9f8c 22 double kv2RB = 2.8070; // maps desired forward velocity from m/s 2 RB unit
pmic 0:937360eb9f8c 23 double kw2RB = 0.9263; // maps desired turn speed from rad/s 2 RB unit
pmic 0:937360eb9f8c 24
pmic 0:937360eb9f8c 25 Timer timer; // timer for time measurement
pmic 0:937360eb9f8c 26 float dt = 0.0f;
pmic 0:937360eb9f8c 27
pmic 0:937360eb9f8c 28 uint32_t i;
pmic 0:937360eb9f8c 29
pmic 0:937360eb9f8c 30 // user defined functions
pmic 0:937360eb9f8c 31 double quat2psi(double qw, double qx, double qy, double qz);
pmic 0:937360eb9f8c 32 double getShortRotation(double ang);
pmic 0:937360eb9f8c 33
pmic 2:27f16e37a176 34 const int N = 40;
pmic 2:27f16e37a176 35 double Pathx[N] = {7.3000, 7.2500, 7.2000, 7.1500, 7.1000, 7.0500, 7.0000, 6.9500, 6.9000, 6.8500, 6.8000, 6.7500, 6.7000, 6.6500, 6.6000, 6.5500, 6.5000, 6.4500, 6.4000, 6.3500, 6.3000, 6.2500, 6.2000, 6.1500, 6.1000, 6.0500, 6.0000, 5.9500, 5.9000, 5.8500, 5.8000, 5.7500, 5.7000, 5.6500, 5.6000, 5.5500, 5.5000, 5.4500, 5.4000, 5.3500};
pmic 2:27f16e37a176 36 double Pathy[N] = {6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9500, 6.9000, 6.8500, 6.8000, 6.7500, 6.7000, 6.6500, 6.6000, 6.5500, 6.5000, 6.4500, 6.4000, 6.3500, 6.3000, 6.2500};
pmic 2:27f16e37a176 37 double R = 0.116959064327485;
pmic 0:937360eb9f8c 38 Target_Planer target_planer(Pathx, Pathy, (uint16_t)N, R);
pmic 2:27f16e37a176 39 double RobotPosx = 4.35;
pmic 2:27f16e37a176 40 double RobotPosy = 6.19;
pmic 1:e79de7ffd211 41 double TargetPosx = -999.0;
pmic 1:e79de7ffd211 42 double TargetPosy = -999.0;
pmic 1:e79de7ffd211 43 double TargetAng = -999.0;
pmic 0:937360eb9f8c 44
pmic 3:e6d345973797 45 double pos0 = 179.0*pi/180.0;
pmic 3:e6d345973797 46 float vel0 = 0.0f*(float)pi/180.0f;
pmic 3:e6d345973797 47 float velMax = 90.0f*(float)pi/180.0f;
pmic 3:e6d345973797 48 float accMax = 60.0f*(float)pi/180.0f;
pmic 3:e6d345973797 49 Motion motion_planer(pos0, vel0, velMax, accMax);
pmic 3:e6d345973797 50 double targetPos = 0*pi/180.0;
pmic 3:e6d345973797 51 float targetVel = 0*(float)pi/180.0f;
pmic 3:e6d345973797 52
pmic 0:937360eb9f8c 53 int main()
pmic 0:937360eb9f8c 54 {
pmic 0:937360eb9f8c 55 pc.baud(2000000);
pmic 0:937360eb9f8c 56
pmic 0:937360eb9f8c 57 timer.start();
pmic 0:937360eb9f8c 58
pmic 0:937360eb9f8c 59 i = 0;
pmic 0:937360eb9f8c 60
pmic 0:937360eb9f8c 61 // reset internal filter storage to zero
pmic 0:937360eb9f8c 62 distance_cntr.reset();
pmic 0:937360eb9f8c 63 angle_cntr.reset();
pmic 0:937360eb9f8c 64
pmic 0:937360eb9f8c 65 while(1) {
pmic 0:937360eb9f8c 66
pmic 0:937360eb9f8c 67 dt = timer.read();
pmic 0:937360eb9f8c 68 timer.reset();
pmic 2:27f16e37a176 69
pmic 2:27f16e37a176 70 target_planer.initialize(RobotPosx, RobotPosy);
pmic 0:937360eb9f8c 71
pmic 0:937360eb9f8c 72 /*
pmic 0:937360eb9f8c 73 double phiR = quat2psi(double qw, double qx, double qy, double qz); // transform actual robot orientation (quaternioni) into turning angle
pmic 0:937360eb9f8c 74 // it is assumed that target_ang and phiR both lie between -pi and pi
pmic 0:937360eb9f8c 75 double e_ang = getShortRotation(target_angle - phiR);
pmic 0:937360eb9f8c 76 // update angle controller
pmic 0:937360eb9f8c 77 */
pmic 0:937360eb9f8c 78 // double qw = 0.984005578673161;
pmic 0:937360eb9f8c 79 // double qx = 0.009063618716137;
pmic 0:937360eb9f8c 80 // double qy = 0.177899336159591;
pmic 0:937360eb9f8c 81 // double qz = 0.001629344754243;
pmic 0:937360eb9f8c 82
pmic 3:e6d345973797 83 if(i < 200) {
pmic 0:937360eb9f8c 84 // pc.printf("%i; %.12f; %.12f; %0.12f; %.6f;\r\n", i, quat2psi(qw, qx, qy, qz), qx*qy + qw*qz, 0.5 - (qy*qy + qz*qz), dt);
pmic 0:937360eb9f8c 85 // pc.printf("%i; %i; %.6f;\r\n", i, target_planer.returnPathLength(), dt);
pmic 1:e79de7ffd211 86 // pc.printf("%i; %.12f; %.12f; %i; %i; %.6f;\r\n", i, target_planer.returnPathxAtIndex(i), target_planer.returnPathyAtIndex(i), target_planer.returnPathLength(), target_planer.returnClosestPointOnPath(RobotPosx, RobotPosy), dt);
pmic 1:e79de7ffd211 87 // pc.printf("%i; %.12f; %.12f;;\r\n", i, TargetPosx, TargetPosy);
pmic 1:e79de7ffd211 88 // target_planer.update(RobotPosx, RobotPosx);
pmic 1:e79de7ffd211 89 // target_planer.readTargetPos(&TargetPosx, &TargetPosy);
pmic 3:e6d345973797 90 // pc.printf("%i; %.12f; %.12f; %.12f; %i; %i; %.6f;\r\n", i, TargetPosx, TargetPosy, TargetAng, target_planer.return_i_min_(), target_planer.returnClosestPointOnPath(RobotPosx, RobotPosy), dt);
pmic 3:e6d345973797 91 // target_planer.updateAndReturnTarget(RobotPosx, RobotPosy, &TargetPosx, &TargetPosy, &TargetAng);
pmic 3:e6d345973797 92
pmic 3:e6d345973797 93 pc.printf("%i; %.6f; %.6f; %.6f; %.6f; %.6f; %.6f;\r\n", i, motion_planer.getTimeToPosition(targetPos), motion_planer.getDistanceToStop(), motion_planer.getPosition(), motion_planer.getVelocity(), motion_planer.getAcceleration(), dt);
pmic 3:e6d345973797 94 motion_planer.incrementToPosition(targetPos, Ts);
pmic 0:937360eb9f8c 95 }
pmic 0:937360eb9f8c 96
pmic 0:937360eb9f8c 97 i++;
pmic 0:937360eb9f8c 98 wait_us(33333);
pmic 0:937360eb9f8c 99 }
pmic 0:937360eb9f8c 100 }
pmic 0:937360eb9f8c 101
pmic 0:937360eb9f8c 102 /*
pmic 0:937360eb9f8c 103 function psi = quat2psi(q)
pmic 0:937360eb9f8c 104 % q = [qw qx qy qz] (= [q0, q1, q2, q3] = [qr, qi, qj, qk])
pmic 0:937360eb9f8c 105 for i = 1:length(q) % restore norm
pmic 0:937360eb9f8c 106 q(i,:) = q(i,:) / sqrt(q(i,1)^2 + q(i,2)^2 + q(i,3)^2 + q(i,4)^2);
pmic 0:937360eb9f8c 107 end
pmic 0:937360eb9f8c 108 psi = atan2( (q(:,2).*q(:,3) + q(:,1).*q(:,4) ), 1/2 - (q(:,3).^2 + q(:,4).^2) );
pmic 0:937360eb9f8c 109 end
pmic 0:937360eb9f8c 110 */
pmic 0:937360eb9f8c 111 double quat2psi(double qw, double qx, double qy, double qz)
pmic 0:937360eb9f8c 112 {
pmic 0:937360eb9f8c 113 double s = 1/(qw*qw + qx*qx + qy*qy + qz*qz);
pmic 0:937360eb9f8c 114 return atan2(s*(qx*qy + qw*qz), 0.5 - s*(qy*qy + qz*qz));
pmic 0:937360eb9f8c 115 }
pmic 0:937360eb9f8c 116
pmic 0:937360eb9f8c 117 double getShortRotation(double ang)
pmic 0:937360eb9f8c 118 {
pmic 0:937360eb9f8c 119 // we maybe need to introduce a threshold in case the angle is noisy and around +/-pi (180° rotation)
pmic 0:937360eb9f8c 120 return atan2(sin(ang), cos(ang));
pmic 0:937360eb9f8c 121 }
pmic 0:937360eb9f8c 122