Controller development communication tutorial
Introduction
The development of a control system can be summarized as the interaction between the teach pendant and the controller. The teach pendant provides a graphical interface through which users can perform operations to send instructions. The controller, in turn, is responsible for processing these instructions, interacting with the robot servo, and providing feedback on interactions and processing results to the teach pendant, which are then displayed through the graphical interface. Therefore, the actual development work involves creating the graphical interactive interface for the teach pendant and implementing the logic functions for the controller.
· Write a program for the controller to print messages
Please refer to the environment setup tutorial in the basic tutorial to install the controller development environment. Download and extract the demo (you can choose the version you need according to the actual situation. The usage is the same, newer versions will have more interfaces, please ensure that the controller and teach pendant versions are consistent).
Use eclipse to import the demo project
Write a new function in demo.cpp
void SayHello(string str)
{
cout << "str = " << str << endl;
cout << "hello" << endl;
}
and write the following in the main function
NRC_SetSocketCustomProtocalCB(0x9200,&SayHello);
As shown in the figure below:
0x9200 represents the communication number used for sending data to the teach pendant. "SayHello" refers to the function that needs to be triggered. The format should be void xxx(string), where the string parameter is responsible for receiving the string sent out by the teach pendant, which in this example is "hello." Please refer to the specific function definition in the header file nrcAPI.h for more details.
Compile and run the controller program in the terminal, wait for the communication connection between the controller and the teach pendant to be established. Then, go back to the user interface of the teach pendant, click on the button we've created in the teach pendant communication tutorial. You should see "hello" successfully printed in the terminal. At this point, the communication is successful.
FAQ
Q:The controller and the teach pendant have been waiting for a long time to connect. A:Check whether the IP setting in the teach pendant is set to the IP of the controller. If it is a direct connection, make sure the IP address of the computer and the controller are in the same network segment. If the connection suddenly fails during repeated tests, consider the possibility of port blocking. In this case, restarting the controller and teach pendant programs should resolve the issue. Q:After the connection is successful, the teach pendant displays a version mismatch error. A:Make sure the controller and teach pendant versions downloaded from the official website are the same. Q:After the connection is successful, the teach pendant prompts that the controller configuration file does not match. A:Enter the teach pendant's "Settings/System setting/More setting" interface in sequence, click on "Restore factory settings", and then restart. Q:Unable to upload the newly compiled program to the controller or encountering "Text file busy" error. A:This is caused by the running nrc.out in the controller. Use sudo killall -9 nrc.out to terminate the existing program. In addition, please avoid running two nrc.out consecutively. It is best to develop the habit of executing the above instructions before each run
· The controller sends information to the teach pendant
In the previous section, we learned how to send messages from the teach pendant to the controller. In this section, we will send messages from the controller to the teach pendant, so as to complete a complete message passing loop. According to the order of sending-receiving, this time we start from the controller, follow the SayHello function in the previous section, and add an interface to this function
NRC_SendSocketCustomProtocal(0x9201,str);
As shown below
Similar to the interface for sending messages from the teach pendant in the first section, 0x9201 represents the protocol number, and str represents the string to be sent, which is the string type here, because str in the first section is actually the hello string sent by the teach pendant, so here we receive hello and return hello to the teach pendant.
Next, look at the receiving part of the teach pendant:
· The teach pendant receives messages from the controller
Next, look at the receiving part of the teach pendant. The receiving interface has been built in the demo, as shown in the figure below
As you can see, after we trigger the button in the controller development communication tutorial, the teach pendant not only sends the hello string to the controller via 0x9200, but also receives the hello string sent by the controller via 0x9201. Once you have mastered the message communication between the teach pendant and the controller, you can realize the interaction between them.
Small exercise
- When using only one protocol number to transmit multiple messages, one approach is to use a JSON-formatted string to carry the messages.
- Try controlling the robot movement through a custom-written teach pendant button. The development interface for robot movement is as follows.