C Language Programming in RTSys for Motion Controllers
Modern motion controllers often require the flexibility of high-level languages for complex algorithms while maintaining the simplicity of BASIC for rapid development. The RTSys software environment allows engineers to combine C language functions with traditional BASIC programs, unlocking advanced capabilities in industrial automation control systems. This integration is particularly useful for tasks like custom mathematical computations, data processing, or interfacing with external devices.
Before diving into the implementation, it’s essential to understand the prerequisites and limitations. The controller firmware must support C function interfaces; look for firmware versions labeled with “cfunc”. Additionally, a single C function within a file can only be declared and called from one BASIC file, preventing conflicts. However, different C functions from the same file can be declared in separate BASIC files, provided the declared function names are unique.
The DEFINE_CFUNC Keyword and Data Types
The bridge between BASIC and C is the DEFINE_CFUNC keyword. It declares a C function within a BASIC program, assigning it a local alias. The syntax is straightforward: DEFINE_CFUNC local_name return_type c_function_name(parameter_types).
Supported data types for parameters and return values include int, float, double, and TYPE_TABLE. The TYPE_TABLE type is especially important when interacting with the controller’s TABLE array, which stores global data. On series 4 and above controllers, TYPE_TABLE corresponds to a double precision floating-point number. When passing arrays or large data structures, using TYPE_TABLE ensures seamless data exchange.
Practical Examples
Let’s examine a typical use case. Suppose you need a C function to perform a division operation with floating-point numbers, and another to initialize some memory. The C code might look like this:
int userc_init(void) {
int* p = (int*)malloc(sizeof(int));
p[0] = 88;
printf("p[0]=%d\n", p[0]);
free(p);
return 0;
}
float divf(float a, float b) {
return (a / b);
}
TYPE_TABLE divd(TYPE_TABLE a, TYPE_TABLE b) {
return (a / b);
}
In the BASIC program, you would declare these functions using DEFINE_CFUNC and then call them with the assigned names:
DEFINE_CFUNC userc_init int userc_init(void) DEFINE_CFUNC userc_divf float divf(float a, float b) DEFINE_CFUNC userc_divd double divd(double a, double b) ?userc_init() ?userc_divf(23.1, 1) ?userc_divd(23.3, 1)
The output would display the initialized value and the division results. Note that the function names in BASIC (userc_init, userc_divf, etc.) can differ from the original C function names, providing flexibility in naming conventions.
Important Considerations
- Functions with no parameters can be directly used in the
INT_CYCLEtask for periodic execution. - A C function called from BASIC can have a maximum of 8 parameters.
- Ensure C code is robust and follows best practices to avoid crashes or deadlocks. Always test in RAM download mode first.
- C functions must execute quickly to maintain the real-time performance of the BASIC tasks. Avoid long loops or blocking calls.
Compilation Platform Selection
Different controller models require specific compilation platforms. The table below summarizes the supported controllers and their corresponding platforms. Only the listed models currently support C language integration; for other series, consult technical support.
| Controller Series | Compilation Platform |
|---|---|
| XPLC series | x86-Linux |
| ZMC4 series | ARM-Linux |
| ZMC3 series | ARM-Linux |
To set the compilation platform in RTSys, right-click in the blank area of the “File View”, select “Settings”, and choose the appropriate platform from the dropdown list. This ensures the C code is compiled correctly for the target hardware.
Step-by-Step Guide to Using C Functions in RTSys
- Create a New Project: In RTSys, click “File” > “New Project”. Choose a storage path and name your project.
- Add a BASIC File: Right-click on the project, select “New File”, choose “Basic” as the file type, and name it accordingly.
- Add a C File: Similarly, create a new file but select “C” as the type. This file will contain your custom C functions.
- Set Compilation Platform: Right-click in the “File View” area, go to “Settings”, and select the correct compilation platform for your controller model.
- Write the C Function: Begin with the necessary header includes, such as
#include "zmcbuildin.h"for built-in functions. Ensure the header file is in the project directory. Then write your C code following standard conventions. - Declare in BASIC: In your BASIC file, use
DEFINE_CFUNCto declare the C function with a local name. After declaration, call the function using that name.
It is recommended to edit C code directly within RTSys to avoid encoding issues that may arise when copying from other editors. Always download to RAM for initial testing to prevent permanent firmware issues.
By leveraging C functions in RTSys, engineers can significantly extend the capabilities of motion controllers, implementing custom algorithms and optimizing performance for demanding industrial automation tasks. The combination of BASIC’s simplicity and C’s power offers a versatile solution for modern control systems.