Tobias Vögeli
/
GRT_VC_PIDT1
pr7
main.cpp@0:05dd1de8cc3f, 2019-05-03 (annotated)
- Committer:
- altb2
- Date:
- Fri May 03 09:37:27 2019 +0000
- Revision:
- 0:05dd1de8cc3f
- Child:
- 1:92f175969d90
Final Template Version for Lab #7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb2 | 0:05dd1de8cc3f | 1 | #include "mbed.h" |
altb2 | 0:05dd1de8cc3f | 2 | #include "EncoderCounter.h" |
altb2 | 0:05dd1de8cc3f | 3 | #include "LinearCharacteristics.h" |
altb2 | 0:05dd1de8cc3f | 4 | //------------------------------------------ |
altb2 | 0:05dd1de8cc3f | 5 | #define PI 3.1415927f |
altb2 | 0:05dd1de8cc3f | 6 | //------------------------------------------ |
altb2 | 0:05dd1de8cc3f | 7 | |
altb2 | 0:05dd1de8cc3f | 8 | /* GRT: Control of voice-coil with PID-T1 - Controller |
altb2 | 0:05dd1de8cc3f | 9 | */ |
altb2 | 0:05dd1de8cc3f | 10 | Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer |
altb2 | 0:05dd1de8cc3f | 11 | InterruptIn button(USER_BUTTON); // User Button, short presses: reduce speed, long presses: increase speed |
altb2 | 0:05dd1de8cc3f | 12 | AnalogOut out(PA_5); // Analog OUT on PA_5 1.6 V -> 0A 3.2A -> 4A (see ESCON) |
altb2 | 0:05dd1de8cc3f | 13 | |
altb2 | 0:05dd1de8cc3f | 14 | bool key_was_pressed = false; |
altb2 | 0:05dd1de8cc3f | 15 | bool controller_active = false; |
altb2 | 0:05dd1de8cc3f | 16 | void pressed(void); // user Button pressed |
altb2 | 0:05dd1de8cc3f | 17 | void released(void); // user Button released |
altb2 | 0:05dd1de8cc3f | 18 | |
altb2 | 0:05dd1de8cc3f | 19 | //------------------------------------------ |
altb2 | 0:05dd1de8cc3f | 20 | // ... here define variables like gains etc. |
altb2 | 0:05dd1de8cc3f | 21 | //------------------------------------------ |
altb2 | 0:05dd1de8cc3f | 22 | LinearCharacteristics i2u(-4.0f,4.0f,0.0f,3.2f / 3.3f); // output is normalized output |
altb2 | 0:05dd1de8cc3f | 23 | //------------------------------------------ |
altb2 | 0:05dd1de8cc3f | 24 | Ticker ControllerLoopTimer; // interrupt for control loop |
altb2 | 0:05dd1de8cc3f | 25 | EncoderCounter counter1(PB_6, PB_7); // initialize counter on PB_6 and PB_7 |
altb2 | 0:05dd1de8cc3f | 26 | Timer t_but; // define timer for button |
altb2 | 0:05dd1de8cc3f | 27 | // ----- User defined functions ----------- |
altb2 | 0:05dd1de8cc3f | 28 | void updateLoop(void); // loop for State machine (via interrupt) |
altb2 | 0:05dd1de8cc3f | 29 | float Ts = 0.0005f; // sample time of main loop |
altb2 | 0:05dd1de8cc3f | 30 | uint16_t k = 0; |
altb2 | 0:05dd1de8cc3f | 31 | //****************************************************************************** |
altb2 | 0:05dd1de8cc3f | 32 | //---------- main loop ------------- |
altb2 | 0:05dd1de8cc3f | 33 | //****************************************************************************** |
altb2 | 0:05dd1de8cc3f | 34 | int main() |
altb2 | 0:05dd1de8cc3f | 35 | { |
altb2 | 0:05dd1de8cc3f | 36 | pc.baud(115200); // for serial comm. |
altb2 | 0:05dd1de8cc3f | 37 | counter1.reset(); // encoder reset |
altb2 | 0:05dd1de8cc3f | 38 | out.write(i2u(0.0)); |
altb2 | 0:05dd1de8cc3f | 39 | button.fall(&pressed); // attach key pressed function |
altb2 | 0:05dd1de8cc3f | 40 | button.rise(&released); // attach key pressed function |
altb2 | 0:05dd1de8cc3f | 41 | pc.printf("Start controller now...\r\n"); |
altb2 | 0:05dd1de8cc3f | 42 | ControllerLoopTimer.attach(&updateLoop, Ts); //Assume Fs = ...; |
altb2 | 0:05dd1de8cc3f | 43 | while(1); |
altb2 | 0:05dd1de8cc3f | 44 | } // END OF main |
altb2 | 0:05dd1de8cc3f | 45 | //****************************************************************************** |
altb2 | 0:05dd1de8cc3f | 46 | //---------- main loop (called via interrupt) ------------- |
altb2 | 0:05dd1de8cc3f | 47 | //****************************************************************************** |
altb2 | 0:05dd1de8cc3f | 48 | void updateLoop(void){ |
altb2 | 0:05dd1de8cc3f | 49 | float x = (float)(counter1)/1000.0f; // get counts from Encoder |
altb2 | 0:05dd1de8cc3f | 50 | float i_des = 0.0f; // default: set motor current to zero (will be overwritten) |
altb2 | 0:05dd1de8cc3f | 51 | if(controller_active) |
altb2 | 0:05dd1de8cc3f | 52 | { |
altb2 | 0:05dd1de8cc3f | 53 | // here calculate controller's output |
altb2 | 0:05dd1de8cc3f | 54 | } |
altb2 | 0:05dd1de8cc3f | 55 | out.write(i2u(i_des)); |
altb2 | 0:05dd1de8cc3f | 56 | if(++k>1000) |
altb2 | 0:05dd1de8cc3f | 57 | { |
altb2 | 0:05dd1de8cc3f | 58 | pc.printf("x: %1.3f, i: %1.4f\r\n",x,i_des); |
altb2 | 0:05dd1de8cc3f | 59 | k=0; |
altb2 | 0:05dd1de8cc3f | 60 | } |
altb2 | 0:05dd1de8cc3f | 61 | } // END OF updateLoop(void) |
altb2 | 0:05dd1de8cc3f | 62 | |
altb2 | 0:05dd1de8cc3f | 63 | //****************************************************************************** |
altb2 | 0:05dd1de8cc3f | 64 | |
altb2 | 0:05dd1de8cc3f | 65 | |
altb2 | 0:05dd1de8cc3f | 66 | // start timer as soon as Button is pressed |
altb2 | 0:05dd1de8cc3f | 67 | void pressed() |
altb2 | 0:05dd1de8cc3f | 68 | { |
altb2 | 0:05dd1de8cc3f | 69 | t_but.start(); |
altb2 | 0:05dd1de8cc3f | 70 | } |
altb2 | 0:05dd1de8cc3f | 71 | // Falling edge of button: enable/disable controller |
altb2 | 0:05dd1de8cc3f | 72 | void released() |
altb2 | 0:05dd1de8cc3f | 73 | { |
altb2 | 0:05dd1de8cc3f | 74 | // readout, stop and reset timer |
altb2 | 0:05dd1de8cc3f | 75 | float ButtonTime = t_but.read(); |
altb2 | 0:05dd1de8cc3f | 76 | t_but.stop(); |
altb2 | 0:05dd1de8cc3f | 77 | t_but.reset(); |
altb2 | 0:05dd1de8cc3f | 78 | if(ButtonTime > 0.05f) |
altb2 | 0:05dd1de8cc3f | 79 | { |
altb2 | 0:05dd1de8cc3f | 80 | controller_active = !controller_active; |
altb2 | 0:05dd1de8cc3f | 81 | if(controller_active) |
altb2 | 0:05dd1de8cc3f | 82 | { |
altb2 | 0:05dd1de8cc3f | 83 | pc.printf("Controller active\r\n"); |
altb2 | 0:05dd1de8cc3f | 84 | // reset controller here!!! |
altb2 | 0:05dd1de8cc3f | 85 | } |
altb2 | 0:05dd1de8cc3f | 86 | else |
altb2 | 0:05dd1de8cc3f | 87 | pc.printf("Controller disabled\r\n"); |
altb2 | 0:05dd1de8cc3f | 88 | } |
altb2 | 0:05dd1de8cc3f | 89 | } |