Example #1
0
    def runPID(self, index):
        """It's just a simple PID that virtually runs a lot of times"""
        c = self.population[index]
        pid = PID(self.defaultPoint, *c.get(), min=0, max=255)
        print("Testing with: ", c.name, " ", *c.get())
        hal = self.hal

        hal.animations.strip_white.upload([0])
        sleep(0.1)
        hal.animations.strip_white.loopin2 = True
        hal.animations.strip_white.playing = True

        i = 0
        while i < self.timesteps:
            lux = get_lux(hal)

            res = int(pid.compute(lux, genetic=True))
            logger.info("Obs=%s, PID asks %s", lux, res)

            hal.animations.strip_white.upload([res])
            i += 1
        fit = self.genetic.fitness(pid.errors, self.score_func)
        dump_errors = [(self.defaultPoint - e, t) for e, t in pid.errors]
        self.dump.writerow([self.gen_number, index, c.kp, c.ki, c.kd,
                            fit, dump_errors])
        return fit
Example #2
0
async def pid(hal, pid=None, times=float('inf')):
    #f = open('testfile.txt','w')
    Ku = 0.015
    Tu = 0.00001

    Kp = 0.6*Ku
    Ki = (2*Ku)/Tu
    Kd = (Kp*Tu)/8

    if not pid: pid = PID(800, Kp, Ki , Kd, min=0, max=255)
    #pid = PID(800, Ku*0.6, 0 , 0, min=0, max=255)

    hal.animations.led.upload([0])
    hal.animations.led.loopin2 = True
    hal.animations.led.playing = True

    i = 0
    while times:
        mean = 0
        for _ in range(3):
            analogRead = None
            while analogRead is None:
                try:
                    analogRead = hal.sensors.lux.value
                except TypeError:
                    pass
            resistance = converters.tension2resistance(analogRead, 10000)
            lux = converters.resistance2lux(resistance, **LUXMETER)
            mean += lux
            await asyncio.sleep(0.02)
        lux = round(mean / 3, 2)

        res = int(pid.compute(lux))
        logger.info("Obs=%s, PID asks %s", lux, res)

        #toWrite = str(i) + " " + str(lux) + "\n"
        #f.write(toWrite)

        hal.animations.led.upload([res])

        await asyncio.sleep(0.5)
        i += 1

        times -= 1
    print("end loop")
Example #3
0
    def __init__(self, *args, **kwargs):
        self.hal = HAL("/tmp/hal")
        self.hal.animations.led_strip.upload([0])
        self.hal.animations.led_strip.looping = True
        self.hal.animations.led_strip.playing = True

        self.hal.animations.ventilo.upload([0])
        self.hal.animations.ventilo.looping = True
        self.hal.animations.ventilo.playing = True

        (0.14049763567229168, 0.24741248122010484, 0.008350324795815805)
        Kp=0.14049763567229168
        Ki=0.24741248122010484
        Kd=0.008350324795815805

        self.pid = PID(800, Kp, Ki, Kd, min=0, max=255)
        self.bang = BangBang(30, 5, False)
        asyncio.async(self.adjust())

        super().__init__(*args, **kwargs)
Example #4
0
class MyComponent(ApplicationSession):
    def __init__(self, *args, **kwargs):
        self.hal = HAL("/tmp/hal")
        self.hal.animations.led_strip.upload([0])
        self.hal.animations.led_strip.looping = True
        self.hal.animations.led_strip.playing = True

        self.hal.animations.ventilo.upload([0])
        self.hal.animations.ventilo.looping = True
        self.hal.animations.ventilo.playing = True

        (0.14049763567229168, 0.24741248122010484, 0.008350324795815805)
        Kp=0.14049763567229168
        Ki=0.24741248122010484
        Kd=0.008350324795815805

        self.pid = PID(800, Kp, Ki, Kd, min=0, max=255)
        self.bang = BangBang(30, 5, False)
        asyncio.async(self.adjust())

        super().__init__(*args, **kwargs)

    async def onJoin(self, details):
        while True:
            self.send_data()
            await asyncio.sleep(0.1)

    def luxmeter(self):
        analogRead = None
        while analogRead is None:
            try:
                analogRead = self.hal.sensors.lux.value
            except TypeError:
                pass
        resistance = converters.tension2resistance(analogRead, 10000)
        lux = converters.resistance2lux(resistance, **LUXMETER)
        return lux

    def thermistor(self):
        analogRead = self.hal.sensors.temp.value
        resistance = converters.tension2resistance(analogRead, 10000)
        temp = converters.resistance2celcius(resistance, **THERMISTANCE)
        return temp

    def send_data(self):
        self.publish('sensor.lux', self.luxmeter())
        #self.publish('sensor.temp', self.thermistor())

    async def adjust(self):
        while True:
            mean = 0
            for _ in range(3):
                mean += self.luxmeter()
                await asyncio.sleep(0.05)
            lux = round(mean / 3, 2)

            res = int(self.pid.compute(lux))
            self.publish('pid.light', res)

            self.hal.animations.led_strip.upload([res])
            await asyncio.sleep(0.1)