Foundations
Before we delve into the practical aspects of programming Raspberry Industrial PLC interrupt inputs with C++, let's establish a foundational understanding of two key elements: interrupts and C++.
Interrupts:
Interrupts serve as signals that momentarily interrupt the standard flow of program execution to address specific events. Interrupts communicate efficiently with devices, and handle input/output tasks in computer systems.
C++: C++ stands out as a robust and versatile programming language widely utilized across diverse domains, including embedded systems and industrial automation. Our Raspberry PLC is well-equipped to handle and execute C++ code, making it a suitable choice for programming tasks associated with hardware control.
In case you want to know how to program Raspberry industrial PLC interrupt inputs with Python you can follow this guide.
Requirements
To successfully program Raspberry Industrial PLC interrupt inputs with C++, ensure the following requirements are met:
- Raspberry PLC Model: Obtain any model of Raspberry PLC from Industrial Shields. Explore the product range here.
- PLC Access: Gain access to the Raspberry Pi Industrial PLC through SSH or by using the HDMI port. Follow the instructions on how to access the Raspberry Pi PLC via SSH here. Alternatively, find guidance on accessing the Raspberry Pi PLC through the HDMI port here.
- Industrial Shields C Library: Ensure that the Industrial Shields C library is installed on your Raspberry PLC. Find the library on GitHub here.
Example
Hardware
- Power Supply: The Raspberry PLC should be powered within the range of 12-24V.
- Connectivity Cables: Utilize either an Ethernet cable or an HDMI cable to establish a connection with the Raspberry PLC.
- Current Supply Cables for Interrupt Inputs:
Employ suitable cables to provide current to the interrupt input pins, specifically (-)IX.5/INT and IX.5/INT. You can use the 5V and GND pins from the PLC.
- Symbol Identification:
Identify the interrupt inputs on your device by locating the
following symbol:
- GPIO Mapping: If you do not know the mapping between the Raspberry GPIOs and the I/O's of your PLC, you can take a look at this table, also included in the Datasheet and the User Guide:
For this example was used the pin GPIO13 of a Raspberry PLC 42+ which is the (-)IX.5/INT and IX.5/INT pins in the Raspberry PLC. Make sure to use the suitable pin for your configuration as you will need this information for the software implementation.
Software
First of all, ensure that you have the librpiplc library installed on your system.
This C++ program demonstrates the usage of interrupts on a Raspberry PLC, specifically handling GPIO13 as an interrupt input. The code uses the pigpio library for GPIO access and rpiplc library for PLC specific functionality. The main purpose of the program is to toggle the state of an output pin (Q0.0) whenever an interrupt is triggered by changes on the input pin (INT_GPIO).
#include <iostream>
#include <pigpio.h>
#include <rpiplc.h>
#define INT_GPIO 13
int led_status = 0;
void int_activated_callback(int gpio, int level, uint32_t tick) {
std::cout << "INT activated" << std::endl;
digitalWrite(Q0_0, led_status);
led_status = !led_status;
}
int main() {
// Inits
initPins();
gpioInitialise();
// Configure pins
gpioSetMode(INT_GPIO, PI_INPUT);
pinMode(Q0_0, OUTPUT);
// Create interrupt
gpioSetAlertFunc(INT_GPIO, &int_activated_callback);
while (1) {
std::cout << "Working " << std::endl;
delay(1000);
}
gpioTerminate();
return 0;
}
Save the code and compile it using the following command:
g++ -o test_int test_int.cpp -L /usr/local/lib -l rpiplc -lpigpio -lrt -lpthread -I /usr/local/include/rpiplc -D MODEL
Note that you will need to write the model of your Raspberry PLC on MODEL. The available models are: RPIPLC, RPIPLC_19R, RPIPLC_21, RPIPLC_38AR, RPIPLC_38R, RPIPLC_42, RPIPLC_50RRA, RPIPLC_53ARR, RPIPLC_54ARA, RPIPLC_57AAR, RPIPLC_57R, RPIPLC_58.
Finally execute the code:
./test_int
To test the program you can connect and disconnect the chosen interrupt input using the 5V and GND pins of the PLC. You will see the digital output Q0.0 switch along with some messages via the serial port.
How to program Raspberry industrial PLC interrupt inputs with C++