Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

The car's initial position is at a negative x value like -1.2 . Based on the following…

  1. User

    The car's initial position is at a negative x value like -1.2 . Based on the following information, implement an end to end PID controller.# Tasks

    1. Plot the reference in `RVIZ` as a yellow line. 
        
        $$
        Reference = y = f(x) = 
        \begin{cases}
        0, & \text{for } 0\leq x\leq 15\\
        2, & \text{for } 15\leq x\leq 30\\
        5, & \text{for } 30\leq x\leq 45\\
        10, & \text{for } 45\leq x\leq 60\\
        15, & \text{for } 60\leq x\leq 200\\
        STOP, & \text{for } 200\le x\\
        \end{cases}
        $$
        
        Marker Details:
        
        Scale → `0.1`
        
        Action → `ADD`
        
        Type → `LINE_STRIP`
        
        Frame_id → `odom`
        
    2. Control the steering of the car using a `PID` controller. 
        
        Steps
        
        1. Get current `y` of the car.
        2. Calculate y `error` to reference, `derivative` of the error and `integral` of the error.
        3. Combine the errors calculated with the `PID` methodology to generate steering input with `PID gains` [ You will tune these gains in part d. ].
        4. Command the car using calculated `steering` input.
        - Car Dynamics [ Please ensure these constraints are followed. ]
            - Speed → `2 m/s` [ Use `rosparam` to set this speed in your launch file. We should be able to change the speed to verify the response for slower and faster speeds. Parameter Name → `Vmax` Type → `double` ]
            - Steering angle → `[-25 deg, 25 deg]`
    3. Car should STOP at the end of the reference. 
        - This can be done by commanding speed of `0 m/s`
    4. Tune PID controller to get a reasonable rise time, overshoot, settling time and steady state.
        - In the context of this assignment, reasonable implies that the reference is followed with an error `≤ 0.5m` by the end of current section and steady-state error `≤ 0.1 m` [ i.e at the end of section 2 of the reference → `x = 30`, the y error `≤ 0.5m` and at `x = 200`, the y error `≤ 0.1 m` ].
        - You can plot the error using `rqt_plot` to visualize the PID parameters.
        - You can also change the speed of the car between `1 m/s` and `3 m/s` to tune your controller for all speeds between `1 m/s` to `3 m/s`. (self-study task)
        - You can calculate the performance of you controller using the `MAE` metric. (self-study task) [reasonable → `MAE ≤ 0.6` at the end of all sections. ]
        - You can also turn the integral component on after the reference stabilizes for some time. This is how most real world PID controller work. This will improve the performance of your controller. (self-study task)
    

    Deliverable → The car must be able to follow the reference with reasonable PID parameters.**

    At x = 70, the car is quite far from the provided reference. But as the reference doesn't change till x = 200, the error reduces to 0. This is what is referred to as steady-state error.

  2. Assistant

    To implement a PID controller in ROS, you can follow these high-level steps:

    1. Create a new ROS package.
    $ catkin_create_pkg pid_controller rospy nav_msgs geometry_msgs visualization_msgs tf
    
    1. Write a Python script to get the current position of the car and plot the reference.
    # pid_controller.py
    import rospy
    from nav_msgs.msg import Odometry
    from visualization_msgs.msg import Marker
    
    def position_callback(position):
        car_position = position.pose.pose.position
        x = car_position.x
        y = car_position.y
        # Perform PID control here
    
    rospy.init_node("pid_controller")
    car_position_sub = rospy.Subscriber("odom", Odometry, position_callback)
    vis_pub = rospy.Publisher("visualization_marker", Marker, queue_size=10)
    
    # Generate the reference line marker
    marker = Marker()
    marker.header.frame_id = "odom"
    marker.ns = "reference_path"
    marker.id = 0
    marker.type = marker.LINE_STRIP
    marker.action = marker.ADD
    marker.scale.x = 0.1
    marker.color.r = 1.0
    marker.color.g = 1.0
    marker.color.b = 0.0
    marker.color.a = 1.0
    marker.lifetime = rospy.Duration.from_sec(0)
    
    # Prepare.x, y points for the reference line (0, 15, 30, 45, 60, 200)
    x_points = [0, 15, 30, 45, 60, 200]
    y_points = [0, 0, 2, 5, 10, 15]
    for x, y in zip(x_points, y_points):
        point = geometry_msgs.msg.Point()
        point.x = x
        point.y = y
        marker.points.append(point)
    
    # Publish the reference line marker
    vis_pub.publish(marker)
    
    rospy.spin()
    
    1. Apply the PID control.
    # pid_controller.py
    import rospy
    from simple_pid import PID  # You may need to install the package: pip install simple-pid
    from nav_msgs.msg import Odometry
    from geometry_msgs.msg import Twist
    from visualization_msgs.msg import Marker
    
    def get_reference(car_x):
        if 0 <= car_x <= 15:
            return 0
        elif 15 <= car_x <= 30:
            return 2
        elif 30 <= car_x <= 45:
            return 5
        elif 45 <= car_x <= 60:
            return 10
        elif 60 <= car_x <= 200:
            return 15
        else:
            return None
    
    def position_callback(position):
        car_position = position.pose.pose.position
        x = car_position.x
        y = car_position.y
        # Perform PID control here
        error = y - get_reference(x)
        steering_angle = pid(error)
        control = Twist()
        control.linear.x = rospy.get_param('Vmax', 2.0)
        control.angular.z = min(max(steering_angle, -25), 25)
        control_pub.publish(control)
    
    pid = PID(1, 1, 0.5, setpoint=0)  # Tune these gains for better performance
    
    rospy.init_node("pid_controller")
    car_position_sub = rospy.Subscriber("odom", Odometry, position_callback)
    control_pub = rospy.Publisher("cmd_vel", Twist, queue_size=10)
    
    rospy.spin()
    
    1. Create a launch file for the PID controller.
    <!-- pid_controller.launch -->
    <launch>
      <param name="Vmax" value="2.0" />
      <node name="pid_controller" pkg="pid_controller" type="pid_controller.py" output="screen">
      </node>
    </launch>
    
    1. Finally, execute the launch file using roslaunch.
    $ roslaunch pid_controller pid_controller.launch
    

    This should make the car follow the given reference path. To tune the PID gains, use tools such as rqt_plot to visualize the error and manually adjust the gains in the PID initialization line. The provided example should work reasonably well, but you can optimize it for your specific task and requirements.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.