In each iteration, a set of 50 (x,y) values is sent to the simulator for moving the car. The points that were not used by the simulator in the last iteration reused for this iteration. The rest (50 - number of points not used in the last iteration) are sampled from a trajectory generated by the spline.h
library on the following set of points:
- some points from the last iteration, for a smooth transition:
spline_X.push_back(previous_path_x[len_previous_path - 5]);
spline_Y.push_back(previous_path_y[len_previous_path - 5]);
spline_X.push_back(previous_path_x[len_previous_path - 1]);
spline_Y.push_back(previous_path_y[len_previous_path - 1]);
- some points within the same lane (or in case of lange change, the desired lane):
spline_X.push_back(
getXY(cur_s + future_values[0], optimal_d, this->landmarks)[0]);
spline_Y.push_back(
getXY(cur_s + future_values[0], optimal_d, this->landmarks)[1]);
spline_X.push_back(
getXY(cur_s + future_values[1], optimal_d, this->landmarks)[0]);
spline_Y.push_back(
getXY(cur_s + future_values[1], optimal_d, this->landmarks)[1]);
spline_X.push_back(
getXY(cur_s + future_values[2], optimal_d, this->landmarks)[0]);
spline_Y.push_back(
getXY(cur_s + future_values[2], optimal_d, this->landmarks)[1]);
Here,
cur_s
is the end of the last iteration's generated path (or the car's s value in case of first iteration),future_values
is a vector of steps, which differs from the case where we want to change lanes (future_values = {50, 70, 90};
), to the case where we go straight (future_values = {13, 20, 35, 60};
). This leads to a smoother experience when changing lanes the reference points for the spline are farther away. On the other hand, the smaller distance in the case of going straight assures that the vehicle stays in it's lane.
After the spline is generated, points are generated in a way, that max acceleration of 10 m/s^2 and max jerk of 10 m/s^3 are not violated. For this some calculations lead to the constraints that
double max_x_for_jerk_max = x_prev + 0.02 * 0.02 * 0.02 * jerk_max
+ 2 * dist_prev + dist_prevprev;
double max_x_for_accel_max = x_prev + dist_prev
+ 0.02 * 0.02 * accel_max;
where x_prev is the last generated x value.
By evaluating the sensor_fusion vector, we determine all cars that are driving close ahead of us in our lane. We then check, if we need to decelerate:
if (!is_in_current_lane(v.d))
continue;
if (distance(v.x, v.y, car_x, car_y) < MIN_DISTANCE_TO_OTHERS
&& distance(car_x, car_y, v.x, v.y) < cur_min_distance
&& vehicle_is_in_front(v) && vehicle_is_slower(v))
{
As soon as we are decelerating we need to think about chaning lanes. Therefore we set a flag to change lanes if speed is too low:
if (previous_lane == -1 && cur_min_speed_mph < 45) {
must_change_lanes = true;
not_yet_changed_lanes = true;
}
previous_lane
is set to the previous lane. This is supposed to keep the car from immediately switching back to a lane it just came from. previous_lane
is reset (to -1
) after a certain amount of iterations to not block the car indefinitely.
As soon as we need to change lanes, we check the adjacent lanes for safe transitioning. Therefore we loop through all other vehicles and check if they're in our target lane (or close to it, in case they also area bout to shift lanes...)
bool PathGenerator::check_lane_safe(int lane_number) {
Vehicle v;
for (auto const& vehicle_map_entry : vehicles) {
v = vehicle_map_entry.second;
if (!is_near_same_lane(v.d, lane_number))
continue;
if (distance(current_path_x, current_path_y, v.x, v.y)
< MIN_DISTANCE_TO_OTHERS_OVERTAKING
&& !(v.s < car_s - 10 && getNorm(v.vx, v.vy) < car_speed - 5)) {
return false;
}
}
return true;
}
During the process of chaning lanes, another flag, not_yet_changed_lanes
is set to true
and only reset to false
, if the car's d
value has reached approximately the value corresponding to the center of the lane it wanted to transition to. This is done to prevent changing lanes forth and back during a lane change cycle.
Self-Driving Car Engineer Nanodegree Program
There's a simulator which contains the Path Planning Project, download here: [releases tab (https://github.com/udacity/self-driving-car-sim/releases/tag/T3_v1.2).
To run the simulator on Mac/Linux, first make the binary file executable with the following command:
sudo chmod u+x {simulator_file_name}
In this project th goal was to safely navigate around a virtual highway with other traffic that is driving +-10 MPH of the 50 MPH speed limit. Provided is the car's localization and sensor fusion data, there is also a sparse map list of waypoints around the highway. The car should try to go as close as possible to the 50 MPH speed limit, which means passing slower traffic when possible, note that other cars will try to change lanes too. The car must avoid hitting other cars at all cost as well as driving inside of the marked road lanes at all times, unless going from one lane to another. The car should be able to make one complete loop around the 6946m highway. Since the car is trying to go 50 MPH, it should take a little over 5 minutes to complete 1 loop. Also the car should not experience total acceleration over 10 m/s^2 and jerk that is greater than 10 m/s^3.
Each waypoint in the list contains [x,y,s,dx,dy] values. x and y are the waypoint's map coordinate position, the s value is the distance along the road to get to that waypoint in meters, the dx and dy values define the unit normal vector pointing outward of the highway loop.
The highway's waypoints loop around so the frenet s value, distance along the road, goes from 0 to 6945.554.
- Clone this repo.
- Make a build directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./path_planning
.
Here is the data provided from the Simulator to the C++ Program
["x"] The car's x position in map coordinates
["y"] The car's y position in map coordinates
["s"] The car's s position in frenet coordinates
["d"] The car's d position in frenet coordinates
["yaw"] The car's yaw angle in the map
["speed"] The car's speed in MPH
//Note: Return the previous list but with processed points removed, can be a nice tool to show how far along the path has processed since last time.
["previous_path_x"] The previous list of x points previously given to the simulator
["previous_path_y"] The previous list of y points previously given to the simulator
["end_path_s"] The previous list's last point's frenet s value
["end_path_d"] The previous list's last point's frenet d value
["sensor_fusion"] A 2d vector of cars and then that car's [car's unique ID, car's x position in map coordinates, car's y position in map coordinates, car's x velocity in m/s, car's y velocity in m/s, car's s position in frenet coordinates, car's d position in frenet coordinates.
-
The car uses a perfect controller and will visit every (x,y) point it recieves in the list every .02 seconds. The units for the (x,y) points are in meters and the spacing of the points determines the speed of the car. The vector going from a point to the next point in the list dictates the angle of the car. Acceleration both in the tangential and normal directions is measured along with the jerk, the rate of change of total Acceleration. The (x,y) point paths that the planner recieves should not have a total acceleration that goes over 10 m/s^2, also the jerk should not go over 50 m/s^3. (NOTE: As this is BETA, these requirements might change. Also currently jerk is over a .02 second interval, it would probably be better to average total acceleration over 1 second and measure jerk from that.
-
There will be some latency between the simulator running and the path planner returning a path, with optimized code usually its not very long maybe just 1-3 time steps. During this delay the simulator will continue using points that it was last given, because of this its a good idea to store the last points you have used so you can have a smooth transition. previous_path_x, and previous_path_y can be helpful for this transition since they show the last points given to the simulator controller with the processed points already removed. You would either return a path that extends this previous path or make sure to create a new path that has a smooth transition with this last path.
- cmake >= 3.5
- All OSes: click here for installation instructions
- make >= 4.1
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - [install Xcode command line tools]((https://developer.apple.com/xcode/features/)
- Windows: recommend using MinGW
- uWebSockets
- Run either
install-mac.sh
orinstall-ubuntu.sh
. - If you install from source, checkout to commit
e94b6e1
, i.e.git clone https://github.com/uWebSockets/uWebSockets cd uWebSockets git checkout e94b6e1
- Run either