Exemplo n.º 1
0
def Car2(sock2,CS):
    #1. Listen on the server socket
    
    #2. Accept the connection from car1. Use the accepted socket to receive input below
    
    for step in range(CS[1].maxSteps, 0, -1):#The simulation steps

    #3. Receive the speed value sent by the car 1 as a new variable Vl from the LeaderCar using the subscriber socket.

    #get sensor inputs similar to the Leader Car
    CS[1].get_servers_input()
    S, R = CS[1].S.d, CS[1].R.d #S --sensors and R--actuators

    #compute the actuations R['accel'], R['steer'],R['gear'] for the first car using PID controllers for LeaderCar
    #Vl refers to the speed S['SpeedX'] of the LeaderCar
    R['steer'] = Controller.ACCSteeringController(S)
    [acc, brake, Xr] = Controller.ACCVelocityController(Vl, S)
    R['accel'] = acc
    R['brake'] = brake
    R['gear'] = Controller.automaticGear(S)

    #4. Log the sensor and actuator data S['SpeedX'], S['SpeedY'], S['SpeedZ'], R['accel'], R['steer'], R['gear'] to a buffer.

    #Respond the actuation values to the simulator
    CS[1].respond_to_server()


#Define main which calls three threads one each for LeaderCar, FollowerCar.
def main(CS):

    #call the publisher and subscriber functions to create publisher and subscriber sockets.
    sock1 = Publisher()
    sock2 = Subscriber()

    #Create three threads to call the LeaderCar, FollowerCar and Buffer functions.
    FollowerThread = Thread(target=Car2, args = (sock2,CS))
    FollowerThread.daemon = True
    FollowerThread.start()

    #1. do the same for the other car

    #2. Join the threads

if __name__ == "__main__":
    #Adding different clients to the simulation
    CS=[TorcsEnv.Client(p=P) for P in [3001, 3002]]
    main(CS)#main function
Exemplo n.º 2
0
def drive_example(c, num):
    global accumulateSpeedTime, accumulateStrTime, targetSpeed, targetStrE, timeIntervalSpeed, timeIntervalStr
    global Vl
    S, R = c.S.d, c.R.d

    if (num == 0):
        # this is the first car controller

        # random the target speed and the target position of the first car
        if (S['curLapTime'] < .20):
            accumulateSpeedTime = 0.0
            accumulateStrTime = 0.0
            targetSpeed = random.gauss(70.0, 30.0)

            speedMin = 0.0
            speedMax = 70.0
            targetSpeed = min(speedMax, targetSpeed)
            targetSpeed = max(speedMin, targetSpeed)

            targetStrE = random.gauss(0.0, 0.5)
            strMin = -0.7
            strMax = 0.7
            targetStrE = min(strMax, targetStrE)
            targetStrE = max(strMin, targetStrE)

            timeIntervalSpeed = float(random.randint(10, 25))
            timeIntervalStr = float(random.randint(10, 25))
            #print('New Lap is Starting....', temp_speed, temp_strE)

        if (S['curLapTime'] > accumulateSpeedTime + timeIntervalSpeed):
            accumulateSpeedTime += timeIntervalSpeed
            timeIntervalSpeed = float(random.randint(10, 25))
            targetSpeed = random.gauss(70.0, 30.0)
            speedMin = 0.0
            speedMax = 70.0
            targetSpeed = min(speedMax, targetSpeed)
            targetSpeed = max(speedMin, targetSpeed)
            #print('update the target speed to', temp_speed)

        if (S['curLapTime'] > accumulateStrTime + timeIntervalStr):
            accumulateStrTime += timeIntervalStr
            timeIntervalStr = float(random.randint(10, 25))
            targetStrE = random.gauss(0.0, 0.5)
            strMin = -0.9
            strMax = 0.9
            targetStrE = min(strMax, targetStrE)
            targetStrE = max(strMin, targetStrE)

        R['steer'] = Controller.steeringControl(S, targetStrE)
        R['accel'] = Controller.speedControl(S, R, targetSpeed)
        R['gear'] = Controller.automaticGear(S)

        Vl = S[
            'speedX']  # transfer the leader car speed to the second car controller

    if (num == 1):
        # this is the second car controller
        R['steer'] = Controller.ACCSteeringController(S)
        [acc, brake] = Controller.ACCVelocityController(Vl, S)
        R['accel'] = acc
        R['brake'] = brake
        R['gear'] = Controller.automaticGear(S)
        collectData(S, R)