ME 405 Portfolio
Functions | Variables
main.py File Reference

File that controls motors for term project. More...

Functions

def main.linspace (a, b, n)
 Special interpolation function that creates a list of interpolated points excluding the last point. More...
 
def main.g (x, theta)
 A function computng the difference between a desired location and current location. More...
 
def main.dg_dtheta (theta)
 A function computng the Jacobian of our Pen Plotter. More...
 
def main.NewtonRaphson (fcn, jacobian, guess, thresh)
 Determines the motor angles required to achieve a desired x and y position. More...
 
def main.startup ()
 A function that performs all calculations before the motors start drawing. More...
 
def main.task_main ()
 A task that controls the motors to draw the desired shape. More...
 

Variables

 main.CLK_pin = Pin.cpu.B7
 Creates a clock pin. More...
 
 main.SPI_CLK_pin = Pin.cpu.B13
 Creates an SPI clock pin. More...
 
 main.MISO_pin = Pin.cpu.B14
 Creates a MISO pin. More...
 
 main.MOSI_pin = Pin.cpu.B15
 Creates a MOSI pin. More...
 
 main.CS1_pin = Pin.cpu.C3
 Creates a chip select pin for the first motor. More...
 
 main.CS2_pin = Pin.cpu.C2
 Creates a chip select pin for the second motor. More...
 
 main.ENN1_pin = Pin.cpu.C0
 Creates an Enable pin to interface with the first TMC 2208. More...
 
 main.ENN2_pin = Pin.cpu.B0
 Creates an Enable pin to interface with the second TMC 2208. More...
 
 main.tim = pyb.Timer(4, period=3, prescaler=0)
 Creates timer. More...
 
 main.clk = tim.channel(2, pin=CLK_pin, mode=pyb.Timer.PWM, pulse_width=2)
 Configures the timer. More...
 
 main.nCS1 = Pin(CS1_pin, mode=Pin.OUT_PP, value=1)
 Configures the chip select pin for the first motor. More...
 
 main.nCS2 = Pin(CS2_pin, mode=Pin.OUT_PP, value=1)
 Configures the chip select pin for the second motor. More...
 
 main.ENN1 = Pin(ENN1_pin, mode=Pin.OUT_PP, pull=Pin.PULL_NONE)
 Configures the Enable pin for the first motor. More...
 
 main.ENN2 = Pin(ENN2_pin, mode=Pin.OUT_PP, pull=Pin.PULL_NONE)
 Configures the Enable pin for the second motor. More...
 
 main.spi = SPI(2, SPI.MASTER, baudrate=100000, polarity=1, phase=1)
 Configures SPI. More...
 
 main.motor1 = stepperdriver.StepperDriver(nCS1, ENN1, spi, reverse=False)
 Creates a motor object for the first motor. More...
 
 main.motor2 = stepperdriver.StepperDriver(nCS2, ENN2, spi, reverse=False)
 Creates a motor object for the second motor. More...
 
 main.pen = actuator.Actuator()
 Creates a pen object for the linear actuator. More...
 
def main.num = startup()
 Number of motor angles. More...
 
 main.elements = task_share.Share ('h', thread_protect = False, name = "Elements")
 A share containing the number of positions. More...
 
 main.task1 = cotask.Task(task_main, name='Task_Main', priority=1, period=5, profile=True, trace=False)
 Creates a task for the main function. More...
 

Detailed Description

File that controls motors for term project.

This file performs all operations necessary to control the Pen Plotter such that it draws a desired shape.
It parses the HPGL code, performs selective interpolation, uses a Newton-Raphson method to compute motor angles, and sends motor commands to the motors.

@author     Baxter Bartlett
@author     Jake Lesher
@date       6/5/2022

Function Documentation

◆ dg_dtheta()

def main.dg_dtheta (   theta)

A function computng the Jacobian of our Pen Plotter.


This function is used in compute the Jacobian of our Pen Plotter during Newton-Raphson.

Parameters
thetaA 2x1 matrix containing the current location for an iteration of Newton-Raphson.
Returns
A 2x2 matrix containing the Jacobian of the system.

◆ g()

def main.g (   x,
  theta 
)

A function computng the difference between a desired location and current location.


This function is used in the Netwon-Raphson method to determine when to stop iterating.

Parameters
xA 2x1 matrix containing the desired location for a round of Newton-Raphson.
thetaA 2x1 matrix containing the current position for an iteration of Newton-Raphson.
Returns
A 2x1 matrix containing the difference between the desired and current position.

◆ linspace()

def main.linspace (   a,
  b,
  n 
)

Special interpolation function that creates a list of interpolated points excluding the last point.


This function interpolates a specified number of points between two points. It then places all those points in a list, but excludes the last point.

Parameters
aThe first point to interpolate between.
bThe second point to interpolate between.
nNumber of points plus one for which the interpolated list should contain.
Returns
A list of interpolated points excluding b.

◆ NewtonRaphson()

def main.NewtonRaphson (   fcn,
  jacobian,
  guess,
  thresh 
)

