PID Motor Speed & Position Control Over WiFi using ESP8266 WiFi module, US Digital E4P-100-079 Quadrature Encoder, HN-GH12-1634T 30:1 200 RPM DC Motor, and LMD18200 H-Bridge Breakout
Dependencies: 4DGL-uLCD-SE PID QEI SDFileSystem mbed
Diff: main.cpp
- Revision:
- 2:07a3107e7664
- Parent:
- 1:d69f57dcde02
- Child:
- 3:7fb9c4c7e24b
--- a/main.cpp Thu Nov 26 17:31:59 2015 +0000
+++ b/main.cpp Fri Nov 27 15:30:50 2015 +0000
@@ -53,8 +53,12 @@
// this is encoder specific.
enum CONTROL_MODE{POSITION = 0, SPEED = 1};
bool control_mode = POSITION;
-float kp, ki, kd; // Gain variables for working
+float kp, ki, kd; // Gain variables for working
const float Ts = 0.04; // 25Hz Sample Freq (40ms Sample Time)
+const float Ts_PID_CALLBACK = Ts/2.0; // Update Motors and sensers twice as
+ // fast as PID sample rate, ensures
+ // PID feedback is upto date every
+ // time PID calculations run
// Vars to store gains for Speed and Position when switching Control modes
const float kp_init = 2.5; // Good Kp for Position Control
@@ -169,7 +173,7 @@
// Update sensors and feedback twice as fast as PID sample time
// this makes pid react in real-time avoiding errors due to
// missing counts etc.
- motor.attach(&pid_callback, Ts/2.0);
+ motor.attach(&pid_callback, Ts_PID_CALLBACK);
// Start PID sampling
pid.start();