def pid(setpoint, ylist, previous_e, integral, Kp, Ki, Kd): """"PID controller to smoothly go to target(setpoint)""" # get measured value measured_value = lr.findy(lr.xlist(ylist)[-1] + 1, lr.xlist(ylist), ylist) # reset previous error previous_error = previous_e error = abs(setpoint - measured_value) # set newintegral newintegral = integral + error # I and D integral = (integral + error) / 0.1 derivative = (error - previous_error) / 0.1 # output and tuning output = (Kp * error) + (Ki * integral) + (Kd * derivative) return output, measured_value, error, newintegral
def difference(data, change, maxchange): """"returns te amount time to change""" # receive data target = data[0][0] time_list = data[1][0][-6:] # make prediction for bedtime. predicted_bt = lr.findy( lr.xlist(time_list)[-1] + 1, lr.xlist(time_list), time_list) # Calculate bedtime offset to bedtime target. diff = target - predicted_bt bt_change = change * diff bt_change = abs(bt_change) # restrict to not go over the maxchange if bt_change > maxchange: bt_change = maxchange return predicted_bt, bt_change, maxchange
def draw_pid_grapth(target, pid_data, errors, error_dict): """"draw graph""" # make x tiks x = lr.xlist(pid_data) # draw graph plt.plot(x, pid_data, label="with PID") plt.plot(x, errors, label="no PID") plt.axhline(y=target, linewidth=1, color='k') plt.scatter(list(error_dict.keys()), list(error_dict.values()), color='black') # make labels plt.xlabel("Time in days") plt.ylabel("diviation from target") plt.legend(loc='upper left') plt.show()
def integral(data): """"returns te amount time to change""" # set n for number of squars in riemann sum n = 100 # receive data ylist = data[1][0] # calculate delta x, the 7 stands for the start of the PID influance deltax = len(ylist) - 0 / n #integral: i = 0 intgrl = 0 while i < len(ylist[7:]): # get y y = lr.findy(i, lr.xlist(ylist[7:]), ylist[7:]) - ylist[6] # calculate integral area = y * deltax intgrl += area i += deltax return intgrl
def drawgrapth(profile, altered_profile): """"draw graph""" # make x tiks x = lr.xlist(altered_profile[1][0]) # make y tiks altered_data = altered_profile[1][0] random_data = cl.lst_strip_day(profile[1][0]) # make Ys ready for graph altered_data = cl.lst_strip_day(altered_data) random_data = cl.lst_strip_day(random_data) # draw graph plt.plot(x, altered_data, label="altered times") plt.plot(x, random_data, 'ro', label="unaltered times") plt.yticks(np.arange(0, 24, step=1)) plt.xticks(np.arange(0, 61, step=7)) # make labels plt.xlabel("Time in days") plt.ylabel("time of day") plt.legend(loc='upper left') plt.show()