def get_pid():
    print "pid is set"
    pid = PID(0.001, 0.0000005, 0.0000005,setpoint=0.0)
    pid.setpoint = 0.0
    pid.sample_time = 0.01
    pid.auto_mode = True
    pid.output_limits = (-0.7, 0.7)
    pid.proportional_on_measurement  =  True
    return pid
Esempio n. 2
0
def reflow_app():
    global oven_state
    global temp_0
    global temp_0_rate
    global Button_Start_pressed
    global command_transfer
    global cycle_started
    timer1 = time.time()

    speed = 1850

    #Controls temperature rate.
    #######################################
    pid_rate1 = PID(3, 0.01, 0.1, setpoint=1)
    pid_rate1.output_limits = (-5, 5)
    pid_rate1.proportional_on_measurement = False
    pid_rate1_F = 100
    pid_rate1_max = 0
    pid_rate1_min = 730
    #######################################
    #Controls the actual temperature of soaking time
    pid_temp1 = PID(.5, 0.01, 0.2, setpoint=140)
    pid_temp1.output_limits = (-2, .5)
    pid_temp1.proportional_on_measurement = False
    pid_temp1_F = 100
    timer_soak = time.time()  #30-90 seconds 30-120 acceptable
    #######################################
    #Controls the actual temperature of soaking time
    pid_rate2 = PID(1.3, 0.02, 0.2, setpoint=.6)
    pid_rate2.output_limits = (-5, 5)
    pid_rate2.proportional_on_measurement = False
    pid_rate2_F = 100
    timer_dripping = time.time()  #30-90 seconds 30-120 acceptable
    #######################################
    #Timestamps
    TimeAboveLiquidus = .07  #Also used to time the reflow 45-60 seconds
    Soak_last_pos = 0

    pid_dt = time.time()

    QThread.msleep(3000)
    application.ui.label_test.setText(f"State: 0. Ready.")

    while 1:
        #First wait for the start button
        #Then start the sequence
        #either use the preheat zone and prewarm to 100
        #Then go to the critical zone (my cabezone)
        #When there use the
        QThread.msleep(50)
        pid_dt = time.time() - timer1
        if (time.time() - timer1) > 1000 / 1000:
            timer1 = time.time()
            #print(timer1)
            temp = temp_0

            #application.ui.label_test.setText(f"State: {oven_state}")

            ###########################################################
            if oven_state == 0:
                ###########################################################
                application.ui.label_test.setText(f"State: 0. Ready.")

                if Button_Start_pressed == 1:
                    Button_Start_pressed = 0
                    reinit_graph()
                    new_position = application.ui.spinBox_preheat.value()
                    print(
                        f"Oven program started, Position preheat: {new_position} Speed {speed}"
                    )
                    print(
                        str.encode('G1X' + str(new_position) + 'F' +
                                   str(speed) + '\n'))
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
                    application.ui.label_test.setText(f"State: 1 : Preheating")
                    oven_state = 1
                    global cycle_started
                    cycle_started = True
            ###########################################################
            elif oven_state == 1:  #Preheat
                ###########################################################
                application.ui.label_test.setText(
                    f"State: 1 : Preheating: {(60-temp):,.2f}")

                if temp > 60:
                    oven_state = 2
                    new_position = application.ui.spinBox_critical.value()
                    print(
                        f"Moving to critical point: {new_position} Speed {speed}"
                    )
                    print(
                        str.encode('G1X' + str(new_position) + 'F' +
                                   str(speed) + '\n'))
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
            ###########################################################

            elif oven_state == 2:  #Waiting to arrive to critical zone
                ###########################################################
                new_position = application.ui.spinBox_critical.value()
                application.ui.label_test.setText(
                    f"State: 2 : Moving to Critical {(new_position-position):,.2f}mm"
                )
                if position >= new_position - 2:  #waiting until we arrive to new location..
                    oven_state = 3
                    print("Arrived to Critical point")
                    application.ui.label_test.setText(f"State: 2 : Arrived!")
            ###########################################################
            elif oven_state == 3:  #Increasing temp to 135 at 1C/s (to SoakZone)
                ###########################################################
                #warming from Preheat to Soaking between 140-150C, 130-170C Acceptable.
                #Here we will use a PID loop jog to the desired value (130C) at rate <2C/s
                #After the that is achieve we will just to the next loop.
                """Add PID loop here"""
                pid_rate1.sample_time = pid_dt
                output = pid_rate1(
                    temp_0_rate
                )  #<-----------------------------------------------
                command_transfer = 'G1X' + str(
                    int(position + output)) + 'F' + str(pid_rate1_F) + '\n'
                print("PID Rate 1 output:", output)
                application.ui.label_test.setText(
                    f"State: 3 : Heating to soaking T-0= {(130-temp):,.2f} PID {output:,.2f}"
                )
                Soak_last_pos = position + output
                if temp > 130:
                    oven_state = 4
                    print("Temp of 130 reached")
                    timer_soak = time.time()
                    application.ui.label_test.setText(
                        f"State: 3 : Arrived to SoakZone!")
                    pid_temp1.set_auto_mode(True, last_output=0)
            ###########################################################
            elif oven_state == 4:  #Soaking stage. 45-60s Recommended. 30-100s Acceptable
                ###########################################################
                #Here the setpoing is changed to maintain the temp between 140-150 using PWM value
                #When the timer is done
                """Cascade PID loop"""
                pid_temp1.sample_time = pid_dt
                output = pid_temp1(
                    temp_0)  #<-----------------------------------------------

                pid_rate1.setpoint = output
                pid_rate1.sample_time = pid_dt
                output2 = pid_rate1(
                    temp_0_rate
                )  #<-----------------------------------------------
                if position + output2 > 730:
                    command_transfer = 'G1X' + str(
                        int(position +
                            output2)) + 'F' + str(pid_rate1_F) + '\n'

                #command_transfer = 'G1X'+str(int(position+output))+'F'+str(pid_temp1_F)+'\n'
                #print(f"PID Temp 1 {output:,.2f}, T-0 = {(time.time() - timer_soak):,.2f}")
                application.ui.label_test.setText(
                    f"State: 4 : Soaking: t-0 = {45-(time.time() - timer_soak):,.2f} PID{output:,.2f}->{output2:,.2f}"
                )

                if time.time() - timer_soak > 45:  #45 seconds?
                    oven_state = 5
                    application.ui.label_test.setText(f"State: 4 : Timeout!")
                    command_transfer = 'G1X' + str(Soak_last_pos) + 'F' + str(
                        pid_rate1_F) + '\n'
            ###########################################################
            elif oven_state == 5:  #Here we slowly move the car at rate of .5-1Cs recommend up to 2.4C/s is acceptable.
                ###########################################################
                """Add PID loop here"""
                pid_rate2.sample_time = pid_dt
                output = pid_rate2(
                    temp_0_rate
                )  #<-----------------------------------------------
                command_transfer = 'G1X' + str(
                    int(position + output)) + 'F' + str(pid_rate2_F) + '\n'
                print("PID Rate 2 output:", output)
                application.ui.label_test.setText(
                    f"State: 5 : Heating to Peak t-0= {(210-temp):,.2f} PID {output:,.2f}"
                )

                if temp >= 183:
                    if TimeAboveLiquidus == .07:  #just to know we haven't been here
                        TimeAboveLiquidus = time.time()
                        print("###Warning above liquidus temp!###")
                        pid_rate2.setpoint = 1.2
                        pid_rate2.Ki = .1

                if temp > 210:
                    oven_state = 6
                    application.ui.label_test.setText(f"State: 5 : Done!")
                    new_position = application.ui.spinBox_maxTravel.value()
                    print(
                        str.encode('G1X' + str(new_position) + 'F' +
                                   str(speed) + '\n'))
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
                    #maybe stop the car just in case
            ###########################################################
            elif oven_state == 6:  #here we just move the car to the reflow position and wait.
                ###########################################################
                #We wait around 30-60 seconds...
                application.ui.label_test.setText(
                    f"State: 6 : reflowing t-0= {30-(time.time() - TimeAboveLiquidus):,.2f}"
                )

                if time.time() - TimeAboveLiquidus > 30:
                    oven_state = 7
                    new_position = application.ui.spinBox_dripping.value()
                    print(
                        str.encode('G1X' + str(new_position) + 'F' +
                                   str(speed) + '\n'))
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
                    application.ui.label_test.setText(f"State: 7 : dripping")
                #send the command to the dripping position.
            ###########################################################
            elif oven_state == 7:  #Here we waiting until we arrive to the dripping
                ###########################################################
                new_position = application.ui.spinBox_dripping.value()
                #We wait around 30-60 seconds...
                application.ui.label_test.setText(
                    f"State: 7 : Waiting for drip... Moving?->{abs(position - new_position)}"
                )

                if abs(position - new_position
                       ) < 11:  #waiting until we arrive to new location..
                    oven_state = 3
                    print("Arrived to dripping")
                    application.ui.label_test.setText(
                        f"State: 7 : Arrived to dripping!")
                    oven_state = 8
                    timer_dripping = time.time()
            ###########################################################
            elif oven_state == 8:  #here we just move the car to the reflow position and wait.
                ###########################################################
                application.ui.label_test.setText(
                    f"State: 8 : Dripping t-O : {time.time() - timer_dripping:,.2f}"
                )
                #We wait around 15 seconds...
                if time.time() - timer_dripping > 25:
                    oven_state = 9
                    application.ui.label_test.setText(f"State: 8 : Timeout!")
                    new_position = application.ui.spinBox_coolDown.value()
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
            ###########################################################
            elif oven_state == 9:  #Here we waint until it cools down to <80
                ###########################################################

                application.ui.label_test.setText(
                    f"State: 9 : Cooling down {(80-temp):,.2f}")
                if temp < 80:
                    application.ui.label_test.setText(
                        f"State: 9 : Cool down completed! DONE!")
                    oven_state = 0
                    cycle_started = False
                    new_position = application.ui.spinBox_home.value()
                    command_transfer = 'G1X' + str(new_position) + 'F' + str(
                        speed) + '\n'
                #we Finish save data whatever and send card to home.

        if close_everything == 1:
            return 0
