-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_wheel_steering_controller.py
986 lines (875 loc) · 39.4 KB
/
multi_wheel_steering_controller.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
#!/usr/bin/python3
import math
from abc import ABC, abstractmethod
from typing import Callable, List
from swerve_controller.profile import TransientVariableProfile
# local
from .control import (
BodyMotionCommand,
DriveModuleMotionCommand,
InvalidMotionCommandException,
MotionCommand,
)
from .control_model import (
ControlModelBase,
SimpleFourWheelSteeringControlModel,
difference_between_angles,
)
from .control_profile import (
BodyControlledDriveModuleProfile,
BodyMotionProfile,
DriveModuleStateProfile,
ModuleStateProfile,
)
from .drive_module import DriveModule
from .geometry import RealNumberValueSpace
from .states import BodyState, DriveModuleDesiredValues, DriveModuleMeasuredValues
class BaseSteeringController(ABC):
# Returns the current pose of the robot body, based on the current state of the
# drive modules.
@abstractmethod
def body_state_at_current_time(self) -> BodyState:
pass
# Returns the states of the drive modules, as measured at the current time.
@abstractmethod
def drive_module_states_at_current_time(self) -> List[DriveModuleMeasuredValues]:
pass
# Returns the state of the drive modules to required to match the current trajectory at the given
# time.
@abstractmethod
def drive_module_state_at_future_time(
self, future_time_in_seconds: float
) -> List[DriveModuleMeasuredValues]:
pass
# Updates the currently stored drive module state
@abstractmethod
def on_state_update(self, current_module_states: List[DriveModuleMeasuredValues]):
pass
# Updates the currently stored desired body state. On the next time tick the
# drive module trajectory will be updated to match the new desired end state.
@abstractmethod
def on_desired_state_update(self, desired_motion: MotionCommand):
pass
# On clock tick, determine if we need to recalculate the trajectories for the drive modules
@abstractmethod
def on_tick(self, current_time_in_seconds: float):
pass
class ModuleFirstSteeringController(BaseSteeringController):
def __init__(
self,
drive_modules: List[DriveModule],
motion_profile_func: Callable[
[float, float, float, RealNumberValueSpace], TransientVariableProfile
],
):
# Get the geometry for the robot
self.modules = drive_modules
self.motion_profile_func = motion_profile_func
# Store the current (estimated) state of the body
self.body_state: BodyState = BodyState(
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
# Store the current (measured) state of the drive modules
self.module_states: List[DriveModuleMeasuredValues] = [
DriveModuleMeasuredValues(
drive_module.name,
drive_module.steering_axis_xy_position.x,
drive_module.steering_axis_xy_position.y,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
for drive_module in drive_modules
]
# Use a simple control model for the time being. Just need something that roughly works
self.control_model = SimpleFourWheelSteeringControlModel(self.modules)
# Store the desired body motion, and the last point in time where this value
# was updated
self.desired_motion: List[DriveModuleDesiredValues] = []
self.motion_command_changed_at_time_in_seconds = 0.0
# Track the current trajectories and update them if necessary
self.drive_module_trajectory: DriveModuleStateProfile = None
# Track the time at which the trajectories were created
self.trajectory_created_at_time_in_seconds = 0.0
# Store the time pointer for where we are on the trajectory
self.trajectory_current_time_in_seconds = 0.0
# Keep track of our position in time so that we can figure out where on the current
# trajectory we should be
self.current_time_in_seconds = 0.0
self.trajectory_was_started_at_time_in_seconds = 0.0
self.last_state_update_time = 0.0
self.min_time_for_trajectory = 1.0
# Returns the current pose of the robot body, based on the current state of the
# drive modules.
def body_state_at_current_time(self) -> BodyState:
return self.body_state
# Returns the states of the drive modules, as measured at the current time.
def drive_module_states_at_current_time(self) -> List[DriveModuleMeasuredValues]:
return self.module_states
# Returns the state of the drive modules to required to match the current trajectory at the given
# time.
def drive_module_state_at_future_time(
self, future_time_in_seconds: float
) -> List[DriveModuleDesiredValues]:
time_from_start_of_trajectory = (
future_time_in_seconds - self.trajectory_was_started_at_time_in_seconds
)
time_fraction = time_from_start_of_trajectory
result: List[DriveModuleDesiredValues] = []
for drive_module in self.modules:
state = self.drive_module_trajectory.value_for_module_at(
drive_module.name, time_fraction
)
result.append(
DriveModuleDesiredValues(
state.name,
state.orientation_in_body_coordinates.z,
state.drive_velocity_in_module_coordinates.x,
)
)
return result
# Gets the control model that is used to determine the state of the body and the drive modules.
def get_control_model(self) -> ControlModelBase:
return self.control_model
# Updates the currently stored drive module state
def on_state_update(self, current_module_states: List[DriveModuleMeasuredValues]):
if current_module_states is None:
raise TypeError()
if len(current_module_states) != len(self.modules):
raise ValueError()
self.module_states = current_module_states
# Calculate the current body state
body_motion = self.control_model.body_motion_from_wheel_module_states(
self.module_states
)
time_step_in_seconds = (
self.current_time_in_seconds - self.last_state_update_time
)
# Position
local_x_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.x
+ body_motion.linear_velocity.x
)
)
local_y_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.y
+ body_motion.linear_velocity.y
)
)
# Orientation
global_orientation = (
self.body_state.orientation_in_world_coordinates.z
+ time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.angular_velocity.z
+ body_motion.angular_velocity.z
)
)
# Acceleration
local_x_acceleration = 0.0
local_y_acceleration = 0.0
orientation_acceleration = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_acceleration = (
body_motion.linear_velocity.x
- self.body_state.motion_in_body_coordinates.linear_velocity.x
) / time_step_in_seconds
local_y_acceleration = (
body_motion.linear_velocity.y
- self.body_state.motion_in_body_coordinates.linear_velocity.y
) / time_step_in_seconds
orientation_acceleration = (
body_motion.angular_velocity.z
- self.body_state.motion_in_body_coordinates.angular_velocity.z
) / time_step_in_seconds
# Jerk
local_x_jerk = 0.0
local_y_jerk = 0.0
orientation_jerk = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_jerk = (
local_x_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.x
) / time_step_in_seconds
local_y_jerk = (
local_y_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.y
) / time_step_in_seconds
orientation_jerk = (
orientation_acceleration
- self.body_state.motion_in_body_coordinates.angular_acceleration.z
) / time_step_in_seconds
self.body_state = BodyState(
self.body_state.position_in_world_coordinates.x
+ local_x_distance * math.cos(global_orientation)
- local_y_distance * math.sin(global_orientation),
self.body_state.position_in_world_coordinates.y
+ local_x_distance * math.sin(global_orientation)
+ local_y_distance * math.cos(global_orientation),
global_orientation,
body_motion.linear_velocity.x,
body_motion.linear_velocity.y,
body_motion.angular_velocity.z,
local_x_acceleration,
local_y_acceleration,
orientation_acceleration,
local_x_jerk,
local_y_jerk,
orientation_jerk,
)
self.last_state_update_time = self.current_time_in_seconds
# Updates the currently stored desired body state. On the next time tick the
# drive module trajectory will be updated to match the new desired end state.
def on_desired_state_update(self, desired_motion: MotionCommand):
desired_potential_states = desired_motion.to_drive_module_state(
self.control_model
)
# Select which state to use, either the forward one or the reverse one.
# - If there are two directions we need to pick, otherwise pick the only one we have
# - If the wheels aren't moving then we can pick the one with the closer steering angle change
# - If the wheels are moving then we use
desired_states = desired_potential_states[0]
if len(desired_potential_states[1]) > 0:
is_stopped = [
math.isclose(
state.drive_velocity_in_module_coordinates.x,
0.0,
rel_tol=1e-7,
abs_tol=1e-7,
)
for state in self.module_states
]
if all(is_stopped):
# wheels aren't moving. Can do any move we like. Limit steering movemement.
total_first_rotation = 0.0
total_second_rotation = 0.0
for i in range(len(self.modules)):
current = self.module_states[i].orientation_in_body_coordinates.z
# Normalize the steering angle to be between 0 and 2pi
if current >= 2 * math.pi:
current -= 2 * math.pi
if current < 0:
current += 2 * math.pi
total_first_rotation += abs(
desired_potential_states[0][i].steering_angle_in_radians
- current
)
total_second_rotation += abs(
desired_potential_states[1][i].steering_angle_in_radians
- current
)
if total_second_rotation < total_first_rotation:
desired_states = desired_potential_states[1]
else:
# Wheels are moving. Pick the first state for now. Not sure how to pick the correct one
pass
self.min_time_for_trajectory = desired_motion.time_for_motion()
self.desired_motion = desired_states
self.motion_command_changed_at_time_in_seconds = self.current_time_in_seconds
# use the twist trajectory to compute the state for the steering modules for the end state
# and several intermediate points, i.e. determine the vector [[v_i];[gamma_i]].
# Use Seegmiller and Kelly to compute the desired velocities and angles
#
# Keep in mind that our update rate determines the points in time where we can do something
#
# Also keep in mind that steering the wheel effectively changes the velocity of the wheel
# if we use a co-axial system
drive_module_trajectory = DriveModuleStateProfile(
self.modules, self.min_time_for_trajectory, self.motion_profile_func
)
drive_module_trajectory.set_current_state(self.module_states)
drive_module_trajectory.set_desired_end_state(self.desired_motion)
self.drive_module_trajectory = drive_module_trajectory
self.trajectory_was_started_at_time_in_seconds = self.current_time_in_seconds
# On clock tick, determine if we need to recalculate the trajectories for the drive modules
def on_tick(self, current_time_in_seconds: float):
self.current_time_in_seconds = current_time_in_seconds
# Returns the time in seconds that the current movement would need to complete.
def time_for_current_movement(self) -> float:
if self.drive_module_trajectory is None:
return 0.0
else:
return self.drive_module_trajectory.min_trajectory_time_in_seconds
class ModuleFollowsBodySteeringController(BaseSteeringController):
def __init__(
self,
drive_modules: List[DriveModule],
motion_profile_func: Callable[
[float, float, float, RealNumberValueSpace], TransientVariableProfile
],
):
# Get the geometry for the robot
self.modules = drive_modules
self.motion_profile_func = motion_profile_func
# Use a simple control model for the time being. Just need something that roughly works
self.control_model = SimpleFourWheelSteeringControlModel(self.modules)
# Store the current (estimated) state of the body
self.body_state: BodyState = BodyState(
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
# Store the current (measured) state of the drive modules
self.module_states: List[DriveModuleMeasuredValues] = [
DriveModuleMeasuredValues(
drive_module.name,
drive_module.steering_axis_xy_position.x,
drive_module.steering_axis_xy_position.y,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
for drive_module in drive_modules
]
self.previous_module_states: List[DriveModuleMeasuredValues] = [
DriveModuleMeasuredValues(
drive_module.name,
drive_module.steering_axis_xy_position.x,
drive_module.steering_axis_xy_position.y,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
for drive_module in drive_modules
]
# trajectories
self.body_trajectory: BodyMotionProfile = None
self.module_trajectory_from_command: DriveModuleStateProfile = None
# Keep track of our position in time so that we can figure out where on the current
# trajectory we should be
self.current_time_in_seconds = 0.0
self.trajectory_was_started_at_time_in_seconds = 0.0
self.last_state_update_time = 0.0
self.min_time_for_trajectory: float = 0.0
# flags
self.is_executing_body_trajectory: bool = False
self.is_executing_module_trajectory: bool = False
def body_state_at_current_time(self) -> BodyState:
return self.body_state
def drive_module_states_at_current_time(self) -> List[DriveModuleMeasuredValues]:
return self.module_states
# Returns the state of the drive modules to required to match the current trajectory at the given
# time.
def drive_module_state_at_future_time(
self, future_time_in_seconds: float
) -> List[DriveModuleDesiredValues]:
time_from_start_of_trajectory = (
future_time_in_seconds - self.trajectory_was_started_at_time_in_seconds
)
trajectory_time = ( # noqa: F841
self.body_trajectory.time_span()
if self.is_executing_body_trajectory
else self.module_trajectory_from_command.time_span()
)
time_fraction = time_from_start_of_trajectory
result: List[DriveModuleDesiredValues] = []
if self.is_executing_body_trajectory:
body_state = self.body_trajectory.body_motion_at(time_fraction)
drive_module_desired_values = (
self.control_model.state_of_wheel_modules_from_body_motion(body_state)
)
for i in range(len(self.modules)):
# Wheels are moving. We don't know what kind of movement yet though, so figure out if:
# - The wheel are moving at some significant velocity, in that case pick the state that most
# closely matches the current state, i.e. match the drive velocity and the steering angle as
# close as possible
# - The wheel is moving slowly, in that case we may just be close to the moment where the wheel
# stops moving (either just before it does that, or just after). This is where we could potentially
# flip directions (or we might just have flipped directions)
# - If we have just flipped directions then we should probably continue in the same way (but maybe not)
# previous_state_for_module = self.previous_module_states[i]
current_state_for_module = self.module_states[i]
current_steering_angle = (
current_state_for_module.orientation_in_body_coordinates.z
)
current_velocity = (
current_state_for_module.drive_velocity_in_module_coordinates.x
)
# previous_rotation_difference = current_steering_angle - previous_state_for_module.orientation_in_body_coordinates.z
# previous_velocity_difference = current_velocity - previous_state_for_module.drive_velocity_in_module_coordinates.x
states_for_module = drive_module_desired_values[i]
first_state_rotation_difference = difference_between_angles(
current_steering_angle,
states_for_module[0].steering_angle_in_radians,
)
second_state_rotation_difference = difference_between_angles(
current_steering_angle,
states_for_module[1].steering_angle_in_radians,
)
first_state_velocity_difference = (
states_for_module[0].drive_velocity_in_meters_per_second
- current_velocity
)
second_state_velocity_difference = (
states_for_module[1].drive_velocity_in_meters_per_second
- current_velocity
)
# Possibilities:
# - first velocity change and first orientation change are the smallest -> pick the first state
# - second velocity change and second orientation change are the smallest -> pick the second state
# - first velocity change is larger and second orientation change is larger -> Bad state. Pick the one with the least relative change?
if abs(first_state_rotation_difference) <= abs(
second_state_rotation_difference
):
if abs(first_state_velocity_difference) <= abs(
second_state_velocity_difference
):
# first rotation and velocity change are the smallest, so take the first state
result.append(states_for_module[0])
else:
if math.isclose(
abs(first_state_rotation_difference),
abs(second_state_rotation_difference),
rel_tol=1e-7,
abs_tol=1e-7,
):
# first rotation is equal to the second rotation
# first velocity larger than the second velocity.
# pick the second state
result.append(states_for_module[1])
else:
# first rotation is the smallest but second velocity is the smallest
result.append(states_for_module[0])
else:
if abs(second_state_velocity_difference) <= abs(
first_state_velocity_difference
):
# second rotation and velocity change are the smallest, so take the second state
result.append(states_for_module[1])
else:
if math.isclose(
abs(first_state_rotation_difference),
abs(second_state_rotation_difference),
rel_tol=1e-7,
abs_tol=1e-7,
):
# second rotation is equal to the first rotation
# second velocity larger than the first velocity.
# pick the first state
result.append(states_for_module[0])
else:
# second rotation is the smallest but first velocity is the smallest
result.append(states_for_module[1])
else:
for drive_module in self.modules:
state = self.module_trajectory_from_command.value_for_module_at(
drive_module.name, time_fraction
)
result.append(
DriveModuleDesiredValues(
state.name,
state.orientation_in_body_coordinates.z,
state.drive_velocity_in_module_coordinates.x,
)
)
return result
# Updates the currently stored desired body state. On the next time tick the
# drive module trajectory will be updated to match the new desired end state.
def on_desired_state_update(self, desired_motion: MotionCommand):
if isinstance(desired_motion, BodyMotionCommand):
trajectory = BodyMotionProfile(
self.body_state,
desired_motion.to_body_state(self.control_model),
desired_motion.time_for_motion(),
self.motion_profile_func,
)
self.body_trajectory = trajectory
self.is_executing_body_trajectory = True
self.is_executing_module_trajectory = False
else:
if isinstance(desired_motion, DriveModuleMotionCommand):
trajectory = DriveModuleStateProfile(
self.modules,
desired_motion.time_for_motion(),
self.motion_profile_func,
)
trajectory.set_current_state(self.module_states)
trajectory.set_desired_end_state(
desired_motion.to_drive_module_state(self.control_model)[0]
)
self.module_trajectory_from_command = trajectory
self.is_executing_body_trajectory = False
self.is_executing_module_trajectory = True
else:
raise InvalidMotionCommandException()
self.trajectory_was_started_at_time_in_seconds = self.current_time_in_seconds
self.min_time_for_trajectory = desired_motion.time_for_motion()
# Updates the currently stored drive module state
def on_state_update(self, current_module_states: List[DriveModuleMeasuredValues]):
if current_module_states is None:
raise TypeError()
if len(current_module_states) != len(self.modules):
raise ValueError()
self.previous_module_states = self.module_states
self.module_states = current_module_states
# Calculate the current body state
body_motion = self.control_model.body_motion_from_wheel_module_states(
self.module_states
)
time_step_in_seconds = (
self.current_time_in_seconds - self.last_state_update_time
)
# Position
local_x_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.x
+ body_motion.linear_velocity.x
)
)
local_y_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.y
+ body_motion.linear_velocity.y
)
)
# Orientation
global_orientation = (
self.body_state.orientation_in_world_coordinates.z
+ time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.angular_velocity.z
+ body_motion.angular_velocity.z
)
)
# Acceleration
local_x_acceleration = 0.0
local_y_acceleration = 0.0
orientation_acceleration = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_acceleration = (
body_motion.linear_velocity.x
- self.body_state.motion_in_body_coordinates.linear_velocity.x
) / time_step_in_seconds
local_y_acceleration = (
body_motion.linear_velocity.y
- self.body_state.motion_in_body_coordinates.linear_velocity.y
) / time_step_in_seconds
orientation_acceleration = (
body_motion.angular_velocity.z
- self.body_state.motion_in_body_coordinates.angular_velocity.z
) / time_step_in_seconds
# Jerk
local_x_jerk = 0.0
local_y_jerk = 0.0
orientation_jerk = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_jerk = (
local_x_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.x
) / time_step_in_seconds
local_y_jerk = (
local_y_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.y
) / time_step_in_seconds
orientation_jerk = (
orientation_acceleration
- self.body_state.motion_in_body_coordinates.angular_acceleration.z
) / time_step_in_seconds
self.body_state = BodyState(
self.body_state.position_in_world_coordinates.x
+ local_x_distance * math.cos(global_orientation)
- local_y_distance * math.sin(global_orientation),
self.body_state.position_in_world_coordinates.y
+ local_x_distance * math.sin(global_orientation)
+ local_y_distance * math.cos(global_orientation),
global_orientation,
body_motion.linear_velocity.x,
body_motion.linear_velocity.y,
body_motion.angular_velocity.z,
local_x_acceleration,
local_y_acceleration,
orientation_acceleration,
local_x_jerk,
local_y_jerk,
orientation_jerk,
)
self.last_state_update_time = self.current_time_in_seconds
# On clock tick, determine if we need to recalculate the trajectories for the drive modules
def on_tick(self, current_time_in_seconds: float):
self.current_time_in_seconds = current_time_in_seconds
# Returns the time in seconds that the current movement would need to complete.
def time_for_current_movement(self) -> float:
if self.is_executing_body_trajectory:
return self.body_trajectory.min_trajectory_time_in_seconds
elif self.is_executing_module_trajectory:
return self.module_trajectory_from_command.min_trajectory_time_in_seconds
else:
return 0.0
class LimitedModuleFollowsBodySteeringController(BaseSteeringController):
def __init__(
self,
drive_modules: List[DriveModule],
motion_profile_func: Callable[
[float, float, float, RealNumberValueSpace], TransientVariableProfile
],
interpolation_frequency_in_hz: int,
):
# Get the geometry for the robot
self.modules = drive_modules
self.motion_profile_func = motion_profile_func
self.interpolation_frequency_in_hz = interpolation_frequency_in_hz
# Use a simple control model for the time being. Just need something that roughly works
self.control_model = SimpleFourWheelSteeringControlModel(self.modules)
# Store the current (estimated) state of the body
self.body_state: BodyState = BodyState(
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
# Store the current (measured) state of the drive modules
self.module_states: List[DriveModuleMeasuredValues] = [
DriveModuleMeasuredValues(
drive_module.name,
drive_module.steering_axis_xy_position.x,
drive_module.steering_axis_xy_position.y,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
for drive_module in drive_modules
]
self.previous_module_states: List[DriveModuleMeasuredValues] = [
DriveModuleMeasuredValues(
drive_module.name,
drive_module.steering_axis_xy_position.x,
drive_module.steering_axis_xy_position.y,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)
for drive_module in drive_modules
]
# trajectories
self.active_trajectory: ModuleStateProfile = None
# Keep track of our position in time so that we can figure out where on the current
# trajectory we should be
self.current_time_in_seconds = 0.0
self.trajectory_was_started_at_time_in_seconds = 0.0
self.last_state_update_time = 0.0
self.min_time_for_trajectory: float = 0.0
def body_state_at_current_time(self) -> BodyState:
return self.body_state
def drive_module_states_at_current_time(self) -> List[DriveModuleMeasuredValues]:
return self.module_states
# Returns the state of the drive modules to required to match the current trajectory at the given
# time.
def drive_module_state_at_future_time(
self, future_time_in_seconds: float
) -> List[DriveModuleDesiredValues]:
time_from_start_of_trajectory = (
future_time_in_seconds - self.trajectory_was_started_at_time_in_seconds
)
trajectory_time = self.active_trajectory.time_span() # noqa: F841
time_fraction = time_from_start_of_trajectory
result: List[DriveModuleDesiredValues] = []
for drive_module in self.modules:
state = self.active_trajectory.value_for_module_at(
drive_module.name, time_fraction
)
result.append(
DriveModuleDesiredValues(
state.name,
state.orientation_in_body_coordinates.z,
state.drive_velocity_in_module_coordinates.x,
)
)
return result
# Updates the currently stored desired body state. On the next time tick the
# drive module trajectory will be updated to match the new desired end state.
def on_desired_state_update(self, desired_motion: MotionCommand):
# Translate the motion profiles into a set of module motion profiles
# Then handle the limiting
if isinstance(desired_motion, BodyMotionCommand):
trajectory = BodyControlledDriveModuleProfile(
self.modules,
self.control_model,
min_body_to_module_resolution_per_second=100,
motion_profile_func=self.motion_profile_func,
)
trajectory.set_current_state(self.module_states)
trajectory.set_desired_end_state(
desired_motion.to_body_state(self.control_model)
)
self.active_trajectory = trajectory
else:
if isinstance(desired_motion, DriveModuleMotionCommand):
trajectory = DriveModuleStateProfile(
self.modules,
desired_motion.time_for_motion(),
self.motion_profile_func,
)
trajectory.set_current_state(self.module_states)
trajectory.set_desired_end_state(
desired_motion.to_drive_module_state(self.control_model)[0]
)
self.active_trajectory = trajectory
else:
raise InvalidMotionCommandException()
# TODO Check that if a large jump in steering angle is required that we actually do something about that
self.trajectory_was_started_at_time_in_seconds = self.current_time_in_seconds
self.min_time_for_trajectory = desired_motion.time_for_motion()
# Updates the currently stored drive module state
def on_state_update(self, current_module_states: List[DriveModuleMeasuredValues]):
if current_module_states is None:
raise TypeError()
if len(current_module_states) != len(self.modules):
raise ValueError()
self.previous_module_states = self.module_states
self.module_states = current_module_states
# Calculate the current body state
body_motion = self.control_model.body_motion_from_wheel_module_states(
self.module_states
)
time_step_in_seconds = (
self.current_time_in_seconds - self.last_state_update_time
)
# Position
local_x_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.x
+ body_motion.linear_velocity.x
)
)
local_y_distance = (
time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.linear_velocity.y
+ body_motion.linear_velocity.y
)
)
# Orientation
global_orientation = (
self.body_state.orientation_in_world_coordinates.z
+ time_step_in_seconds
* 0.5
* (
self.body_state.motion_in_body_coordinates.angular_velocity.z
+ body_motion.angular_velocity.z
)
)
# Acceleration
local_x_acceleration = 0.0
local_y_acceleration = 0.0
orientation_acceleration = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_acceleration = (
body_motion.linear_velocity.x
- self.body_state.motion_in_body_coordinates.linear_velocity.x
) / time_step_in_seconds
local_y_acceleration = (
body_motion.linear_velocity.y
- self.body_state.motion_in_body_coordinates.linear_velocity.y
) / time_step_in_seconds
orientation_acceleration = (
body_motion.angular_velocity.z
- self.body_state.motion_in_body_coordinates.angular_velocity.z
) / time_step_in_seconds
# Jerk
local_x_jerk = 0.0
local_y_jerk = 0.0
orientation_jerk = 0.0
if not math.isclose(time_step_in_seconds, 0.0, abs_tol=1e-4, rel_tol=1e-4):
local_x_jerk = (
local_x_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.x
) / time_step_in_seconds
local_y_jerk = (
local_y_acceleration
- self.body_state.motion_in_body_coordinates.linear_acceleration.y
) / time_step_in_seconds
orientation_jerk = (
orientation_acceleration
- self.body_state.motion_in_body_coordinates.angular_acceleration.z
) / time_step_in_seconds
self.body_state = BodyState(
self.body_state.position_in_world_coordinates.x
+ local_x_distance * math.cos(global_orientation)
- local_y_distance * math.sin(global_orientation),
self.body_state.position_in_world_coordinates.y
+ local_x_distance * math.sin(global_orientation)
+ local_y_distance * math.cos(global_orientation),
global_orientation,
body_motion.linear_velocity.x,
body_motion.linear_velocity.y,
body_motion.angular_velocity.z,
local_x_acceleration,
local_y_acceleration,
orientation_acceleration,
local_x_jerk,
local_y_jerk,
orientation_jerk,
)
self.last_state_update_time = self.current_time_in_seconds
# On clock tick, determine if we need to recalculate the trajectories for the drive modules
def on_tick(self, current_time_in_seconds: float):
self.current_time_in_seconds = current_time_in_seconds
# Returns the time in seconds that the current movement would need to complete.
def time_for_current_movement(self) -> float:
if self.active_trajectory is None:
return 0.0
else:
return self.active_trajectory.time_span()