-
Notifications
You must be signed in to change notification settings - Fork 30
/
Control.py
202 lines (160 loc) · 5.57 KB
/
Control.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
import rospy
from time import time
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from gazebo_msgs.msg import ModelState
from math import *
import numpy as np
from tf.transformations import euler_from_quaternion, quaternion_from_euler
# Q-learning speed parameters
CONST_LINEAR_SPEED_FORWARD = 0.08
CONST_ANGULAR_SPEED_FORWARD = 0.0
CONST_LINEAR_SPEED_TURN = 0.06
CONST_ANGULAR_SPEED_TURN = 0.4
# Feedback control parameters
K_RO = 2
K_ALPHA = 15
K_BETA = -3
V_CONST = 0.1 # [m/s]
# Goal reaching threshold
GOAL_DIST_THRESHOLD = 0.1 # [m]
GOAL_ANGLE_THRESHOLD = 15 # [degrees]
# Get theta in [radians]
def getRotation(odomMsg):
orientation_q = odomMsg.pose.pose.orientation
orientation_list = [ orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
(roll, pitch, yaw) = euler_from_quaternion(orientation_list)
return yaw
# Get (x,y) coordinates in [m]
def getPosition(odomMsg):
x = odomMsg.pose.pose.position.x
y = odomMsg.pose.pose.position.y
return ( x , y)
# Get linear speed in [m/s]
def getLinVel(odomMsg):
return odomMsg.twist.twist.linear.x
# Get angular speed in [rad/s] - z axis
def getAngVel(odomMsg):
return odomMsg.twist.twist.angular.z
# Create rosmsg Twist()
def createVelMsg(v,w):
velMsg = Twist()
velMsg.linear.x = v
velMsg.linear.y = 0
velMsg.linear.z = 0
velMsg.angular.x = 0
velMsg.angular.y = 0
velMsg.angular.z = w
return velMsg
# Go forward command
def robotGoForward(velPub):
velMsg = createVelMsg(CONST_LINEAR_SPEED_FORWARD,CONST_ANGULAR_SPEED_FORWARD)
velPub.publish(velMsg)
# Turn left command
def robotTurnLeft(velPub):
velMsg = createVelMsg(CONST_LINEAR_SPEED_TURN,+CONST_ANGULAR_SPEED_TURN)
velPub.publish(velMsg)
# Turn right command
def robotTurnRight(velPub):
velMsg = createVelMsg(CONST_LINEAR_SPEED_TURN,-CONST_ANGULAR_SPEED_TURN)
velPub.publish(velMsg)
# Stop command
def robotStop(velPub):
velMsg = createVelMsg(0.0,0.0)
velPub.publish(velMsg)
# Set robot position and orientation
def robotSetPos(setPosPub, x, y, theta):
checkpoint = ModelState()
checkpoint.model_name = 'turtlebot3_burger'
checkpoint.pose.position.x = x
checkpoint.pose.position.y = y
checkpoint.pose.position.z = 0.0
[x_q,y_q,z_q,w_q] = quaternion_from_euler(0.0,0.0,radians(theta))
checkpoint.pose.orientation.x = x_q
checkpoint.pose.orientation.y = y_q
checkpoint.pose.orientation.z = z_q
checkpoint.pose.orientation.w = w_q
checkpoint.twist.linear.x = 0.0
checkpoint.twist.linear.y = 0.0
checkpoint.twist.linear.z = 0.0
checkpoint.twist.angular.x = 0.0
checkpoint.twist.angular.y = 0.0
checkpoint.twist.angular.z = 0.0
setPosPub.publish(checkpoint)
return ( x , y , theta )
# Set random initial robot position and orientation
def robotSetRandomPos(setPosPub):
x_range = np.array([-0.4, 0.6, 0.6, -1.4, -1.4, 2.0, 2.0, -2.5, 1.0, -1.0])
y_range = np.array([-0.4, 0.6, -1.4, 0.6, -1.4, 1.0, -1.0, 0.0, 2.0, 2.0])
theta_range = np.arange(0, 360, 15)
#theta_range = np.array([0, 30, 45, 60, 75, 90])
ind = np.random.randint(0,len(x_range))
ind_theta = np.random.randint(0,len(theta_range))
x = x_range[ind]
y = y_range[ind]
theta = theta_range[ind_theta]
checkpoint = ModelState()
checkpoint.model_name = 'turtlebot3_burger'
checkpoint.pose.position.x = x
checkpoint.pose.position.y = y
checkpoint.pose.position.z = 0.0
[x_q,y_q,z_q,w_q] = quaternion_from_euler(0.0,0.0,radians(theta))
checkpoint.pose.orientation.x = x_q
checkpoint.pose.orientation.y = y_q
checkpoint.pose.orientation.z = z_q
checkpoint.pose.orientation.w = w_q
checkpoint.twist.linear.x = 0.0
checkpoint.twist.linear.y = 0.0
checkpoint.twist.linear.z = 0.0
checkpoint.twist.angular.x = 0.0
checkpoint.twist.angular.y = 0.0
checkpoint.twist.angular.z = 0.0
setPosPub.publish(checkpoint)
return ( x , y , theta )
# Perform an action
def robotDoAction(velPub, action):
status = 'robotDoAction => OK'
if action == 0:
robotGoForward(velPub)
elif action == 1:
robotTurnLeft(velPub)
elif action == 2:
robotTurnRight(velPub)
else:
status = 'robotDoAction => INVALID ACTION'
robotGoForward(velPub)
return status
# Feedback Control Algorithm
def robotFeedbackControl(velPub, x, y, theta, x_goal, y_goal, theta_goal):
# theta goal normalization
if theta_goal >= pi:
theta_goal_norm = theta_goal - 2 * pi
else:
theta_goal_norm = theta_goal
ro = sqrt( pow( ( x_goal - x ) , 2 ) + pow( ( y_goal - y ) , 2) )
lamda = atan2( y_goal - y , x_goal - x )
alpha = (lamda - theta + pi) % (2 * pi) - pi
beta = (theta_goal - lamda + pi) % (2 * pi) - pi
if ro < GOAL_DIST_THRESHOLD and degrees(abs(theta-theta_goal_norm)) < GOAL_ANGLE_THRESHOLD:
status = 'Goal position reached!'
v = 0
w = 0
v_scal = 0
w_scal = 0
else:
status = 'Goal position not reached!'
v = K_RO * ro
w = K_ALPHA * alpha + K_BETA * beta
v_scal = v / abs(v) * V_CONST
w_scal = w / abs(v) * V_CONST
velMsg = createVelMsg(v_scal, w_scal)
velPub.publish(velMsg)
return status
# Stability Condition
def check_stability(k_rho, k_alpha, k_beta):
return k_rho > 0 and k_beta < 0 and k_alpha > k_rho
# Strong Stability Condition
def check_strong_stability(k_rho, k_alpha, k_beta):
return k_rho > 0 and k_beta < 0 and k_alpha + 5 * k_beta / 3 - 2 * k_rho / np.pi > 0