-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad_proper_walk.py
140 lines (100 loc) · 3.17 KB
/
quad_proper_walk.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
import math
from controller import Robot, Motor
import numpy as np
robot = Robot()
TIME_STEP = 1
class Leg:
def __init__(self , leg_name):
self.sh = robot.getMotor("shoulder " + leg_name)
self.el = robot.getMotor("elbow " + leg_name)
def go_to(self, x, y):
l1 = 0.3
l2 = 0.3
self.current_x = x
if y == 0:
y = 0.0001
self.current_y = y
theta_2 = math.acos((x**2 + y**2 - l1**2 - l2**2)/(2*l1*l2))
theta_1 = math.atan(x/y) - math.atan((l2 * math.sin(theta_2)/(l1 + l2*math.cos(theta_2))))
self.sh.setPosition(theta_1)
self.el.setPosition(theta_2)
print("theta_1 = ", theta_1)
print("theta_2 = ", theta_2)
def go_to_2(self, x, y):
l1 = 0.3
l2 = 0.3
self.current_x = x
self.current_y = y
theta_2 = math.pi - math.acos((l1**2 + l2**2 - x**2 - y**2)/(2*l1*l2))
theta_1 = math.atan(x/y) - math.atan((l2 * math.sin(theta_2)/(l1 + l2*math.cos(theta_2))))
theta_1 = theta_1 - theta_2
self.sh.setPosition(theta_1)
self.el.setPosition(theta_2)
print("theta_1 = ", theta_1)
print("theta_2 = ", theta_2)
def swing(self, i):
#start = (-0.05, 0.4)
#end = (0.05, 0.4)
x = np.linspace(-0.1, 0.1, 25)
y_1 = np.linspace(0.4, 0.37, 12)
y_2 = np.linspace(0.37, 0.4, 13)
y = np.append(y_1, y_2)
self.go_to(x[i], y[i])
def stance(self, i):
#start = (0.05, 0.4)
#end = (-0.05, 0.4)
x = np.linspace(0.1, -0.1, 25)
y = 0.4
self.go_to(x[i], y)
class Quad:
def __init__(self):
self.lf = Leg("left front")
self.rf = Leg("right front")
self.rr = Leg("right rear")
self.lr = Leg("left rear")
def stand(self):
self.lf.go_to(0, 0.4)
self.rf.go_to(0, 0.4)
self.lr.go_to(0, 0.4)
self.rr.go_to(0, 0.4)
def sit(self):
self.lf.go_to(0, 0)
self.rf.go_to(0, 0)
self.lr.go_to(0, 0)
self.rr.go_to(0, 0)
def see_up(self):
self.lf.go_to(0, 0.4)
self.rf.go_to(0, 0.4)
self.rr.go_to(0, 0.35)
self.lr.go_to(0, 0.35)
def set_height(self, y):
self.lf.go_to(0, y)
self.rf.go_to(0, y)
self.rr.go_to(0, y)
self.lr.go_to(0, y)
def walk_position(self):
self.lf.go_to(0.1, 0.4)
self.rf.go_to(-0.1, 0.4)
self.rr.go_to(0.1, 0.4)
self.lr.go_to(-0.1, 0.4)
def walk(self, i):
step_length = 0.1
if i<25:
self.lf.stance(i)
self.rf.swing(i)
self.rr.stance(i)
self.lr.swing(i)
else:
i = i - 25
self.rf.stance(i)
self.lf.swing(i)
self.lr.stance(i)
self.rr.swing(i)
i = 0
quad = Quad()
y = np.linspace(0.1, 0.5, 100)
while robot.step(TIME_STEP) != -1:
quad.walk(i)
i += 1
if i == 50:
i = 0