Skip to content

Commit a9adec3

Browse files
author
yuth
committed
update
1 parent 906a00e commit a9adec3

20 files changed

+960
-25
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# Labbing Python
1+
# Lab Python
2+
This Package is create to collect some of my python code that I have learnt. [For Mom!]
3+
4+
### Reference [List](./doc/)
5+
There are detail of the package, read more in the references list link above.

algorithms/insertion_sort.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
def insertion_sort_increase(A):
2-
for j in range(1,len(A)):
2+
for j in range(1, len(A)):
33
key = A[j]
44
# insert A[j] into the sorted seq A[1 ... j-1]
5-
i = j-1
6-
while i >= 0 and A[i]>key: # important for increase ( if i+1 is smaller than i = swap it)
7-
A[i+1] = A[i]
5+
i = j - 1
6+
while i >= 0 and A[i] > key: # important for increase ( if i+1 is smaller than i = swap it)
7+
A[i + 1] = A[i]
88
i = i - 1
9-
A[i+1] = key
9+
A[i + 1] = key
1010

1111
return A
1212

13+
1314
def insertion_sort_deincrease(A):
14-
for j in range(1,len(A)):
15+
for j in range(1, len(A)):
1516
key = A[j]
1617
# insert A[j] into the sorted seq A[1 ... j-1]
17-
i = j-1
18-
while i >= 0 and A[i]<key: # important for increase ( if i+1 is bigger than i = swap it)
19-
A[i+1] = A[i]
18+
i = j - 1
19+
while i >= 0 and A[i] < key: # important for increase ( if i+1 is bigger than i = swap it)
20+
A[i + 1] = A[i]
2021
i = i - 1
21-
A[i+1] = key
22+
A[i + 1] = key
2223

2324
return A
2425

2526

26-
A = [5,2,4,6,1,3]
27-
A = [31,41,59,26,41,58]
27+
A = [5, 2, 4, 6, 1, 3]
28+
A = [31, 41, 59, 26, 41, 58]
2829
print(A)
2930
result_increase = insertion_sort_increase(A)
3031
print(result_increase)
3132

3233
result_deincrease = insertion_sort_deincrease(A)
33-
print(result_deincrease)
34+
print(result_deincrease)

algorithms/linear_search.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
def linear_search(A,v):
1+
def linear_search(A, v):
22
for j in range(len(A)):
33
if A[j] == v:
44
print("Found the number at: index")
55

6-
return j # index of found number
6+
return j # index of found number
77

88

9-
A = [5,2,4,6,1,3]
9+
A = [5, 2, 4, 6, 1, 3]
1010

1111
v = 6
1212
print(A)
13-
result = linear_search(A,v)
14-
print(result)
13+
result = linear_search(A, v)
14+
print(result)

doc/cover.png

-61.3 KB
Binary file not shown.

hardware/urrtde/forcemode_example.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
3+
rtde_c = RTDEControl("127.0.0.1")
4+
5+
task_frame = [0, 0, 0, 0, 0, 0]
6+
selection_vector = [0, 0, 1, 0, 0, 0]
7+
wrench_down = [0, 0, -10, 0, 0, 0]
8+
wrench_up = [0, 0, 10, 0, 0, 0]
9+
force_type = 2
10+
limits = [2, 2, 1.5, 1, 1, 1]
11+
joint_q = [-1.54, -1.83, -2.28, -0.59, 1.60, 0.023]
12+
13+
# Move to initial joint position with a regular moveJ
14+
rtde_c.moveJ(joint_q)
15+
16+
# Execute 500Hz control loop for 4 seconds, each cycle is 2ms
17+
for i in range(2000):
18+
t_start = rtde_c.initPeriod()
19+
# First move the robot down for 2 seconds, then up for 2 seconds
20+
if i > 1000:
21+
rtde_c.forceMode(task_frame, selection_vector, wrench_up, force_type, limits)
22+
else:
23+
rtde_c.forceMode(task_frame, selection_vector, wrench_down, force_type, limits)
24+
rtde_c.waitPeriod(t_start)
25+
26+
rtde_c.forceModeStop()
27+
rtde_c.stopScript()

hardware/urrtde/io_example.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from rtde_io import RTDEIOInterface as RTDEIO
2+
from rtde_receive import RTDEReceiveInterface as RTDEReceive
3+
import time
4+
5+
rtde_io_ = RTDEIO("127.0.0.1")
6+
rtde_receive_ = RTDEReceive("127.0.0.1")
7+
8+
# How-to set and get standard and tool digital outputs. Notice that we need the
9+
# RTDEIOInterface for setting an output and RTDEReceiveInterface for getting the state
10+
# of an output.
11+
12+
if rtde_receive_.getDigitalOutState(7):
13+
print("Standard digital out (7) is HIGH")
14+
else:
15+
print("Standard digital out (7) is LOW")
16+
17+
if rtde_receive_.getDigitalOutState(16):
18+
print("Tool digital out (16) is HIGH")
19+
else:
20+
print("Tool digital out (16) is LOW")
21+
22+
rtde_io_.setStandardDigitalOut(7, True)
23+
rtde_io_.setToolDigitalOut(0, True)
24+
time.sleep(0.01)
25+
26+
if rtde_receive_.getDigitalOutState(7):
27+
print("Standard digital out (7) is HIGH")
28+
else:
29+
print("Standard digital out (7) is LOW")
30+
31+
if rtde_receive_.getDigitalOutState(16):
32+
print("Tool digital out (16) is HIGH")
33+
else:
34+
print("Tool digital out (16) is LOW")
35+
36+
# How to set a analog output with a specified current ratio
37+
rtde_io_.setAnalogOutputCurrent(1, 0.25)

