Skip to content

Commit 3d68bdc

Browse files
committed
Add example_control_loop.py
1 parent 2fa8991 commit 3d68bdc

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

examples/example_control_loop.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env python
2+
# Copyright (c) 2016, Universal Robots A/S,
3+
# All rights reserved.
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
# * Redistributions of source code must retain the above copyright
7+
# notice, this list of conditions and the following disclaimer.
8+
# * Redistributions in binary form must reproduce the above copyright
9+
# notice, this list of conditions and the following disclaimer in the
10+
# documentation and/or other materials provided with the distribution.
11+
# * Neither the name of the Universal Robots A/S nor the names of its
12+
# contributors may be used to endorse or promote products derived
13+
# from this software without specific prior written permission.
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
# DISCLAIMED. IN NO EVENT SHALL UNIVERSAL ROBOTS A/S BE LIABLE FOR ANY
18+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
25+
import sys
26+
sys.path.append('..')
27+
import logging
28+
29+
import rtde.rtde as rtde
30+
import rtde.rtde_config as rtde_config
31+
32+
33+
#logging.basicConfig(level=logging.INFO)
34+
35+
ROBOT_HOST = 'localhost'
36+
ROBOT_PORT = 30004
37+
config_filename = 'control_loop_configuration.xml'
38+
39+
keep_running = True
40+
41+
logging.getLogger().setLevel(logging.INFO)
42+
43+
conf = rtde_config.ConfigFile(config_filename)
44+
state_names, state_types = conf.get_recipe('state')
45+
setp_names, setp_types = conf.get_recipe('setp')
46+
watchdog_names, watchdog_types = conf.get_recipe('watchdog')
47+
48+
con = rtde.RTDE(ROBOT_HOST, ROBOT_PORT)
49+
con.connect()
50+
51+
# get controller version
52+
con.get_controller_version()
53+
54+
# setup recipes
55+
con.send_output_setup(state_names, state_types)
56+
setp = con.send_input_setup(setp_names, setp_types)
57+
watchdog = con.send_input_setup(watchdog_names, watchdog_types)
58+
59+
# Setpoints to move the robot to
60+
setp1 = [-0.12, -0.43, 0.14, 0, 3.11, 0.04]
61+
setp2 = [-0.12, -0.51, 0.21, 0, 3.11, 0.04]
62+
63+
setp.input_double_register_0 = 0
64+
setp.input_double_register_1 = 0
65+
setp.input_double_register_2 = 0
66+
setp.input_double_register_3 = 0
67+
setp.input_double_register_4 = 0
68+
setp.input_double_register_5 = 0
69+
70+
# The function "rtde_set_watchdog" in the "rtde_control_loop.urp" creates a 1 Hz watchdog
71+
watchdog.input_int_register_0 = 0
72+
73+
def setp_to_list(setp):
74+
list = []
75+
for i in range(0,6):
76+
list.append(setp.__dict__["input_double_register_%i" % i])
77+
return list
78+
79+
def list_to_setp(setp, list):
80+
for i in range (0,6):
81+
setp.__dict__["input_double_register_%i" % i] = list[i]
82+
return setp
83+
84+
#start data synchronization
85+
if not con.send_start():
86+
sys.exit()
87+
88+
# control loop
89+
while keep_running:
90+
# receive the current state
91+
state = con.receive()
92+
93+
if state is None:
94+
break;
95+
96+
# do something...
97+
if state.output_int_register_0 != 0:
98+
new_setp = setp1 if setp_to_list(setp) == setp2 else setp2
99+
list_to_setp(setp, new_setp)
100+
# send new setpoint
101+
con.send(setp)
102+
103+
# kick watchdog
104+
con.send(watchdog)
105+
106+
con.send_pause()
107+
108+
con.disconnect()

0 commit comments

Comments
 (0)