-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotfields_takeshi.py
165 lines (125 loc) · 4.38 KB
/
potfields_takeshi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
5#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 17 22:54:18 2019
@author: oscar
"""
import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist , PointStamped
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion
from sensor_msgs.msg import LaserScan
import numpy as np
def normalize(x,y):
xn= x/np.linalg.norm((x,y))
yn= y/np.linalg.norm((x,y))
return ((xn,yn))
def newOdom (msg):
global x
global y
global th
x=msg.pose.pose.position.x
y=msg.pose.pose.position.y
quaternion = (
msg.pose.pose.orientation.x,
msg.pose.pose.orientation.y,
msg.pose.pose.orientation.z,
msg.pose.pose.orientation.w)
euler = euler_from_quaternion(quaternion)
th=euler[2]
def readPoint(punto):
global xcl
global ycl
xcl =punto.point.x
ycl =punto.point.y
def readSensor(data):
lec=np.asarray(data.ranges)
lec[np.isinf(lec)]=13.5
Fx, Fy,Fth = 0.001,0.001,0
deltaang=4.7124/len(data.ranges)
laserdegs= np.arange(-2.3562,2.3562,deltaang)
Fx=0
Fy = 0.001
for i,deg in enumerate(laserdegs):
if (lec[i] <2.61):
Fx = Fx + (1/lec[i])**2 * np.cos(deg)
Fy = Fy + (1/lec[i])**2 * np.sin(deg)
Fth= np.arctan2(Fy,(Fx+.000000000001))+np.pi
print('FxFyFth',Fx,Fy,Fth*180/np.pi)
Fmag= np.linalg.norm((Fx,Fy))
xy,xycl=np.array((x,y)) , np.array((xcl,ycl))
euclD=np.linalg.norm(xy-xycl)
#print("xrob,yrob, throbot",x,y,th*180/3.1416)
print("xclick,yclick",xcl,ycl,"euclD",euclD)
Fatrx =( -x + xcl)/euclD
Fatry =( -y + ycl)/euclD
Fatrth=np.arctan2(Fatry, Fatrx)
Fatrth=Fatrth-th
Fmagat= np.linalg.norm((Fatrx,Fatry))
print ('Fatx, Fatry, Fatrth',Fatrx,Fatry,(Fatrth)*180/np.pi )
Ftotx= Fmag*np.cos(Fth)*.0051 + Fmagat*np.cos(Fatrth)
Ftoty= Fmag*np.sin(Fth)*.0051 + Fmagat*np.sin(Fatrth)
Ftotth=np.arctan2(Ftoty,Ftotx)
if ( Ftotth> np.pi ):
Ftotth= -np.pi- (Ftotth-np.pi)
if (Ftotth < -np.pi):
Ftotth= (Ftotth +2 *np.pi)
print('Ftotxy',Ftotx,Ftoty,Ftotth*180/np.pi)
"""Fatmag=np.linalg.norm((Fatrx,Fatry))
Fmag=np.linalg.norm((Fx,Fy))
print ("theta robot",th*180/3.1416,'---------------------------')
print ('fasorFatrth',np.linalg.norm((Fatrx,Fatry)),(Fatrth)*180/3.1416 )
print ("FXATR,FYATR",Fatrx,Fatry)
print ('fasorFrepth',np.linalg.norm((Fx,Fy)),Fth*180/3.1416)
print ("Frepx,Frepy",Fx,Fy)"""
"""Fx,Fy= Fmag*np.cos(Fth) , Fmag*np.sin(Fth)
Fatrx,Fatry= Fatmag*np.cos(Fatrth) , Fatmag*np.sin(Fatrth) """
vel=0
if( abs(Ftotth) < .23) :#or (np.linalg.norm((Fx,Fy)) < 100):
speed.linear.x=1.9
print('lin')
speed.angular.z=0
else:
if Ftotth < 0:
if (abs( Ftotth ) < np.pi/2):
vel=.24
print('open curve')
print('Vang-')
speed.linear.x=vel
speed.angular.z=-0.55
if Ftotth > 0:
if (abs( Ftotth ) < np.pi/2):
vel=.24
print('open curve')
print('Vang+')
speed.linear.x=vel
speed.angular.z=.5
"""if Ftotth < -1.57:
print('ang++')
speed.linear.x=0
speed.angular.z=0.5
if Ftotth > 1.57:
print('ang-')
speed.linear.x=0
speed.angular.z=-0.5"""
speed=Twist()
speed.angular.z=0
def inoutinout():
sub= rospy.Subscriber("/hsrb/odom",Odometry,newOdom)
sub2=rospy.Subscriber("/hsrb/base_scan",LaserScan,readSensor)
sub3=rospy.Subscriber("/clicked_point",PointStamped,readPoint)
pub = rospy.Publisher('/hsrb/command_velocity', Twist, queue_size=1)
rospy.init_node('talker_cmdvel', anonymous=True)
rate = rospy.Rate(15) # 10hz
while not rospy.is_shutdown():
hello_str = "Time %s" % rospy.get_time()
#print("odom",x,y,th)
#
pub.publish(speed)
rate.sleep()
if __name__ == '__main__':
try:
inoutinout()
except rospy.ROSInterruptException:
pass