def main():

    actor_list = []
    verboseIsEnabled = None
    try:
        """
        Section for starting the client and connecting to the server
        """
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        for arg in sys.argv:
            if (arg == '--verbose'):
                verboseIsEnabled = True

        if verboseIsEnabled:
            print('client version: %s' % client.get_client_version())
            print('server version: %s' % client.get_server_version())
            print('client to server connection status: {}'.format(
                client.get_server_version()))

            print('Retrieving the world data from server...')

        world = client.get_world()
        if verboseIsEnabled:
            print('{} \n'.format(world))
        """
        Section for retrieving the blueprints and spawn the actors
        """
        blueprint_library = world.get_blueprint_library()
        if verboseIsEnabled:
            print('\nRetrieving CARLA blueprint library...')
            print('\nobject: %s\n\nblueprint methods: %s\n\nblueprint list:' %
                  (type(blueprint_library), dir(blueprint_library)))
            for blueprint in blueprint_library:
                print(blueprint)

        audi_blueprint = blueprint_library.find('vehicle.audi.tt')
        print('\n%s\n' % audi_blueprint)

        color = '191,191,191'
        audi_blueprint.set_attribute('color', color)

        transform = carla.Transform(carla.Location(x=10.5, y=-1.8, z=38.5),
                                    carla.Rotation(yaw=0.0))

        vehicleEgo = world.spawn_actor(audi_blueprint, transform)
        actor_list.append(vehicleEgo)
        print('created %s' % vehicleEgo.type_id)

        color = random.choice(
            audi_blueprint.get_attribute('color').recommended_values)
        audi_blueprint.set_attribute('color', color)
        """
        Section for initializing the PID testing
        """
        user_input_sp = None
        while (not isinstance(user_input_sp, int)) and (not isinstance(
                user_input_sp, float)):
            user_input_sp = input('Enter the desired Setpoint:\n')
        data = TestData(DataInit.total_duration, DataInit.sampling_period)
        start = time.time()

        print(
            '\nStarting test:\n\n' +
            'Time(s) current_vel(m/s) setpoint_vel(m/s) throttle(%) pid_demand'
        )
        time.sleep(2.5)
        print(
            '.................................................................\n'
        )
        time.sleep(1)

        # raise SystemExit

        p = PID(DataInit.K['Kp'], DataInit.K['Ki'], DataInit.K['Kd'])
        p.setPoint(user_input_sp)
        p.Integrator_min = -5
        p.Integrator_max = 40
        pid = 0
        for _ in range(
                int(DataInit.total_duration / DataInit.sampling_period) + 1):
            measurement_value = vehicleEgo.get_velocity().x
            vehicleEgo.apply_control(carla.VehicleControl(
                pid)) if 1 > pid > 0 else vehicleEgo.apply_control(
                    carla.VehicleControl(1))
            if 0 > pid:
                vehicleEgo.apply_control(carla.VehicleControl(brake=abs(pid)))
            pid = p.update(measurement_value)
            data.append_data(round(time.time() - start, 2), p.getSetPoint(),
                             round(vehicleEgo.get_velocity().x, 5),
                             p.getError())
            time.sleep(DataInit.sampling_period)

            print('%0.3f\t%0.2f\t\t\t%0.2f\t\t%0.2f\t%0.2f' %
                  (time.time() - start, vehicleEgo.get_velocity().x,
                   p.set_point, vehicleEgo.get_control().throttle, pid))

        data.plot()
        print('\nError Mean (Steady State):\n' + str(
            round(
                np.absolute(
                    np.mean(data.error[data.error.shape[0] /
                                       2:data.error.shape[0]])), 5) * 100) +
              '%\n')

    finally:
        print('destroying actors')
        for actor in actor_list:
            actor.destroy()
        print('done.')
def main():

    actor_list = []
    verboseIsEnabled = None
    try:
        """
        Section for starting the client and connecting to the server
        """
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        for arg in sys.argv:
            if (arg == '--verbose'):
                verboseIsEnabled = True

        if verboseIsEnabled:
            print('client version: %s' % client.get_client_version())
            print('server version: %s' % client.get_server_version())
            print('client to server connection status: {}'.format(client.get_server_version()))

            print('Retrieving the world data from server...')

        world = client.get_world()
        if verboseIsEnabled:
            print('{} \n'.format(world))

        """
        Section for retrieving the blueprints and spawn the actors
        """
        blueprint_library = world.get_blueprint_library()
        if verboseIsEnabled:
            print('\nRetrieving CARLA blueprint library...')
            print('\nobject: %s\n\nblueprint methods: %s\n\nblueprint list:' % (type(blueprint_library), dir(blueprint_library)) )
            for blueprint in blueprint_library:
                print(blueprint)

        audi_blueprint = blueprint_library.find('vehicle.audi.tt')
        print('\n%s\n' % audi_blueprint)

        color = '191,191,191'
        audi_blueprint.set_attribute('color', color)

        transform = carla.Transform(
			carla.Location(
                x=10.5, y=-1.8,
            z=38.5),carla.Rotation(yaw=0.0)
		)

        vehicleEgo = world.spawn_actor(audi_blueprint, transform)
        actor_list.append(vehicleEgo)
        print('created %s' % vehicleEgo.type_id)

        color = random.choice(audi_blueprint.get_attribute('color').recommended_values)
        audi_blueprint.set_attribute('color', color)


        """
        Section for initializing the PID testing
        """
        spd_profile = SpeedProfile()
        spd_data = spd_profile.input_file()

        print('\nStarting test:\n\n' + 'Time(s) current_vel(m/s) setpoint_vel(m/s) throttle(%) pid_demand')
        time.sleep(1.25)
        print('.................................................................\n')
        time.sleep(.5)

        input_sp = 0
        data = TestData(DataInit.total_duration, DataInit.sampling_period, spd_data)
        pid = 0
        cycle_counter = 1
        init_index = 0
        moving_list = []

        p = PID(
                DataInit.K['Kp'], 
                DataInit.K['Ki'],
                DataInit.K['Kd']
                )
        p.setPoint(input_sp)
        p.setWindup(*DataInit.windup_values)
        start = time.time()
        for _ in range(int(DataInit.total_duration / DataInit.sampling_period) + 1):
            measurement_value = vehicleEgo.get_velocity().x
            current_time = time.time()
            init_index = apply_dynamic_sp(
                p,
                cycle_counter,
                spd_data,
                current_time,
                start,
                init_index,
                moving_list,
                DataInit.response_time_offset
                )
            apply_control(pid, vehicleEgo)
            pid = p.update(measurement_value)
            data.append_data(round(current_time - start, 2), p.getSetPoint(), round(vehicleEgo.get_velocity().x, 5), p.getError())

            print('%0.3f\t%0.2f\t\t\t%0.2f\t\t%0.2f\t%0.2f' % (time.time() - start,
                                                                vehicleEgo.get_velocity().x,
                                                                p.set_point,
                                                                vehicleEgo.get_control().throttle,
                                                                pid))
            if DataInit.force_sampling_period:
                time.sleep(DataInit.sampling_period)
            cycle_counter += 1

        data.plot()
        # print('\nError Mean (if Steady State):\n' + 
        #     str(round(np.absolute(np.mean(data.error[data.error.shape[0]/2:data.error.shape[0]])), 5)) + 
        #     '%\n')

    finally:
        print('destroying actors')
        for actor in actor_list:
            actor.destroy()
        print('done.')