m-lundberg / simple-pid

A simple and easy to use PID controller in Python
MIT License
799 stars 217 forks source link

Implementation is incorrect #66

Closed datonefaridze closed 2 years ago

datonefaridze commented 2 years ago

Hi, take a look at this cartpole example, it's cant solve it (actually i tried 100000 coefficients, didn't work):

import numpy as np
import gym
from simple_pid import PID

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

env = gym.make('CartPole-v1')

pid = PID(0.9, 0.1, 0, setpoint=0, sample_time=0.00001)

for i_episode in range(20):
    state = env.reset()
    integral = 0
    derivative = 0
    prev_error = 0
    for t in range(500):
        env.render()
        print("state: ", state)
        control = pid(state[2])
        print("control: ", control)
        action = sigmoid(control)
        print("action: ", action)
        action = np.round(action).astype(np.int32)
        print("action: ", action)
        state, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break
env.close()