Refactoring Ironcup 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
starling
Date:
Mon Sep 21 21:42:07 2020 +0000
Revision:
0:8f5db5085df7
12 mar 2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
starling 0:8f5db5085df7 1 #ifndef __PIDCONTROLLER__
starling 0:8f5db5085df7 2 #define __PIDCONTROLLER__
starling 0:8f5db5085df7 3
starling 0:8f5db5085df7 4 #include "mbed.h"
starling 0:8f5db5085df7 5 #include "Motor.hpp"
starling 0:8f5db5085df7 6 #include "FXAS21002.h"
starling 0:8f5db5085df7 7 #include "FXOS8700Q.h"
starling 0:8f5db5085df7 8 #include "rtos.h"
starling 0:8f5db5085df7 9
starling 0:8f5db5085df7 10 #define PI 3.141593
starling 0:8f5db5085df7 11
starling 0:8f5db5085df7 12 #define INITIAL_P 0.452531214933414
starling 0:8f5db5085df7 13 #define INITIAL_I 5.45748932024049
starling 0:8f5db5085df7 14 #define INITIAL_D 0.000233453623255507
starling 0:8f5db5085df7 15 #define INITIAL_N 51.0605584484153
starling 0:8f5db5085df7 16
starling 0:8f5db5085df7 17 #define ERROR_THRESH PI/5
starling 0:8f5db5085df7 18 #define MINIMUM_CURVE_VELOCITY 19
starling 0:8f5db5085df7 19
starling 0:8f5db5085df7 20 class PIDController
starling 0:8f5db5085df7 21 {
starling 0:8f5db5085df7 22 private:
starling 0:8f5db5085df7 23
starling 0:8f5db5085df7 24 PwmOut* servo;
starling 0:8f5db5085df7 25 Motor* motor;
starling 0:8f5db5085df7 26 FXAS21002* gyro;
starling 0:8f5db5085df7 27
starling 0:8f5db5085df7 28 float target_angle;
starling 0:8f5db5085df7 29 float P;
starling 0:8f5db5085df7 30 float I;
starling 0:8f5db5085df7 31 float D;
starling 0:8f5db5085df7 32 float N;
starling 0:8f5db5085df7 33 float e[2];
starling 0:8f5db5085df7 34 float up[2];
starling 0:8f5db5085df7 35 float ui[2];
starling 0:8f5db5085df7 36 float ud[2];
starling 0:8f5db5085df7 37 float u;
starling 0:8f5db5085df7 38 float saved_velocity;
starling 0:8f5db5085df7 39 float gyro_reference;
starling 0:8f5db5085df7 40
starling 0:8f5db5085df7 41 Ticker pid_trigger;
starling 0:8f5db5085df7 42 float interrupt_time;
starling 0:8f5db5085df7 43 float Ts;
starling 0:8f5db5085df7 44
starling 0:8f5db5085df7 45 void PIDTrigger();
starling 0:8f5db5085df7 46
starling 0:8f5db5085df7 47 void Control();
starling 0:8f5db5085df7 48
starling 0:8f5db5085df7 49 void RunPID();
starling 0:8f5db5085df7 50
starling 0:8f5db5085df7 51 public:
starling 0:8f5db5085df7 52 PIDController(Motor* motor, PwmOut* servo, FXAS21002* gyro);
starling 0:8f5db5085df7 53
starling 0:8f5db5085df7 54 void initializeController();
starling 0:8f5db5085df7 55 // Atach PIDController.
starling 0:8f5db5085df7 56 void PIDAttach();
starling 0:8f5db5085df7 57
starling 0:8f5db5085df7 58 // Detach PIDController.
starling 0:8f5db5085df7 59 void PIDDetach();
starling 0:8f5db5085df7 60
starling 0:8f5db5085df7 61 void setInterruptTime(float interrupt_time);
starling 0:8f5db5085df7 62
starling 0:8f5db5085df7 63 void setPIDConstants(float P, float I, float D, float N);
starling 0:8f5db5085df7 64
starling 0:8f5db5085df7 65 void setTargetAngle(float target_angle);
starling 0:8f5db5085df7 66
starling 0:8f5db5085df7 67 void addTargetAngle(float add_angle);
starling 0:8f5db5085df7 68
starling 0:8f5db5085df7 69 void setGyroReference(float gyro_ref);
starling 0:8f5db5085df7 70
starling 0:8f5db5085df7 71
starling 0:8f5db5085df7 72 };
starling 0:8f5db5085df7 73
starling 0:8f5db5085df7 74 #endif //__PIDCONTROLLER__