Fertig

Dependencies:   mbed

Fork of RT2_P3_students by RT2_P3_students

Committer:
Kiwicjam
Date:
Tue Apr 17 12:03:20 2018 +0000
Revision:
4:16f47c056c7c
Parent:
3:769ce5f06d3e
letzter stand;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altb 0:78ca29b4c49e 1 #include "mbed.h"
altb 0:78ca29b4c49e 2 #include "math.h"
altb 0:78ca29b4c49e 3 //------------------------------------------
altb 0:78ca29b4c49e 4 #define PI 3.1415927f
altb 0:78ca29b4c49e 5 //------------------------------------------
altb 0:78ca29b4c49e 6 #include "EncoderCounter.h"
altb 0:78ca29b4c49e 7 #include "DiffCounter.h"
altb 0:78ca29b4c49e 8 #include "IIR_filter.h"
altb 0:78ca29b4c49e 9 #include "LinearCharacteristics.h"
Kiwicjam 4:16f47c056c7c 10 #include "PI_Cntrl.h"
Kiwicjam 4:16f47c056c7c 11 #include "GPA.h"
altb 0:78ca29b4c49e 12 /* Cuboid balance on one edge on Nucleo F446RE
altb 0:78ca29b4c49e 13
altb 0:78ca29b4c49e 14 **** IMPORTANT: use ..\Labormodelle\RT-MOD054 - Würfel\Escon_Parameter_4nucleo.edc
altb 0:78ca29b4c49e 15 settings for Maxon ESCON controller (upload via ESCON Studio) ****
altb 0:78ca29b4c49e 16 hardware Connections:
altb 0:78ca29b4c49e 17
altb 0:78ca29b4c49e 18 CN7 CN10
altb 0:78ca29b4c49e 19 : :
altb 0:78ca29b4c49e 20 : :
altb 0:78ca29b4c49e 21 .. ..
altb 0:78ca29b4c49e 22 .. .. 15.
altb 0:78ca29b4c49e 23 .. AOUT i_des on (PA_5)o.
altb 0:78ca29b4c49e 24 .. ..
altb 0:78ca29b4c49e 25 .. ..
altb 0:78ca29b4c49e 26 .. ENC CH A o.
altb 0:78ca29b4c49e 27 o. GND .. 10.
altb 0:78ca29b4c49e 28 o. ENC CH B ..
altb 0:78ca29b4c49e 29 .. ..
altb 0:78ca29b4c49e 30 .. ..
altb 0:78ca29b4c49e 31 .o AIN acx (PA_0) ..
altb 0:78ca29b4c49e 32 .o AIN acy (PA_1) .. 5.
altb 0:78ca29b4c49e 33 .o AIN Gyro(PA_4) .o Analog GND
altb 0:78ca29b4c49e 34 .. ..
altb 0:78ca29b4c49e 35 .. ..
altb 0:78ca29b4c49e 36 .. .. 1.
altb 0:78ca29b4c49e 37 ----------------------------
altb 0:78ca29b4c49e 38 CN7 CN10
altb 0:78ca29b4c49e 39 */
altb 0:78ca29b4c49e 40 Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer
altb 0:78ca29b4c49e 41 InterruptIn button(USER_BUTTON); // User Button, short presses: reduce speed, long presses: increase speed
altb 0:78ca29b4c49e 42 AnalogIn ax(PA_0); // Analog IN (acc x) on PA_0
altb 0:78ca29b4c49e 43 AnalogIn ay(PA_1); // Analog IN (acc y) on PA_1
altb 0:78ca29b4c49e 44 AnalogIn gz(PA_4); // Analog IN (gyr z) on PA_4
altb 0:78ca29b4c49e 45 AnalogOut out(PA_5); // Analog OUT on PA_5 1.6 V -> 0A 3.2A -> 2A (see ESCON)
altb 0:78ca29b4c49e 46 float out_value = 1.6f; // set voltage on 1.6 V (0 A current)
altb 0:78ca29b4c49e 47 float w_soll = 10.0f; // desired velocity
altb 1:a30512c3ac73 48 float Ts = 0.002f; // sample time of main loops
altb 3:769ce5f06d3e 49 int k = 0;
altb 0:78ca29b4c49e 50 //------------------------------------------
altb 0:78ca29b4c49e 51 // ... here define variables like gains etc.
altb 0:78ca29b4c49e 52 //------------------------------------------
Kiwicjam 4:16f47c056c7c 53 LinearCharacteristics i2u(0.8f,-2.0f);
Kiwicjam 4:16f47c056c7c 54 PI_Cntrl controller(0.12f,0.0f);
Kiwicjam 4:16f47c056c7c 55 GPA gpa1(1.0f, 200.0f, 50, 3, 400, Ts, 5.0f, 0.3f);
altb 0:78ca29b4c49e 56
altb 0:78ca29b4c49e 57 //------------------------------------------
altb 0:78ca29b4c49e 58 Ticker ControllerLoopTimer; // interrupt for control loop
altb 0:78ca29b4c49e 59 EncoderCounter counter1(PB_6, PB_7); // initialize counter on PB_6 and PB_7
altb 0:78ca29b4c49e 60 DiffCounter diff(0.01,Ts); // discrete differentiate, based on encoder data
altb 0:78ca29b4c49e 61 //------------------------------------------
altb 0:78ca29b4c49e 62 // ... here define instantiate classes
altb 0:78ca29b4c49e 63 //------------------------------------------
altb 0:78ca29b4c49e 64
altb 0:78ca29b4c49e 65 // ... define some linear characteristics -----------------------------------------
altb 0:78ca29b4c49e 66
altb 0:78ca29b4c49e 67 // ----- User defined functions -----------
altb 0:78ca29b4c49e 68 void updateControllers(void); // speed controller loop (via interrupt)
altb 0:78ca29b4c49e 69 // ------ END User defined functions ------
altb 0:78ca29b4c49e 70
altb 0:78ca29b4c49e 71 //******************************************************************************
altb 0:78ca29b4c49e 72 //---------- main loop -------------
altb 0:78ca29b4c49e 73 //******************************************************************************
altb 0:78ca29b4c49e 74 int main()
altb 0:78ca29b4c49e 75 {
altb 0:78ca29b4c49e 76 //attach controller loop to timer interrupt
altb 0:78ca29b4c49e 77 pc.baud(2000000); // for serial comm.
altb 0:78ca29b4c49e 78 counter1.reset(); // encoder reset
altb 0:78ca29b4c49e 79 diff.reset(0.0f,0.0f);
altb 0:78ca29b4c49e 80 ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = ...;
altb 3:769ce5f06d3e 81
altb 3:769ce5f06d3e 82 float value = i2u(22.2);
altb 3:769ce5f06d3e 83
altb 3:769ce5f06d3e 84
altb 3:769ce5f06d3e 85
altb 3:769ce5f06d3e 86
altb 0:78ca29b4c49e 87 }
altb 0:78ca29b4c49e 88 //******************************************************************************
altb 0:78ca29b4c49e 89 //---------- control loop (called via interrupt) -------------
altb 0:78ca29b4c49e 90 //******************************************************************************
altb 0:78ca29b4c49e 91 void updateControllers(void){
altb 0:78ca29b4c49e 92 short counts = counter1; // get counts from Encoder
altb 0:78ca29b4c49e 93 float vel = diff(counts); // motor velocity
Kiwicjam 4:16f47c056c7c 94 float esc = gpa1.update(0.0, 0.0);
Kiwicjam 4:16f47c056c7c 95
Kiwicjam 4:16f47c056c7c 96 out.write(i2u(controller.doStep(w_soll - vel))/3.3f);
altb 0:78ca29b4c49e 97 if(++k >= 199){
altb 0:78ca29b4c49e 98 k = 0;
altb 0:78ca29b4c49e 99 pc.printf("omega_soll=%3.2f omega=%3.2f \r\n",w_soll,vel);
altb 0:78ca29b4c49e 100 }
altb 0:78ca29b4c49e 101 }
altb 0:78ca29b4c49e 102 //******************************************************************************
altb 0:78ca29b4c49e 103 //********** User functions like buttens handle etc. **************
altb 0:78ca29b4c49e 104 //******************************************************************************
altb 0:78ca29b4c49e 105 // pressed button
altb 0:78ca29b4c49e 106 //******************************************************************************
altb 0:78ca29b4c49e 107
altb 0:78ca29b4c49e 108 //...