PI Step test csv
Dependencies: Motor PID QEI mbed
Fork of PIDRover by
main.cpp@1:811bb0e318a8, 2015-11-26 (annotated)
- Committer:
- Tokalic
- Date:
- Thu Nov 26 07:21:21 2015 +0000
- Revision:
- 1:811bb0e318a8
- Parent:
- 0:be99ed42340d
Step test to csv
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tokalic | 1:811bb0e318a8 | 1 | #include "PID.h" |
aberk | 0:be99ed42340d | 2 | #include "QEI.h" |
Tokalic | 1:811bb0e318a8 | 3 | #define RATE 0.01 |
Tokalic | 1:811bb0e318a8 | 4 | #define Kc 0.473 |
Tokalic | 1:811bb0e318a8 | 5 | #define Ti 0.025 |
Tokalic | 1:811bb0e318a8 | 6 | #define Td 0.0 |
aberk | 0:be99ed42340d | 7 | |
Tokalic | 1:811bb0e318a8 | 8 | PwmOut Motor(p21); |
Tokalic | 1:811bb0e318a8 | 9 | QEI Qei(p29, p30, NC, 504); |
Tokalic | 1:811bb0e318a8 | 10 | PID Controller(Kc, Ti, Td, RATE); |
Tokalic | 1:811bb0e318a8 | 11 | //------- |
Tokalic | 1:811bb0e318a8 | 12 | // Datoteke |
Tokalic | 1:811bb0e318a8 | 13 | //------- |
Tokalic | 1:811bb0e318a8 | 14 | LocalFileSystem local("local"); |
Tokalic | 1:811bb0e318a8 | 15 | FILE* fp; |
aberk | 0:be99ed42340d | 16 | |
Tokalic | 1:811bb0e318a8 | 17 | Timer endTimer; |
Tokalic | 1:811bb0e318a8 | 18 | |
Tokalic | 1:811bb0e318a8 | 19 | //Radne varijable |
aberk | 0:be99ed42340d | 20 | |
Tokalic | 1:811bb0e318a8 | 21 | volatile int Pulses = 0; |
Tokalic | 1:811bb0e318a8 | 22 | volatile int PrevPulses = 0; |
Tokalic | 1:811bb0e318a8 | 23 | volatile float PwmDuty = 0.0; |
Tokalic | 1:811bb0e318a8 | 24 | volatile float Velocity = 0.0; |
Tokalic | 1:811bb0e318a8 | 25 | //Zadana brzina |
Tokalic | 1:811bb0e318a8 | 26 | int goal = 21000; |
Tokalic | 1:811bb0e318a8 | 27 | |
aberk | 0:be99ed42340d | 28 | |
Tokalic | 1:811bb0e318a8 | 29 | void initializeMotors(void); |
Tokalic | 1:811bb0e318a8 | 30 | void initializePidControllers(void); |
Tokalic | 1:811bb0e318a8 | 31 | void initializeMotors(void){ |
Tokalic | 1:811bb0e318a8 | 32 | |
Tokalic | 1:811bb0e318a8 | 33 | Motor.period_us(50); |
Tokalic | 1:811bb0e318a8 | 34 | Motor = 1.0; |
Tokalic | 1:811bb0e318a8 | 35 | } |
Tokalic | 1:811bb0e318a8 | 36 | |
Tokalic | 1:811bb0e318a8 | 37 | void initializePidControllers(void){ |
Tokalic | 1:811bb0e318a8 | 38 | |
Tokalic | 1:811bb0e318a8 | 39 | Controller.setInputLimits(0.0, 54500); |
Tokalic | 1:811bb0e318a8 | 40 | Controller.setOutputLimits(0.0, 1.0); |
Tokalic | 1:811bb0e318a8 | 41 | Controller.setBias(1.0); |
Tokalic | 1:811bb0e318a8 | 42 | Controller.setMode(AUTO_MODE); |
Tokalic | 1:811bb0e318a8 | 43 | } |
Tokalic | 1:811bb0e318a8 | 44 | |
Tokalic | 1:811bb0e318a8 | 45 | int main() { |
Tokalic | 1:811bb0e318a8 | 46 | wait(2); |
Tokalic | 1:811bb0e318a8 | 47 | //Inicijalizacija |
Tokalic | 1:811bb0e318a8 | 48 | initializeMotors(); |
Tokalic | 1:811bb0e318a8 | 49 | initializePidControllers(); |
Tokalic | 1:811bb0e318a8 | 50 | |
Tokalic | 1:811bb0e318a8 | 51 | //Kreiranje datoteke za spremanje rezultata. |
Tokalic | 1:811bb0e318a8 | 52 | fp = fopen("/local/pidtest.csv", "w"); |
Tokalic | 1:811bb0e318a8 | 53 | |
Tokalic | 1:811bb0e318a8 | 54 | endTimer.start(); |
Tokalic | 1:811bb0e318a8 | 55 | |
Tokalic | 1:811bb0e318a8 | 56 | Controller.setSetPoint(goal); |
Tokalic | 1:811bb0e318a8 | 57 | |
Tokalic | 1:811bb0e318a8 | 58 | //Vrijeme mjerenja je 3 sekunde |
Tokalic | 1:811bb0e318a8 | 59 | while (endTimer.read() < 3.0){ |
Tokalic | 1:811bb0e318a8 | 60 | Pulses = Qei.getPulses(); |
Tokalic | 1:811bb0e318a8 | 61 | Velocity = (Pulses - PrevPulses) / RATE; |
Tokalic | 1:811bb0e318a8 | 62 | PrevPulses = Pulses; |
Tokalic | 1:811bb0e318a8 | 63 | Controller.setProcessValue(Velocity); |
Tokalic | 1:811bb0e318a8 | 64 | PwmDuty = Controller.compute(); |
Tokalic | 1:811bb0e318a8 | 65 | Motor = PwmDuty; |
Tokalic | 1:811bb0e318a8 | 66 | fprintf(fp, "%f,%f,\n", Velocity, PwmDuty); |
Tokalic | 1:811bb0e318a8 | 67 | wait(RATE); |
aberk | 0:be99ed42340d | 68 | } |
Tokalic | 1:811bb0e318a8 | 69 | //Spremanje izmjerenih podataka |
Tokalic | 1:811bb0e318a8 | 70 | fclose(fp); |
aberk | 0:be99ed42340d | 71 | } |