Trouble controlling stepper motor from matlab

13 Jul 2011

Hi All I am currently building a stepper motor driven linear actuator. This motor is being driven by the mbed NXP-LPC1768 via the Sure Step (2 N-m torque) driver. Now, when I run the motor by uploading the code, it works fine. But, when I connect it via USB virtual serial interface to Matlab, and run it from there; my motor speed greatly reduces; and I am not able to increase it. I am running this off a GUI I made in Matlab. (I would like to be this way i.e. the motor run form the GUI in Matlab)

Also, I am not sure whether just hooking it up to the USB virtual port is the correct way. I was thinking of connecting it via RS-232 and TTL converter to the PC,but I do not want to spend without being sure of it.

Any suggestions would be welcome.

19 Jul 2011

Hi Aayush,

Are you using a real-time environment?(my guess is no).

A 'real-time' system (or model) is a model that runs on a very specific schedule where certain time constraints must be met, such as sending or receiving data over serial. Most operating systems are not 'real-time'. The operating system, for instance, can decide when it wants to send or receive data (or perform some other task) based on what's running in the background. Normaly, for every day use, this is not an issue because the task eventually executes (give or take dozens of milliseconds). When you want to perform faster (or more periodic) tasks, a non-real time operating system (RTOS for short) such as windows or mac or linux will not be able to keep up because its trying to perform other (less meaningful) tasks. The problem is that the operating system does not know that your matlab process and the serial port should be treated as real-time tasks.

Matlab/Simulink have a work-around where you can install a real-time kernel without installing an RTOS The Real-Time Windows Target (for windows only) which will cause the simulation to run on a different level (i.e. higher priority) within the operating system. I've never tried to use the real-time windows target, so I'm afraid I won't be much help with that.

Can you tell us more about your setup (i.e. serial baud rate, simulation settings, timing requirements, perhaps even a screenshot of the models). Maybe we can tweak something to get you closer to what you want.

I've tried to interface simulink with the mbed. It works ok. It's kinda hard to get a robust connection without flow control.

The serial port is certainly fast enough for most applications. The issue is flow control and scheduling of the OS and simulink model.

But feel free to post some more details and maybe someone can help you out!

22 Jul 2011

I am currently using the virtual serial USB. I found the solution though. I tried calling custom functions/variables over RPC, and it worked. I am going to try the same approach over serial and see the results. Will post it once I get it working. Thanks for the reply Igor.