-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheeled_chassis.go
executable file
·392 lines (314 loc) · 9.43 KB
/
wheeled_chassis.go
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package chassis
import (
"github.com/gonum/matrix/mat64"
"log"
"math"
"fmt"
"runtime"
"time"
"github.com/fuzzycow/ev32go/robotics/telemetry/influxtl"
"github.com/fuzzycow/ev32go/ev3api/device"
"github.com/fuzzycow/ev32go/robotics/utils"
"github.com/fuzzycow/ev32go/robotics/pid"
)
const (
TACHOCOUNT = 0
MAXSPEED = 1
ROTATIONSPEED = 2
TYPE_DIFFERENTIAL = 2
TYPE_HOLONOMIC = 3
)
type WheeledChassis struct {
wheels []Wheel
dummyWheels int
forward *mat64.Dense
reverse *mat64.Dense
forwardAbs *mat64.Dense
reverseAbs *mat64.Dense
linearSpeed float64
linearAccel float64
angularSpeed float64
angularAccel float64
ctrl *pid.Controller
}
func New(wheels []Wheel, dim int) *WheeledChassis {
c := &WheeledChassis{wheels: wheels}
nWheels := len(wheels)
if nWheels < dim {
log.Fatalf("The c must have at least %d motorized wheels", dim)
}
if dim == TYPE_DIFFERENTIAL {
c.dummyWheels = 1
}
totalWheels := nWheels + c.dummyWheels
c.forward = newMatrix(totalWheels, 3)
//motorSpeed := mat64.NewDense(totalWheels, 1,make([]float64,totalWheels))
for row := 0; row < nWheels; row++ {
c.forward.SetRow(row, wheels[row].Factors())
}
if (c.dummyWheels == 1) {
c.forward.SetRow(nWheels, []float64{0, 1, 0});
}
logMatrix("Fwd", c.forward)
c.reverse = newMatrix(totalWheels, 3)
if err := c.reverse.Inverse(c.forward); err != nil {
panic(fmt.Errorf("Invalid wheel setup, this robot is not controlable. Check position of the wheels: %v", err))
}
c.forwardAbs = matrixAbsCopy(c.forward)
c.reverseAbs = matrixAbsCopy(c.reverse)
c.InitPidController()
return c
}
func (c *WheeledChassis) InitPidController() {
tl := influxtl.NewClient("udp","192.168.1.10:8089","robot")
if err := tl.Open(); err != nil {
log.Fatalf("failed to open udp conn to InfluxDB line protocol socket")
}
motors := make([]*device.Motor,c.NumMotors())
for i := 0; i< c.NumMotors();i++ {
motors[i] = c.MotorN(i)
}
ctrl := pid.NewController(
motors,
&pid.PID{P: 100,I: 130,D: 0, F: 130},
tl)
c.ctrl = ctrl
ctrl.Start()
}
func (c *WheeledChassis) NumWheels() int {
return len(c.wheels)
}
func (c *WheeledChassis) WheelN(n int) Wheel {
return c.wheels[n]
}
func (c *WheeledChassis) NumMotors() int {
return c.NumWheels()
}
func (c *WheeledChassis) MotorN(n int) *device.Motor {
return c.wheels[n].GetMotor()
}
func (c *WheeledChassis) SetPidController(ctrl *pid.Controller) {
c.ctrl = ctrl
}
func (c *WheeledChassis) SetVelocity(linearSpeed, direction, angularSpeed float64) {
if c.dummyWheels == 1 && ( int(direction) % 180 != 0 ) {
log.Fatalln("Invalid direction for differential a robot.")
}
tSpeed := matrixMul(
c.forward,
cartesianMatrix(
linearSpeed,
utils.ToRadians(direction),
angularSpeed))
motorAccel := matrixMul(
c.forwardAbs,
matrixAbsCopy(
cartesianMatrix(
c.linearAccel,
utils.ToRadians(direction),
c.angularAccel)))
deltaSpeed := c.getMotorFnMatrix(func(m *device.Motor) int {return -1 * m.Speed()})
deltaSpeed.Add(deltaSpeed,tSpeed)
dt := matrixDivElem(deltaSpeed,motorAccel)
// FIXME - decelleration / negative values?
longest := mat64.Max(matrixAbsCopy(dt))
if longest <= 0.001 {
log.Printf("FIXME2: no speed change")
return
}
speeds := make([]int,c.NumMotors())
for i := 0; i < c.NumMotors(); i++ {
speedFloat := tSpeed.At(i, 0)
speed := int(speedFloat)
speeds[i] = speed
// m := c.MotorN(i)
// m.SetStopCommand(m.StopCommand_Brake())
// m.Command_RunDirect()
log.Printf("SetVelocity: motor[%d] speed=%d, accel=%f", i,speed,longest)
/* rampTimeMs := int(int(longest*1000))
m := c.MotorN(i)
m.SetSpeedSP(speed)
m.SetRampUpSP(rampTimeMs)
m.SetRampDownSP(rampTimeMs)
m.SetStopCommand(m.StopCommand_Coast()) */
}
move := pid.NewMove().
WithSpeed(speeds).
WithFusion(true).
WithRampUp( time.Duration(int64(longest*1000)) * time.Millisecond)
c.ctrl.MoveCh <- move
// runtime.LockOSThread()
// defer runtime.UnlockOSThread()
//c.runForever()
//logMatrix("SetVelocity motorSpeed", motorSpeed)
}
func (c *WheeledChassis) SetSpeed(linearSpeed, angularSpeed float64) {
if linearSpeed < 0 || angularSpeed < 0 {
panic("Speed must be greater then 0")
}
c.linearSpeed = linearSpeed
c.angularSpeed = angularSpeed
}
func (c *WheeledChassis) SetAccel(linearAccel, angularAccel float64) {
if linearAccel < 0 || angularAccel < 0 {
panic("Speed must be greater then 0")
}
c.linearAccel = linearAccel
c.angularAccel = angularAccel
}
func (c *WheeledChassis) travelCartesian(xSpeed, ySpeed, angularSpeed float64) {
c.SetVelocity(math.Sqrt(xSpeed * xSpeed + ySpeed * ySpeed), math.Atan2(ySpeed, xSpeed), angularSpeed);
}
func (c *WheeledChassis) Travel(linearDistance float64) {
if math.IsInf(linearDistance, 1) || math.IsInf(linearDistance, -1) {
c.SetVelocity(utils.SigFloat64(linearDistance) * c.linearSpeed, 0, 0)
} else {
motorDelta := newMatrix(3, 1)
motorDelta.Mul(c.forward, toMatrix(linearDistance, 0, 0))
motorSpeed := newMatrix(3, 1)
motorSpeed.Mul(c.forward, toMatrix(c.linearSpeed, 0, 0))
c.setMotors(motorDelta, motorSpeed)
}
}
func (c *WheeledChassis) Rotate(angularDistance float64) {
if math.IsInf(angularDistance, 1) || math.IsInf(angularDistance, -1) {
c.SetVelocity(0, 0, utils.SigFloat64(angularDistance) * c.angularSpeed)
} else {
motorDelta := newMatrix(3, 1)
motorDelta.Mul(c.forward, toMatrix(0, 0, angularDistance))
motorSpeed := newMatrix(3, 1)
motorSpeed.Mul(c.forward, toMatrix(0, 0, c.angularSpeed))
c.setMotors(motorDelta, motorSpeed)
}
}
func (c *WheeledChassis) Arc(radius, angle float64) {
if angle == 0 {
return
}
ratio := math.Abs(math.Pi * radius / 180)
switch {
case math.IsInf(angle, 1) || math.IsInf(angle, -1):
if ratio > 1 {
c.SetVelocity(utils.SigFloat64(angle) * c.linearSpeed,
0,
utils.SigFloat64(angle) * c.linearSpeed / ratio)
} else {
c.SetVelocity(
utils.SigFloat64(angle) * c.angularSpeed * ratio,
0,
utils.SigFloat64(radius) * c.angularSpeed)
}
case math.IsInf(radius, 1) || math.IsInf(radius, -1):
if angle < 0 {
c.Travel(math.Inf(1))
} else {
c.Travel(math.Inf(-1))
}
default:
displacement := toMatrix(
utils.SigFloat64(angle) * 2 * math.Pi * math.Abs(radius) * math.Abs(angle) / 360,
0,
utils.SigFloat64(radius) * angle)
var (
tSpeed *mat64.Dense
tAccel *mat64.Dense
)
if ratio > 1 {
tSpeed = toMatrix(c.linearSpeed, 0, c.linearSpeed / ratio)
tAccel = toMatrix(c.linearAccel, 0, c.linearAccel / ratio)
} else {
tSpeed = toMatrix(c.angularSpeed * ratio, 0, c.angularSpeed)
tAccel = toMatrix(c.angularAccel * ratio, 0, c.angularAccel)
}
motorDelta := matrixMul(c.forward, displacement)
logMatrix("displacement", displacement)
logMatrix("motor delta", motorDelta)
// using abs of delta, to fix going backwards
mRatio := matrixMulEach(
motorDelta,
1 / mat64.Max(matrixAbsCopy(motorDelta)))
logMatrix("mratio", mRatio)
motorSpeed := matrixMulEach(
mRatio,
mat64.Max(matrixMul(c.forwardAbs, tSpeed)))
//FIXME - Need Abs for Accel ???
motorAccel := matrixMulEach(
mRatio,
mat64.Max(matrixMul(c.forwardAbs, tAccel)))
_ = motorAccel
c.setMotors(motorDelta, motorSpeed)
}
}
func (c *WheeledChassis) Stop() {
c.SetVelocity(0, 0, 0)
}
func (c *WheeledChassis) StopMotors() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for i := 0; i < c.NumMotors(); i++ {
c.MotorN(i).Stop()
}
}
func (c *WheeledChassis) Close() {
c.StopMotors()
for i := 0; i < c.NumMotors(); i++ {
c.MotorN(i).Close()
}
}
func (c *WheeledChassis) runForever() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for i := 0; i < c.NumMotors(); i++ {
c.MotorN(i).RunForever()
}
}
func (c *WheeledChassis) runToRelPos() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for i := 0; i < c.NumMotors(); i++ {
c.MotorN(i).RunToRelPos()
}
}
func (c *WheeledChassis) WaitComplete() {
for i := 0; i < c.NumMotors(); i++ {
WAIT: for {
state := c.MotorN(i).GetAttrString("state")
if state == "" || state == "holding" {
break WAIT
} else {
time.Sleep(10 * time.Millisecond)
}
}
}
}
func (c *WheeledChassis) setMotors(motorDelta, motorSpeed mat64.Matrix) {
for i := 0; i < c.NumMotors(); i++ {
//motor := c.wheels[i].motor
speed := int(motorSpeed.At(i, 0))
delta := int(motorDelta.At(i, 0))
fmt.Printf("Motor %d delta=%d, speed=%d\n", i, delta, speed)
m := c.MotorN(i)
m.SetStopCommand(m.StopCommand_Brake())
m.SetSpeedRegulationEnabled(m.SpeedRegulation_On())
m.SetPolarity(m.Polarity_Inversed())
m.SetRampUpSP(500)
m.SetRampDownSP(500)
m.SetSpeedSP(speed)
m.SetPositionSP(delta)
m.SetStopCommand(m.StopCommand_Brake())
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
c.runToRelPos()
}
type motorAttrGetFn func(*device.Motor) int
// WARNING: Will misfire is called for a non-int attr
func (c *WheeledChassis) getMotorFnMatrix(fn motorAttrGetFn) *mat64.Dense {
values := make([]float64,c.NumMotors())
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for i := 0; i < c.NumMotors(); i++ {
values[i] = float64(fn(c.MotorN(i)))
}
return mat64.NewDense(3,1,values)
}