MATLAB Motor Controller

From Mech
Revision as of 14:24, 3 July 2009 by John Rula (talk | contribs)
Jump to navigationJump to search

MATLAB Motor Controller

This project expands on the work done by Matt Turpin and his I2C Motor Controller. In this premise, the slave PIC controls the high speed motor with quadrature encoding. The slave PICs used are the 18f4431 since they have a built in quadrature encoder. The slave PICs implement volatile SRAM to track motor positions to aid in control tuning. The slave PICs are controlled via I2C by the master PIC which is an 18f4520. The master PIC is controlled through MATLAB over RS-232. The motors can be monitored, controlled and tracked through a MATLAB GUI (shown below).

JR-matlab-gui2.jpg

SRAM Memory

SPI SRAM

The project originally was going to use SPI SRAM from Microchip since you can find capacities around 256kbit with only 4 connections. Unfortunately I was unable to get this memory to work with the 5V PICS I was using. I tested the Microchip 23A256 and Winbound SPI SRAM chips. Neither of these chips run at 5V, the Microchip SRAM runs at 1.8V and the Winbound chip runs at 3.3V. I tried voltage dividers, diode drops and pull-ups with diodes and was unsuccessful at communicating with the SRAM chips.For communicating through SPI, there are several other pages on the wiki which discuss it more in detail.

Parallel SRAM

I instead used parallel memory in the final design, with the only downside that this memory requires 26 dedicated pins from the PIC (15 for address, 8 for data I/O and 3 for control). I wrote a small library for communicating with parallel memory located [here]. Notice that since so many pins are required, they will most likely always be different, so the pins are listed in order in arrays data_pins[] and address_pins[]. The parallel ram functions can be found here.

Code

The code for the project can be found here.

MATLAB GUI Programming

The GUI for the controller is programmed through the GUIde feature of MATLAB. The code for the GUI is contained [here]. The difference between normal MATLAB programming and GUI programming is that there are no global variables for storing values that are easily creatable or accessible. If you want to access data from a GUI component, you have to through the 'handles' structure, which is a structure containing all of the GUI components on your form. In order to access a string in a text-box, it has to be retrieved in the following manner:

 str = get(handles.textbox-1, 'String')

Also note that in GUI programming, every component has a Callback, which is called when an action is performed on a component (ie a button is pushed). Within the callback, three things are passed, the object itself called 'hobject', 'eventData' which does nothing, and the 'handles' structure. Utilizing callbacks are how you are able to perform actions based on interaction.

If you want to store variables that you want to either access at a later time, or have variables that you can only create once (ie serial ports), then you have to store them in the application data structure. Application data can be placed and accessed in the following way:

 setappdata(hObject, 'serialport', sp)
 sp = getappdata(handles.pushbutton1, 'serialport')

where 'serialport' is the name give in the structure, and sp is what you are storing, in this case an open serial port. Also note that since the application data was stored under pushbutton1, it has to always be accessed through that same component.