-data logging revision

Dependencies:   FastPWM

Committer:
ernstpre
Date:
Thu Aug 05 08:27:51 2021 +0000
Revision:
1:25a2b47ca291
Parent:
0:d2e117716219
Child:
2:92c25cb669f4
No changes, just redownloading for final check for documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altb2 0:d2e117716219 1 #include "ControllerLoop.h"
altb2 0:d2e117716219 2 using namespace std;
altb2 0:d2e117716219 3
altb2 0:d2e117716219 4 // contructor for controller loop
altb2 0:d2e117716219 5 ControllerLoop::ControllerLoop(float Ts) : thread(osPriorityHigh,4096), dout1(PB_9)
altb2 0:d2e117716219 6 {
altb2 0:d2e117716219 7 this->Ts = Ts;
altb2 0:d2e117716219 8 diff1.reset(0.0f,0);
altb2 0:d2e117716219 9 diff2.reset(0.0f,0);
altb2 0:d2e117716219 10 is_initialized = false;
altb2 0:d2e117716219 11 ti.reset();
altb2 0:d2e117716219 12 ti.start();
altb2 0:d2e117716219 13 data.laser_on = false;
altb2 0:d2e117716219 14 }
altb2 0:d2e117716219 15
altb2 0:d2e117716219 16 // decontructor for controller loop
altb2 0:d2e117716219 17 ControllerLoop::~ControllerLoop() {}
altb2 0:d2e117716219 18
altb2 0:d2e117716219 19 // ----------------------------------------------------------------------------
altb2 0:d2e117716219 20 // this is the main loop called every Ts with high priority
altb2 0:d2e117716219 21 void ControllerLoop::loop(void){
altb2 0:d2e117716219 22 float w01=2*3.1415927 * 8;
altb2 0:d2e117716219 23 float xy[2];
ernstpre 1:25a2b47ca291 24 float exc = 0;
ernstpre 1:25a2b47ca291 25 PID_Cntrl v_cntrl_1(0.0153f, 3.06,0,0,Ts,-0.8,0.8);
ernstpre 1:25a2b47ca291 26 PID_Cntrl v_cntrl_2(0.0153f, 3.06,0,0,Ts,-0.8,0.8);
altb2 0:d2e117716219 27 while(1)
altb2 0:d2e117716219 28 {
altb2 0:d2e117716219 29 ThisThread::flags_wait_any(threadFlag);
altb2 0:d2e117716219 30 // THE LOOP ------------------------------------------------------------
altb2 0:d2e117716219 31 short c1 = counter1 - index1.positionAtIndexPulse - mk.inc_offset[0]- mk.inc_additional_offset[0]; // get counts from Encoder
altb2 0:d2e117716219 32 short c2 = counter2 - index2.positionAtIndexPulse - mk.inc_offset[1]- mk.inc_additional_offset[1]; // get counts from Encoder
altb2 0:d2e117716219 33 data.sens_phi[0] = uw2pi1(2.0f*3.1415927f/4000.0f*(float)c1);
altb2 0:d2e117716219 34 data.sens_Vphi[0] = diff1(c1); // motor velocity
altb2 0:d2e117716219 35 data.sens_phi[1] = uw2pi2(2.0f*3.1415927f/4000.0f*(float)c2);
altb2 0:d2e117716219 36 data.sens_Vphi[1] = diff2(c2); // motor velocity
altb2 0:d2e117716219 37 // -------------------------------------------------------------
altb2 0:d2e117716219 38 // at very beginning: move system slowly to find the zero pulse
altb2 0:d2e117716219 39 // set "if(0)" if you like to ommit at beginning
ernstpre 1:25a2b47ca291 40 if(!is_initialized)
altb2 0:d2e117716219 41 {
altb2 0:d2e117716219 42 find_index();
altb2 0:d2e117716219 43 if(index1.positionAtIndexPulse != 0 && index2.positionAtIndexPulse != 0)
altb2 0:d2e117716219 44 is_initialized=true;
altb2 0:d2e117716219 45 }
altb2 0:d2e117716219 46 else
altb2 0:d2e117716219 47 {
ernstpre 1:25a2b47ca291 48 // float Kp = 0.005;
ernstpre 1:25a2b47ca291 49 // data.i_des[0] = 0.1f + Kp*(exc+50.0f - data.sens_Vphi[0]);
altb2 0:d2e117716219 50 // ------------------------ do the control first
altb2 0:d2e117716219 51
altb2 0:d2e117716219 52 // calculate desired currents here, you can do "anything" here,
altb2 0:d2e117716219 53 // if you like to refer to values e.g. from the gui or from the trafo,
altb2 0:d2e117716219 54 // please use data.xxx values, they are calculated 30 lines below
ernstpre 1:25a2b47ca291 55 //float e1 = 50 - data.sens_Vphi[0];
ernstpre 1:25a2b47ca291 56 //float e2 = 50 - data.sens_Vphi[1];
ernstpre 1:25a2b47ca291 57 //float v_des1 = exc;
ernstpre 1:25a2b47ca291 58 //float v_des2 = 0;
ernstpre 1:25a2b47ca291 59 float phi1_des = 0.025f*sinf(2.0f* 3.14159f*2.0f*ti.read());
ernstpre 1:25a2b47ca291 60 float phi2_des = 0.025f*cosf(2.0f* 3.14159f*2.0f*ti.read());
ernstpre 1:25a2b47ca291 61 float Kv = 123;
ernstpre 1:25a2b47ca291 62 float v_des1 = Kv*(phi1_des - data.sens_phi[0]);
ernstpre 1:25a2b47ca291 63 float v_des2 = Kv*(phi2_des - data.sens_phi[1]);
ernstpre 1:25a2b47ca291 64 data.i_des[0] = v_cntrl_1(v_des1 - data.sens_Vphi[0]);
ernstpre 1:25a2b47ca291 65 data.i_des[1] = v_cntrl_2(v_des2 - data.sens_Vphi[1]);
ernstpre 1:25a2b47ca291 66
ernstpre 1:25a2b47ca291 67 //data.i_des[1] =0.0;
altb2 0:d2e117716219 68
altb2 0:d2e117716219 69 // ------------------------ write outputs
altb2 0:d2e117716219 70 i_des1.write(i2u(data.i_des[0]));
altb2 0:d2e117716219 71 i_des2.write(i2u(data.i_des[1]));
altb2 0:d2e117716219 72 // GPA: if you want to use the GPA, uncomment and improve following line:
ernstpre 1:25a2b47ca291 73 //exc = myGPA(data.i_des[0],data.sens_Vphi[0]);
ernstpre 1:25a2b47ca291 74 exc = myGPA(v_des1, data.sens_phi[0]);
altb2 0:d2e117716219 75
altb2 0:d2e117716219 76 // now do trafos etc
altb2 0:d2e117716219 77
altb2 0:d2e117716219 78 if(mk.external_control) // get desired values from external source (GUI)
altb2 0:d2e117716219 79 {
altb2 0:d2e117716219 80 if(mk.trafo_is_on) // use desired xy values from xternal source and transform
altb2 0:d2e117716219 81 // otherwise external source delivers phi1, phi2 values directly
altb2 0:d2e117716219 82 {
altb2 0:d2e117716219 83 bool dum = mk.X2P(data.cntrl_xy_des,data.cntrl_phi_des);
altb2 0:d2e117716219 84 }
altb2 0:d2e117716219 85 }
altb2 0:d2e117716219 86 else // this is called, when desired values are calculated here internally (e.g. pathplanner)
altb2 0:d2e117716219 87 {
altb2 0:d2e117716219 88 if(mk.trafo_is_on)
altb2 0:d2e117716219 89 {
altb2 0:d2e117716219 90 data.cntrl_xy_des[0] = 30.0f*cosf(w01*glob_ti.read()); // make a circle in xy-co-ordinates
altb2 0:d2e117716219 91 data.cntrl_xy_des[1] = 30.0f*sinf(w01*glob_ti.read());
altb2 0:d2e117716219 92 bool dum = mk.X2P(data.cntrl_xy_des,data.cntrl_phi_des);
altb2 0:d2e117716219 93 }
altb2 0:d2e117716219 94 else
altb2 0:d2e117716219 95 {
altb2 0:d2e117716219 96 data.cntrl_phi_des[0] = .250f*cosf(w01*glob_ti.read()); // make some harmonic movements directly on phi1/phi2
altb2 0:d2e117716219 97 data.cntrl_phi_des[1] = .250f*sinf(w01*glob_ti.read());
altb2 0:d2e117716219 98 }
altb2 0:d2e117716219 99 }
altb2 0:d2e117716219 100 bool dum = mk.P2X(data.sens_phi,data.est_xy); // calculate actual xy-values, uncomment this if there are timing issues
altb2 0:d2e117716219 101 //current_path->get_x_v(glob_ti.read(),&phi_des,&v_des);
altb2 0:d2e117716219 102 } // else(..)
altb2 0:d2e117716219 103 laser_on = data.laser_on;
altb2 0:d2e117716219 104 i_enable = big_button;
altb2 0:d2e117716219 105 }// endof the main loop
altb2 0:d2e117716219 106 }
altb2 0:d2e117716219 107
altb2 0:d2e117716219 108 void ControllerLoop::sendSignal() {
altb2 0:d2e117716219 109 thread.flags_set(threadFlag);
altb2 0:d2e117716219 110 }
altb2 0:d2e117716219 111 void ControllerLoop::start_loop(void)
altb2 0:d2e117716219 112 {
altb2 0:d2e117716219 113 thread.start(callback(this, &ControllerLoop::loop));
altb2 0:d2e117716219 114 ticker.attach(callback(this, &ControllerLoop::sendSignal), Ts);
altb2 0:d2e117716219 115 }
altb2 0:d2e117716219 116
altb2 0:d2e117716219 117 float ControllerLoop::pos_cntrl(float d_phi)
altb2 0:d2e117716219 118 {
altb2 0:d2e117716219 119
altb2 0:d2e117716219 120 // write position controller here
altb2 0:d2e117716219 121 return 0.0;
altb2 0:d2e117716219 122 }
altb2 0:d2e117716219 123
altb2 0:d2e117716219 124 void ControllerLoop::init_controllers(void)
altb2 0:d2e117716219 125 {
altb2 0:d2e117716219 126 // set values for your velocity and position controller here!
altb2 0:d2e117716219 127
altb2 0:d2e117716219 128
altb2 0:d2e117716219 129 }
altb2 0:d2e117716219 130 // find_index: move axis slowly to detect the zero-pulse
altb2 0:d2e117716219 131 void ControllerLoop::find_index(void)
altb2 0:d2e117716219 132 {
altb2 0:d2e117716219 133 // use a simple P-controller to get system spinning, add a constant current to overcome friction
altb2 0:d2e117716219 134 float Kp = 0.005;
altb2 0:d2e117716219 135 float i1 = 0.2f + Kp*(50.0f - data.sens_Vphi[0]);
altb2 0:d2e117716219 136 float i2 = 0.2f + Kp*(50.0f - data.sens_Vphi[1]) ;
altb2 0:d2e117716219 137 i_des1.write(i2u(i1));
altb2 0:d2e117716219 138 i_des2.write(i2u(i2));
altb2 0:d2e117716219 139 }