示例#1
0
def runSim():
    print('sim started')
    ## SET-UP SIM ##
    # Spawn a trike
    batch = []
    blueprint = random.choice(blueprints)
    if blueprint.has_attribute('color'):
        color = random.choice(
            blueprint.get_attribute('color').recommended_values)
        blueprint.set_attribute('color', color)
    blueprint.set_attribute('role_name', 'autopilot')

    random.shuffle(spawn_points)
    batch.append(SpawnActor(blueprint, spawn_points[0]))

    for response in client.apply_batch_sync(batch):
        if response.error:
            logging.error(response.error)
        else:
            vehicle = response.actor_id

    # Attach nmeaSensor to trike (speed directly taken from trike actor)
    actor = world.get_actors().find(vehicle)
    for x in world.get_actors():
        if vehicle == x.id:
            actor = x
    #print(vehicle)
    #print(world.get_actors())
    #next((x for x in world.get_actors() if x.id == vehicle), None)

    logger = DataLogger(actor)
    blueprint = world.get_blueprint_library().find('sensor.other.gnss')
    transform = carla.Transform(carla.Location(x=0.8, z=1.7))

    nmeaSensor = world.spawn_actor(blueprint, transform, actor)
    sensors.append(nmeaSensor)
    #speedSensor = world.spawn_actor(blueprint,transform,actor)

    ### SET SIM VIEWPOINT TO VEHICLE ####
    world.get_spectator().set_transform(actor.get_transform())
    #################

    ## RUN SIM ##
    try:
        ser.write('f'.encode('utf-8'))  #Notify Due that system is starting

        ##### FOR GPS SENSOR USAGE ####
        logger.setListen()
        logger.setNmea('Test@')  # Fill private member to avoid error
        nmeaSensor.listen(lambda data: nmeaGPS.consoleout(data, logger))
        ##########################

        while True:
            # Wait for ready queue from due
            if ser.in_waiting:
                ### FOR GPS SENSOR USAGE (Set the stop variable to short the listener func) ###
                logger.setStopListen()
                #nmeaSensor.stop()
                ###########
                ''' 
                ADD SERIAL READS FOR ACTUATION HERE 
                
                Currently just clears an arbitrary char in buffer sent from
                router Due
                '''

                ## Receive in order: throttle, steer, brake
                t = float(ser.readline().decode('ASCII'))
                s = float(ser.readline().decode('ASCII'))
                b = float(ser.readline().decode('ASCII'))

                actor.apply_control(
                    carla.VehicleControl(throttle=t, steer=s, brake=b))
                ''' 
                Finish processing actuation commands here
                
                Here's how data is sent from Due:
                - Throttle : float (-1 to 1)
                - Steering : float (-1 to 1)
                - Brakes   : float (0 for off 0.3 for on) <- because current implementation of brake
                    is siimply on/off.  Feel free to change on value of 0.3.
                '''

                ### ACCESS/SEND LAT/LONG FROM LAST TICK ###
                ### Can use for location if disabling GPS Sensor

                #geo = world.get_map().transform_to_geolocation(logger.actorId.get_location())
                #msg = geo.__str__() + '@'
                #logger.setNmea(msg)
                ###########

                ### ACCESS/SEND X/Y/Z FROM LAST TICK  #####
                ### Can use for location if disabling GPS Sensor

                #msg = logger.actorId.get_location().__str__() + '@'
                #logger.setNmea(msg)
                ########

                # Get the speed of Trike
                getSpeed(logger.actorId, logger)

                # Send most current data
                # ORDER MATTERS FOR ARDUINO CODE
                logger.sendCyclometer(ser)
                logger.sendNmea(ser)

                ### FOR SENSOR USAGE ###
                logger.setListen()
                #nmeaSensor.listen(lambda data: nmeaGPS.consoleout(data,logger))
                #############

    except KeyboardInterrupt:
        print('\ndestroying %d sensors' % len(sensors))
        for x in sensors:
            carla.command.DestroyActor(x)
        print('\ndestroying vehicle')
        actor.destroy()
        return