Beispiel #1
0
	def pid_episode(self, init_pid):	
		pid = PID()
		pid.set_pid(init_pid[0], init_pid[1], init_pid[2])

		for i in range(4000):
			if i==0:
				pid.pid0(self.objective_theta-self.theta)
			elif i==1:
				pid.pid1(self.objective_theta-self.theta)
			else:
				pid.pid(self.objective_theta-self.theta)
			self.step(pid.output)
		
		reward = (self.objective_theta-self.theta)**2+self.omiga**2
		return reward

if __name__ == "__main__":
    import sys
    sys.path.append('..')
    from tool.plot import plot_curve
    from controller.pid import PID
    from controller.pi import PI

    ip = InvertedPendulum()
    ip.reset(0.0, 0.0, 0.1, 0.0)
    pid = PID()
    pid.set_pid(36, 30, 10)

    ####
    T = range(4000)
    X = []
    for i in T:
        if i == 0:
            pid.pid0(0.1 - 0.0)
        elif i == 1:
            pid.pid1(ip.theta - 0.00)
        else:
            pid.pid(ip.theta - 0.00)

        ip.step(pid.output)
        X.append(ip.x)
    ####

    plot_curve(np.array(T) * ip.dt, X)
    import sys
    sys.path.append('..')
    from tool.plot import plot_curve
    from controller.pid import PID
    from controller.pi import PI

    ip = InvertedPendulum()
    ip.reset(0.0, 0.0, 0.1, 0.0)
    pid = PID()
    pid.set_pid(40, 30, 10)

    ####
    T = range(4000)
    X = []
    theta = 0.5
    for i in T:
        if i == 0:
            pid.pid0(0.1 - theta)
        elif i == 1:
            pid.pid1(ip.theta - theta)
        else:
            pid.pid(ip.theta - theta)

        ip.step(pid.output)
        X.append(ip.theta)
    ####
    reward = (ip.theta - theta)**2 + ip.omiga**2
    print reward

    plot_curve(np.array(T) * ip.dt, X)