-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffDriveControl.py
138 lines (103 loc) · 3.44 KB
/
DiffDriveControl.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
# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
import Transform as Transform
import DiffDriveRobot
class Wheel(object):
"""docstring for Wheel."""
def __init__(self):
super(Wheel, self).__init__()
self.speed = 0
def setSpeed(self, speed):
self.speed = speed
def getSpeed(self):
return self.speed
def getDist(self, dT):
return self.speed * dT
class Robot(object):
"""docstring for Robot."""
def __init__(self, x, y, theta, robot):
super(Robot, self).__init__()
self.polygon = np.array([[-150, -150], [-150, 150], [150, 150], [150, -150], [-150, -150]],dtype =float)
self.x = x
self.y = y
self.theta = theta
self.robot = robot
self.MEntreAxes = 200
self.OEntreAxes = 250
self.xC = x
self.yC = y
self.thetaC = theta
self.XErr = 0
self.YErr = 0
self.ThetaErr = 0
self.DistErr = 0
self.CapErr = 0
self.alpha = []
self.thetaa = []
self.DistErra = []
# mutateurs
def setX(self, x):
self.x = x
def setY(self, y):
self.y = y
def setTheta(self, theta):
self.theta = theta
def setXC(self, xC):
self.xC = xC
def setYC(self, yC):
self.yC = yC
def setThetaC(self, thetaC):
self.thetaC = thetaC
# asscenseurs
def getX(self):
return self.x
def getY(self):
return self.y
def getTheta(self):
return self.theta
#autres methodes
#fonctions traduisant le fonctionment du robot (modèle)
def updateOdometry(self, dT):
dOL = self.robot.getLeftEncoderDist(dT)
dOR = self.robot.getRightEncoderDist(dT)
dXrobot = (dOR + dOL)/2
dTheta = (dOR - dOL)/self.OEntreAxes
self.theta = self.theta + dTheta
if(self.theta <= -np.pi): self.theta = self.theta + 2*np.pi
if(self.theta > np.pi): self.theta = self.theta - 2*np.pi
self.x = self.x + dXrobot*np.cos(self.theta)
self.y = self.y + dXrobot*np.sin(self.theta)
def computeError(self): # Equations 11 & 12
self.XErr = self.xC - self.x
self.YErr = self.yC - self.y
self.ThetaErr = self.thetaC - self.theta #unused
Kp = 1
Kalpha = 5
alpha = np.arctan2(self.YErr, self.XErr)-self.theta
if alpha <= -np.pi: alpha+= 2*np.pi
if alpha > +np.pi: alpha-= 2*np.pi
self.thetaa.append(self.theta)
self.alpha.append(alpha)
self.DistErr = Kp*np.sqrt(self.XErr**2 + self.YErr**2)*np.cos(alpha)
# self.CapErr = Kp*np.sin(alpha)*np.cos(alpha) + Kalpha*alpha
self.CapErr = Kalpha*np.sin(alpha)*np.cos(alpha)
self.DistErra.append(self.DistErr)
def setConsign(self):
V = self.DistErr
Omega = self.CapErr
VMG = (V - Omega * self.MEntreAxes/2)/1 #1 = wheelRadius
VMD = (V + Omega * self.MEntreAxes/2)/1
self.robot.setLeftMotorSpeed(VMG)
self.robot.setRightMotorSpeed(VMD)
def draw(self):
shape2 = np.transpose(Transform.rotate(self.polygon, self.theta))
shape2 = np.transpose(Transform.translate(np.transpose(shape2), self.x, self.y))
plt.plot( shape2[0], shape2[1])
plt.plot( self.xC, self.yC , 'bx')
def update(self, dT):
self.updateOdometry(dT)
self.computeError()
self.setConsign()
if __name__== "__main__":
import main