29/10/2019

Dependencies:   Encoder_Nucleo_16_bits

Dependents:   Kenya_2019

Embed: (wiki syntax)

« Back to documentation index

PID Class Reference

PID Class Reference

Includes : Mbed Library + Nucleo_Encoder_16_bits library. More...

#include <PID.h>

Public Member Functions

 PID (TIM_TypeDef *LTIM, TIM_TypeDef *RTIM, PinName LPWM, PinName RPWM, PinName LDIRA, PinName LDIRB, PinName RDIRA, PinName RDIRB)
 Constructor (standard) with full bridge control.
float setProportionnalValue (float KpValue)
 Set the Kp value.
float setintegralValue (float KiValue)
 Set the Ki value.
float setDerivativeValue (float KdValue)
 Set the Kd value.
void setSpeed (double left, double right)
 Set the Set point value of the speed for integrated full bridge.
void getPosition (double *x, double *y, double *theta)
 Get position of the robot (in mm for X and Y and radian for Theta)
void resetPosition (void)
 Reset position of the robot (in mm for X and Y and radian for Theta)
void getSpeed (double *vG, double *vD)
 Get speed of the two wheels of the robot.

Data Fields

double _Kp
 Global Variable of corrective proportionnal value.
double _Ki
 Global Variable of corrective integral value.
double _Kd
 Global Variable of corrective derivative value.
double _SpeedG
 Global Variable of Left speed.
double _SpeedD
 Global Variable of Right speed.
double _measSpeedG
 Global Variable of measured left speed.
double _measSpeedD
 Global Variable of measured right speed.
double _measDistG
 Global Variable of measured Left wheel displacement.
double _measDistD
 Global Variable of measured Right wheel displacement.
int _WheelStuckG
 Global Variable to indicate that required Left wheel speed is unreachable (set if speed is unreachable)
int _WheelStuckD
 Global Variable to indicate that required Right wheel speed is unreachable (set if speed is unreachable)
double _PwmValueG
 Global Variable of Left wheel PWM value.
double _PwmValueD
 Global Variable of Right wheel PWM value.
double _X
 Global Variable of odometric measurement of X position of gravity center of robot.
double _Y
 Global Variable of odometric measurement of Y position of gravity center of robot.
double _THETA
 Global Variable of odometric measurement of THETA angle of gravity center of robot.
int RobotIsStuck
 Global Variable to indicate that required speed is unreachable (=1 if speed is unreachable)

Detailed Description

Includes : Mbed Library + Nucleo_Encoder_16_bits library.

PID Motion control system (speed control). refere to wikipedia for more explainations : https://en.wikipedia.org/wiki/PID_controller

Note:
This library require a tuning (mostly base on try/modify) and will not give a correct performance until you have tuned all 3 parameters Kp, Ki and Kd for each wheels of the robot
You must use a QE (quadrature Encoder) connected to a 16 bits timer to get proper motion control. Pins A & B of the QE must be connected respectively to pin 1 and 2 of the timer. Typicaly on Nucleo F446RE TIM3 and TIM4 are perfect to do this job. In this case simply use TIM3 or TIM4 as timer parameter.
You must also specify the number of pulses generated by the QE for a 1mm displacement of the wheel. This is the scale parameter
Outputed PWM value evolve from 1 (full PWM fortward) to -1 (full PWM backward). This value can also be found in the global variable : _PwmValue. The PWM value is based on a 1.3 m/s maximum speed.
As this motion control system is implemented in a microcontroler it is important to understand that there is a loop time for the motion control system and that this loop time MUST be constant. Kp, Ki and Kd are dependent of the loop time. Changing loop time means changing all the corrector's coefficients. Library use a Ticker to control loop time. The looptime can be set by software as long as it remain constant during the whole use of PID controler. Loop time is set to 1ms by default.
The PID is initialized with Ki = 0, Kd = 0 and Kp = 1
  • Increasing Kp will shorten your response time but will also create instability (at beggining overshoots, then instability).
  • Adding a bit of Ki will allow your system to bring to 0 the static error (ie : will make the error, between the set point and your mesurment, tend to 0) but might create instability and increase setting time.
  • Adding a bit of Kd will stabilize the response (with almost no bad effect, as long as Kd remains small).
More info can be found here : https://en.wikipedia.org/wiki/PID_controller
The odometric position is computed by the trapezoidal integration method (approximation of a part of a circle by a trapezoid) after each program loop. Hence, odometric error increase with time.
The odometric X coordinate is the direction in front of the robot at startup. Y coordinate is the orthogonal vector in trigonometric direction (counterclockwise) refered to X, and THETA is the angle of the robot refered to X at startup

Definition at line 67 of file PID.h.


Constructor & Destructor Documentation

PID ( TIM_TypeDef *  LTIM,
TIM_TypeDef *  RTIM,
PinName  LPWM,
PinName  RPWM,
PinName  LDIRA,
PinName  LDIRB,
PinName  RDIRA,
PinName  RDIRB 
)

Constructor (standard) with full bridge control.

