def __init__(self, role_name, width, height, node): self.role_name = role_name self.dim = (width, height) self.node = node font = pygame.font.Font(pygame.font.get_default_font(), 20) fonts = [x for x in pygame.font.get_fonts() if 'mono' in x] default_font = 'ubuntumono' mono = default_font if default_font in fonts else fonts[0] mono = pygame.font.match_font(mono) self._font_mono = pygame.font.Font(mono, 14) self._notifications = FadingText(font, (width, 40), (0, height - 40)) self.help = HelpText(pygame.font.Font(mono, 24), width, height) self._show_info = True self._info_text = [] self.vehicle_status = CarlaEgoVehicleStatus() self.vehicle_status_subscriber = node.new_subscription( CarlaEgoVehicleStatus, "/carla/{}/vehicle_status".format(self.role_name), self.vehicle_status_updated, qos_profile=10) self.vehicle_info = CarlaEgoVehicleInfo() self.vehicle_info_subscriber = node.new_subscription( CarlaEgoVehicleInfo, "/carla/{}/vehicle_info".format(self.role_name), self.vehicle_info_updated, qos_profile=QoSProfile( depth=10, durability=DurabilityPolicy.TRANSIENT_LOCAL)) self.x, self.y, self.z = 0, 0, 0 self.yaw = 0 self.latitude = 0 self.longitude = 0 self.manual_control = False self.gnss_subscriber = node.new_subscription(NavSatFix, "/carla/{}/gnss".format( self.role_name), self.gnss_updated, qos_profile=10) self.odometry_subscriber = node.new_subscription( Odometry, "/carla/{}/odometry".format(self.role_name), self.odometry_updated, qos_profile=10) self.manual_control_subscriber = node.new_subscription( Bool, "/carla/{}/vehicle_control_manual_override".format(self.role_name), self.manual_control_override_updated, qos_profile=10) self.carla_status = CarlaStatus() self.status_subscriber = node.new_subscription( CarlaStatus, "/carla/status", self.carla_status_updated, qos_profile=10)
def __init__(self, role_name, width, height): self.role_name = role_name self.dim = (width, height) font = pygame.font.Font(pygame.font.get_default_font(), 20) fonts = [x for x in pygame.font.get_fonts() if 'mono' in x] default_font = 'ubuntumono' mono = default_font if default_font in fonts else fonts[0] mono = pygame.font.match_font(mono) self._font_mono = pygame.font.Font(mono, 14) self._notifications = FadingText(font, (width, 40), (0, height - 40)) self.help = HelpText(pygame.font.Font(mono, 24), width, height) self._show_info = True self._info_text = [] self.vehicle_status = CarlaEgoVehicleStatus() self.tf_listener = tf.TransformListener() self.vehicle_status_subscriber = rospy.Subscriber( "/carla/{}/vehicle_status".format(self.role_name), CarlaEgoVehicleStatus, self.vehicle_status_updated) self.vehicle_info = CarlaEgoVehicleInfo() self.vehicle_info_subscriber = rospy.Subscriber( "/carla/{}/vehicle_info".format(self.role_name), CarlaEgoVehicleInfo, self.vehicle_info_updated) self.latitude = 0 self.longitude = 0 self.manual_control = False self.gnss_subscriber = rospy.Subscriber( "/carla/{}/gnss/gnss1/fix".format(self.role_name), NavSatFix, self.gnss_updated) self.manual_control_subscriber = rospy.Subscriber( "/carla/{}/vehicle_control_manual_override".format(self.role_name), Bool, self.manual_control_override_updated) self.carla_status = CarlaStatus() self.status_subscriber = rospy.Subscriber("/carla/status", CarlaStatus, self.carla_status_updated)
def publish_can(self, sensor_id, data): """ publish can data """ if not self.vehicle_info_publisher: self.vehicle_info_publisher = rospy.Publisher( '/carla/ego_vehicle/vehicle_info', CarlaEgoVehicleInfo, queue_size=1, latch=True) info_msg = CarlaEgoVehicleInfo() for wheel in data['wheels']: wheel_info = CarlaEgoVehicleInfoWheel() wheel_info.tire_friction = wheel['tire_friction'] wheel_info.damping_rate = wheel['damping_rate'] wheel_info.steer_angle = wheel['steer_angle'] wheel_info.disable_steering = wheel['disable_steering'] info_msg.wheels.append(wheel_info) info_msg.max_rpm = data['max_rpm'] info_msg.moi = data['moi'] info_msg.damping_rate_full_throttle = data[ 'damping_rate_full_throttle'] info_msg.damping_rate_zero_throttle_clutch_engaged info_msg.damping_rate_zero_throttle_clutch_disengaged = data[ 'damping_rate_zero_throttle_clutch_disengaged'] info_msg.use_gear_autobox = data['use_gear_autobox'] info_msg.clutch_strength = data['clutch_strength'] info_msg.mass = data['mass'] info_msg.drag_coefficient = data['drag_coefficient'] info_msg.center_of_mass.x = data['center_of_mass']['x'] info_msg.center_of_mass.y = data['center_of_mass']['y'] info_msg.center_of_mass.z = data['center_of_mass']['z'] self.vehicle_info_publisher.publish(info_msg) msg = CarlaEgoVehicleStatus() msg.header = self.get_header() msg.velocity = data['speed'] self.speed = data['speed'] #todo msg.acceleration msg.control.throttle = self.current_control.throttle msg.control.steer = self.current_control.steer msg.control.brake = self.current_control.brake msg.control.hand_brake = self.current_control.hand_brake msg.control.reverse = self.current_control.reverse msg.control.gear = self.current_control.gear msg.control.manual_gear_shift = self.current_control.manual_gear_shift self.vehicle_status_publisher.publish(msg)
def __init__(self, role_name, width, height, node): self.role_name = role_name self.dim = (width, height) self.node = node font = pygame.font.Font(pygame.font.get_default_font(), 20) fonts = [x for x in pygame.font.get_fonts() if 'mono' in x] default_font = 'ubuntumono' mono = default_font if default_font in fonts else fonts[0] mono = pygame.font.match_font(mono) self._font_mono = pygame.font.Font(mono, 14) self._notifications = FadingText(font, (width, 40), (0, height - 40)) self.help = HelpText(pygame.font.Font(mono, 24), width, height) self._show_info = True self._info_text = [] self.vehicle_status = CarlaEgoVehicleStatus() if ROS_VERSION == 1: self.tf_listener = tf.TransformListener() self.callback_group = None elif ROS_VERSION == 2: self.tf_buffer = tf2_ros.Buffer() self.tf_listener = tf2_ros.TransformListener(self.tf_buffer, node=self.node) self.callback_group = ReentrantCallbackGroup() self.vehicle_status_subscriber = node.create_subscriber( CarlaEgoVehicleStatus, "/carla/{}/vehicle_status".format(self.role_name), self.vehicle_status_updated, callback_group=self.callback_group) self.vehicle_info = CarlaEgoVehicleInfo() self.vehicle_info_subscriber = node.create_subscriber( CarlaEgoVehicleInfo, "/carla/{}/vehicle_info".format(self.role_name), self.vehicle_info_updated, callback_group=self.callback_group, qos_profile=QoSProfile(depth=10, durability=latch_on)) self.latitude = 0 self.longitude = 0 self.manual_control = False self.gnss_subscriber = node.create_subscriber( NavSatFix, "/carla/{}/gnss".format(self.role_name), self.gnss_updated, callback_group=self.callback_group) self.manual_control_subscriber = node.create_subscriber( Bool, "/carla/{}/vehicle_control_manual_override".format(self.role_name), self.manual_control_override_updated, callback_group=self.callback_group) self.carla_status = CarlaStatus() self.status_subscriber = node.create_subscriber(CarlaStatus, "/carla/status", self.carla_status_updated, callback_group=self.callback_group)
def send_vehicle_msgs(self): """ send messages related to vehicle status :return: """ vehicle_status = CarlaEgoVehicleStatus( header=self.get_msg_header("map")) vehicle_status.velocity = self.get_vehicle_speed_abs(self.carla_actor) vehicle_status.acceleration.linear = self.get_current_ros_accel( ).linear vehicle_status.orientation = self.get_current_ros_pose().orientation vehicle_status.control.throttle = self.carla_actor.get_control( ).throttle vehicle_status.control.steer = self.carla_actor.get_control().steer vehicle_status.control.brake = self.carla_actor.get_control().brake vehicle_status.control.hand_brake = self.carla_actor.get_control( ).hand_brake vehicle_status.control.reverse = self.carla_actor.get_control().reverse vehicle_status.control.gear = self.carla_actor.get_control().gear vehicle_status.control.manual_gear_shift = self.carla_actor.get_control( ).manual_gear_shift self.vehicle_status_publisher.publish(vehicle_status) # only send vehicle once (in latched-mode) if not self.vehicle_info_published: self.vehicle_info_published = True vehicle_info = CarlaEgoVehicleInfo() vehicle_info.id = self.carla_actor.id vehicle_info.type = self.carla_actor.type_id vehicle_info.rolename = self.carla_actor.attributes.get( 'role_name') vehicle_physics = self.carla_actor.get_physics_control() for wheel in vehicle_physics.wheels: wheel_info = CarlaEgoVehicleInfoWheel() wheel_info.tire_friction = wheel.tire_friction wheel_info.damping_rate = wheel.damping_rate wheel_info.max_steer_angle = math.radians( wheel.max_steer_angle) wheel_info.radius = wheel.radius wheel_info.max_brake_torque = wheel.max_brake_torque wheel_info.max_handbrake_torque = wheel.max_handbrake_torque wheel_info.position.x = (wheel.position.x/100.0) - \ self.carla_actor.get_transform().location.x wheel_info.position.y = -( (wheel.position.y / 100.0) - self.carla_actor.get_transform().location.y) wheel_info.position.z = (wheel.position.z/100.0) - \ self.carla_actor.get_transform().location.z vehicle_info.wheels.append(wheel_info) vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.moi = vehicle_physics.moi vehicle_info.damping_rate_full_throttle = vehicle_physics.damping_rate_full_throttle vehicle_info.damping_rate_zero_throttle_clutch_engaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_engaged vehicle_info.damping_rate_zero_throttle_clutch_disengaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_disengaged vehicle_info.use_gear_autobox = vehicle_physics.use_gear_autobox vehicle_info.gear_switch_time = vehicle_physics.gear_switch_time vehicle_info.clutch_strength = vehicle_physics.clutch_strength vehicle_info.mass = vehicle_physics.mass vehicle_info.drag_coefficient = vehicle_physics.drag_coefficient vehicle_info.center_of_mass.x = vehicle_physics.center_of_mass.x vehicle_info.center_of_mass.y = vehicle_physics.center_of_mass.y vehicle_info.center_of_mass.z = vehicle_physics.center_of_mass.z self.vehicle_info_publisher.publish(vehicle_info)
def send_vehicle_msgs(self): """ Function (override) to send odometry message of the ego vehicle instead of an object message. The ego vehicle doesn't send its information as part of the object list. A nav_msgs.msg.Odometry is prepared to be published :return: """ vehicle_status = CarlaEgoVehicleStatus() vehicle_status.header.stamp = self.get_current_ros_time() vehicle_status.velocity = self.get_vehicle_speed_abs(self.carla_actor) vehicle_status.acceleration = self.get_vehicle_acceleration_abs( self.carla_actor) vehicle_status.orientation = self.get_current_ros_pose().orientation vehicle_status.control.throttle = self.carla_actor.get_control( ).throttle vehicle_status.control.steer = self.carla_actor.get_control().steer vehicle_status.control.brake = self.carla_actor.get_control().brake vehicle_status.control.hand_brake = self.carla_actor.get_control( ).hand_brake vehicle_status.control.reverse = self.carla_actor.get_control().reverse vehicle_status.control.gear = self.carla_actor.get_control().gear vehicle_status.control.manual_gear_shift = self.carla_actor.get_control( ).manual_gear_shift self.publish_ros_message(self.topic_name() + "/vehicle_status", vehicle_status) if not self.vehicle_info_published: self.vehicle_info_published = True vehicle_info = CarlaEgoVehicleInfo() vehicle_info.id = self.carla_actor.id vehicle_info.type = self.carla_actor.type_id vehicle_info.rolename = self.carla_actor.attributes.get( 'role_name') vehicle_physics = self.carla_actor.get_physics_control() for wheel in vehicle_physics.wheels: wheel_info = CarlaEgoVehicleInfoWheel() wheel_info.tire_friction = wheel.tire_friction wheel_info.damping_rate = wheel.damping_rate wheel_info.steer_angle = math.radians(wheel.steer_angle) wheel_info.disable_steering = wheel.disable_steering vehicle_info.wheels.append(wheel_info) vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.moi = vehicle_physics.moi vehicle_info.damping_rate_full_throttle = vehicle_physics.damping_rate_full_throttle vehicle_info.damping_rate_zero_throttle_clutch_engaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_engaged vehicle_info.damping_rate_zero_throttle_clutch_disengaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_disengaged vehicle_info.use_gear_autobox = vehicle_physics.use_gear_autobox vehicle_info.gear_switch_time = vehicle_physics.gear_switch_time vehicle_info.clutch_strength = vehicle_physics.clutch_strength vehicle_info.mass = vehicle_physics.mass vehicle_info.drag_coefficient = vehicle_physics.drag_coefficient vehicle_info.center_of_mass.x = vehicle_physics.center_of_mass.x vehicle_info.center_of_mass.y = vehicle_physics.center_of_mass.y vehicle_info.center_of_mass.z = vehicle_physics.center_of_mass.z self.publish_ros_message(self.topic_name() + "/vehicle_info", vehicle_info, True) # @todo: do we still need this? odometry = Odometry(header=self.get_msg_header()) odometry.child_frame_id = self.get_frame_id() odometry.pose.pose = self.get_current_ros_pose() odometry.twist.twist = self.get_current_ros_twist() self.publish_ros_message(self.topic_name() + "/odometry", odometry)
def __init__(self): """ Constructor """ super(CarlaAckermannControl, self).__init__("carla_ackermann_control") # PID controller # the controller has to run with the simulation time, not with real-time # # To prevent "float division by zero" within PID controller initialize it with # a previous point in time (the error happens because the time doesn't # change between initialization and first call, therefore dt is 0) sys.modules['simple_pid.PID']._current_time = ( # pylint: disable=protected-access lambda: self.get_time() - 0.1) # we might want to use a PID controller to reach the final target speed self.speed_controller = PID(Kp=self.get_param("speed_Kp", alternative_value=0.05), Ki=self.get_param("speed_Ki", alternative_value=0.), Kd=self.get_param("speed_Kd", alternative_value=0.5), sample_time=0.05, output_limits=(-1., 1.)) self.accel_controller = PID(Kp=self.get_param("accel_Kp", alternative_value=0.05), Ki=self.get_param("accel_Ki", alternative_value=0.), Kd=self.get_param("accel_Kd", alternative_value=0.05), sample_time=0.05, output_limits=(-1, 1)) # use the correct time for further calculations sys.modules['simple_pid.PID']._current_time = ( # pylint: disable=protected-access lambda: self.get_time()) if ROS_VERSION == 1: self.reconfigure_server = Server( EgoVehicleControlParameterConfig, namespace="", callback=self.reconfigure_pid_parameters, ) if ROS_VERSION == 2: self.add_on_set_parameters_callback( self.reconfigure_pid_parameters) self.control_loop_rate = self.get_param("control_loop_rate", 0.05) self.last_ackermann_msg_received_sec = self.get_time() self.vehicle_status = CarlaEgoVehicleStatus() self.vehicle_info = CarlaEgoVehicleInfo() self.role_name = self.get_param('role_name', 'ego_vehicle') # control info self.info = EgoVehicleControlInfo() # set initial maximum values self.vehicle_info_updated(self.vehicle_info) # target values self.info.target.steering_angle = 0. self.info.target.speed = 0. self.info.target.speed_abs = 0. self.info.target.accel = 0. self.info.target.jerk = 0. # current values self.info.current.time_sec = self.get_time() self.info.current.speed = 0. self.info.current.speed_abs = 0. self.info.current.accel = 0. # control values self.info.status.status = 'n/a' self.info.status.speed_control_activation_count = 0 self.info.status.speed_control_accel_delta = 0. self.info.status.speed_control_accel_target = 0. self.info.status.accel_control_pedal_delta = 0. self.info.status.accel_control_pedal_target = 0. self.info.status.brake_upper_border = 0. self.info.status.throttle_lower_border = 0. # control output self.info.output.throttle = 0. self.info.output.brake = 1.0 self.info.output.steer = 0. self.info.output.reverse = False self.info.output.hand_brake = True # ackermann drive commands self.control_subscriber = self.new_subscription( AckermannDrive, "/carla/" + self.role_name + "/ackermann_cmd", self.ackermann_command_updated, qos_profile=10) # current status of the vehicle self.vehicle_status_subscriber = self.new_subscription( CarlaEgoVehicleStatus, "/carla/" + self.role_name + "/vehicle_status", self.vehicle_status_updated, qos_profile=10) # vehicle info self.vehicle_info_subscriber = self.new_subscription( CarlaEgoVehicleInfo, "/carla/" + self.role_name + "/vehicle_info", self.vehicle_info_updated, qos_profile=10) # to send command to carla self.carla_control_publisher = self.new_publisher( CarlaEgoVehicleControl, "/carla/" + self.role_name + "/vehicle_control_cmd", qos_profile=1) # report controller info self.control_info_publisher = self.new_publisher( EgoVehicleControlInfo, "/carla/" + self.role_name + "/ackermann_control/control_info", qos_profile=1)
def __init__(self): """ Constructor """ self.control_loop_rate = rospy.Rate(10) # 10Hz self.lastAckermannMsgReceived = datetime.datetime( datetime.MINYEAR, 1, 1) self.vehicle_status = CarlaEgoVehicleStatus() self.vehicle_info = CarlaEgoVehicleInfo() self.role_name = rospy.get_param('/carla/ackermann_control/role_name') # control info self.info = EgoVehicleControlInfo() # set initial maximum values self.vehicle_info_updated(self.vehicle_info) # target values self.info.target.steering_angle = 0. self.info.target.speed = 0. self.info.target.speed_abs = 0. self.info.target.accel = 0. self.info.target.jerk = 0. # current values self.info.current.time_sec = rospy.get_rostime().to_sec() self.info.current.speed = 0. self.info.current.speed_abs = 0. self.info.current.accel = 0. # control values self.info.status.status = 'n/a' self.info.status.speed_control_activation_count = 0 self.info.status.speed_control_accel_delta = 0. self.info.status.speed_control_accel_target = 0. self.info.status.accel_control_pedal_delta = 0. self.info.status.accel_control_pedal_target = 0. self.info.status.brake_upper_border = 0. self.info.status.throttle_lower_border = 0. # control output self.info.output.throttle = 0. self.info.output.brake = 1.0 self.info.output.steer = 0. self.info.output.reverse = False self.info.output.hand_brake = True # PID controller # the controller has to run with the simulation time, not with real-time # # To prevent "float division by zero" within PID controller initialize it with # a previous point in time (the error happens because the time doesn't # change between initialization and first call, therefore dt is 0) sys.modules['simple_pid.PID']._current_time = ( # pylint: disable=protected-access lambda: rospy.get_rostime().to_sec() - 0.1) # we might want to use a PID controller to reach the final target speed self.speed_controller = PID(Kp=0.0, Ki=0.0, Kd=0.0, sample_time=0.05, output_limits=(-1., 1.)) self.accel_controller = PID(Kp=0.0, Ki=0.0, Kd=0.0, sample_time=0.05, output_limits=(-1, 1)) # use the correct time for further calculations sys.modules['simple_pid.PID']._current_time = ( # pylint: disable=protected-access lambda: rospy.get_rostime().to_sec()) self.reconfigure_server = Server( EgoVehicleControlParameterConfig, namespace="/carla/" + self.role_name + "/ackermann_control", callback=(lambda config, level: CarlaAckermannControl. reconfigure_pid_parameters(self, config, level))) # ackermann drive commands self.control_subscriber = rospy.Subscriber( "/carla/" + self.role_name + "/ackermann_cmd", AckermannDrive, self.ackermann_command_updated) # current status of the vehicle self.vehicle_status_subscriber = rospy.Subscriber( "/carla/" + self.role_name + "/vehicle_status", CarlaEgoVehicleStatus, self.vehicle_status_updated) # vehicle info self.vehicle_info_subscriber = rospy.Subscriber( "/carla/" + self.role_name + "/vehicle_info", CarlaEgoVehicleInfo, self.vehicle_info_updated) # to send command to carla self.carla_control_publisher = rospy.Publisher( "/carla/" + self.role_name + "/vehicle_control_cmd", CarlaEgoVehicleControl, queue_size=1) # report controller info self.control_info_publisher = rospy.Publisher( "/carla/" + self.role_name + "/ackermann_control/control_info", EgoVehicleControlInfo, queue_size=1)
def send_vehicle_msgs(self): """ send messages related to vehicle status :return: """ vehicle_status = CarlaEgoVehicleStatus( header=self.get_msg_header("map")) vehicle_status.velocity = self.get_vehicle_speed_abs(self.carla_actor) vehicle_status.acceleration.linear = transforms.carla_vector_to_ros_vector_rotated( self.carla_actor.get_acceleration(), self.carla_actor.get_transform().rotation) vehicle_status.orientation = self.get_current_ros_pose().orientation vehicle_status.control.throttle = self.carla_actor.get_control( ).throttle vehicle_status.control.steer = self.carla_actor.get_control().steer vehicle_status.control.brake = self.carla_actor.get_control().brake vehicle_status.control.hand_brake = self.carla_actor.get_control( ).hand_brake vehicle_status.control.reverse = self.carla_actor.get_control().reverse vehicle_status.control.gear = self.carla_actor.get_control().gear vehicle_status.control.manual_gear_shift = self.carla_actor.get_control( ).manual_gear_shift self.publish_message(self.get_topic_prefix() + "/vehicle_status", vehicle_status) # only send vehicle once (in latched-mode) if not self.vehicle_info_published: self.vehicle_info_published = True vehicle_info = CarlaEgoVehicleInfo() vehicle_info.id = self.carla_actor.id vehicle_info.type = self.carla_actor.type_id vehicle_info.rolename = self.carla_actor.attributes.get( 'role_name') vehicle_physics = self.carla_actor.get_physics_control() for wheel in vehicle_physics.wheels: wheel_info = CarlaEgoVehicleInfoWheel() wheel_info.tire_friction = wheel.tire_friction wheel_info.damping_rate = wheel.damping_rate wheel_info.max_steer_angle = math.radians( wheel.max_steer_angle) vehicle_info.wheels.append(wheel_info) vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.max_rpm = vehicle_physics.max_rpm vehicle_info.moi = vehicle_physics.moi vehicle_info.damping_rate_full_throttle = vehicle_physics.damping_rate_full_throttle vehicle_info.damping_rate_zero_throttle_clutch_engaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_engaged vehicle_info.damping_rate_zero_throttle_clutch_disengaged = \ vehicle_physics.damping_rate_zero_throttle_clutch_disengaged vehicle_info.use_gear_autobox = vehicle_physics.use_gear_autobox vehicle_info.gear_switch_time = vehicle_physics.gear_switch_time vehicle_info.clutch_strength = vehicle_physics.clutch_strength vehicle_info.mass = vehicle_physics.mass vehicle_info.drag_coefficient = vehicle_physics.drag_coefficient vehicle_info.center_of_mass.x = vehicle_physics.center_of_mass.x vehicle_info.center_of_mass.y = vehicle_physics.center_of_mass.y vehicle_info.center_of_mass.z = vehicle_physics.center_of_mass.z self.publish_message(self.get_topic_prefix() + "/vehicle_info", vehicle_info, True) # @todo: do we still need this? odometry = Odometry(header=self.get_msg_header("map")) odometry.child_frame_id = self.get_prefix() odometry.pose.pose = self.get_current_ros_pose() odometry.twist.twist = self.get_current_ros_twist() self.publish_message(self.get_topic_prefix() + "/odometry", odometry)