Esempio n. 3
0

def read_settings(fn):
    with open(fn) as f:
        params = json.load(f)
    return params  # (tunings["Kp"], tunings["Ki"], tunings["Kd"])


if __name__ == '__main__':

    log = tl.LogFile()

    settings_fn = "settings.json"
    settings = read_settings(settings_fn)
    settings_1 = settings["PID_1"]
    settings_2 = settings["PID_2"]

    pid_tunings = (settings_1["Kp"], settings_1["Ki"], settings_1["Kd"])

    pid = PID()
    pid.sample_time = updatePIDInt  # /100 # settings["sample_time"]
    pid.output_limits = (0, 100)
    pid.tunings = pid_tunings
    pid.setpoint = 25.0
    pid.auto_mode = True
    pid.proportional_on_measurement = False

    print(pid.tunings)

    HomebrewApp().run()
t1 = 0
x1, y1, y2, px, py = [], [], [],[],[]
#PID
#Se establece el setpoint del PID
setpx=10
setpy=7
#Se crea con la libreria simple_pid el pid para cada eje
pid1 = PID(0.35, 0.1, 0.2, setpoint=setpx)
pid2 = PID(0.35, 0.1, 0.2, setpoint=setpy)
#Se establecen sus limites de salida
pid1.output_limits = (-10, 10)
pid2.output_limits=(-10,10)
#Se establece el tiempo de muestreo
pid1.sample_time = 0.01
pid2.sample_time=0.01
pid1.proportional_on_measurement = False
pid2.proportional_on_measurement = False

#-------------VIsion----------------
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
    help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,
    help="max buffer size")
args = vars(ap.parse_args())
#Se define el color de la pelota
colorLower = np.array([0, 100, 0])
colorUpper = np.array([179, 255, 255])

pts = deque(maxlen=args["buffer"])
 
Esempio n. 5
0
altIMU.setAccelEnable(True)
altIMU.setCompassEnable(True)

# Get minimum poll interval
poll_interval = altIMU.IMUGetPollInterval()
print(poll_interval)

# PID coefficients - still need to be tuned
KP = 15
KI = 0
KD = 0
startPoint = -10.85
f = open("setting.txt", "r")
pid = PID(KP, KI, KD, setpoint=startPoint)
pid.output_limits = (-127, 127)
pid.proportional_on_measurement = True
# Initialize some values
lastFusionRollX = 0.0
Ivalue = 0.0
orginal = 0
orginalX = 0
count = 0
while True:
    if (count == 1 / 0.02):
        count = 0
        f = open("setting.txt", "r")
        setting = f.read().split()
        startPoint = float(setting[0])
        KP = float(setting[1])
        KI = float(setting[2])
        KD = float(setting[3])