Template for group 4

Dependencies:   mbed

Fork of RT2_P3_students by RT2_P3_students

Committer:
altb
Date:
Sun Apr 22 19:54:59 2018 +0000
Revision:
7:01a7363583b2
Parent:
6:8ed679044a72
Child:
8:72f260c467ad
P3_2

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"
altb 3:d79c437626e7 10 #include "PI_Cntrl.h"
altb 6:8ed679044a72 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 7:01a7363583b2 48 float Ts = 0.002f; // sample time of main loops
altb 2:769ce5f06d3e 49 int k = 0;
altb 4:2cc56521aa16 50
altb 0:78ca29b4c49e 51 //------------------------------------------
altb 0:78ca29b4c49e 52 // ... here define variables like gains etc.
altb 0:78ca29b4c49e 53 //------------------------------------------
altb 7:01a7363583b2 54 //LinearCharacteristics i2u(0.8f,-2.0f);
altb 7:01a7363583b2 55 LinearCharacteristics i2u(-2.0f,2.0f,0.0f,3.2f);
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 6:8ed679044a72 64 PI_Cntrl vel_cntrl(0.5f,.05f,Ts,0.4f);
altb 7:01a7363583b2 65 //GPA gpa1(1.0f, 200.0f, 150, 4, 400, Ts, 10.0f, 0.3f);
altb 6:8ed679044a72 66 // GPA(t fMin, t fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1)
altb 0:78ca29b4c49e 67 // ... define some linear characteristics -----------------------------------------
altb 0:78ca29b4c49e 68
altb 0:78ca29b4c49e 69 // ----- User defined functions -----------
altb 0:78ca29b4c49e 70 void updateControllers(void); // speed controller loop (via interrupt)
altb 0:78ca29b4c49e 71 // ------ END User defined functions ------
altb 0:78ca29b4c49e 72
altb 0:78ca29b4c49e 73 //******************************************************************************
altb 0:78ca29b4c49e 74 //---------- main loop -------------
altb 0:78ca29b4c49e 75 //******************************************************************************
altb 0:78ca29b4c49e 76 int main()
altb 0:78ca29b4c49e 77 {
altb 0:78ca29b4c49e 78 //attach controller loop to timer interrupt
altb 0:78ca29b4c49e 79 pc.baud(2000000); // for serial comm.
altb 0:78ca29b4c49e 80 counter1.reset(); // encoder reset
altb 0:78ca29b4c49e 81 diff.reset(0.0f,0.0f);
altb 0:78ca29b4c49e 82 ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = ...;
altb 3:d79c437626e7 83
altb 2:769ce5f06d3e 84
altb 0:78ca29b4c49e 85 }
altb 0:78ca29b4c49e 86 //******************************************************************************
altb 0:78ca29b4c49e 87 //---------- control loop (called via interrupt) -------------
altb 0:78ca29b4c49e 88 //******************************************************************************
altb 0:78ca29b4c49e 89 void updateControllers(void){
altb 0:78ca29b4c49e 90 short counts = counter1; // get counts from Encoder
altb 0:78ca29b4c49e 91 float vel = diff(counts); // motor velocity
altb 7:01a7363583b2 92
altb 6:8ed679044a72 93
altb 7:01a7363583b2 94 /*float torq_des = vel_cntrl(excWobble + w_soll - vel);
altb 6:8ed679044a72 95 excWobble = gpa1(torq_des,vel);
altb 6:8ed679044a72 96 out.write(i2u(torq_des/0.217f)); // the controller! convert torque to Amps km = 0.217 Nm/A
altb 7:01a7363583b2 97 */
altb 6:8ed679044a72 98
altb 7:01a7363583b2 99 if(++k >= 249){
altb 7:01a7363583b2 100 k = 0;
altb 7:01a7363583b2 101 pc.printf("Some Output %3.3f\r\n",1.11111);
altb 7:01a7363583b2 102 }
altb 0:78ca29b4c49e 103 }
altb 0:78ca29b4c49e 104 //******************************************************************************
altb 0:78ca29b4c49e 105 //********** User functions like buttens handle etc. **************
altb 0:78ca29b4c49e 106 //******************************************************************************
altb 0:78ca29b4c49e 107 // pressed button
altb 0:78ca29b4c49e 108 //******************************************************************************
altb 0:78ca29b4c49e 109
altb 0:78ca29b4c49e 110 //...