hardware/urrtde/joint_ordered.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import rtde_control
3+
import rtde_receive
4+
5+
6+
rtde_c = rtde_control.RTDEControlInterface("192.168.0.3")
7+
rtde_r = rtde_receive.RTDEReceiveInterface("192.168.0.3")
8+
9+
# from techpendant
10+
# -10.0 -80.0 -97.0 -30.0 9.0 0.0
11+
12+
# it seem ros joint is not start in order
13+
# elbow_joint, shoulder_lift_joint, shoulder_pan_joint, wrist_1_joint, wrist_2_joint, wrist_3_joint
14+
rosjointvalue = np.rad2deg([-1.692978858947754, -1.3962847751430054, -0.17454559007753545, -0.5235684675029297, 0.15709757804870605, -3.987947572881012e-05])
15+
16+
rtdejointvalue = np.rad2deg(rtde_r.getActualQ())
17+
18+
# > rosjointvalue: [-9.70005434e+01 -8.00012246e+01 -1.00007256e+01 -2.99982635e+01 9.00102819e+00 -2.28492565e-03]
19+
# > rtdejointvalue: [-9.99796625e+00 -8.00012246e+01 -9.70033028e+01 -3.00010229e+01 9.00034518e+00 -8.91567058e-04]

hardware/urrtde/move_async_example.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
from rtde_receive import RTDEReceiveInterface as RTDEReceive
3+
import time
4+
5+
rtde_c = RTDEControl("127.0.0.1")
6+
rtde_r = RTDEReceive("127.0.0.1")
7+
init_q = rtde_r.getActualQ()
8+
9+
# Target in the robot base
10+
new_q = init_q[:]
11+
new_q[0] += 0.20
12+
13+
# Move asynchronously in joint space to new_q, we specify asynchronous behavior by setting the async parameter to
14+
# 'True'. Try to set the async parameter to 'False' to observe a default synchronous movement, which cannot be stopped
15+
# by the stopJ function due to the blocking behaviour.
16+
rtde_c.moveJ(new_q, 1.05, 1.4, True)
17+
time.sleep(0.2)
18+
# Stop the movement before it reaches new_q
19+
rtde_c.stopJ(0.5)
20+
21+
# Target in the Z-Axis of the TCP
22+
target = rtde_r.getActualTCPPose()
23+
target[2] += 0.10
24+
25+
# Move asynchronously in cartesian space to target, we specify asynchronous behavior by setting the async parameter to
26+
# 'True'. Try to set the async parameter to 'False' to observe a default synchronous movement, which cannot be stopped
27+
# by the stopL function due to the blocking behaviour.
28+
rtde_c.moveL(target, 0.25, 0.5, True)
29+
time.sleep(0.2)
30+
# Stop the movement before it reaches target
31+
rtde_c.stopL(0.5)
32+
33+
# Move back to initial joint configuration
34+
rtde_c.moveJ(init_q)
35+
36+
# Stop the RTDE control script
37+
rtde_c.stopScript()
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
from rtde_control import Path, PathEntry
3+
import time
4+
5+
rtde_c = RTDEControl("localhost")
6+
7+
path = Path()
8+
vel = 0.5
9+
acc = 4.0
10+
blend = 0.099
11+
path.addEntry(PathEntry(PathEntry.MoveJ, PathEntry.PositionTcpPose, [-0.140, -0.400, 0.100, 0, 3.14, 0, vel, acc, 0.0]))
12+
path.addEntry(PathEntry(PathEntry.MoveL, PathEntry.PositionTcpPose, [-0.140, -0.400, 0.300, 0, 3.14, 0, vel, acc, blend]))
13+
path.addEntry(PathEntry(PathEntry.MoveL, PathEntry.PositionTcpPose, [-0.140, -0.600, 0.300, 0, 3.14, 0, vel, acc, blend]))
14+
path.addEntry(PathEntry(PathEntry.MoveJ, PathEntry.PositionTcpPose, [-0.140, -0.600, 0.100, 0, 3.14, 0, vel, acc, blend]))
15+
path.addEntry(PathEntry(PathEntry.MoveJ, PathEntry.PositionTcpPose, [-0.140, -0.400, 0.100, 0, 3.14, 0, vel, acc, 0.0]))
16+
17+
# First move given path synchronously
18+
print("Move path synchronously...")
19+
rtde_c.movePath(path, False)
20+
print("Path finished!")
21+
22+
# Now move given path asynchronously
23+
print("Move path asynchronously with progress feedback...")
24+
rtde_c.movePath(path, True)
25+
26+
# Wait for start of asynchronous operation
27+
while not rtde_c.getAsyncOperationProgressEx().isAsyncOperationRunning():
28+
time.sleep(0.010)
29+
print("Async path started.. ")
30+
31+
# Wait for end of asynchronous operation
32+
waypoint = -1
33+
while rtde_c.getAsyncOperationProgressEx().isAsyncOperationRunning():
34+
time.sleep(0.2)
35+
new_waypoint = rtde_c.getAsyncOperationProgress()
36+
if new_waypoint != waypoint:
37+
waypoint = new_waypoint
38+
print("Moving to path waypoint ")
39+
40+
print("Async path finished...\n\n")
41+
rtde_c.stopScript()
42+

