Giter VIP home page Giter VIP logo

ros2_pyads's Introduction

ros2_pyads

This is a ros2 package that will be a wrapper around the pyads library to have communication between ros2 and beckhoff PLCs.

Dependencies

Please view the requirements.txt file for the python dependencies.

pyads is the main dependency and can be installed with pip:

This is a ros2 python package, so ROS2 Humble will also need to be installed:

Installation

Open a terminal and navigate to the src directory of your ros2 workspace.

cd /opt/ros2_ws/

Clone the repository into the src directory.

git clone https://github.com/jodle001/ros2_pyads.git src/ros2_pyads

Install the dependencies with pip.

pip3 install -r src/ros2_pyads/requirements.txt

Build the package.

colcon build --packages-select ros2_pyads

Configuration

Communications:

Read the com_config.yaml file in the config directory carefully and set up the inputs, here is an example:

# This configuration file is used to set up the pyads library objects to establish communication between
# a machine with ROS2 and a machine running the TwinCat PLC Software.

# The IP address should match the machine with ROS2 with the
# added '.1.1' on the end of the IP address.
sender_ams: '192.168.33.10.1.1'
# This is the IP address of the host machine with the TwinCat Software
plc_ip: '192.168.33.4'
# This is arbitrarily named
route_name: 'ROS2PLC'
# This should match the plc_ip address
host_name: '192.168.33.4'
# This is gotten from the TwinCat System About menu "AMS Net id"
remote_ads: '10.100.101.14.1.1'

Administrator Access:

To have the pyads library to successfully communicate with the Beckhoff Software, it needs to have administrator access. This can be done by creating a local user with admin access.

Create and fill out a yaml for PLC Admin Access

  • Create a yaml called plc_admin.yaml in the config directory
  • This file is not tracked because it contains sensitive information, so it will not be pushed to the repository.
  • Fill out the yaml with the following, replacing the values with your own:
plc_admin_user: 'youreadminuser'
plc_admin_pass: 'adminpassword'

Usage

Running the Bool Test

This is a simple test to see if the communication is working. It will read a bool from the PLC and then write the opposite value back to the PLC.

  1. For this to work, add a boolean variable in the MAIN program of the PLC with the name bTest:

    PROGRAM MAIN
    VAR
        bTest : BOOL;
    END_VAR
  2. Then build the solution and run the PLC with the TwinCat software, and login to the PLC to view the variable which should start as false.

    You should see the following:

    bool_false

  3. After the PLC is running, open a terminal and source the ros2 workspace, and launch the ads_com_bool_test.launch.py launch file.

    source /opt/ros2_ws/install/setup.bash
    ros2 launch ros2_pyads ads_com_bool_test.launch.py
  4. The terminal should output the following:

    username:~$ ros2 launch ros2_pyads ads_com_bool_test.launch.py 
    [INFO] [launch]: All log files can be found below /home/username/.ros/log/2023-10-18-16-14-47-618657-OROC-LINUX01-3370411
    [INFO] [launch]: Default logging verbosity is set to INFO
    [INFO] [ads_com_bool_test_node-1]: process started with pid [3370412]
    [ads_com_bool_test_node-1] 2023-10-18T16:14:47-0400 Info: Connected to 192.168.33.4
    [ads_com_bool_test_node-1] 2023-10-18T16:14:47-0400 Info: connection closed by remote
    [ads_com_bool_test_node-1] 2023-10-18T16:14:47-0400 Info: Connected to 192.168.33.4
    [ads_com_bool_test_node-1] 2023-10-18T16:14:47-0400 Info: connection closed by remote
    [INFO] [ads_com_bool_test_node-1]: process has finished cleanly [pid 3370412]

    And the boolean in the Beckhoff PLC should toggle each time you run this:

    bool_true

Using Services

In order to call the services the ads_com_node needs to be running:

ros2 launch ros2_pyads ads_com.launch.py

Once the node is running, you can call the services with the following commands:

  • Reading a bool:
     ros2 service call /ads_com_node/read_bool ros2_pyads_interfaces/srv/ReadBool "{tag_name: 'MAIN.bTest'}"
  • Writing a bool:
    ros2 service call /ads_com_node/write_bool ros2_pyads_interfaces/srv/WriteBool "{tag_name: 'MAIN.bTest', tag_value: true}"

Other service will work the same way to view them just run the following command:

