CALPOLY MECHATRONICS
Documentation for all Mechatronics Labs
controller.Controller Class Reference

A state machine to control a motor using PID. More...

Public Member Functions

def __init__ (self, motor, encoder, closedLoop, state=0)
 Method to initialise a Controller object. More...
 
def runStateMachine (self)
 A method that runs through the logic of the controller state machine once.
 
def transitionStates (self, state, transition=True)
 A method that changes the state machine state, self.state and prints the state transition message. More...
 
def pointControl (self, pos, vel)
 A method to calculate the motor PWM to get to a given position and velocity. More...
 
def update (self)
 A method to check the encoder position and velocity.
 
def getNextReferencePosition (self, file)
 A method that updates self.referencePoint to the next line in the reference csv. More...
 
def step (self, magnitude, timeOut, delayms=20)
 Method to subject the motor to a step function. More...
 
def selfTune (self, pos, vel, timeOut=5, delayms=20, pIn=1, iIn=0.01)
 Method to self tune the PI gains. More...
 

Public Attributes

 motor
 The Motor object to control.
 
 encoder
 The Encoder object to get feedback from the motor.
 
 closedLoop
 The ClosedLoop object to calculate closed loop control PWM level.
 
 state
 The current state of the state machine.
 
 position
 The position of self.motor.
 
 targetPosition
 The current target position for the motor.
 
 velocity
 The current velocity of self.motor.
 
 targetVelocity
 The current target velocity of the motor.
 
 prevTime
 The last time (in ms from startup) that self.update() was called.
 
 lastTransition
 The last time (in ms from startup) that the state machine transitioned states.
 
 referenceFile
 The .csv reference file of format time(s), velocity (rpm), position (deg) to control the motor to.
 
 referencePoint
 The current point reference point, format [time, velocity, position].
 

Static Public Attributes

dictionary stateMessages
 A Dictionary of messages to display at each state change. More...
 

Detailed Description

A state machine to control a motor using PID.

The controller task is a state machine that uses closed loop feedback from an encoderDriver.encoder object to move a motor, controlled using a motorDriver.motor object along a reference path and velocity. The controller task also has several methods, controller.Controller.selfTune, step, and ramp, for automatically tunning the PID gains and are not implemented as part of the state machine. These methods are only for debugging and tuning and should not be run with this, or any other state machine, as they will pause the program until complete. Link to source code: https://bitbucket.org/ebriefer/me305_lab/src/master/Final%20Project/controller.py

Constructor & Destructor Documentation

◆ __init__()

def controller.Controller.__init__ (   self,
  motor,
  encoder,
  closedLoop,
  state = 0 
)

Method to initialise a Controller object.

Parameters
motorA Motor object to control.
encoderAn Encoder object to get feedback from the motor.
closedLoopA ClosedLoop object to calculate the needed motor PWM level.
stateThe initial state to start the state machine in.

Member Function Documentation

◆ getNextReferencePosition()

def controller.Controller.getNextReferencePosition (   self,
  file 
)

A method that updates self.referencePoint to the next line in the reference csv.

Parameters
fileThe File object to read from, should be a .csv of the same format as self.referenceFile.

◆ pointControl()

def controller.Controller.pointControl (   self,
  pos,
  vel 
)

A method to calculate the motor PWM to get to a given position and velocity.

Parameters
posThe target position to move the motor to, in rotations.
velThe target velocity to move the motor at, in rotations per second.
Returns
The motor PWM level, from -100 to 100 (or capped at self.closedLoop saturation level)

◆ selfTune()

def controller.Controller.selfTune (   self,
  pos,
  vel,
  timeOut = 5,
  delayms = 20,
  pIn = 1,
  iIn = 0.01 
)

Method to self tune the PI gains.

This method is not implemented for use in a state machine and should not be run in conjucntion with any state machines. This method is only for testing and tuning.

Parameters
posThe magnitude of the step function used for tuning Kp, in rotations.
velThe magnitude of the ramp function used for tuning Kd, in rotaions per second.
timeOutThe duration of each test, in seconds
delaymsThe period to update the motor controler at, in ms
pInThe initial Kp to guess. should be the minimum realistic Kp, not 0.
iInThe initial Ki to guess. should be the minimum realistic Ki, not 0.

◆ step()

def controller.Controller.step (   self,
  magnitude,
  timeOut,
  delayms = 20 
)

Method to subject the motor to a step function.

This method is not implemented for use in a state machine and should not be run in conjucntion with any state machines. This method is only for testing and tuning.

Parameters
magnitudeThe magnitude of the step function, in rotations.
timeOutThe durration to run the step function for, in seconds.
delaymsThe period to update the motor control at, in ms.
Returns
The J value of the controler's step response'

◆ transitionStates()

def controller.Controller.transitionStates (   self,
  state,
  transition = True 
)

A method that changes the state machine state, self.state and prints the state transition message.

Parameters
stateThe state to transition to.
transitionA boolean for wether or not to transition, if False no transition will occur.

Member Data Documentation

◆ stateMessages

dictionary controller.Controller.stateMessages
static
Initial value:
= {0 : 'State 0: startup',
1 : 'State 1: Enabled',
2 : 'State 2: Wating',
3 : 'State 3: Reseting',
4 : 'State 4: Error'}

A Dictionary of messages to display at each state change.


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