Parameters:
LTIM(TIM_TypeDef*) : the Mbed 16 bits timer used to connect the A&B output of left QE
RTIM(TIM_TypeDef*) : the Mbed 16 bits timer used to connect the A&B output of right QE
LPWM(PinName) : the Mbed pin used to send PWM for the left wheel's full bridge
RPWM(PinName) : the Mbed pin used to send PWM for the right wheel's full bridge
LDIRA(PinName) : the Mbed pin used to send direction to the Left wheel IN1 full bridge pin
LDIRB(PinName) : the Mbed pin used to send direction to the Left wheel IN2 full bridge pin
RDIRA(PinName) : the Mbed pin used to send direction to the Right wheel IN1 full bridge pin
RDIRB(PinName) : the Mbed pin used to send direction to the Right wheel IN2 full bridge pin

Definition at line 3 of file PID.cpp.


Member Function Documentation

void getPosition ( double *  x,
double *  y,
double *  theta 
)

Get position of the robot (in mm for X and Y and radian for Theta)

Parameters:
x(double - passed by reference) : actual position of the center of the robot, in mm, along X axis (front of the robot at startup)
y(double - passed by reference) : actual position of the center of the robot, in mm, along Y axis (left of the robot at startup)
theta(double - passed by reference) : radian angle between the normal to the line passing through the 2 wheels and the angle of the robot at startup
Note:
the position is updated each time a motion computation take place (ie : each milliseconds)

Definition at line 48 of file PID.cpp.

void getSpeed ( double *  vG,
double *  vD 
)

Get speed of the two wheels of the robot.

Parameters:
vG(double - passed by reference) : actual speed in mm/s of the left wheel
vD(double - passed by reference) : actual speed in mm/s of the right wheel

Definition at line 42 of file PID.cpp.

void resetPosition ( void   )

Reset position of the robot (in mm for X and Y and radian for Theta)

Note:
the positionis equal to 0 on all 3 axis (cannot be undone)

Definition at line 55 of file PID.cpp.

float setDerivativeValue ( float  KdValue )

Set the Kd value.

Parameters:
Kd(float) : value of Kd
Returns:
The value of Kd (float)
Note:
Can also be accessed using the global variable _Kd

Definition at line 30 of file PID.cpp.

float setintegralValue ( float  KiValue )

Set the Ki value.

Parameters:
Ki(float) : value of Ki
Returns:
The value of Ki (float)
Note:
Can also be accessed using the global variable _Ki

Definition at line 24 of file PID.cpp.

float setProportionnalValue ( float  KpValue )

Set the Kp value.

Parameters:
Kp(float) : value of Kp
Returns:
The value of Kp (float)
Note:
Can also be accessed using the global variable _Kp

Definition at line 18 of file PID.cpp.

void setSpeed ( double  left,
double  right 
)

Set the Set point value of the speed for integrated full bridge.

Parameters:
left(double) : Set point value for left wheel speed (in mm/s)
right(double) : Set point value for right wheel speed (in mm/s)

Definition at line 36 of file PID.cpp.


Field Documentation

double _Kd

Global Variable of corrective derivative value.

Note:
works well with Kp = 2.0, Ki = 0.4 and Kd = 1

Definition at line 160 of file PID.h.

double _Ki

Global Variable of corrective integral value.

Note:
works well with Kp = 2.0, Ki = 0.4 and Kd = 1

Definition at line 155 of file PID.h.

double _Kp

Global Variable of corrective proportionnal value.

Note:
works well with Kp = 2.0, Ki = 0.4 and Kd = 1

Definition at line 150 of file PID.h.

double _measDistD

Global Variable of measured Right wheel displacement.

Definition at line 184 of file PID.h.

double _measDistG

Global Variable of measured Left wheel displacement.

Definition at line 180 of file PID.h.

double _measSpeedD

Global Variable of measured right speed.

Definition at line 176 of file PID.h.

double _measSpeedG

Global Variable of measured left speed.

Definition at line 172 of file PID.h.

double _PwmValueD

Global Variable of Right wheel PWM value.

Definition at line 202 of file PID.h.

double _PwmValueG

Global Variable of Left wheel PWM value.

Definition at line 198 of file PID.h.

double _SpeedD

Global Variable of Right speed.

Definition at line 168 of file PID.h.

double _SpeedG

Global Variable of Left speed.

Definition at line 164 of file PID.h.

double _THETA

Global Variable of odometric measurement of THETA angle of gravity center of robot.

Note:
odometric error increase with time

Definition at line 217 of file PID.h.

Global Variable to indicate that required Right wheel speed is unreachable (set if speed is unreachable)

Note:
Must be cleared by user

Definition at line 194 of file PID.h.

Global Variable to indicate that required Left wheel speed is unreachable (set if speed is unreachable)

Note:
Must be cleared by user

Definition at line 189 of file PID.h.

double _X

Global Variable of odometric measurement of X position of gravity center of robot.

Note:
odometric error increase with time

Definition at line 207 of file PID.h.

double _Y

Global Variable of odometric measurement of Y position of gravity center of robot.

Note:
odometric error increase with time

Definition at line 212 of file PID.h.

Global Variable to indicate that required speed is unreachable (=1 if speed is unreachable)

Note:
Must be cleared by user

Definition at line 222 of file PID.h.