Matlab xPC Getting Started

The purpose of this page is to give a brief description of how to get the Mathwork's xPC system up and running. The help and demos on the Mathwork's website are pretty good but do not cover some of the issues I ran into when first setting up the system. I will also touch on some of the issues specific to the equipment in our lab. It is by no means an exhaustive tutorial but a guide to getting xPC up and running if you are unfamiliar with it.

What is xPC?

xPC Target is a realtime operating system developed by the Mathworks that allows you to compile and build Simulink models (with a C-compiler) to run realtime simulations and hardware in-the-loop control. The xPC kernel runs on a target PC that can have basically any operating system or processor. Typically you will boot the target PC with an xPC target boot disk (floppy) so you need a floppy drive that is working. You actually build and compile the models on a Host PC (non-realtime) that is running Matlab and a C-compiler. The Host and Target PC communicate via TCP/IP or serial protocols. I recommend using TCP/IP and this guide assumes that you are using this protocol.

Target PC Requirements

The target PC need not be very new or fast. It only needs to have a reasonable processor, floppy disk drive that it can boot from, and an Ethernet card supported by xPC. A list of supported Ethernet cards can be found on the Mathwork's website. The list is pretty short. However, I was able to get one that was not on the list to work. The critical thing is that the drivers for the card be supported by xPC. The card currently installed on the PC I am using for the target (Threepio) has a card in it that works with the NE2000 driver option, which is supported. If you can't get an existing card to work, buy one from the list on the web. The Target can have a keyboard and mouse but this is not required. I would recommend it initially to make sure everything works correctly. Once you've got it running, you can get rid of it if you want.

Host PC Requirements

The Host PC can be Windows, Mac, Linux or whatever. It must have Matlab, Simulink, Realtime toolbox, xPC Target toolbox, and Simulink Fixed Point toolbox. You must also have a C-compiler installed such as Visual Studio. The Host PC must also have a floppy disk drive so you can create the boot disk for the target, and of course an Ethernet card, though it need not be supported by xPC since it uses the operating system's drivers. The Host and Target PC need not be connected to one another directly but both need to be on the same LAN and the Target must have a static IP address.

Getting Started

Once your hardware and software are set up as described above, you need to set your working directory somewhere outside the Matlab root directory. To do this, right-click on the Matlab launch icon and select Start in..., and then enter a directory. The instructions on the web do not tell you this but if you choose a directory path with spaces, you will have problems creating a boot disk later. It is a bug that may be fixed in later versions but it caused me problems initially. I used C:\xPC as my directory.

Now you need to set the environment parameters for the host and target PC. You can do this in xPC Target Explorer, a GUI for this kind of stuff. Just type xpcexplr to launch it. For the Host PC configuration all you need to do is select the compiler. In the left pane of the xPC target explorer, click on the Compiler(s) Configuration node. Pick your C-compiler from the dropdown list (Visual C for Threepio), and type in the path of the compiler (C:\program files\Microsoft visual studio). Click apply to save.

Next you need to configure the Target PC. To do this, expand the Target PC1 node in the explorer. If no target PCs appear or you want to create a new one, right click on the host PC node and select Add Target. Click on the communications node. Select TCPIP as your communication mode and PCI or ISA depending on what type of Ethernet card you are using. Threepio uses PCI. Enter the IP address and masks etc. for the target in the boxes. You will need to enter the driver type for your Ethernet card as well. For Threepio as currently configured, these are the properties:

  • IP Address:
  • Target Port: 22222
  • LAN Subnet Mask:
  • Gateway Address:

If you cannot easily determine the IP address etc. for your computer because it is not booting a normal operating system (I had this problem with Threepio), try plugging a laptop into the router where you plan to put the target and see what IP address it is assigned.

Once this is done you're ready to create a boot disk.

Boot Disk

Get a blank floppy disk and put it in the Host Computer's A drive. Reformat the disk using a format A: at the command prompt. This can take a while. Then open target explorer and click on the configuration node under the target PC that you want to create a disk for. Boot Floppy should be the only option you have on this pane. Click the Create Boot Disk button and click OK when the box pops up telling you to insert a disk. When it's done, you can check the disk by trying to create it again. If it's a good boot disk, you'll get a pop-up saying so.

Put the disk in the target PC's A drive and turn it on. It should load the xPC Kernel. If you see on the boot screen something like Loading xPC Kernal...., but it hangs and doen not boot after about 30 seconds, get a new disk and repeat the process above. I tried 3 disks before I got one to work. Apparently it's very particular about this. Other people I've talked to have had the same problem.

Once the Kernel is loaded, you can test the installation by typing xpctest at the Matlab prompt on the host PC. it will ping the target and try to build and download a demo model. If all goes well, the test will be successful. If you get a failed test, you'll need to troubleshoot that part of the configuration (e.g. communication, complier).

Create a Simulink Model

The details of how to create a Simulink model are not covered here. However there are a few xPC specific items that you need to do with your model to allow it to work properly. First of all, you must use a fixed-step solver. You can select this and set the sample time in the Configuration Parameters window under the Simulation menu in Simulink. Click on the solver node in the configuration pane. All of your sample times for any components you add to the system need to be integer multiples of your fixed-step sample time.

If you want to display a signal on a scope, you will need to use the xPC Target Scope block rather than a normal Simulink scope. The target scope will display on the Target PC Monitor. You can find this block in the Simulink library browser under xPC Target>Misc. There are also special xPC blocks for importing and exporting data to/from the workspace or files, though you can also use the in/out blocks under sinks in the library browser.

If you are using A/D boards to do hardware in-the-loop control, make sure the board you are using is supported by xPC. There is a list on the web. You can use PCI or ISA devices. If you use ISA boards, you will need to configure the BUS address for the card to work. It's pretty easy to add A/D and D/A channels to your Simulink model. In the library browser, open xPC and then D/A, A/D, DIO or whatever you want to do. Then select your board from the list and place it in your model. Then double click on it to select the address/channels/levels etc for the board you are using. That's pretty much it.

Build and run the Model

When your model is done, you need to configure the realtime workshop parameters. Under the simulation menu, open Configuration Parameters. Click on the Realtime Workshop node. As your target file select xpctarget.tlc. Now click on the xPC Target options node. Make sure the execution mode is real-time. Select other parameters as meets your needs. Then give a name to the Target object or use the default (tg).

Now you can build the model. This creates the C code and downloads the program to the Target. To do this, in the Simulink window click on Tools>Realtime workshop>Build Model or click the incremental build button in Simulink. You should see a bunch of text on the Matlab screen with a successful completion message at the end if there were no errors. The build process will create a Target object (tg). You can see it parameters by typing tg at the command prompt. The easiest way to start the program is just to type any of the following at the command prompt:

  • start(tg)
  • tg.start
  • +tg

When it's done or when you want to stop it, just type stop(tg), tg.stop or -tg.

That covers the basics. I'm still learning how to use many of the features. The online documentation is quite good. Another good resource is Chris Gerdesí group next door as they use this system extensively.

-- JasonWheeler - 09 Nov 2006

This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback