-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveTools.tcl
258 lines (179 loc) · 4.48 KB
/
moveTools.tcl
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
if { [info exists _MOVETOOLS.TCL] == 0 } {
set _MOVETOOLS.TCL 1
set pi 3.14159265358979
proc wrapAngle {angle} {
upvar $angle newAngle
global pi
if {$newAngle > $pi} {
set newAngle [expr $newAngle - [expr 2 * $pi]]
}
if {$newAngle <= -$pi} {
set newAngle [expr $newAngle + [expr 2 * $pi]]
}
}
proc findDistance {dx dy d objective position} {
upvar $dx REFdx
upvar $dy REFdy
upvar $d REFd
upvar $objective REFobjective
upvar $position REFposition
set REFdx [expr $REFobjective(x) - $REFposition(xRob)]
set REFdy [expr $REFobjective(y) - $REFposition(yRob)]
set REFd [expr sqrt($REFdx * $REFdx + $REFdy * $REFdy)]
}
proc findTheta {dx dy dt position} {
upvar $dx REFdx
upvar $dy REFdy
upvar $dt REFdt
upvar $position REFposition
set newTheta [expr atan2($REFdy,$REFdx)]
set REFdt [expr $newTheta - $REFposition(theta)]
wrapAngle REFdt
}
## first turn towards direction
## Then move to the objective xfinal yfinal
## Verify and repeat if necessary
proc moveXY {objective} {
upvar $objective REFobjective
global pi
getPosition position
findDistance dx dy d REFobjective position
findTheta dx dy dt position
if {[expr abs($dt)] > [expr $pi/2]} {
set dt [expr $dt + $pi]
wrapAngle dt
set d -$d
}
if { [expr abs($d)] > 0.10 } {
if { [expr abs($dt)] > [expr $pi/180*5] } {
set aux [expr $dt*180/$pi]
puts "Manque turner $aux"
turn $dt
moveXY REFobjective
} else {
if { [expr abs($d)] > 0.10 } {
puts "Manque avancer $d"
if { abs($d) > 1 } {
set d [expr $d/2]
}
move $d
moveXY REFobjective
}
}
}
}
proc moveT {objective} {
upvar $objective REFobjective
global pi
getPosition position
set dt [expr $REFobjective(theta) - $position(theta)]
wrapAngle dt
if {[expr {abs($dt)}] > [expr $pi/180]} {
puts "manque turner : $dt"
turn $dt
moveT REFobjective
} else {
}
}
proc moveXYT {objective} {
upvar $objective REFobjective
moveXY REFobjective
moveT REFobjective
}
proc defineObjective { varObjective upAngle } {
upvar $varObjective objective
upvar $upAngle ang
set yMax 1.25
set xMax 1.5
set centerX 0.5
set centerY -0.25
set xun [expr cos($ang)]
set yun [expr sin($ang)]
if {$xun == 0} {
set objective(x) 0
if {$yun >= 0} {
set objective(y) $yMax
} else {
set objective(y) -$yMax
}
} elseif {$xun > 0} {
set objective(x) $xMax
set objective(y) [expr $xMax*$yun/$xun]
if {$objective(y) >= $yMax} {
set objective(y) $yMax
set objective(x) [expr $yMax*$xun/$yun]
} elseif {$objective(y) < -$yMax} {
set objective(y) -$yMax
set objective(x) [expr -$yMax*$xun/$yun]
}
} else {
set objective(x) -$xMax
set objective(y) [expr -$xMax*$yun/$xun]
if {$objective(y) > $yMax} {
set objective(y) $yMax
set objective(x) [expr $yMax*$xun/$yun]
} elseif {$objective(y) < -$yMax} {
set objective(y) -$yMax
set objective(x) [expr -$yMax*$xun/$yun]
}
}
set objective(x) [expr $objective(x)+$centerX]
set objective(y) [expr $objective(y)+$centerY]
limitObjective objective
}
proc limitObjective { objective } {
upvar $objective REFobjective
if { $REFobjective(x) > 2.5 } {
set REFobjective(x) 2.5
} elseif { $REFobjective(x) < -3.5 } {
set REFobjective(x) -3.5
}
if { $REFobjective(y) > 2 } {
set REFobjective(y) 2
} elseif { $REFobjective(y) < -2 } {
set REFobjective(y) -2
}
}
proc contourPoints { REFpoints } {
upvar $REFpoints points
set yMax 1.25
set xMax 1.5
set centerX 0.5
set centerY -0.25
set k 0
set i 1
for {set j 0} { $j <= 1} {incr j} {
incr k
set points($k,x) [expr $centerX + $i*$xMax]
set points($k,y) [expr $centerY + $j*$yMax]
}
set j 1
for {set i 0} { $i >= -1} {set i [expr $i-1]} {
incr k
set points($k,x) [expr $centerX + $i*$xMax]
set points($k,y) [expr $centerY + $j*$yMax]
}
set i -1
for {set j 0} { $j >= -1} {set j [expr $j-1]} {
incr k
set points($k,x) [expr $centerX + $i*$xMax]
set points($k,y) [expr $centerY + $j*$yMax]
}
set j -1
for {set i 0} { $i <= 1} {set i [expr $i+1]} {
incr k
set points($k,x) [expr $centerX + $i*$xMax]
set points($k,y) [expr $centerY + $j*$yMax]
}
set points(length) $k
}
proc rotation { vector angleRad } {
upvar $vector v
set cosAngle [expr cos($angleRad)]
set sinAngle [expr sin($angleRad)]
set vx [expr $cosAngle*$v(xRob)-$sinAngle*$v(yRob)]
set vy [expr $sinAngle*$v(xRob)+$cosAngle*$v(yRob)]
set v(xRob) $vx
set v(yRob) $vy
}
}