Determines the motor angles required to achieve a desired x and y position.


This function uses a Newton-Raphson method to determine the motor angles required to achieve a desired x and y position.

Parameters
fcnPlaceholder for the difference function (g) to be passed into the function. fcn must be a lambda function that passes the the desired location, effectively making g a function of the motor angles solely.
jacobianPasses in the jacobian function.
guessA 2x1 matrix containing an initial motor angle guess to start iteration.
threshA number for which g should be less than to stop iteration.
Returns
The motor angles required to achieve a given position.

◆ startup()

def main.startup ( )

A function that performs all calculations before the motors start drawing.


This function is called at the start of the drawing process. It parses the HPGL code, performs selective interpolation, and executes the Newton-Raphon method on every location to obtain the required motor angles. To store the immense amount of data points, two text files are used: one to store interpolated points and one to store points obtained via Newton-Raphson.

Returns
The number of data points.

◆ task_main()

def main.task_main ( )

A task that controls the motors to draw the desired shape.


This function reads from the text file containing the points obtained through the Newton-Raphson method.
It sends these points to the motors if the motors have arrived at the previous position. It also sends pen-up and pen down commands to the linear actuator and writes x location, y location, and arm angle over UART such that a live animation of the robot drawing can be made.

Variable Documentation

◆ clk

main.clk = tim.channel(2, pin=CLK_pin, mode=pyb.Timer.PWM, pulse_width=2)

Configures the timer.


The timer is configured as a clock to facillitate data transfer.

◆ CLK_pin

main.CLK_pin = Pin.cpu.B7

Creates a clock pin.

The clock pin is used to create a timer.

◆ CS1_pin

main.CS1_pin = Pin.cpu.C3

Creates a chip select pin for the first motor.

The clock pin is used to indicate which motor to read/write from in SPI.

◆ CS2_pin

main.CS2_pin = Pin.cpu.C2

Creates a chip select pin for the second motor.

The clock pin is used to indicate which motor to read/write from in SPI.

◆ elements

main.elements = task_share.Share ('h', thread_protect = False, name = "Elements")

A share containing the number of positions.


This share is used to determine when to cease writing motor angles to the motors.

◆ ENN1

main.ENN1 = Pin(ENN1_pin, mode=Pin.OUT_PP, pull=Pin.PULL_NONE)

Configures the Enable pin for the first motor.


Configures the Enable pin as a Push-Pull.

◆ ENN1_pin

main.ENN1_pin = Pin.cpu.C0

Creates an Enable pin to interface with the first TMC 2208.

Each motor requires a TMC 2208 IC, which requires an Enable Pin.

◆ ENN2

main.ENN2 = Pin(ENN2_pin, mode=Pin.OUT_PP, pull=Pin.PULL_NONE)

Configures the Enable pin for the second motor.


Configures the Enable pin as a Push-Pull.

◆ ENN2_pin

main.ENN2_pin = Pin.cpu.B0

Creates an Enable pin to interface with the second TMC 2208.

Each motor requires a TMC 2208 IC, which requires an Enable Pin.

◆ MISO_pin

main.MISO_pin = Pin.cpu.B14

Creates a MISO pin.

The pin is used as a MISO line in SPI

◆ MOSI_pin

main.MOSI_pin = Pin.cpu.B15

Creates a MOSI pin.

The pin is used as a MOSI line in SPI

◆ motor1

main.motor1 = stepperdriver.StepperDriver(nCS1, ENN1, spi, reverse=False)

Creates a motor object for the first motor.


Configures the first motor.

◆ motor2

main.motor2 = stepperdriver.StepperDriver(nCS2, ENN2, spi, reverse=False)

Creates a motor object for the second motor.


Configures the second motor.

◆ nCS1

main.nCS1 = Pin(CS1_pin, mode=Pin.OUT_PP, value=1)

Configures the chip select pin for the first motor.


Configures the chip select pin as a Push-Pull.

◆ nCS2

main.nCS2 = Pin(CS2_pin, mode=Pin.OUT_PP, value=1)

Configures the chip select pin for the second motor.


Configures the chip select pin as a Push-Pull.

◆ num

def main.num = startup()

Number of motor angles.


Variable containing the number of positions.

◆ pen

main.pen = actuator.Actuator()

Creates a pen object for the linear actuator.


Configures the linear actuator.

◆ spi

main.spi = SPI(2, SPI.MASTER, baudrate=100000, polarity=1, phase=1)

Configures SPI.

Creates an SPI object for SPI between the motors and microcontroller.

◆ SPI_CLK_pin

main.SPI_CLK_pin = Pin.cpu.B13

Creates an SPI clock pin.

The clock pin is used to control the rate that data is sent in SPI.

◆ task1

main.task1 = cotask.Task(task_main, name='Task_Main', priority=1, period=5, profile=True, trace=False)

Creates a task for the main function.


This task is called by the scheduler.

◆ tim

main.tim = pyb.Timer(4, period=3, prescaler=0)

Creates timer.


The timer is used to control the rate of data transfer.