ros2 service list | grep -i "ads_com_node"

ros2_pyads's People

Contributors

jodle001 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ros2_pyads's Issues

ADSCom' object has no attribute 'plc

I've tried to download ros2_pyads and to install it. Everything went smoothly, however when I try to launch

ros2 launch ros2_pyads ads_com_bool_test.launch.py

I get the following error:

[INFO] [launch]: All log files can be found below C:\Users\dinterra.ros\log\2024-06-12-19-03-33-196428-DIN-IRMA-LT01-25096
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [ads_com_bool_test_node.EXE-1]: process started with pid [10948]
[ads_com_bool_test_node.EXE-1] Traceback (most recent call last):
[ads_com_bool_test_node.EXE-1] File "C:\dev\ros2_ws\install\lib\ros2_pyads\ads_com_bool_test_node-script.py", line 33, in
[ads_com_bool_test_node.EXE-1] sys.exit(load_entry_point('ros2-pyads==0.0.0', 'console_scripts', 'ads_com_bool_test_node')())
[ads_com_bool_test_node.EXE-1] File "C:\dev\ros2_ws\install\Lib\site-packages\ros2_pyads\ads_com_bool_test_node.py", line 53, in main
[ads_com_bool_test_node.EXE-1] ADSComBoolTestNode()
[ads_com_bool_test_node.EXE-1] File "C:\dev\ros2_ws\install\Lib\site-packages\ros2_pyads\ads_com_bool_test_node.py", line 44, in init
[ads_com_bool_test_node.EXE-1] var_value=not self.ads_com.read_by_name('MAIN.bTest', pyads.PLCTYPE_BOOL), # Toggle the existing value
[ads_com_bool_test_node.EXE-1] File "C:\dev\ros2_ws\install\Lib\site-packages\ros2_pyads\ads_com.py", line 68, in read_by_name
[ads_com_bool_test_node.EXE-1] raise e
[ads_com_bool_test_node.EXE-1] File "C:\dev\ros2_ws\install\Lib\site-packages\ros2_pyads\ads_com.py", line 64, in read_by_name
[ads_com_bool_test_node.EXE-1] var = self.plc.read_by_name(var_name, var_type)
[ads_com_bool_test_node.EXE-1] AttributeError: 'ADSCom' object has no attribute 'plc'
[ERROR] [ads_com_bool_test_node.EXE-1]: process has died [pid 10948, exit code 1, cmd 'C:\dev\ros2_ws\install\lib\ros2_pyads\ads_com_bool_test_node.EXE --ros-args --params-file C:\Users\dinterra\AppData\Local\Temp\launch_params_zazljcay --params-file C:\Users\dinterra\AppData\Local\Temp\launch_params_5wyx8mi_'].

The comconfig file was changed with these values:

'sender_ams': '192.168.12.253.1.1',
'plc_ip': '192.168.12.253',
'route_name': 'ROS2PLC',
'host_name': '192.168.12.253',
'remote_ads': '192.168.12.253.1.1'

ReadByteArray.srv is not built

Complete set(srv_files "srv/ReadBool.srv" "srv/WriteBool.srv" "srv/ReadString.srv" **"srv/ReadByteArray.srv"** ) in https://github.com/jodle001/ros2_pyads/blob/main/ros2_pyads_interfaces/CMakeLists.txt for successful generation of custom srv message

This function in ads_com "write_by_name" should return a boolean value.

Here is the function in question. which doesn't return any boolean value.

def write_by_name(self, var_name, var_value, var_type):
"""
Write a variable to the PLC by name.
:param var_name: The name of the variable to write (e.g., 'MAIN.testVar')
:param var_value: The value of the variable to write (e.g., 5)
:param var_type: The type of the variable to write (e.g., pyads.PLCTYPE_DWORD)
"""
with pyads.Connection(self.remote_ads, pyads.PORT_TC3PLC1, self.plc_ip) as plc:
plc.write_by_name(var_name, var_value, var_type)

Here is where it is returning false in every case:

success = self.ads_com.write_by_name(
var_name='MAIN.bTest',
var_value=not self.ads_com.read_by_name('MAIN.bTest', pyads.PLCTYPE_BOOL), # Toggle the existing value
var_type=pyads.PLCTYPE_BOOL)
if not success:
self.get_logger().error('Failed to write variable to PLC')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.