CarModel.cpp
Go to the documentation of this file.
1 
8 
9 CarModel::CarModel(const double dAxis, const ros::Time& time,
10  std::vector<double> initialPose, const double vMax,
11  const double angleMax, const double speedMax,
12  const double steeringMax)
13  : lastUpdate(time), vMax(vMax), angleMax(angleMax), speedMax(speedMax),
14  steeringMax(steeringMax)
15 {
16  // init kinematic model
17  fwdKin = ForwardKinematics(dAxis, initialPose);
18  // set steering to quasi neutral
19  setSteering(8);
20  // start with stationary state
21  velocity = 0;
22  distance = 0;
23  timeStep = 0;
24  pose = std::vector<double>(3, 0);
25  angularVelocity = 0;
26 }
27 
28 const pose_ptr CarModel::getUpdate(const int newSteering, const int newSpeed,
29  const ros::Time& time)
30 {
31  timeStep = (time - lastUpdate).toSec();
32  lastUpdate = time;
33  double ds = velocity * timeStep;
34  double oldYaw = pose[2];
36  distance += std::fabs(ds);
37  double prevV = velocity;
38  speedToVelocity(newSpeed);
39  setAngularVelocity(oldYaw);
40  setSteering(newSteering);
43  return std::make_shared<std::vector<double>>(pose);
44 }
45 
47  const ros::Time& time)
48 {
49  timeStep = (time - lastUpdate).toSec();
50  lastUpdate = time;
51  double ds = velocity * timeStep;
52  double oldYaw = pose[2];
54  distance += std::fabs(ds);
55  double prevV = velocity;
56  setVelocity(cmdVel.linear.x);
57  setAngularVelocity(oldYaw);
58  angleToSteering(fwdKin.radToDeg(cmdVel.angular.z));
61  return std::make_shared<std::vector<double>>(pose);
62 }
63 
64 const double CarModel::getTimeStep() const { return timeStep; }
65 
66 const double CarModel::getDistance() const { return distance; }
67 
68 const double CarModel::getVelocity() const { return velocity; }
69 
70 const int CarModel::getSteering() const { return steering; }
71 const double CarModel::getSteeringAngle() const { return steeringAngle; }
72 
73 const double CarModel::getAngularVelocity() const { return angularVelocity; }
74 
75 const double CarModel::getVx() const { return v_x; }
76 const double CarModel::getVy() const { return v_y; }
77 const double CarModel::getAx() const { return a_x; }
78 const double CarModel::getAy() const { return a_y; }
79 
80 void CarModel::angleToSteering(const double alpha)
81 {
82  if (alpha > angleMax)
83  {
85  }
86  else if (alpha < -angleMax)
87  {
89  }
90  else
91  {
92  setSteering((alpha / angleMax) * steeringMax);
93  }
94 }
95 void CarModel::setVelocity(const double newVel)
96 {
97  if (newVel > vMax)
98  {
100  }
101  else if (newVel < -vMax)
102  {
104  }
105  else
106  {
107  speedToVelocity((newVel / vMax) * speedMax);
108  }
109 }
110 
111 void CarModel::speedToVelocity(const int speed)
112 {
113  velocity = (speed / speedMax) * vMax;
114 }
115 void CarModel::setAngularVelocity(const double oldYaw)
116 {
117  double dTh = pose[2] - oldYaw;
118  if (dTh < -fwdKin.PI)
119  {
120  dTh += 2 * fwdKin.PI;
121  }
122  else if (dTh > fwdKin.PI)
123  {
124  dTh -= 2 * fwdKin.PI;
125  }
126  angularVelocity = dTh / timeStep;
127 }
129 {
130  if (steering > steeringMax)
131  {
132  this->steering = steeringMax;
133  steeringToAngle();
134  }
135  else if (steering < -steeringMax)
136  {
137  this->steering = -steeringMax;
138  steeringToAngle();
139  }
140  else
141  {
142  this->steering = steering;
143  steeringToAngle();
144  }
145 }
147 {
149 }
151 {
152  v_x = velocity * std::cos(pose[2]);
153  v_y = velocity * std::sin(pose[2]);
154 }
155 void CarModel::setAccelerationComponents(const double prevV)
156 {
157  a_x = (velocity - prevV) / timeStep;
158  if (steeringAngle == 0)
159  {
160  a_y = 0;
161  }
162  else
163  {
164  double sign = (steeringAngle > 0) ? 1 : -1;
165  a_y = sign * velocity * velocity / fwdKin.getRadius();
166  }
167 }
double v_x
Definition: CarModel.h:154
const pose_ptr getUpdate(const int newSteering, const int newSpeed, const ros::Time &time)
Update the simulation with current control settings and time.
Definition: CarModel.cpp:28
double angleMax
Definition: CarModel.h:143
void setAccelerationComponents(const double prevV)
Calculate and set current acceleration components in x and y direction.
Definition: CarModel.cpp:155
CarModel(const double dAxis, const ros::Time &time, std::vector< double > initialPose=std::vector< double >(3, 0), const double vMax=2.0, const double angleMax=22.5, const double speedMax=20, const double steeringMax=50)
CarModel constructor.
Definition: CarModel.cpp:9
std::shared_ptr< std::vector< double > > pose_ptr
shortcut for a pointer to a vector of doubles containing pose information
Definition: CarModel.h:26
void angleToSteering(const double alpha)
Conversion from given steering angle to current steering level based on a lookup table.
Definition: CarModel.cpp:80
double distance
Definition: CarModel.h:150
void setAngularVelocity(const double oldYaw)
Calculate and set angular velocity by providing the orientation of the previous time step...
Definition: CarModel.cpp:115
const double getVy() const
Get current velocity in y direction of the mobile plattform relative to the global coordinate system...
Definition: CarModel.cpp:76
const double getAy() const
Get current acceleration in y direction of the mobile plattform relative to the robot coordinate syst...
Definition: CarModel.cpp:78
double velocity
Definition: CarModel.h:149
void speedToVelocity(const int speed)
Conversion from given motor level to current velocity based on a lookup table.
Definition: CarModel.cpp:111
const int getSteering() const
Get current steering level of the mobile plattform.
Definition: CarModel.cpp:70
void setVelocityComponents()
Calculate and set current velocity components in x and y direction.
Definition: CarModel.cpp:150
std::vector< double > pose
Definition: CarModel.h:151
int steering
Definition: CarModel.h:146
double steeringMax
Definition: CarModel.h:145
double radToDeg(double angle)
Conversion from radiants to degrees.
ros::Time lastUpdate
Definition: CarModel.h:141
Header file for the CarModel class.
void steeringToAngle()
Conversion from current steering level to steering angle based on a lookup table. ...
Definition: CarModel.cpp:146
const double getTimeStep() const
Get current time step.
Definition: CarModel.cpp:64
double steeringAngle
Definition: CarModel.h:147
double timeStep
Definition: CarModel.h:148
const double getAngularVelocity() const
Get current angular velocity of the mobile plattform.
Definition: CarModel.cpp:73
const double getAx() const
Get current acceleration in x direction of the mobile plattform relative to the robot coordinate syst...
Definition: CarModel.cpp:77
const double getSteeringAngle() const
Get current steering angle of the mobile plattform.
Definition: CarModel.cpp:71
std::vector< double > & getUpdate(double alpha, double distance)
Get the current position and orientation.
void setVelocity(const double newVel)
Set current velocity.
Definition: CarModel.cpp:95
ForwardKinematics fwdKin
Definition: CarModel.h:140
double vMax
Definition: CarModel.h:142
double a_x
Definition: CarModel.h:158
double speedMax
Definition: CarModel.h:144
double v_y
Definition: CarModel.h:156
const double getVx() const
Get current velocity in x direction of the mobile plattform relative to the global coordinate system...
Definition: CarModel.cpp:75
const double getRadius() const
Get the radius of the current robot tracjectory around the instantaneous center of curvature (ICC)...
The ForwardKinematics class simulates kinematic behavoir of a car like mobile platform given its curr...
const double getDistance() const
Get total travelled distance of the mobile plattform.
Definition: CarModel.cpp:66
double angularVelocity
Definition: CarModel.h:153
const double getVelocity() const
Get current velocity of the mobile plattform.
Definition: CarModel.cpp:68
void setSteering(const int steering)
Set current steering level.
Definition: CarModel.cpp:128
const pose_ptr getUpdateTwist(const twist_msg cmd_vel, const ros::Time &time)
Update the simulation with current motion parameters and time.
Definition: CarModel.cpp:46
double a_y
Definition: CarModel.h:160
double degToRad(double angle)
Conversion from degrees to radiants.
geometry_msgs::Twist twist_msg
shortcut for the twist message
Definition: CarModel.h:20


pses_simulation
Author(s): Sebastian Ehmes
autogenerated on Wed Dec 13 2017 19:37:54