-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaliput_railcar.cc
489 lines (425 loc) · 18.2 KB
/
maliput_railcar.cc
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
#include "drake/automotive/maliput_railcar.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <utility>
#include <vector>
#include <Eigen/Geometry>
#include "drake/automotive/calc_smooth_acceleration.h"
#include "drake/automotive/maliput/api/branch_point.h"
#include "drake/automotive/maliput/api/lane.h"
#include "drake/automotive/maliput/api/lane_data.h"
#include "drake/common/cond.h"
#include "drake/common/drake_assert.h"
#include "drake/math/roll_pitch_yaw_using_quaternion.h"
#include "drake/multibody/multibody_tree/math/spatial_velocity.h"
#include "drake/systems/framework/basic_vector.h"
#include "drake/systems/framework/value.h"
#include "drake/systems/framework/vector_base.h"
namespace drake {
using maliput::api::GeoPosition;
using maliput::api::IsoLaneVelocity;
using maliput::api::Lane;
using maliput::api::LaneEnd;
using maliput::api::LanePosition;
using maliput::api::Rotation;
using math::RollPitchYawToQuaternion;
using systems::BasicVector;
using systems::Context;
using systems::ContinuousState;
using systems::InputPortDescriptor;
using systems::LeafContext;
using systems::OutputPortDescriptor;
using systems::Parameters;
using systems::SparsityMatrix;
using systems::State;
using systems::SystemOutput;
using systems::VectorBase;
using systems::rendering::FrameVelocity;
using systems::rendering::PoseVector;
namespace automotive {
template <typename T> constexpr T MaliputRailcar<T>::kDefaultInitialS;
template <typename T> constexpr T MaliputRailcar<T>::kDefaultInitialSpeed;
template <typename T> constexpr double MaliputRailcar<T>::kLaneEndEpsilon;
template <typename T> constexpr double MaliputRailcar<T>::kTimeEpsilon;
template <typename T>
MaliputRailcar<T>::MaliputRailcar(const LaneDirection& initial_lane_direction)
: initial_lane_direction_(initial_lane_direction) {
command_input_port_index_ =
this->DeclareInputPort(systems::kVectorValued, 1).get_index();
state_output_port_index_ =
this->DeclareVectorOutputPort(MaliputRailcarState<T>()).get_index();
lane_state_output_port_index_ =
this->DeclareAbstractOutputPort(
systems::Value<LaneDirection>(initial_lane_direction)).get_index();
pose_output_port_index_ =
this->DeclareVectorOutputPort(PoseVector<T>()).get_index();
velocity_output_port_index_ =
this->DeclareVectorOutputPort(FrameVelocity<T>()).get_index();
this->DeclareContinuousState(MaliputRailcarState<T>());
this->DeclareNumericParameter(MaliputRailcarParams<T>());
}
template <typename T>
const InputPortDescriptor<T>& MaliputRailcar<T>::command_input() const {
return this->get_input_port(command_input_port_index_);
}
template <typename T>
const OutputPortDescriptor<T>& MaliputRailcar<T>::state_output() const {
return this->get_output_port(state_output_port_index_);
}
template <typename T>
const OutputPortDescriptor<T>& MaliputRailcar<T>::lane_state_output() const {
return this->get_output_port(lane_state_output_port_index_);
}
template <typename T>
const OutputPortDescriptor<T>& MaliputRailcar<T>::pose_output() const {
return this->get_output_port(pose_output_port_index_);
}
template <typename T>
const OutputPortDescriptor<T>& MaliputRailcar<T>::velocity_output() const {
return this->get_output_port(velocity_output_port_index_);
}
template <typename T>
MaliputRailcarParams<T>* MaliputRailcar<T>::get_mutable_parameters(
systems::Context<T>* context) const {
return this->template GetMutableNumericParameter<MaliputRailcarParams>(
context, 0);
}
template <typename T>
void MaliputRailcar<T>::DoCalcOutput(const Context<T>& context,
SystemOutput<T>* output) const {
// Obtains the parameters.
const MaliputRailcarParams<T>& params =
this->template GetNumericParameter<MaliputRailcarParams>(context, 0);
// Obtains the state.
const MaliputRailcarState<T>* const state =
dynamic_cast<const MaliputRailcarState<T>*>(
&context.get_continuous_state_vector());
DRAKE_ASSERT(state != nullptr);
const LaneDirection& lane_direction =
context.template get_abstract_state<LaneDirection>(0);
// Obtains and updates the output vectors.
MaliputRailcarState<T>* const state_vector =
dynamic_cast<MaliputRailcarState<T>*>(
output->GetMutableVectorData(state_output_port_index_));
DRAKE_ASSERT(state_vector != nullptr);
ImplCalcOutput(*state, state_vector);
LaneDirection& lane_direction_output =
output->GetMutableData(lane_state_output_port_index_)->
template GetMutableValue<LaneDirection>();
ImplCalcLaneOutput(lane_direction, &lane_direction_output);
PoseVector<T>* const pose_vector =
dynamic_cast<PoseVector<T>*>(
output->GetMutableVectorData(pose_output_port_index_));
DRAKE_ASSERT(pose_vector != nullptr);
ImplCalcPose(params, *state, lane_direction, pose_vector);
FrameVelocity<T>* const frame_velocity =
dynamic_cast<FrameVelocity<T>*>(
output->GetMutableVectorData(velocity_output_port_index_));
DRAKE_ASSERT(frame_velocity != nullptr);
ImplCalcVelocity(params, *state, lane_direction, frame_velocity);
}
template <typename T>
void MaliputRailcar<T>::ImplCalcOutput(const MaliputRailcarState<T>& state,
MaliputRailcarState<T>* output) const {
output->set_value(state.get_value());
// Don't allow small negative speed to escape our state.
DRAKE_ASSERT(state.speed() >= -1e-3);
using std::max;
output->set_speed(max(T(0), state.speed()));
}
template <typename T>
void MaliputRailcar<T>::ImplCalcLaneOutput(const LaneDirection& lane_direction,
LaneDirection* output) const {
*output = lane_direction;
}
template <typename T>
T MaliputRailcar<T>::CalcR(const MaliputRailcarParams<T>& params,
const LaneDirection& lane_direction) const {
if (lane_direction.with_s == initial_lane_direction_.with_s) {
return params.r();
} else {
return -params.r();
}
}
template <typename T>
void MaliputRailcar<T>::ImplCalcPose(const MaliputRailcarParams<T>& params,
const MaliputRailcarState<T>& state, const LaneDirection& lane_direction,
PoseVector<T>* pose) const {
const LanePosition lane_position(state.s(), CalcR(params, lane_direction),
params.h());
const GeoPosition geo_position =
lane_direction.lane->ToGeoPosition(lane_position);
const Rotation rotation =
lane_direction.lane->GetOrientation(lane_position);
using std::atan2;
using std::sin;
using std::cos;
// Adjust the rotation based on whether the vehicle is traveling with s or
// against s.
const Rotation adjusted_rotation =
(lane_direction.with_s ? rotation :
Rotation::FromRpy(-rotation.roll(),
-rotation.pitch(),
atan2(-sin(rotation.yaw()), -cos(rotation.yaw()))));
pose->set_translation(Eigen::Translation<T, 3>(geo_position.xyz()));
pose->set_rotation(RollPitchYawToQuaternion(
Vector3<T>(adjusted_rotation.roll(), adjusted_rotation.pitch(),
adjusted_rotation.yaw())));
}
template <typename T>
void MaliputRailcar<T>::ImplCalcVelocity(const MaliputRailcarParams<T>& params,
const MaliputRailcarState<T>& state, const LaneDirection& lane_direction,
FrameVelocity<T>* frame_velocity) const {
// In the following code:
// - v is the translational component of the spatial velocity.
// - C is the car's frame.
// - L is the lane frame.
// - W is the world frame.
// - R is a rotation matrix.
const Vector3<T> v_LC_L(lane_direction.with_s ? state.speed() :
-state.speed(),
0 /* r_dot */, 0 /* h_dot */);
const Rotation rotation =
lane_direction.lane->GetOrientation(
LanePosition(state.s(), params.r(), params.h()));
const Eigen::Matrix<T, 3, 3> R_WL = rotation.matrix();
const Vector3<T> v_WC_W = R_WL * v_LC_L;
// TODO(liang.fok) Add support for non-zero rotational velocity. See #5751.
const Vector3<T> w(T(0), T(0), T(0));
frame_velocity->set_velocity(multibody::SpatialVelocity<T>(w, v_WC_W));
}
template <typename T>
void MaliputRailcar<T>::DoCalcTimeDerivatives(
const Context<T>& context, ContinuousState<T>* derivatives) const {
DRAKE_ASSERT(derivatives != nullptr);
// Obtains the parameters.
const MaliputRailcarParams<T>& params =
this->template GetNumericParameter<MaliputRailcarParams>(context, 0);
// Obtains the state.
const VectorBase<T>& context_state = context.get_continuous_state_vector();
const MaliputRailcarState<T>* const state =
dynamic_cast<const MaliputRailcarState<T>*>(&context_state);
DRAKE_ASSERT(state != nullptr);
const LaneDirection& lane_direction =
context.template get_abstract_state<LaneDirection>(0);
// Obtains the input.
const BasicVector<T>* input =
this->template EvalVectorInput<BasicVector>(context,
command_input_port_index_);
// Allocates and uses a BasicVector containing a zero acceleration command in
// case the input contains nullptr.
const auto default_input = BasicVector<T>::Make(0);
if (input == nullptr) {
input = default_input.get();
}
DRAKE_ASSERT(input->size() == 1);
// Obtains the result structure.
VectorBase<T>* const vector_derivatives = derivatives->get_mutable_vector();
DRAKE_ASSERT(vector_derivatives);
MaliputRailcarState<T>* const rates =
dynamic_cast<MaliputRailcarState<T>*>(vector_derivatives);
DRAKE_ASSERT(rates != nullptr);
ImplCalcTimeDerivatives(params, *state, lane_direction, *input, rates);
}
template<typename T>
void MaliputRailcar<T>::ImplCalcTimeDerivatives(
const MaliputRailcarParams<T>& params,
const MaliputRailcarState<T>& state,
const LaneDirection& lane_direction,
const BasicVector<T>& input,
MaliputRailcarState<T>* rates) const {
const T speed = state.speed();
const T sigma_v = cond(lane_direction.with_s, speed, -speed);
const LanePosition motion_derivatives =
lane_direction.lane->EvalMotionDerivatives(
LanePosition(state.s(), CalcR(params, lane_direction), params.h()),
IsoLaneVelocity(sigma_v, 0 /* rho_v */, 0 /* eta_v */));
// Since the railcar's IsoLaneVelocity's rho_v and eta_v values are both
// zero, we expect the resulting motion derivative's r and h values to
// also be zero. The IsoLaneVelocity's sigma_v, which may be non-zero, maps
// to the motion derivative's s value.
DRAKE_ASSERT(motion_derivatives.r() == 0);
DRAKE_ASSERT(motion_derivatives.h() == 0);
rates->set_s(motion_derivatives.s());
const T desired_acceleration = input.GetAtIndex(0);
const T smooth_acceleration = calc_smooth_acceleration(
desired_acceleration, params.max_speed(), params.velocity_limit_kp(),
state.speed());
rates->set_speed(smooth_acceleration);
}
template <typename T>
std::unique_ptr<systems::AbstractValues>
MaliputRailcar<T>::AllocateAbstractState() const {
std::vector<std::unique_ptr<systems::AbstractValue>> abstract_values;
const LaneDirection lane_direction;
abstract_values.push_back(std::unique_ptr<systems::AbstractValue>(
std::make_unique<systems::Value<LaneDirection>>(lane_direction)));
return std::make_unique<systems::AbstractValues>(std::move(abstract_values));
}
template <typename T>
bool MaliputRailcar<T>::DoHasDirectFeedthrough(
const SparsityMatrix*, int, int) const {
return false;
}
template <typename T>
void MaliputRailcar<T>::SetDefaultState(const Context<T>&,
State<T>* state) const {
MaliputRailcarState<T>* railcar_state =
dynamic_cast<MaliputRailcarState<T>*>(
state->get_mutable_continuous_state()->get_mutable_vector());
DRAKE_DEMAND(railcar_state != nullptr);
SetDefaultState(railcar_state);
LaneDirection& lane_direction =
state->get_mutable_abstract_state()->get_mutable_value(0).
template GetMutableValue<LaneDirection>();
lane_direction = initial_lane_direction_;
}
template <typename T>
void MaliputRailcar<T>::SetDefaultState(
MaliputRailcarState<T>* railcar_state) {
railcar_state->set_s(kDefaultInitialS);
railcar_state->set_speed(kDefaultInitialSpeed);
}
// TODO(liang.fok): Switch to guard functions once they are available. The
// following computes an estimate of when the vehicle will reach the end of
// its lane. This estimate will be off when r != 0 and the lane is very
// curvy because the scale factors used in Lane::EvalMotionDerivatives() will
// not be constant.
//
// Another reason why the estimate will be off is the acceleration of the
// vehicle is not considered (see #5532).
template <typename T>
void MaliputRailcar<T>::DoCalcNextUpdateTime(const systems::Context<T>& context,
systems::UpdateActions<T>* actions) const {
const VectorBase<T>& context_state = context.get_continuous_state_vector();
const MaliputRailcarState<T>* const state =
dynamic_cast<const MaliputRailcarState<T>*>(&context_state);
DRAKE_ASSERT(state != nullptr);
if (state->speed() == 0) {
actions->time = T(std::numeric_limits<double>::infinity());
} else {
const MaliputRailcarParams<T>& params =
this->template GetNumericParameter<MaliputRailcarParams>(context, 0);
const LaneDirection& lane_direction =
context.template get_abstract_state<LaneDirection>(0);
const T& s = state->s();
const T& speed = state->speed();
const maliput::api::Lane* lane = lane_direction.lane;
const bool with_s = lane_direction.with_s;
DRAKE_ASSERT(lane != nullptr);
// Computes `s_dot`, the time derivative of `s`.
const T sigma_v = cond(with_s, speed, -speed);
const LanePosition motion_derivatives =
lane_direction.lane->EvalMotionDerivatives(
LanePosition(s, CalcR(params, lane_direction), params.h()),
IsoLaneVelocity(sigma_v, 0 /* rho_v */, 0 /* eta_v */));
const T s_dot = motion_derivatives.s();
const T distance = cond(with_s, T(lane->length()) - s, -s);
actions->time = context.get_time() + distance / s_dot;
}
// Gracefully handle the situation when the next update time is equal to the
// current time. Since the integrator requires that the next update time be
// strictly greater than the current time, a small time epsilon is used.
if (actions->time == context.get_time()) {
actions->time = context.get_time() + kTimeEpsilon;
}
actions->events.push_back(systems::DiscreteEvent<T>());
actions->events.back().action =
systems::DiscreteEvent<T>::kUnrestrictedUpdateAction;
}
template <typename T>
void MaliputRailcar<T>::DoCalcUnrestrictedUpdate(
const systems::Context<T>& context,
systems::State<T>* next_state) const {
const MaliputRailcarState<T>* const current_railcar_state =
dynamic_cast<const MaliputRailcarState<T>*>(
&context.get_continuous_state_vector());
DRAKE_ASSERT(current_railcar_state != nullptr);
const LaneDirection& current_lane_direction =
context.template get_abstract_state<LaneDirection>(0);
DRAKE_ASSERT(current_lane_direction.lane != nullptr);
const bool current_with_s = current_lane_direction.with_s;
const double current_s = current_railcar_state->s();
const double current_length = current_lane_direction.lane->length();
// Copies the present state into the new one.
next_state->CopyFrom(context.get_state());
ContinuousState<T>* cs = next_state->get_mutable_continuous_state();
DRAKE_ASSERT(cs != nullptr);
VectorBase<T>* cv = cs->get_mutable_vector();
DRAKE_ASSERT(cv != nullptr);
MaliputRailcarState<T>* const next_railcar_state =
dynamic_cast<MaliputRailcarState<T>*>(cv);
DRAKE_ASSERT(next_railcar_state != nullptr);
// Handles the case where no lane change or speed adjustment is necessary. No
// lane change is necessary when the vehicle is more than epilon away from the
// next lane boundary.
if ((current_with_s && current_s < current_length - kLaneEndEpsilon) ||
(!current_with_s && current_s > kLaneEndEpsilon)) {
return;
}
// Sets the speed to be zero if the car is at or is after the end of the road.
if (current_with_s) {
const int num_branches = current_lane_direction.lane->
GetOngoingBranches(LaneEnd::kFinish)->size();
if (num_branches == 0 && current_s >= current_length - kLaneEndEpsilon) {
next_railcar_state->set_speed(0);
}
} else {
const int num_branches = current_lane_direction.lane->
GetOngoingBranches(LaneEnd::kStart)->size();
if (num_branches == 0 && current_s <= kLaneEndEpsilon) {
next_railcar_state->set_speed(0);
}
}
if (next_railcar_state->speed() != 0) {
LaneDirection& next_lane_direction =
next_state->template get_mutable_abstract_state<LaneDirection>(0);
// TODO(liang.fok) Generalize the following to support the selection of
// non-default branches or non-zero ongoing branches. See #5702.
std::unique_ptr<LaneEnd> next_branch;
if (current_with_s) {
next_branch = current_lane_direction.lane->GetDefaultBranch(
LaneEnd::kFinish);
if (next_branch == nullptr) {
const maliput::api::LaneEndSet* ongoing_lanes =
current_lane_direction.lane->GetOngoingBranches(LaneEnd::kFinish);
if (ongoing_lanes != nullptr) {
if (ongoing_lanes->size() > 0) {
next_branch = std::make_unique<LaneEnd>(ongoing_lanes->get(0));
}
}
}
} else {
next_branch = current_lane_direction.lane->GetDefaultBranch(
LaneEnd::kStart);
if (next_branch == nullptr) {
const maliput::api::LaneEndSet* ongoing_lanes =
current_lane_direction.lane->GetOngoingBranches(LaneEnd::kStart);
if (ongoing_lanes != nullptr) {
if (ongoing_lanes->size() > 0) {
next_branch = std::make_unique<LaneEnd>(ongoing_lanes->get(0));
}
}
}
}
if (next_branch == nullptr) {
DRAKE_ABORT_MSG("MaliputRailcar::DoCalcUnrestrictedUpdate: ERROR: "
"Vehicle should switch lanes but no default or ongoing branch "
"exists.");
} else {
next_lane_direction.lane = next_branch->lane;
if (next_branch->end == LaneEnd::kStart) {
next_lane_direction.with_s = true;
next_railcar_state->set_s(0);
} else {
next_lane_direction.with_s = false;
next_railcar_state->set_s(next_lane_direction.lane->length());
}
}
}
}
// This section must match the API documentation in maliput_railcar.h.
template class MaliputRailcar<double>;
} // namespace automotive
} // namespace drake