-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbicycle.py
1347 lines (1153 loc) · 40 KB
/
bicycle.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# standard library
from math import sin, cos, tan, atan, pi
# external libraries
import numpy as np
from scipy.optimize import newton
from matplotlib.pyplot import figure, rcParams
# local libraries
from .inertia import y_rot
def benchmark_state_space_vs_speed(M, C1, K0, K2, speeds=None, v0=0.,
vf=10., num=50, g=9.81):
"""Returns the state and input matrices for a set of speeds.
Parameters
----------
M : array_like, shape(2,2)
The mass matrix.
C1 : array_like, shape(2,2)
The speed proportional damping matrix.
K0 : array_like, shape(2,2)
The gravity proportional stiffness matrix.
K2 : array_like, shape(2,2)
The speed squared proportional stiffness matrix.
speeds : array_like, shape(n,), optional
An array of speeds in meters per second at which to compute the state
and input matrices. If none, the `v0`, `vf`, and `num` parameters are
used to generate a linearly spaced array.
v0 : float, optional, default: 0.0
The initial speed.
vf : float, optional, default: 10.0
The final speed.
num : int, optional, default: 50
The number of speeds.
g : float, optional, default: 9.81
Acceleration due to gravity in meters per second squared.
Returns
-------
speeds : ndarray, shape(n,)
An array of speeds in meters per second.
As : ndarray, shape(n,4,4)
The state matrices evaluated at each speed in `speeds`.
Bs : ndarray, shape(n,4,2)
The input matrices
Notes
-----
The second order equations of motion take this form:
M * q'' + v * C1 * q' + [g * K0 + v**2 * K2] * q' = f
where q = [roll angle,
steer angle]
and f = [roll torque,
steer torque]
The first order equations of motion take this form:
x' = A * x + B * u
where x = [roll angle,
steer angle,
roll rate,
steer rate]
and u = [roll torque,
steer torque]
Examples
--------
>>> from dtk.bicycle import benchmark_matrices, benchmark_state_space_vs_speed
>>> M, C1, K0, K2 = benchmark_matrices()
>>> vs, As, Bs = benchmark_state_space_vs_speed(M, C1, K0, K2, num=3)
>>> vs
array([ 0., 5., 10.])
>>> As
array([[[ 0. , 0. , 1. , 0. ],
[ 0. , 0. , 0. , 1. ],
[ 9.48977445, -0.57152317, -0. , -0. ],
[ 11.71947687, 30.90875339, -0. , -0. ]],
<BLANKLINE>
[[ 0. , 0. , 1. , 0. ],
[ 0. , 0. , 0. , 1. ],
[ 9.48977445, -22.85146663, -0.52761225, -1.65257699],
[ 11.71947687, -18.38412373, 18.38402617, -15.42432764]],
<BLANKLINE>
[[ 0. , 0. , 1. , 0. ],
[ 0. , 0. , 0. , 1. ],
[ 9.48977445, -89.69129698, -1.0552245 , -3.30515399],
[ 11.71947687, -166.26275511, 36.76805233, -30.84865527]]])
>>> Bs
array([[[ 0. , 0. ],
[ 0. , 0. ],
[ 0.01593498, -0.12409203],
[-0.12409203, 4.32384018]],
<BLANKLINE>
[[ 0. , 0. ],
[ 0. , 0. ],
[ 0.01593498, -0.12409203],
[-0.12409203, 4.32384018]],
<BLANKLINE>
[[ 0. , 0. ],
[ 0. , 0. ],
[ 0.01593498, -0.12409203],
[-0.12409203, 4.32384018]]])
"""
if speeds is None:
speeds = np.linspace(v0, vf, num=num)
As = np.zeros((len(speeds), 4, 4))
Bs = np.zeros((len(speeds), 4, 2))
for i, v in enumerate(speeds):
A, B = benchmark_state_space(M, C1, K0, K2, v, g)
As[i] = A
Bs[i] = B
return speeds, As, Bs
def benchmark_parameters():
"""Returns the benchmark bicycle parameters from [Meijaard2007]_.
Examples
--------
>>> from pprint import pprint
>>> from dtk.bicycle import benchmark_parameters
>>> pprint(benchmark_parameters())
{'IBxx': 9.2,
'IBxz': 2.4,
'IByy': 11.0,
'IBzz': 2.8,
'IFxx': 0.1405,
'IFyy': 0.28,
'IHxx': 0.05892,
'IHxz': -0.00756,
'IHyy': 0.06,
'IHzz': 0.00708,
'IRxx': 0.0603,
'IRyy': 0.12,
'c': 0.08,
'g': 9.81,
'lam': 0.3141592653589793,
'lambda': 0.3141592653589793,
'mB': 85.0,
'mF': 3.0,
'mH': 4.0,
'mR': 2.0,
'rF': 0.35,
'rR': 0.3,
'w': 1.02,
'xB': 0.3,
'xH': 0.9,
'zB': -0.9,
'zH': -0.7}
"""
p = {}
p['w'] = 1.02
p['c'] = 0.08
p['lam'], p['lambda'] = pi / 10., pi / 10.
p['g'] = 9.81
p['rR'] = 0.3
p['mR'] = 2.0
p['IRxx'] = 0.0603
p['IRyy'] = 0.12
p['xB'] = 0.3
p['zB'] = -0.9
p['mB'] = 85.0
p['IBxx'] = 9.2
p['IByy'] = 11.0
p['IBzz'] = 2.8
p['IBxz'] = 2.4
p['xH'] = 0.9
p['zH'] = -0.7
p['mH'] = 4.0
p['IHxx'] = 0.05892
p['IHyy'] = 0.06
p['IHzz'] = 0.00708
p['IHxz'] = -0.00756
p['rF'] = 0.35
p['mF'] = 3.0
p['IFxx'] = 0.1405
p['IFyy'] = 0.28
return p
def benchmark_matrices():
"""Returns the entries to the M, C1, K0, and K2 matrices for the benchmark
parameter set printed in [Meijaard2007]_.
Returns
-------
M : ndarray, shape(2,2)
The mass matrix.
C1 : ndarray, shape(2,2)
The speed proportional damping matrix.
K0 : ndarray, shape(2,2)
The gravity proportional stiffness matrix.
K2 : ndarray, shape(2,2)
The speed squared proportional stiffness matrix.
Notes
-----
The equations of motion take this form:
M * q'' + v * C1 * q' + [g * K0 + v**2 * K2] * q' = f
where q = [roll angle,
steer angle]
and f = [roll torque,
steer torque]
Examples
--------
>>> from dtk.bicycle import benchmark_matrices
>>> M, C1, K0, K2 = benchmark_matrices()
>>> M
array([[80.81722 , 2.31941332],
[ 2.31941332, 0.29784188]])
>>> C1
array([[ 0. , 33.86641391],
[-0.85035641, 1.68540397]])
>>> K0
array([[-80.95 , -2.59951685],
[ -2.59951685, -0.80329488]])
>>> K2
array([[ 0. , 76.5973459 ],
[ 0. , 2.65431524]])
"""
M = np.array([[80.81722, 2.31941332208709],
[2.31941332208709, 0.29784188199686]])
C1 = np.array([[0., 33.86641391492494],
[-0.85035641456978, 1.68540397397560]])
K0 = np.array([[-80.95, -2.59951685249872],
[-2.59951685249872, -0.80329488458618]])
K2 = np.array([[0., 76.59734589573222],
[0., 2.65431523794604]])
return M, C1, K0, K2
def front_contact(q1, q2, q3, q4, q7, d1, d2, d3, rr, rf, guess=None):
"""Returns the location in the ground plane of the front wheel contact
point.
Parameters
----------
q1 : float
The location of the rear wheel contact point with respect to the
inertial origin along the 1 axis (forward).
q2 : float
The location of the rear wheel contact point with respect to the
inertial origin along the 2 axis (right).
q3 : float
The yaw angle.
q4 : float
The roll angle.
q7 : float
The steer angle.
d1 : float
The distance from the rear wheel center to the steer axis.
d2 : float
The distance between the front and rear wheel centers along the steer
axis.
d3 : float
The distance from the front wheel center to the steer axis.
rr : float
The radius of the rear wheel.
rf : float
The radius of the front wheel.
guess : float, optional
A guess for the pitch angle. This may be only needed for extremely
large steer and roll angles.
Returns
-------
q9 : float
The location of the front wheel contact point with respect to the
inertial origin along the 1 axis.
q10 : float
The location of the front wheel contact point with respect to the
inertial origin along the 2 axis.
Examples
--------
>>> import numpy as np
>>> from dtk.bicycle import front_contact
>>> front_contact(0.0, 0.0,
... np.deg2rad(5.0), np.deg2rad(5.0), np.deg2rad(5.0),
... 0.6, 0.3, 0.03, 0.3, 0.3)
(0.6987001194987257, 0.05266663513621053)
"""
q5 = pitch_from_roll_and_steer(q4, q7, rf, rr, d1, d2, d3, guess=guess)
q9 = q1 + (d2 * (sin(q5) * cos(q3) + sin(q3) * sin(q4) * cos(q5)) + d1 *
(cos(q3) * cos(q5) - sin(q3) * sin(q4) * sin(q5)) + rf * cos(q4) *
cos(q5) * (sin(q5) * cos(q3) + sin(q3) * sin(q4) * cos(q5)) /
pow((pow(cos(q4), 2) * pow(cos(q5), 2) + pow((sin(q4) * sin(q7) -
sin(q5) * cos(q4) * cos(q7)), 2)), 0.5) + (cos(q3) * cos(q5) *
cos(q7) - sin(q3) * (sin(q7) * cos(q4) + sin(q4) * sin(q5) *
cos(q7))) * (d3 + rf * (sin(q4) * sin(q7) - sin(q5) * cos(q4) *
cos(q7)) / pow((pow(cos(q4), 2) * pow(cos(q5), 2) + pow((sin(q4) *
sin(q7)-sin(q5) * cos(q4) * cos(q7)), 2)), 0.5)) - rr * sin(q3) *
sin(q4))
q10 = q2 + (rr * sin(q4) * cos(q3) + d1 * (sin(q3) * cos(q5) + sin(q4) *
sin(q5) * cos(q3)) + d2 * (sin(q3) * sin(q5) - sin(q4) * cos(q3) *
cos(q5)) + rf * cos(q4) * cos(q5) * (sin(q3) * sin(q5) - sin(q4) *
cos(q3) * cos(q5)) / pow((pow(cos(q4), 2) * pow(cos(q5), 2) +
pow((sin(q4) * sin(q7) - sin(q5) * cos(q4) * cos(q7)), 2)), 0.5) +
(sin(q3) * cos(q5) * cos(q7) + cos(q3) * (sin(q7) * cos(q4) + sin(q4) *
sin(q5) * cos(q7))) * (d3 + rf * (sin(q4) * sin(q7) - sin(q5) *
cos(q4) * cos(q7)) / pow((pow(cos(q4), 2) * pow(cos(q5), 2) +
pow((sin(q4) * sin(q7) - sin(q5) * cos(q4) * cos(q7)), 2)), 0.5)))
return q9, q10
def meijaard_figure_four(time, rollRate, steerRate, speed):
"""Returns a figure that matches Figure #4 in [Meijaard2007]_.
.. plot::
:context: reset
:include-source:
import numpy as np
from scipy.signal import lti, lsim
from dtk.bicycle import (benchmark_matrices, benchmark_state_space,
meijaard_figure_four)
t = np.linspace(0.0, 5.0)
x0 = np.array([0.0, 0.0, 0.5, 0.0])
speed = 4.6 # m/s
A, B = benchmark_state_space(*benchmark_matrices(), speed, 9.81)
C, D = np.eye(4), np.zeros((4, 2))
system = lti(A, B, C, D)
t, y, _ = lsim(system, 0.0, t, X0=x0)
meijaard_figure_four(t, y[:, 2], y[:, 3], speed*np.ones_like(t))
"""
width = 4.0 # inches
golden_ratio = (np.sqrt(5.0) - 1.0) / 2.0
height = width * golden_ratio
fig = figure()
fig.set_size_inches([width, height])
params = {
'axes.labelsize': 10,
'legend.fontsize': 10,
'xtick.labelsize': 8,
'ytick.labelsize': 8,
}
rcParams.update(params)
fig.subplots_adjust(right=0.85, left=0.15, bottom=0.15)
rateAxis = fig.add_subplot(111)
speedAxis = rateAxis.twinx()
p1, = rateAxis.plot(time, rollRate, "k--", label="Roll Rate")
p2, = rateAxis.plot(time, steerRate, "k:", label="Steer Rate")
p3, = speedAxis.plot(time, speed, "k-", label="Speed")
rateAxis.set_ylim(-0.5, 1.0)
rateAxis.set_yticks([-0.5, 0.0, 0.5, 1.0])
rateAxis.set_xticks([0., 1., 2., 3., 4., 5.])
rateAxis.set_xlabel('Time [sec]')
rateAxis.set_ylabel('Angular Rate [rad/sec]')
lines = [p1, p2, p3]
rateAxis.legend(lines, [l.get_label() for l in lines])
speedAxis.set_ylim(4.55, 4.7)
speedAxis.set_yticks([4.55, 4.60, 4.65, 4.70])
speedAxis.set_ylabel('Speed [m/s]')
return fig
def moore_to_basu(moore, rr, lam):
"""Returns the coordinates, speeds, and accelerations in
[BasuMandal2007]_'s convention.
Parameters
----------
moore : dictionary
A dictionary containg values for the q's, u's and u dots.
rr : float
Rear wheel radius.
lam : float
Steer axis tilt.
Returns
-------
basu : dictionary
A dictionary containing the coordinates, speeds and accelerations.
Examples
--------
>>> import numpy as np
>>> from pprint import pprint
>>> from dtk.bicycle import basu_table_one_input
>>> from dtk.bicycle import basu_to_moore_input, moore_to_basu
>>> rr, lam = 0.3, np.pi/10
>>> basu = basu_table_one_input()
>>> pprint(basu)
{'betaf': 0.0,
'betafd': 8.0133620584155,
'betar': 0.0,
'betard': 8.912989661489,
'phi': 3.1257073014894,
'phid': -0.0119185528069,
'psi': 0.9501292851472,
'psid': 0.6068425835418,
'psif': 0.2311385135743,
'psifd': 0.4859824687093,
'theta': 0.0,
'thetad': 0.7830033527065,
'x': 0.0,
'xd': -2.8069345714545,
'y': 0.0,
'yd': -0.1480982396001,
'z': 0.2440472102925,
'zd': 0.1058778746261}
>>> moore = basu_to_moore_input(basu, rr, lam)
>>> pprint(moore)
{'q1': -0.0,
'q2': -0.17447337661787718,
'q3': -0.0,
'q4': 0.6206670416476966,
'q5': 0.3300446174593725,
'q6': -0.0,
'q7': -0.2311385135743,
'q8': -0.0,
'u1': 2.6703213326046784,
'u2': -2.453592884421596e-14,
'u3': -0.7830033527065,
'u4': -0.6068425835418,
'u5': 0.0119185528069,
'u6': -8.912989661489,
'u7': -0.4859824687093,
'u8': -8.0133620584155}
>>> moore['u1p'] = 1.0
>>> moore['u2p'] = 2.0
>>> moore['u3p'] = 3.0
>>> moore['u4p'] = 4.0
>>> moore['u5p'] = 5.0
>>> moore['u6p'] = 6.0
>>> moore['u7p'] = 7.0
>>> moore['u8p'] = 8.0
>>> pprint(moore_to_basu(moore, rr, lam))
{'betaf': 0.0,
'betafd': 8.0133620584155,
'betafdd': -8.0,
'betar': 0.0,
'betard': 8.912989661489,
'betardd': -6.0,
'phi': 3.1257073014894,
'phid': -0.0119185528069,
'phidd': -5.0,
'psi': 0.9501292851472,
'psid': 0.6068425835418,
'psidd': -4.0,
'psif': 0.2311385135743,
'psifd': 0.4859824687093,
'psifdd': -7.0,
'theta': 0.0,
'thetad': 0.7830033527065,
'thetadd': -3.0,
'x': 0.0,
'xd': -2.8069345714545,
'xdd': -0.24465703387278925,
'y': 0.0,
'yd': -0.14809823960010002,
'ydd': 2.804969014148545,
'z': 0.2440472102925096,
'zd': 0.10587787462605407,
'zdd': -0.7877658248084111}
"""
m = moore
basu = {}
s3 = sin(m['q3'])
c3 = cos(m['q3'])
s4 = sin(m['q4'])
c4 = cos(m['q4'])
basu['x'] = rr * s3 * s4 - m['q1']
basu['y'] = rr * c3 * s4 + m['q2']
basu['z'] = rr * c4
basu['theta'] = -m['q3']
basu['psi'] = pi / 2.0 - m['q4']
basu['phi'] = pi + lam - m['q5']
basu['betar'] = -m['q6']
basu['psif'] = -m['q7']
basu['betaf'] = -m['q8']
basu['xd'] = rr * (c3 * s4 * m['u3'] + s3 * c4 * m['u4']) - m['u1']
basu['yd'] = rr * (-s3 * s4 * m['u3'] + c3 * c4 * m['u4']) + m['u2']
basu['zd'] = -rr * m['u4'] * s4
basu['thetad'] = -m['u3']
basu['psid'] = -m['u4']
basu['phid'] = -m['u5']
basu['betard'] = -m['u6']
basu['psifd'] = -m['u7']
basu['betafd'] = -m['u8']
basu['xdd'] = (rr * (-s3 * s4 * m['u3']**2 + c3 * c4 * m['u3'] * m['u4'] +
c3 * s4 * m['u3p'] + c3 * c4 * m['u3'] * m['u4'] - s3
* s4 * m['u4']**2 + s3 * c4 * m['u4p']) - m['u1p'])
basu['ydd'] = (m['u2p'] - rr * c3 * s4 * m['u3']**2 - rr * s3 * c4 *
m['u3'] * m['u4'] - rr * s3 * s4 * m['u3p'] - rr * s3 * c4 *
m['u3'] * m['u4'] - rr * c3 * s4 * m['u4']**2 + rr * c3 * c4
* m['u4p'])
basu['zdd'] = -rr * (m['u4p'] * s4 + m['u4']**2 * c4)
basu['thetadd'] = -m['u3p']
basu['psidd'] = -m['u4p']
basu['phidd'] = -m['u5p']
basu['betardd'] = -m['u6p']
basu['psifdd'] = -m['u7p']
basu['betafdd'] = -m['u8p']
return basu
def basu_sig_figs():
"""Returns the number of significant figures reported in Table 1 of
[BasuMandal2007]_.
Examples
--------
>>> from pprint import pprint
>>> from dtk.bicycle import basu_sig_figs
>>> pprint(basu_sig_figs())
{'betaf': 0,
'betafd': 14,
'betafdd': 13,
'betar': 0,
'betard': 13,
'betardd': 14,
'phi': 14,
'phid': 12,
'phidd': 13,
'psi': 13,
'psid': 13,
'psidd': 14,
'psif': 13,
'psifd': 13,
'psifdd': 14,
'theta': 0,
'thetad': 13,
'thetadd': 13,
'x': 0,
'xd': 14,
'xdd': 13,
'y': 0,
'yd': 13,
'ydd': 13,
'z': 13,
'zd': 13,
'zdd': 13}
"""
# q, qd, qdd
sigFigTable = [[0, 14, 13], # x
[0, 13, 13], # y
[13, 13, 13], # z
[0, 13, 13], # theta
[13, 13, 14], # psi
[14, 12, 13], # phi
[13, 13, 14], # psif
[0, 13, 14], # betar
[0, 14, 13]] # betaf
deriv = ['', 'd', 'dd']
coordinates = ['x', 'y', 'z', 'theta', 'psi', 'phi', 'psif', 'betar',
'betaf']
sigFigs = {}
for i, row in enumerate(sigFigTable):
for j, col in enumerate(row):
sigFigs[coordinates[i] + deriv[j]] = col
return sigFigs
def basu_table_one_output():
"""
Examples
--------
>>> from pprint import pprint
>>> from dtk.bicycle import basu_table_one_output
>>> pprint(basu_table_one_output())
{'betafdd': 2.454807290455,
'betardd': 1.8472554144217,
'phidd': 0.1205543897884,
'psidd': -7.8555281128244,
'psifdd': -4.6198904039403,
'thetadd': 0.8353281706379,
'xdd': -0.5041626315047,
'ydd': -0.3449706619454,
'zdd': -1.460452833298}
"""
basu = {}
basu['xdd'] = -0.5041626315047
basu['ydd'] = -0.3449706619454
basu['zdd'] = -1.4604528332980
basu['thetadd'] = 0.8353281706379
basu['psidd'] = -7.8555281128244
basu['phidd'] = 0.1205543897884
basu['psifdd'] = -4.6198904039403
basu['betardd'] = 1.8472554144217
basu['betafdd'] = 2.4548072904550
return basu
def basu_table_one_input():
"""
Examples
--------
>>> from pprint import pprint
>>> from dtk.bicycle import basu_table_one_input
>>> pprint(basu_table_one_input())
{'betaf': 0.0,
'betafd': 8.0133620584155,
'betar': 0.0,
'betard': 8.912989661489,
'phi': 3.1257073014894,
'phid': -0.0119185528069,
'psi': 0.9501292851472,
'psid': 0.6068425835418,
'psif': 0.2311385135743,
'psifd': 0.4859824687093,
'theta': 0.0,
'thetad': 0.7830033527065,
'x': 0.0,
'xd': -2.8069345714545,
'y': 0.0,
'yd': -0.1480982396001,
'z': 0.2440472102925,
'zd': 0.1058778746261}
"""
basu = {}
# coordinates
basu['x'] = 0.
basu['y'] = 0.
basu['z'] = 0.2440472102925
basu['theta'] = 0.
basu['psi'] = 0.9501292851472
basu['phi'] = 3.1257073014894
basu['psif'] = 0.2311385135743
basu['betar'] = 0.
basu['betaf'] = 0.
# speeds
basu['xd'] = -2.8069345714545
basu['yd'] = -0.1480982396001
basu['zd'] = 0.1058778746261
basu['thetad'] = 0.7830033527065
basu['psid'] = 0.6068425835418
basu['phid'] = -0.0119185528069
basu['psifd'] = 0.4859824687093
basu['betard'] = 8.9129896614890
basu['betafd'] = 8.0133620584155
return basu
def basu_to_moore_input(basu, rr, lam):
"""Returns the coordinates and speeds of the [Moore2012]_ derivation of the
Whipple bicycle model as a function of the states and speeds of the
[BasuMandal2007]_ coordinates and speeds.
Parameters
----------
basu : dictionary
A dictionary containing the states and speeds of the Basu-Mandal
formulation. The states are represented with words corresponding to the
greek letter and the speeds are the words with `d` appended, e.g. `psi`
and `psid`.
rr : float
Rear wheel radius.
lam : float
Steer axis tilt.
Returns
-------
moore : dictionary
A dictionary with the coordinates, q's, and speeds, u's, for the Moore
formulation.
Examples
--------
>>> import numpy as np
>>> from pprint import pprint
>>> from dtk.bicycle import basu_table_one_input, basu_to_moore_input
>>> vars = basu_table_one_input()
>>> pprint(basu_to_moore_input(vars, 0.3, np.pi/10))
{'q1': -0.0,
'q2': -0.17447337661787718,
'q3': -0.0,
'q4': 0.6206670416476966,
'q5': 0.3300446174593725,
'q6': -0.0,
'q7': -0.2311385135743,
'q8': -0.0,
'u1': 2.6703213326046784,
'u2': -2.453592884421596e-14,
'u3': -0.7830033527065,
'u4': -0.6068425835418,
'u5': 0.0119185528069,
'u6': -8.912989661489,
'u7': -0.4859824687093,
'u8': -8.0133620584155}
"""
moore = {}
# coordinates
moore['q1'] = -rr * sin(basu['theta']) * cos(basu['psi']) - basu['x']
moore['q2'] = basu['y'] - rr * cos(basu['theta']) * cos(basu['psi'])
moore['q3'] = -basu['theta']
moore['q4'] = pi / 2. - basu['psi']
moore['q5'] = pi - basu['phi'] + lam
moore['q6'] = -basu['betar']
moore['q7'] = -basu['psif']
moore['q8'] = -basu['betaf']
# speeds
moore['u1'] = (rr * basu['psid'] * sin(basu['theta']) * sin(basu['psi']) -
rr * basu['thetad'] * cos(basu['theta']) * cos(basu['psi'])
- basu['xd'])
moore['u2'] = (basu['yd'] + rr * basu['thetad'] * sin(basu['theta']) *
cos(basu['psi']) + rr * basu['psid'] * cos(basu['theta']) *
sin(basu['psi']))
moore['u3'] = -basu['thetad']
moore['u4'] = -basu['psid']
moore['u5'] = -basu['phid']
moore['u6'] = -basu['betard']
moore['u7'] = -basu['psifd']
moore['u8'] = -basu['betafd']
return moore
def pitch_from_roll_and_steer(q4, q7, rF, rR, d1, d2, d3, guess=None):
"""Returns the pitch angle of the bicycle frame for a given roll, steer and
geometry.
Parameters
----------
q4 : float
Roll angle.
q5 : float
Steer angle.
rF : float
Front wheel radius.
rR : float
Rear wheel radius.
d1 : float
The rear wheel offset from the steer axis.
d2 : float
The distance along the steer axis between the intersection of the front
and rear offset lines.
d3 : float
The front wheel offset from the steer axis.
guess : float, optional
A good guess for the pitch angle. If not specified, the program will
make a good guess for most roll and steer combinations.
Returns
-------
q5 : float
Pitch angle.
Notes
-----
All of the geometry parameters should be expressed in the same units.
Examples
--------
>>> import numpy as np
>>> from dtk.bicycle import pitch_from_roll_and_steer
>>> from dtk.bicycle import benchmark_parameters, benchmark_to_moore
>>> steer, roll = np.deg2rad(10.0), np.deg2rad(-5.0)
>>> p = benchmark_to_moore(benchmark_parameters())
>>> float(np.rad2deg(pitch_from_roll_and_steer(steer, roll,
... p['rf'], p['rr'],
... p['d1'], p['d2'], p['d3'])))
18.062710178550127
"""
def pitch_constraint(q5, q4, q7, rF, rR, d1, d2, d3):
zero = (d2 * cos(q4) * cos(q5) + rF * cos(q4)**2 * cos(q5)**2 /
(cos(q4)**2 * cos(q5)**2 + (sin(q4) * sin(q7) - sin(q5) *
cos(q4) * cos(q7))**2)**0.5 + (sin(q4) * sin(q7) -
sin(q5) * cos(q4) * cos(q7)) * (d3+rF * (sin(q4) * sin(q7) -
sin(q5) * cos(q4) * cos(q7)) / (cos(q4)**2 * cos(q5)**2 +
(sin(q4) * sin(q7) - sin(q5) * cos(q4) * cos(q7))**2)**0.5) -
rR * cos(q4) - d1 * sin(q5) * cos(q4))
return zero
if guess is None:
# guess based on steer and roll being both zero
guess = lambda_from_abc(rF, rR, d1, d3, d2)
args = (q4, q7, rF, rR, d1, d2, d3)
q5 = newton(pitch_constraint, guess, args=args)
return float(q5)
def benchmark_to_moore(benchmarkParameters, oldMassCenter=False):
"""Returns the parameters for the Whipple model as derived by Jason K.
Moore.
Parameters
----------
benchmarkParameters : dictionary
Contains the set of parameters for the Whipple bicycle model as
presented in [Meijaard2007]_.
oldMassCenter : boolean
If true it returns the fork mass center dimensions, l3 and l4, with
respect to the rear offset intersection with the steer axis, otherwise
the dimensions are with respect to the front wheel.
Returns
-------
mooreParameters : dictionary
The parameter set for the Moore derivation of the whipple bicycle model
as presented in [Moore2012]_.
Examples
--------
>>> from pprint import pprint
>>> from dtk.bicycle import benchmark_parameters, benchmark_to_moore
>>> par = benchmark_parameters()
>>> pprint(benchmark_to_moore(par))
{'d1': 0.9534570696121849,
'd2': 0.2676445084476887,
'd3': 0.03207142672761929,
'g': 9.81,
'ic11': 7.178169776497895,
'ic12': 0.0,
'ic22': 11.0,
'ic23': 0.0,
'ic31': 3.8225535938357873,
'ic33': 4.821830223502103,
'id11': 0.0603,
'id22': 0.12,
'id33': 0.0603,
'ie11': 0.05841337700152972,
'ie12': 0.0,
'ie22': 0.06,
'ie23': 0.0,
'ie31': 0.009119225261946298,
'ie33': 0.007586622998470264,
'if11': 0.1405,
'if22': 0.28,
'if33': 0.1405,
'l1': 0.4707271515135145,
'l2': -0.47792881146460797,
'l3': -0.00597083392418685,
'l4': -0.3699518200282974,
'mc': 85.0,
'md': 2.0,
'me': 4.0,
'mf': 3.0,
'rf': 0.35,
'rr': 0.3}
"""
bP = benchmarkParameters
mP = {}
# geometry
mP['rf'] = bP['rF']
mP['rr'] = bP['rR']
mP['d1'] = cos(bP['lam']) * (bP['c'] + bP['w'] - bP['rR'] * tan(bP['lam']))
mP['d3'] = -cos(bP['lam']) * (bP['c'] - bP['rF'] * tan(bP['lam']))
mP['d2'] = (bP['rR'] + mP['d1'] * sin(bP['lam']) - bP['rF'] + mP['d3'] *
sin(bP['lam'])) / cos(bP['lam'])
# mass center locations
# bicycle frame
mP['l1'] = (bP['xB'] * cos(bP['lam']) - bP['zB'] * sin(bP['lam']) -
bP['rR'] * sin(bP['lam']))
mP['l2'] = (bP['xB'] * sin(bP['lam']) + bP['zB'] * cos(bP['lam']) +
bP['rR'] * cos(bP['lam']))
if 'xcl' in bP and 'zcl' in bP:
mP['d4'] = (bP['xcl'] * cos(bP['lam']) - bP['zcl'] * sin(bP['lam']) -
bP['rR'] * sin(bP['lam']))
mP['d5'] = (bP['xcl'] * sin(bP['lam']) + bP['zcl'] * cos(bP['lam']) +
bP['rR'] * cos(bP['lam']))
# bicycle fork
if oldMassCenter is True:
# l3 and l4 are with reference to the point where the rear offset line
# intersects the steer axis (this is the old way)
mP['l3'] = mP['d3'] + mP['l3']
mP['l4'] = mP['d2'] + mP['l4']
elif oldMassCenter is False:
# l3 and l4 are with reference to the front wheel center (the new way)
mP['l4'] = ((bP['zH'] + bP['rF']) * cos(bP['lam']) + (bP['xH'] -
bP['w']) *
sin(bP['lam']))
mP['l3'] = ((bP['xH'] - bP['w'] - mP['l4'] * sin(bP['lam'])) /
cos(bP['lam']))
else:
raise ValueError('oldMassCenter must be True or False')
# masses
mP['mc'] = bP['mB']
mP['md'] = bP['mR']
mP['me'] = bP['mH']
mP['mf'] = bP['mF']
# inertia
# rear wheel inertia
mP['id11'] = bP['IRxx']
mP['id22'] = bP['IRyy']
mP['id33'] = bP['IRxx']
# front wheel inertia
mP['if11'] = bP['IFxx']
mP['if22'] = bP['IFyy']
mP['if33'] = bP['IFxx']
# lambda rotation matrix
R = y_rot(bP['lam'])
# rotate the benchmark bicycle frame inertia through the steer axis tilt,
# lambda
IB = np.array([[bP['IBxx'], 0., bP['IBxz']],
[0., bP['IByy'], 0.],
[bP['IBxz'], 0., bP['IBzz']]])
IBrot = R @ IB @ R.T
# bicycle frame inertia
mP['ic11'] = float(IBrot[0, 0])
mP['ic12'] = float(IBrot[0, 1])
mP['ic22'] = float(IBrot[1, 1])
mP['ic23'] = float(IBrot[1, 2])
mP['ic31'] = float(IBrot[2, 0])
mP['ic33'] = float(IBrot[2, 2])
# rotate the benchmark bicycle fork inertia through the steer axis tilt,
# lambda
IH = np.array([[bP['IHxx'], 0., bP['IHxz']],
[0., bP['IHyy'], 0.],
[bP['IHxz'], 0., bP['IHzz']]])
IHrot = R @ IH @ R.T
# fork/handlebar inertia
mP['ie11'] = float(IHrot[0, 0])
mP['ie12'] = float(IHrot[0, 1])
mP['ie22'] = float(IHrot[1, 1])
mP['ie23'] = float(IHrot[1, 2])
mP['ie31'] = float(IHrot[2, 0])
mP['ie33'] = float(IHrot[2, 2])
# gravity
mP['g'] = bP['g']
return mP
def lambda_from_abc(rF, rR, a, b, c):