-data logging revision

Dependencies:   FastPWM

Committer:
altb2
Date:
Sun May 02 19:32:30 2021 +0000
Revision:
0:d2e117716219
Child:
1:25a2b47ca291
1st commit;

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];
altb2 0:d2e117716219 24 while(1)
altb2 0:d2e117716219 25 {
altb2 0:d2e117716219 26 ThisThread::flags_wait_any(threadFlag);
altb2 0:d2e117716219 27 // THE LOOP ------------------------------------------------------------
altb2 0:d2e117716219 28 short c1 = counter1 - index1.positionAtIndexPulse - mk.inc_offset[0]- mk.inc_additional_offset[0]; // get counts from Encoder
altb2 0:d2e117716219 29 short c2 = counter2 - index2.positionAtIndexPulse - mk.inc_offset[1]- mk.inc_additional_offset[1]; // get counts from Encoder
altb2 0:d2e117716219 30 data.sens_phi[0] = uw2pi1(2.0f*3.1415927f/4000.0f*(float)c1);
altb2 0:d2e117716219 31 data.sens_Vphi[0] = diff1(c1); // motor velocity
altb2 0:d2e117716219 32 data.sens_phi[1] = uw2pi2(2.0f*3.1415927f/4000.0f*(float)c2);
altb2 0:d2e117716219 33 data.sens_Vphi[1] = diff2(c2); // motor velocity
altb2 0:d2e117716219 34 // -------------------------------------------------------------
altb2 0:d2e117716219 35 // at very beginning: move system slowly to find the zero pulse
altb2 0:d2e117716219 36 // set "if(0)" if you like to ommit at beginning
altb2 0:d2e117716219 37 if(0)//!is_initialized)
altb2 0:d2e117716219 38 {
altb2 0:d2e117716219 39 find_index();
altb2 0:d2e117716219 40 if(index1.positionAtIndexPulse != 0 && index2.positionAtIndexPulse != 0)
altb2 0:d2e117716219 41 is_initialized=true;
altb2 0:d2e117716219 42 }
altb2 0:d2e117716219 43 else
altb2 0:d2e117716219 44 {
altb2 0:d2e117716219 45 // ------------------------ do the control first
altb2 0:d2e117716219 46
altb2 0:d2e117716219 47 // calculate desired currents here, you can do "anything" here,
altb2 0:d2e117716219 48 // if you like to refer to values e.g. from the gui or from the trafo,
altb2 0:d2e117716219 49 // please use data.xxx values, they are calculated 30 lines below
altb2 0:d2e117716219 50
altb2 0:d2e117716219 51 data.i_des[0] = data.i_des[1] =0.0;
altb2 0:d2e117716219 52
altb2 0:d2e117716219 53 // ------------------------ write outputs
altb2 0:d2e117716219 54 i_des1.write(i2u(data.i_des[0]));
altb2 0:d2e117716219 55 i_des2.write(i2u(data.i_des[1]));
altb2 0:d2e117716219 56 // GPA: if you want to use the GPA, uncomment and improve following line:
altb2 0:d2e117716219 57 // exc = myGPA(data.i_des[0],data.sens_Vphi[0]);
altb2 0:d2e117716219 58
altb2 0:d2e117716219 59 // now do trafos etc
altb2 0:d2e117716219 60
altb2 0:d2e117716219 61 if(mk.external_control) // get desired values from external source (GUI)
altb2 0:d2e117716219 62 {
altb2 0:d2e117716219 63 if(mk.trafo_is_on) // use desired xy values from xternal source and transform
altb2 0:d2e117716219 64 // otherwise external source delivers phi1, phi2 values directly
altb2 0:d2e117716219 65 {
altb2 0:d2e117716219 66 bool dum = mk.X2P(data.cntrl_xy_des,data.cntrl_phi_des);
altb2 0:d2e117716219 67 }
altb2 0:d2e117716219 68 }
altb2 0:d2e117716219 69 else // this is called, when desired values are calculated here internally (e.g. pathplanner)
altb2 0:d2e117716219 70 {
altb2 0:d2e117716219 71 if(mk.trafo_is_on)
altb2 0:d2e117716219 72 {
altb2 0:d2e117716219 73 data.cntrl_xy_des[0] = 30.0f*cosf(w01*glob_ti.read()); // make a circle in xy-co-ordinates
altb2 0:d2e117716219 74 data.cntrl_xy_des[1] = 30.0f*sinf(w01*glob_ti.read());
altb2 0:d2e117716219 75 bool dum = mk.X2P(data.cntrl_xy_des,data.cntrl_phi_des);
altb2 0:d2e117716219 76 }
altb2 0:d2e117716219 77 else
altb2 0:d2e117716219 78 {
altb2 0:d2e117716219 79 data.cntrl_phi_des[0] = .250f*cosf(w01*glob_ti.read()); // make some harmonic movements directly on phi1/phi2
altb2 0:d2e117716219 80 data.cntrl_phi_des[1] = .250f*sinf(w01*glob_ti.read());
altb2 0:d2e117716219 81 }
altb2 0:d2e117716219 82 }
altb2 0:d2e117716219 83 bool dum = mk.P2X(data.sens_phi,data.est_xy); // calculate actual xy-values, uncomment this if there are timing issues
altb2 0:d2e117716219 84 //current_path->get_x_v(glob_ti.read(),&phi_des,&v_des);
altb2 0:d2e117716219 85 } // else(..)
altb2 0:d2e117716219 86 laser_on = data.laser_on;
altb2 0:d2e117716219 87 i_enable = big_button;
altb2 0:d2e117716219 88 }// endof the main loop
altb2 0:d2e117716219 89 }
altb2 0:d2e117716219 90
altb2 0:d2e117716219 91 void ControllerLoop::sendSignal() {
altb2 0:d2e117716219 92 thread.flags_set(threadFlag);
altb2 0:d2e117716219 93 }
altb2 0:d2e117716219 94 void ControllerLoop::start_loop(void)
altb2 0:d2e117716219 95 {
altb2 0:d2e117716219 96 thread.start(callback(this, &ControllerLoop::loop));
altb2 0:d2e117716219 97 ticker.attach(callback(this, &ControllerLoop::sendSignal), Ts);
altb2 0:d2e117716219 98 }
altb2 0:d2e117716219 99
altb2 0:d2e117716219 100 float ControllerLoop::pos_cntrl(float d_phi)
altb2 0:d2e117716219 101 {
altb2 0:d2e117716219 102
altb2 0:d2e117716219 103 // write position controller here
altb2 0:d2e117716219 104 return 0.0;
altb2 0:d2e117716219 105 }
altb2 0:d2e117716219 106
altb2 0:d2e117716219 107 void ControllerLoop::init_controllers(void)
altb2 0:d2e117716219 108 {
altb2 0:d2e117716219 109 // set values for your velocity and position controller here!
altb2 0:d2e117716219 110
altb2 0:d2e117716219 111
altb2 0:d2e117716219 112 }
altb2 0:d2e117716219 113 // find_index: move axis slowly to detect the zero-pulse
altb2 0:d2e117716219 114 void ControllerLoop::find_index(void)
altb2 0:d2e117716219 115 {
altb2 0:d2e117716219 116 // use a simple P-controller to get system spinning, add a constant current to overcome friction
altb2 0:d2e117716219 117 float Kp = 0.005;
altb2 0:d2e117716219 118 float i1 = 0.2f + Kp*(50.0f - data.sens_Vphi[0]);
altb2 0:d2e117716219 119 float i2 = 0.2f + Kp*(50.0f - data.sens_Vphi[1]) ;
altb2 0:d2e117716219 120 i_des1.write(i2u(i1));
altb2 0:d2e117716219 121 i_des2.write(i2u(i2));
altb2 0:d2e117716219 122 }