hardware/urrtde/move_until_contact.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
3+
rtde_c = RTDEControl("127.0.0.1")
4+
speed = [0, 0, -0.100, 0, 0, 0]
5+
rtde_c.moveUntilContact(speed)
6+
7+
rtde_c.stopScript()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
3+
rtde_c = RTDEControl("127.0.0.1")
4+
5+
velocity = 0.5
6+
acceleration = 0.5
7+
blend_1 = 0.0
8+
blend_2 = 0.02
9+
blend_3 = 0.0
10+
path_pose1 = [-0.143, -0.435, 0.20, -0.001, 3.12, 0.04, velocity, acceleration, blend_1]
11+
path_pose2 = [-0.143, -0.51, 0.21, -0.001, 3.12, 0.04, velocity, acceleration, blend_2]
12+
path_pose3 = [-0.32, -0.61, 0.31, -0.001, 3.12, 0.04, velocity, acceleration, blend_3]
13+
path = [path_pose1, path_pose2, path_pose3]
14+
15+
# Send a linear path with blending in between - (currently uses separate script)
16+
rtde_c.moveL(path)
17+
rtde_c.stopScript()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from rtde_control import RTDEControlInterface as RTDEControl
2+
from rtde_receive import RTDEReceiveInterface as RTDEReceive
3+
import datetime
4+
import math
5+
import os
6+
import psutil
7+
import sys
8+
9+
10+
def getCircleTarget(pose, timestep, radius=0.075, freq=1.0):
11+
circ_target = pose[:]
12+
circ_target[0] = pose[0] + radius * math.cos((2 * math.pi * freq * timestep))
13+
circ_target[1] = pose[1] + radius * math.sin((2 * math.pi * freq * timestep))
14+
return circ_target
15+
16+
17+
# Parameters
18+
vel = 0.05
19+
acc = 0.05
20+
rtde_frequency = 500.0
21+
dt = 1.0/rtde_frequency # 2ms
22+
flags = RTDEControl.FLAG_VERBOSE | RTDEControl.FLAG_UPLOAD_SCRIPT
23+
ur_cap_port = 50002
24+
robot_ip = "192.168.0.3"
25+
26+
lookahead_time = 0.1
27+
gain = 600
28+
29+
# ur_rtde realtime priorities
30+
rt_receive_priority = 90
31+
rt_control_priority = 85
32+
33+
rtde_r = RTDEReceive(robot_ip, rtde_frequency, [], True, False, rt_receive_priority)
34+
rtde_c = RTDEControl(robot_ip, rtde_frequency, flags, ur_cap_port, rt_control_priority)
35+
36+
# Set application real-time priority
37+
os_used = sys.platform
38+
process = psutil.Process(os.getpid())
39+
if os_used == "win32": # Windows (either 32-bit or 64-bit)
40+
process.nice(psutil.REALTIME_PRIORITY_CLASS)
41+
elif os_used == "linux": # linux
42+
rt_app_priority = 80
43+
param = os.sched_param(rt_app_priority)
44+
try:
45+
os.sched_setscheduler(0, os.SCHED_FIFO, param)
46+
except OSError:
47+
print("Failed to set real-time process scheduler to %u, priority %u" % (os.SCHED_FIFO, rt_app_priority))
48+
else:
49+
print("Process real-time priority set to: %u" % rt_app_priority)
50+
51+
time_counter = 0.0
52+
53+
# Move to init position using moveL
54+
actual_tcp_pose = rtde_r.getActualTCPPose()
55+
init_pose = getCircleTarget(actual_tcp_pose, time_counter)
56+
rtde_c.moveL(init_pose, vel, acc)
57+
58+
try:
59+
while True:
60+
t_start = rtde_c.initPeriod()
61+
servo_target = getCircleTarget(actual_tcp_pose, time_counter)
62+
rtde_c.servoL(servo_target, vel, acc, dt, lookahead_time, gain)
63+
rtde_c.waitPeriod(t_start)
64+
time_counter += dt
65+
66+
except KeyboardInterrupt:
67+
print("Control Interrupted!")
68+
rtde_c.servoStop()
69+
rtde_c.stopScript()

0 commit comments

Comments
 (0)