![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Fertig
Dependencies: mbed
Fork of RT2_P3_students by
main.cpp@6:2cc56521aa16, 2018-04-10 (annotated)
- Committer:
- altb
- Date:
- Tue Apr 10 11:44:36 2018 +0000
- Revision:
- 6:2cc56521aa16
- Parent:
- 5:d79c437626e7
- Child:
- 7:72982ede2ff6
G3_1
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 | 0:78ca29b4c49e | 11 | /* Cuboid balance on one edge on Nucleo F446RE |
altb | 0:78ca29b4c49e | 12 | |
altb | 0:78ca29b4c49e | 13 | **** IMPORTANT: use ..\Labormodelle\RT-MOD054 - Würfel\Escon_Parameter_4nucleo.edc |
altb | 0:78ca29b4c49e | 14 | settings for Maxon ESCON controller (upload via ESCON Studio) **** |
altb | 0:78ca29b4c49e | 15 | hardware Connections: |
altb | 0:78ca29b4c49e | 16 | |
altb | 0:78ca29b4c49e | 17 | CN7 CN10 |
altb | 0:78ca29b4c49e | 18 | : : |
altb | 0:78ca29b4c49e | 19 | : : |
altb | 0:78ca29b4c49e | 20 | .. .. |
altb | 0:78ca29b4c49e | 21 | .. .. 15. |
altb | 0:78ca29b4c49e | 22 | .. AOUT i_des on (PA_5)o. |
altb | 0:78ca29b4c49e | 23 | .. .. |
altb | 0:78ca29b4c49e | 24 | .. .. |
altb | 0:78ca29b4c49e | 25 | .. ENC CH A o. |
altb | 0:78ca29b4c49e | 26 | o. GND .. 10. |
altb | 0:78ca29b4c49e | 27 | o. ENC CH B .. |
altb | 0:78ca29b4c49e | 28 | .. .. |
altb | 0:78ca29b4c49e | 29 | .. .. |
altb | 0:78ca29b4c49e | 30 | .o AIN acx (PA_0) .. |
altb | 0:78ca29b4c49e | 31 | .o AIN acy (PA_1) .. 5. |
altb | 0:78ca29b4c49e | 32 | .o AIN Gyro(PA_4) .o Analog GND |
altb | 0:78ca29b4c49e | 33 | .. .. |
altb | 0:78ca29b4c49e | 34 | .. .. |
altb | 0:78ca29b4c49e | 35 | .. .. 1. |
altb | 0:78ca29b4c49e | 36 | ---------------------------- |
altb | 0:78ca29b4c49e | 37 | CN7 CN10 |
altb | 0:78ca29b4c49e | 38 | */ |
altb | 0:78ca29b4c49e | 39 | Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer |
altb | 0:78ca29b4c49e | 40 | InterruptIn button(USER_BUTTON); // User Button, short presses: reduce speed, long presses: increase speed |
altb | 0:78ca29b4c49e | 41 | AnalogIn ax(PA_0); // Analog IN (acc x) on PA_0 |
altb | 0:78ca29b4c49e | 42 | AnalogIn ay(PA_1); // Analog IN (acc y) on PA_1 |
altb | 0:78ca29b4c49e | 43 | AnalogIn gz(PA_4); // Analog IN (gyr z) on PA_4 |
altb | 0:78ca29b4c49e | 44 | AnalogOut out(PA_5); // Analog OUT on PA_5 1.6 V -> 0A 3.2A -> 2A (see ESCON) |
altb | 0:78ca29b4c49e | 45 | float out_value = 1.6f; // set voltage on 1.6 V (0 A current) |
altb | 0:78ca29b4c49e | 46 | float w_soll = 10.0f; // desired velocity |
altb | 1:a30512c3ac73 | 47 | float Ts = 0.002f; // sample time of main loops |
altb | 3:769ce5f06d3e | 48 | int k = 0; |
altb | 6:2cc56521aa16 | 49 | |
altb | 0:78ca29b4c49e | 50 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 51 | // ... here define variables like gains etc. |
altb | 0:78ca29b4c49e | 52 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 53 | |
altb | 0:78ca29b4c49e | 54 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 55 | Ticker ControllerLoopTimer; // interrupt for control loop |
altb | 0:78ca29b4c49e | 56 | EncoderCounter counter1(PB_6, PB_7); // initialize counter on PB_6 and PB_7 |
altb | 0:78ca29b4c49e | 57 | DiffCounter diff(0.01,Ts); // discrete differentiate, based on encoder data |
altb | 0:78ca29b4c49e | 58 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 59 | // ... here define instantiate classes |
altb | 0:78ca29b4c49e | 60 | //------------------------------------------ |
altb | 0:78ca29b4c49e | 61 | |
altb | 0:78ca29b4c49e | 62 | // ... define some linear characteristics ----------------------------------------- |
altb | 0:78ca29b4c49e | 63 | |
altb | 0:78ca29b4c49e | 64 | // ----- User defined functions ----------- |
altb | 0:78ca29b4c49e | 65 | void updateControllers(void); // speed controller loop (via interrupt) |
altb | 0:78ca29b4c49e | 66 | // ------ END User defined functions ------ |
altb | 0:78ca29b4c49e | 67 | |
altb | 0:78ca29b4c49e | 68 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 69 | //---------- main loop ------------- |
altb | 0:78ca29b4c49e | 70 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 71 | int main() |
altb | 0:78ca29b4c49e | 72 | { |
altb | 0:78ca29b4c49e | 73 | //attach controller loop to timer interrupt |
altb | 0:78ca29b4c49e | 74 | pc.baud(2000000); // for serial comm. |
altb | 0:78ca29b4c49e | 75 | counter1.reset(); // encoder reset |
altb | 0:78ca29b4c49e | 76 | diff.reset(0.0f,0.0f); |
altb | 0:78ca29b4c49e | 77 | ControllerLoopTimer.attach(&updateControllers, Ts); //Assume Fs = ...; |
altb | 5:d79c437626e7 | 78 | |
altb | 3:769ce5f06d3e | 79 | |
altb | 0:78ca29b4c49e | 80 | } |
altb | 0:78ca29b4c49e | 81 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 82 | //---------- control loop (called via interrupt) ------------- |
altb | 0:78ca29b4c49e | 83 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 84 | void updateControllers(void){ |
altb | 0:78ca29b4c49e | 85 | short counts = counter1; // get counts from Encoder |
altb | 0:78ca29b4c49e | 86 | float vel = diff(counts); // motor velocity |
altb | 6:2cc56521aa16 | 87 | // ... your code |
altb | 5:d79c437626e7 | 88 | if(++k >= 249){ |
altb | 0:78ca29b4c49e | 89 | k = 0; |
altb | 5:d79c437626e7 | 90 | pc.printf("Pulse: %d Velocity: %3.2f\r\n",counts,vel); |
altb | 0:78ca29b4c49e | 91 | } |
altb | 0:78ca29b4c49e | 92 | } |
altb | 0:78ca29b4c49e | 93 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 94 | //********** User functions like buttens handle etc. ************** |
altb | 0:78ca29b4c49e | 95 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 96 | // pressed button |
altb | 0:78ca29b4c49e | 97 | //****************************************************************************** |
altb | 0:78ca29b4c49e | 98 | |
altb | 0:78ca29b4c49e | 99 | //... |