![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Fertig
Dependencies: mbed
Fork of RT2_P3_students by
main.cpp@9:dc0eb7dd0d92, 2018-04-17 (annotated)
- Committer:
- Kiwicjam
- Date:
- Tue Apr 17 13:30:52 2018 +0000
- Revision:
- 9:dc0eb7dd0d92
- Parent:
- 8:8ed679044a72
- Child:
- 10:ddbc7e4c41a0
start p4 pi lauft
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:d79c437626e7 | 10 | #include "PI_Cntrl.h" |
altb | 8: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 |
Kiwicjam | 9:dc0eb7dd0d92 | 48 | float Ts = 0.002f; // sample time of main loops |
altb | 3:769ce5f06d3e | 49 | int k = 0; |
altb | 6:2cc56521aa16 | 50 | |
altb | 0:78ca29b4c49e | 51 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 52 | // ... here define variables like gains etc. |
altb | 0:78ca29b4c49e | 53 | //------------------------------------------ |
Kiwicjam | 9:dc0eb7dd0d92 | 54 | //LinearCharacteristics i2u(0.8f,-2.0f); |
Kiwicjam | 9:dc0eb7dd0d92 | 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 | 8:8ed679044a72 | 64 | PI_Cntrl vel_cntrl(0.5f,.05f,Ts,0.4f); |
Kiwicjam | 9:dc0eb7dd0d92 | 65 | //GPA gpa1(1.0f, 200.0f, 150, 4, 400, Ts, 10.0f, 0.3f); |
Kiwicjam | 9:dc0eb7dd0d92 | 66 | //float excWobble = 0.0f; |
altb | 8:8ed679044a72 | 67 | // GPA(t fMin, t fMax, NfexcDes, NperMin, NmeasMin, Ts, Aexc0, Aexc1) |
altb | 0:78ca29b4c49e | 68 | // ... define some linear characteristics ----------------------------------------- |
altb | 0:78ca29b4c49e | 69 | |
altb | 0:78ca29b4c49e | 70 | // ----- User defined functions ----------- |
altb | 0:78ca29b4c49e | 71 | void updateControllers(void); // speed controller loop (via interrupt) |
altb | 0:78ca29b4c49e | 72 | // ------ END User defined functions ------ |
altb | 0:78ca29b4c49e | 73 | |
altb | 0:78ca29b4c49e | 74 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 75 | //---------- main loop ------------- |
altb | 0:78ca29b4c49e | 76 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 77 | int main() |
altb | 0:78ca29b4c49e | 78 | { |
altb | 0:78ca29b4c49e | 79 | //attach controller loop to timer interrupt |
altb | 0:78ca29b4c49e | 80 | pc.baud(2000000); // for serial comm. |
altb | 0:78ca29b4c49e | 81 | counter1.reset(); // encoder reset |
altb | 0:78ca29b4c49e | 82 | diff.reset(0.0f,0.0f); |
altb | 0:78ca29b4c49e | 83 | ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = ...; |
altb | 5:d79c437626e7 | 84 | |
altb | 3:769ce5f06d3e | 85 | |
altb | 0:78ca29b4c49e | 86 | } |
altb | 0:78ca29b4c49e | 87 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 88 | //---------- control loop (called via interrupt) ------------- |
altb | 0:78ca29b4c49e | 89 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 90 | void updateControllers(void){ |
altb | 0:78ca29b4c49e | 91 | short counts = counter1; // get counts from Encoder |
altb | 0:78ca29b4c49e | 92 | float vel = diff(counts); // motor velocity |
Kiwicjam | 9:dc0eb7dd0d92 | 93 | //desTorque = vel_cntrl(w_soll - vel); |
Kiwicjam | 9:dc0eb7dd0d92 | 94 | /* outWobble = omega; |
altb | 8:8ed679044a72 | 95 | excWobble = Wobble(excWobble, outWobble); */ |
altb | 8:8ed679044a72 | 96 | |
Kiwicjam | 9:dc0eb7dd0d92 | 97 | float torq_des = vel_cntrl(w_soll - vel); |
altb | 8:8ed679044a72 | 98 | out.write(i2u(torq_des/0.217f)); // the controller! convert torque to Amps km = 0.217 Nm/A |
altb | 8:8ed679044a72 | 99 | |
altb | 8:8ed679044a72 | 100 | |
Kiwicjam | 9:dc0eb7dd0d92 | 101 | if(++k >= 249){ |
Kiwicjam | 9:dc0eb7dd0d92 | 102 | k = 0; |
Kiwicjam | 9:dc0eb7dd0d92 | 103 | pc.printf("Des. velocity: %3.3f, Velocity: %3.3f\r\n",w_soll,vel); |
Kiwicjam | 9:dc0eb7dd0d92 | 104 | } |
altb | 0:78ca29b4c49e | 105 | } |
altb | 0:78ca29b4c49e | 106 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 107 | //********** User functions like buttens handle etc. ************** |
altb | 0:78ca29b4c49e | 108 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 109 | // pressed button |
altb | 0:78ca29b4c49e | 110 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 111 | |
altb | 0:78ca29b4c49e | 112 | //... |