Mechatronics Portfolio
Logan Williamson's Mechatronics Portfolio and Documentation Repository
Public Member Functions | Public Attributes | List of all members
encoder.Encoder Class Reference

Interface with quadrature encoders. More...

Public Member Functions

def __init__ (self, chA_pin, chB_pin, timNum)
 Constructs an encoder object. More...
 
def update (self)
 Updates encoder position and delta. More...
 
def get_position (self)
 Returns encoder position. More...
 
def zero (self)
 Resets the encoder position to zero. More...
 
def get_delta (self)
 Returns encoder delta. More...
 
def get_speed (self)
 Returns encoder delta. More...
 

Public Attributes

 tim
 tim creates new timer object for storing the encoder position. More...
 
 prevTime
 prevTime creates a variable for storing the previous time that update ran. More...
 
 ch1
 creates channel 1 and channel 2 objects connected to the two pins input in the init function More...
 
 ch2
 
 prevCount
 Sets base object of prevCount (previous position) to 0. More...
 
 position
 Position is the absolute position of the encoder. More...
 
 vel_const
 Variable for converting encoder speed in ticks/microsecond to radians/second. More...
 
 pos_const
 Variable for converting encoder position in ticks toradians. More...
 
 clock
 
 count
 
 delta
 delta is the difference between the current and previous positions More...
 
 delta_t
 
 speed
 

Detailed Description

Interface with quadrature encoders.

When called, this class generates an encoder object and assigns the desired timer and pins to it such that the position and change in position since the last update of the encoder position can be determined and returned as outputs.

Constructor & Destructor Documentation

◆ __init__()

def encoder.Encoder.__init__ (   self,
  chA_pin,
  chB_pin,
  timNum 
)

Constructs an encoder object.

Upon calling the Encoder.py class, the init function will run automatically. To run properly, this requires three inputs to the Encoder.py class call. There are: one input for each of the desired pins you wish to associate with the two encoder sensor channels. Additionally, the timer number to be used for the encoder position count must be defined as the third input when calling this class. This function will then instanciate an encoder object with the above parameters.

Parameters
chA_pincreates empty object for an input of pin position. In this case we are using PB6 on the nucleo
chB_pincreates empty object for an input of pin position. In this case we are using PB7 on the nucleo
timNumcreates an empty object for input timer. We will be using timer 4

Member Function Documentation

◆ get_delta()

def encoder.Encoder.get_delta (   self)

Returns encoder delta.

if user asks for delta to be returned, this function will return delta value calculated from update funciton

Returns
The change in position of the encoder shaft between the two most recent updates

◆ get_position()

def encoder.Encoder.get_position (   self)

Returns encoder position.

If the user asks for the position, after position is calculated using update function, this function returns the calculated value

Returns
The position of the encoder shaft

◆ get_speed()

def encoder.Encoder.get_speed (   self)

Returns encoder delta.

if user asks for delta to be returned, this function will return delta value calculated from update funciton

Returns
The change in position of the encoder shaft between the two most recent updates

◆ update()

def encoder.Encoder.update (   self)

Updates encoder position and delta.

When called, this function will read and update the variable containing the encoder position, as well as the delta value of the encoder. This delta value will be analyzed for magnitude and direction to determine whether an overflow or underflow in the encoder position count has occurred since the last update. In the event that the encoder position count has overflowed or underflowed, this update function will add or subtract the appropriate value from the encoder position count to reflect the true position of the encoder relative to its home position.

Parameters
clockcreates a timer internal to the encoder which will be used to record the time

◆ zero()

def encoder.Encoder.zero (   self)

Resets the encoder position to zero.

If user inputs the desire to reset the counter to zero, this function resets the encoder position to zero

Member Data Documentation

◆ ch1

encoder.Encoder.ch1

creates channel 1 and channel 2 objects connected to the two pins input in the init function

ch1 and ch2 correspond to the inputs that will cause the encoder position to increment. This uses ENC_AB configuration which puts the timer in encoder mode: The counter changes when CH1 or CH2 changes. The order in which they change determines whether the encoder position increments or decrements.

◆ delta

encoder.Encoder.delta

delta is the difference between the current and previous positions

delta is used to test whether the counter has overflowed or not. It represents the change in count since the last time update() was called

◆ pos_const

encoder.Encoder.pos_const

Variable for converting encoder position in ticks toradians.

This variable is used to convert the position of the encoder from ticks to radians. microseconds to seconds

◆ position

encoder.Encoder.position

Position is the absolute position of the encoder.

position is initially set to zero and will be updated when update() runs to reflect the absolute position of the encoder.

◆ prevCount

encoder.Encoder.prevCount

Sets base object of prevCount (previous position) to 0.

This will be used in further calculations of delta, change in encoder position since the last update() call.

◆ prevTime

encoder.Encoder.prevTime

prevTime creates a variable for storing the previous time that update ran.

This is used to calculate the difference in time, delta_t, used to then calculate encoder speed.

◆ tim

encoder.Encoder.tim

tim creates new timer object for storing the encoder position.

Runs timer 4 on the nucleo with a period of 2^16-1 and a prescaler of 0

◆ vel_const

encoder.Encoder.vel_const

Variable for converting encoder speed in ticks/microsecond to radians/second.

This variable is used to convert the speed of the encoder object from ticks/microsecond to radians per second. This factor reflects the fact that the encoder has 4000 ticks/revolution or 4000 ticks per 2*pi radians, and the delta_t value this position is divided by must be converted from microseconds to seconds


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