myWRIO
C++ framework for NationalInstruments myRIO
myRIO::MotorPID Class Reference

This class can be used to regulate a motor speed. More...

#include <MotorPID.h>

Public Member Functions

 MotorPID (double kp, double ki)
 Set the PI coefficients of the motors. More...
 
double compute (long enc)
 Refresh the PID output. More...
 
void setSetpoint (double setpoint)
 Set the setpoint. More...
 
double getSetpoint ()
 Get the setpoint. More...
 
double getAvgSpeed ()
 Calculate the average of the last NB_AVG speeds. More...
 
 ~MotorPID ()
 Destructor (Does nothing) More...
 

Private Attributes

Time stopwatch
 Timer to get the elapsed tiem since the last refresh. More...
 
const double toAngularSpeed = 360./(12.*52.734)
 The encoder gives 12 clock edges for a complete turn (360°). More...
 
double lastSpeeds [NB_AVG]
 Moving average filter. More...
 
long lastEnc
 The value of the encoder, last time mesured. More...
 
double setpoint
 The setpoint of the motors. More...
 
double errSum
 The sum of the errors Errors from integration. More...
 
double kp
 Proportional coefficient. More...
 
double ki
 integral coefficient More...
 

Detailed Description

This class can be used to regulate a motor speed.

Examples
example_motor_pid_encoder.cpp, project_car.cpp, and project_pendulum.cpp.

Definition at line 14 of file MotorPID.h.

Constructor & Destructor Documentation

◆ MotorPID()

MotorPID::MotorPID ( double  kp,
double  ki 
)

Set the PI coefficients of the motors.

Parameters
kpproportional coefficient
kiintegral coefficicent This functions sets the coefficients of the PI motor corrector. And initialises a vector with 0's.

Definition at line 11 of file MotorPID.cpp.

◆ ~MotorPID()

MotorPID::~MotorPID ( )

Destructor (Does nothing)

Definition at line 83 of file MotorPID.cpp.

Member Function Documentation

◆ compute()

double MotorPID::compute ( long  enc)

Refresh the PID output.

Parameters
encThe current encoder value
Returns
The output value, in degrees per second

Definition at line 38 of file MotorPID.cpp.

◆ getAvgSpeed()

double MotorPID::getAvgSpeed ( )

Calculate the average of the last NB_AVG speeds.

Returns
the average

Definition at line 73 of file MotorPID.cpp.

◆ getSetpoint()

double MotorPID::getSetpoint ( )

Get the setpoint.

Returns
returns the setpoint

Definition at line 29 of file MotorPID.cpp.

◆ setSetpoint()

void MotorPID::setSetpoint ( double  setpoint)

Set the setpoint.

Parameters
setpointthe setpoint that we want to give to the motor

Definition at line 22 of file MotorPID.cpp.

Member Data Documentation

◆ errSum

double myRIO::MotorPID::errSum
private

The sum of the errors Errors from integration.

Definition at line 35 of file MotorPID.h.

◆ ki

double myRIO::MotorPID::ki
private

integral coefficient

Definition at line 40 of file MotorPID.h.

◆ kp

double myRIO::MotorPID::kp
private

Proportional coefficient.

Definition at line 39 of file MotorPID.h.

◆ lastEnc

long myRIO::MotorPID::lastEnc
private

The value of the encoder, last time mesured.

Definition at line 33 of file MotorPID.h.

◆ lastSpeeds

double myRIO::MotorPID::lastSpeeds[NB_AVG]
private

Moving average filter.

Definition at line 31 of file MotorPID.h.

◆ setpoint

double myRIO::MotorPID::setpoint
private

The setpoint of the motors.

Definition at line 34 of file MotorPID.h.

◆ stopwatch

Time myRIO::MotorPID::stopwatch
private

Timer to get the elapsed tiem since the last refresh.

Definition at line 26 of file MotorPID.h.

◆ toAngularSpeed

const double myRIO::MotorPID::toAngularSpeed = 360./(12.*52.734)
private

The encoder gives 12 clock edges for a complete turn (360°).

The gear ratio is 1/52.734 ; hence, an edge is 0.568893°

Definition at line 28 of file MotorPID.h.


The documentation for this class was generated from the following files: