def airSimCommunicator(self): while (self.remaining > 0): time.sleep(0.02) self.remaining -= 0.02 kpDur = numpy.array([self.f(xi) for xi in self.keyPressDurations]) max_ = numpy.sqrt(numpy.sqrt(500)) throttle = (kpDur[0] - kpDur[1]) / max_ * 0.5 yaw = (kpDur[2] - kpDur[3]) / max_ * 0.5 pitch = (kpDur[4] - kpDur[5]) / max_ * 0.5 roll = (kpDur[6] - kpDur[7]) / max_ * 0.5 rcdata = airsim.RCData(0, pitch=pitch, roll=roll, throttle=throttle, yaw=yaw, is_initialized=True, is_valid=True) global client client.moveByRC(rcdata) # debug self.historicalValues = numpy.c_[self.historicalValues, [throttle, yaw, pitch, roll]]
def _step(self, action): assert self.action_space.contains( action), "%r (%s) invalid" % (action, type(action)) self.addToLog('action', action) self.stepN += 1 airgym.moveByRC(rcdata=airsim.RCData(roll=action[0], pitch=action[1], yaw=action[2], throttle=action[3], is_initialized=True, is_valid=True)) collided = airgym.take_action(action) now = airgym.getPosition() track = airgym.goal_direction(self.goal, now) if collided == True: done = True reward = -100.0 distance = np.sqrt( np.power((self.goal[0] - now.x_val), 2) + np.power((self.goal[1] - now.y_val), 2)) elif collided == 99: done = True reward = 0.0 distance = np.sqrt( np.power((self.goal[0] - now.x_val), 2) + np.power((self.goal[1] - now.y_val), 2)) else: done = False reward, distance = self.computeReward(now, track) # Youuuuu made it if distance < 3: done = True reward = 100.0 self.addToLog('reward', reward) rewardSum = np.sum(self.allLogs['reward']) self.addToLog('distance', distance) self.addToLog('track', track) # Terminate the episode on large cumulative amount penalties, # since drone probably got into an unexpected loop of some sort if rewardSum < -100: done = True sys.stdout.write( "\r\x1b[K{}/{}==>reward/depth: {:.1f}/{:.1f} \t {:.0f} {:.0f}". format(self.episodeN, self.stepN, reward, rewardSum, track, action)) sys.stdout.flush() info = {"x_pos": now.x_val, "y_pos": now.y_val} self.state = airgym.getMultirotorState() return self.state, reward, done, info
def thread_obdelava(q): global hist global histVhod global done chunkN = 0 while(True): try: data = q.get() vhod = normaliziraj_vektorsko(numpy.fromstring(data, numpy.int16)) vhod = vhod.flatten()[0:vhod.size:2] # najprej iscemo prvi vzorec manjsi od -0.5, nato alterniramo med manjsi/vecji vecOd05 = False threshhold = -0.5 downs = numpy.zeros(9) ups = numpy.zeros(9) kanal = 0 for i in range(0, CHUNK, 1): if (kanal >= 9): break if (vecOd05): if (vhod[i] > threshhold): ups[kanal] = i #prepare for next kanal += 1 vecOd05 = False i += 5 else: if (vhod[i] < threshhold): downs[kanal] = i # prepare for next vecOd05 = True i += 5 kanali = numpy.zeros(8) for i in range(0, 8, 1): kanali[i] = downs[i + 1] - ups[i] MAX_KANAL = 71 MIN_KANAL = 26 RANGE = MAX_KANAL - MIN_KANAL global client # throttle = (kanali[2] - MIN_KANAL)/RANGE-0.5 # print(throttle) client.moveByRC(rcdata=airsim.RCData(throttle=(kanali[2] - MIN_KANAL)/RANGE-0.5, yaw=(kanali[3] - MIN_KANAL)/RANGE-0.5, pitch=( kanali[1] - MIN_KANAL)/RANGE - 0.5, roll=(kanali[0] - MIN_KANAL)/RANGE - 0.5, is_initialized=True, is_valid=True)) hist = numpy.c_[hist, kanali.T] for k in range(CHUNK): histVhod[k + chunkN * CHUNK] = vhod[k] chunkN += 1 q.task_done() except Exception as e: logging.exception("error get") break return None
For connecting to the AirSim drone environment and testing API functionality """ import setup_path import airsim import os import tempfile import pprint # connect to the AirSim simulator client = airsim.MultirotorClient() client.confirmConnection() client.enableApiControl(True) client.armDisarm(True) state = client.getMultirotorState() s = pprint.pformat(state) print("state: %s" % s) client.moveByManualAsync(vx_max=1E6, vy_max=1E6, z_min=-1E6, duration=1E10) airsim.wait_key( 'Manual mode is setup. Press any key to send RC data to takeoff') client.moveByRC(rcdata=airsim.RCData( pitch=0.0, throttle=1.0, is_initialized=True, is_valid=True)) airsim.wait_key('Set Yaw and pitch to 0.5') client.moveByRC(rcdata=airsim.RCData( roll=1100, throttle=2000, yaw=0.0, is_initialized=True, is_valid=True))
if (thread_obd is not None): done = True thread_obd.join() q.join() al = autolander.AutoLander(0.55, 13, 0.4) altitude = client.simGetVehiclePose().z_val historicalAltitudes.append(altitude) now = timer() al.addHeightMeasurement(now - start, altitude) throttle = al.thrust / 13 - 0.5 client.moveByRC( airsim.RCData(throttle=throttle, is_initialized=True, is_valid=True)) time.sleep(0.002) stream.stop_stream() stream.close() p.terminate() keyRecorder.poor_mans_destructor() plt.plot(histVhod) plt.show() plt.plot(historicalAltitudes) plt.show() for i in range(8):