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
def drive_example(c, num): global accumulateSpeedTime, accumulateStrTime, targetSpeed, targetStrE, timeIntervalSpeed, timeIntervalStr global Vl global trackPoslast S, R = c.S.d, c.R.d if (num == 0): # 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 #Random generation of the leader car speed targetSpeed = random.gauss(70.0, 30.0) speedMin = 0.0 speedMax = 70.0 targetSpeed = min(speedMax, targetSpeed) targetSpeed = max(speedMin, targetSpeed) #Random generation of the leader car track position targetStrE = random.gauss(0.0, 0.5) strMin = -0.7 strMax = 0.7 targetStrE = min(strMax, targetStrE) targetStrE = max(strMin, targetStrE) #Random time interval generator within the current lap timeIntervalSpeed = float(random.randint(10, 25)) timeIntervalStr = float(random.randint(10, 25)) #print('New Lap is Starting....', temp_speed, temp_strE) #Random generation of the leader car speed 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) #Random generation of the leader car track position 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) #Acctuators of the first car 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): # The second car controller #R['steer'] = Controller.ACCSteeringController(S) [acc, brake, Xr] = Controller.ACCVelocityController(Vl, S) #R['accel'] = acc #R['brake'] = brake # nn controller trackPos = Controller.trackPosCalc(S['track']) distance = min(S['opponents']) row = [ S['speedX'], S['speedY'], trackPos, trackPoslast, distance, Vl, Xr ] row = (row - dataNormalization['X_mean']) / dataNormalization['X_std'] trackPoslast = trackPos Y = Controller.nncontroller(row, model) #Y = Y * dataNormalization['Y_std'] R['steer'] = Y[0] R['accel'] = Y[1] R['brake'] = Y[2] R['gear'] = Controller.automaticGear(S)
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)