-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a parameter-free unicycle car system and agent
- Loading branch information
1 parent
d0e7ae1
commit bd22e35
Showing
21 changed files
with
1,088 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @file src/agents/simple_car | ||
* | ||
* Copyright 2020 Toyota Research Institute | ||
*/ | ||
/***************************************************************************** | ||
** Includes | ||
*****************************************************************************/ | ||
|
||
#include "agents/unicycle_car.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "backend/ign_subscriber_system.h" | ||
#include "gen/simple_car_state.h" | ||
#include "systems/unicycle_car.h" | ||
#include "translations/ign_angular_rate_acceleration_command_to_drake.h" | ||
|
||
#include <maliput/common/maliput_unused.h> | ||
|
||
/***************************************************************************** | ||
** Namespaces | ||
*****************************************************************************/ | ||
|
||
namespace delphyne { | ||
|
||
/***************************************************************************** | ||
** Implementation | ||
*****************************************************************************/ | ||
|
||
UnicycleCarBlueprint::UnicycleCarBlueprint(const std::string& name, double x, double y, double heading, double speed) | ||
: BasicAgentBlueprint(name), initial_conditions_(x, y, heading, speed) {} | ||
|
||
std::unique_ptr<Agent::Diagram> UnicycleCarBlueprint::DoBuildDiagram( | ||
const maliput::api::RoadGeometry* road_geometry) const { | ||
maliput::common::unused(road_geometry); | ||
|
||
AgentBlueprint::DiagramBuilder builder(this->name()); | ||
|
||
/********************* | ||
* Context | ||
*********************/ | ||
typedef SimpleCarState<double> ContextContinuousState; | ||
ContextContinuousState context_continuous_state; | ||
context_continuous_state.set_x(initial_conditions_.x); | ||
context_continuous_state.set_y(initial_conditions_.y); | ||
context_continuous_state.set_heading(initial_conditions_.heading); | ||
context_continuous_state.set_velocity(initial_conditions_.speed); | ||
|
||
/********************* | ||
* Unicycle Car System | ||
*********************/ | ||
typedef UnicycleCar<double> UnicycleCarSystem; | ||
UnicycleCarSystem* simple_car_system = | ||
builder.AddSystem(std::make_unique<UnicycleCarSystem>(context_continuous_state)); | ||
simple_car_system->set_name(this->name() + "_system"); | ||
|
||
/********************* | ||
* Teleop Systems | ||
*********************/ | ||
std::string command_channel = "teleop/" + this->name(); | ||
typedef IgnSubscriberSystem<ignition::msgs::AutomotiveAngularRateAccelerationCommand> | ||
AngularRateAccelerationCommandSubscriber; | ||
AngularRateAccelerationCommandSubscriber* input_command_subscriber = | ||
builder.AddSystem(std::make_unique<AngularRateAccelerationCommandSubscriber>(command_channel)); | ||
|
||
auto input_command_translator = builder.AddSystem(std::make_unique<IgnAngularRateAccelerationCommandToDrake>()); | ||
|
||
// Ignition anglular rate and acceleration commands received through the subscriber are translated | ||
// to Drake. | ||
builder.Connect(*input_command_subscriber, *input_command_translator); | ||
|
||
// And then the translated Drake command is sent to the car. | ||
builder.Connect(*input_command_translator, *simple_car_system); | ||
|
||
/********************* | ||
* Diagram Outputs | ||
*********************/ | ||
builder.ExportStateOutput(simple_car_system->state_output()); | ||
builder.ExportPoseOutput(simple_car_system->pose_output()); | ||
builder.ExportVelocityOutput(simple_car_system->velocity_output()); | ||
|
||
return builder.Build(); | ||
} | ||
|
||
/***************************************************************************** | ||
** Trailers | ||
*****************************************************************************/ | ||
|
||
} // namespace delphyne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @file src/agents/simple_car.h | ||
* | ||
* Copyright 2020 Toyota Research Institute | ||
*/ | ||
/***************************************************************************** | ||
** Includes | ||
*****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include <maliput/api/road_geometry.h> | ||
#include "delphyne/mi6/agent_base_blueprint.h" | ||
|
||
/***************************************************************************** | ||
** Namespaces | ||
*****************************************************************************/ | ||
|
||
namespace delphyne { | ||
|
||
/***************************************************************************** | ||
** Interfaces | ||
*****************************************************************************/ | ||
|
||
/// @brief A very simple vehicle agent that can be teleoperated. | ||
class UnicycleCarBlueprint : public BasicAgentBlueprint { | ||
public: | ||
DELPHYNE_NO_COPY_NO_MOVE_NO_ASSIGN(UnicycleCarBlueprint) | ||
|
||
explicit UnicycleCarBlueprint(const std::string& name, double x, double y, double heading, double speed); | ||
|
||
private: | ||
// Container for the agent's initial configuration. | ||
// | ||
// Note: this is independent of whatever computational mechanisms | ||
// are used internally and is a useful construct for recording and | ||
// logging / streaming to debug configuration errors. | ||
struct InitialConditions { | ||
double x{0.0}; // scene x-coordinate (m) | ||
double y{0.0}; // scene y-coordinate (m) | ||
double heading{0.0}; // scene heading (around z-axis (radians) | ||
double speed{0.0}; // speed in axis defined by the heading (m/s) | ||
InitialConditions(double x, double y, double heading, double speed) : x(x), y(y), heading(heading), speed(speed) {} | ||
} initial_conditions_; | ||
|
||
std::unique_ptr<Agent::Diagram> DoBuildDiagram(const maliput::api::RoadGeometry* road_geometry) const override; | ||
}; | ||
|
||
/***************************************************************************** | ||
** Trailers | ||
*****************************************************************************/ | ||
|
||
} // namespace delphyne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2020 Toyota Research Institute | ||
|
||
#include "gen/angular_rate_acceleration_command.h" | ||
|
||
namespace delphyne { | ||
|
||
const int AngularRateAccelerationCommandIndices::kNumCoordinates; | ||
const int AngularRateAccelerationCommandIndices::kAngularRate; | ||
const int AngularRateAccelerationCommandIndices::kAcceleration; | ||
|
||
const std::vector<std::string>& AngularRateAccelerationCommandIndices::GetCoordinateNames() { | ||
static const maliput::common::never_destroyed<std::vector<std::string>> coordinates(std::vector<std::string>{ | ||
"angular_rate", // BR | ||
"acceleration", // BR | ||
}); | ||
return coordinates.access(); | ||
} | ||
|
||
} // namespace delphyne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright 2020 Toyota Research Institute | ||
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <Eigen/Core> | ||
|
||
#include <drake/common/drake_bool.h> | ||
#include <drake/common/dummy_value.h> | ||
#include <drake/common/symbolic.h> | ||
#include <drake/systems/framework/basic_vector.h> | ||
|
||
#include <maliput/common/maliput_never_destroyed.h> | ||
|
||
// TODO(jwnimmer-tri) Elevate this to drake/common. | ||
#if __has_cpp_attribute(nodiscard) | ||
#define DRAKE_VECTOR_GEN_NODISCARD [[nodiscard]] // NOLINT(whitespace/braces) | ||
#else | ||
#define DRAKE_VECTOR_GEN_NODISCARD | ||
#endif | ||
|
||
namespace delphyne { | ||
|
||
/// Describes the row indices of a AngularRateAccelerationCommand. | ||
struct AngularRateAccelerationCommandIndices { | ||
/// The total number of rows (coordinates). | ||
static const int kNumCoordinates = 2; | ||
|
||
// The index of each individual coordinate. | ||
static const int kAngularRate = 0; | ||
static const int kAcceleration = 1; | ||
|
||
/// Returns a vector containing the names of each coordinate within this | ||
/// class. The indices within the returned vector matches that of this class. | ||
/// In other words, `AngularRateAccelerationCommandIndices::GetCoordinateNames()[i]` | ||
/// is the name for `BasicVector::GetAtIndex(i)`. | ||
static const std::vector<std::string>& GetCoordinateNames(); | ||
}; | ||
|
||
/// Specializes BasicVector with specific getters and setters. | ||
template <typename T> | ||
class AngularRateAccelerationCommand final : public drake::systems::BasicVector<T> { | ||
public: | ||
/// An abbreviation for our row index constants. | ||
typedef AngularRateAccelerationCommandIndices K; | ||
|
||
/// Default constructor. Sets all rows to their default value: | ||
/// @arg @c angular_rate defaults to 0.0 rad. | ||
/// @arg @c acceleration defaults to 0.0 m/s^2. | ||
AngularRateAccelerationCommand() : drake::systems::BasicVector<T>(K::kNumCoordinates) { | ||
this->set_angular_rate(0.0); | ||
this->set_acceleration(0.0); | ||
} | ||
|
||
// Note: It's safe to implement copy and move because this class is final. | ||
|
||
/// @name Implements CopyConstructible, CopyAssignable, MoveConstructible, | ||
/// MoveAssignable | ||
//@{ | ||
AngularRateAccelerationCommand(const AngularRateAccelerationCommand& other) | ||
: drake::systems::BasicVector<T>(other.values()) {} | ||
AngularRateAccelerationCommand(AngularRateAccelerationCommand&& other) noexcept | ||
: drake::systems::BasicVector<T>(std::move(other.values())) {} | ||
AngularRateAccelerationCommand& operator=(const AngularRateAccelerationCommand& other) { | ||
this->values() = other.values(); | ||
return *this; | ||
} | ||
AngularRateAccelerationCommand& operator=(AngularRateAccelerationCommand&& other) noexcept { | ||
this->values() = std::move(other.values()); | ||
other.values().resize(0); | ||
return *this; | ||
} | ||
//@} | ||
|
||
/// Create a symbolic::Variable for each element with the known variable | ||
/// name. This is only available for T == symbolic::Expression. | ||
template <typename U = T> | ||
typename std::enable_if<std::is_same<U, drake::symbolic::Expression>::value>::type SetToNamedVariables() { | ||
this->set_angular_rate(drake::symbolic::Variable("angular_rate")); | ||
this->set_acceleration(drake::symbolic::Variable("acceleration")); | ||
} | ||
|
||
AngularRateAccelerationCommand<T>* DoClone() const final { return new AngularRateAccelerationCommand; } | ||
|
||
/// @name Getters and Setters | ||
//@{ | ||
/// The desired angular rate; positive results in the vehicle turning left. | ||
/// @note @c angular_rate is expressed in units of rad/s. | ||
const T& angular_rate() const { return this->GetAtIndex(K::kAngularRate); } | ||
void set_angular_rate(const T& angular_rate) { | ||
ThrowIfEmpty(); | ||
this->SetAtIndex(K::kAngularRate, angular_rate); | ||
} | ||
/// Fluent setter that matches angular_rate(). | ||
/// Returns a copy of `this` with angular_rate set to a new value. | ||
DRAKE_VECTOR_GEN_NODISCARD | ||
AngularRateAccelerationCommand<T> with_angular_rate(const T& angular_rate) const { | ||
AngularRateAccelerationCommand<T> result(*this); | ||
result.set_angular_rate(angular_rate); | ||
return result; | ||
} | ||
/// The signed acceleration, positive means speed up; negative means slow | ||
/// down, but should not move in reverse. | ||
/// @note @c acceleration is expressed in units of m/s^2. | ||
const T& acceleration() const { | ||
ThrowIfEmpty(); | ||
return this->GetAtIndex(K::kAcceleration); | ||
} | ||
// Setter that matches acceleration(). | ||
void set_acceleration(const T& acceleration) { | ||
ThrowIfEmpty(); | ||
this->SetAtIndex(K::kAcceleration, acceleration); | ||
} | ||
/// Fluent setter that matches acceleration(). | ||
/// Returns a copy of `this` with acceleration set to a new value. | ||
DRAKE_VECTOR_GEN_NODISCARD | ||
AngularRateAccelerationCommand<T> with_acceleration(const T& acceleration) const { | ||
AngularRateAccelerationCommand<T> result(*this); | ||
result.set_acceleration(acceleration); | ||
return result; | ||
} | ||
//@} | ||
|
||
/// See AngularRateAccelerationCommandIndices::GetCoordinateNames(). | ||
static const std::vector<std::string>& GetCoordinateNames() { | ||
return AngularRateAccelerationCommandIndices::GetCoordinateNames(); | ||
} | ||
|
||
/// Returns whether the current values of this vector are well-formed. | ||
drake::boolean<T> IsValid() const { | ||
using std::isnan; | ||
drake::boolean<T> result{true}; | ||
result = result && !isnan(angular_rate()); | ||
result = result && !isnan(acceleration()); | ||
return result; | ||
} | ||
|
||
private: | ||
void ThrowIfEmpty() const { | ||
if (this->values().size() == 0) { | ||
throw std::out_of_range( | ||
"The AngularRateAccelerationCommand vector has been moved-from; " | ||
"accessor methods may no longer be used"); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace delphyne | ||
|
||
#undef DRAKE_VECTOR_GEN_NODISCARD |
Oops, something went wrong.