Updates error values

Dependents:   locomotion_pid_action_refactor_EMG

Committer:
tvlogman
Date:
Fri Oct 20 12:24:03 2017 +0000
Revision:
0:cb9eda46a58c
Child:
1:9e2c9237d88b
Should work now but not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tvlogman 0:cb9eda46a58c 1 #include "errorFetch.h"
tvlogman 0:cb9eda46a58c 2 #include "mbed.h"
tvlogman 0:cb9eda46a58c 3 #include "QEI.h"
tvlogman 0:cb9eda46a58c 4 #include "refGen.h"
tvlogman 0:cb9eda46a58c 5
tvlogman 0:cb9eda46a58c 6 // Member function definitions
tvlogman 0:cb9eda46a58c 7 errorFetch::errorFetch(QEI Encoder, float N, float TS, refGen REF):motorEncoder(Encoder), gearRatio(N), Ts(TS), e_pos(0), e_int(0), e_der(0), e_prev(0), ref(REF){
tvlogman 0:cb9eda46a58c 8
tvlogman 0:cb9eda46a58c 9 }
tvlogman 0:cb9eda46a58c 10
tvlogman 0:cb9eda46a58c 11 void errorFetch::getError(){
tvlogman 0:cb9eda46a58c 12 // Getting encoder counts and calculating motor position
tvlogman 0:cb9eda46a58c 13 int counts = motorEncoder.getPulses();
tvlogman 0:cb9eda46a58c 14 double motorPosition = 2*3.14*(counts/(gearRatio*64.0f));
tvlogman 0:cb9eda46a58c 15
tvlogman 0:cb9eda46a58c 16 // Computing position error
tvlogman 0:cb9eda46a58c 17 e_pos = ref.getReferencePosition(ref.maxAngle, ref.r_direction) - motorPosition;
tvlogman 0:cb9eda46a58c 18
tvlogman 0:cb9eda46a58c 19 // Limiting the integral error to prevent integrator saturation
tvlogman 0:cb9eda46a58c 20 if(fabs(e_int) <= 5){
tvlogman 0:cb9eda46a58c 21 e_int = e_int + Ts*e_pos;
tvlogman 0:cb9eda46a58c 22 }
tvlogman 0:cb9eda46a58c 23
tvlogman 0:cb9eda46a58c 24 // Derivative error
tvlogman 0:cb9eda46a58c 25 e_der = (e_pos - e_prev)/Ts;
tvlogman 0:cb9eda46a58c 26 e_prev = e_pos; // Store current position error as we'll need it to compute the next derivative error
tvlogman 0:cb9eda46a58c 27 }